File indexing completed on 2024-05-19 04:36:07

0001 /* SPDX-FileCopyrightText: 2023 Noah Davis <noahadvs@gmail.com>
0002  * SPDX-License-Identifier: LGPL-2.0-or-later
0003  */
0004 
0005 #pragma once
0006 
0007 #include <KConfigGroup>
0008 #include <QDateTime>
0009 #include <QFileInfo>
0010 #include <QStandardPaths>
0011 
0012 // automatically use this when including this header
0013 using namespace Qt::StringLiterals;
0014 using KeyMap = QMap<const char *, const char *>;
0015 using ValueMap = QMap<QString, QString>;
0016 
0017 inline void replaceEntryKeys(KConfigGroup &group, const KeyMap &oldNewMap)
0018 {
0019     if (!group.exists()) {
0020         return;
0021     }
0022     for (auto it = oldNewMap.cbegin(); it != oldNewMap.cend(); ++it) {
0023         if (!group.hasKey(it.key())) {
0024             continue;
0025         }
0026         // Only write if new key is not empty
0027         if (!QByteArrayLiteral(it.value()).isEmpty()) {
0028             group.writeEntry(it.value(), group.readEntry(it.key()));
0029         }
0030         group.deleteEntry(it.key());
0031     }
0032 };
0033 
0034 inline void replaceEntryValues(KConfigGroup &group, const char *key,
0035                                const ValueMap &oldNewMap)
0036 {
0037     if (!group.exists() || !group.hasKey(key)) {
0038         return;
0039     }
0040     for (auto it = oldNewMap.cbegin(); it != oldNewMap.cend(); ++it) {
0041         if (group.readEntry(key) != it.key()) {
0042             continue;
0043         }
0044         // Only write if new value is not empty
0045         if (!it.value().isEmpty()) {
0046             group.writeEntry(key, it.value());
0047         } else {
0048             // Delete if new value is empty because it'll be removed anyway.
0049             group.deleteEntry(key);
0050         }
0051     }
0052 };
0053 
0054 inline bool continueUpdate(const QString &fileName, const QString &isoDateTime = {})
0055 {
0056     const auto path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, fileName);
0057     // false if there is no existing user config.
0058     if (path.isEmpty() || !path.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation))) {
0059         return false;
0060     }
0061 
0062     // true if we aren't doing a datetime check
0063     if (isoDateTime.isEmpty()) {
0064         return true;
0065     }
0066 
0067     // false if the existing config is newer than the threshold datetime.
0068     QFileInfo fileInfo(path);
0069     auto configDateTime = fileInfo.birthTime();
0070     auto thresholdDateTime = QDateTime::fromString(isoDateTime, Qt::ISODate);
0071     if (!configDateTime.isValid() || !thresholdDateTime.isValid()
0072         || configDateTime > thresholdDateTime) {
0073         return false;
0074     }
0075 
0076     return true;
0077 }
0078 
0079 inline bool isEntryDefault(KConfigGroup &group, const char *key)
0080 {
0081     return !group.exists() || group.readEntry(key).isEmpty();
0082 }