CP_Main.cpp 21 KB

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