CP_Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // CP_Main.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "Misc.h"
  7. #include "SelectDB.h"
  8. #include ".\cp_main.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // The one and only CCP_MainApp object
  16. CCP_MainApp theApp;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CCP_MainApp
  19. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  20. //{{AFX_MSG_MAP(CCP_MainApp)
  21. // NOTE - the ClassWizard will add and remove mapping macros here.
  22. // DO NOT EDIT what you see in these blocks of generated code!
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCP_MainApp construction
  27. CCP_MainApp::CCP_MainApp()
  28. {
  29. m_bAppRunning = false;
  30. m_bAppExiting = false;
  31. m_MainhWnd = NULL;
  32. m_pMainFrame = NULL;
  33. m_bShowingQuickPaste = false;
  34. m_bShowingOptions = false;
  35. m_bShowCopyProperties = false;
  36. m_bRemoveOldEntriesPending = false;
  37. m_IC_bCopy = false;
  38. m_GroupDefaultID = 0;
  39. m_GroupID = 0;
  40. m_GroupParentID = 0;
  41. m_GroupText = "History";
  42. m_FocusID = -1; // -1 == keep previous position, 0 == go to latest ID
  43. m_pDatabase = NULL;
  44. // Place all significant initialization in InitInstance
  45. m_bAsynchronousRefreshView = true;
  46. }
  47. CCP_MainApp::~CCP_MainApp()
  48. {
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CCP_MainApp initialization
  52. BOOL CCP_MainApp::InitInstance()
  53. {
  54. LOG("InitInstance");
  55. AfxEnableControlContainer();
  56. m_hMutex = CreateMutex(NULL, FALSE, "Ditto Is Now Running");
  57. DWORD dwError = GetLastError();
  58. if(dwError == ERROR_ALREADY_EXISTS)
  59. {
  60. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  61. if(hWnd)
  62. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  63. return TRUE;
  64. }
  65. m_cfIgnoreClipboard = ::RegisterClipboardFormat("Clipboard Viewer Ignore");
  66. if(CheckDBExists(CGetSetOptions::GetDBPath()) == FALSE)
  67. {
  68. AfxMessageBox("Error Opening Database.");
  69. return TRUE;
  70. }
  71. AfxOleInit();
  72. // if(DoCleanups() == FALSE)
  73. // return TRUE;
  74. CMainFrame* pFrame = new CMainFrame;
  75. m_pMainWnd = m_pMainFrame = pFrame;
  76. // prevent no one having focus on startup
  77. TargetActiveWindow();
  78. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  79. pFrame->ShowWindow(SW_SHOW);
  80. pFrame->UpdateWindow();
  81. // prevent no one having focus on startup
  82. ReleaseFocus();
  83. return TRUE;
  84. }
  85. void CCP_MainApp::AfterMainCreate()
  86. {
  87. m_MainhWnd = m_pMainFrame->m_hWnd;
  88. ASSERT( ::IsWindow(m_MainhWnd) );
  89. g_Opt.SetMainHWND((long)m_MainhWnd);
  90. g_HotKeys.Init( m_MainhWnd );
  91. // create hotkeys here. They are automatically deleted on exit
  92. m_pDittoHotKey = new CHotKey("DittoHotKey",704); //704 is ctrl-tilda
  93. m_pCopyHotKey = new CHotKey("CopyHotKey");
  94. m_pPosOne = new CHotKey("Position1", 0, true);
  95. m_pPosTwo = new CHotKey("Position2", 0, true);
  96. m_pPosThree = new CHotKey("Position3", 0, true);
  97. m_pPosFour = new CHotKey("Position4", 0, true);
  98. m_pPosFive = new CHotKey("Position5", 0, true);
  99. m_pPosSix = new CHotKey("Position6", 0, true);
  100. m_pPosSeven = new CHotKey("Position7", 0, true);
  101. m_pPosEight = new CHotKey("Position8", 0, true);
  102. m_pPosNine = new CHotKey("Position9", 0, true);
  103. m_pPosTen = new CHotKey("Position10", 0, true);
  104. g_HotKeys.RegisterAll();
  105. // CopyThread initialization
  106. StartCopyThread();
  107. m_bAppRunning = true;
  108. }
  109. void CCP_MainApp::BeforeMainClose()
  110. {
  111. ASSERT( m_bAppRunning && !m_bAppExiting );
  112. m_bAppRunning = false;
  113. m_bAppExiting = true;
  114. g_HotKeys.UnregisterAll();
  115. StopCopyThread();
  116. }
  117. /*
  118. Re: Targeting the previous focus window
  119. We usually gain focus after the following messages:
  120. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  121. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  122. CMainFrame::PreTranslateMessage is used to intercept messages before
  123. they are processed (before we are actually given focus) in order to
  124. save the previous window that had focus.
  125. - It currently just handles "activating" mouse messages when showing.
  126. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  127. finding the Target on hotkey activation.
  128. This works well for most window switching (mouse or hotkey), but does
  129. not work well for <Alt>-<Tab> or other window switching applications
  130. (e.g. the taskbar tray), since the previous window was only a means to
  131. switching and not the target itself.
  132. - one solution might be to always monitor the current foreground
  133. window using system hooks or a short (e.g. 1 sec) timer... though this
  134. *might* be cpu intensive (slow). I'm currently looking into using
  135. WH_CBT system hooks in a separate dll (see: robpitt's
  136. http://website.lineone.net/~codebox/focuslog.zip).
  137. */
  138. bool CCP_MainApp::TargetActiveWindow()
  139. {
  140. HWND hOld = m_hTargetWnd;
  141. HWND hNew = ::GetForegroundWindow();
  142. if( hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew) )
  143. return false;
  144. m_hTargetWnd = hNew;
  145. if( QPasteWnd() )
  146. QPasteWnd()->UpdateStatus(true);
  147. // Tracking / Debugging
  148. /*
  149. LOG( StrF(
  150. "Target Changed" \
  151. "\n\tOld = 0x%08x: \"%s\"" \
  152. "\n\tNew = 0x%08x: \"%s\"\n",
  153. hOld, (LPCTSTR) GetWndText(hOld),
  154. hNew, (LPCTSTR) GetWndText(hNew) ) );
  155. */
  156. return true;
  157. }
  158. bool CCP_MainApp::ActivateTarget()
  159. {
  160. if( !::IsWindow(m_hTargetWnd) || m_hTargetWnd == ::GetForegroundWindow() )
  161. return false;
  162. ::SetForegroundWindow( m_hTargetWnd );
  163. // ::SetFocus( m_hTargetWnd );
  164. return true;
  165. }
  166. bool CCP_MainApp::ReleaseFocus()
  167. {
  168. if( IsAppWnd(::GetForegroundWindow()) )
  169. return ActivateTarget();
  170. return false;
  171. }
  172. // sends Ctrl-V to the TargetWnd
  173. void CCP_MainApp::SendPaste(bool bActivateTarget)
  174. {
  175. if(bActivateTarget && !ActivateTarget())
  176. {
  177. SetStatus("SendPaste FAILED!",TRUE);
  178. return;
  179. }
  180. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  181. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  182. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  183. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  184. }
  185. // CopyThread
  186. void CCP_MainApp::StartCopyThread()
  187. {
  188. ASSERT( m_MainhWnd );
  189. CClipTypes* pTypes = LoadTypesFromDB();
  190. // initialize to:
  191. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  192. // - true = use Asynchronous communication (PostMessage)
  193. // - true = enable copying on clipboard changes
  194. // - pTypes = the supported types to use
  195. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  196. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  197. m_CopyThread.ResumeThread();
  198. }
  199. void CCP_MainApp::StopCopyThread()
  200. {
  201. EnableCbCopy(false);
  202. m_CopyThread.Quit();
  203. SaveCopyClips();
  204. }
  205. // Allocates a new CClipTypes
  206. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  207. {
  208. CClipTypes* pTypes = new CClipTypes;
  209. try
  210. {
  211. CTypesTable recset;
  212. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Types" ,NULL);
  213. while(!recset.IsEOF())
  214. {
  215. pTypes->Add( GetFormatID(recset.m_TypeText) );
  216. recset.MoveNext();
  217. }
  218. recset.Close();
  219. }
  220. catch(CDaoException* e)
  221. {
  222. ASSERT(FALSE);
  223. e->Delete();
  224. }
  225. if( pTypes->GetSize() <= 0 )
  226. {
  227. pTypes->Add(CF_TEXT);
  228. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  229. }
  230. return pTypes;
  231. }
  232. void CCP_MainApp::ReloadTypes()
  233. {
  234. CClipTypes* pTypes = LoadTypesFromDB();
  235. if( pTypes )
  236. m_CopyThread.SetSupportedTypes( pTypes );
  237. }
  238. long CCP_MainApp::SaveCopyClips()
  239. {
  240. long lID = 0;
  241. int count;
  242. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  243. if( !pClips )
  244. return 0;
  245. count = pClips->AddToDB( true );
  246. if( count > 0 )
  247. {
  248. lID = pClips->GetTail()->m_ID;
  249. OnCopyCompleted( lID, count );
  250. }
  251. delete pClips;
  252. return lID;
  253. }
  254. void CCP_MainApp::RefreshView()
  255. {
  256. if( m_bShowingQuickPaste )
  257. {
  258. ASSERT( QPasteWnd() );
  259. if(m_bAsynchronousRefreshView)
  260. QPasteWnd()->PostMessage(WM_REFRESH_VIEW);
  261. else
  262. QPasteWnd()->SendMessage(WM_REFRESH_VIEW);
  263. }
  264. }
  265. void CCP_MainApp::OnPasteCompleted()
  266. {
  267. // the list only changes if UpdateTimeOnPaste is true (updated time)
  268. if( g_Opt.m_bUpdateTimeOnPaste )
  269. RefreshView();
  270. }
  271. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  272. {
  273. if( count <= 0 )
  274. return;
  275. // queue a message to RemoveOldEntries
  276. Delayed_RemoveOldEntries( 60000 );
  277. // update copy statistics
  278. CGetSetOptions::SetTripCopyCount( -count );
  279. CGetSetOptions::SetTotalCopyCount( -count );
  280. // if we are in the History group, focus on the latest copy
  281. if( m_GroupID == 0 )
  282. m_FocusID = 0;
  283. RefreshView();
  284. ShowCopyProperties( lLastID );
  285. }
  286. // Internal Clipboard for cut/copy/paste items between Groups
  287. // if NULL, this uses the current QPaste selection
  288. void CCP_MainApp::IC_Cut( ARRAY* pIDs )
  289. {
  290. if( pIDs == NULL )
  291. {
  292. if( QPasteWnd() )
  293. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  294. else
  295. m_IC_IDs.SetSize(0);
  296. }
  297. else
  298. m_IC_IDs.Copy( *pIDs );
  299. m_IC_bCopy = false;
  300. if( QPasteWnd() )
  301. QPasteWnd()->UpdateStatus();
  302. }
  303. // if NULL, this uses the current QPaste selection
  304. void CCP_MainApp::IC_Copy( ARRAY* pIDs )
  305. {
  306. if( pIDs == NULL )
  307. {
  308. if( QPasteWnd() )
  309. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  310. else
  311. m_IC_IDs.SetSize(0);
  312. }
  313. else
  314. m_IC_IDs.Copy( *pIDs );
  315. m_IC_bCopy = true;
  316. if( QPasteWnd() )
  317. QPasteWnd()->UpdateStatus();
  318. }
  319. void CCP_MainApp::IC_Paste()
  320. {
  321. if( m_IC_IDs.GetSize() <= 0 )
  322. return;
  323. if( m_IC_bCopy )
  324. m_IC_IDs.CopyTo( GetValidGroupID() );
  325. else // Move
  326. m_IC_IDs.MoveTo( GetValidGroupID() );
  327. // don't process the same items twice.
  328. m_IC_IDs.SetSize(0);
  329. RefreshView();
  330. }
  331. // Groups
  332. BOOL CCP_MainApp::EnterGroupID( long lID )
  333. {
  334. BOOL bResult = FALSE;
  335. if( m_GroupID == lID )
  336. return TRUE;
  337. // if we are switching to the parent, focus on the previous group
  338. if( m_GroupParentID == lID && m_GroupID > 0 )
  339. m_FocusID = m_GroupID;
  340. switch( lID )
  341. {
  342. case 0: // History Group "ID"
  343. m_GroupID = 0;
  344. m_GroupParentID = 0;
  345. m_GroupText = "History";
  346. bResult = TRUE;
  347. break;
  348. case -1: // All Groups "ID"
  349. m_GroupID = -1;
  350. m_GroupParentID = 0;
  351. m_GroupText = "Groups";
  352. bResult = TRUE;
  353. break;
  354. default: // Normal Group
  355. try
  356. {
  357. CMainTable recs;
  358. COleVariant varKey( lID, VT_I4 );
  359. recs.Open( dbOpenTable, "Main" );
  360. recs.SetCurrentIndex("lID");
  361. // Find first record whose [lID] field == lID
  362. if( recs.Seek(_T("="), &varKey) && recs.m_bIsGroup )
  363. {
  364. m_GroupID = recs.m_lID;
  365. m_GroupParentID = recs.m_lParentID;
  366. // if( m_GroupParentID == 0 )
  367. // m_GroupParentID = -1; // back out into "all top-level groups" list.
  368. m_GroupText = recs.m_strText;
  369. bResult = TRUE;
  370. }
  371. recs.Close();
  372. }
  373. CATCHDAO
  374. break;
  375. }
  376. if( bResult )
  377. {
  378. theApp.RefreshView();
  379. if( QPasteWnd() )
  380. QPasteWnd()->UpdateStatus( true );
  381. }
  382. return bResult;
  383. }
  384. // returns a usable group id (not negative)
  385. long CCP_MainApp::GetValidGroupID()
  386. {
  387. if( m_GroupID <= 0 )
  388. return 0;
  389. return m_GroupID;
  390. }
  391. // sets a valid id
  392. void CCP_MainApp::SetGroupDefaultID( long lID )
  393. {
  394. if( m_GroupDefaultID == lID )
  395. return;
  396. if( lID <= 0 )
  397. m_GroupDefaultID = 0;
  398. else
  399. m_GroupDefaultID = lID;
  400. if( QPasteWnd() )
  401. QPasteWnd()->UpdateStatus();
  402. }
  403. // Window States
  404. void CCP_MainApp::SetStatus( const char* status, bool bRepaintImmediately )
  405. {
  406. m_Status = status;
  407. if( QPasteWnd() )
  408. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  409. }
  410. void CCP_MainApp::ShowPersistent( bool bVal )
  411. {
  412. g_Opt.SetShowPersistent( bVal );
  413. // give some visual indication
  414. if( m_bShowingQuickPaste )
  415. {
  416. ASSERT( QPasteWnd() );
  417. QPasteWnd()->SetCaptionColorActive( !g_Opt.m_bShowPersistent );
  418. QPasteWnd()->RefreshNc();
  419. }
  420. }
  421. void CCP_MainApp::ShowCopyProperties( long lID )
  422. {
  423. if( m_bShowCopyProperties && lID > 0 )
  424. {
  425. HWND hWndFocus = ::GetForegroundWindow();
  426. m_bShowCopyProperties = false;
  427. ::SendMessage(m_MainhWnd, WM_COPYPROPERTIES, lID, 0); // modal
  428. ::SetForegroundWindow(hWndFocus);
  429. }
  430. }
  431. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  432. {
  433. if( !m_bRemoveOldEntriesPending )
  434. {
  435. m_bRemoveOldEntriesPending = true;
  436. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  437. }
  438. }
  439. /////////////////////////////////////////////////////////////////////////////
  440. // CCP_MainApp message handlers
  441. int CCP_MainApp::ExitInstance()
  442. {
  443. LOG("ExitInstance");
  444. if(CGetSetOptions::GetCompactAndRepairOnExit())
  445. CompactDatabase();
  446. CloseDB();
  447. return CWinApp::ExitInstance();
  448. }
  449. CDaoDatabase* CCP_MainApp::EnsureOpenDB(CString csName)
  450. {
  451. try
  452. {
  453. if(!m_pDatabase)
  454. m_pDatabase = new CDaoDatabase;
  455. if(!m_pDatabase->IsOpen())
  456. {
  457. if(csName == "")
  458. m_pDatabase->Open(GetDBName());
  459. else
  460. m_pDatabase->Open(csName);
  461. }
  462. if(m_pMainWnd)
  463. ((CMainFrame *)m_pMainWnd)->ResetKillDBTimer();
  464. }
  465. CATCHDAO
  466. return m_pDatabase;
  467. }
  468. BOOL CCP_MainApp::CloseDB()
  469. {
  470. if(m_pDatabase)
  471. {
  472. if(m_pDatabase->IsOpen())
  473. m_pDatabase->Close();
  474. delete m_pDatabase;
  475. m_pDatabase = NULL;
  476. }
  477. return TRUE;
  478. }
  479. // return TRUE if there is more idle processing to do
  480. BOOL CCP_MainApp::OnIdle(LONG lCount)
  481. {
  482. // let winapp handle its idle processing
  483. if( CWinApp::OnIdle(lCount) )
  484. return TRUE;
  485. return FALSE;
  486. }