CP_Main.cpp 21 KB

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