CP_Main.cpp 26 KB

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