Skip to content

Commit 6ec047a

Browse files
committed
Automatically refresh read-only databases on modified database file
This adds a new setting in "Preferences > Database" (defaulting to false) to watch the database file when opened in read-only mode and refresh the browser on updated file. See issues #1767 and #2508.
1 parent 222e430 commit 6ec047a

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/MainWindow.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,11 @@ bool MainWindow::fileOpen(const QString& fileName, bool openFromProject, bool re
583583
loadPragmas();
584584

585585
refreshTableBrowsers();
586-
586+
if (db.readOnly()) {
587+
if (!fileSystemWatch.files().isEmpty())
588+
fileSystemWatch.removePaths(fileSystemWatch.files());
589+
fileSystemWatch.addPath(wFile);
590+
}
587591
retval = true;
588592
} else {
589593
QMessageBox::warning(this, qApp->applicationName(), tr("Could not open database file.\nReason: %1").arg(db.lastError()));
@@ -700,6 +704,10 @@ void MainWindow::refreshTableBrowsers(bool all)
700704
QApplication::restoreOverrideCursor();
701705
}
702706

707+
void MainWindow::refreshDb() {
708+
refreshTableBrowsers();
709+
}
710+
703711
bool MainWindow::fileSaveAs() {
704712

705713
QString fileName = FileDialog::getSaveFileName(
@@ -743,6 +751,8 @@ bool MainWindow::fileClose()
743751
if(!db.close())
744752
return false;
745753

754+
if (!fileSystemWatch.files().isEmpty())
755+
fileSystemWatch.removePaths(fileSystemWatch.files());
746756
TableBrowser::resetSharedSettings();
747757
setCurrentFile(QString());
748758
loadPragmas();
@@ -2462,6 +2472,14 @@ void MainWindow::reloadSettings()
24622472

24632473
ui->actionDropQualifiedCheck->setChecked(Settings::getValue("SchemaDock", "dropQualifiedNames").toBool());
24642474
ui->actionEnquoteNamesCheck->setChecked(Settings::getValue("SchemaDock", "dropEnquotedNames").toBool());
2475+
2476+
if (Settings::getValue("db", "watcher").toBool())
2477+
connect(&fileSystemWatch, &QFileSystemWatcher::fileChanged, this, &MainWindow::refreshDb, Qt::UniqueConnection);
2478+
else {
2479+
disconnect(&fileSystemWatch, &QFileSystemWatcher::fileChanged, nullptr, nullptr);
2480+
if (!fileSystemWatch.files().isEmpty())
2481+
fileSystemWatch.removePaths(fileSystemWatch.files());
2482+
}
24652483
}
24662484

24672485
void MainWindow::checkNewVersion(const bool automatic)

src/MainWindow.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include <memory>
88
#include <vector>
9+
910
#include <QMainWindow>
11+
#include <QFileSystemWatcher>
1012

1113
struct BrowseDataTableSettings;
1214
class DbStructureModel;
@@ -121,6 +123,8 @@ friend TableBrowserDock;
121123
QString currentProjectFilename;
122124
bool isProjectModified;
123125

126+
QFileSystemWatcher fileSystemWatch;
127+
124128
void init();
125129
void clearCompleterModelsFields();
126130

@@ -176,6 +180,7 @@ private slots:
176180
void fileNewInMemoryDatabase(bool open_create_dialog = true);
177181
// Refresh visible table browsers. When all is true, refresh all browsers.
178182
void refreshTableBrowsers(bool all = false);
183+
void refreshDb();
179184
bool fileClose();
180185
bool fileSaveAs();
181186
void createTable();

src/PreferencesDialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void PreferencesDialog::loadSettings()
9999
}
100100

101101
ui->spinStructureFontSize->setValue(Settings::getValue("db", "fontsize").toInt());
102+
ui->watcherCheckBox->setChecked(Settings::getValue("db", "watcher").toBool());
102103

103104
// Gracefully handle the preferred Data Browser font not being available
104105
int matchingFont = ui->comboDataBrowserFont->findText(Settings::getValue("databrowser", "font").toString(), Qt::MatchExactly);
@@ -197,6 +198,7 @@ void PreferencesDialog::saveSettings(bool accept)
197198
Settings::setValue("db", "defaultsqltext", ui->editDatabaseDefaultSqlText->text());
198199
Settings::setValue("db", "defaultfieldtype", ui->defaultFieldTypeComboBox->currentIndex());
199200
Settings::setValue("db", "fontsize", ui->spinStructureFontSize->value());
201+
Settings::setValue("db", "watcher", ui->watcherCheckBox->isChecked());
200202

201203
Settings::setValue("checkversion", "enabled", ui->checkUpdates->isChecked());
202204

src/PreferencesDialog.ui

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,29 @@ in new project file</string>
739739
<item row="5" column="1">
740740
<widget class="QSpinBox" name="spinStructureFontSize"/>
741741
</item>
742+
<item row="6" column="0">
743+
<widget class="QLabel" name="label_5">
744+
<property name="toolTip">
745+
<string>When the database is open in readonly mode, watch the database file and refresh when it is updated.</string>
746+
</property>
747+
<property name="text">
748+
<string>&amp;When readonly, refresh on changed file</string>
749+
</property>
750+
<property name="buddy">
751+
<cstring>watcherCheckBox</cstring>
752+
</property>
753+
</widget>
754+
</item>
755+
<item row="6" column="1">
756+
<widget class="QCheckBox" name="watcherCheckBox">
757+
<property name="text">
758+
<string>enabled</string>
759+
</property>
760+
<property name="checked">
761+
<bool>true</bool>
762+
</property>
763+
</widget>
764+
</item>
742765
</layout>
743766
</item>
744767
<item>

src/Settings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ QVariant Settings::getDefaultValue(const std::string& group, const std::string&
159159
if(group == "db" && name == "fontsize")
160160
return 10;
161161

162+
// db/watcher?
163+
if(group == "db" && name == "watcher")
164+
return false;
165+
162166
// exportcsv/firstrowheader?
163167
if(group == "exportcsv" && name == "firstrowheader")
164168
return true;

0 commit comments

Comments
 (0)