CP_Main.cpp 18 KB

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