CP_Main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. // CP_Main.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. //#include "vld.h"
  5. #include "CP_Main.h"
  6. #include "MainFrm.h"
  7. #include "Misc.h"
  8. #include ".\cp_main.h"
  9. #include "server.h"
  10. #include "Client.h"
  11. #include "InternetUpdate.h"
  12. #include <io.h>
  13. #include "Path.h"
  14. #include "Clip_ImportExport.h"
  15. #include "HyperLink.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // The one and only CCP_MainApp object
  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. }
  60. BOOL m_bDisconnect;
  61. BOOL m_bConnect;
  62. BOOL m_bU3;
  63. BOOL m_bU3Stop;
  64. BOOL m_bU3Install;
  65. };
  66. CCP_MainApp theApp;
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CCP_MainApp
  69. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  70. //{{AFX_MSG_MAP(CCP_MainApp)
  71. // NOTE - the ClassWizard will add and remove mapping macros here.
  72. // DO NOT EDIT what you see in these blocks of generated code!
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CCP_MainApp construction
  77. CCP_MainApp::CCP_MainApp()
  78. {
  79. m_bAppRunning = false;
  80. m_bAppExiting = false;
  81. m_MainhWnd = NULL;
  82. m_pMainFrame = NULL;
  83. m_bShowingQuickPaste = false;
  84. m_bShowingOptions = false;
  85. m_bRemoveOldEntriesPending = false;
  86. m_IC_bCopy = false;
  87. m_GroupDefaultID = 0;
  88. m_GroupID = -1;
  89. m_GroupParentID = 0;
  90. m_GroupText = "History";
  91. m_FocusID = -1;
  92. m_bAsynchronousRefreshView = true;
  93. m_lClipsSent = 0;
  94. m_lClipsRecieved = 0;
  95. m_oldtStartUp = COleDateTime::GetCurrentTime();
  96. m_bExitServerThread = false;
  97. m_lLastGoodIndexForNextworkPassword = -2;
  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. m_QuickPasteMode = NONE_QUICK_PASTE;
  104. m_pQuickPasteClip = NULL;
  105. m_bDittoHasFocus = false;
  106. ::InitializeCriticalSection(&m_CriticalSection);
  107. }
  108. CCP_MainApp::~CCP_MainApp()
  109. {
  110. ::DeleteCriticalSection(&m_CriticalSection);
  111. }
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CCP_MainApp initialization
  114. BOOL CCP_MainApp::InitInstance()
  115. {
  116. AfxEnableControlContainer();
  117. AfxOleInit();
  118. AfxInitRichEditEx();
  119. DittoCommandLineInfo cmdInfo;
  120. ParseCommandLine(cmdInfo);
  121. if(cmdInfo.m_strFileName.IsEmpty() == FALSE)
  122. {
  123. try
  124. {
  125. CClip_ImportExport Clip;
  126. CppSQLite3DB db;
  127. db.open(cmdInfo.m_strFileName);
  128. CClip_ImportExport clip;
  129. if(clip.ImportFromSqliteDB(db, false, true))
  130. {
  131. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Importing_Good", "Clip placed on clipboard"));
  132. }
  133. else
  134. {
  135. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"));
  136. }
  137. }
  138. catch (CppSQLite3Exception& e)
  139. {
  140. ASSERT(FALSE);
  141. CString csError;
  142. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  143. ShowCommandLineError("Ditto", csError);
  144. }
  145. return FALSE;
  146. }
  147. else if(cmdInfo.m_bConnect || cmdInfo.m_bDisconnect)
  148. {
  149. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  150. if(hWnd)
  151. ::SendMessage(hWnd, WM_SET_CONNECTED, cmdInfo.m_bConnect, cmdInfo.m_bDisconnect);
  152. return FALSE;
  153. }
  154. else if(cmdInfo.m_bU3Install)
  155. {
  156. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  157. csFile += "U3_Install.htm";
  158. CHyperLink::GotoURL(csFile, SW_SHOW);
  159. return FALSE;
  160. }
  161. //if starting from a u3 device we will pass in -U3Start
  162. if(cmdInfo.m_bU3)
  163. g_Opt.m_bU3 = cmdInfo.m_bU3 ? TRUE : FALSE;
  164. g_Opt.LoadSettings();
  165. if(cmdInfo.m_bU3Stop)
  166. {
  167. MessageBox(NULL, _T("recieved Stop"), _T(""), MB_OK);
  168. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  169. if(hWnd)
  170. {
  171. MessageBox(NULL, _T("Sending Close"), _T(""), MB_OK);
  172. ::SendMessage(hWnd, WM_CLOSE, 0, 0);
  173. }
  174. return FALSE;
  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. m_hMutex = CreateMutex(NULL, FALSE, csMutex);
  190. DWORD dwError = GetLastError();
  191. if(dwError == ERROR_ALREADY_EXISTS)
  192. {
  193. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  194. if(hWnd)
  195. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  196. return TRUE;
  197. }
  198. CString csFile = CGetSetOptions::GetLanguageFile();
  199. if(csFile.GetLength() > 0 && !m_Language.LoadLanguageFile(csFile))
  200. {
  201. CString cs;
  202. cs.Format(_T("Error loading language file - %s - \n\n%s"), csFile, m_Language.m_csLastError);
  203. Log(cs);
  204. }
  205. // if(g_Opt.m_bU3)
  206. // {
  207. // CopyDownDatabase();
  208. // }
  209. int nRet = CheckDBExists(CGetSetOptions::GetDBPath());
  210. if(nRet == FALSE)
  211. {
  212. AfxMessageBox(theApp.m_Language.GetString("Error_Opening_Database", "Error Opening Database."));
  213. return FALSE;
  214. }
  215. CMainFrame* pFrame = new CMainFrame;
  216. m_pMainWnd = m_pMainFrame = pFrame;
  217. // prevent no one having focus on startup
  218. TargetActiveWindow();
  219. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  220. pFrame->ShowWindow(SW_SHOW);
  221. pFrame->UpdateWindow();
  222. // prevent no one having focus on startup
  223. ReleaseFocus();
  224. return TRUE;
  225. }
  226. void CCP_MainApp::AfterMainCreate()
  227. {
  228. m_MainhWnd = m_pMainFrame->m_hWnd;
  229. ASSERT( ::IsWindow(m_MainhWnd) );
  230. g_Opt.SetMainHWND((long)m_MainhWnd);
  231. //Save the HWND so the stop app can send us a close message
  232. if(g_Opt.m_bU3)
  233. {
  234. CGetSetOptions::WriteU3Hwnd(m_MainhWnd);
  235. }
  236. g_HotKeys.Init(m_MainhWnd);
  237. // create hotkeys here. They are automatically deleted on exit
  238. m_pDittoHotKey = new CHotKey(CString("DittoHotKey"), 704); //704 is ctrl-tilda
  239. //A U3 device is unable to use the keyboard hooks, so named paste and copy
  240. //can't be used
  241. if(!g_Opt.m_bU3)
  242. {
  243. m_pNamedCopy = new CHotKey("CopyHotKey");
  244. m_pNamedPaste = new CHotKey("NamedPaste");
  245. }
  246. m_pPosOne = new CHotKey("Position1", 0, true);
  247. m_pPosTwo = new CHotKey("Position2", 0, true);
  248. m_pPosThree = new CHotKey("Position3", 0, true);
  249. m_pPosFour = new CHotKey("Position4", 0, true);
  250. m_pPosFive = new CHotKey("Position5", 0, true);
  251. m_pPosSix = new CHotKey("Position6", 0, true);
  252. m_pPosSeven = new CHotKey("Position7", 0, true);
  253. m_pPosEight = new CHotKey("Position8", 0, true);
  254. m_pPosNine = new CHotKey("Position9", 0, true);
  255. m_pPosTen = new CHotKey("Position10", 0, true);
  256. g_HotKeys.RegisterAll();
  257. // CopyThread initialization
  258. StartCopyThread();
  259. StartStopServerThread();
  260. m_bAppRunning = true;
  261. }
  262. void CCP_MainApp::StartStopServerThread()
  263. {
  264. if(CGetSetOptions::GetDisableRecieve() == FALSE)
  265. {
  266. AfxBeginThread(MTServerThread, m_MainhWnd);
  267. }
  268. else
  269. {
  270. m_bExitServerThread = true;
  271. closesocket(theApp.m_sSocket);
  272. }
  273. }
  274. void CCP_MainApp::StopServerThread()
  275. {
  276. m_bExitServerThread = true;
  277. closesocket(theApp.m_sSocket);
  278. }
  279. void CCP_MainApp::BeforeMainClose()
  280. {
  281. ASSERT( m_bAppRunning && !m_bAppExiting );
  282. m_bAppRunning = false;
  283. m_bAppExiting = true;
  284. g_HotKeys.UnregisterAll();
  285. StopServerThread();
  286. StopCopyThread();
  287. }
  288. /*
  289. Re: Targeting the previous focus window
  290. We usually gain focus after the following messages:
  291. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  292. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  293. CMainFrame::PreTranslateMessage is used to intercept messages before
  294. they are processed (before we are actually given focus) in order to
  295. save the previous window that had focus.
  296. - It currently just handles "activating" mouse messages when showing.
  297. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  298. finding the Target on hotkey activation.
  299. This works well for most window switching (mouse or hotkey), but does
  300. not work well for <Alt>-<Tab> or other window switching applications
  301. (e.g. the taskbar tray), since the previous window was only a means to
  302. switching and not the target itself.
  303. - one solution might be to always monitor the current foreground
  304. window using system hooks or a short (e.g. 1 sec) timer... though this
  305. *might* be cpu intensive (slow). I'm currently looking into using
  306. WH_CBT system hooks in a separate dll (see: robpitt's
  307. http://website.lineone.net/~codebox/focuslog.zip).
  308. */
  309. bool CCP_MainApp::TargetActiveWindow()
  310. {
  311. if(g_Opt.m_bUseHookDllForFocus)
  312. return true;
  313. HWND hOld = m_hTargetWnd;
  314. GUITHREADINFO guiThreadInfo;
  315. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  316. HWND hNew = ::GetForegroundWindow();
  317. DWORD OtherThreadID = GetWindowThreadProcessId(hNew, NULL);
  318. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  319. {
  320. hNew = guiThreadInfo.hwndFocus;
  321. }
  322. if(hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew))
  323. return false;
  324. m_hTargetWnd = hNew;
  325. if(QPasteWnd())
  326. QPasteWnd()->UpdateStatus(true);
  327. // Tracking / Debugging
  328. /*
  329. LOG( StrF(
  330. "Target Changed" \
  331. "\n\tOld = 0x%08x: \"%s\"" \
  332. "\n\tNew = 0x%08x: \"%s\"\n",
  333. hOld, (LPCTSTR) GetWndText(hOld),
  334. hNew, (LPCTSTR) GetWndText(hNew) ) );
  335. */
  336. return true;
  337. }
  338. bool CCP_MainApp::ActivateTarget()
  339. {
  340. ::SetForegroundWindow(m_hTargetWnd);
  341. ::SetFocus(m_hTargetWnd);
  342. return true;
  343. }
  344. bool CCP_MainApp::ReleaseFocus()
  345. {
  346. if( IsAppWnd(::GetForegroundWindow()) )
  347. return ActivateTarget();
  348. return false;
  349. }
  350. // sends Ctrl-V to the TargetWnd
  351. void CCP_MainApp::SendPaste(bool bActivateTarget)
  352. {
  353. Log(_T("SendPaste"));
  354. char ch;
  355. //Make sure all the keys are up
  356. for(ch = '0'; ch <= '9'; ch++)
  357. {
  358. keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  359. }
  360. for(ch = 'A'; ch <= 'Z'; ch++)
  361. {
  362. keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  363. }
  364. keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  365. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  366. keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  367. keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  368. Sleep(50);
  369. if(bActivateTarget && !ActivateTarget())
  370. {
  371. SetStatus(_T("SendPaste FAILED!"), TRUE);
  372. return;
  373. }
  374. MSG KeyboardMsg;
  375. while (::PeekMessage(&KeyboardMsg, NULL, 0, 0, PM_REMOVE))
  376. {
  377. ::TranslateMessage(&KeyboardMsg);
  378. ::DispatchMessage(&KeyboardMsg);
  379. }
  380. Sleep(50);
  381. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  382. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  383. Sleep(50);
  384. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  385. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  386. Log(_T("END SendPaste"));
  387. }
  388. // sends Ctrl-V to the TargetWnd
  389. void CCP_MainApp::SendCopy()
  390. {
  391. char ch;
  392. //Make sure all the keys are up
  393. for(ch = '0'; ch <= '9'; ch++)
  394. {
  395. keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  396. }
  397. for(ch = 'A'; ch <= 'Z'; ch++)
  398. {
  399. keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  400. }
  401. keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  402. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  403. keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  404. keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  405. Sleep(50);
  406. MSG KeyboardMsg;
  407. while (::PeekMessage(&KeyboardMsg, NULL, 0, 0, PM_REMOVE))
  408. {
  409. ::TranslateMessage(&KeyboardMsg);
  410. ::DispatchMessage(&KeyboardMsg);
  411. }
  412. Sleep(50);
  413. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  414. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  415. Sleep(50);
  416. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  417. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  418. }
  419. // CopyThread
  420. void CCP_MainApp::StartCopyThread()
  421. {
  422. ASSERT( m_MainhWnd );
  423. CClipTypes* pTypes = LoadTypesFromDB();
  424. // initialize to:
  425. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  426. // - true = use Asynchronous communication (PostMessage)
  427. // - true = enable copying on clipboard changes
  428. // - pTypes = the supported types to use
  429. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  430. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  431. m_CopyThread.ResumeThread();
  432. }
  433. void CCP_MainApp::StopCopyThread()
  434. {
  435. EnableCbCopy(false);
  436. m_CopyThread.Quit();
  437. SaveCopyClips();
  438. }
  439. // returns the current Clipboard Viewer Connect state (though it might not yet
  440. // be actually connected -- check IsClipboardViewerConnected())
  441. bool CCP_MainApp::ToggleConnectCV()
  442. {
  443. bool bConnect = !GetConnectCV();
  444. SetConnectCV(bConnect);
  445. return bConnect;
  446. }
  447. // Sets a menu entry according to the current Clipboard Viewer Connection status
  448. // - the menu text indicates the available command (opposite the current state)
  449. // - a check mark appears in the rare cases that the menu text actually represents
  450. // the current state, e.g. if we are supposed to be connected, but we somehow
  451. // lose that connection, "Disconnect from Clipboard" will have a check next to it.
  452. void CCP_MainApp::UpdateMenuConnectCV(CMenu* pMenu, UINT nMenuID)
  453. {
  454. if(pMenu == NULL)
  455. return;
  456. bool bConnect = theApp.GetConnectCV();
  457. CString cs;
  458. if(bConnect)
  459. {
  460. cs = theApp.m_Language.GetString("Disconnect_Clipboard", "Disconnect from Clipboard.");
  461. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  462. }
  463. else
  464. {
  465. cs = theApp.m_Language.GetString("Connect_Clipboard", "Connect to Clipboard.");
  466. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  467. }
  468. }
  469. // Allocates a new CClipTypes
  470. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  471. {
  472. CClipTypes* pTypes = new CClipTypes;
  473. try
  474. {
  475. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT TypeText FROM Types"));
  476. while(q.eof() == false)
  477. {
  478. pTypes->Add(GetFormatID(q.getStringField(_T("TypeText"))));
  479. q.nextRow();
  480. }
  481. }
  482. CATCH_SQLITE_EXCEPTION
  483. if(pTypes->GetSize() <= 0)
  484. {
  485. pTypes->Add(CF_TEXT);
  486. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  487. pTypes->Add(CF_UNICODETEXT);
  488. pTypes->Add(CF_HDROP);
  489. if(g_Opt.m_bU3 == false)
  490. {
  491. pTypes->Add(CF_DIB);
  492. }
  493. }
  494. return pTypes;
  495. }
  496. void CCP_MainApp::ReloadTypes()
  497. {
  498. CClipTypes* pTypes = LoadTypesFromDB();
  499. if( pTypes )
  500. m_CopyThread.SetSupportedTypes( pTypes );
  501. }
  502. long CCP_MainApp::SaveCopyClips()
  503. {
  504. long lID = 0;
  505. int count;
  506. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  507. if(!pClips)
  508. return 0;
  509. CClipList* pCopyOfClips = NULL;
  510. if(g_Opt.m_lAutoSendClientCount > 0)
  511. {
  512. //The thread will free these
  513. pCopyOfClips = new CClipList;
  514. if(pCopyOfClips != NULL)
  515. {
  516. *pCopyOfClips = *pClips;
  517. }
  518. }
  519. bool bEnteredThread = false;
  520. bool bDeletepClips = true;
  521. if(m_QuickPasteMode == ADDING_QUICK_PASTE)
  522. {
  523. //this will be added when they are done entering the quick paste text
  524. m_pQuickPasteClip = pClips;
  525. bDeletepClips = false;
  526. //go ahead and send the clips out even though it won't be added for a bit
  527. count = 1;
  528. }
  529. else
  530. {
  531. count = pClips->AddToDB(true);
  532. if(count > 0)
  533. {
  534. lID = pClips->GetTail()->m_ID;
  535. OnCopyCompleted(lID, count);
  536. }
  537. }
  538. if(count > 0)
  539. {
  540. if(g_Opt.m_lAutoSendClientCount > 0)
  541. {
  542. AfxBeginThread(SendClientThread, pCopyOfClips);
  543. bEnteredThread = true;
  544. }
  545. }
  546. if(bEnteredThread == false)
  547. delete pCopyOfClips;
  548. if(bDeletepClips)
  549. delete pClips;
  550. return lID;
  551. }
  552. void CCP_MainApp::RefreshView()
  553. {
  554. CQPasteWnd *pWnd = QPasteWnd();
  555. if(pWnd)
  556. {
  557. if(m_bAsynchronousRefreshView)
  558. pWnd->PostMessage(WM_REFRESH_VIEW);
  559. else
  560. pWnd->SendMessage(WM_REFRESH_VIEW);
  561. }
  562. }
  563. void CCP_MainApp::OnPasteCompleted()
  564. {
  565. // the list only changes if UpdateTimeOnPaste is true (updated time)
  566. if(g_Opt.m_bUpdateTimeOnPaste)
  567. RefreshView();
  568. }
  569. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  570. {
  571. if(count <= 0)
  572. return;
  573. // queue a message to RemoveOldEntries
  574. Delayed_RemoveOldEntries(60000);
  575. // update copy statistics
  576. CGetSetOptions::SetTripCopyCount(-count);
  577. CGetSetOptions::SetTotalCopyCount(-count);
  578. RefreshView();
  579. }
  580. // Internal Clipboard for cut/copy/paste items between Groups
  581. // if NULL, this uses the current QPaste selection
  582. void CCP_MainApp::IC_Cut(ARRAY* pIDs)
  583. {
  584. if( pIDs == NULL )
  585. {
  586. if( QPasteWnd() )
  587. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  588. else
  589. m_IC_IDs.SetSize(0);
  590. }
  591. else
  592. m_IC_IDs.Copy( *pIDs );
  593. m_IC_bCopy = false;
  594. if( QPasteWnd() )
  595. QPasteWnd()->UpdateStatus();
  596. }
  597. // if NULL, this uses the current QPaste selection
  598. void CCP_MainApp::IC_Copy(ARRAY* pIDs)
  599. {
  600. if(pIDs == NULL)
  601. {
  602. if(QPasteWnd())
  603. QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
  604. else
  605. m_IC_IDs.SetSize(0);
  606. }
  607. else
  608. m_IC_IDs.Copy(*pIDs);
  609. m_IC_bCopy = true;
  610. if(QPasteWnd())
  611. QPasteWnd()->UpdateStatus();
  612. }
  613. void CCP_MainApp::IC_Paste()
  614. {
  615. if(m_IC_IDs.GetSize() <= 0)
  616. return;
  617. if(m_IC_bCopy)
  618. m_IC_IDs.CopyTo(GetValidGroupID());
  619. else // Move
  620. m_IC_IDs.MoveTo(GetValidGroupID());
  621. // don't process the same items twice.
  622. m_IC_IDs.SetSize(0);
  623. RefreshView();
  624. }
  625. // Groups
  626. BOOL CCP_MainApp::EnterGroupID(long lID)
  627. {
  628. BOOL bResult = FALSE;
  629. if(m_GroupID == lID)
  630. return TRUE;
  631. // if we are switching to the parent, focus on the previous group
  632. if(m_GroupParentID == lID && m_GroupID > 0)
  633. m_FocusID = m_GroupID;
  634. switch(lID)
  635. {
  636. case -1:
  637. m_FocusID = -1;
  638. m_GroupID = -1;
  639. m_GroupParentID = -1;
  640. m_GroupText = "History";
  641. bResult = TRUE;
  642. break;
  643. default: // Normal Group
  644. try
  645. {
  646. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lParentID, mText, bIsGroup FROM Main WHERE lID = %d"), lID);
  647. if(q.eof() == false)
  648. {
  649. if(q.getIntField(_T("bIsGroup")) > 0)
  650. {
  651. m_GroupID = lID;
  652. m_GroupParentID = q.getIntField(_T("lParentID"));
  653. // if( m_GroupParentID == 0 )
  654. // m_GroupParentID = -1; // back out into "all top-level groups" list.
  655. m_GroupText = q.getStringField(_T("mText"));
  656. bResult = TRUE;
  657. }
  658. }
  659. }
  660. CATCH_SQLITE_EXCEPTION
  661. break;
  662. }
  663. if(bResult)
  664. {
  665. theApp.RefreshView();
  666. if(QPasteWnd())
  667. QPasteWnd()->UpdateStatus(true);
  668. }
  669. return bResult;
  670. }
  671. // returns a usable group id (not negative)
  672. long CCP_MainApp::GetValidGroupID()
  673. {
  674. return m_GroupID;
  675. }
  676. // sets a valid id
  677. void CCP_MainApp::SetGroupDefaultID( long lID )
  678. {
  679. if( m_GroupDefaultID == lID )
  680. return;
  681. if( lID <= 0 )
  682. m_GroupDefaultID = 0;
  683. else
  684. m_GroupDefaultID = lID;
  685. if( QPasteWnd() )
  686. QPasteWnd()->UpdateStatus();
  687. }
  688. // Window States
  689. void CCP_MainApp::SetStatus( const TCHAR* status, bool bRepaintImmediately )
  690. {
  691. m_Status = status;
  692. if( QPasteWnd() )
  693. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  694. }
  695. void CCP_MainApp::ShowPersistent( bool bVal )
  696. {
  697. g_Opt.SetShowPersistent( bVal );
  698. // give some visual indication
  699. if( m_bShowingQuickPaste )
  700. {
  701. ASSERT( QPasteWnd() );
  702. QPasteWnd()->SetCaptionColorActive(!g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  703. QPasteWnd()->RefreshNc();
  704. }
  705. }
  706. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  707. {
  708. if( !m_bRemoveOldEntriesPending )
  709. {
  710. m_bRemoveOldEntriesPending = true;
  711. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  712. }
  713. }
  714. /////////////////////////////////////////////////////////////////////////////
  715. // CCP_MainApp message handlers
  716. int CCP_MainApp::ExitInstance()
  717. {
  718. Log(_T("ExitInstance"));
  719. m_db.close();
  720. // if(g_Opt.m_bU3)
  721. // {
  722. // if(g_Opt.IsU3DeviceAvailable())
  723. // {
  724. // CopyUpDatabase();
  725. // }
  726. // else
  727. // {
  728. // Log(_T("Needed to copy up database but device was not available to copy to"));
  729. // }
  730. // }
  731. return CWinApp::ExitInstance();
  732. }
  733. // return TRUE if there is more idle processing to do
  734. BOOL CCP_MainApp::OnIdle(LONG lCount)
  735. {
  736. // let winapp handle its idle processing
  737. if( CWinApp::OnIdle(lCount) )
  738. return TRUE;
  739. return FALSE;
  740. }
  741. CString CCP_MainApp::GetTargetName()
  742. {
  743. TCHAR cWindowText[100];
  744. HWND hParent = m_hTargetWnd;
  745. ::GetWindowText(hParent, cWindowText, 100);
  746. int nCount = 0;
  747. while(STRLEN(cWindowText) <= 0)
  748. {
  749. hParent = ::GetParent(hParent);
  750. if(hParent == NULL)
  751. break;
  752. ::GetWindowText(hParent, cWindowText, 100);
  753. nCount++;
  754. if(nCount > 100)
  755. {
  756. Log(_T("GetTargetName reached maximum search depth of 100"));
  757. break;
  758. }
  759. }
  760. return cWindowText;
  761. }
  762. void CCP_MainApp::SetConnectCV(bool bConnect)
  763. {
  764. m_CopyThread.SetConnectCV(bConnect);
  765. if(bConnect)
  766. m_pMainFrame->m_TrayIcon.SetIcon(IDR_MAINFRAME);
  767. else
  768. m_pMainFrame->m_TrayIcon.SetIcon(IDI_DITTO_NOCOPYCB);
  769. if(QPasteWnd())
  770. {
  771. QPasteWnd()->SetCaptionColorActive(!g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  772. QPasteWnd()->RefreshNc();
  773. }
  774. }
  775. void CCP_MainApp::OnDeleteID(long lID)
  776. {
  777. if(QPasteWnd())
  778. {
  779. QPasteWnd()->PostMessage(NM_ITEM_DELETED, lID, 0);
  780. }
  781. }
  782. bool CCP_MainApp::ImportClips(HWND hWnd)
  783. {
  784. OPENFILENAME FileName;
  785. TCHAR szFileName[400];
  786. TCHAR szDir[400];
  787. memset(&FileName, 0, sizeof(FileName));
  788. memset(szFileName, 0, sizeof(szFileName));
  789. memset(&szDir, 0, sizeof(szDir));
  790. CString csInitialDir = CGetSetOptions::GetLastImportDir();
  791. STRCPY(szDir, csInitialDir);
  792. FileName.lStructSize = sizeof(FileName);
  793. FileName.lpstrTitle = _T("Import Clips");
  794. FileName.Flags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  795. FileName.nMaxFile = 400;
  796. FileName.lpstrFile = szFileName;
  797. FileName.lpstrInitialDir = szDir;
  798. FileName.lpstrFilter = _T("Exported Ditto Clips (.dto)\0*.dto\0\0");
  799. FileName.lpstrDefExt = _T("dto");
  800. if(GetOpenFileName(&FileName) == 0)
  801. return false;
  802. using namespace nsPath;
  803. CPath path(FileName.lpstrFile);
  804. CString csPath = path.GetPath();
  805. CGetSetOptions::SetLastImportDir(csPath);
  806. try
  807. {
  808. CppSQLite3DB db;
  809. db.open(FileName.lpstrFile);
  810. CClip_ImportExport clip;
  811. if(clip.ImportFromSqliteDB(db, true, false))
  812. {
  813. CShowMainFrame Show;
  814. CString cs;
  815. cs.Format(_T("%s %d "), theApp.m_Language.GetString("Import_Successfully", "Successfully imported"), clip.m_lImportCount);
  816. if(clip.m_lImportCount = 1)
  817. cs += theApp.m_Language.GetString("Clip", "clip");
  818. else
  819. cs += theApp.m_Language.GetString("Clips", "clips");
  820. MessageBox(hWnd, cs, _T("Ditto"), MB_OK);
  821. }
  822. else
  823. {
  824. CShowMainFrame Show;
  825. MessageBox(hWnd, theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"), _T("Ditto"), MB_OK);
  826. }
  827. }
  828. catch (CppSQLite3Exception& e)
  829. {
  830. ASSERT(FALSE);
  831. CString csError;
  832. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  833. MessageBox(hWnd, csError, _T("Ditto"), MB_OK);
  834. }
  835. return true;
  836. }
  837. void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
  838. {
  839. Log(StrF(_T("ShowCommandLineError %s - %s"), csTitle, csMessage));
  840. CToolTipEx *pErrorWnd = new CToolTipEx;
  841. pErrorWnd->Create(NULL);
  842. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  843. CPoint pt;
  844. CRect rcScreen;
  845. GetMonitorRect(0, &rcScreen);
  846. pt = rcScreen.BottomRight();
  847. CRect cr = pErrorWnd->GetBoundsRect();
  848. pt.x -= max(cr.Width()+50, 150);
  849. pt.y -= max(cr.Height()+50, 150);
  850. pErrorWnd->Show(pt);
  851. Sleep(4000);
  852. pErrorWnd->DestroyWindow();
  853. }
  854. BOOL CCP_MainApp::GetClipData(long lID, CClipFormat &Clip)
  855. {
  856. BOOL bRet = FALSE;
  857. try
  858. {
  859. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT ooData FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(Clip.m_cfType));
  860. if(q.eof() == false)
  861. {
  862. int nDataLen = 0;
  863. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  864. if(cData != NULL)
  865. {
  866. Clip.m_hgData = NewGlobal(nDataLen);
  867. ::CopyToGlobalHP(Clip.m_hgData, (LPVOID)cData, nDataLen);
  868. bRet = TRUE;
  869. }
  870. }
  871. }
  872. CATCH_SQLITE_EXCEPTION
  873. return bRet;
  874. }
  875. bool CCP_MainApp::EditItems(CClipIDs &Ids, bool bShowError)
  876. {
  877. m_pMainFrame->ShowEditWnd(Ids);
  878. return true;
  879. }