CP_Main.cpp 26 KB

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