CP_Main.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. pTypes->Add(GetFormatID(_T("PNG")));
  575. }
  576. return pTypes;
  577. }
  578. void CCP_MainApp::ReloadTypes()
  579. {
  580. CClipTypes* pTypes = LoadTypesFromDB();
  581. if(pTypes)
  582. {
  583. m_CopyThread.SetSupportedTypes(pTypes);
  584. }
  585. }
  586. void CCP_MainApp::RefreshView(CopyReasonEnum::CopyReason copyReason)
  587. {
  588. CQPasteWnd *pWnd = QPasteWnd();
  589. if(pWnd)
  590. {
  591. if(m_bAsynchronousRefreshView)
  592. {
  593. pWnd->PostMessage(WM_REFRESH_VIEW, copyReason, 0);
  594. }
  595. else
  596. {
  597. pWnd->SendMessage(WM_REFRESH_VIEW, copyReason, 0);
  598. }
  599. }
  600. }
  601. void CCP_MainApp::RefreshClipAfterPaste(int clipId, int updateFlags)
  602. {
  603. CQPasteWnd *pWnd = QPasteWnd();
  604. if(pWnd)
  605. {
  606. if(m_bAsynchronousRefreshView)
  607. {
  608. pWnd->PostMessage(WM_RELOAD_CLIP_AFTER_PASTE, clipId, updateFlags);
  609. }
  610. else
  611. {
  612. pWnd->SendMessage(WM_RELOAD_CLIP_AFTER_PASTE, clipId, updateFlags);
  613. }
  614. }
  615. }
  616. void CCP_MainApp::OnPasteCompleted()
  617. {
  618. }
  619. void CCP_MainApp::OnCopyCompleted(long lLastID, int count, CopyReasonEnum::CopyReason copyReason)
  620. {
  621. if(count <= 0)
  622. {
  623. return;
  624. }
  625. // update copy statistics
  626. CGetSetOptions::SetTripCopyCount(-count);
  627. CGetSetOptions::SetTotalCopyCount(-count);
  628. if(m_CopyBuffer.Active())
  629. {
  630. m_CopyBuffer.EndCopy(lLastID);
  631. }
  632. RefreshView(copyReason);
  633. }
  634. void CCP_MainApp::SaveCurrentGroupState()
  635. {
  636. m_oldGroupID = m_GroupID;
  637. m_oldGroupParentID = m_GroupParentID;
  638. m_oldGroupText = m_GroupText;
  639. }
  640. void CCP_MainApp::ClearOldGroupState()
  641. {
  642. m_oldGroupID = -2;
  643. m_oldGroupParentID = -2;
  644. m_oldGroupText = _T("");
  645. }
  646. BOOL CCP_MainApp::TryEnterOldGroupState()
  647. {
  648. BOOL enteredGroup = FALSE;
  649. if(m_oldGroupID > -2)
  650. {
  651. m_GroupID = m_oldGroupID;
  652. m_GroupParentID = m_oldGroupParentID;
  653. m_GroupText = m_oldGroupText;
  654. ClearOldGroupState();
  655. theApp.RefreshView();
  656. if(QPasteWnd())
  657. QPasteWnd()->UpdateStatus(true);
  658. enteredGroup = TRUE;
  659. }
  660. return enteredGroup;
  661. }
  662. BOOL CCP_MainApp::EnterGroupID(long lID, BOOL clearOldGroupState/* = TRUE*/, BOOL saveCurrentGroupState/* = FALSE*/)
  663. {
  664. BOOL bResult = FALSE;
  665. if(m_GroupID == lID)
  666. return TRUE;
  667. DWORD startTick = GetTickCount();
  668. if(clearOldGroupState)
  669. {
  670. ClearOldGroupState();
  671. }
  672. if(saveCurrentGroupState)
  673. {
  674. SaveCurrentGroupState();
  675. }
  676. // if we are switching to the parent, focus on the previous group
  677. if(m_GroupParentID == lID && m_GroupID > 0)
  678. m_FocusID = m_GroupID;
  679. switch(lID)
  680. {
  681. case -1:
  682. m_FocusID = -1;
  683. m_GroupID = -1;
  684. m_GroupParentID = -1;
  685. m_GroupText = "History";
  686. bResult = TRUE;
  687. break;
  688. default: // Normal Group
  689. try
  690. {
  691. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lParentID, mText, bIsGroup FROM Main WHERE lID = %d"), lID);
  692. if(q.eof() == false)
  693. {
  694. if(q.getIntField(_T("bIsGroup")) > 0)
  695. {
  696. m_GroupID = lID;
  697. m_GroupParentID = q.getIntField(_T("lParentID"));
  698. m_GroupText = q.getStringField(_T("mText"));
  699. bResult = TRUE;
  700. }
  701. }
  702. }
  703. CATCH_SQLITE_EXCEPTION
  704. break;
  705. }
  706. if(bResult)
  707. {
  708. theApp.RefreshView();
  709. if(QPasteWnd())
  710. QPasteWnd()->UpdateStatus(true);
  711. }
  712. DWORD endTick = GetTickCount();
  713. if((endTick-startTick) > 150)
  714. Log(StrF(_T("Paste Timing EnterParentId: %d"), endTick-startTick));
  715. return bResult;
  716. }
  717. // returns a usable group id (not negative)
  718. long CCP_MainApp::GetValidGroupID()
  719. {
  720. return m_GroupID;
  721. }
  722. // sets a valid id
  723. void CCP_MainApp::SetGroupDefaultID(long lID)
  724. {
  725. if(m_GroupDefaultID == lID)
  726. {
  727. return;
  728. }
  729. if(lID <= 0)
  730. {
  731. m_GroupDefaultID = 0;
  732. }
  733. else
  734. {
  735. m_GroupDefaultID = lID;
  736. }
  737. if(QPasteWnd())
  738. {
  739. QPasteWnd()->UpdateStatus();
  740. }
  741. }
  742. void CCP_MainApp::SetStatus(const TCHAR* status, bool bRepaintImmediately)
  743. {
  744. m_Status = status;
  745. if(QPasteWnd())
  746. {
  747. QPasteWnd()->UpdateStatus(bRepaintImmediately);
  748. }
  749. }
  750. void CCP_MainApp::ShowPersistent(bool bVal)
  751. {
  752. g_Opt.SetShowPersistent(bVal);
  753. // give some visual indication
  754. if(m_bShowingQuickPaste)
  755. {
  756. ASSERT(QPasteWnd());
  757. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  758. QPasteWnd()->RefreshNc();
  759. }
  760. }
  761. /////////////////////////////////////////////////////////////////////////////
  762. // CCP_MainApp message handlers
  763. int CCP_MainApp::ExitInstance()
  764. {
  765. Log(_T("ExitInstance"));
  766. DeleteDittoTempFiles(FALSE);
  767. m_db.close();
  768. if(m_pUacPasteThread != NULL)
  769. {
  770. if(m_pUacPasteThread->ThreadWasStarted() == false)
  771. {
  772. m_pUacPasteThread->FireExit();
  773. }
  774. delete m_pUacPasteThread;
  775. }
  776. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  777. return CWinApp::ExitInstance();
  778. }
  779. // return TRUE if there is more idle processing to do
  780. BOOL CCP_MainApp::OnIdle(LONG lCount)
  781. {
  782. // let winapp handle its idle processing
  783. if(CWinApp::OnIdle(lCount))
  784. return TRUE;
  785. return FALSE;
  786. }
  787. void CCP_MainApp::SetConnectCV(bool bConnect)
  788. {
  789. m_CopyThread.SetConnectCV(bConnect);
  790. g_Opt.SetConnectedToClipboard(bConnect == true);
  791. if(bConnect)
  792. {
  793. m_pMainFrame->m_trayIcon.SetIcon(IDR_MAINFRAME);
  794. m_pMainFrame->m_trayIcon.SetTooltipText(_T("Ditto"));
  795. }
  796. else
  797. {
  798. m_pMainFrame->m_trayIcon.SetIcon(IDI_DITTO_NOCOPYCB);
  799. CString cs;
  800. cs = _T("Ditto ");
  801. cs += theApp.m_Language.GetString("disconnected", "[Disconnected]");
  802. m_pMainFrame->m_trayIcon.SetTooltipText(cs);
  803. }
  804. if(QPasteWnd())
  805. {
  806. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  807. QPasteWnd()->RefreshNc();
  808. }
  809. }
  810. void CCP_MainApp::OnDeleteID(long lID)
  811. {
  812. if(QPasteWnd())
  813. {
  814. QPasteWnd()->PostMessage(NM_ITEM_DELETED, lID, 0);
  815. }
  816. }
  817. bool CCP_MainApp::ImportClips(HWND hWnd)
  818. {
  819. OPENFILENAME FileName;
  820. TCHAR szFileName[400];
  821. TCHAR szDir[400];
  822. memset(&FileName, 0, sizeof(FileName));
  823. memset(szFileName, 0, sizeof(szFileName));
  824. memset(&szDir, 0, sizeof(szDir));
  825. CString csInitialDir = CGetSetOptions::GetLastImportDir();
  826. STRCPY(szDir, csInitialDir);
  827. FileName.lStructSize = sizeof(FileName);
  828. FileName.lpstrTitle = _T("Import Clips");
  829. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
  830. FileName.nMaxFile = 400;
  831. FileName.lpstrFile = szFileName;
  832. FileName.lpstrInitialDir = szDir;
  833. FileName.lpstrFilter = _T("Exported Ditto Clips (.dto)\0*.dto\0\0");
  834. FileName.lpstrDefExt = _T("dto");
  835. if(GetOpenFileName(&FileName) == 0)
  836. {
  837. return false;
  838. }
  839. using namespace nsPath;
  840. CPath path(FileName.lpstrFile);
  841. CString csPath = path.GetPath();
  842. CGetSetOptions::SetLastImportDir(csPath);
  843. try
  844. {
  845. CppSQLite3DB db;
  846. db.open(FileName.lpstrFile);
  847. CClip_ImportExport clip;
  848. if(clip.ImportFromSqliteDB(db, true, false))
  849. {
  850. CShowTaskBarIcon show;
  851. CString cs;
  852. cs.Format(_T("%s %d "), theApp.m_Language.GetString("Import_Successfully", "Successfully imported"), clip.m_importCount);
  853. if(clip.m_importCount = 1)
  854. cs += theApp.m_Language.GetString("Clip", "clip");
  855. else
  856. cs += theApp.m_Language.GetString("Clips", "clips");
  857. MessageBox(hWnd, cs, _T("Ditto"), MB_OK);
  858. }
  859. else
  860. {
  861. CShowTaskBarIcon show;
  862. MessageBox(hWnd, theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"), _T("Ditto"), MB_OK);
  863. }
  864. }
  865. catch (CppSQLite3Exception& e)
  866. {
  867. ASSERT(FALSE);
  868. CString csError;
  869. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  870. MessageBox(hWnd, csError, _T("Ditto"), MB_OK);
  871. }
  872. return true;
  873. }
  874. void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
  875. {
  876. Log(StrF(_T("ShowCommandLineError %s - %s"), csTitle, csMessage));
  877. CToolTipEx *pErrorWnd = new CToolTipEx;
  878. pErrorWnd->Create(NULL);
  879. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  880. CPoint pt;
  881. CRect rcScreen = DefaultMonitorRect();
  882. pt = rcScreen.BottomRight();
  883. CRect cr = pErrorWnd->GetBoundsRect();
  884. pt.x -= max(cr.Width()+50, 150);
  885. pt.y -= max(cr.Height()+50, 150);
  886. pErrorWnd->Show(pt);
  887. PumpMessageEx(pErrorWnd->m_hWnd);
  888. Sleep(4000);
  889. pErrorWnd->DestroyWindow();
  890. }
  891. BOOL CCP_MainApp::GetClipData(long parentId, CClipFormat &Clip)
  892. {
  893. BOOL bRet = FALSE;
  894. try
  895. {
  896. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT ooData FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), parentId, GetFormatName(Clip.m_cfType));
  897. if(q.eof() == false)
  898. {
  899. int nDataLen = 0;
  900. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  901. if(cData != NULL)
  902. {
  903. Clip.m_hgData = NewGlobal(nDataLen);
  904. ::CopyToGlobalHP(Clip.m_hgData, (LPVOID)cData, nDataLen);
  905. bRet = TRUE;
  906. }
  907. }
  908. }
  909. CATCH_SQLITE_EXCEPTION
  910. return bRet;
  911. }
  912. bool CCP_MainApp::EditItems(CClipIDs &Ids, bool bShowError)
  913. {
  914. m_pMainFrame->ShowEditWnd(Ids);
  915. return true;
  916. }
  917. void CCP_MainApp::PumpMessageEx(HWND hWnd)
  918. {
  919. MSG KeyboardMsg;
  920. while (::PeekMessage(&KeyboardMsg, hWnd, 0, 0, PM_REMOVE))
  921. {
  922. ::TranslateMessage(&KeyboardMsg);
  923. ::DispatchMessage(&KeyboardMsg);
  924. }
  925. }
  926. HWND CCP_MainApp::QPastehWnd()
  927. {
  928. if(m_pMainFrame != NULL)
  929. {
  930. if(m_pMainFrame->m_quickPaste.m_pwndPaste != NULL)
  931. {
  932. return m_pMainFrame->m_quickPaste.m_pwndPaste->GetSafeHwnd();
  933. }
  934. }
  935. return NULL;
  936. }
  937. CQPasteWnd* CCP_MainApp::QPasteWnd()
  938. {
  939. if(m_pMainFrame != NULL)
  940. {
  941. return m_pMainFrame->m_quickPaste.m_pwndPaste;
  942. }
  943. return NULL;
  944. }
  945. bool CCP_MainApp::UACPaste()
  946. {
  947. if(m_pUacPasteThread == NULL)
  948. {
  949. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  950. }
  951. return m_pUacPasteThread->UACPaste();
  952. }
  953. bool CCP_MainApp::UACCopy()
  954. {
  955. if(m_pUacPasteThread == NULL)
  956. {
  957. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  958. }
  959. return m_pUacPasteThread->UACCopy();
  960. }
  961. bool CCP_MainApp::UACCut()
  962. {
  963. if(m_pUacPasteThread == NULL)
  964. {
  965. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  966. }
  967. return m_pUacPasteThread->UACCut();
  968. }
  969. bool CCP_MainApp::UACThreadRunning()
  970. {
  971. if(m_pUacPasteThread != NULL)
  972. {
  973. return m_pUacPasteThread->IsRunning();
  974. }
  975. return false;
  976. }
  977. void CCP_MainApp::RefreshShowInTaskBar()
  978. {
  979. if(m_pMainFrame != NULL)
  980. {
  981. m_pMainFrame->RefreshShowInTaskBar();
  982. }
  983. }
  984. void CCP_MainApp::SetActiveGroupId(int groupId)
  985. {
  986. m_activeGroupId = groupId;
  987. m_activeGroupStartTime = GetTickCount();
  988. }
  989. int CCP_MainApp::GetActiveGroupId()
  990. {
  991. int ret = -1;
  992. DWORD maxDiff = CGetSetOptions::GetSaveToGroupTimeoutMS();
  993. DWORD diff = GetTickCount() - m_activeGroupStartTime;
  994. if(m_activeGroupId > -1 &&
  995. diff < maxDiff)
  996. {
  997. ret = m_activeGroupId;
  998. }
  999. m_activeGroupId = -1;
  1000. m_activeGroupStartTime = 0;
  1001. return ret;
  1002. }
  1003. void CCP_MainApp::SetCopyReason(CopyReasonEnum::CopyReason copyReason)
  1004. {
  1005. m_copyReason = copyReason;
  1006. m_copyReasonStartTime = GetTickCount();
  1007. }
  1008. CopyReasonEnum::CopyReason CCP_MainApp::GetCopyReason()
  1009. {
  1010. CopyReasonEnum::CopyReason ret = CopyReasonEnum::COPY_TO_UNKOWN;
  1011. DWORD maxDiff = CGetSetOptions::GetCopyReasonTimeoutMS();
  1012. DWORD diff = GetTickCount() - m_copyReasonStartTime;
  1013. if(m_copyReason != CopyReasonEnum::COPY_TO_UNKOWN &&
  1014. diff < maxDiff)
  1015. {
  1016. ret = m_copyReason;
  1017. }
  1018. m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
  1019. m_copyReasonStartTime = 0;
  1020. return ret;
  1021. }