CP_Main.cpp 22 KB

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