OptionsGeneral.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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_EXPIRE_AFTER, m_eExpireAfter);
  47. DDX_Control(pDX, IDC_MAX_SAVED_COPIES, m_eMaxSavedCopies);
  48. DDX_Control(pDX, IDC_MAXIMUM, m_btMaximumCheck);
  49. DDX_Control(pDX, IDC_EXPIRE, m_btExpire);
  50. DDX_Control(pDX, IDC_DISPLAY_IN_SYSTEMTRAY, m_btShowIconInSysTray);
  51. DDX_Control(pDX, IDC_START_ON_STARTUP, m_btRunOnStartup);
  52. DDX_Text(pDX, IDC_EDIT_PLAY_SOUND, m_csPlaySound);
  53. DDX_Control(pDX, IDC_EDIT_CLIP_SEPARATOR, m_ClipSeparator);
  54. //}}AFX_DATA_MAP
  55. DDX_Control(pDX, IDC_ALLOW_DUPLICATES, m_btAllowDuplicates);
  56. DDX_Control(pDX, IDC_UPDATE_TIME_ON_PASTE, m_btUpdateTimeOnPaste);
  57. DDX_Control(pDX, IDC_SAVE_MULTIPASTE, m_btSaveMultiPaste);
  58. DDX_Control(pDX, IDC_EDIT_APP_COPY_INCLUDE, m_copyAppInclude);
  59. DDX_Control(pDX, IDC_EDIT_APP_COPY_EXCLUDE, m_copyAppExclude);
  60. }
  61. BEGIN_MESSAGE_MAP(COptionsGeneral, CPropertyPage)
  62. //{{AFX_MSG_MAP(COptionsGeneral)
  63. ON_BN_CLICKED(IDC_BT_COMPACT_AND_REPAIR, OnBtCompactAndRepair)
  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. if (CGetSetOptions::GetIsWindowsApp())
  78. {
  79. m_btRunOnStartup.SetCheck(BST_CHECKED);
  80. GetDlgItem(IDC_START_ON_STARTUP)->EnableWindow(FALSE);
  81. }
  82. else
  83. {
  84. m_btRunOnStartup.SetCheck(CGetSetOptions::GetRunOnStartUp());
  85. }
  86. m_btShowIconInSysTray.SetCheck(CGetSetOptions::GetShowIconInSysTray());
  87. m_btMaximumCheck.SetCheck(CGetSetOptions::GetCheckForMaxEntries());
  88. m_btExpire.SetCheck(CGetSetOptions::GetCheckForExpiredEntries());
  89. m_eExpireAfter.SetNumber(CGetSetOptions::GetExpiredEntries());
  90. m_eMaxSavedCopies.SetNumber(CGetSetOptions::GetMaxEntries());
  91. m_DescTextSize.SetNumber(g_Opt.m_bDescTextSize);
  92. m_SaveDelay.SetNumber(g_Opt.m_dwSaveClipDelay);
  93. m_btAllowDuplicates.SetCheck( g_Opt.m_bAllowDuplicates );
  94. m_btUpdateTimeOnPaste.SetCheck( g_Opt.m_bUpdateTimeOnPaste );
  95. m_btSaveMultiPaste.SetCheck( g_Opt.m_bSaveMultiPaste );
  96. m_btHideDittoOnHotKey.SetCheck(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown);
  97. m_btSendPasteMessage.SetCheck(g_Opt.m_bSendPasteMessageAfterSelection);
  98. m_EnsureConnected.SetCheck(g_Opt.m_bEnsureConnectToClipboard);
  99. m_copyAppInclude.SetWindowText(g_Opt.GetCopyAppInclude());
  100. m_copyAppExclude.SetWindowText(g_Opt.GetCopyAppExclude());
  101. m_ClipSeparator.SetWindowText(g_Opt.GetMultiPasteSeparator(false));
  102. if(g_Opt.m_lMaxClipSizeInBytes > 0)
  103. {
  104. CString csMax;
  105. csMax.Format(_T("%d"), g_Opt.m_lMaxClipSizeInBytes);
  106. m_MaxClipSize.SetWindowText(csMax);
  107. }
  108. CString csPath = CGetSetOptions::GetDBPath();
  109. if(csPath.IsEmpty())
  110. {
  111. m_ePath.EnableWindow(FALSE);
  112. m_btGetPath.EnableWindow(FALSE);
  113. csPath = CGetSetOptions::GetDBPath();
  114. m_ePath.SetWindowText(csPath);
  115. }
  116. else
  117. {
  118. m_btSetDatabasePath.SetCheck(BST_CHECKED);
  119. m_ePath.SetWindowText(csPath);
  120. }
  121. m_csPlaySound = g_Opt.m_csPlaySoundOnCopy;
  122. FillLanguages();
  123. UpdateData(FALSE);
  124. theApp.m_Language.UpdateOptionGeneral(this);
  125. return TRUE;
  126. }
  127. #define NO_MATCH -2
  128. #define FOUND_MATCH -1
  129. void COptionsGeneral::FillLanguages()
  130. {
  131. CString csFile = CGetSetOptions::GetPath(PATH_LANGUAGE);
  132. csFile += "*.xml";
  133. CString csLanguage = CGetSetOptions::GetLanguageFile();
  134. CFileFind find;
  135. BOOL bCont = find.FindFile(csFile);
  136. int nEnglishIndex = NO_MATCH;
  137. while(bCont)
  138. {
  139. bCont = find.FindNextFile();
  140. int nIndex = m_cbLanguage.AddString(find.GetFileTitle());
  141. if(find.GetFileTitle() == csLanguage)
  142. {
  143. nEnglishIndex = -1;
  144. m_cbLanguage.SetCurSel(nIndex);
  145. }
  146. else if(find.GetFileTitle() == _T("English"))
  147. {
  148. if(nEnglishIndex == NO_MATCH)
  149. nEnglishIndex = nIndex;
  150. }
  151. }
  152. if(nEnglishIndex >= 0)
  153. {
  154. m_cbLanguage.SetCurSel(nEnglishIndex);
  155. }
  156. }
  157. BOOL COptionsGeneral::OnApply()
  158. {
  159. UpdateData();
  160. ::SendMessage(theApp.m_MainhWnd, WM_SHOW_TRAY_ICON, m_btShowIconInSysTray.GetCheck(), 0);
  161. CGetSetOptions::SetShowIconInSysTray(m_btShowIconInSysTray.GetCheck());
  162. if (CGetSetOptions::GetIsWindowsApp() == FALSE)
  163. {
  164. CGetSetOptions::SetRunOnStartUp(m_btRunOnStartup.GetCheck());
  165. }
  166. CGetSetOptions::SetCheckForMaxEntries(m_btMaximumCheck.GetCheck());
  167. CGetSetOptions::SetCheckForExpiredEntries(m_btExpire.GetCheck());
  168. CGetSetOptions::SetHideDittoOnHotKeyIfAlreadyShown(m_btHideDittoOnHotKey.GetCheck());
  169. CGetSetOptions::SetSendPasteAfterSelection(m_btSendPasteMessage.GetCheck());
  170. CGetSetOptions::SetEnsureConnectToClipboard(m_EnsureConnected.GetCheck());
  171. CGetSetOptions::SetMaxEntries(m_eMaxSavedCopies.GetNumber());
  172. CGetSetOptions::SetExpiredEntries(m_eExpireAfter.GetNumber());
  173. CGetSetOptions::SetDescTextSize(m_DescTextSize.GetNumber());
  174. CGetSetOptions::SetSaveClipDelay(m_SaveDelay.GetNumber());
  175. CGetSetOptions::SetPlaySoundOnCopy(m_csPlaySound);
  176. g_Opt.SetAllowDuplicates(m_btAllowDuplicates.GetCheck());
  177. g_Opt.SetUpdateTimeOnPaste(m_btUpdateTimeOnPaste.GetCheck());
  178. g_Opt.SetSaveMultiPaste(m_btSaveMultiPaste.GetCheck());
  179. CString stringVal;
  180. m_ClipSeparator.GetWindowText(stringVal);
  181. g_Opt.SetMultiPasteSeparator(stringVal);
  182. m_copyAppInclude.GetWindowText(stringVal);
  183. g_Opt.SetCopyAppInclude(stringVal);
  184. m_copyAppExclude.GetWindowText(stringVal);
  185. g_Opt.SetCopyAppExclude(stringVal);
  186. CString csLanguage;
  187. if(m_cbLanguage.GetCurSel() >= 0)
  188. {
  189. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  190. g_Opt.SetLanguageFile(csLanguage);
  191. }
  192. if(csLanguage.IsEmpty() == FALSE)
  193. {
  194. if(!theApp.m_Language.LoadLanguageFile(csLanguage))
  195. {
  196. CString cs;
  197. cs.Format(_T("Error loading language file - %s - \n\n%s"), csLanguage, theApp.m_Language.m_csLastError);
  198. MessageBox(cs, _T("Ditto"), MB_OK);
  199. }
  200. }
  201. CString csMax;
  202. m_MaxClipSize.GetWindowText(csMax);
  203. g_Opt.SetMaxClipSizeInBytes(ATOI(csMax));
  204. if(m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  205. {
  206. CString csPath;
  207. m_ePath.GetWindowText(csPath);
  208. bool bOpenNewDatabase = false;
  209. if(csPath.IsEmpty() == FALSE)
  210. {
  211. if(FileExists(csPath) == FALSE)
  212. {
  213. CString cs;
  214. cs.Format(_T("The database %s does not exist.\n\nCreate a new database?"), csPath);
  215. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDYES)
  216. {
  217. // -- create a new one
  218. if(CreateDB(csPath))
  219. {
  220. bOpenNewDatabase = true;
  221. }
  222. else
  223. MessageBox(_T("Error Creating Database"));
  224. }
  225. else
  226. return FALSE;
  227. }
  228. else
  229. {
  230. if(ValidDB(csPath) == FALSE)
  231. {
  232. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  233. m_ePath.SetFocus();
  234. return FALSE;
  235. }
  236. else
  237. {
  238. bOpenNewDatabase = true;
  239. }
  240. }
  241. if(bOpenNewDatabase)
  242. {
  243. if(OpenDatabase(csPath) == FALSE)
  244. {
  245. MessageBox(_T("Error Opening new database"), _T("Ditto"), MB_OK);
  246. m_ePath.SetFocus();
  247. return FALSE;
  248. }
  249. else
  250. {
  251. theApp.RefreshView();
  252. }
  253. }
  254. }
  255. }
  256. return CPropertyPage::OnApply();
  257. }
  258. BOOL COptionsGeneral::OnSetActive()
  259. {
  260. return CPropertyPage::OnSetActive();
  261. }
  262. void COptionsGeneral::OnBtCompactAndRepair()
  263. {
  264. CWaitCursor wait;
  265. try
  266. {
  267. try
  268. {
  269. for(int i = 0; i < 100; i++)
  270. {
  271. int toDeleteCount = theApp.m_db.execScalar(_T("SELECT COUNT(clipID) FROM MainDeletes"));
  272. if(toDeleteCount <= 0)
  273. break;
  274. RemoveOldEntries(false);
  275. }
  276. }
  277. CATCH_SQLITE_EXCEPTION
  278. theApp.m_db.execDML(_T("PRAGMA auto_vacuum = 1"));
  279. theApp.m_db.execQuery(_T("VACUUM"));
  280. }
  281. CATCH_SQLITE_EXCEPTION
  282. }
  283. void COptionsGeneral::OnSetDbPath()
  284. {
  285. if(m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  286. {
  287. m_ePath.EnableWindow(TRUE);
  288. m_btGetPath.EnableWindow(TRUE);
  289. }
  290. else
  291. {
  292. m_ePath.EnableWindow(FALSE);
  293. m_btGetPath.EnableWindow(FALSE);
  294. }
  295. }
  296. void COptionsGeneral::OnGetPath()
  297. {
  298. OPENFILENAME FileName;
  299. TCHAR szFileName[400];
  300. TCHAR szDir[400];
  301. memset(&FileName, 0, sizeof(FileName));
  302. memset(szFileName, 0, sizeof(szFileName));
  303. memset(&szDir, 0, sizeof(szDir));
  304. FileName.lStructSize = sizeof(FileName);
  305. FileName.lpstrTitle = _T("Open Database");
  306. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
  307. FileName.nMaxFile = 400;
  308. FileName.lpstrFile = szFileName;
  309. FileName.lpstrInitialDir = szDir;
  310. FileName.lpstrFilter = _T("Ditto Databases (*.db; *.mdb)\0*.db;*.mdb\0\0");
  311. FileName.lpstrDefExt = _T("db");
  312. FileName.hwndOwner = m_hWnd;
  313. if(GetOpenFileName(&FileName) == 0)
  314. return;
  315. CString csPath(FileName.lpstrFile);
  316. CPath path(FileName.lpstrFile);
  317. if(path.GetExtension() == _T("mdb"))
  318. {
  319. CString cs;
  320. cs.Format(_T("The database '%s' must be converted to a Sqlite Database (Version 3 format).\n\nConvert database?"), FileName.lpstrFile);
  321. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDNO)
  322. return;
  323. CString csNewDBPath = path.RemoveExtension();
  324. //Make sure the db name is unique
  325. CString csTempName;
  326. csTempName.Format(_T("%s.db"), csNewDBPath);
  327. int i = 1;
  328. while(FileExists(csTempName))
  329. {
  330. csTempName.Format(_T("%s_%d.db"), csNewDBPath, i);
  331. i++;
  332. }
  333. csNewDBPath = csTempName;
  334. CreateDB(csNewDBPath);
  335. CAccessToSqlite Convert;
  336. if(Convert.ConvertDatabase(csNewDBPath, FileName.lpstrFile))
  337. {
  338. csPath = csNewDBPath;
  339. }
  340. else
  341. {
  342. MessageBox(_T("Error converting database."), _T("Ditto"), MB_OK);
  343. DeleteFile(csNewDBPath);
  344. return;
  345. }
  346. }
  347. if(FileExists(csPath))
  348. {
  349. if(ValidDB(csPath) == FALSE)
  350. {
  351. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  352. m_ePath.SetFocus();
  353. }
  354. else
  355. {
  356. m_ePath.SetWindowText(csPath);
  357. }
  358. }
  359. else
  360. {
  361. m_ePath.SetWindowText(csPath);
  362. }
  363. }
  364. void COptionsGeneral::OnSelectSound()
  365. {
  366. OPENFILENAME FileName;
  367. TCHAR szFileName[400];
  368. TCHAR szDir[400];
  369. memset(&FileName, 0, sizeof(FileName));
  370. memset(szFileName, 0, sizeof(szFileName));
  371. memset(&szDir, 0, sizeof(szDir));
  372. FileName.lStructSize = sizeof(FileName);
  373. FileName.lpstrTitle = _T("Select .wav file");
  374. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
  375. FileName.nMaxFile = 400;
  376. FileName.lpstrFile = szFileName;
  377. FileName.lpstrInitialDir = szDir;
  378. FileName.lpstrFilter = _T("Sounds(*.wav)\0*.wav\0\0");
  379. FileName.lpstrDefExt = _T("wav");
  380. FileName.hwndOwner = m_hWnd;
  381. if(GetOpenFileName(&FileName) == 0)
  382. return;
  383. CString csPath(FileName.lpstrFile);
  384. if(csPath.GetLength())
  385. m_csPlaySound = csPath;
  386. UpdateData(FALSE);
  387. }
  388. void COptionsGeneral::OnButtonPlay()
  389. {
  390. UpdateData();
  391. PlaySound(m_csPlaySound, NULL, SND_FILENAME|SND_ASYNC);
  392. }
  393. void COptionsGeneral::OnButtonAbout()
  394. {
  395. CMultiLanguage Lang;
  396. CString csLanguage;
  397. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  398. Lang.SetOnlyGetHeader(true);
  399. if(Lang.LoadLanguageFile(csLanguage))
  400. {
  401. CString csMessage;
  402. csMessage.Format(_T("Language - %s\n")
  403. _T("Version - %d\n")
  404. _T("Author - %s\n")
  405. _T("Notes - %s"), csLanguage,
  406. Lang.GetVersion(),
  407. Lang.GetAuthor(),
  408. Lang.GetNotes());
  409. MessageBox(csMessage, _T("Ditto"), MB_OK);
  410. }
  411. else
  412. {
  413. CString csError;
  414. csError.Format(_T("Error loading language file - %s - reason = "), csLanguage, Lang.m_csLastError);
  415. MessageBox(csError, _T("Ditto"), MB_OK);
  416. }
  417. }