CP_Main.cpp 26 KB

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