CP_Main.cpp 21 KB

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