CP_Main.cpp 17 KB

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