CP_Main.cpp 29 KB

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