DatabaseUtilities.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. }
  320. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  321. return TRUE;
  322. }
  323. BOOL BackupDB(CString dbPath, CString prefix)
  324. {
  325. CString backup = GetFilePath(dbPath);
  326. CInternetUpdate update;
  327. long runningVersion = update.GetRunningVersion();
  328. CString versionString = update.GetVersionString(runningVersion);
  329. backup += GetFileName(dbPath) += _T("_") + prefix + _T("_") + versionString;
  330. backup.Replace(_T(".db"), _T(""));
  331. backup.Replace(_T("."), _T("_"));
  332. CString temp = backup;
  333. temp += _T(".db");
  334. int i = 1;
  335. while(FileExists(temp))
  336. {
  337. temp.Format(_T("%s_%d.db"), backup, i);
  338. i++;
  339. }
  340. backup = temp;
  341. BOOL ret = CopyFile(dbPath, backup, TRUE);
  342. return ret;
  343. }
  344. BOOL CreateDB(CString csFile)
  345. {
  346. try
  347. {
  348. CppSQLite3DB db;
  349. db.open(csFile);
  350. db.execDML(_T("PRAGMA auto_vacuum = 1"));
  351. db.execDML(_T("CREATE TABLE Main(")
  352. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  353. _T("lDate INTEGER, ")
  354. _T("mText TEXT, ")
  355. _T("lShortCut INTEGER, ")
  356. _T("lDontAutoDelete INTEGER, ")
  357. _T("CRC INTEGER, ")
  358. _T("bIsGroup INTEGER, ")
  359. _T("lParentID INTEGER, ")
  360. _T("QuickPasteText TEXT, ")
  361. _T("clipOrder REAL, ")
  362. _T("clipGroupOrder REAL, ")
  363. _T("globalShortCut INTEGER, ")
  364. _T("lastPasteDate INTEGER);"));
  365. db.execDML(_T("CREATE TABLE Data(")
  366. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  367. _T("lParentID INTEGER, ")
  368. _T("strClipBoardFormat TEXT, ")
  369. _T("ooData BLOB);"));
  370. db.execDML(_T("CREATE TABLE Types(")
  371. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  372. _T("TypeText TEXT);"));
  373. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  374. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  375. db.execDML(_T("CREATE INDEX Main_ClipOrder on Main(clipOrder DESC)"));
  376. db.execDML(_T("CREATE INDEX Main_ClipGroupOrder on Main(clipGroupOrder DESC)"));
  377. db.execDML(_T("CREATE INDEX Main_ParentId on Main(lParentID DESC)"));
  378. db.execDML(_T("CREATE INDEX Main_IsGroup on Main(bIsGroup DESC)"));
  379. db.execDML(_T("CREATE INDEX Main_ShortCut on Main(lShortCut DESC)"));
  380. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  381. _T("BEGIN\n")
  382. _T("INSERT INTO MainDeletes VALUES(old.lID, datetime('now'));\n")
  383. _T("END\n"));
  384. db.execDML(_T("CREATE TABLE CopyBuffers(")
  385. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  386. _T("lClipID INTEGER, ")
  387. _T("lCopyBuffer INTEGER)"));
  388. db.execDML(_T("CREATE TABLE MainDeletes(")
  389. _T("clipID INTEGER,")
  390. _T("modifiedDate)"));
  391. db.execDML(_T("CREATE TRIGGER MainDeletes_delete_data_trigger BEFORE DELETE ON MainDeletes FOR EACH ROW\n")
  392. _T("BEGIN\n")
  393. _T("DELETE FROM CopyBuffers WHERE lClipID = old.clipID;\n")
  394. _T("DELETE FROM Data WHERE lParentID = old.clipID;\n")
  395. _T("END\n"));
  396. db.close();
  397. }
  398. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  399. return TRUE;
  400. }
  401. BOOL CompactDatabase()
  402. {
  403. // if(!theApp.CloseDB())
  404. // return FALSE;
  405. //
  406. // CString csDBName = GetDBName();
  407. // CString csTempDBName = csDBName;
  408. // csTempDBName.Replace(".mdb", "TempDBName.mdb");
  409. //
  410. // //Compact the database
  411. // try
  412. // {
  413. // CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
  414. // }
  415. // catch(CDaoException* e)
  416. // {
  417. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  418. // DeleteFile(csTempDBName);
  419. // e->Delete();
  420. // return FALSE;
  421. // }
  422. // catch(CMemoryException* e)
  423. // {
  424. // AfxMessageBox("Memory Exception");
  425. // DeleteFile(csTempDBName);
  426. // e->Delete();
  427. // return FALSE;
  428. // }
  429. //
  430. // //Since compacting the database creates a new db delete the old one and replace it
  431. // //with the compacted db
  432. // if(DeleteFile(csDBName))
  433. // {
  434. // try
  435. // {
  436. // CFile::Rename(csTempDBName, csDBName);
  437. // }
  438. // catch(CFileException *e)
  439. // {
  440. // e->ReportError();
  441. // e->Delete();
  442. // return FALSE;
  443. // }
  444. // }
  445. // else
  446. // AfxMessageBox("Error Compacting Database");
  447. return TRUE;
  448. }
  449. BOOL RepairDatabase()
  450. {
  451. // if(!theApp.CloseDB())
  452. // return FALSE;
  453. // try
  454. // {
  455. // CDaoWorkspace::RepairDatabase(GetDBName());
  456. // }
  457. // catch(CDaoException *e)
  458. // {
  459. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  460. // e->Delete();
  461. // return FALSE;
  462. // }
  463. return TRUE;
  464. }
  465. BOOL RemoveOldEntries(bool checkIdleTime)
  466. {
  467. Log(StrF(_T("Beginning of RemoveOldEntries MaxEntries: %d - Keep days: %d"), CGetSetOptions::GetMaxEntries(), CGetSetOptions::GetExpiredEntries()));
  468. try
  469. {
  470. CppSQLite3DB db;
  471. CString csDbPath = CGetSetOptions::GetDBPath();
  472. db.open(csDbPath);
  473. if(CGetSetOptions::GetCheckForMaxEntries())
  474. {
  475. long lMax = CGetSetOptions::GetMaxEntries();
  476. if(lMax >= 0)
  477. {
  478. CClipIDs IDs;
  479. int clipId;
  480. 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);
  481. while(q.eof() == false)
  482. {
  483. int shortcut = q.getIntField(_T("lShortCut"));
  484. int dontDelete = q.getIntField(_T("lDontAutoDelete"));
  485. int parentId = q.getIntField(_T("lParentID"));
  486. double stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  487. double stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  488. //Only delete entries that have no shortcut and don't have the flag set
  489. if(shortcut == 0 &&
  490. dontDelete == 0 &&
  491. parentId <= 0 &&
  492. stickyClipOrder == 0.0 &&
  493. stickyClipGroupOrder == 0.0)
  494. {
  495. clipId = q.getIntField(_T("lID"));
  496. IDs.Add(clipId);
  497. Log(StrF(_T("From MaxEntries - Deleting Id: %d"), clipId));
  498. }
  499. q.nextRow();
  500. }
  501. if(IDs.GetCount() > 0)
  502. {
  503. IDs.DeleteIDs(false, db);
  504. }
  505. }
  506. }
  507. if(CGetSetOptions::GetCheckForExpiredEntries())
  508. {
  509. long lExpire = CGetSetOptions::GetExpiredEntries();
  510. if(lExpire)
  511. {
  512. CTime now = CTime::GetCurrentTime();
  513. now -= CTimeSpan(lExpire, 0, 0, 0);
  514. CClipIDs IDs;
  515. CppSQLite3Query q = db.execQueryEx(_T("SELECT lID FROM Main ")
  516. _T("WHERE lastPasteDate < %d AND ")
  517. _T("bIsGroup = 0 AND lShortCut = 0 AND lParentID <= 0 AND lDontAutoDelete = 0 AND stickyClipOrder = 0 AND stickyClipGroupOrder = 0"), (int)now.GetTime());
  518. while(q.eof() == false)
  519. {
  520. IDs.Add(q.getIntField(_T("lID")));
  521. Log(StrF(_T("From Clips Expire - Deleting Id: %d"), q.getIntField(_T("lID"))));
  522. q.nextRow();
  523. }
  524. if(IDs.GetCount() > 0)
  525. {
  526. IDs.DeleteIDs(false, db);
  527. }
  528. }
  529. }
  530. int toDeleteCount = db.execScalar(_T("SELECT COUNT(clipID) FROM MainDeletes"));
  531. Log(StrF(_T("Before Deleting emptied out data, count: %d, Idle Seconds: %f"), toDeleteCount, IdleSeconds()));
  532. //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
  533. //to lock up
  534. CppSQLite3Query q = db.execQueryEx(_T("SELECT * FROM MainDeletes LIMIT %d"), CGetSetOptions::GetMainDeletesDeleteCount());
  535. int deleteCount = 0;
  536. while(q.eof() == false)
  537. {
  538. double idleSeconds = IdleSeconds();
  539. if(checkIdleTime == false || idleSeconds > CGetSetOptions::GetIdleSecondsBeforeDelete())
  540. {
  541. //delete any data items sitting out there that the main table data was deleted
  542. //this was done to speed up deleted from the main table
  543. deleteCount = db.execDMLEx(_T("DELETE FROM MainDeletes WHERE clipID=%d"), q.getIntField(_T("clipID")));
  544. }
  545. else
  546. {
  547. Log(StrF(_T("Computer has not been idle long enough to delete clips, Min Idle: %d, current Idle: %d"),
  548. CGetSetOptions::GetIdleSecondsBeforeDelete(), idleSeconds));
  549. break;
  550. }
  551. q.nextRow();
  552. }
  553. toDeleteCount = db.execScalar(_T("SELECT COUNT(clipID) FROM MainDeletes"));
  554. Log(StrF(_T("After Deleting emptied out data rows, Count: %d, toDelete: %d"), deleteCount, toDeleteCount));
  555. }
  556. CATCH_SQLITE_EXCEPTION
  557. Log(_T("End of RemoveOldEntries"));
  558. return TRUE;
  559. }
  560. BOOL EnsureDirectory(CString csPath)
  561. {
  562. TCHAR drive[_MAX_DRIVE];
  563. TCHAR dir[_MAX_DIR];
  564. TCHAR fname[_MAX_FNAME];
  565. TCHAR ext[_MAX_EXT];
  566. SPLITPATH(csPath, drive, dir, fname, ext);
  567. CString csDir(drive);
  568. csDir += dir;
  569. if(FileExists(csDir) == FALSE)
  570. {
  571. if(CreateDirectory(csDir, NULL))
  572. return TRUE;
  573. }
  574. else
  575. return TRUE;
  576. return FALSE;
  577. }
  578. // BOOL RunZippApp(CString csCommandLine)
  579. // {
  580. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  581. // FIX_CSTRING_PATH(csLocalPath);
  582. //
  583. // CString csZippApp = GETENV(_T("U3_DEVICE_EXEC_PATH"));
  584. // FIX_CSTRING_PATH(csZippApp);
  585. // csZippApp += "7za.exe";
  586. //
  587. // csZippApp += " ";
  588. // csZippApp += csCommandLine;
  589. //
  590. // Log(csZippApp);
  591. //
  592. // STARTUPINFO StartupInfo;
  593. // PROCESS_INFORMATION ProcessInformation;
  594. //
  595. // ZeroMemory(&StartupInfo, sizeof(StartupInfo));
  596. // StartupInfo.cb = sizeof(StartupInfo);
  597. // ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
  598. //
  599. // StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  600. // StartupInfo.wShowWindow = SW_HIDE;
  601. //
  602. // BOOL bRet = CreateProcess(NULL, csZippApp.GetBuffer(csZippApp.GetLength()), NULL, NULL, FALSE,
  603. // CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, csLocalPath,
  604. // &StartupInfo, &ProcessInformation);
  605. //
  606. // if(bRet)
  607. // {
  608. // WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
  609. //
  610. // DWORD dwExitCode;
  611. // GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
  612. //
  613. // CString cs;
  614. // cs.Format(_T("Exit code from unzip = %d"), dwExitCode);
  615. // Log(cs);
  616. //
  617. // if(dwExitCode != 0)
  618. // {
  619. // bRet = FALSE;
  620. // }
  621. // }
  622. // else
  623. // {
  624. // bRet = FALSE;
  625. // Log(_T("Create Process Failed"));
  626. // }
  627. //
  628. // csZippApp.ReleaseBuffer();
  629. //
  630. // return bRet;
  631. // }
  632. // BOOL CopyDownDatabase()
  633. // {
  634. // BOOL bRet = FALSE;
  635. //
  636. // CString csZippedPath = GETENV(_T("U3_APP_DATA_PATH"));
  637. // FIX_CSTRING_PATH(csZippedPath);
  638. //
  639. // CString csUnZippedPath = csZippedPath;
  640. // csUnZippedPath += "Ditto.db";
  641. //
  642. // csZippedPath += "Ditto.7z";
  643. //
  644. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  645. // FIX_CSTRING_PATH(csLocalPath);
  646. //
  647. // if(FileExists(csZippedPath))
  648. // {
  649. // CString csCommandLine;
  650. //
  651. // //e = extract
  652. // //surround command line arguments with quotes
  653. // //-aoa = overight files with extracted files
  654. //
  655. // csCommandLine += "e ";
  656. // csCommandLine += "\"";
  657. // csCommandLine += csZippedPath;
  658. // csCommandLine += "\"";
  659. // csCommandLine += " -o";
  660. // csCommandLine += "\"";
  661. // csCommandLine += csLocalPath;
  662. // csCommandLine += "\"";
  663. // csCommandLine += " -aoa";
  664. //
  665. // bRet = RunZippApp(csCommandLine);
  666. //
  667. // csLocalPath += "Ditto.db";
  668. // }
  669. // else if(FileExists(csUnZippedPath))
  670. // {
  671. // csLocalPath += "Ditto.db";
  672. // bRet = CopyFile(csUnZippedPath, csLocalPath, FALSE);
  673. // }
  674. //
  675. // if(FileExists(csLocalPath) == FALSE)
  676. // {
  677. // Log(_T("Failed to copy files from device zip file"));
  678. // }
  679. //
  680. // g_Opt.nLastDbWriteTime = GetLastWriteTime(csLocalPath);
  681. //
  682. // return bRet;
  683. // }
  684. //BOOL CopyUpDatabase()
  685. //{
  686. // CStringA csZippedPath = "C:\\";//getenv("U3_APP_DATA_PATH");
  687. // FIX_CSTRING_PATH(csZippedPath);
  688. // csZippedPath += "Ditto.zip";
  689. // CStringA csLocalPath = GetDBName();//getenv("U3_HOST_EXEC_PATH");
  690. // //FIX_CSTRING_PATH(csLocalPath);
  691. // //csLocalPath += "Ditto.db";
  692. //
  693. // CZipper Zip;
  694. //
  695. // if(Zip.OpenZip(csZippedPath))
  696. // {
  697. // Zip.AddFileToZip(csLocalPath);
  698. // }
  699. //
  700. // return TRUE;
  701. //}