CP_Main.cpp 27 KB

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