DatabaseUtilities.cpp 20 KB

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