CP_Main.cpp 27 KB

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