CP_Main.cpp 27 KB

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