Skip to content

Commit

Permalink
Automatically remove duplicate conflicted notes.sqlite database files (
Browse files Browse the repository at this point in the history
  • Loading branch information
pbek committed Feb 13, 2020
1 parent ff7d065 commit 3d08822
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# QOwnNotes Changelog

## 20.2.4
- conflicted copies of the note folder database `notes.sqlite` are now
immediately deleted if they are not different to the current `notes.sqlite`
(for [#1625](https://github.com/pbek/QOwnNotes/issues/1625))

## 20.2.3
- improved Setext-style headers highlighting with leading spaces
(for [#101](https://github.com/pbek/qmarkdowntextedit/issues/101), thank you @Waqar144)
Expand Down
27 changes: 20 additions & 7 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3251,9 +3251,28 @@ void MainWindow::removeConflictedNotesDatabaseCopies() {
QDirIterator it(NoteFolder::currentLocalPath(), filter,
QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot);
auto files = QStringList();
const QSignalBlocker blocker(this->noteDirectoryWatcher);
Q_UNUSED(blocker)

// we try to fix problems with the blocker
directoryWatcherWorkaround(true);

while (it.hasNext()) {
files << it.next();
const QString &file = it.next();

// check if conflicted database copy is the same as the current note
// folder database
if (Utils::Misc::isSameFile(
file, DatabaseService::getNoteFolderDatabasePath())) {
showStatusBarMessage(
tr(QFile::remove(file)
? "Removed duplicate conflicted database: %1"
: "Could not remove duplicate conflicted database: %1")
.arg(file),
4000);
} else {
files << file;
}
}

int count = files.count();
Expand All @@ -3276,12 +3295,6 @@ void MainWindow::removeConflictedNotesDatabaseCopies() {
return;
}

const QSignalBlocker blocker(this->noteDirectoryWatcher);
Q_UNUSED(blocker)

// we try to fix problems with the blocker
directoryWatcherWorkaround(true);

count = 0;

// remove the database files
Expand Down
25 changes: 25 additions & 0 deletions src/services/databaseservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,28 @@ QString DatabaseService::getAppData(const QString& name,

return QString();
}

/**
* WIP
*
* @param path
* @return
*/
bool DatabaseService::mergeNoteFolderDatabase(QString path) {
QSqlDatabase mergeDB = QSqlDatabase::addDatabase(
QStringLiteral("QSQLITE"), QStringLiteral("note_folder_merge"));
mergeDB.setDatabaseName(path);

if (!mergeDB.open()) {
QMessageBox::critical(
nullptr, QWidget::tr("Cannot open database"),
QWidget::tr("Unable to establish a database connection with "
"note folder database to merge '%1'.\nAre the folder "
"and the file writeable?")
.arg(path),
QMessageBox::Ok);
return false;
}

return true;
}
3 changes: 2 additions & 1 deletion src/services/databaseservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class DatabaseService {
static QSqlDatabase getNoteFolderDatabase();
static void closeDatabaseConnection(const QSqlDatabase& db,
QSqlQuery& query);
static QString getNoteFolderDatabasePath();

private:
static QString getNoteFolderDatabasePath();
static bool createMemoryConnection();
static bool createDiskConnection();
static bool mergeNoteFolderDatabase(QString path);
};

#endif // DATABASESERVICE_H
28 changes: 28 additions & 0 deletions src/utils/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1939,3 +1939,31 @@ QString Utils::Misc::localDictionariesPath() {
dir.mkpath(path);
return path;
}

/**
* Generates a SHA1 signature for a file
*
* @return
*/
QByteArray Utils::Misc::generateFileSha1Signature(const QString &path) {
QCryptographicHash hash(QCryptographicHash::Sha1);
QFile file(path);

if (file.open(QIODevice::ReadOnly)) {
hash.addData(file.readAll());
} else {
return QByteArray();
}

// retrieve the SHA1 signature of the file
return hash.result();
}

/**
* Checks if two files are the same
*
* @return
*/
bool Utils::Misc::isSameFile(const QString &path1, const QString &path2) {
return generateFileSha1Signature(path1) == generateFileSha1Signature(path2);
}
2 changes: 2 additions & 0 deletions src/utils/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ QString remotePreviewImageTagToInlineImageTag(QString imageTag,
int &imageWidth);
QString createUuidString();
QString localDictionariesPath();
QByteArray generateFileSha1Signature(const QString &path);
bool isSameFile(const QString &path1, const QString &path2);
} // namespace Misc
} // namespace Utils

Expand Down

0 comments on commit 3d08822

Please sign in to comment.