MainFrm.cpp 28 KB

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