File indexing completed on 2024-04-28 05:50:45

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "ProfileReader.h"
0011 
0012 // Qt
0013 #include <QDir>
0014 #include <QFile>
0015 
0016 // KDE
0017 #include <KConfig>
0018 #include <KConfigGroup>
0019 
0020 // Konsole
0021 #include "ShellCommand.h"
0022 
0023 using namespace Konsole;
0024 
0025 // FIXME: A dup line from Profile.cpp - redo these
0026 static const char GENERAL_GROUP[] = "General";
0027 static const char FEATURES_GROUP[] = "Terminal Features";
0028 static const char URLHINTS_KEY[] = "EnableUrlHints";
0029 static const char URLHINTSMODIFIERS_KEY[] = "UrlHintsModifiers";
0030 
0031 ProfileReader::ProfileReader() = default;
0032 ProfileReader::~ProfileReader() = default;
0033 
0034 QStringList ProfileReader::findProfiles()
0035 {
0036     QStringList profiles;
0037     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("konsole"), QStandardPaths::LocateDirectory);
0038     profiles.reserve(dirs.size());
0039 
0040     for (const QString &dir : dirs) {
0041         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.profile"));
0042         for (const QString &file : fileNames) {
0043             profiles.append(dir + QLatin1Char('/') + file);
0044         }
0045     }
0046     return profiles;
0047 }
0048 
0049 void ProfileReader::readProperties(const KConfig &config, Profile::Ptr profile)
0050 {
0051     const char *groupName = nullptr;
0052     KConfigGroup group;
0053     for (const Profile::PropertyInfo &info : Profile::DefaultProperties) {
0054         if (info.group == nullptr) {
0055             continue;
0056         }
0057         if (groupName == nullptr || qstrcmp(groupName, info.group) != 0) {
0058             group = config.group(QLatin1String(info.group));
0059             groupName = info.group;
0060         }
0061 
0062         const QString name(QLatin1String(info.name));
0063 
0064         if (group.hasKey(name)) {
0065             profile->setProperty(info.property, group.readEntry(name, QVariant(info.defaultValue.metaType())));
0066         }
0067     }
0068 }
0069 
0070 bool ProfileReader::readProfile(const QString &path, Profile::Ptr profile, QString &parentProfile)
0071 {
0072     if (!QFile::exists(path)) {
0073         return false;
0074     }
0075 
0076     KConfig config(path, KConfig::NoGlobals);
0077 
0078     KConfigGroup general = config.group(QLatin1String(GENERAL_GROUP));
0079     if (general.hasKey("Parent")) {
0080         parentProfile = general.readEntry("Parent");
0081     }
0082 
0083     if (general.hasKey("Command")) {
0084         ShellCommand shellCommand(general.readEntry("Command"));
0085 
0086         profile->setProperty(Profile::Command, shellCommand.command());
0087         profile->setProperty(Profile::Arguments, shellCommand.arguments());
0088     }
0089 
0090     // Check if the user earlier had set the URL hints option, and in that case set the default
0091     // URL hints modifier to the earlier default.
0092     if (config.hasGroup(QLatin1String(FEATURES_GROUP))) {
0093         KConfigGroup features = config.group(QLatin1String(FEATURES_GROUP));
0094         if (features.hasKey(URLHINTS_KEY)) {
0095             bool enable = features.readEntry(URLHINTS_KEY, false);
0096             if (enable && !features.hasKey(URLHINTSMODIFIERS_KEY)) {
0097                 features.writeEntry(URLHINTSMODIFIERS_KEY, int(Qt::ControlModifier));
0098             }
0099             features.deleteEntry(URLHINTS_KEY);
0100         }
0101     }
0102 
0103     profile->setProperty(Profile::UntranslatedName, general.readEntryUntranslated("Name"));
0104 
0105     // Read remaining properties
0106     readProperties(config, profile);
0107 
0108     return true;
0109 }