CP_Main.cpp 27 KB

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