MainFrm.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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(!g_Opt.m_bU3 && wParam == theApp.m_pNamedCopy->m_Atom)
  207. {
  208. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  209. {
  210. theApp.m_QuickPasteMode = CCP_MainApp::DITTO_BUFFER_QUICK_PASTE;
  211. theApp.CreateDittoCopyBuffer(1);
  212. SetTimer(END_DITTO_BUFFER_CLIPBOARD_TIMER, 2000, NULL);
  213. }
  214. }
  215. else if(!g_Opt.m_bU3 && wParam == theApp.m_pNamedPaste->m_Atom)
  216. {
  217. if(theApp.m_QuickPasteMode == CCP_MainApp::NONE_QUICK_PASTE)
  218. {
  219. CDittoCopyBuffer Past;
  220. Past.PastCopyBuffer(1);
  221. }
  222. }
  223. else if(wParam == theApp.m_pPosOne->m_Atom)
  224. {
  225. DoFirstTenPositionsPaste(0);
  226. }
  227. else if(wParam == theApp.m_pPosTwo->m_Atom)
  228. {
  229. DoFirstTenPositionsPaste(1);
  230. }
  231. else if(wParam == theApp.m_pPosThree->m_Atom)
  232. {
  233. DoFirstTenPositionsPaste(2);
  234. }
  235. else if(wParam == theApp.m_pPosFour->m_Atom)
  236. {
  237. DoFirstTenPositionsPaste(3);
  238. }
  239. else if(wParam == theApp.m_pPosFive->m_Atom)
  240. {
  241. DoFirstTenPositionsPaste(4);
  242. }
  243. else if(wParam == theApp.m_pPosSix->m_Atom)
  244. {
  245. DoFirstTenPositionsPaste(5);
  246. }
  247. else if(wParam == theApp.m_pPosSeven->m_Atom)
  248. {
  249. DoFirstTenPositionsPaste(6);
  250. }
  251. else if(wParam == theApp.m_pPosEight->m_Atom)
  252. {
  253. DoFirstTenPositionsPaste(7);
  254. }
  255. else if(wParam == theApp.m_pPosNine->m_Atom)
  256. {
  257. DoFirstTenPositionsPaste(8);
  258. }
  259. else if(wParam == theApp.m_pPosTen->m_Atom)
  260. {
  261. DoFirstTenPositionsPaste(9);
  262. }
  263. return TRUE;
  264. }
  265. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  266. {
  267. try
  268. {
  269. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, bIsGroup, lDate FROM Main ")
  270. _T("WHERE ((bIsGroup = 1 AND lParentID = -1) OR bIsGroup = 0) ")
  271. _T("ORDER BY bIsGroup ASC, lDate DESC ")
  272. _T("LIMIT 1 OFFSET %d"), nPos);
  273. if(q.eof() == false)
  274. {
  275. if(q.getIntField(_T("bIsGroup")) == FALSE)
  276. {
  277. //Don't move these to the top
  278. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  279. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  280. CProcessPaste paste;
  281. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  282. paste.m_bActivateTarget = false;
  283. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  284. paste.DoPaste();
  285. theApp.OnPasteCompleted();
  286. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  287. }
  288. }
  289. }
  290. CATCH_SQLITE_EXCEPTION
  291. }
  292. void CMainFrame::DoDittoCopyBufferPaste(int nCopyBuffer)
  293. {
  294. try
  295. {
  296. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CopyBuffer = %d"), nCopyBuffer);
  297. if(q.eof() == false)
  298. {
  299. //Don't move these to the top
  300. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  301. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  302. CProcessPaste paste;
  303. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  304. paste.m_bActivateTarget = false;
  305. paste.DoPaste();
  306. theApp.OnPasteCompleted();
  307. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  308. }
  309. }
  310. CATCH_SQLITE_EXCEPTION
  311. }
  312. void CMainFrame::OnTimer(UINT nIDEvent)
  313. {
  314. switch(nIDEvent)
  315. {
  316. case HIDE_ICON_TIMER:
  317. {
  318. m_TrayIcon.HideIcon();
  319. KillTimer(nIDEvent);
  320. break;
  321. }
  322. case CLOSE_WINDOW_TIMER:
  323. {
  324. QuickPaste.CloseQPasteWnd();
  325. break;
  326. }
  327. case REMOVE_OLD_ENTRIES_TIMER:
  328. {
  329. theApp.m_bRemoveOldEntriesPending = false;
  330. RemoveOldEntries();
  331. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  332. break;
  333. }
  334. case CHECK_FOR_UPDATE:
  335. {
  336. KillTimer(CHECK_FOR_UPDATE);
  337. CInternetUpdate Update;
  338. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  339. {
  340. PostMessage(WM_CLOSE, 0, 0);
  341. }
  342. else
  343. {
  344. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*60*24, NULL);
  345. }
  346. break;
  347. }
  348. case CLOSE_APP:
  349. {
  350. PostMessage(WM_CLOSE, 0, 0);
  351. KillTimer(CLOSE_APP);
  352. break;
  353. }
  354. case STOP_MONITORING_KEYBOARD_TIMER:
  355. {
  356. StopLookingForKeystrokes(false);
  357. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  358. {
  359. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  360. {
  361. PasteQuickPasteEntry(m_csKeyboardPaste);
  362. }
  363. else
  364. {
  365. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  366. }
  367. StopLookingForKeystrokes(true);
  368. }
  369. break;
  370. }
  371. case STOP_LOOKING_FOR_KEYBOARD:
  372. {
  373. if(theApp.m_QuickPasteMode == CCP_MainApp::ADDING_QUICK_PASTE)
  374. {
  375. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  376. }
  377. //They didn't type anything within 2 seconds stop looking
  378. StopLookingForKeystrokes(true);
  379. break;
  380. }
  381. case REMOVE_OLD_REMOTE_COPIES:
  382. AfxBeginThread(CMainFrame::RemoteOldRemoteFilesThread, NULL);
  383. break;
  384. case END_DITTO_BUFFER_CLIPBOARD_TIMER:
  385. KillTimer(END_DITTO_BUFFER_CLIPBOARD_TIMER);
  386. theApp.ClearDittoCopyBuffer();
  387. break;
  388. }
  389. CFrameWnd::OnTimer(nIDEvent);
  390. }
  391. void CMainFrame::StopLookingForKeystrokes(bool bInitAppVaribles)
  392. {
  393. StopMonitoringKeyboardChanges();
  394. SetCaptureKeys(false);
  395. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  396. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  397. if(bInitAppVaribles)
  398. {
  399. theApp.m_QuickPasteMode = CCP_MainApp::NONE_QUICK_PASTE;
  400. m_csKeyboardPaste.Empty();
  401. delete theApp.m_pQuickPasteClip;
  402. theApp.m_pQuickPasteClip = NULL;
  403. m_pTypingToolTip->Hide();
  404. m_pTypingToolTip->DestroyWindow();
  405. }
  406. }
  407. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  408. {
  409. if(lParam)
  410. {
  411. if(!m_TrayIcon.Visible())
  412. {
  413. KillTimer(HIDE_ICON_TIMER);
  414. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  415. }
  416. }
  417. if(wParam)
  418. m_TrayIcon.ShowIcon();
  419. else
  420. m_TrayIcon.HideIcon();
  421. return TRUE;
  422. }
  423. void CMainFrame::OnFirstShowquickpaste()
  424. {
  425. QuickPaste.ShowQPasteWnd(this, true, false, FALSE);
  426. }
  427. void CMainFrame::OnFirstToggleConnectCV()
  428. {
  429. theApp.ToggleConnectCV();
  430. }
  431. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI* pCmdUI)
  432. {
  433. theApp.UpdateMenuConnectCV( pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV );
  434. }
  435. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  436. {
  437. long lID = (long)wParam;
  438. if(lID > 0)
  439. {
  440. bool bOldState = theApp.EnableCbCopy(false);
  441. CCopyProperties props(lID, this);
  442. props.SetHideOnKillFocus(true);
  443. props.DoModal();
  444. theApp.EnableCbCopy( bOldState );
  445. }
  446. return TRUE;
  447. }
  448. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  449. {
  450. SetTimer(CLOSE_APP, 100, NULL);
  451. return TRUE;
  452. }
  453. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  454. {
  455. // if the delay is undesirable, this could be altered to save one at a time,
  456. // allowing the processing of other messages between saving clips.
  457. theApp.SaveCopyClips();
  458. return TRUE;
  459. }
  460. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  461. {
  462. // target before mouse messages change the focus
  463. if( theApp.m_bShowingQuickPaste &&
  464. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  465. { theApp.TargetActiveWindow(); }
  466. return CFrameWnd::PreTranslateMessage(pMsg);
  467. }
  468. void CMainFrame::OnClose()
  469. {
  470. CloseAllOpenDialogs();
  471. if(m_pEditFrameWnd)
  472. {
  473. if(m_pEditFrameWnd->CloseAll() == false)
  474. return;
  475. }
  476. theApp.BeforeMainClose();
  477. CFrameWnd::OnClose();
  478. }
  479. bool CMainFrame::CloseAllOpenDialogs()
  480. {
  481. bool bRet = false;
  482. DWORD dwordProcessId;
  483. DWORD dwordChildWindowProcessId;
  484. GetWindowThreadProcessId(this->m_hWnd, &dwordProcessId);
  485. ASSERT(dwordProcessId);
  486. CWnd *pTempWnd = GetDesktopWindow()->GetWindow(GW_CHILD);
  487. while((pTempWnd = pTempWnd->GetWindow(GW_HWNDNEXT)) != NULL)
  488. {
  489. if(pTempWnd->GetSafeHwnd() == NULL)
  490. break;
  491. GetWindowThreadProcessId(pTempWnd->GetSafeHwnd(), &dwordChildWindowProcessId);
  492. if(dwordChildWindowProcessId == dwordProcessId)
  493. {
  494. TCHAR szTemp[100];
  495. GetClassName(pTempWnd->GetSafeHwnd(), szTemp, 100);
  496. // #32770 is class name for dialogs so don't process the message if it is a dialog
  497. if(STRCMP(szTemp, _T("#32770")) == 0)
  498. {
  499. pTempWnd->SendMessage(WM_CLOSE, 0, 0);
  500. bRet = true;
  501. }
  502. }
  503. }
  504. MSG msg;
  505. while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  506. {
  507. TranslateMessage(&msg);
  508. DispatchMessage(&msg);
  509. }
  510. return bRet;
  511. }
  512. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  513. {
  514. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  515. CClipList *pClipList = (CClipList*)wParam;
  516. if(pClipList == NULL)
  517. {
  518. LogSendRecieveInfo("---------ERROR pClipList == NULL");
  519. return FALSE;
  520. }
  521. BOOL bSetToClipBoard = (BOOL)lParam;
  522. if(bSetToClipBoard)
  523. {
  524. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  525. CClip *pClip = pClipList->GetTail();
  526. if(pClip)
  527. {
  528. CClip NewClip;
  529. NewClip = *pClip;
  530. LogSendRecieveInfo("---------After =");
  531. CProcessPaste paste;
  532. //Don't send the paste just load it into memory
  533. paste.m_bSendPaste = false;
  534. paste.m_pOle->PutFormatOnClipboard(&NewClip.m_Formats, false);
  535. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  536. LogSendRecieveInfo("---------After LoadFormats");
  537. paste.DoPaste();
  538. }
  539. else
  540. {
  541. LogSendRecieveInfo("---------GetTail returned NULL");
  542. }
  543. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  544. }
  545. pClipList->AddToDB(true);
  546. LogSendRecieveInfo("---------After AddToDB");
  547. CClip *pClip = pClipList->GetTail();
  548. if(pClip)
  549. {
  550. theApp.m_FocusID = pClip->m_ID;
  551. }
  552. theApp.RefreshView();
  553. delete pClipList;
  554. pClipList = NULL;
  555. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  556. theApp.Delayed_RemoveOldEntries(60000);
  557. return TRUE;
  558. }
  559. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  560. {
  561. CString csNewText = (TCHAR*)wParam;
  562. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  563. return TRUE;
  564. }
  565. LRESULT CMainFrame::OnKeyBoardChanged(WPARAM wParam, LPARAM lParam)
  566. {
  567. if(wParam == VK_ESCAPE)
  568. {
  569. StopLookingForKeystrokes(true);
  570. }
  571. else if(wParam == VK_RETURN)
  572. {
  573. StopLookingForKeystrokes(false);
  574. if(m_csKeyboardPaste.IsEmpty() == FALSE)
  575. {
  576. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  577. {
  578. PasteQuickPasteEntry(m_csKeyboardPaste);
  579. }
  580. else
  581. {
  582. SaveQuickPasteEntry(m_csKeyboardPaste, theApp.m_pQuickPasteClip);
  583. }
  584. }
  585. StopLookingForKeystrokes(true);
  586. }
  587. else if((wParam >= 32 && wParam <= 96) || wParam == VK_BACK)
  588. {
  589. KillTimer(STOP_LOOKING_FOR_KEYBOARD);
  590. KillTimer(STOP_MONITORING_KEYBOARD_TIMER);
  591. bool bContinue = true;
  592. if(wParam == VK_BACK)
  593. {
  594. m_csKeyboardPaste = m_csKeyboardPaste.Left(m_csKeyboardPaste.GetLength()-1);
  595. }
  596. else
  597. {
  598. CString cs((char)wParam);
  599. m_csKeyboardPaste += cs;
  600. if(theApp.m_QuickPasteMode == CCP_MainApp::PASTING_QUICK_PASTE)
  601. {
  602. try
  603. {
  604. int nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText LIKE \'%%%s%%\'"), m_csKeyboardPaste);
  605. if(nCount == 1)
  606. {
  607. nCount = theApp.m_db.execScalarEx(_T("SELECT COUNT(lID) FROM Main WHERE QuickPasteText = \'%s\'"), m_csKeyboardPaste);
  608. if(nCount == 1)
  609. {
  610. StopLookingForKeystrokes(false);
  611. PasteQuickPasteEntry(m_csKeyboardPaste);
  612. StopLookingForKeystrokes(true);
  613. bContinue = false;
  614. }
  615. }
  616. }
  617. CATCH_SQLITE_EXCEPTION
  618. }
  619. }
  620. if(bContinue)
  621. {
  622. m_pTypingToolTip->SetToolTipText(csTypeToolTipTitle + "\n\n" + m_csKeyboardPaste);
  623. m_pTypingToolTip->Show(m_ToolTipPoint);
  624. SetTimer(STOP_MONITORING_KEYBOARD_TIMER, 10000, NULL);
  625. Log(StrF(_T("OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  626. }
  627. }
  628. else
  629. {
  630. CString cs((char)wParam);
  631. Log(StrF(_T("INVALID Key OnKeyboard Changed - %d - %s"), wParam, m_csKeyboardPaste));
  632. }
  633. return TRUE;
  634. }
  635. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  636. {
  637. if(g_Opt.m_bUseHookDllForFocus == FALSE)
  638. return TRUE;
  639. HWND hFocus = (HWND)wParam;
  640. HWND hParent = hFocus;
  641. HWND hLastGoodParent = hParent;
  642. static DWORD dLastDittoHasFocusTick = 0;
  643. //Sometimes when we bring ditto up there will come a null focus
  644. //rite after that
  645. if(hFocus == NULL && (GetTickCount() - dLastDittoHasFocusTick < 500))
  646. {
  647. Log(_T("NULL focus within 500 ticks of bringing up ditto"));
  648. return TRUE;
  649. }
  650. else if(hFocus == NULL)
  651. {
  652. Log(_T("NULL focus received"));
  653. }
  654. //only proceed if something changed
  655. if(theApp.m_hTargetWnd == hFocus)
  656. return TRUE;
  657. TCHAR cWindowText[100];
  658. ::GetWindowText(hFocus, cWindowText, 100);
  659. HWND hTray = ::FindWindow(_T("Shell_TrayWnd"), _T(""));
  660. // Log(StrF("Focus = %d", hFocus));
  661. int nCount = 0;
  662. while(true)
  663. {
  664. hParent = ::GetParent(hParent);
  665. if(hParent == NULL)
  666. break;
  667. // Log(StrF("Focus2 = %d", hParent));
  668. //allow focus on edit window
  669. if(m_pEditFrameWnd && hParent == m_pEditFrameWnd->GetSafeHwnd())
  670. {
  671. break;
  672. }
  673. hLastGoodParent = hParent;
  674. nCount++;
  675. if(nCount > 100)
  676. {
  677. Log(_T("OnFocusChanged reached maximum search depth of 100"));
  678. break;
  679. }
  680. }
  681. //If the parent is ditto or the tray icon then don't set focus to that window
  682. if(hLastGoodParent == m_hWnd || hLastGoodParent == hTray)
  683. {
  684. Log(_T("Ditto Has Focus"));
  685. theApp.m_bDittoHasFocus = true;
  686. dLastDittoHasFocusTick = GetTickCount();
  687. }
  688. else
  689. {
  690. theApp.m_bDittoHasFocus = false;
  691. theApp.m_hTargetWnd = hFocus;
  692. if(theApp.QPasteWnd() )
  693. theApp.QPasteWnd()->UpdateStatus(true);
  694. }
  695. return TRUE;
  696. }
  697. void CMainFrame::OnFirstHelp()
  698. {
  699. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  700. csFile += "DittoGettingStarted.htm";
  701. CHyperLink::GotoURL(csFile, SW_SHOW);
  702. }
  703. LRESULT CMainFrame::OnCustomizeTrayMenu(WPARAM wParam, LPARAM lParam)
  704. {
  705. CMenu *pMenu = (CMenu*)wParam;
  706. if(pMenu)
  707. {
  708. theApp.m_Language.UpdateTrayIconRightClickMenu(pMenu);
  709. }
  710. return true;
  711. }
  712. bool CMainFrame::PasteQuickPasteEntry(CString csQuickPaste)
  713. {
  714. Log(StrF(_T("PasteQuickPasteEntry -- %s"), csQuickPaste));
  715. CString csTitle = theApp.m_Language.GetString("Named_Paste_Title", "Ditto - Named Paste");
  716. bool bRet = false;
  717. try
  718. {
  719. csQuickPaste.Replace(_T("'"), _T("''"));
  720. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE QuickPasteText = '%s'"), csQuickPaste);
  721. if(q.eof() == false)
  722. {
  723. CClip Clip;
  724. if(Clip.LoadFormats(q.getIntField(_T("lID"))))
  725. {
  726. CProcessPaste paste;
  727. paste.m_pOle->PutFormatOnClipboard(&Clip.m_Formats, false);
  728. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  729. if(paste.DoPaste())
  730. {
  731. bRet = TRUE;
  732. }
  733. }
  734. else
  735. {
  736. CString csError = theApp.m_Language.GetString("Named_Paste_Error1", "Error loading data");
  737. ShowErrorMessage(csTitle, StrF(_T("%s - id:%d"), csError, q.getIntField(_T("lID"))));
  738. }
  739. }
  740. else
  741. {
  742. CString csError = theApp.m_Language.GetString("Named_Paste_Error2", "Error finding clip with QuickPaste Text of");
  743. ShowErrorMessage(csTitle, StrF(_T("%s - '%s'"), csError, csQuickPaste));
  744. }
  745. }
  746. catch (CppSQLite3Exception& e)
  747. {
  748. CString csError = theApp.m_Language.GetString("Named_Paste_Error3", "Exception finding the clip");
  749. ShowErrorMessage(csTitle, StrF(_T("%s - '%s' - %s"), csError, csQuickPaste, e.errorMessage()));
  750. }
  751. return bRet;
  752. }
  753. bool CMainFrame::SaveQuickPasteEntry(CString csQuickPaste, CClipList *pClipList)
  754. {
  755. Log(StrF(_T("SaveQuickPasteEntry text = %s - recived copy = %d"), csQuickPaste, pClipList != NULL));
  756. CString csTitle = theApp.m_Language.GetString("Named_Copy_Title", "Ditto - Named Copy");
  757. bool bRet = false;
  758. if(pClipList)
  759. {
  760. try
  761. {
  762. CClip *pClip = pClipList->GetHead();
  763. pClip->m_csQuickPaste = csQuickPaste;
  764. pClip->m_lDontAutoDelete = (long)CTime::GetCurrentTime().GetTime();
  765. if(csQuickPaste.IsEmpty() == FALSE)
  766. {
  767. //Remove any other instances of this quick paste
  768. csQuickPaste.Replace(_T("'"), _T("''"));
  769. int nCount = theApp.m_db.execDMLEx(_T("UPDATE Main SET QuickPasteText = '' WHERE QuickPasteText = '%s';"), csQuickPaste);
  770. if(nCount > 0)
  771. {
  772. Log(StrF(_T("Removed quick paste '%s', count = %d"), csQuickPaste, nCount));
  773. }
  774. }
  775. int count = pClipList->AddToDB(true);
  776. if(count > 0)
  777. {
  778. long lID = pClipList->GetTail()->m_ID;
  779. theApp.OnCopyCompleted(lID, count);
  780. bRet = true;
  781. }
  782. else
  783. {
  784. CString csError = theApp.m_Language.GetString("Named_Copy_Error1", "Error saving Named Coy to Database");
  785. ShowErrorMessage(csTitle, csError);
  786. }
  787. }
  788. catch (CppSQLite3Exception& e)
  789. {
  790. Log(StrF(_T("SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage()));
  791. ASSERT(FALSE);
  792. CString csError = theApp.m_Language.GetString("Named_Copy_Error2", "Exception saving Named Copy");
  793. ShowErrorMessage(csTitle, StrF(_T("%s - %s"), csError, e.errorMessage()));
  794. }
  795. }
  796. else
  797. {
  798. CString csError = theApp.m_Language.GetString("Named_Copy_Error3", "Ditto did not receive a copy");
  799. ShowErrorMessage(csTitle, csError);
  800. }
  801. return bRet;
  802. }
  803. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  804. {
  805. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  806. CToolTipEx *pErrorWnd = new CToolTipEx;
  807. pErrorWnd->Create(this);
  808. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  809. CPoint pt;
  810. CRect rcScreen;
  811. GetMonitorRect(0, &rcScreen);
  812. pt = rcScreen.BottomRight();
  813. CRect cr = pErrorWnd->GetBoundsRect();
  814. pt.x -= max(cr.Width()+50, 150);
  815. pt.y -= max(cr.Height()+50, 150);
  816. pErrorWnd->Show(pt);
  817. pErrorWnd->HideWindowInXMilliSeconds(4000);
  818. }
  819. void CMainFrame::DeleteOldRemoteCopies(CString csDir)
  820. {
  821. //must be deleting a sub folder in the musicgen directory
  822. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  823. return;
  824. FIX_CSTRING_PATH(csDir);
  825. CTime ctOld = CTime::GetCurrentTime();
  826. CTime ctFile;
  827. ctOld -= CTimeSpan(0, 0, 0, 1);
  828. CFileFind Find;
  829. CString csFindString;
  830. csFindString.Format(_T("%s*.*"), csDir);
  831. BOOL bFound = Find.FindFile(csFindString);
  832. while(bFound)
  833. {
  834. bFound = Find.FindNextFile();
  835. if(Find.IsDots())
  836. continue;
  837. if(Find.IsDirectory())
  838. {
  839. CString csDir(Find.GetFilePath());
  840. DeleteOldRemoteCopies(csDir);
  841. RemoveDirectory(csDir);
  842. }
  843. if(Find.GetLastAccessTime(ctFile))
  844. {
  845. //Delete the remote copied file if it has'nt been used for the last day
  846. if(ctFile < ctOld)
  847. {
  848. DeleteFile(Find.GetFilePath());
  849. }
  850. }
  851. else
  852. {
  853. DeleteFile(Find.GetFilePath());
  854. }
  855. }
  856. }
  857. UINT CMainFrame::RemoteOldRemoteFilesThread(LPVOID pParam)
  858. {
  859. CString csDir = CGetSetOptions::GetPath(PATH_REMOTE_FILES);
  860. if(FileExists(csDir))
  861. {
  862. DeleteOldRemoteCopies(csDir);
  863. }
  864. return TRUE;
  865. }
  866. void CMainFrame::OnFirstImport()
  867. {
  868. theApp.ImportClips(theApp.m_MainhWnd);
  869. }
  870. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  871. {
  872. CWaitCursor wait;
  873. bool bCreatedWindow = false;
  874. if(m_pEditFrameWnd == NULL)
  875. {
  876. m_pEditFrameWnd = new CEditFrameWnd;
  877. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  878. bCreatedWindow = true;
  879. }
  880. if(m_pEditFrameWnd)
  881. {
  882. m_pEditFrameWnd->EditIds(Ids);
  883. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  884. if(bCreatedWindow)
  885. {
  886. CSize sz;
  887. CPoint pt;
  888. CGetSetOptions::GetEditWndSize(sz);
  889. CGetSetOptions::GetEditWndPoint(pt);
  890. CRect cr(pt, sz);
  891. EnsureWindowVisible(&cr);
  892. m_pEditFrameWnd->MoveWindow(cr);
  893. }
  894. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  895. m_pEditFrameWnd->SetForegroundWindow();
  896. m_pEditFrameWnd->SetFocus();
  897. }
  898. }
  899. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  900. {
  901. m_pEditFrameWnd = NULL;
  902. return TRUE;
  903. }
  904. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  905. {
  906. if(wParam)
  907. theApp.SetConnectCV(true);
  908. else if(lParam)
  909. theApp.SetConnectCV(false);
  910. return TRUE;
  911. }
  912. void CMainFrame::OnDestroy()
  913. {
  914. CFrameWnd::OnDestroy();
  915. if(m_pEditFrameWnd)
  916. {
  917. m_pEditFrameWnd->DestroyWindow();
  918. }
  919. }
  920. void CMainFrame::OnFirstNewclip()
  921. {
  922. CClipIDs IDs;
  923. IDs.Add(-1);
  924. theApp.EditItems(IDs, true);
  925. }