CP_Main.cpp 16 KB

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