DatabaseUtilities.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // DatabaseUtilites.cpp: implementation of the CDatabaseUtilites class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "DatabaseUtilities.h"
  7. #include <io.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CString GetDBName()
  12. {
  13. return CGetSetOptions::GetDBPath();
  14. }
  15. CString GetDefaultDBName()
  16. {
  17. CString csDefaultPath;
  18. LPMALLOC pMalloc;
  19. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  20. {
  21. LPITEMIDLIST pidlPrograms;
  22. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  23. char string[MAX_PATH];
  24. SHGetPathFromIDList(pidlPrograms, string);
  25. pMalloc->Free(pidlPrograms);
  26. pMalloc->Release();
  27. csDefaultPath = string;
  28. csDefaultPath += "\\Ditto\\";
  29. if(_access(csDefaultPath, 0) == -1)
  30. CreateDirectory(csDefaultPath, NULL);
  31. csDefaultPath += DEFAULT_DB_NAME;
  32. }
  33. return csDefaultPath;
  34. }
  35. BOOL CheckDBExists(CString csDBPath)
  36. {
  37. if(_access(csDBPath, 0) == -1)
  38. {
  39. //Database didn't exist
  40. CGetSetOptions::SetDBPath("");
  41. // -- create a new one
  42. CreateDB(GetDefaultDBName());
  43. return TRUE;
  44. }
  45. BOOL bRet = FALSE;
  46. if(ValidDB(csDBPath) == FALSE)
  47. {
  48. theApp.CloseDB();
  49. CGetSetOptions::SetDBPath("");
  50. //Db existed but was bad
  51. CString csMarkAsBad;
  52. csMarkAsBad = csDBPath;
  53. csMarkAsBad.Replace(".", "_BAD.");
  54. CString csPath = GetDefaultDBName();
  55. CString cs;
  56. cs.Format("Unrecognized Database Format \"%s\",\n"
  57. "the file will be renamed \"%s\",\n"
  58. "and a new database will be created,\n"
  59. "\"%s\"", csDBPath, csMarkAsBad, csPath);
  60. AfxMessageBox(cs);
  61. CFile::Rename(csDBPath, csMarkAsBad);
  62. bRet = CreateDB(csPath);
  63. }
  64. else
  65. bRet = TRUE;
  66. return bRet;
  67. }
  68. BOOL ValidDB(CString csPath)
  69. {
  70. try
  71. {
  72. CDaoDatabase db;
  73. db.Open(csPath);
  74. CDaoTableDef table(&db);
  75. CDaoFieldInfo info;
  76. table.Open("Main");
  77. table.GetFieldInfo("lID", info);
  78. table.GetFieldInfo("lDate", info);
  79. table.GetFieldInfo("strType", info);
  80. table.GetFieldInfo("strText", info);
  81. table.GetFieldInfo("lShortCut", info);
  82. table.GetFieldInfo("lDontAutoDelete", info);
  83. table.GetFieldInfo("lTotalCopySize", info);
  84. table.Close();
  85. table.Open("Data");
  86. table.GetFieldInfo("lID", info);
  87. table.GetFieldInfo("lParentID", info);
  88. table.GetFieldInfo("strClipBoardFormat", info);
  89. table.GetFieldInfo("ooData", info);
  90. table.Close();
  91. table.Open("Types");
  92. table.GetFieldInfo("ID", info);
  93. table.GetFieldInfo("TypeText", info);
  94. table.Close();
  95. }
  96. catch(CDaoException* e)
  97. {
  98. e->Delete();
  99. ASSERT(FALSE);
  100. return FALSE;
  101. }
  102. return TRUE;
  103. }
  104. BOOL CreateDB(CString csPath)
  105. {
  106. CDaoDatabase db;
  107. try
  108. {
  109. EnsureDirectory(csPath);
  110. db.Create(csPath);
  111. CDaoTableDefEx table(&db);
  112. //Creat the Main table
  113. table.Create("Main");
  114. table.CreateField("lID", dbLong, 4, dbAutoIncrField);
  115. table.CreateIndex(TRUE, "lID");
  116. table.CreateField("lDate", dbLong, 4, 0, "0");
  117. table.CreateIndex(FALSE, "lDate");
  118. table.CreateField("strType", dbText, 50, dbVariableField);
  119. table.CreateField("strText", dbText, 255, dbVariableField);
  120. table.CreateField("lShortCut", dbLong, 4, 0, "0");
  121. table.CreateIndex(FALSE, "lShortCut");
  122. table.CreateField("lDontAutoDelete", dbLong, 4, 0, "0");
  123. table.CreateField("lTotalCopySize", dbLong, 4, 0, "0");
  124. table.Append();
  125. table.Close();
  126. //Create the Data Table
  127. table.Create("Data");
  128. table.CreateField("lID", dbLong, 4, dbAutoIncrField);
  129. table.CreateIndex(TRUE, "lID");
  130. table.CreateField("lParentID", dbLong, 4, 0, "0");
  131. table.CreateIndex(FALSE, "lParentID");
  132. table.CreateField("strClipBoardFormat", dbText, 50, dbVariableField);
  133. table.CreateField("ooData", dbLongBinary, 0);
  134. table.Append();
  135. table.Close();
  136. //Create the Types table
  137. table.Create("Types");
  138. table.CreateField("ID", dbLong, 4, dbAutoIncrField);
  139. table.CreateField("TypeText", dbText, 50, dbVariableField);
  140. table.Append();
  141. table.Close();
  142. db.Close();
  143. return TRUE;
  144. }
  145. catch(CDaoException *e)
  146. {
  147. ASSERT(FALSE);
  148. e->Delete();
  149. }
  150. return FALSE;
  151. }
  152. BOOL CompactDatabase()
  153. {
  154. if(!theApp.CloseDB())
  155. return FALSE;
  156. CString csDBName = GetDBName();
  157. CString csTempDBName = csDBName;
  158. csTempDBName.Replace(".mdb", "TempDBName.mdb");
  159. //Compact the database
  160. try
  161. {
  162. CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
  163. }
  164. catch(CDaoException* e)
  165. {
  166. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  167. DeleteFile(csTempDBName);
  168. e->Delete();
  169. return FALSE;
  170. }
  171. catch(CMemoryException* e)
  172. {
  173. AfxMessageBox("Memory Exception");
  174. DeleteFile(csTempDBName);
  175. e->Delete();
  176. return FALSE;
  177. }
  178. //Since compacting the database creates a new db delete the old one and replace it
  179. //with the compacted db
  180. if(DeleteFile(csDBName))
  181. {
  182. try
  183. {
  184. CFile::Rename(csTempDBName, csDBName);
  185. }
  186. catch(CFileException *e)
  187. {
  188. e->ReportError();
  189. e->Delete();
  190. return FALSE;
  191. }
  192. }
  193. else
  194. AfxMessageBox("Error Compacting Database");
  195. return TRUE;
  196. }
  197. BOOL RepairDatabase()
  198. {
  199. if(!theApp.CloseDB())
  200. return FALSE;
  201. try
  202. {
  203. CDaoWorkspace::RepairDatabase(GetDBName());
  204. }
  205. catch(CDaoException *e)
  206. {
  207. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  208. e->Delete();
  209. return FALSE;
  210. }
  211. return TRUE;
  212. }
  213. BOOL RemoveOldEntries()
  214. {
  215. if(CGetSetOptions::GetCheckForMaxEntries())
  216. {
  217. long lMax = CGetSetOptions::GetMaxEntries();
  218. CMainTable recset;
  219. recset.Open("SELECT * FROM Main ORDER BY lDate DESC");
  220. if(!recset.IsEOF())
  221. {
  222. recset.MoveLast();
  223. long lCount = recset.GetRecordCount();
  224. ARRAY IDs;
  225. while((lCount > lMax) && (!recset.IsBOF()))
  226. {
  227. //Don't delete entries that have shorcuts or the flag set
  228. if(recset.m_lDontAutoDelete <= 0)
  229. IDs.Add(recset.m_lID);
  230. lCount--;
  231. recset.MovePrev();
  232. }
  233. CClip::Delete(IDs);
  234. }
  235. }
  236. if(CGetSetOptions::GetCheckForExpiredEntries())
  237. {
  238. long lExpire = CGetSetOptions::GetExpiredEntries();
  239. if(lExpire)
  240. {
  241. CTime now = CTime::GetCurrentTime();
  242. now -= CTimeSpan(lExpire, 0, 0, 0);
  243. CMainTable recset;
  244. recset.Open("SELECT * FROM Main "
  245. "WHERE lDate < %d AND "
  246. "lShortCut <= 0 AND lDontAutoDelete <= 0", now.GetTime());
  247. ARRAY IDs;
  248. while(!recset.IsEOF())
  249. {
  250. IDs.Add(recset.m_lID);
  251. recset.MoveNext();
  252. }
  253. CClip::Delete(IDs);
  254. }
  255. }
  256. return TRUE;
  257. }
  258. BOOL EnsureDirectory(CString csPath)
  259. {
  260. char drive[_MAX_DRIVE];
  261. char dir[_MAX_DIR];
  262. char fname[_MAX_FNAME];
  263. char ext[_MAX_EXT];
  264. _splitpath(csPath, drive, dir, fname, ext);
  265. CString csDir(drive);
  266. csDir += dir;
  267. if(_access(csDir, 0) == -1)
  268. {
  269. if(CreateDirectory(csDir, NULL))
  270. return TRUE;
  271. }
  272. else
  273. return TRUE;
  274. return FALSE;
  275. }