CP_Main.cpp 18 KB

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