CP_Main.cpp 27 KB

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