CP_Main.cpp 26 KB

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