CP_Main.cpp 27 KB

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