CP_Main.cpp 27 KB

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