DatabaseUtilities.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 "ProcessPaste.h"
  8. #include <io.h>
  9. #include "AccessToSqlite.h"
  10. #include "Path.h"
  11. #include "InternetUpdate.h"
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. BOOL CreateBackup(CString csPath)
  16. {
  17. CString csOriginal;
  18. int count = 0;
  19. // create a backup of the existing database
  20. do
  21. {
  22. count++;
  23. csOriginal = csPath + StrF(_T(".%03d"), count);
  24. // in case of some weird infinite loop
  25. if( count > 50 )
  26. {
  27. ASSERT(0);
  28. return FALSE;
  29. }
  30. } while( !::CopyFile(csPath, csOriginal, TRUE));
  31. return TRUE;
  32. }
  33. CString GetDBName()
  34. {
  35. return CGetSetOptions::GetDBPath();
  36. }
  37. CString GetOLDDefaultDBName()
  38. {
  39. CString csDefaultPath;
  40. LPMALLOC pMalloc;
  41. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  42. {
  43. LPITEMIDLIST pidlPrograms;
  44. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  45. TCHAR string[MAX_PATH];
  46. SHGetPathFromIDList(pidlPrograms, string);
  47. pMalloc->Free(pidlPrograms);
  48. pMalloc->Release();
  49. csDefaultPath = string;
  50. csDefaultPath += "\\Ditto\\";
  51. csDefaultPath += "DittoDB.mdb";
  52. }
  53. return csDefaultPath;
  54. }
  55. CString GetDefaultDBName()
  56. {
  57. CString csDefaultPath = _T("c:\\program files\\Ditto\\");
  58. if(g_Opt.m_bU3)
  59. {
  60. csDefaultPath = CGetSetOptions::GetPath(PATH_DATABASE);
  61. }
  62. else
  63. {
  64. //If portable then default to the running path
  65. if(CGetSetOptions::GetIsPortableDitto())
  66. {
  67. csDefaultPath.Empty();
  68. }
  69. else
  70. {
  71. LPMALLOC pMalloc;
  72. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  73. {
  74. LPITEMIDLIST pidlPrograms;
  75. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  76. TCHAR string[MAX_PATH];
  77. SHGetPathFromIDList(pidlPrograms, string);
  78. pMalloc->Free(pidlPrograms);
  79. pMalloc->Release();
  80. csDefaultPath = string;
  81. }
  82. FIX_CSTRING_PATH(csDefaultPath);
  83. csDefaultPath += "Ditto\\";
  84. }
  85. }
  86. CString csTempName = csDefaultPath + "Ditto.db";
  87. int i = 1;
  88. while(FileExists(csTempName))
  89. {
  90. csTempName.Format(_T("%sDitto_%d.db"), csDefaultPath, i);
  91. i++;
  92. }
  93. csDefaultPath = csTempName;
  94. return csDefaultPath;
  95. }
  96. BOOL CheckDBExists(CString csDBPath)
  97. {
  98. //If this is the first time running this version then convert the old database to the new db
  99. if(csDBPath.IsEmpty() && g_Opt.m_bU3 == false)
  100. {
  101. csDBPath = GetDefaultDBName();
  102. if(FileExists(csDBPath) == FALSE && CGetSetOptions::GetIsPortableDitto() == FALSE)
  103. {
  104. CString csOldDB = CGetSetOptions::GetDBPathOld();
  105. if(csOldDB.IsEmpty())
  106. {
  107. csOldDB = GetOLDDefaultDBName();
  108. }
  109. if(FileExists(csOldDB))
  110. {
  111. //create the new sqlite db
  112. CreateDB(csDBPath);
  113. CAccessToSqlite Convert;
  114. Convert.ConvertDatabase(csDBPath, csOldDB);
  115. }
  116. }
  117. }
  118. BOOL bRet = FALSE;
  119. if(FileExists(csDBPath) == FALSE)
  120. {
  121. csDBPath = GetDefaultDBName();
  122. nsPath::CPath FullPath(csDBPath);
  123. CString csPath = FullPath.GetPath().GetStr();
  124. if(csPath.IsEmpty() == false && FileExists(csDBPath) == FALSE)
  125. {
  126. CreateDirectory(csPath, NULL);
  127. }
  128. // -- create a new one
  129. bRet = CreateDB(csDBPath);
  130. }
  131. else
  132. {
  133. if(ValidDB(csDBPath) == FALSE)
  134. {
  135. //Db existed but was bad
  136. CString csMarkAsBad;
  137. csMarkAsBad = csDBPath;
  138. csMarkAsBad.Replace(_T("."), _T("_BAD."));
  139. CString csPath = GetDefaultDBName();
  140. CString cs;
  141. cs.Format(_T("%s \"%s\",\n")
  142. _T("%s \"%s\",\n")
  143. _T("%s,\n")
  144. _T("\"%s\""),
  145. theApp.m_Language.GetString("Database_Format", "Unrecognized Database Format"),
  146. csDBPath,
  147. theApp.m_Language.GetString("File_Renamed", "the file will be renamed"),
  148. csMarkAsBad,
  149. theApp.m_Language.GetString("New_Database", "and a new database will be created"),
  150. csPath);
  151. AfxMessageBox(cs);
  152. CFile::Rename(csDBPath, csMarkAsBad);
  153. csDBPath = csPath;
  154. bRet = CreateDB(csDBPath);
  155. }
  156. else
  157. {
  158. bRet = TRUE;
  159. }
  160. }
  161. if(bRet)
  162. {
  163. bRet = OpenDatabase(csDBPath);
  164. }
  165. return bRet;
  166. }
  167. BOOL OpenDatabase(CString csDB)
  168. {
  169. try
  170. {
  171. theApp.m_db.close();
  172. theApp.m_db.open(csDB);
  173. CGetSetOptions::SetDBPath(csDB);
  174. theApp.m_db.setBusyTimeout(CGetSetOptions::GetDbTimeout());
  175. return TRUE;
  176. }
  177. CATCH_SQLITE_EXCEPTION
  178. return FALSE;
  179. }
  180. BOOL ValidDB(CString csPath, BOOL bUpgrade)
  181. {
  182. try
  183. {
  184. BOOL didBackup = FALSE;
  185. CString backupFilePrefix = _T("Before_Update_To");
  186. CppSQLite3DB db;
  187. db.open(csPath);
  188. db.execQuery(_T("SELECT lID, lDate, mText, lShortCut, lDontAutoDelete, ")
  189. _T("CRC, bIsGroup, lParentID, QuickPasteText ")
  190. _T("FROM Main"));
  191. db.execQuery(_T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data"));
  192. db.execQuery(_T("SELECT lID, TypeText FROM Types"));
  193. try
  194. {
  195. db.execDML(_T("DROP TRIGGER delete_data_trigger"));
  196. }
  197. catch(CppSQLite3Exception& e)
  198. {
  199. e.errorCode();
  200. }
  201. try
  202. {
  203. db.execDML(_T("DROP TRIGGER delete_copy_buffer_trigger"));
  204. }
  205. catch(CppSQLite3Exception& e)
  206. {
  207. e.errorCode();
  208. }
  209. //This was added later so try to add each time and catch the exception here
  210. try
  211. {
  212. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  213. _T("BEGIN\n")
  214. _T("INSERT INTO MainDeletes VALUES(old.lID, datetime('now'));\n")
  215. _T("END\n"));
  216. }
  217. catch(CppSQLite3Exception& e)
  218. {
  219. if(didBackup == FALSE)
  220. didBackup = BackupDB(csPath, backupFilePrefix);
  221. e.errorCode();
  222. }
  223. //This was added later so try to add each time and catch the exception here
  224. try
  225. {
  226. db.execQuery(_T("SELECT lID, lClipID, lCopyBuffer FROM CopyBuffers"));
  227. }
  228. catch(CppSQLite3Exception& e)
  229. {
  230. if(didBackup == FALSE)
  231. didBackup = BackupDB(csPath, backupFilePrefix);
  232. e.errorCode();
  233. db.execDML(_T("CREATE TABLE CopyBuffers(")
  234. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  235. _T("lClipID INTEGER,")
  236. _T("lCopyBuffer INTEGER)"));
  237. }
  238. //This was added later so try to add each time and catch the exception here
  239. try
  240. {
  241. db.execQuery(_T("SELECT clipId FROM MainDeletes"));
  242. }
  243. catch(CppSQLite3Exception& e)
  244. {
  245. if(didBackup == FALSE)
  246. didBackup = BackupDB(csPath, backupFilePrefix);
  247. e.errorCode();
  248. db.execDML(_T("CREATE TABLE MainDeletes(")
  249. _T("clipID INTEGER,")
  250. _T("modifiedDate)"));
  251. db.execDML(_T("CREATE TRIGGER MainDeletes_delete_data_trigger BEFORE DELETE ON MainDeletes FOR EACH ROW\n")
  252. _T("BEGIN\n")
  253. _T("DELETE FROM CopyBuffers WHERE lClipID = old.clipID;\n")
  254. _T("DELETE FROM Data WHERE lParentID = old.clipID;\n")
  255. _T("END\n"));
  256. }
  257. try
  258. {
  259. db.execDML(_T("CREATE INDEX Main_ParentId on Main(lParentID DESC)"));
  260. db.execDML(_T("CREATE INDEX Main_IsGroup on Main(bIsGroup DESC)"));
  261. db.execDML(_T("CREATE INDEX Main_ShortCut on Main(lShortCut DESC)"));
  262. }
  263. catch(CppSQLite3Exception& e)
  264. {
  265. e.errorCode();
  266. }
  267. try
  268. {
  269. db.execQuery(_T("SELECT clipOrder, clipGroupOrder FROM Main"));
  270. }
  271. catch(CppSQLite3Exception& e)
  272. {
  273. if(didBackup == FALSE)
  274. didBackup = BackupDB(csPath, backupFilePrefix);
  275. db.execDML(_T("ALTER TABLE Main ADD clipOrder REAL"));
  276. db.execDML(_T("ALTER TABLE Main ADD clipGroupOrder REAL"));
  277. db.execDML(_T("Update Main set clipOrder = lDate, clipGroupOrder = lDate"));
  278. db.execDML(_T("CREATE INDEX Main_ClipOrder on Main(clipOrder DESC)"));
  279. db.execDML(_T("CREATE INDEX Main_ClipGroupOrder on Main(clipGroupOrder DESC)"));
  280. db.execDML(_T("DROP INDEX Main_Date"));
  281. e.errorCode();
  282. }
  283. try
  284. {
  285. db.execQuery(_T("SELECT globalShortCut FROM Main"));
  286. }
  287. catch(CppSQLite3Exception& e)
  288. {
  289. if(didBackup == FALSE)
  290. didBackup = BackupDB(csPath, backupFilePrefix);
  291. db.execDML(_T("ALTER TABLE Main ADD globalShortCut INTEGER"));
  292. e.errorCode();
  293. }
  294. try
  295. {
  296. db.execQuery(_T("SELECT lastPasteDate FROM Main"));
  297. }
  298. catch(CppSQLite3Exception& e)
  299. {
  300. if(didBackup == FALSE)
  301. didBackup = BackupDB(csPath, backupFilePrefix);
  302. db.execDML(_T("ALTER TABLE Main ADD lastPasteDate INTEGER"));
  303. db.execDML(_T("Update Main set lastPasteDate = lDate"));
  304. db.execDMLEx(_T("Update Main set lastPasteDate = %d where lastPasteDate <= 0"), (int)CTime::GetCurrentTime().GetTime());
  305. e.errorCode();
  306. }
  307. try
  308. {
  309. db.execQuery(_T("SELECT stickyClipOrder FROM Main"));
  310. }
  311. catch (CppSQLite3Exception& e)
  312. {
  313. if (didBackup == FALSE)
  314. didBackup = BackupDB(csPath, backupFilePrefix);
  315. db.execDML(_T("ALTER TABLE Main ADD stickyClipOrder REAL"));
  316. db.execDML(_T("ALTER TABLE Main ADD stickyClipGroupOrder REAL"));
  317. e.errorCode();
  318. }
  319. try
  320. {
  321. CppSQLite3Query q = db.execQuery(_T("PRAGMA index_info(Main_NoGroup);"));
  322. int count = 0;
  323. while (q.eof() == false)
  324. {
  325. count++;
  326. q.nextRow();
  327. }
  328. if(count == 0)
  329. {
  330. if (didBackup == FALSE)
  331. didBackup = BackupDB(csPath, backupFilePrefix);
  332. db.execDML(_T("Update Main set stickyClipOrder = -(2147483647) where stickyClipOrder IS NULL;"));
  333. db.execDML(_T("Update Main set stickyClipGroupOrder = -(2147483647) where stickyClipGroupOrder IS NULL;"));
  334. db.execDML(_T("Update Main set stickyClipOrder = -(2147483647) where stickyClipOrder = 0;"));
  335. db.execDML(_T("Update Main set stickyClipGroupOrder = -(2147483647) where stickyClipGroupOrder = 0;"));
  336. db.execDML(_T("CREATE INDEX Main_NoGroup ON Main(bIsGroup ASC, stickyClipOrder DESC, clipOrder DESC);"));
  337. db.execDML(_T("CREATE INDEX Main_InGroup ON Main(lParentId ASC, bIsGroup ASC, stickyClipGroupOrder DESC, clipGroupOrder DESC);"));
  338. db.execDML(_T("CREATE INDEX Data_ParentId_Format ON Data(lParentID COLLATE BINARY ASC, strClipBoardFormat COLLATE NOCASE ASC);"));
  339. }
  340. }
  341. catch (CppSQLite3Exception& e)
  342. {
  343. if (didBackup == FALSE)
  344. didBackup = BackupDB(csPath, backupFilePrefix);
  345. e.errorCode();
  346. }
  347. }
  348. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  349. return TRUE;
  350. }
  351. BOOL BackupDB(CString dbPath, CString prefix)
  352. {
  353. CString backup = GetFilePath(dbPath);
  354. CInternetUpdate update;
  355. long runningVersion = update.GetRunningVersion();
  356. CString versionString = update.GetVersionString(runningVersion);
  357. backup += GetFileName(dbPath) += _T("_") + prefix + _T("_") + versionString;
  358. backup.Replace(_T(".db"), _T(""));
  359. backup.Replace(_T("."), _T("_"));
  360. CString temp = backup;
  361. temp += _T(".db");
  362. int i = 1;
  363. while(FileExists(temp))
  364. {
  365. temp.Format(_T("%s_%d.db"), backup, i);
  366. i++;
  367. }
  368. backup = temp;
  369. BOOL ret = CopyFile(dbPath, backup, TRUE);
  370. return ret;
  371. }
  372. BOOL CreateDB(CString csFile)
  373. {
  374. try
  375. {
  376. CppSQLite3DB db;
  377. db.open(csFile);
  378. db.execDML(_T("PRAGMA auto_vacuum = 1"));
  379. db.execDML(_T("CREATE TABLE Main(")
  380. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  381. _T("lDate INTEGER, ")
  382. _T("mText TEXT, ")
  383. _T("lShortCut INTEGER, ")
  384. _T("lDontAutoDelete INTEGER, ")
  385. _T("CRC INTEGER, ")
  386. _T("bIsGroup INTEGER, ")
  387. _T("lParentID INTEGER, ")
  388. _T("QuickPasteText TEXT, ")
  389. _T("clipOrder REAL, ")
  390. _T("clipGroupOrder REAL, ")
  391. _T("globalShortCut INTEGER, ")
  392. _T("lastPasteDate INTEGER, ")
  393. _T("stickyClipOrder REAL, ")
  394. _T("stickyClipGroupOrder REAL);"));
  395. db.execDML(_T("CREATE TABLE Data(")
  396. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  397. _T("lParentID INTEGER, ")
  398. _T("strClipBoardFormat TEXT, ")
  399. _T("ooData BLOB);"));
  400. db.execDML(_T("CREATE TABLE Types(")
  401. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  402. _T("TypeText TEXT);"));
  403. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  404. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  405. db.execDML(_T("CREATE INDEX Main_ClipOrder on Main(clipOrder DESC)"));
  406. db.execDML(_T("CREATE INDEX Main_ClipGroupOrder on Main(clipGroupOrder DESC)"));
  407. db.execDML(_T("CREATE INDEX Main_ParentId on Main(lParentID DESC)"));
  408. db.execDML(_T("CREATE INDEX Main_IsGroup on Main(bIsGroup DESC)"));
  409. db.execDML(_T("CREATE INDEX Main_ShortCut on Main(lShortCut DESC)"));
  410. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  411. _T("BEGIN\n")
  412. _T("INSERT INTO MainDeletes VALUES(old.lID, datetime('now'));\n")
  413. _T("END\n"));
  414. db.execDML(_T("CREATE TABLE CopyBuffers(")
  415. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  416. _T("lClipID INTEGER, ")
  417. _T("lCopyBuffer INTEGER)"));
  418. db.execDML(_T("CREATE TABLE MainDeletes(")
  419. _T("clipID INTEGER,")
  420. _T("modifiedDate)"));
  421. db.execDML(_T("CREATE TRIGGER MainDeletes_delete_data_trigger BEFORE DELETE ON MainDeletes FOR EACH ROW\n")
  422. _T("BEGIN\n")
  423. _T("DELETE FROM CopyBuffers WHERE lClipID = old.clipID;\n")
  424. _T("DELETE FROM Data WHERE lParentID = old.clipID;\n")
  425. _T("END\n"));
  426. db.execDML(_T("CREATE INDEX Main_NoGroup ON Main(bIsGroup ASC, stickyClipOrder DESC, clipOrder DESC);"));
  427. db.execDML(_T("CREATE INDEX Main_InGroup ON Main(lParentId ASC, bIsGroup ASC, stickyClipGroupOrder DESC, clipGroupOrder DESC);"));
  428. db.execDML(_T("CREATE INDEX Data_ParentId_Format ON Data(lParentID COLLATE BINARY ASC, strClipBoardFormat COLLATE NOCASE ASC);"));
  429. db.close();
  430. }
  431. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  432. return TRUE;
  433. }
  434. BOOL CompactDatabase()
  435. {
  436. // if(!theApp.CloseDB())
  437. // return FALSE;
  438. //
  439. // CString csDBName = GetDBName();
  440. // CString csTempDBName = csDBName;
  441. // csTempDBName.Replace(".mdb", "TempDBName.mdb");
  442. //
  443. // //Compact the database
  444. // try
  445. // {
  446. // CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
  447. // }
  448. // catch(CDaoException* e)
  449. // {
  450. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  451. // DeleteFile(csTempDBName);
  452. // e->Delete();
  453. // return FALSE;
  454. // }
  455. // catch(CMemoryException* e)
  456. // {
  457. // AfxMessageBox("Memory Exception");
  458. // DeleteFile(csTempDBName);
  459. // e->Delete();
  460. // return FALSE;
  461. // }
  462. //
  463. // //Since compacting the database creates a new db delete the old one and replace it
  464. // //with the compacted db
  465. // if(DeleteFile(csDBName))
  466. // {
  467. // try
  468. // {
  469. // CFile::Rename(csTempDBName, csDBName);
  470. // }
  471. // catch(CFileException *e)
  472. // {
  473. // e->ReportError();
  474. // e->Delete();
  475. // return FALSE;
  476. // }
  477. // }
  478. // else
  479. // AfxMessageBox("Error Compacting Database");
  480. return TRUE;
  481. }
  482. BOOL RepairDatabase()
  483. {
  484. // if(!theApp.CloseDB())
  485. // return FALSE;
  486. // try
  487. // {
  488. // CDaoWorkspace::RepairDatabase(GetDBName());
  489. // }
  490. // catch(CDaoException *e)
  491. // {
  492. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  493. // e->Delete();
  494. // return FALSE;
  495. // }
  496. return TRUE;
  497. }
  498. BOOL RemoveOldEntries(bool checkIdleTime)
  499. {
  500. Log(StrF(_T("Beginning of RemoveOldEntries MaxEntries: %d - Keep days: %d"), CGetSetOptions::GetMaxEntries(), CGetSetOptions::GetExpiredEntries()));
  501. try
  502. {
  503. CppSQLite3DB db;
  504. CString csDbPath = CGetSetOptions::GetDBPath();
  505. db.open(csDbPath);
  506. if(CGetSetOptions::GetCheckForMaxEntries())
  507. {
  508. long lMax = CGetSetOptions::GetMaxEntries();
  509. if(lMax >= 0)
  510. {
  511. CClipIDs IDs;
  512. int clipId;
  513. CppSQLite3Query q = db.execQueryEx(_T("SELECT lID, lShortCut, lParentID, lDontAutoDelete, stickyClipOrder, stickyClipGroupOrder FROM Main WHERE bIsGroup = 0 ORDER BY clipOrder DESC LIMIT -1 OFFSET %d"), lMax);
  514. while(q.eof() == false)
  515. {
  516. int shortcut = q.getIntField(_T("lShortCut"));
  517. int dontDelete = q.getIntField(_T("lDontAutoDelete"));
  518. int parentId = q.getIntField(_T("lParentID"));
  519. double stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  520. double stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  521. //Only delete entries that have no shortcut and don't have the flag set and aren't in groups and
  522. if(shortcut == 0 &&
  523. dontDelete == 0 &&
  524. parentId <= 0 &&
  525. stickyClipOrder == 0.0 &&
  526. stickyClipGroupOrder == 0.0)
  527. {
  528. clipId = q.getIntField(_T("lID"));
  529. IDs.Add(clipId);
  530. Log(StrF(_T("From MaxEntries - Deleting Id: %d"), clipId));
  531. }
  532. q.nextRow();
  533. }
  534. if(IDs.GetCount() > 0)
  535. {
  536. IDs.DeleteIDs(false, db);
  537. }
  538. }
  539. }
  540. if(CGetSetOptions::GetCheckForExpiredEntries())
  541. {
  542. long lExpire = CGetSetOptions::GetExpiredEntries();
  543. if(lExpire)
  544. {
  545. CTime now = CTime::GetCurrentTime();
  546. now -= CTimeSpan(lExpire, 0, 0, 0);
  547. CClipIDs IDs;
  548. CppSQLite3Query q = db.execQueryEx(_T("SELECT lID FROM Main ")
  549. _T("WHERE lastPasteDate < %d AND ")
  550. _T("bIsGroup = 0 AND lShortCut = 0 AND lParentID <= 0 AND lDontAutoDelete = 0 AND stickyClipOrder = 0 AND stickyClipGroupOrder = 0"), (int)now.GetTime());
  551. while(q.eof() == false)
  552. {
  553. IDs.Add(q.getIntField(_T("lID")));
  554. Log(StrF(_T("From Clips Expire - Deleting Id: %d"), q.getIntField(_T("lID"))));
  555. q.nextRow();
  556. }
  557. if(IDs.GetCount() > 0)
  558. {
  559. IDs.DeleteIDs(false, db);
  560. }
  561. }
  562. }
  563. int toDeleteCount = db.execScalar(_T("SELECT COUNT(clipID) FROM MainDeletes"));
  564. Log(StrF(_T("Before Deleting emptied out data, count: %d, Idle Seconds: %f"), toDeleteCount, IdleSeconds()));
  565. //Only delete 1 at a time, was finding that it was taking a long time to delete clips, locking the db and causing other queries
  566. //to lock up
  567. CppSQLite3Query q = db.execQueryEx(_T("SELECT * FROM MainDeletes LIMIT %d"), CGetSetOptions::GetMainDeletesDeleteCount());
  568. int deleteCount = 0;
  569. while(q.eof() == false)
  570. {
  571. double idleSeconds = IdleSeconds();
  572. if(checkIdleTime == false || idleSeconds > CGetSetOptions::GetIdleSecondsBeforeDelete())
  573. {
  574. //delete any data items sitting out there that the main table data was deleted
  575. //this was done to speed up deleted from the main table
  576. deleteCount = db.execDMLEx(_T("DELETE FROM MainDeletes WHERE clipID=%d"), q.getIntField(_T("clipID")));
  577. }
  578. else
  579. {
  580. Log(StrF(_T("Computer has not been idle long enough to delete clips, Min Idle: %d, current Idle: %d"),
  581. CGetSetOptions::GetIdleSecondsBeforeDelete(), idleSeconds));
  582. break;
  583. }
  584. q.nextRow();
  585. }
  586. toDeleteCount = db.execScalar(_T("SELECT COUNT(clipID) FROM MainDeletes"));
  587. Log(StrF(_T("After Deleting emptied out data rows, Count: %d, toDelete: %d"), deleteCount, toDeleteCount));
  588. }
  589. CATCH_SQLITE_EXCEPTION
  590. Log(_T("End of RemoveOldEntries"));
  591. return TRUE;
  592. }
  593. BOOL EnsureDirectory(CString csPath)
  594. {
  595. TCHAR drive[_MAX_DRIVE];
  596. TCHAR dir[_MAX_DIR];
  597. TCHAR fname[_MAX_FNAME];
  598. TCHAR ext[_MAX_EXT];
  599. SPLITPATH(csPath, drive, dir, fname, ext);
  600. CString csDir(drive);
  601. csDir += dir;
  602. if(FileExists(csDir) == FALSE)
  603. {
  604. if(CreateDirectory(csDir, NULL))
  605. return TRUE;
  606. }
  607. else
  608. return TRUE;
  609. return FALSE;
  610. }
  611. // BOOL RunZippApp(CString csCommandLine)
  612. // {
  613. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  614. // FIX_CSTRING_PATH(csLocalPath);
  615. //
  616. // CString csZippApp = GETENV(_T("U3_DEVICE_EXEC_PATH"));
  617. // FIX_CSTRING_PATH(csZippApp);
  618. // csZippApp += "7za.exe";
  619. //
  620. // csZippApp += " ";
  621. // csZippApp += csCommandLine;
  622. //
  623. // Log(csZippApp);
  624. //
  625. // STARTUPINFO StartupInfo;
  626. // PROCESS_INFORMATION ProcessInformation;
  627. //
  628. // ZeroMemory(&StartupInfo, sizeof(StartupInfo));
  629. // StartupInfo.cb = sizeof(StartupInfo);
  630. // ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
  631. //
  632. // StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  633. // StartupInfo.wShowWindow = SW_HIDE;
  634. //
  635. // BOOL bRet = CreateProcess(NULL, csZippApp.GetBuffer(csZippApp.GetLength()), NULL, NULL, FALSE,
  636. // CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, csLocalPath,
  637. // &StartupInfo, &ProcessInformation);
  638. //
  639. // if(bRet)
  640. // {
  641. // WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
  642. //
  643. // DWORD dwExitCode;
  644. // GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
  645. //
  646. // CString cs;
  647. // cs.Format(_T("Exit code from unzip = %d"), dwExitCode);
  648. // Log(cs);
  649. //
  650. // if(dwExitCode != 0)
  651. // {
  652. // bRet = FALSE;
  653. // }
  654. // }
  655. // else
  656. // {
  657. // bRet = FALSE;
  658. // Log(_T("Create Process Failed"));
  659. // }
  660. //
  661. // csZippApp.ReleaseBuffer();
  662. //
  663. // return bRet;
  664. // }
  665. // BOOL CopyDownDatabase()
  666. // {
  667. // BOOL bRet = FALSE;
  668. //
  669. // CString csZippedPath = GETENV(_T("U3_APP_DATA_PATH"));
  670. // FIX_CSTRING_PATH(csZippedPath);
  671. //
  672. // CString csUnZippedPath = csZippedPath;
  673. // csUnZippedPath += "Ditto.db";
  674. //
  675. // csZippedPath += "Ditto.7z";
  676. //
  677. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  678. // FIX_CSTRING_PATH(csLocalPath);
  679. //
  680. // if(FileExists(csZippedPath))
  681. // {
  682. // CString csCommandLine;
  683. //
  684. // //e = extract
  685. // //surround command line arguments with quotes
  686. // //-aoa = overight files with extracted files
  687. //
  688. // csCommandLine += "e ";
  689. // csCommandLine += "\"";
  690. // csCommandLine += csZippedPath;
  691. // csCommandLine += "\"";
  692. // csCommandLine += " -o";
  693. // csCommandLine += "\"";
  694. // csCommandLine += csLocalPath;
  695. // csCommandLine += "\"";
  696. // csCommandLine += " -aoa";
  697. //
  698. // bRet = RunZippApp(csCommandLine);
  699. //
  700. // csLocalPath += "Ditto.db";
  701. // }
  702. // else if(FileExists(csUnZippedPath))
  703. // {
  704. // csLocalPath += "Ditto.db";
  705. // bRet = CopyFile(csUnZippedPath, csLocalPath, FALSE);
  706. // }
  707. //
  708. // if(FileExists(csLocalPath) == FALSE)
  709. // {
  710. // Log(_T("Failed to copy files from device zip file"));
  711. // }
  712. //
  713. // g_Opt.nLastDbWriteTime = GetLastWriteTime(csLocalPath);
  714. //
  715. // return bRet;
  716. // }
  717. //BOOL CopyUpDatabase()
  718. //{
  719. // CStringA csZippedPath = "C:\\";//getenv("U3_APP_DATA_PATH");
  720. // FIX_CSTRING_PATH(csZippedPath);
  721. // csZippedPath += "Ditto.zip";
  722. // CStringA csLocalPath = GetDBName();//getenv("U3_HOST_EXEC_PATH");
  723. // //FIX_CSTRING_PATH(csLocalPath);
  724. // //csLocalPath += "Ditto.db";
  725. //
  726. // CZipper Zip;
  727. //
  728. // if(Zip.OpenZip(csZippedPath))
  729. // {
  730. // Zip.AddFileToZip(csLocalPath);
  731. // }
  732. //
  733. // return TRUE;
  734. //}