MainFrm.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "afxole.h"
  7. #include "Misc.h"
  8. #include "CopyProperties.h"
  9. #include "InternetUpdate.h"
  10. #include ".\mainfrm.h"
  11. #include "focusdll\focusdll.h"
  12. #include "HyperLink.h"
  13. #include "tinyxml.h"
  14. #include "Path.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #define WM_ICON_NOTIFY WM_APP+10
  21. bool CShowMainFrame::m_bShowingMainFrame = false;
  22. CShowMainFrame::CShowMainFrame() : m_bHideMainFrameOnExit(false)
  23. {
  24. if(m_bShowingMainFrame == false)
  25. {
  26. theApp.m_pMainFrame->m_TrayIcon.MaximiseFromTray(theApp.m_pMainFrame);
  27. m_bHideMainFrameOnExit = true;
  28. m_bShowingMainFrame = true;
  29. }
  30. }
  31. CShowMainFrame::~CShowMainFrame()
  32. {
  33. if(m_bHideMainFrameOnExit)
  34. {
  35. theApp.m_pMainFrame->m_TrayIcon.MinimiseToTray(theApp.m_pMainFrame);
  36. m_bShowingMainFrame = false;
  37. }
  38. }
  39. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  40. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  41. //{{AFX_MSG_MAP(CMainFrame)
  42. ON_WM_CREATE()
  43. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  44. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  45. ON_WM_TIMER()
  46. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  47. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  48. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  49. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  50. //}}AFX_MSG_MAP
  51. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  52. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  53. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  54. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  55. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  56. ON_WM_CLOSE()
  57. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  58. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  59. ON_MESSAGE(WM_FOCUS_CHANGED, OnFocusChanged)
  60. ON_MESSAGE(WM_FOCUS_CHANGED+1, OnKeyBoardChanged)
  61. ON_MESSAGE(WM_CUSTOMIZE_TRAY_MENU, OnCustomizeTrayMenu)
  62. ON_COMMAND(ID_FIRST_IMPORT, OnFirstImport)
  63. ON_MESSAGE(WM_EDIT_WND_CLOSING, OnEditWndClose)
  64. ON_WM_DESTROY()
  65. ON_COMMAND(ID_FIRST_NEWCLIP, OnFirstNewclip)
  66. ON_MESSAGE(WM_SET_CONNECTED, OnSetConnected)
  67. END_MESSAGE_MAP()
  68. static UINT indicators[] =
  69. {
  70. ID_SEPARATOR, // status line indicator
  71. ID_INDICATOR_CAPS,
  72. ID_INDICATOR_NUM,
  73. ID_INDICATOR_SCRL,
  74. };
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CMainFrame construction/destruction
  77. CMainFrame::CMainFrame()
  78. {
  79. m_pEditFrameWnd = NULL;
  80. }
  81. CMainFrame::~CMainFrame()
  82. {
  83. if(g_Opt.m_bUseHookDllForFocus)
  84. StopMonitoringFocusChanges();
  85. CGetSetOptions::SetMainHWND(0);
  86. }
  87. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  88. {
  89. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  90. return -1;
  91. //Center the main window so message boxes are in the center
  92. CRect rcScreen;
  93. GetMonitorRect(0, &rcScreen);
  94. CPoint cpCenter = rcScreen.CenterPoint();
  95. MoveWindow(cpCenter.x,cpCenter.x, -2, -2);
  96. //Then set the main window to transparent so it's never shown
  97. //if it is shown then only the task tray icon
  98. m_Transparency.SetTransparent(m_hWnd, 0, true);
  99. SetWindowText(_T(""));
  100. if(g_Opt.m_bUseHookDllForFocus)
  101. MonitorFocusChanges(m_hWnd, WM_FOCUS_CHANGED);
  102. SetWindowText(_T("Ditto"));
  103. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  104. m_TrayIcon.Create(
  105. NULL, // Let icon deal with its own messages
  106. WM_ICON_NOTIFY, // Icon notify message to use
  107. _T("Ditto"), // tooltip
  108. hIcon,
  109. IDR_MENU, // ID of tray icon
  110. FALSE,
  111. _T(""), // balloon tip
  112. _T(""), // balloon title
  113. NULL, // balloon icon
  114. 20 );
  115. m_TrayIcon.SetSingleClickSelect(TRUE);
  116. m_TrayIcon.MinimiseToTray(this);
  117. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  118. //Only if in release
  119. #ifndef _DEBUG
  120. {
  121. //If not showing the icon show it for 40 seconds so they can get to the option
  122. //in case they can't remember the hot keys or something like that
  123. if(!(CGetSetOptions::GetShowIconInSysTray()))
  124. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  125. }
  126. #endif
  127. //don't check for updates if running from a U3 device
  128. if(!g_Opt.m_bU3)
  129. {
  130. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  131. }
  132. SetTimer(CLOSE_WINDOW_TIMER, ONE_MINUTE*60, 0);
  133. SetTimer(REMOVE_OLD_REMOTE_COPIES, ONE_DAY, 0);
  134. theApp.Delayed_RemoveOldEntries(ONE_MINUTE*2);
  135. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  136. theApp.AfterMainCreate();
  137. return 0;
  138. }
  139. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  140. {
  141. if(cs.hMenu!=NULL)
  142. {
  143. ::DestroyMenu(cs.hMenu); // delete menu if loaded
  144. cs.hMenu = NULL; // no menu for this window
  145. }
  146. if( !CFrameWnd::PreCreateWindow(cs) )
  147. return FALSE;
  148. WNDCLASS wc;
  149. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  150. wc.lpfnWndProc = AfxWndProc;
  151. wc.cbClsExtra = 0;
  152. wc.cbWndExtra = 0;
  153. wc.hInstance = AfxGetInstanceHandle();
  154. wc.hIcon = NULL;
  155. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  156. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  157. wc.lpszMenuName = NULL;
  158. wc.lpszClassName = _T("Ditto");
  159. // Create the QPaste window class
  160. if (!AfxRegisterClass(&wc))
  161. return FALSE;
  162. cs.lpszClass = wc.lpszClassName;
  163. return TRUE;
  164. }
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CMainFrame diagnostics
  167. #ifdef _DEBUG
  168. void CMainFrame::AssertValid() const
  169. {
  170. CFrameWnd::AssertValid();
  171. }
  172. void CMainFrame::Dump(CDumpContext& dc) const
  173. {
  174. CFrameWnd::Dump(dc);
  175. }
  176. #endif //_DEBUG
  177. /////////////////////////////////////////////////////////////////////////////
  178. // CMainFrame message handlers
  179. void CMainFrame::OnFirstOption()
  180. {
  181. CShowMainFrame Show;
  182. DoOptions(this);
  183. QuickPaste.UpdateFont();
  184. }
  185. void CMainFrame::OnFirstExit()
  186. {
  187. this->SendMessage(WM_CLOSE, 0, 0);
  188. }
  189. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  190. {
  191. if(wParam == theApp.m_pDittoHotKey->m_Atom)
  192. {
  193. if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && QuickPaste.IsWindowVisibleEx())
  194. {
  195. QuickPaste.HideQPasteWnd();
  196. }
  197. else
  198. {
  199. theApp.TargetActiveWindow();
  200. QuickPaste.ShowQPasteWnd(this, false, true);
  201. }
  202. }
  203. else if(!g_Opt.m_bU3 && wParam == theApp.m_pNamedCopy->m_Atom)
  204. {
  205. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  206. {
  207. theApp.m_QuickPasteMode = CCP_MainApp::ADDING_QUICK_PASTE;
  208. theApp.SendCopy();
  209. m_pTypingToolTip = new CToolTipEx;
  210. m_pTypingToolTip->Create(this);
  211. csTypeToolTipTitle = theApp.m_Language.GetString("Named_Copy_Title", "Ditto - Named Copy");
  212. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle);
  213. CRect rcScreen;
  214. GetMonitorRect(0, &rcScreen);
  215. m_ToolTipPoint = GetFocusedCaretPos();
  216. if(m_ToolTipPoint.x < 0 || m_ToolTipPoint.y < 0)
  217. {
  218. CRect cr;
  219. ::GetWindowRect(theApp.m_hTargetWnd, cr);
  220. m_ToolTipPoint = cr.CenterPoint();
  221. }
  222. m_ToolTipPoint.Offset(-15, 15);
  223. m_pTypingToolTip->Show(m_ToolTipPoint);
  224. //If they don't type anything for 2 seconds stop looking
  225. SetTimer(STOP_LOOKING_FOR_KEYBOARD, 20000, NULL);
  226. MonitorKeyboardChanges(m_hWnd, WM_FOCUS_CHANGED+1);
  227. SetCaptureKeys(true);
  228. }
  229. }
  230. else if(!g_Opt.m_bU3 && wParam == theApp.m_pNamedPaste->m_Atom)
  231. {
  232. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  233. {
  234. theApp.m_QuickPasteMode = CCP_MainApp::PASTING_QUICK_PASTE;
  235. m_pTypingToolTip = new CToolTipEx;
  236. m_pTypingToolTip->Create(this);
  237. csTypeToolTipTitle = theApp.m_Language.GetString("Named_Paste_Title", "Ditto - Named Paste");
  238. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle);
  239. CRect rcScreen;
  240. m_ToolTipPoint = GetFocusedCaretPos();
  241. if(m_ToolTipPoint.x < 0 || m_ToolTipPoint.y < 0)
  242. {
  243. CRect cr;
  244. ::GetWindowRect(theApp.m_hTargetWnd, cr);
  245. m_ToolTipPoint = cr.CenterPoint();
  246. }
  247. m_ToolTipPoint.Offset(-15, 15);
  248. m_pTypingToolTip->Show(m_ToolTipPoint);
  249. //If they don't type anything for 2 seconds stop looking
  250. SetTimer(STOP_LOOKING_FOR_KEYBOARD, 20000, NULL);
  251. MonitorKeyboardChanges(m_hWnd, WM_FOCUS_CHANGED+1);
  252. SetCaptureKeys(true);
  253. }
  254. }
  255. else if(wParam == theApp.m_pPosOne->m_Atom)
  256. {
  257. DoFirstTenPositionsPaste(0);
  258. }
  259. else if(wParam == theApp.m_pPosTwo->m_Atom)
  260. {
  261. DoFirstTenPositionsPaste(1);
  262. }
  263. else if(wParam == theApp.m_pPosThree->m_Atom)
  264. {
  265. DoFirstTenPositionsPaste(2);
  266. }
  267. else if(wParam == theApp.m_pPosFour->m_Atom)
  268. {
  269. DoFirstTenPositionsPaste(3);
  270. }
  271. else if(wParam == theApp.m_pPosFive->m_Atom)
  272. {
  273. DoFirstTenPositionsPaste(4);
  274. }
  275. else if(wParam == theApp.m_pPosSix->m_Atom)
  276. {
  277. DoFirstTenPositionsPaste(5);
  278. }
  279. else if(wParam == theApp.m_pPosSeven->m_Atom)
  280. {
  281. DoFirstTenPositionsPaste(6);
  282. }
  283. else if(wParam == theApp.m_pPosEight->m_Atom)
  284. {
  285. DoFirstTenPositionsPaste(7);
  286. }
  287. else if(wParam == theApp.m_pPosNine->m_Atom)
  288. {
  289. DoFirstTenPositionsPaste(8);
  290. }
  291. else if(wParam == theApp.m_pPosTen->m_Atom)
  292. {
  293. DoFirstTenPositionsPaste(9);
  294. }
  295. return TRUE;
  296. }
  297. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  298. {
  299. try
  300. {
  301. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, bIsGroup, lDate FROM Main ")
  302. _T("WHERE ((bIsGroup = 1 AND lParentID = -1) OR bIsGroup = 0) ")
  303. _T("ORDER BY bIsGroup ASC, lDate DESC ")
  304. _T("LIMIT 1 OFFSET %d"), nPos);
  305. if(q.eof() == false)
  306. {
  307. if(q.getIntField(_T("bIsGroup")) == FALSE)
  308. {
  309. //Don't move these to the top
  310. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  311. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  312. CProcessPaste paste;
  313. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  314. paste.m_bActivateTarget = false;
  315. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  316. paste.DoPaste();
  317. theApp.OnPasteCompleted();
  318. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  319. }
  320. }
  321. }
  322. CATCH_SQLITE_EXCEPTION
  323. }
  324. void CMainFrame::OnTimer(UINT nIDEvent)
  325. {
  326. switch(nIDEvent)
  327. {
  328. case HIDE_ICON_TIMER:
  329. {
  330. m_TrayIcon.HideIcon();
  331. KillTimer(nIDEvent);
  332. break;
  333. }
  334. case CLOSE_WINDOW_TIMER:
  335. {
  336. QuickPaste.CloseQPasteWnd();
  337. break;
  338. }
  339. case REMOVE_OLD_ENTRIES_TIMER:
  340. {
  341. theApp.m_bRemoveOldEntriesPending = false;
  342. RemoveOldEntries();
  343. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  344. break;
  345. }
  346. case CHECK_FOR_UPDATE:
  347. {
  348. KillTimer(CHECK_FOR_UPDATE);
  349. CInternetUpdate Update;
  350. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  351. {
  352. PostMessage(WM_CLOSE, 0, 0);
  353. }
  354. else
  355. {
  356. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*60*24, NULL);
  357. }
  358. break;
  359. }
  360. case CLOSE_APP:
  361. {
  362. if(theApp.m_bShowingOptions == false)
  363. {
  364. PostMessage(WM_CLOSE, 0, 0);
  365. KillTimer(CLOSE_APP);
  366. }
  367. break;
  368. }
  369. case STOP_MONITORING_KEYBOARD_TIMER:
  370. {
  371. StopLookingForKeystrokes(false);
  372. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  373. {
  374. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  375. {
  376. PasteQuickPasteEntry(m_csKeyboardPaste);
  377. }
  378. else
  379. {
  380. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  381. }
  382. StopLookingForKeystrokes(true);
  383. }
  384. break;
  385. }
  386. case STOP_LOOKING_FOR_KEYBOARD:
  387. {
  388. if(theApp.m_QuickPasteMode == CCP_MainApp::ADDING_QUICK_PASTE)
  389. {
  390. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  391. }
  392. //They didn't type anything within 2 seconds stop looking
  393. StopLookingForKeystrokes(true);
  394. break;
  395. }
  396. case REMOVE_OLD_REMOTE_COPIES:
  397. AfxBeginThread(CMainFrame::RemoteOldRemoteFilesThread, NULL);
  398. break;
  399. }
  400. CFrameWnd::OnTimer(nIDEvent);
  401. }
  402. void CMainFrame::StopLookingForKeystrokes(bool bInitAppVaribles)
  403. {
  404. StopMonitoringKeyboardChanges();
  405. SetCaptureKeys(false);
  406. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  407. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  408. if(bInitAppVaribles)
  409. {
  410. theApp.m_QuickPasteMode = CCP_MainApp::NONE_QUICK_PASTE;
  411. m_csKeyboardPaste.Empty();
  412. delete theApp.m_pQuickPasteClip;
  413. theApp.m_pQuickPasteClip = NULL;
  414. m_pTypingToolTip->Hide();
  415. m_pTypingToolTip->DestroyWindow();
  416. }
  417. }
  418. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  419. {
  420. if(lParam)
  421. {
  422. if(!m_TrayIcon.Visible())
  423. {
  424. KillTimer(HIDE_ICON_TIMER);
  425. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  426. }
  427. }
  428. if(wParam)
  429. m_TrayIcon.ShowIcon();
  430. else
  431. m_TrayIcon.HideIcon();
  432. return TRUE;
  433. }
  434. void CMainFrame::OnFirstShowquickpaste()
  435. {
  436. QuickPaste.ShowQPasteWnd(this, true, false);
  437. }
  438. void CMainFrame::OnFirstToggleConnectCV()
  439. {
  440. theApp.ToggleConnectCV();
  441. }
  442. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI* pCmdUI)
  443. {
  444. theApp.UpdateMenuConnectCV( pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV );
  445. }
  446. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  447. {
  448. long lID = (long)wParam;
  449. if(lID > 0)
  450. {
  451. bool bOldState = theApp.EnableCbCopy(false);
  452. theApp.m_bShowingOptions = true;
  453. CCopyProperties props(lID, this);
  454. props.SetHideOnKillFocus(true);
  455. props.DoModal();
  456. theApp.m_bShowingOptions = false;
  457. theApp.EnableCbCopy( bOldState );
  458. }
  459. return TRUE;
  460. }
  461. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  462. {
  463. SetTimer(CLOSE_APP, 100, NULL);
  464. return TRUE;
  465. }
  466. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  467. {
  468. // if the delay is undesirable, this could be altered to save one at a time,
  469. // allowing the processing of other messages between saving clips.
  470. theApp.SaveCopyClips();
  471. return TRUE;
  472. }
  473. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  474. {
  475. // target before mouse messages change the focus
  476. if( theApp.m_bShowingQuickPaste &&
  477. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  478. { theApp.TargetActiveWindow(); }
  479. return CFrameWnd::PreTranslateMessage(pMsg);
  480. }
  481. void CMainFrame::OnClose()
  482. {
  483. if(m_pEditFrameWnd)
  484. {
  485. if(m_pEditFrameWnd->CloseAll() == false)
  486. return;
  487. }
  488. theApp.BeforeMainClose();
  489. CFrameWnd::OnClose();
  490. }
  491. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  492. {
  493. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  494. CClipList *pClipList = (CClipList*)wParam;
  495. if(pClipList == NULL)
  496. {
  497. LogSendRecieveInfo("---------ERROR pClipList == NULL");
  498. return FALSE;
  499. }
  500. BOOL bSetToClipBoard = (BOOL)lParam;
  501. if(bSetToClipBoard)
  502. {
  503. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  504. CClip *pClip = pClipList->GetTail();
  505. if(pClip)
  506. {
  507. CClip NewClip;
  508. NewClip = *pClip;
  509. LogSendRecieveInfo("---------After =");
  510. CProcessPaste paste;
  511. //Don't send the paste just load it into memory
  512. paste.m_bSendPaste = false;
  513. paste.m_pOle->PutFormatOnClipboard(&NewClip.m_Formats, false);
  514. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  515. LogSendRecieveInfo("---------After LoadFormats");
  516. paste.DoPaste();
  517. }
  518. else
  519. {
  520. LogSendRecieveInfo("---------GetTail returned NULL");
  521. }
  522. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  523. }
  524. pClipList->AddToDB(true);
  525. LogSendRecieveInfo("---------After AddToDB");
  526. CClip *pClip = pClipList->GetTail();
  527. if(pClip)
  528. {
  529. theApp.m_FocusID = pClip->m_ID;
  530. }
  531. theApp.RefreshView();
  532. delete pClipList;
  533. pClipList = NULL;
  534. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  535. theApp.Delayed_RemoveOldEntries(60000);
  536. return TRUE;
  537. }
  538. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  539. {
  540. CString csNewText = (TCHAR*)wParam;
  541. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  542. return TRUE;
  543. }
  544. LRESULT CMainFrame::OnKeyBoardChanged(WPARAM wParam, LPARAM lParam)
  545. {
  546. if(wParam == VK_ESCAPE)
  547. {
  548. StopLookingForKeystrokes(true);
  549. }
  550. else if(wParam == VK_RETURN)
  551. {
  552. StopLookingForKeystrokes(false);
  553. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  554. {
  555. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  556. {
  557. PasteQuickPasteEntry(m_csKeyboardPaste);
  558. }
  559. else
  560. {
  561. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  562. }
  563. }
  564. StopLookingForKeystrokes(true);
  565. }
  566. else if((wParam >= 32 && wParam <= 96) || wParam == VK_BACK)
  567. {
  568. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  569. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  570. bool bContinue = true;
  571. if(wParam == VK_BACK)
  572. {
  573. m_csKeyboardPaste = m_csKeyboardPaste.Left(m_csKeyboardPaste.GetLength()-1);
  574. }
  575. else
  576. {
  577. CString cs((char)wParam);
  578. m_csKeyboardPaste += cs;
  579. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  580. {
  581. try
  582. {
  583. int nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText LIKE \'%%%s%%\'"), m_csKeyboardPaste);
  584. if(nCount == 1)
  585. {
  586. nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText = \'%s\'"), m_csKeyboardPaste);
  587. if(nCount == 1)
  588. {
  589. StopLookingForKeystrokes(false);
  590. PasteQuickPasteEntry(m_csKeyboardPaste);
  591. StopLookingForKeystrokes(true);
  592. bContinue = false;
  593. }
  594. }
  595. }
  596. CATCH_SQLITE_EXCEPTION
  597. }
  598. }
  599. if(bContinue)
  600. {
  601. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle + "\n\n" + m_csKeyboardPaste);
  602. m_pTypingToolTip->Show(m_ToolTipPoint);
  603. SetTimer(STOP_MONITORING_KEYBOARD_TIMER, 10000, NULL);
  604. Log(StrF(_T("OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  605. }
  606. }
  607. else
  608. {
  609. CString cs((char)wParam);
  610. Log(StrF(_T("INVALID Key OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  611. }
  612. return TRUE;
  613. }
  614. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  615. {
  616. if(g_Opt.m_bUseHookDllForFocus == FALSE)
  617. return TRUE;
  618. HWND hFocus = (HWND)wParam;
  619. HWND hParent = hFocus;
  620. HWND hLastGoodParent = hParent;
  621. static DWORD dLastDittoHasFocusTick = 0;
  622. //Sometimes when we bring ditto up there will come a null focus
  623. //rite after that
  624. if(hFocus == NULL && (GetTickCount() - dLastDittoHasFocusTick < 500))
  625. {
  626. Log(_T("NULL focus within 500 ticks of bringing up ditto"));
  627. return TRUE;
  628. }
  629. else if(hFocus == NULL)
  630. {
  631. Log(_T("NULL focus received"));
  632. }
  633. //only proceed if something changed
  634. if(theApp.m_hTargetWnd == hFocus)
  635. return TRUE;
  636. TCHAR cWindowText[100];
  637. ::GetWindowText(hFocus, cWindowText, 100);
  638. HWND hTray = ::FindWindow(_T("Shell_TrayWnd"), _T(""));
  639. // Log(StrF("Focus = %d", hFocus));
  640. int nCount = 0;
  641. while(true)
  642. {
  643. hParent = ::GetParent(hParent);
  644. if(hParent == NULL)
  645. break;
  646. // Log(StrF("Focus2 = %d", hParent));
  647. //allow focus on edit window
  648. if(m_pEditFrameWnd && hParent == m_pEditFrameWnd->GetSafeHwnd())
  649. {
  650. break;
  651. }
  652. hLastGoodParent = hParent;
  653. nCount++;
  654. if(nCount > 100)
  655. {
  656. Log(_T("OnFocusChanged reached maximum search depth of 100"));
  657. break;
  658. }
  659. }
  660. //If the parent is ditto or the tray icon then don't set focus to that window
  661. if(hLastGoodParent == m_hWnd || hLastGoodParent == hTray)
  662. {
  663. Log(_T("Ditto Has Focus"));
  664. theApp.m_bDittoHasFocus = true;
  665. dLastDittoHasFocusTick = GetTickCount();
  666. }
  667. else
  668. {
  669. theApp.m_bDittoHasFocus = false;
  670. theApp.m_hTargetWnd = hFocus;
  671. if(theApp.QPasteWnd() )
  672. theApp.QPasteWnd()->UpdateStatus(true);
  673. }
  674. return TRUE;
  675. }
  676. void CMainFrame::OnFirstHelp()
  677. {
  678. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  679. csFile += "DittoGettingStarted.htm";
  680. CHyperLink::GotoURL(csFile, SW_SHOW);
  681. }
  682. LRESULT CMainFrame::OnCustomizeTrayMenu(WPARAM wParam, LPARAM lParam)
  683. {
  684. CMenu *pMenu = (CMenu*)wParam;
  685. if(pMenu)
  686. {
  687. theApp.m_Language.UpdateTrayIconRightClickMenu(pMenu);
  688. }
  689. return true;
  690. }
  691. bool CMainFrame::PasteQuickPasteEntry(CString csQuickPaste)
  692. {
  693. Log(StrF(_T("PasteQuickPasteEntry -- %s"), csQuickPaste));
  694. CString csTitle = theApp.m_Language.GetString("Named_Paste_Title", "Ditto - Named Paste");
  695. bool bRet = false;
  696. try
  697. {
  698. csQuickPaste.Replace(_T("'"), _T("''"));
  699. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE QuickPasteText = '%s'"), csQuickPaste);
  700. if(q.eof() == false)
  701. {
  702. CClip Clip;
  703. if(Clip.LoadFormats(q.getIntField(_T("lID"))))
  704. {
  705. CProcessPaste paste;
  706. paste.m_pOle->PutFormatOnClipboard(&Clip.m_Formats, false);
  707. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  708. if(paste.DoPaste())
  709. {
  710. bRet = TRUE;
  711. }
  712. }
  713. else
  714. {
  715. CString csError = theApp.m_Language.GetString("Named_Paste_Error1", "Error loading data");
  716. ShowErrorMessage(csTitle, StrF(_T("%s - id:%d"), csError, q.getIntField(_T("lID"))));
  717. }
  718. }
  719. else
  720. {
  721. CString csError = theApp.m_Language.GetString("Named_Paste_Error2", "Error finding clip with QuickPaste Text of");
  722. ShowErrorMessage(csTitle, StrF(_T("%s - '%s'"), csError, csQuickPaste));
  723. }
  724. }
  725. catch (CppSQLite3Exception& e)
  726. {
  727. CString csError = theApp.m_Language.GetString("Named_Paste_Error3", "Exception finding the clip");
  728. ShowErrorMessage(csTitle, StrF(_T("%s - '%s' - %s"), csError, csQuickPaste, e.errorMessage()));
  729. }
  730. return bRet;
  731. }
  732. bool CMainFrame::SaveQuickPasteEntry(CString csQuickPaste, CClipList *pClipList)
  733. {
  734. Log(StrF(_T("SaveQuickPasteEntry text = %s - recived copy = %d"), csQuickPaste, pClipList != NULL));
  735. CString csTitle = theApp.m_Language.GetString("Named_Copy_Title", "Ditto - Named Copy");
  736. bool bRet = false;
  737. if(pClipList)
  738. {
  739. try
  740. {
  741. CClip *pClip = pClipList->GetHead();
  742. pClip->m_csQuickPaste = csQuickPaste;
  743. pClip->m_lDontAutoDelete = (long)CTime::GetCurrentTime().GetTime();
  744. if(csQuickPaste.IsEmpty() == FALSE)
  745. {
  746. //Remove any other instances of this quick paste
  747. csQuickPaste.Replace(_T("'"), _T("''"));
  748. int nCount = theApp.m_db.execDMLEx(_T("UPDATE Main SET QuickPasteText = '' WHERE QuickPasteText = '%s';"), csQuickPaste);
  749. if(nCount > 0)
  750. {
  751. Log(StrF(_T("Removed quick paste '%s', count = %d"), csQuickPaste, nCount));
  752. }
  753. }
  754. int count = pClipList->AddToDB(true);
  755. if(count > 0)
  756. {
  757. long lID = pClipList->GetTail()->m_ID;
  758. theApp.OnCopyCompleted(lID, count);
  759. bRet = true;
  760. }
  761. else
  762. {
  763. CString csError = theApp.m_Language.GetString("Named_Copy_Error1", "Error saving Named Coy to Database");
  764. ShowErrorMessage(csTitle, csError);
  765. }
  766. }
  767. catch (CppSQLite3Exception& e)
  768. {
  769. Log(StrF(_T("SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage()));
  770. ASSERT(FALSE);
  771. CString csError = theApp.m_Language.GetString("Named_Copy_Error2", "Exception saving Named Copy");
  772. ShowErrorMessage(csTitle, StrF(_T("%s - %s"), csError, e.errorMessage()));
  773. }
  774. }
  775. else
  776. {
  777. CString csError = theApp.m_Language.GetString("Named_Copy_Error3", "Ditto did not receive a copy");
  778. ShowErrorMessage(csTitle, csError);
  779. }
  780. return bRet;
  781. }
  782. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  783. {
  784. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  785. CToolTipEx *pErrorWnd = new CToolTipEx;
  786. pErrorWnd->Create(this);
  787. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  788. CPoint pt;
  789. CRect rcScreen;
  790. GetMonitorRect(0, &rcScreen);
  791. pt = rcScreen.BottomRight();
  792. CRect cr = pErrorWnd->GetBoundsRect();
  793. pt.x -= max(cr.Width()+50, 150);
  794. pt.y -= max(cr.Height()+50, 150);
  795. pErrorWnd->Show(pt);
  796. pErrorWnd->HideWindowInXMilliSeconds(4000);
  797. }
  798. void CMainFrame::DeleteOldRemoteCopies(CString csDir)
  799. {
  800. //must be deleting a sub folder in the musicgen directory
  801. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  802. return;
  803. FIX_CSTRING_PATH(csDir);
  804. CTime ctOld = CTime::GetCurrentTime();
  805. CTime ctFile;
  806. ctOld -= CTimeSpan(0, 0, 0, 1);
  807. CFileFind Find;
  808. CString csFindString;
  809. csFindString.Format(_T("%s*.*"), csDir);
  810. BOOL bFound = Find.FindFile(csFindString);
  811. while(bFound)
  812. {
  813. bFound = Find.FindNextFile();
  814. if(Find.IsDots())
  815. continue;
  816. if(Find.IsDirectory())
  817. {
  818. CString csDir(Find.GetFilePath());
  819. DeleteOldRemoteCopies(csDir);
  820. RemoveDirectory(csDir);
  821. }
  822. if(Find.GetLastAccessTime(ctFile))
  823. {
  824. //Delete the remote copied file if it has'nt been used for the last day
  825. if(ctFile < ctOld)
  826. {
  827. DeleteFile(Find.GetFilePath());
  828. }
  829. }
  830. else
  831. {
  832. DeleteFile(Find.GetFilePath());
  833. }
  834. }
  835. }
  836. UINT CMainFrame::RemoteOldRemoteFilesThread(LPVOID pParam)
  837. {
  838. CString csDir = CGetSetOptions::GetPath(PATH_REMOTE_FILES);
  839. if(FileExists(csDir))
  840. {
  841. DeleteOldRemoteCopies(csDir);
  842. }
  843. return TRUE;
  844. }
  845. void CMainFrame::OnFirstImport()
  846. {
  847. theApp.ImportClips(theApp.m_MainhWnd);
  848. }
  849. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  850. {
  851. CWaitCursor wait;
  852. bool bCreatedWindow = false;
  853. if(m_pEditFrameWnd == NULL)
  854. {
  855. m_pEditFrameWnd = new CEditFrameWnd;
  856. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  857. bCreatedWindow = true;
  858. }
  859. if(m_pEditFrameWnd)
  860. {
  861. m_pEditFrameWnd->EditIds(Ids);
  862. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  863. if(bCreatedWindow)
  864. {
  865. CSize sz;
  866. CPoint pt;
  867. CGetSetOptions::GetEditWndSize(sz);
  868. CGetSetOptions::GetEditWndPoint(pt);
  869. CRect cr(pt, sz);
  870. EnsureWindowVisible(&cr);
  871. m_pEditFrameWnd->MoveWindow(cr);
  872. }
  873. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  874. m_pEditFrameWnd->SetForegroundWindow();
  875. m_pEditFrameWnd->SetFocus();
  876. }
  877. }
  878. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  879. {
  880. m_pEditFrameWnd = NULL;
  881. return TRUE;
  882. }
  883. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  884. {
  885. if(wParam)
  886. theApp.SetConnectCV(true);
  887. else if(lParam)
  888. theApp.SetConnectCV(false);
  889. return TRUE;
  890. }
  891. void CMainFrame::OnDestroy()
  892. {
  893. CFrameWnd::OnDestroy();
  894. if(m_pEditFrameWnd)
  895. {
  896. m_pEditFrameWnd->DestroyWindow();
  897. }
  898. }
  899. void CMainFrame::OnFirstNewclip()
  900. {
  901. CClipIDs IDs;
  902. IDs.Add(-1);
  903. theApp.EditItems(IDs, true);
  904. }