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 "ProfileWriter.h"
0011 
0012 // Qt
0013 
0014 // KDE
0015 #include <KConfig>
0016 #include <KConfigGroup>
0017 
0018 // Konsole
0019 #include "ShellCommand.h"
0020 
0021 using namespace Konsole;
0022 
0023 // FIXME: A dup line from Profile.cpp - redo these
0024 static const char GENERAL_GROUP[] = "General";
0025 
0026 ProfileWriter::ProfileWriter() = default;
0027 ProfileWriter::~ProfileWriter() = default;
0028 
0029 // All profiles changes are stored under users' local account
0030 QString ProfileWriter::getPath(const Profile::Ptr &profile)
0031 {
0032     // If any changes are made to this location, check that programs using
0033     // the Konsole part can write/save profiles
0034     static const QString localDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konsole");
0035 
0036     return localDataLocation + QLatin1Char('/') + profile->untranslatedName() + QLatin1String(".profile");
0037 }
0038 
0039 void ProfileWriter::writeProperties(KConfig &config, const Profile::Ptr &profile)
0040 {
0041     const char *groupName = nullptr;
0042     KConfigGroup group;
0043 
0044     for (const Profile::PropertyInfo &info : Profile::DefaultProperties) {
0045         if (info.group != nullptr) {
0046             if (groupName == nullptr || qstrcmp(groupName, info.group) != 0) {
0047                 group = config.group(QLatin1String(info.group));
0048                 groupName = info.group;
0049             }
0050 
0051             if (profile->isPropertySet(info.property)) {
0052                 group.writeEntry(QLatin1String(info.name), profile->property<QVariant>(info.property));
0053             }
0054         }
0055     }
0056 }
0057 
0058 bool ProfileWriter::writeProfile(const QString &path, const Profile::Ptr &profile)
0059 {
0060     KConfig config(path, KConfig::NoGlobals);
0061 
0062     if (!config.isConfigWritable(false)) {
0063         return false;
0064     }
0065 
0066     KConfigGroup general = config.group(QLatin1String(GENERAL_GROUP));
0067 
0068     // Parent profile if set, when loading the profile in future, the parent
0069     // must be loaded as well if it exists.
0070     if (profile->parent()) {
0071         general.writeEntry("Parent", profile->parent()->path());
0072     }
0073 
0074     if (profile->isPropertySet(Profile::Command) || profile->isPropertySet(Profile::Arguments)) {
0075         general.writeEntry("Command", ShellCommand(profile->command(), profile->arguments()).fullCommand());
0076     }
0077 
0078     // Write remaining properties
0079     writeProperties(config, profile);
0080 
0081     return true;
0082 }