CP_Main.cpp 26 KB

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