CP_Main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "MainFrm.h"
  4. #include "Misc.h"
  5. #include ".\cp_main.h"
  6. #include "server.h"
  7. #include "Client.h"
  8. #include "InternetUpdate.h"
  9. #include <io.h>
  10. #include "Path.h"
  11. #include "Clip_ImportExport.h"
  12. #include "HyperLink.h"
  13. #include "OptionsSheet.h"
  14. #include "DittoCopyBuffer.h"
  15. #include "SendKeys.h"
  16. #include "MainTableFunctions.h"
  17. #include "ShowTaskBarIcon.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. class DittoCommandLineInfo : public CCommandLineInfo
  24. {
  25. public:
  26. DittoCommandLineInfo()
  27. {
  28. m_bDisconnect = FALSE;
  29. m_bConnect = FALSE;
  30. m_bU3 = FALSE;
  31. m_bU3Stop = FALSE;
  32. m_bU3Install = FALSE;
  33. m_uacPID = 0;
  34. m_bOpenWindow = FALSE;
  35. m_bCloseWindow = FALSE;
  36. }
  37. virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast)
  38. {
  39. if(bFlag)
  40. {
  41. if(STRICMP(pszParam, _T("Connect")) == 0)
  42. {
  43. m_bConnect = TRUE;
  44. }
  45. else if(STRICMP(pszParam, _T("Disconnect")) == 0)
  46. {
  47. m_bDisconnect = TRUE;
  48. }
  49. else if(STRICMP(pszParam, _T("U3")) == 0)
  50. {
  51. m_bU3 = TRUE;
  52. }
  53. else if(STRICMP(pszParam, _T("U3appStop")) == 0)
  54. {
  55. m_bU3Stop = TRUE;
  56. }
  57. else if(STRICMP(pszParam, _T("U3Install")) == 0)
  58. {
  59. m_bU3Install = TRUE;
  60. }
  61. else if(wcsncmp(pszParam, _T("uacpaste"), 8) == 0)
  62. {
  63. CString pidCommand(pszParam);
  64. long sep = pidCommand.ReverseFind(':');
  65. if(sep > -1)
  66. {
  67. CString pid = pidCommand.Right(pidCommand.GetLength() - sep - 1);
  68. m_uacPID = ATOI(pid);
  69. }
  70. }
  71. else if(STRICMP(pszParam, _T("open")) == 0)
  72. {
  73. m_bOpenWindow = TRUE;
  74. }
  75. else if(STRICMP(pszParam, _T("close")) == 0)
  76. {
  77. m_bCloseWindow = TRUE;
  78. }
  79. }
  80. CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
  81. }
  82. BOOL m_bDisconnect;
  83. BOOL m_bConnect;
  84. BOOL m_bU3;
  85. BOOL m_bU3Stop;
  86. BOOL m_bU3Install;
  87. int m_uacPID;
  88. BOOL m_bCloseWindow;
  89. BOOL m_bOpenWindow;
  90. };
  91. CCP_MainApp theApp;
  92. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  93. //{{AFX_MSG_MAP(CCP_MainApp)
  94. // NOTE - the ClassWizard will add and remove mapping macros here.
  95. // DO NOT EDIT what you see in these blocks of generated code!
  96. //}}AFX_MSG_MAP
  97. END_MESSAGE_MAP()
  98. CCP_MainApp::CCP_MainApp()
  99. {
  100. theApp.m_activeWnd.TrackActiveWnd(false);
  101. m_pUacPasteThread = NULL;
  102. m_bAppRunning = false;
  103. m_bAppExiting = false;
  104. m_connectOnStartup = -1;
  105. m_MainhWnd = NULL;
  106. m_pMainFrame = NULL;
  107. BOOL set = ::AllowSetForegroundWindow(ASFW_ANY);
  108. m_bShowingQuickPaste = false;
  109. m_IC_bCopy = false;
  110. m_GroupDefaultID = 0;
  111. m_GroupID = -1;
  112. m_GroupParentID = 0;
  113. m_GroupText = "History";
  114. m_FocusID = -1;
  115. ClearOldGroupState();
  116. m_bAsynchronousRefreshView = true;
  117. m_lClipsSent = 0;
  118. m_lClipsRecieved = 0;
  119. m_oldtStartUp = COleDateTime::GetCurrentTime();
  120. m_bExitServerThread = false;
  121. m_lLastGoodIndexForNextworkPassword = -2;
  122. m_RTFFormat = ::RegisterClipboardFormat(_T("Rich Text Format"));
  123. m_HTML_Format = ::RegisterClipboardFormat(_T("HTML Format"));
  124. m_PingFormat = ::RegisterClipboardFormat(_T("Ditto Ping Format"));
  125. m_cfIgnoreClipboard = ::RegisterClipboardFormat(_T("Clipboard Viewer Ignore"));
  126. m_cfDelaySavingData = ::RegisterClipboardFormat(_T("Ditto Delay Saving Data"));
  127. m_RemoteCF_HDROP = ::RegisterClipboardFormat(_T("Ditto Remote CF_HDROP"));
  128. }
  129. CCP_MainApp::~CCP_MainApp()
  130. {
  131. }
  132. BOOL CCP_MainApp::InitInstance()
  133. {
  134. LoadLibrary(TEXT("riched20.dll"));
  135. AfxEnableControlContainer();
  136. AfxOleInit();
  137. AfxInitRichEditEx();
  138. afxAmbientActCtx = FALSE;
  139. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  140. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  141. DittoCommandLineInfo cmdInfo;
  142. ParseCommandLine(cmdInfo);
  143. //if starting from a u3 device we will pass in -U3Start
  144. if(cmdInfo.m_bU3)
  145. g_Opt.m_bU3 = cmdInfo.m_bU3 ? TRUE : FALSE;
  146. g_Opt.LoadSettings();
  147. if(cmdInfo.m_uacPID > 0)
  148. {
  149. Log(StrF(_T("Startup up ditto as admin to paste to admin windows, parent process id: %d"), cmdInfo.m_uacPID));
  150. CString mutex;
  151. mutex.Format(_T("DittoAdminPaste_%d"), cmdInfo.m_uacPID);
  152. m_adminPasteMutex = CreateMutex(NULL, FALSE, mutex);
  153. m_pUacPasteThread = new CUAC_Thread(cmdInfo.m_uacPID);
  154. m_pUacPasteThread->Start();
  155. m_pUacPasteThread->WaitForThreadToExit(INT_MAX);
  156. return FALSE;
  157. }
  158. if(cmdInfo.m_strFileName.IsEmpty() == FALSE)
  159. {
  160. try
  161. {
  162. g_Opt.m_bEnableDebugLogging = g_Opt.GetEnableDebugLogging();
  163. CClip_ImportExport Clip;
  164. CppSQLite3DB db;
  165. db.open(cmdInfo.m_strFileName);
  166. CClip_ImportExport clip;
  167. if(clip.ImportFromSqliteDB(db, false, true))
  168. {
  169. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Importing_Good", "Clip placed on clipboard"));
  170. }
  171. else
  172. {
  173. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"));
  174. }
  175. }
  176. catch (CppSQLite3Exception& e)
  177. {
  178. ASSERT(FALSE);
  179. CString csError;
  180. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  181. ShowCommandLineError("Ditto", csError);
  182. }
  183. return FALSE;
  184. }
  185. else if(cmdInfo.m_bConnect || cmdInfo.m_bDisconnect)
  186. {
  187. //First get the saved hwnd and send it a message
  188. //If ditt is running then this will return 1, meening the running ditto process
  189. //handled this message
  190. //If it didn't handle the message(ditto is not running) then startup this processes of ditto
  191. //disconnected from the clipboard
  192. LRESULT ret = 0;
  193. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  194. if(hWnd)
  195. {
  196. ret = ::SendMessage(hWnd, WM_SET_CONNECTED, cmdInfo.m_bConnect, cmdInfo.m_bDisconnect);
  197. }
  198. //passed off to the running instance of ditto, exit this instance
  199. if(ret == 1)
  200. {
  201. return FALSE;
  202. }
  203. if(cmdInfo.m_bConnect)
  204. {
  205. m_connectOnStartup = TRUE;
  206. }
  207. else if(cmdInfo.m_bDisconnect)
  208. {
  209. m_connectOnStartup = FALSE;
  210. }
  211. }
  212. else if(cmdInfo.m_bOpenWindow || cmdInfo.m_bCloseWindow)
  213. {
  214. //First get the saved hwnd and send it a message
  215. //If ditt is running then this will return 1, meening the running ditto process
  216. //handled this message
  217. //If it didn't handle the message(ditto is not running) then startup this processes of ditto
  218. //disconnected from the clipboard
  219. LRESULT ret = 0;
  220. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  221. if(hWnd)
  222. {
  223. ret = ::SendMessage(hWnd, WM_OPEN_CLOSE_WINDWOW, cmdInfo.m_bOpenWindow, cmdInfo.m_bCloseWindow);
  224. }
  225. return FALSE;
  226. }
  227. CInternetUpdate update;
  228. long lRunningVersion = update.GetRunningVersion();
  229. CString cs = update.GetVersionString(lRunningVersion);
  230. cs.Insert(0, _T("InitInstance - Running Version - "));
  231. Log(cs);
  232. CString csMutex("Ditto Is Now Running");
  233. if(g_Opt.m_bU3)
  234. {
  235. //If running from a U3 device then allow other ditto's to run
  236. //only prevent Ditto from running from the same device
  237. csMutex += " ";
  238. csMutex += GETENV(_T("U3_DEVICE_SERIAL"));
  239. }
  240. else if(g_Opt.GetIsPortableDitto())
  241. {
  242. csMutex += " ";
  243. csMutex += g_Opt.GetExeFileName();
  244. }
  245. m_hMutex = CreateMutex(NULL, FALSE, csMutex);
  246. DWORD dwError = GetLastError();
  247. if(dwError == ERROR_ALREADY_EXISTS)
  248. {
  249. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  250. if(hWnd)
  251. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  252. return TRUE;
  253. }
  254. CString csFile = CGetSetOptions::GetLanguageFile();
  255. if(m_Language.LoadLanguageFile(csFile) == false)
  256. {
  257. CString cs;
  258. cs.Format(_T("Error loading language file - %s - \n\n%s"), csFile, m_Language.m_csLastError);
  259. Log(cs);
  260. m_Language.LoadLanguageFile(_T("English.xml"));
  261. }
  262. //The first time we run Ditto on U3 show a web page about ditto
  263. if(g_Opt.m_bU3)
  264. {
  265. if(FileExists(CGetSetOptions::GetDBPath()) == FALSE)
  266. {
  267. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  268. csFile += "U3_Install.htm";
  269. CHyperLink::GotoURL(csFile, SW_SHOW);
  270. }
  271. }
  272. int nRet = CheckDBExists(CGetSetOptions::GetDBPath());
  273. if(nRet == FALSE)
  274. {
  275. AfxMessageBox(theApp.m_Language.GetString("Error_Opening_Database", "Error Opening Database."));
  276. return FALSE;
  277. }
  278. CMainFrame* pFrame = new CMainFrame;
  279. m_pMainWnd = m_pMainFrame = pFrame;
  280. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  281. pFrame->ShowWindow(SW_SHOW);
  282. pFrame->UpdateWindow();
  283. return TRUE;
  284. }
  285. void CCP_MainApp::AfterMainCreate()
  286. {
  287. m_MainhWnd = m_pMainFrame->m_hWnd;
  288. ASSERT( ::IsWindow(m_MainhWnd) );
  289. g_Opt.SetMainHWND((long)m_MainhWnd);
  290. //Save the HWND so the stop app can send us a close message
  291. if(g_Opt.m_bU3)
  292. {
  293. CGetSetOptions::WriteU3Hwnd(m_MainhWnd);
  294. }
  295. g_HotKeys.Init(m_MainhWnd);
  296. // create hotkeys here. They are automatically deleted on exit
  297. m_pDittoHotKey = new CHotKey(CString("DittoHotKey"), 704); //704 is ctrl-tilda
  298. m_pPosOne = new CHotKey("Position1", 0, true);
  299. m_pPosTwo = new CHotKey("Position2", 0, true);
  300. m_pPosThree = new CHotKey("Position3", 0, true);
  301. m_pPosFour = new CHotKey("Position4", 0, true);
  302. m_pPosFive = new CHotKey("Position5", 0, true);
  303. m_pPosSix = new CHotKey("Position6", 0, true);
  304. m_pPosSeven = new CHotKey("Position7", 0, true);
  305. m_pPosEight = new CHotKey("Position8", 0, true);
  306. m_pPosNine = new CHotKey("Position9", 0, true);
  307. m_pPosTen = new CHotKey("Position10", 0, true);
  308. m_pCopyBuffer1 = new CHotKey("CopyBufferCopyHotKey_0", 0, true);
  309. m_pPasteBuffer1 = new CHotKey("CopyBufferPasteHotKey_0", 0, true);
  310. m_pCutBuffer1 = new CHotKey("CopyBufferCutHotKey_0", 0, true);
  311. m_pCopyBuffer2 = new CHotKey("CopyBufferCopyHotKey_1", 0, true);
  312. m_pPasteBuffer2 = new CHotKey("CopyBufferPasteHotKey_1", 0, true);
  313. m_pCutBuffer2 = new CHotKey("CopyBufferCutHotKey_1", 0, true);
  314. m_pCopyBuffer3 = new CHotKey("CopyBufferCopyHotKey_2", 0, true);
  315. m_pPasteBuffer3 = new CHotKey("CopyBufferPasteHotKey_2", 0, true);
  316. m_pCutBuffer3 = new CHotKey("CopyBufferCutHotKey_2", 0, true);
  317. m_pTextOnlyPaste = new CHotKey("TextOnlyPaste", 0, true);
  318. LoadGlobalClips();
  319. g_HotKeys.RegisterAll();
  320. StartCopyThread();
  321. StartStopServerThread();
  322. #ifdef UNICODE
  323. m_Addins.LoadAll();
  324. #endif
  325. m_bAppRunning = true;
  326. }
  327. void CCP_MainApp::LoadGlobalClips()
  328. {
  329. try
  330. {
  331. CppSQLite3Query q = m_db.execQuery(_T("SELECT lID, lShortCut, mText FROM Main WHERE lShortCut > 0 AND globalShortCut = 1"));
  332. while(q.eof() == false)
  333. {
  334. int id = q.getIntField(_T("lID"));
  335. int shortcut = q.getIntField(_T("lShortCut"));
  336. CString desc = q.getStringField(_T("mText"));
  337. //Constructor will add to a global list and free
  338. CHotKey* globalHotKey = new CHotKey(desc, shortcut, true);
  339. if(globalHotKey != NULL)
  340. {
  341. globalHotKey->m_clipId = id;
  342. }
  343. q.nextRow();
  344. }
  345. }
  346. CATCH_SQLITE_EXCEPTION
  347. }
  348. void CCP_MainApp::StartStopServerThread()
  349. {
  350. if(CGetSetOptions::GetDisableRecieve() == FALSE && g_Opt.GetAllowFriends())
  351. {
  352. AfxBeginThread(MTServerThread, m_MainhWnd);
  353. }
  354. else
  355. {
  356. m_bExitServerThread = true;
  357. closesocket(theApp.m_sSocket);
  358. }
  359. }
  360. void CCP_MainApp::StopServerThread()
  361. {
  362. m_bExitServerThread = true;
  363. closesocket(theApp.m_sSocket);
  364. }
  365. void CCP_MainApp::BeforeMainClose()
  366. {
  367. ASSERT( m_bAppRunning && !m_bAppExiting );
  368. m_bAppRunning = false;
  369. m_bAppExiting = true;
  370. g_HotKeys.UnregisterAll();
  371. StopServerThread();
  372. StopCopyThread();
  373. }
  374. void CCP_MainApp::StartCopyThread()
  375. {
  376. ASSERT( m_MainhWnd );
  377. CClipTypes* pTypes = LoadTypesFromDB();
  378. // initialize to:
  379. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  380. // - true = use Asynchronous communication (PostMessage)
  381. // - true = enable copying on clipboard changes
  382. // - pTypes = the supported types to use
  383. m_CopyThread.Init(CCopyConfig(m_MainhWnd, true, true, pTypes));
  384. if(m_connectOnStartup == FALSE || g_Opt.GetConnectedToClipboard() == FALSE)
  385. {
  386. m_CopyThread.m_connectOnStartup = false;
  387. Log(StrF(_T("Starting Ditto up disconnected from the clipboard, commandLine: %d, saved value: %d"), m_connectOnStartup, g_Opt.GetConnectedToClipboard()));
  388. SetConnectCV(false);
  389. }
  390. else if(m_connectOnStartup == TRUE)
  391. {
  392. SetConnectCV(true);
  393. Log(_T("Starting Ditto up connected from the clipboard, passed in true from command line to start connected"));
  394. }
  395. VERIFY(m_CopyThread.CreateThread(CREATE_SUSPENDED));
  396. m_CopyThread.ResumeThread();
  397. }
  398. void CCP_MainApp::StopCopyThread()
  399. {
  400. EnableCbCopy(false);
  401. m_CopyThread.Quit();
  402. }
  403. // returns the current Clipboard Viewer Connect state (though it might not yet
  404. // be actually connected -- check IsClipboardViewerConnected())
  405. bool CCP_MainApp::ToggleConnectCV()
  406. {
  407. bool bConnect = !GetConnectCV();
  408. SetConnectCV(bConnect);
  409. return bConnect;
  410. }
  411. // Sets a menu entry according to the current Clipboard Viewer Connection status
  412. // - the menu text indicates the available command (opposite the current state)
  413. // - a check mark appears in the rare cases that the menu text actually represents
  414. // the current state, e.g. if we are supposed to be connected, but we somehow
  415. // lose that connection, "Disconnect from Clipboard" will have a check next to it.
  416. void CCP_MainApp::UpdateMenuConnectCV(CMenu* pMenu, UINT nMenuID)
  417. {
  418. if(pMenu == NULL)
  419. return;
  420. bool bConnect = theApp.GetConnectCV();
  421. CString cs;
  422. if(bConnect)
  423. {
  424. cs = theApp.m_Language.GetString("Disconnect_Clipboard", "Disconnect from Clipboard.");
  425. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  426. }
  427. else
  428. {
  429. cs = theApp.m_Language.GetString("Connect_Clipboard", "Connect to Clipboard.");
  430. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  431. }
  432. }
  433. // Allocates a new CClipTypes
  434. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  435. {
  436. CClipTypes* pTypes = new CClipTypes;
  437. try
  438. {
  439. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT TypeText FROM Types"));
  440. while(q.eof() == false)
  441. {
  442. pTypes->Add(GetFormatID(q.getStringField(_T("TypeText"))));
  443. q.nextRow();
  444. }
  445. }
  446. CATCH_SQLITE_EXCEPTION
  447. if(pTypes->GetSize() <= 0)
  448. {
  449. pTypes->Add(CF_TEXT);
  450. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  451. pTypes->Add(CF_UNICODETEXT);
  452. pTypes->Add(CF_HDROP);
  453. if(g_Opt.m_bU3 == false)
  454. {
  455. pTypes->Add(CF_DIB);
  456. }
  457. }
  458. return pTypes;
  459. }
  460. void CCP_MainApp::ReloadTypes()
  461. {
  462. CClipTypes* pTypes = LoadTypesFromDB();
  463. if(pTypes)
  464. {
  465. m_CopyThread.SetSupportedTypes(pTypes);
  466. }
  467. }
  468. void CCP_MainApp::RefreshView()
  469. {
  470. CQPasteWnd *pWnd = QPasteWnd();
  471. if(pWnd)
  472. {
  473. if(m_bAsynchronousRefreshView)
  474. {
  475. pWnd->PostMessage(WM_REFRESH_VIEW);
  476. }
  477. else
  478. {
  479. pWnd->SendMessage(WM_REFRESH_VIEW);
  480. }
  481. }
  482. }
  483. void CCP_MainApp::RefreshClipOrder(int clipId)
  484. {
  485. CQPasteWnd *pWnd = QPasteWnd();
  486. if(pWnd)
  487. {
  488. if(m_bAsynchronousRefreshView)
  489. {
  490. pWnd->PostMessage(WM_RELOAD_CLIP_ORDER, clipId, 0);
  491. }
  492. else
  493. {
  494. pWnd->SendMessage(WM_RELOAD_CLIP_ORDER, clipId, 0);
  495. }
  496. }
  497. }
  498. void CCP_MainApp::OnPasteCompleted()
  499. {
  500. }
  501. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  502. {
  503. if(count <= 0)
  504. {
  505. return;
  506. }
  507. // update copy statistics
  508. CGetSetOptions::SetTripCopyCount(-count);
  509. CGetSetOptions::SetTotalCopyCount(-count);
  510. if(m_CopyBuffer.Active())
  511. {
  512. m_CopyBuffer.EndCopy(lLastID);
  513. }
  514. RefreshView();
  515. }
  516. // Internal Clipboard for cut/copy/paste items between Groups
  517. // if NULL, this uses the current QPaste selection
  518. void CCP_MainApp::IC_Cut(ARRAY* pIDs)
  519. {
  520. if(pIDs == NULL)
  521. {
  522. if(QPasteWnd())
  523. {
  524. QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
  525. }
  526. else
  527. {
  528. m_IC_IDs.SetSize(0);
  529. }
  530. }
  531. else
  532. {
  533. m_IC_IDs.Copy(*pIDs);
  534. }
  535. m_IC_bCopy = false;
  536. if(QPasteWnd())
  537. {
  538. QPasteWnd()->UpdateStatus();
  539. }
  540. }
  541. // if NULL, this uses the current QPaste selection
  542. void CCP_MainApp::IC_Copy(ARRAY* pIDs)
  543. {
  544. if(pIDs == NULL)
  545. {
  546. if(QPasteWnd())
  547. {
  548. QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
  549. }
  550. else
  551. {
  552. m_IC_IDs.SetSize(0);
  553. }
  554. }
  555. else
  556. {
  557. m_IC_IDs.Copy(*pIDs);
  558. }
  559. m_IC_bCopy = true;
  560. RefreshView();
  561. }
  562. void CCP_MainApp::IC_Paste()
  563. {
  564. if(m_IC_IDs.GetSize() <= 0)
  565. {
  566. return;
  567. }
  568. if(m_IC_bCopy)
  569. {
  570. m_IC_IDs.CopyTo(GetValidGroupID());
  571. }
  572. else // Move
  573. {
  574. m_IC_IDs.MoveTo(GetValidGroupID());
  575. }
  576. // don't process the same items twice.
  577. m_IC_IDs.SetSize(0);
  578. RefreshView();
  579. }
  580. // Groups
  581. void CCP_MainApp::SaveCurrentGroupState()
  582. {
  583. m_oldGroupID = m_GroupID;
  584. m_oldGroupParentID = m_GroupParentID;
  585. m_oldGroupText = m_GroupText;
  586. }
  587. void CCP_MainApp::ClearOldGroupState()
  588. {
  589. m_oldGroupID = -2;
  590. m_oldGroupParentID = -2;
  591. m_oldGroupText = _T("");
  592. }
  593. BOOL CCP_MainApp::TryEnterOldGroupState()
  594. {
  595. BOOL enteredGroup = FALSE;
  596. if(m_oldGroupID > -2)
  597. {
  598. m_GroupID = m_oldGroupID;
  599. m_GroupParentID = m_oldGroupParentID;
  600. m_GroupText = m_oldGroupText;
  601. ClearOldGroupState();
  602. theApp.RefreshView();
  603. if(QPasteWnd())
  604. QPasteWnd()->UpdateStatus(true);
  605. enteredGroup = TRUE;
  606. }
  607. return enteredGroup;
  608. }
  609. BOOL CCP_MainApp::EnterGroupID(long lID, BOOL clearOldGroupState/* = TRUE*/, BOOL saveCurrentGroupState/* = FALSE*/)
  610. {
  611. BOOL bResult = FALSE;
  612. if(m_GroupID == lID)
  613. return TRUE;
  614. DWORD startTick = GetTickCount();
  615. if(clearOldGroupState)
  616. {
  617. ClearOldGroupState();
  618. }
  619. if(saveCurrentGroupState)
  620. {
  621. SaveCurrentGroupState();
  622. }
  623. // if we are switching to the parent, focus on the previous group
  624. if(m_GroupParentID == lID && m_GroupID > 0)
  625. m_FocusID = m_GroupID;
  626. switch(lID)
  627. {
  628. case -1:
  629. m_FocusID = -1;
  630. m_GroupID = -1;
  631. m_GroupParentID = -1;
  632. m_GroupText = "History";
  633. bResult = TRUE;
  634. break;
  635. default: // Normal Group
  636. try
  637. {
  638. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lParentID, mText, bIsGroup FROM Main WHERE lID = %d"), lID);
  639. if(q.eof() == false)
  640. {
  641. if(q.getIntField(_T("bIsGroup")) > 0)
  642. {
  643. m_GroupID = lID;
  644. m_GroupParentID = q.getIntField(_T("lParentID"));
  645. m_GroupText = q.getStringField(_T("mText"));
  646. bResult = TRUE;
  647. }
  648. }
  649. }
  650. CATCH_SQLITE_EXCEPTION
  651. break;
  652. }
  653. if(bResult)
  654. {
  655. theApp.RefreshView();
  656. if(QPasteWnd())
  657. QPasteWnd()->UpdateStatus(true);
  658. }
  659. DWORD endTick = GetTickCount();
  660. if((endTick-startTick) > 150)
  661. Log(StrF(_T("Paste Timing EnterParentId: %d"), endTick-startTick));
  662. return bResult;
  663. }
  664. // returns a usable group id (not negative)
  665. long CCP_MainApp::GetValidGroupID()
  666. {
  667. return m_GroupID;
  668. }
  669. // sets a valid id
  670. void CCP_MainApp::SetGroupDefaultID(long lID)
  671. {
  672. if(m_GroupDefaultID == lID)
  673. {
  674. return;
  675. }
  676. if(lID <= 0)
  677. {
  678. m_GroupDefaultID = 0;
  679. }
  680. else
  681. {
  682. m_GroupDefaultID = lID;
  683. }
  684. if(QPasteWnd())
  685. {
  686. QPasteWnd()->UpdateStatus();
  687. }
  688. }
  689. void CCP_MainApp::SetStatus(const TCHAR* status, bool bRepaintImmediately)
  690. {
  691. m_Status = status;
  692. if(QPasteWnd())
  693. {
  694. QPasteWnd()->UpdateStatus(bRepaintImmediately);
  695. }
  696. }
  697. void CCP_MainApp::ShowPersistent(bool bVal)
  698. {
  699. g_Opt.SetShowPersistent(bVal);
  700. // give some visual indication
  701. if(m_bShowingQuickPaste)
  702. {
  703. ASSERT(QPasteWnd());
  704. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  705. QPasteWnd()->RefreshNc();
  706. }
  707. }
  708. /////////////////////////////////////////////////////////////////////////////
  709. // CCP_MainApp message handlers
  710. int CCP_MainApp::ExitInstance()
  711. {
  712. Log(_T("ExitInstance"));
  713. m_db.close();
  714. if(m_pUacPasteThread != NULL)
  715. {
  716. if(m_pUacPasteThread->ThreadWasStarted() == false)
  717. {
  718. m_pUacPasteThread->FireExit();
  719. }
  720. delete m_pUacPasteThread;
  721. }
  722. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  723. return CWinApp::ExitInstance();
  724. }
  725. // return TRUE if there is more idle processing to do
  726. BOOL CCP_MainApp::OnIdle(LONG lCount)
  727. {
  728. // let winapp handle its idle processing
  729. if(CWinApp::OnIdle(lCount))
  730. return TRUE;
  731. return FALSE;
  732. }
  733. void CCP_MainApp::SetConnectCV(bool bConnect)
  734. {
  735. m_CopyThread.SetConnectCV(bConnect);
  736. g_Opt.SetConnectedToClipboard(bConnect == true);
  737. if(bConnect)
  738. {
  739. m_pMainFrame->m_TrayIcon.SetIcon(IDR_MAINFRAME);
  740. }
  741. else
  742. {
  743. m_pMainFrame->m_TrayIcon.SetIcon(IDI_DITTO_NOCOPYCB);
  744. }
  745. if(QPasteWnd())
  746. {
  747. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  748. QPasteWnd()->RefreshNc();
  749. }
  750. }
  751. void CCP_MainApp::OnDeleteID(long lID)
  752. {
  753. if(QPasteWnd())
  754. {
  755. QPasteWnd()->PostMessage(NM_ITEM_DELETED, lID, 0);
  756. }
  757. }
  758. bool CCP_MainApp::ImportClips(HWND hWnd)
  759. {
  760. OPENFILENAME FileName;
  761. TCHAR szFileName[400];
  762. TCHAR szDir[400];
  763. memset(&FileName, 0, sizeof(FileName));
  764. memset(szFileName, 0, sizeof(szFileName));
  765. memset(&szDir, 0, sizeof(szDir));
  766. CString csInitialDir = CGetSetOptions::GetLastImportDir();
  767. STRCPY(szDir, csInitialDir);
  768. FileName.lStructSize = sizeof(FileName);
  769. FileName.lpstrTitle = _T("Import Clips");
  770. FileName.Flags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  771. FileName.nMaxFile = 400;
  772. FileName.lpstrFile = szFileName;
  773. FileName.lpstrInitialDir = szDir;
  774. FileName.lpstrFilter = _T("Exported Ditto Clips (.dto)\0*.dto\0\0");
  775. FileName.lpstrDefExt = _T("dto");
  776. if(GetOpenFileName(&FileName) == 0)
  777. {
  778. return false;
  779. }
  780. using namespace nsPath;
  781. CPath path(FileName.lpstrFile);
  782. CString csPath = path.GetPath();
  783. CGetSetOptions::SetLastImportDir(csPath);
  784. try
  785. {
  786. CppSQLite3DB db;
  787. db.open(FileName.lpstrFile);
  788. CClip_ImportExport clip;
  789. if(clip.ImportFromSqliteDB(db, true, false))
  790. {
  791. CShowTaskBarIcon show;
  792. CString cs;
  793. cs.Format(_T("%s %d "), theApp.m_Language.GetString("Import_Successfully", "Successfully imported"), clip.m_importCount);
  794. if(clip.m_importCount = 1)
  795. cs += theApp.m_Language.GetString("Clip", "clip");
  796. else
  797. cs += theApp.m_Language.GetString("Clips", "clips");
  798. MessageBox(hWnd, cs, _T("Ditto"), MB_OK);
  799. }
  800. else
  801. {
  802. CShowTaskBarIcon show;
  803. MessageBox(hWnd, theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"), _T("Ditto"), MB_OK);
  804. }
  805. }
  806. catch (CppSQLite3Exception& e)
  807. {
  808. ASSERT(FALSE);
  809. CString csError;
  810. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  811. MessageBox(hWnd, csError, _T("Ditto"), MB_OK);
  812. }
  813. return true;
  814. }
  815. void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
  816. {
  817. Log(StrF(_T("ShowCommandLineError %s - %s"), csTitle, csMessage));
  818. CToolTipEx *pErrorWnd = new CToolTipEx;
  819. pErrorWnd->Create(NULL);
  820. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  821. CPoint pt;
  822. CRect rcScreen;
  823. GetMonitorRect(0, &rcScreen);
  824. pt = rcScreen.BottomRight();
  825. CRect cr = pErrorWnd->GetBoundsRect();
  826. pt.x -= max(cr.Width()+50, 150);
  827. pt.y -= max(cr.Height()+50, 150);
  828. pErrorWnd->Show(pt);
  829. Sleep(4000);
  830. pErrorWnd->DestroyWindow();
  831. }
  832. BOOL CCP_MainApp::GetClipData(long parentId, CClipFormat &Clip)
  833. {
  834. BOOL bRet = FALSE;
  835. try
  836. {
  837. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT ooData FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), parentId, GetFormatName(Clip.m_cfType));
  838. if(q.eof() == false)
  839. {
  840. int nDataLen = 0;
  841. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  842. if(cData != NULL)
  843. {
  844. Clip.m_hgData = NewGlobal(nDataLen);
  845. ::CopyToGlobalHP(Clip.m_hgData, (LPVOID)cData, nDataLen);
  846. bRet = TRUE;
  847. }
  848. }
  849. }
  850. CATCH_SQLITE_EXCEPTION
  851. return bRet;
  852. }
  853. bool CCP_MainApp::EditItems(CClipIDs &Ids, bool bShowError)
  854. {
  855. m_pMainFrame->ShowEditWnd(Ids);
  856. return true;
  857. }
  858. void CCP_MainApp::PumpMessageEx(HWND hWnd)
  859. {
  860. MSG KeyboardMsg;
  861. while (::PeekMessage(&KeyboardMsg, hWnd, 0, 0, PM_REMOVE))
  862. {
  863. ::TranslateMessage(&KeyboardMsg);
  864. ::DispatchMessage(&KeyboardMsg);
  865. }
  866. }
  867. HWND CCP_MainApp::QPastehWnd()
  868. {
  869. if(m_pMainFrame != NULL)
  870. {
  871. if(m_pMainFrame->m_quickPaste.m_pwndPaste != NULL)
  872. {
  873. return m_pMainFrame->m_quickPaste.m_pwndPaste->GetSafeHwnd();
  874. }
  875. }
  876. return NULL;
  877. }
  878. CQPasteWnd* CCP_MainApp::QPasteWnd()
  879. {
  880. if(m_pMainFrame != NULL)
  881. {
  882. return m_pMainFrame->m_quickPaste.m_pwndPaste;
  883. }
  884. return NULL;
  885. }
  886. bool CCP_MainApp::UACPaste()
  887. {
  888. if(m_pUacPasteThread == NULL)
  889. {
  890. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  891. }
  892. return m_pUacPasteThread->UACPaste();
  893. }
  894. bool CCP_MainApp::UACCopy()
  895. {
  896. if(m_pUacPasteThread == NULL)
  897. {
  898. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  899. }
  900. return m_pUacPasteThread->UACCopy();
  901. }
  902. bool CCP_MainApp::UACCut()
  903. {
  904. if(m_pUacPasteThread == NULL)
  905. {
  906. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  907. }
  908. return m_pUacPasteThread->UACCut();
  909. }
  910. bool CCP_MainApp::UACThreadRunning()
  911. {
  912. if(m_pUacPasteThread != NULL)
  913. {
  914. return m_pUacPasteThread->IsRunning();
  915. }
  916. return false;
  917. }
  918. void CCP_MainApp::RefreshShowInTaskBar()
  919. {
  920. if(m_pMainFrame != NULL)
  921. {
  922. m_pMainFrame->RefreshShowInTaskBar();
  923. }
  924. }