Skip to content

Commit bb4f73c

Browse files
committed
#3396 checksum: refactor openmode handling
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 35fb8e3 commit bb4f73c

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/entities/note.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,11 +1499,7 @@ bool Note::storeNoteTextFileToDisk(bool &currentNoteTextChanged,
14991499
// Read the current file content using the same flags as when writing
15001500
// to ensure newline handling is consistent (important on Windows)
15011501
QFile checkFile(fullNoteFilePath());
1502-
QFile::OpenMode checkFlags = QIODevice::ReadOnly;
1503-
if (!useUNIXNewline) {
1504-
checkFlags |= QIODevice::Text;
1505-
}
1506-
if (checkFile.open(checkFlags)) {
1502+
if (checkFile.open(Utils::Misc::getNoteFileOpenFlags())) {
15071503
QTextStream checkIn(&checkFile);
15081504
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
15091505
checkIn.setCodec("UTF-8");
@@ -1949,14 +1945,7 @@ bool Note::updateNoteTextFromDisk() {
19491945
QFile file(fullNoteFilePath());
19501946

19511947
// Use the same flags as when writing to ensure consistent newline handling
1952-
const SettingsService settings;
1953-
const bool useUNIXNewline = settings.value(QStringLiteral("useUNIXNewline")).toBool();
1954-
QFile::OpenMode flags = QIODevice::ReadOnly;
1955-
if (!useUNIXNewline) {
1956-
flags |= QIODevice::Text;
1957-
}
1958-
1959-
if (!file.open(flags)) {
1948+
if (!file.open(Utils::Misc::getNoteFileOpenFlags())) {
19601949
qDebug() << __func__ << " - 'file': " << file.fileName();
19611950
qDebug() << __func__ << " - " << file.errorString();
19621951
return false;
@@ -2010,7 +1999,7 @@ bool Note::updateNoteTextFromDisk() {
20101999
}
20112000

20122001
// Calculate and store the checksum of the loaded text (if enabled)
2013-
// settings was already retrieved at the beginning of the function
2002+
const SettingsService settings;
20142003
const bool enableNoteChecksumChecks =
20152004
settings.value(QStringLiteral("enableNoteChecksumChecks"), false).toBool();
20162005
if (enableNoteChecksumChecks) {
@@ -2341,14 +2330,7 @@ QString Note::detectNewlineCharacters() {
23412330

23422331
void Note::createFromFile(QFile &file, int noteSubFolderId, bool withNoteNameHook) {
23432332
// Use the same flags as when writing to ensure consistent newline handling
2344-
const SettingsService settings;
2345-
const bool useUNIXNewline = settings.value(QStringLiteral("useUNIXNewline")).toBool();
2346-
QFile::OpenMode flags = QIODevice::ReadOnly;
2347-
if (!useUNIXNewline) {
2348-
flags |= QIODevice::Text;
2349-
}
2350-
2351-
if (file.open(flags)) {
2333+
if (file.open(Utils::Misc::getNoteFileOpenFlags())) {
23522334
QFileInfo fileInfo;
23532335
fileInfo.setFile(file);
23542336

@@ -2396,7 +2378,7 @@ void Note::createFromFile(QFile &file, int noteSubFolderId, bool withNoteNameHoo
23962378
this->_fileLastModified = fileInfo.lastModified();
23972379

23982380
// Calculate the checksum before storing (if enabled)
2399-
// settings was already retrieved above
2381+
const SettingsService settings;
24002382
const bool enableNoteChecksumChecks =
24012383
settings.value(QStringLiteral("enableNoteChecksumChecks"), false).toBool();
24022384
if (enableNoteChecksumChecks) {

src/utils/misc.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,3 +2829,22 @@ QString Utils::Misc::jsValueToJsonString(const QJSValue &value) {
28292829
QJsonDocument doc = QJsonDocument::fromVariant(variant);
28302830
return doc.toJson(QJsonDocument::Indented);
28312831
}
2832+
2833+
/**
2834+
* Get the correct file open flags for reading note files
2835+
* This ensures consistent newline handling across platforms, especially on Windows
2836+
*
2837+
* @param baseFlags - The base open mode flags (default: QIODevice::ReadOnly)
2838+
* @return The complete open mode flags with Text flag added if useUNIXNewline is disabled
2839+
*/
2840+
QFile::OpenMode Utils::Misc::getNoteFileOpenFlags(QFile::OpenMode baseFlags) {
2841+
const SettingsService settings;
2842+
const bool useUNIXNewline = settings.value(QStringLiteral("useUNIXNewline")).toBool();
2843+
2844+
QFile::OpenMode flags = baseFlags;
2845+
if (!useUNIXNewline) {
2846+
flags |= QIODevice::Text;
2847+
}
2848+
2849+
return flags;
2850+
}

src/utils/misc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
#pragma once
1616

17+
#include <QFile>
1718
#include <QHash>
19+
#include <QIODevice>
1820
#include <QMetaType>
1921
#include <QString>
2022
#include <QStringList>
2123
#include <QVector>
2224

2325
struct TerminalCmd;
24-
class QFile;
2526
class QDataStream;
2627
class QPrinter;
2728
class QJSValue;
@@ -159,6 +160,7 @@ QByteArray friendlyUserAgentString();
159160
QLatin1String platform();
160161
void switchToDarkOrLightMode(bool darkMode);
161162
void switchToDarkMode();
163+
QFile::OpenMode getNoteFileOpenFlags(QFile::OpenMode baseFlags = QIODevice::ReadOnly);
162164
void switchToLightMode();
163165
void unescapeEvernoteImportText(QString &content);
164166
void transformEvernoteImportText(QString &content, bool withCleanup = false);

0 commit comments

Comments
 (0)