[pim/trojita] src/Plugins/AbookAddressbook: Refactor ensureAbookPath into several methods

Espen Sandøy Hustad <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 67abdb09e00d6c9381e993e325769ecb3511c319 by Espen Sandøy Hustad.
Committed on 03/08/2026 at 18:42.
Pushed by ehustad into branch 'master'.

Refactor ensureAbookPath into several methods

Split ensureAbookPath into 3 methods with names that
reflect what each method actually is doing.

M  +27   -17   src/Plugins/AbookAddressbook/AbookAddressbook.cpp
M  +3    -1    src/Plugins/AbookAddressbook/AbookAddressbook.h

https://invent.kde.org/pim/trojita/-/commit/67abdb09e00d6c9381e993e325769ecb3511c319

diff --git a/src/Plugins/AbookAddressbook/AbookAddressbook.cpp b/src/Plugins/AbookAddressbook/AbookAddressbook.cpp
index fa3eedfb6..61445370c 100644
--- a/src/Plugins/AbookAddressbook/AbookAddressbook.cpp
+++ b/src/Plugins/AbookAddressbook/AbookAddressbook.cpp
@@ -113,7 +113,9 @@ AbookAddressbook::AbookAddressbook(QObject *parent): AddressbookPlugin(parent),
 
     m_contacts = new QStandardItemModel(this);
 
-    ensureAbookPath();
+    createAbookDir();
+    updateConfigFile();
+    createAbookDBFile();
 
     // read abook
     readAbook(false);
@@ -170,37 +172,45 @@ void AbookAddressbook::remonitorAdressbook()
     m_filesystemWatcher->addPath(QDir::homePath() + QLatin1String("/.abook/addressbook"));
 }
 
-void AbookAddressbook::ensureAbookPath()
+void AbookAddressbook::createAbookDir()
 {
     if (!QDir::home().exists(QStringLiteral(".abook"))) {
         QDir::home().mkdir(QStringLiteral(".abook"));
     }
-    QDir abook(QDir::homePath() + QLatin1String("/.abook/"));
-    QStringList abookrc;
+}
+
+void AbookAddressbook::updateConfigFile() const
+{
     QFile file(QDir::homePath() + QLatin1String("/.abook/abookrc"));
-    if (file.exists() && file.open(QIODevice::ReadWrite|QIODevice::Text)) {
+    QStringList abookrc;
+    if (!file.exists()) {
+        abookrc << QStringLiteral("field photo = Photo") << QStringLiteral("set preserve_fields=all");
+    } else {
+        file.open(QIODevice::ReadOnly|QIODevice::Text);
         abookrc = QString::fromLocal8Bit(file.readAll()).split(QStringLiteral("\n"));
+
         bool havePhoto = false;
         for (QStringList::iterator it = abookrc.begin(), end = abookrc.end(); it != end; ++it) {
-            if (it->contains(QLatin1String("preserve_fields")))
+            if (it->contains(QLatin1String("preserve_fields"))) {
                 *it = QStringLiteral("set preserve_fields=all");
-            else if (it->contains(QLatin1String("photo")) && it->contains(QLatin1String("field")))
+            } else if (it->contains(QLatin1String("photo")) && it->contains(QLatin1String("field"))) {
                 havePhoto = true;
+            }
         }
-        if (!havePhoto)
+
+        if (!havePhoto) {
             abookrc << QStringLiteral("field photo = Photo");
-    } else {
-        abookrc << QStringLiteral("field photo = Photo") << QStringLiteral("set preserve_fields=all");
-        file.open(QIODevice::WriteOnly|QIODevice::Text);
-    }
-    if (file.isOpen()) {
-        if (file.isWritable()) {
-            file.seek(0);
-            file.write(abookrc.join(QStringLiteral("\n")).toLocal8Bit());
         }
         file.close();
     }
-    QFile abookFile(abook.filePath(QStringLiteral("addressbook")));
+
+    file.open(QIODevice::WriteOnly|QIODevice::Truncate);
+    file.write(abookrc.join(QStringLiteral("\n")).toLocal8Bit());
+}
+
+void AbookAddressbook::createAbookDBFile() const
+{
+    QFile abookFile(QDir::homePath() + QLatin1String("/.abook/addressbook"));
     if (!abookFile.exists()) {
         abookFile.open(QIODevice::WriteOnly);
     }
diff --git a/src/Plugins/AbookAddressbook/AbookAddressbook.h b/src/Plugins/AbookAddressbook/AbookAddressbook.h
index a605de660..d0cc412b9 100644
--- a/src/Plugins/AbookAddressbook/AbookAddressbook.h
+++ b/src/Plugins/AbookAddressbook/AbookAddressbook.h
@@ -72,7 +72,9 @@ private slots:
     void scheduleAbookUpdate();
 
 private:
-    void ensureAbookPath();
+    void createAbookDir();
+    void updateConfigFile() const;
+    void createAbookDBFile() const;
     void remonitorAdressbook();
 
     QFileSystemWatcher *m_filesystemWatcher;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.