CP_Main.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #include "server.h"
  10. #include "Client.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // The one and only CCP_MainApp object
  18. CCP_MainApp theApp;
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CCP_MainApp
  21. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  22. //{{AFX_MSG_MAP(CCP_MainApp)
  23. // NOTE - the ClassWizard will add and remove mapping macros here.
  24. // DO NOT EDIT what you see in these blocks of generated code!
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CCP_MainApp construction
  29. CCP_MainApp::CCP_MainApp()
  30. {
  31. m_bAppRunning = false;
  32. m_bAppExiting = false;
  33. m_MainhWnd = NULL;
  34. m_pMainFrame = NULL;
  35. m_bShowingQuickPaste = false;
  36. m_bShowingOptions = false;
  37. m_bShowCopyProperties = false;
  38. m_bRemoveOldEntriesPending = false;
  39. m_IC_bCopy = false;
  40. m_GroupDefaultID = 0;
  41. m_GroupID = 0;
  42. m_GroupParentID = 0;
  43. m_GroupText = "History";
  44. m_FocusID = -1; // -1 == keep previous position, 0 == go to latest ID
  45. m_pDatabase = NULL;
  46. // Place all significant initialization in InitInstance
  47. m_bAsynchronousRefreshView = true;
  48. m_lClipsSent = 0;
  49. m_lClipsRecieved = 0;
  50. m_oldtStartUp = COleDateTime::GetCurrentTime();
  51. m_bExitServerThread = false;
  52. m_HTML_Format = ::RegisterClipboardFormat("HTML Format");
  53. ::InitializeCriticalSection(&m_CriticalSection);
  54. }
  55. CCP_MainApp::~CCP_MainApp()
  56. {
  57. ::DeleteCriticalSection(&m_CriticalSection);
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CCP_MainApp initialization
  61. BOOL CCP_MainApp::InitInstance()
  62. {
  63. LOG("InitInstance");
  64. AfxEnableControlContainer();
  65. m_hMutex = CreateMutex(NULL, FALSE, "Ditto Is Now Running");
  66. DWORD dwError = GetLastError();
  67. if(dwError == ERROR_ALREADY_EXISTS)
  68. {
  69. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  70. if(hWnd)
  71. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  72. return TRUE;
  73. }
  74. AfxOleInit();
  75. AfxInitRichEdit();
  76. m_cfIgnoreClipboard = ::RegisterClipboardFormat("Clipboard Viewer Ignore");
  77. int nRet = CheckDBExists(CGetSetOptions::GetDBPath());
  78. if(nRet == FALSE)
  79. {
  80. AfxMessageBox("Error Opening Database.");
  81. return TRUE;
  82. }
  83. else if(nRet == ERROR_OPENING_DATABASE)
  84. {
  85. CString cs;
  86. cs.Format("Unable to initialize DAO/Jet db engine.\nSelect YES to download DAO from http://ditto-cp.sourceforge.net/dao_setup.exe\n\nRestart Ditto after installation of DAO.");
  87. if(MessageBox(NULL, cs, "Ditto", MB_YESNO) == IDYES)
  88. {
  89. ShellExecute(NULL, "open", "http://ditto-cp.sourceforge.net/dao_setup.exe", "", "", SW_SHOW);
  90. }
  91. return TRUE;
  92. }
  93. CMainFrame* pFrame = new CMainFrame;
  94. m_pMainWnd = m_pMainFrame = pFrame;
  95. // prevent no one having focus on startup
  96. TargetActiveWindow();
  97. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  98. pFrame->ShowWindow(SW_SHOW);
  99. pFrame->UpdateWindow();
  100. // prevent no one having focus on startup
  101. ReleaseFocus();
  102. return TRUE;
  103. }
  104. void CCP_MainApp::AfterMainCreate()
  105. {
  106. m_MainhWnd = m_pMainFrame->m_hWnd;
  107. ASSERT( ::IsWindow(m_MainhWnd) );
  108. g_Opt.SetMainHWND((long)m_MainhWnd);
  109. g_HotKeys.Init( m_MainhWnd );
  110. // create hotkeys here. They are automatically deleted on exit
  111. m_pDittoHotKey = new CHotKey("DittoHotKey",704); //704 is ctrl-tilda
  112. m_pCopyHotKey = new CHotKey("CopyHotKey");
  113. m_pPosOne = new CHotKey("Position1", 0, true);
  114. m_pPosTwo = new CHotKey("Position2", 0, true);
  115. m_pPosThree = new CHotKey("Position3", 0, true);
  116. m_pPosFour = new CHotKey("Position4", 0, true);
  117. m_pPosFive = new CHotKey("Position5", 0, true);
  118. m_pPosSix = new CHotKey("Position6", 0, true);
  119. m_pPosSeven = new CHotKey("Position7", 0, true);
  120. m_pPosEight = new CHotKey("Position8", 0, true);
  121. m_pPosNine = new CHotKey("Position9", 0, true);
  122. m_pPosTen = new CHotKey("Position10", 0, true);
  123. g_HotKeys.RegisterAll();
  124. // CopyThread initialization
  125. StartCopyThread();
  126. StartStopServerThread();
  127. m_bAppRunning = true;
  128. m_pcpSendRecieveError = NULL;
  129. }
  130. void CCP_MainApp::StartStopServerThread()
  131. {
  132. if(CGetSetOptions::GetDisableRecieve() == FALSE)
  133. {
  134. AfxBeginThread(MTServerThread, m_MainhWnd);
  135. }
  136. else
  137. {
  138. m_bExitServerThread = true;
  139. closesocket(theApp.m_sSocket);
  140. }
  141. }
  142. void CCP_MainApp::StopServerThread()
  143. {
  144. m_bExitServerThread = true;
  145. closesocket(theApp.m_sSocket);
  146. }
  147. void CCP_MainApp::BeforeMainClose()
  148. {
  149. ASSERT( m_bAppRunning && !m_bAppExiting );
  150. m_bAppRunning = false;
  151. m_bAppExiting = true;
  152. g_HotKeys.UnregisterAll();
  153. StopServerThread();
  154. StopCopyThread();
  155. }
  156. /*
  157. Re: Targeting the previous focus window
  158. We usually gain focus after the following messages:
  159. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  160. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  161. CMainFrame::PreTranslateMessage is used to intercept messages before
  162. they are processed (before we are actually given focus) in order to
  163. save the previous window that had focus.
  164. - It currently just handles "activating" mouse messages when showing.
  165. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  166. finding the Target on hotkey activation.
  167. This works well for most window switching (mouse or hotkey), but does
  168. not work well for <Alt>-<Tab> or other window switching applications
  169. (e.g. the taskbar tray), since the previous window was only a means to
  170. switching and not the target itself.
  171. - one solution might be to always monitor the current foreground
  172. window using system hooks or a short (e.g. 1 sec) timer... though this
  173. *might* be cpu intensive (slow). I'm currently looking into using
  174. WH_CBT system hooks in a separate dll (see: robpitt's
  175. http://website.lineone.net/~codebox/focuslog.zip).
  176. */
  177. bool CCP_MainApp::TargetActiveWindow()
  178. {
  179. if(g_Opt.m_bUseHookDllForFocus)
  180. return true;
  181. HWND hOld = m_hTargetWnd;
  182. HWND hNew = ::GetForegroundWindow();
  183. if( hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew) )
  184. return false;
  185. m_hTargetWnd = hNew;
  186. if( QPasteWnd() )
  187. QPasteWnd()->UpdateStatus(true);
  188. // Tracking / Debugging
  189. /*
  190. LOG( StrF(
  191. "Target Changed" \
  192. "\n\tOld = 0x%08x: \"%s\"" \
  193. "\n\tNew = 0x%08x: \"%s\"\n",
  194. hOld, (LPCTSTR) GetWndText(hOld),
  195. hNew, (LPCTSTR) GetWndText(hNew) ) );
  196. */
  197. return true;
  198. }
  199. bool CCP_MainApp::ActivateTarget()
  200. {
  201. if( !::IsWindow(m_hTargetWnd) || m_hTargetWnd == ::GetForegroundWindow() )
  202. return false;
  203. ::SetForegroundWindow( m_hTargetWnd );
  204. // ::SetFocus( m_hTargetWnd );
  205. return true;
  206. }
  207. bool CCP_MainApp::ReleaseFocus()
  208. {
  209. if( IsAppWnd(::GetForegroundWindow()) )
  210. return ActivateTarget();
  211. return false;
  212. }
  213. // sends Ctrl-V to the TargetWnd
  214. void CCP_MainApp::SendPaste(bool bActivateTarget)
  215. {
  216. if(bActivateTarget && !ActivateTarget())
  217. {
  218. SetStatus("SendPaste FAILED!",TRUE);
  219. return;
  220. }
  221. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  222. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  223. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  224. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  225. }
  226. // CopyThread
  227. void CCP_MainApp::StartCopyThread()
  228. {
  229. ASSERT( m_MainhWnd );
  230. CClipTypes* pTypes = LoadTypesFromDB();
  231. // initialize to:
  232. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  233. // - true = use Asynchronous communication (PostMessage)
  234. // - true = enable copying on clipboard changes
  235. // - pTypes = the supported types to use
  236. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  237. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  238. m_CopyThread.ResumeThread();
  239. }
  240. void CCP_MainApp::StopCopyThread()
  241. {
  242. EnableCbCopy(false);
  243. m_CopyThread.Quit();
  244. SaveCopyClips();
  245. }
  246. // returns the current Clipboard Viewer Connect state (though it might not yet
  247. // be actually connected -- check IsClipboardViewerConnected())
  248. bool CCP_MainApp::ToggleConnectCV()
  249. {
  250. bool bConnect = !GetConnectCV();
  251. SetConnectCV( bConnect );
  252. if( bConnect )
  253. m_pMainFrame->m_TrayIcon.SetIcon( IDR_MAINFRAME );
  254. else
  255. m_pMainFrame->m_TrayIcon.SetIcon( IDI_DITTO_NOCOPYCB );
  256. return bConnect;
  257. }
  258. // Sets a menu entry according to the current Clipboard Viewer Connection status
  259. // - the menu text indicates the available command (opposite the current state)
  260. // - a check mark appears in the rare cases that the menu text actually represents
  261. // the current state, e.g. if we are supposed to be connected, but we somehow
  262. // lose that connection, "Disconnect from Clipboard" will have a check next to it.
  263. void CCP_MainApp::UpdateMenuConnectCV( CMenu* pMenu, UINT nMenuID )
  264. {
  265. if( pMenu == NULL )
  266. return;
  267. bool bConnect = theApp.GetConnectCV();
  268. bool bIsConnected = theApp.IsClipboardViewerConnected();
  269. CString cs;
  270. if( bConnect )
  271. {
  272. cs = "Disconnect from Clipboard";
  273. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  274. // add a check mark if we are still disconnected (temporarily)
  275. if( !bIsConnected )
  276. pMenu->CheckMenuItem(nMenuID, MF_CHECKED);
  277. else
  278. pMenu->CheckMenuItem(nMenuID, MF_UNCHECKED);
  279. }
  280. else // CV is disconnected, so provide the option of connecting
  281. {
  282. cs = "Connect to Clipboard";
  283. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  284. // add a check mark if we are still connected (temporarily)
  285. if( bIsConnected )
  286. pMenu->CheckMenuItem(nMenuID, MF_CHECKED);
  287. else
  288. pMenu->CheckMenuItem(nMenuID, MF_UNCHECKED);
  289. }
  290. }
  291. // Allocates a new CClipTypes
  292. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  293. {
  294. CClipTypes* pTypes = new CClipTypes;
  295. try
  296. {
  297. CTypesTable recset;
  298. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Types" ,NULL);
  299. while(!recset.IsEOF())
  300. {
  301. pTypes->Add( GetFormatID(recset.m_TypeText) );
  302. recset.MoveNext();
  303. }
  304. recset.Close();
  305. }
  306. catch(CDaoException* e)
  307. {
  308. ASSERT(FALSE);
  309. e->Delete();
  310. }
  311. if( pTypes->GetSize() <= 0 )
  312. {
  313. pTypes->Add(CF_TEXT);
  314. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  315. pTypes->Add(CF_DIB);
  316. }
  317. return pTypes;
  318. }
  319. void CCP_MainApp::ReloadTypes()
  320. {
  321. CClipTypes* pTypes = LoadTypesFromDB();
  322. if( pTypes )
  323. m_CopyThread.SetSupportedTypes( pTypes );
  324. }
  325. long CCP_MainApp::SaveCopyClips()
  326. {
  327. long lID = 0;
  328. int count;
  329. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  330. if( !pClips )
  331. return 0;
  332. CClipList* pCopyOfClips = NULL;
  333. if(g_Opt.m_lAutoSendClientCount > 0)
  334. {
  335. //The thread will free these
  336. pCopyOfClips = new CClipList;
  337. if(pCopyOfClips != NULL)
  338. {
  339. *pCopyOfClips = *pClips;
  340. }
  341. }
  342. bool bEnteredThread = false;
  343. count = pClips->AddToDB( true );
  344. if( count > 0 )
  345. {
  346. lID = pClips->GetTail()->m_ID;
  347. OnCopyCompleted(lID, count);
  348. if(g_Opt.m_lAutoSendClientCount > 0)
  349. {
  350. AfxBeginThread(SendClientThread, pCopyOfClips);
  351. bEnteredThread = true;
  352. }
  353. }
  354. if(bEnteredThread == false)
  355. delete pCopyOfClips;
  356. delete pClips;
  357. return lID;
  358. }
  359. void CCP_MainApp::RefreshView()
  360. {
  361. if( m_bShowingQuickPaste )
  362. {
  363. ASSERT( QPasteWnd() );
  364. if(m_bAsynchronousRefreshView)
  365. QPasteWnd()->PostMessage(WM_REFRESH_VIEW);
  366. else
  367. QPasteWnd()->SendMessage(WM_REFRESH_VIEW);
  368. }
  369. }
  370. void CCP_MainApp::OnPasteCompleted()
  371. {
  372. // the list only changes if UpdateTimeOnPaste is true (updated time)
  373. if( g_Opt.m_bUpdateTimeOnPaste )
  374. RefreshView();
  375. }
  376. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  377. {
  378. if( count <= 0 )
  379. return;
  380. // queue a message to RemoveOldEntries
  381. Delayed_RemoveOldEntries( 60000 );
  382. // update copy statistics
  383. CGetSetOptions::SetTripCopyCount( -count );
  384. CGetSetOptions::SetTotalCopyCount( -count );
  385. // if we are in the History group, focus on the latest copy
  386. if( m_GroupID == 0 )
  387. m_FocusID = 0;
  388. RefreshView();
  389. ShowCopyProperties( lLastID );
  390. }
  391. // Internal Clipboard for cut/copy/paste items between Groups
  392. // if NULL, this uses the current QPaste selection
  393. void CCP_MainApp::IC_Cut( ARRAY* pIDs )
  394. {
  395. if( pIDs == NULL )
  396. {
  397. if( QPasteWnd() )
  398. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  399. else
  400. m_IC_IDs.SetSize(0);
  401. }
  402. else
  403. m_IC_IDs.Copy( *pIDs );
  404. m_IC_bCopy = false;
  405. if( QPasteWnd() )
  406. QPasteWnd()->UpdateStatus();
  407. }
  408. // if NULL, this uses the current QPaste selection
  409. void CCP_MainApp::IC_Copy( ARRAY* pIDs )
  410. {
  411. if( pIDs == NULL )
  412. {
  413. if( QPasteWnd() )
  414. QPasteWnd()->m_lstHeader.GetSelectionItemData( m_IC_IDs );
  415. else
  416. m_IC_IDs.SetSize(0);
  417. }
  418. else
  419. m_IC_IDs.Copy( *pIDs );
  420. m_IC_bCopy = true;
  421. if( QPasteWnd() )
  422. QPasteWnd()->UpdateStatus();
  423. }
  424. void CCP_MainApp::IC_Paste()
  425. {
  426. if( m_IC_IDs.GetSize() <= 0 )
  427. return;
  428. if( m_IC_bCopy )
  429. m_IC_IDs.CopyTo( GetValidGroupID() );
  430. else // Move
  431. m_IC_IDs.MoveTo( GetValidGroupID() );
  432. // don't process the same items twice.
  433. m_IC_IDs.SetSize(0);
  434. RefreshView();
  435. }
  436. // Groups
  437. BOOL CCP_MainApp::EnterGroupID( long lID )
  438. {
  439. BOOL bResult = FALSE;
  440. if( m_GroupID == lID )
  441. return TRUE;
  442. // if we are switching to the parent, focus on the previous group
  443. if( m_GroupParentID == lID && m_GroupID > 0 )
  444. m_FocusID = m_GroupID;
  445. switch( lID )
  446. {
  447. case 0: // History Group "ID"
  448. m_FocusID = -2;
  449. m_GroupID = 0;
  450. m_GroupParentID = 0;
  451. m_GroupText = "History";
  452. bResult = TRUE;
  453. break;
  454. case -1: // All Groups "ID"
  455. m_GroupID = -1;
  456. m_GroupParentID = 0;
  457. m_GroupText = "Groups";
  458. bResult = TRUE;
  459. break;
  460. default: // Normal Group
  461. try
  462. {
  463. CMainTable recs;
  464. COleVariant varKey( lID, VT_I4 );
  465. recs.Open( dbOpenTable, "Main" );
  466. recs.SetCurrentIndex("lID");
  467. // Find first record whose [lID] field == lID
  468. if( recs.Seek(_T("="), &varKey) && recs.m_bIsGroup )
  469. {
  470. m_GroupID = recs.m_lID;
  471. m_GroupParentID = recs.m_lParentID;
  472. // if( m_GroupParentID == 0 )
  473. // m_GroupParentID = -1; // back out into "all top-level groups" list.
  474. m_GroupText = recs.m_strText;
  475. bResult = TRUE;
  476. }
  477. recs.Close();
  478. }
  479. CATCHDAO
  480. break;
  481. }
  482. if( bResult )
  483. {
  484. theApp.RefreshView();
  485. if( QPasteWnd() )
  486. QPasteWnd()->UpdateStatus( true );
  487. }
  488. return bResult;
  489. }
  490. // returns a usable group id (not negative)
  491. long CCP_MainApp::GetValidGroupID()
  492. {
  493. if( m_GroupID <= 0 )
  494. return 0;
  495. return m_GroupID;
  496. }
  497. // sets a valid id
  498. void CCP_MainApp::SetGroupDefaultID( long lID )
  499. {
  500. if( m_GroupDefaultID == lID )
  501. return;
  502. if( lID <= 0 )
  503. m_GroupDefaultID = 0;
  504. else
  505. m_GroupDefaultID = lID;
  506. if( QPasteWnd() )
  507. QPasteWnd()->UpdateStatus();
  508. }
  509. // Window States
  510. void CCP_MainApp::SetStatus( const char* status, bool bRepaintImmediately )
  511. {
  512. m_Status = status;
  513. if( QPasteWnd() )
  514. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  515. }
  516. void CCP_MainApp::ShowPersistent( bool bVal )
  517. {
  518. g_Opt.SetShowPersistent( bVal );
  519. // give some visual indication
  520. if( m_bShowingQuickPaste )
  521. {
  522. ASSERT( QPasteWnd() );
  523. QPasteWnd()->SetCaptionColorActive(!g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  524. QPasteWnd()->RefreshNc();
  525. }
  526. }
  527. void CCP_MainApp::ShowCopyProperties( long lID )
  528. {
  529. if( m_bShowCopyProperties && lID > 0 )
  530. {
  531. HWND hWndFocus = ::GetForegroundWindow();
  532. m_bShowCopyProperties = false;
  533. ::SendMessage(m_MainhWnd, WM_COPYPROPERTIES, lID, 0); // modal
  534. ::SetForegroundWindow(hWndFocus);
  535. }
  536. }
  537. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  538. {
  539. if( !m_bRemoveOldEntriesPending )
  540. {
  541. m_bRemoveOldEntriesPending = true;
  542. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  543. }
  544. }
  545. /////////////////////////////////////////////////////////////////////////////
  546. // CCP_MainApp message handlers
  547. int CCP_MainApp::ExitInstance()
  548. {
  549. LOG("ExitInstance");
  550. if(CGetSetOptions::GetCompactAndRepairOnExit())
  551. CompactDatabase();
  552. CloseDB();
  553. return CWinApp::ExitInstance();
  554. }
  555. CDaoDatabase* CCP_MainApp::EnsureOpenDB(CString csName)
  556. {
  557. try
  558. {
  559. if(!m_pDatabase)
  560. m_pDatabase = new CDaoDatabase;
  561. if(!m_pDatabase->IsOpen())
  562. {
  563. if(csName == "")
  564. m_pDatabase->Open(GetDBName());
  565. else
  566. m_pDatabase->Open(csName);
  567. }
  568. if(m_pMainWnd)
  569. ((CMainFrame *)m_pMainWnd)->ResetKillDBTimer();
  570. }
  571. CATCHDAO
  572. return m_pDatabase;
  573. }
  574. BOOL CCP_MainApp::CloseDB()
  575. {
  576. if(m_pDatabase)
  577. {
  578. if(m_pDatabase->IsOpen())
  579. m_pDatabase->Close();
  580. delete m_pDatabase;
  581. m_pDatabase = NULL;
  582. }
  583. return TRUE;
  584. }
  585. // return TRUE if there is more idle processing to do
  586. BOOL CCP_MainApp::OnIdle(LONG lCount)
  587. {
  588. // let winapp handle its idle processing
  589. if( CWinApp::OnIdle(lCount) )
  590. return TRUE;
  591. return FALSE;
  592. }
  593. CString CCP_MainApp::GetTargetName()
  594. {
  595. char cWindowText[100];
  596. HWND hParent = m_hTargetWnd;
  597. ::GetWindowText(hParent, cWindowText, 100);
  598. while(strlen(cWindowText) <= 0)
  599. {
  600. hParent = ::GetParent(hParent);
  601. if(hParent == NULL)
  602. break;
  603. ::GetWindowText(hParent, cWindowText, 100);
  604. }
  605. return cWindowText;
  606. }
  607. void CCP_MainApp::SetConnectCV(bool bConnect)
  608. {
  609. m_CopyThread.SetConnectCV(bConnect);
  610. if(QPasteWnd())
  611. {
  612. QPasteWnd()->SetCaptionColorActive(!g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  613. QPasteWnd()->RefreshNc();
  614. }
  615. }