NoDbFrameWnd.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "stdafx.h"
  2. #include "NoDbFrameWnd.h"
  3. #include "resource.h"
  4. #include "OptionsSheet.h"
  5. #include "DatabaseUtilities.h"
  6. #include "Options.h"
  7. #include "CP_Main.h"
  8. #include "Misc.h"
  9. #define WM_TRAYNOTIFY WM_USER + 100
  10. #define TIMER_OPEN_DB 1
  11. #define TIMER_ERROR_MSG 2
  12. BEGIN_MESSAGE_MAP(CNoDbFrameWnd, CFrameWnd)
  13. ON_WM_CREATE()
  14. ON_COMMAND(ID_FIRST_OPTIONS, &CNoDbFrameWnd::OnFirstOptions)
  15. ON_COMMAND(ID_FIRST_EXIT_NO_DB, &CNoDbFrameWnd::OnFirstExitNoDb)
  16. ON_MESSAGE(WM_TRAYNOTIFY, &CNoDbFrameWnd::OnTrayNotification)
  17. ON_MESSAGE(WM_OPTIONS_CLOSED, OnOptionsClosed)
  18. ON_WM_TIMER()
  19. ON_WM_HOTKEY()
  20. END_MESSAGE_MAP()
  21. CNoDbFrameWnd::CNoDbFrameWnd()
  22. {
  23. m_pOptions = NULL;
  24. m_pDittoHotKey = NULL;
  25. m_pDittoHotKey2 = NULL;
  26. m_pDittoHotKey3 = NULL;
  27. }
  28. int CNoDbFrameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  29. {
  30. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  31. return -1;
  32. ////Center the main window so message boxes are in the center
  33. CRect rcScreen = DefaultMonitorRect();
  34. CPoint cpCenter = rcScreen.CenterPoint();
  35. MoveWindow(cpCenter.x, cpCenter.x, 1, 1);
  36. SetWindowText(_T("Ditto"));
  37. m_trayIcon.Create(this, IDR_MENU_NO_DB, _T("Ditto"), CTrayNotifyIcon::LoadIcon(IDI_MAINFRAME_NO_DB), WM_TRAYNOTIFY, 0, 1);
  38. m_trayIcon.SetDefaultMenuItem(ID_FIRST_OPTIONS, FALSE);
  39. m_trayIcon.MinimiseToTray(this);
  40. SetTimer(TIMER_OPEN_DB, 15000, NULL);
  41. SetTimer(TIMER_ERROR_MSG, 180000, NULL);
  42. g_HotKeys.Init(m_hWnd);
  43. m_pDittoHotKey = new CHotKey(CString("DittoHotKey"), 704); //704 is ctrl-tilda
  44. m_pDittoHotKey2 = new CHotKey(CString("DittoHotKey2"));
  45. m_pDittoHotKey3 = new CHotKey(CString("DittoHotKey3"));
  46. g_HotKeys.RegisterAll();
  47. return 0;
  48. }
  49. void CNoDbFrameWnd::OnFirstOptions()
  50. {
  51. if (m_pOptions != NULL)
  52. {
  53. ::SetForegroundWindow(m_pOptions->m_hWnd);
  54. }
  55. else
  56. {
  57. m_pOptions = new COptionsSheet(_T(""));
  58. if (m_pOptions != NULL)
  59. {
  60. ((COptionsSheet*)m_pOptions)->SetNotifyWnd(m_hWnd);
  61. m_pOptions->Create();
  62. m_pOptions->ShowWindow(SW_SHOW);
  63. }
  64. }
  65. }
  66. void CNoDbFrameWnd::OnFirstExitNoDb()
  67. {
  68. this->SendMessage(WM_CLOSE, 0, 0);
  69. }
  70. LRESULT CNoDbFrameWnd::OnTrayNotification(WPARAM wParam, LPARAM lParam)
  71. {
  72. m_trayIcon.OnTrayNotification(wParam, lParam);
  73. return 0L;
  74. }
  75. void CNoDbFrameWnd::OnTimer(UINT_PTR nIDEvent)
  76. {
  77. switch (nIDEvent)
  78. {
  79. case TIMER_OPEN_DB:
  80. TryOpenDatabase();
  81. break;
  82. case TIMER_ERROR_MSG:
  83. KillTimer(TIMER_ERROR_MSG);
  84. ShowNoDbMessage();
  85. break;
  86. }
  87. CFrameWnd::OnTimer(nIDEvent);
  88. }
  89. void CNoDbFrameWnd::ShowNoDbMessage()
  90. {
  91. CString msg = theApp.m_Language.GetString(_T("StartupNoDbMsg"), _T("Ditto was unable to open its database, waiting until it can be opened. Update the path in Options if needed. Path: "));
  92. msg += StrF(_T(" %s"), CGetSetOptions::GetDBPath());
  93. m_trayIcon.SetBalloonDetails(msg, _T("Ditto"), CTrayNotifyIcon::BalloonStyle::Info, CGetSetOptions::GetBalloonTimeout());
  94. }
  95. void CNoDbFrameWnd::TryOpenDatabase()
  96. {
  97. if (IsDatabaseOpen() ||
  98. CheckDBExists(CGetSetOptions::GetDBPath()))
  99. {
  100. g_HotKeys.Remove(m_pDittoHotKey);
  101. delete m_pDittoHotKey;
  102. m_pDittoHotKey = NULL;
  103. g_HotKeys.Remove(m_pDittoHotKey2);
  104. delete m_pDittoHotKey2;
  105. m_pDittoHotKey2 = NULL;
  106. g_HotKeys.Remove(m_pDittoHotKey3);
  107. delete m_pDittoHotKey3;
  108. m_pDittoHotKey3 = NULL;
  109. KillTimer(TIMER_OPEN_DB);
  110. KillTimer(TIMER_ERROR_MSG);
  111. m_trayIcon.Hide();
  112. theApp.CreateMainWnd();
  113. }
  114. }
  115. LRESULT CNoDbFrameWnd::OnOptionsClosed(WPARAM wParam, LPARAM lParam)
  116. {
  117. delete m_pOptions;
  118. m_pOptions = NULL;
  119. TryOpenDatabase();
  120. return TRUE;
  121. }
  122. void CNoDbFrameWnd::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
  123. {
  124. if (m_pDittoHotKey && nHotKeyId == m_pDittoHotKey->m_Atom ||
  125. m_pDittoHotKey2 && nHotKeyId == m_pDittoHotKey2->m_Atom ||
  126. m_pDittoHotKey3 && nHotKeyId == m_pDittoHotKey3->m_Atom)
  127. {
  128. ShowNoDbMessage();
  129. }
  130. CFrameWnd::OnHotKey(nHotKeyId, nKey1, nKey2);
  131. }