CP_Main.cpp 19 KB

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