MainFrm.cpp 25 KB

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