CP_Main.cpp 19 KB

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