OptionsGeneral.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // OptionsGeneral.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "OptionsGeneral.h"
  6. #include "InternetUpdate.h"
  7. #include <io.h>
  8. #include <Mmsystem.h> //play sound
  9. #include "Path.h"
  10. #include "AccessToSqlite.h"
  11. using namespace nsPath;
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // COptionsGeneral property page
  19. IMPLEMENT_DYNCREATE(COptionsGeneral, CPropertyPage)
  20. COptionsGeneral::COptionsGeneral() : CPropertyPage(COptionsGeneral::IDD)
  21. {
  22. m_csTitle = theApp.m_Language.GetString("GeneralTitle", "General");
  23. m_psp.pszTitle = m_csTitle;
  24. m_psp.dwFlags |= PSP_USETITLE;
  25. //{{AFX_DATA_INIT(COptionsGeneral)
  26. m_csPlaySound = _T("");
  27. //}}AFX_DATA_INIT
  28. }
  29. COptionsGeneral::~COptionsGeneral()
  30. {
  31. }
  32. void COptionsGeneral::DoDataExchange(CDataExchange* pDX)
  33. {
  34. CPropertyPage::DoDataExchange(pDX);
  35. //{{AFX_DATA_MAP(COptionsGeneral)
  36. DDX_Control(pDX, IDC_ENSURE, m_EnsureConnected);
  37. DDX_Control(pDX, IDC_EDIT_SAVE_DELAY, m_SaveDelay);
  38. DDX_Control(pDX, IDC_COMBO_LANGUAGE, m_cbLanguage);
  39. DDX_Control(pDX, IDC_EDIT_MAX_SIZE, m_MaxClipSize);
  40. DDX_Control(pDX, IDC_SEND_PASTE_MESSAGE, m_btSendPasteMessage);
  41. DDX_Control(pDX, IDC_HIDE_DITO_ON_HOT_KEY, m_btHideDittoOnHotKey);
  42. DDX_Control(pDX, IDC_DESC_TEXT_SIZE, m_DescTextSize);
  43. DDX_Control(pDX, IDC_GET_PATH, m_btGetPath);
  44. DDX_Control(pDX, IDC_PATH, m_ePath);
  45. DDX_Control(pDX, IDC_SET_DB_PATH, m_btSetDatabasePath);
  46. DDX_Control(pDX, IDC_CHECK_UPDATES, m_btCheckForUpdates);
  47. DDX_Control(pDX, IDC_EXPIRE_AFTER, m_eExpireAfter);
  48. DDX_Control(pDX, IDC_MAX_SAVED_COPIES, m_eMaxSavedCopies);
  49. DDX_Control(pDX, IDC_MAXIMUM, m_btMaximumCheck);
  50. DDX_Control(pDX, IDC_EXPIRE, m_btExpire);
  51. DDX_Control(pDX, IDC_DISPLAY_IN_SYSTEMTRAY, m_btShowIconInSysTray);
  52. DDX_Control(pDX, IDC_START_ON_STARTUP, m_btRunOnStartup);
  53. DDX_Text(pDX, IDC_EDIT_PLAY_SOUND, m_csPlaySound);
  54. DDX_Control(pDX, IDC_EDIT_CLIP_SEPARATOR, m_ClipSeparator);
  55. //}}AFX_DATA_MAP
  56. DDX_Control(pDX, IDC_ALLOW_DUPLICATES, m_btAllowDuplicates);
  57. DDX_Control(pDX, IDC_UPDATE_TIME_ON_PASTE, m_btUpdateTimeOnPaste);
  58. DDX_Control(pDX, IDC_SAVE_MULTIPASTE, m_btSaveMultiPaste);
  59. }
  60. BEGIN_MESSAGE_MAP(COptionsGeneral, CPropertyPage)
  61. //{{AFX_MSG_MAP(COptionsGeneral)
  62. ON_BN_CLICKED(IDC_BT_COMPACT_AND_REPAIR, OnBtCompactAndRepair)
  63. ON_BN_CLICKED(IDC_CHECK_FOR_UPDATES, OnCheckForUpdates)
  64. ON_BN_CLICKED(IDC_SET_DB_PATH, OnSetDbPath)
  65. ON_BN_CLICKED(IDC_GET_PATH, OnGetPath)
  66. ON_BN_CLICKED(IDC_SELECT_SOUND, OnSelectSound)
  67. ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
  68. ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
  69. //}}AFX_MSG_MAP
  70. END_MESSAGE_MAP()
  71. /////////////////////////////////////////////////////////////////////////////
  72. // COptionsGeneral message handlers
  73. BOOL COptionsGeneral::OnInitDialog()
  74. {
  75. CPropertyPage::OnInitDialog();
  76. m_pParent = (COptionsSheet *)GetParent();
  77. m_btRunOnStartup.SetCheck(CGetSetOptions::GetRunOnStartUp());
  78. m_btShowIconInSysTray.SetCheck(CGetSetOptions::GetShowIconInSysTray());
  79. m_btMaximumCheck.SetCheck(CGetSetOptions::GetCheckForMaxEntries());
  80. m_btExpire.SetCheck(CGetSetOptions::GetCheckForExpiredEntries());
  81. m_btCheckForUpdates.SetCheck(CGetSetOptions::GetCheckForUpdates());
  82. m_eExpireAfter.SetNumber(CGetSetOptions::GetExpiredEntries());
  83. m_eMaxSavedCopies.SetNumber(CGetSetOptions::GetMaxEntries());
  84. m_DescTextSize.SetNumber(g_Opt.m_bDescTextSize);
  85. m_SaveDelay.SetNumber(g_Opt.m_dwSaveClipDelay);
  86. m_btAllowDuplicates.SetCheck( g_Opt.m_bAllowDuplicates );
  87. m_btUpdateTimeOnPaste.SetCheck( g_Opt.m_bUpdateTimeOnPaste );
  88. m_btSaveMultiPaste.SetCheck( g_Opt.m_bSaveMultiPaste );
  89. m_btHideDittoOnHotKey.SetCheck(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown);
  90. m_btSendPasteMessage.SetCheck(g_Opt.m_bSendPasteMessageAfterSelection);
  91. m_EnsureConnected.SetCheck(g_Opt.m_bEnsureConnectToClipboard);
  92. m_ClipSeparator.SetWindowText(g_Opt.GetMultiPasteSeparator(false));
  93. if(g_Opt.m_lMaxClipSizeInBytes > 0)
  94. {
  95. CString csMax;
  96. csMax.Format(_T("%d"), g_Opt.m_lMaxClipSizeInBytes);
  97. m_MaxClipSize.SetWindowText(csMax);
  98. }
  99. CString csPath = CGetSetOptions::GetDBPath();
  100. if(csPath.IsEmpty())
  101. {
  102. m_ePath.EnableWindow(FALSE);
  103. m_btGetPath.EnableWindow(FALSE);
  104. csPath = CGetSetOptions::GetDBPath();
  105. m_ePath.SetWindowText(csPath);
  106. }
  107. else
  108. {
  109. m_btSetDatabasePath.SetCheck(BST_CHECKED);
  110. m_ePath.SetWindowText(csPath);
  111. }
  112. m_csPlaySound = g_Opt.m_csPlaySoundOnCopy;
  113. FillLanguages();
  114. UpdateData(FALSE);
  115. if(g_Opt.m_bU3)
  116. {
  117. //If running from a U3 device then don't allow them to set
  118. //-run on startup
  119. //-db path
  120. m_btRunOnStartup.ShowWindow(SW_HIDE);
  121. m_ePath.EnableWindow(FALSE);
  122. m_btSetDatabasePath.EnableWindow(FALSE);
  123. m_btGetPath.EnableWindow(FALSE);
  124. m_ePath.SetWindowText(_T("U3 Device"));
  125. m_btCheckForUpdates.ShowWindow(SW_HIDE);
  126. ::ShowWindow(::GetDlgItem(m_hWnd, IDC_CHECK_FOR_UPDATES), SW_HIDE);
  127. }
  128. theApp.m_Language.UpdateOptionGeneral(this);
  129. return TRUE;
  130. }
  131. #define NO_MATCH -2
  132. #define FOUND_MATCH -1
  133. void COptionsGeneral::FillLanguages()
  134. {
  135. CString csFile = CGetSetOptions::GetPath(PATH_LANGUAGE);
  136. csFile += "*.xml";
  137. CString csLanguage = CGetSetOptions::GetLanguageFile();
  138. CFileFind find;
  139. BOOL bCont = find.FindFile(csFile);
  140. int nEnglishIndex = NO_MATCH;
  141. while(bCont)
  142. {
  143. bCont = find.FindNextFile();
  144. int nIndex = m_cbLanguage.AddString(find.GetFileTitle());
  145. if(find.GetFileTitle() == csLanguage)
  146. {
  147. nEnglishIndex = -1;
  148. m_cbLanguage.SetCurSel(nIndex);
  149. }
  150. else if(find.GetFileTitle() == _T("English"))
  151. {
  152. if(nEnglishIndex == NO_MATCH)
  153. nEnglishIndex = nIndex;
  154. }
  155. }
  156. if(nEnglishIndex >= 0)
  157. {
  158. m_cbLanguage.SetCurSel(nEnglishIndex);
  159. }
  160. }
  161. BOOL COptionsGeneral::OnApply()
  162. {
  163. UpdateData();
  164. ::SendMessage(theApp.m_MainhWnd, WM_SHOW_TRAY_ICON, m_btShowIconInSysTray.GetCheck(), 0);
  165. CGetSetOptions::SetShowIconInSysTray(m_btShowIconInSysTray.GetCheck());
  166. CGetSetOptions::SetRunOnStartUp(m_btRunOnStartup.GetCheck());
  167. CGetSetOptions::SetCheckForMaxEntries(m_btMaximumCheck.GetCheck());
  168. CGetSetOptions::SetCheckForExpiredEntries(m_btExpire.GetCheck());
  169. CGetSetOptions::SetCheckForUpdates(m_btCheckForUpdates.GetCheck());
  170. CGetSetOptions::SetHideDittoOnHotKeyIfAlreadyShown(m_btHideDittoOnHotKey.GetCheck());
  171. CGetSetOptions::SetSendPasteAfterSelection(m_btSendPasteMessage.GetCheck());
  172. CGetSetOptions::SetEnsureConnectToClipboard(m_EnsureConnected.GetCheck());
  173. CGetSetOptions::SetMaxEntries(m_eMaxSavedCopies.GetNumber());
  174. CGetSetOptions::SetExpiredEntries(m_eExpireAfter.GetNumber());
  175. CGetSetOptions::SetDescTextSize(m_DescTextSize.GetNumber());
  176. CGetSetOptions::SetSaveClipDelay(m_SaveDelay.GetNumber());
  177. CGetSetOptions::SetPlaySoundOnCopy(m_csPlaySound);
  178. g_Opt.SetAllowDuplicates(m_btAllowDuplicates.GetCheck());
  179. g_Opt.SetUpdateTimeOnPaste(m_btUpdateTimeOnPaste.GetCheck());
  180. g_Opt.SetSaveMultiPaste(m_btSaveMultiPaste.GetCheck());
  181. CString csSep;
  182. m_ClipSeparator.GetWindowText(csSep);
  183. g_Opt.SetMultiPasteSeparator(csSep);
  184. CString csLanguage;
  185. if(m_cbLanguage.GetCurSel() >= 0)
  186. {
  187. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  188. g_Opt.SetLanguageFile(csLanguage);
  189. }
  190. if(csLanguage.IsEmpty() == FALSE)
  191. {
  192. if(!theApp.m_Language.LoadLanguageFile(csLanguage))
  193. {
  194. CString cs;
  195. cs.Format(_T("Error loading language file - %s - \n\n%s"), csLanguage, theApp.m_Language.m_csLastError);
  196. MessageBox(cs, _T("Ditto"), MB_OK);
  197. }
  198. }
  199. CString csMax;
  200. m_MaxClipSize.GetWindowText(csMax);
  201. g_Opt.SetMaxClipSizeInBytes(ATOI(csMax));
  202. if(!g_Opt.m_bU3 && m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  203. {
  204. CString csPath;
  205. m_ePath.GetWindowText(csPath);
  206. bool bOpenNewDatabase = false;
  207. if(csPath.IsEmpty() == FALSE)
  208. {
  209. if(FileExists(csPath) == FALSE)
  210. {
  211. CString cs;
  212. cs.Format(_T("The database %s does not exist.\n\nCreate a new database?"), csPath);
  213. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDYES)
  214. {
  215. // -- create a new one
  216. if(CreateDB(csPath))
  217. {
  218. bOpenNewDatabase = true;
  219. }
  220. else
  221. MessageBox(_T("Error Creating Database"));
  222. }
  223. else
  224. return FALSE;
  225. }
  226. else
  227. {
  228. if(ValidDB(csPath) == FALSE)
  229. {
  230. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  231. m_ePath.SetFocus();
  232. return FALSE;
  233. }
  234. else
  235. {
  236. bOpenNewDatabase = true;
  237. }
  238. }
  239. if(bOpenNewDatabase)
  240. {
  241. if(OpenDatabase(csPath) == FALSE)
  242. {
  243. MessageBox(_T("Error Opening new database"), _T("Ditto"), MB_OK);
  244. m_ePath.SetFocus();
  245. return FALSE;
  246. }
  247. else
  248. {
  249. theApp.RefreshView();
  250. }
  251. }
  252. }
  253. }
  254. return CPropertyPage::OnApply();
  255. }
  256. BOOL COptionsGeneral::OnSetActive()
  257. {
  258. return CPropertyPage::OnSetActive();
  259. }
  260. void COptionsGeneral::OnBtCompactAndRepair()
  261. {
  262. CWaitCursor wait;
  263. try
  264. {
  265. theApp.m_db.execDML(_T("PRAGMA auto_vacuum = 1"));
  266. theApp.m_db.execQuery(_T("VACUUM"));
  267. }
  268. CATCH_SQLITE_EXCEPTION
  269. }
  270. void COptionsGeneral::OnCheckForUpdates()
  271. {
  272. CInternetUpdate update;
  273. if(update.CheckForUpdate(m_hWnd, FALSE, TRUE))
  274. {
  275. ::PostMessage(theApp.m_MainhWnd, WM_CLOSE_APP, 0, 0);
  276. m_pParent->EndDialog(-1);
  277. }
  278. }
  279. void COptionsGeneral::OnSetDbPath()
  280. {
  281. if(m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  282. {
  283. m_ePath.EnableWindow(TRUE);
  284. m_btGetPath.EnableWindow(TRUE);
  285. }
  286. else
  287. {
  288. m_ePath.EnableWindow(FALSE);
  289. m_btGetPath.EnableWindow(FALSE);
  290. }
  291. }
  292. void COptionsGeneral::OnGetPath()
  293. {
  294. OPENFILENAME FileName;
  295. TCHAR szFileName[400];
  296. TCHAR szDir[400];
  297. memset(&FileName, 0, sizeof(FileName));
  298. memset(szFileName, 0, sizeof(szFileName));
  299. memset(&szDir, 0, sizeof(szDir));
  300. FileName.lStructSize = sizeof(FileName);
  301. FileName.lpstrTitle = _T("Open Database");
  302. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  303. FileName.nMaxFile = 400;
  304. FileName.lpstrFile = szFileName;
  305. FileName.lpstrInitialDir = szDir;
  306. FileName.lpstrFilter = _T("Ditto Databases (*.db; *.mdb)\0*.db;*.mdb\0\0");
  307. FileName.lpstrDefExt = _T("db");
  308. if(GetOpenFileName(&FileName) == 0)
  309. return;
  310. CString csPath(FileName.lpstrFile);
  311. CPath path(FileName.lpstrFile);
  312. if(path.GetExtension() == _T("mdb"))
  313. {
  314. CString cs;
  315. cs.Format(_T("The database '%s' must be converted to a Sqlite Database (Version 3 format).\n\nConvert database?"), FileName.lpstrFile);
  316. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDNO)
  317. return;
  318. CString csNewDBPath = path.RemoveExtension();
  319. //Make sure the db name is unique
  320. CString csTempName;
  321. csTempName.Format(_T("%s.db"), csNewDBPath);
  322. int i = 1;
  323. while(FileExists(csTempName))
  324. {
  325. csTempName.Format(_T("%s_%d.db"), csNewDBPath, i);
  326. i++;
  327. }
  328. csNewDBPath = csTempName;
  329. CreateDB(csNewDBPath);
  330. CAccessToSqlite Convert;
  331. if(Convert.ConvertDatabase(csNewDBPath, FileName.lpstrFile))
  332. {
  333. csPath = csNewDBPath;
  334. }
  335. else
  336. {
  337. MessageBox(_T("Error converting database."), _T("Ditto"), MB_OK);
  338. DeleteFile(csNewDBPath);
  339. return;
  340. }
  341. }
  342. if(FileExists(csPath))
  343. {
  344. if(ValidDB(csPath) == FALSE)
  345. {
  346. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  347. m_ePath.SetFocus();
  348. }
  349. else
  350. {
  351. m_ePath.SetWindowText(csPath);
  352. }
  353. }
  354. else
  355. {
  356. m_ePath.SetWindowText(csPath);
  357. }
  358. }
  359. void COptionsGeneral::OnSelectSound()
  360. {
  361. OPENFILENAME FileName;
  362. TCHAR szFileName[400];
  363. TCHAR szDir[400];
  364. memset(&FileName, 0, sizeof(FileName));
  365. memset(szFileName, 0, sizeof(szFileName));
  366. memset(&szDir, 0, sizeof(szDir));
  367. FileName.lStructSize = sizeof(FileName);
  368. FileName.lpstrTitle = _T("Select .wav file");
  369. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  370. FileName.nMaxFile = 400;
  371. FileName.lpstrFile = szFileName;
  372. FileName.lpstrInitialDir = szDir;
  373. FileName.lpstrFilter = _T("Sounds(*.wav)\0*.wav\0\0");
  374. FileName.lpstrDefExt = _T("wav");
  375. if(GetOpenFileName(&FileName) == 0)
  376. return;
  377. CString csPath(FileName.lpstrFile);
  378. if(csPath.GetLength())
  379. m_csPlaySound = csPath;
  380. UpdateData(FALSE);
  381. }
  382. void COptionsGeneral::OnButtonPlay()
  383. {
  384. UpdateData();
  385. PlaySound(m_csPlaySound, NULL, SND_FILENAME|SND_ASYNC);
  386. }
  387. void COptionsGeneral::OnButtonAbout()
  388. {
  389. CMultiLanguage Lang;
  390. CString csLanguage;
  391. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  392. Lang.SetOnlyGetHeader(true);
  393. if(Lang.LoadLanguageFile(csLanguage))
  394. {
  395. CString csMessage;
  396. csMessage.Format(_T("Language - %s\n")
  397. _T("Version - %d\n")
  398. _T("Author - %s\n")
  399. _T("Notes - %s"), csLanguage,
  400. Lang.GetVersion(),
  401. Lang.GetAuthor(),
  402. Lang.GetNotes());
  403. MessageBox(csMessage, _T("Ditto"), MB_OK);
  404. }
  405. else
  406. {
  407. CString csError;
  408. csError.Format(_T("Error loading language file - %s - reason = "), csLanguage, Lang.m_csLastError);
  409. MessageBox(csError, _T("Ditto"), MB_OK);
  410. }
  411. }