Skip to content

Commit

Permalink
feat(cache): user can now select custom cache location and name
Browse files Browse the repository at this point in the history
  • Loading branch information
theophanemayaud committed Apr 10, 2022
1 parent b6a62ff commit e361b5d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 43 deletions.
31 changes: 26 additions & 5 deletions QtProject/app/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ bool Db::initDbAndCacheLocation(Prefs *prefs){
QMessageBox::about(prefs->_mainwPtr,
"Default cache location not accessible",
"The default cache location was not writable, please manually select a location for the cache file.");

dbfilename = QFileDialog::getSaveFileName(prefs->_mainwPtr, "Save/Load Cache",
cacheFolder,
"Cache (*.db)");
dbfilename = getUserSelectedCacheNamePath(prefs);
db.setDatabaseName(dbfilename);
if(!db.open()){
return false;
Expand All @@ -54,6 +51,24 @@ bool Db::initDbAndCacheLocation(Prefs *prefs){
return true;
}

bool Db::initCustomDbAndCacheLocation(Prefs *prefs){
const QString uniqueConnexionName = QUuid::createUuid().toString(); // each instance of Db must connect separately, uniquely
{
QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), uniqueConnexionName);
QString dbfilename = getUserSelectedCacheNamePath(prefs);
db.setDatabaseName(dbfilename);
if(!db.open()){
return false;
}
prefs->cacheFilePathName = dbfilename;
createTables(db, prefs->appVersion);
db.close();
}

QSqlDatabase().removeDatabase(uniqueConnexionName); // clear the connexion backlog, basically... !
return true;
}

void Db::emptyAllDb(const Prefs prefs){
if(prefs.cacheFilePathName.isEmpty()){
qDebug() << "Database path not set, can't empty cache.";
Expand Down Expand Up @@ -93,7 +108,6 @@ QString Db::pathnameHashId(const QString &filename)
// identify, that doesn't depend on path, to have file moving proofness !
return QCryptographicHash::hash(filename.toLatin1(), QCryptographicHash::Md5).toHex();
}

// -------------------- END : public static functions -------------------
// ----------------------------------------------------------------------

Expand All @@ -120,6 +134,13 @@ void Db::createTables(QSqlDatabase db, const QString appVersion)
query.exec(QStringLiteral("INSERT OR REPLACE INTO version VALUES('%1');").arg(appVersion));
}

QString Db::getUserSelectedCacheNamePath(Prefs *prefs){
// attempt with user specified location
QString dbfilename = QFileDialog::getSaveFileName(prefs->_mainwPtr, "Save/Load Cache",
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
"Cache (*.db)");
return dbfilename;
}
// -------------------- END : private static functions ------------------
// ----------------------------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions QtProject/app/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class Db
QSqlDatabase _db;
QString _uniqueConnexionName;
static void createTables(QSqlDatabase db, const QString appVersion);
static QString getUserSelectedCacheNamePath(Prefs *prefs);

public:
static bool initDbAndCacheLocation(Prefs *prefs);

static bool initCustomDbAndCacheLocation(Prefs *prefs);

static void emptyAllDb(const Prefs prefs);

//return md5 hash of parameter's file, used internally as "unique id" for each file
Expand Down
24 changes: 24 additions & 0 deletions QtProject/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,30 @@ void MainWindow::on_actionEmpty_cache_triggered()
Db::emptyAllDb(_prefs);
}

void MainWindow::on_actionSet_custom_cache_location_triggered()
{
if(Db::initCustomDbAndCacheLocation(&_prefs)){
ui->actionSet_custom_cache_location->setEnabled(false);
ui->actionRestore_default_cache_location->setEnabled(true);
addStatusMessage(QString("\nCache now used: %1\n").arg(_prefs.cacheFilePathName));
}
else
addStatusMessage(QString("\nError selecting custom cache.\n"));
}

void MainWindow::on_actionRestore_default_cache_location_triggered()
{
_prefs.cacheFilePathName = "";
if(Db::initDbAndCacheLocation(&_prefs)){
addStatusMessage("\nCache restored to: " + _prefs.cacheFilePathName + "\n");
}
else
addStatusMessage(QString("\nError restoring default cache. Probably no cache now.\n"));

ui->actionSet_custom_cache_location->setEnabled(true);
ui->actionRestore_default_cache_location->setEnabled(false);
}

void MainWindow::on_actionCredits_triggered()
{
QFile file(":/CREDITS.md");
Expand Down
2 changes: 2 additions & 0 deletions QtProject/app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ private slots:
void on_actionContact_triggered();
void on_actionChange_trash_folder_triggered();
void on_actionRestoreMoveToTrash_triggered();
void on_actionSet_custom_cache_location_triggered();
void on_actionRestore_default_cache_location_triggered();
};

#endif // MAINWINDOW_H
26 changes: 23 additions & 3 deletions QtProject/app/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>677</width>
<width>685</width>
<height>642</height>
</rect>
</property>
Expand Down Expand Up @@ -467,8 +467,8 @@ Errors will be listed in any case.</string>
<rect>
<x>0</x>
<y>0</y>
<width>677</width>
<height>24</height>
<width>685</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuGeneral">
Expand All @@ -482,8 +482,12 @@ Errors will be listed in any case.</string>
<string>Tools</string>
</property>
<addaction name="actionEmpty_cache"/>
<addaction name="actionSet_custom_cache_location"/>
<addaction name="actionRestore_default_cache_location"/>
<addaction name="separator"/>
<addaction name="actionChange_trash_folder"/>
<addaction name="actionRestoreMoveToTrash"/>
<addaction name="separator"/>
</widget>
<widget class="QMenu" name="menuInfo">
<property name="title">
Expand Down Expand Up @@ -511,6 +515,9 @@ Errors will be listed in any case.</string>
</property>
</action>
<action name="actionEmpty_cache">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Empty cache</string>
</property>
Expand Down Expand Up @@ -538,6 +545,19 @@ Errors will be listed in any case.</string>
<string>Restore move to trash behavior</string>
</property>
</action>
<action name="actionSet_custom_cache_location">
<property name="text">
<string>Set custom cache location</string>
</property>
</action>
<action name="actionRestore_default_cache_location">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Restore default cache location</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
Expand Down
47 changes: 12 additions & 35 deletions QtProject/video-simili-duplicate-cleaner_macos.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.1, 2022-01-13T16:56:20. -->
<!-- Written by QtCreator 6.0.2, 2022-04-10T18:12:38. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -67,27 +67,7 @@
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<valuemap type="QVariantMap" key="AutoTest.CheckStates">
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_mainwindow/tst_mainwindow.cpp:TestMainWindow">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_mainwindow/tst_mainwindow.cpp:cleanupTestCase">Unchecked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_mainwindow/tst_mainwindow.cpp:initTestCase">Unchecked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_mainwindow/tst_mainwindow.cpp:test_case1">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:TestVideo">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:cleanupTestCase">Unchecked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:emptyDb">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:initTestCase">Unchecked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBcheckRefVidParams">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBcheckRefVidParams_cachedNoThumbsCheck">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBcheckRefVidParams_nocache">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBwholeApp">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBwholeApp_cached">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_100GBwholeApp_nocache">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_check_refvidparams_cached">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_check_refvidparams_nocache">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_whole_app">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_whole_app_cached">Checked</value>
<value type="Qt::CheckState" key="1@/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/tests/test_video/tst_video.cpp:test_whole_app_nocache">Checked</value>
</valuemap>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
Expand All @@ -100,18 +80,18 @@
<value type="int" key="ClangTools.ParallelJobs">4</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles">
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/ssim.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/comparison.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/mainwindow.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/thumbnail.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/prefs.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/video.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/comparison.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/db.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/mainwindow.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/db.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/video.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/prefs.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/comparison.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/main.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/thumbnail.h</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/comparison.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/ssim.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/mainwindow.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/video.cpp</value>
<value type="QString">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/app/main.cpp</value>
</valuelist>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
Expand Down Expand Up @@ -294,8 +274,8 @@
<variable>ProjectExplorer.Project.Target.1</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.15.2 clang 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.15.2 clang 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Replacement for &quot;Desktop Qt 5.15.2 clang 64bit&quot;</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Replacement for &quot;Desktop Qt 5.15.2 clang 64bit&quot;</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5152.clang_64_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
Expand Down Expand Up @@ -443,7 +423,6 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/builds/build-video-simili-duplicate-cleaner-Desktop Qt 5.15.2 clang 64bit-Debug/tests/test_video/Video simili duplicate cleaner.app/Contents/MacOS</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.1">
<value type="QString" key="Analyzer.Perf.CallgraphMode">dwarf</value>
Expand All @@ -464,7 +443,6 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/builds/build-video-simili-duplicate-cleaner-Desktop Qt 5.15.2 clang 64bit-Debug/tests/test_mainwindow/test_mainwindow.app/Contents/MacOS</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.2">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
Expand All @@ -481,7 +459,6 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/Users/theophanemayaud/Dev/Programming videos dupplicates/video-simili-duplicate-cleaner/QtProject/builds/build-video-simili-duplicate-cleaner-Desktop Qt 5.15.2 clang 64bit-Debug/app/Video simili duplicate cleaner.app/Contents/MacOS</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">3</value>
</valuemap>
Expand Down

0 comments on commit e361b5d

Please sign in to comment.