From 4dae2895bb53d63cdd3d1a4b85f0aa67d5f9b435 Mon Sep 17 00:00:00 2001 From: Kurt Hindenburg Date: Fri, 16 Mar 2012 11:14:05 -0400 Subject: [PATCH] Separate the profile reader/writer to their own file --- src/CMakeLists.txt | 2 + src/Profile.cpp | 114 ----------------------------------------- src/Profile.h | 62 ---------------------- src/ProfileReader.cpp | 95 ++++++++++++++++++++++++++++++++++ src/ProfileReader.h | 65 +++++++++++++++++++++++ src/ProfileWriter.cpp | 99 +++++++++++++++++++++++++++++++++++ src/ProfileWriter.h | 62 ++++++++++++++++++++++ src/SessionManager.cpp | 2 + 8 files changed, 325 insertions(+), 176 deletions(-) create mode 100644 src/ProfileReader.cpp create mode 100644 src/ProfileReader.h create mode 100644 src/ProfileWriter.cpp create mode 100644 src/ProfileWriter.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bcfaeafc1..87ee788bd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -83,6 +83,8 @@ ${CMAKE_CURRENT_BINARY_DIR}/tests/CTestCustom.cmake) ProcessInfo.cpp Profile.cpp ProfileList.cpp + ProfileReader.cpp + ProfileWriter.cpp Pty.cpp RenameTabsDialog.cpp Screen.cpp diff --git a/src/Profile.cpp b/src/Profile.cpp index 9042644e4..7343a9f46 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -288,120 +288,6 @@ const QStringList Profile::propertiesInfoList() const return info; } -QString KDE4ProfileWriter::getPath(const Profile::Ptr info) -{ - QString newPath; - - if (info->isPropertySet(Profile::Path) && - info->path().startsWith(KGlobal::dirs()->saveLocation("data", "konsole/"))) { - newPath = info->path(); - } else { - // use the profile name + ".profile" and save it in $KDEHOME - newPath = KGlobal::dirs()->saveLocation("data", "konsole/") + info->name() + ".profile"; - } - - //kDebug() << "Saving profile under name: " << newPath; - - return newPath; -} -void KDE4ProfileWriter::writeProperties(KConfig& config, - const Profile::Ptr profile, - const Profile::PropertyInfo* properties) -{ - const char* groupName = 0; - KConfigGroup group; - - while (properties->name != 0) { - if (properties->group != 0) { - if (groupName == 0 || qstrcmp(groupName, properties->group) != 0) { - group = config.group(properties->group); - groupName = properties->group; - } - - if (profile->isPropertySet(properties->property)) - group.writeEntry(QString(properties->name), - profile->property(properties->property)); - } - - properties++; - } -} -bool KDE4ProfileWriter::writeProfile(const QString& path , const Profile::Ptr profile) -{ - KConfig config(path, KConfig::NoGlobals); - - KConfigGroup general = config.group(GENERAL_GROUP); - - // Parent profile if set, when loading the profile in future, the parent - // must be loaded as well if it exists. - if (profile->parent()) - general.writeEntry("Parent", profile->parent()->path()); - - if (profile->isPropertySet(Profile::Command) - || profile->isPropertySet(Profile::Arguments)) - general.writeEntry("Command", - ShellCommand(profile->command(), profile->arguments()).fullCommand()); - - // Write remaining properties - writeProperties(config, profile, Profile::DefaultPropertyNames); - - return true; -} - -QStringList KDE4ProfileReader::findProfiles() -{ - return KGlobal::dirs()->findAllResources("data", "konsole/*.profile", - KStandardDirs::NoDuplicates); -} -void KDE4ProfileReader::readProperties(const KConfig& config, Profile::Ptr profile, - const Profile::PropertyInfo* properties) -{ - const char* groupName = 0; - KConfigGroup group; - - while (properties->name != 0) { - if (properties->group != 0) { - if (groupName == 0 || qstrcmp(groupName, properties->group) != 0) { - group = config.group(properties->group); - groupName = properties->group; - } - - QString name(properties->name); - - if (group.hasKey(name)) - profile->setProperty(properties->property, - group.readEntry(name, QVariant(properties->type))); - - } - - properties++; - } -} - -bool KDE4ProfileReader::readProfile(const QString& path , Profile::Ptr profile , QString& parentProfile) -{ - if (!QFile::exists(path)) - return false; - - KConfig config(path, KConfig::NoGlobals); - - KConfigGroup general = config.group(GENERAL_GROUP); - if (general.hasKey("Parent")) - parentProfile = general.readEntry("Parent"); - - if (general.hasKey("Command")) { - ShellCommand shellCommand(general.readEntry("Command")); - - profile->setProperty(Profile::Command, shellCommand.command()); - profile->setProperty(Profile::Arguments, shellCommand.arguments()); - } - - // Read remaining properties - readProperties(config, profile, Profile::DefaultPropertyNames); - - return true; -} - QHash ProfileCommandParser::parse(const QString& input) { QHash changes; diff --git a/src/Profile.h b/src/Profile.h index 1225b9ecd..6fee6fb13 100644 --- a/src/Profile.h +++ b/src/Profile.h @@ -569,68 +569,6 @@ inline Profile::GroupPtr Profile::asGroup() return Profile::GroupPtr(dynamic_cast(this)); } -/** Interface for all classes which can load profile settings from a file. */ -class ProfileReader -{ -public: - virtual ~ProfileReader() {} - /** Returns a list of paths to profiles which this reader can read. */ - virtual QStringList findProfiles() { - return QStringList(); - } - /** - * Attempts to read a profile from @p path and - * save the property values described into @p profile. - * - * Returns true if the profile was successfully read or false otherwise. - * - * @param path Path to the profile to read - * @param profile Pointer to the Profile the settings will be read into - * @param parentProfile Receives the name of the parent profile - */ - virtual bool readProfile(const QString& path , Profile::Ptr profile , QString& parentProfile) = 0; -}; - -/** Reads a KDE 4 .profile file. */ -class KDE4ProfileReader : public ProfileReader -{ -public: - virtual QStringList findProfiles(); - virtual bool readProfile(const QString& path , Profile::Ptr profile, QString& parentProfile); -private: - void readProperties(const KConfig& config, Profile::Ptr profile, - const Profile::PropertyInfo* properties); -}; -/** Interface for all classes which can write profile settings to a file. */ -class ProfileWriter -{ -public: - virtual ~ProfileWriter() {} - /** - * Returns a suitable path-name for writing - * @p profile to. The path-name should be accepted by - * the corresponding ProfileReader class. - */ - virtual QString getPath(const Profile::Ptr profile) = 0; - /** - * Writes the properties and values from @p profile to the file specified - * by @p path. This profile should be readable by the corresponding - * ProfileReader class. - */ - virtual bool writeProfile(const QString& path , const Profile::Ptr profile) = 0; -}; -/** Writes a KDE 4 .profile file. */ -class KDE4ProfileWriter : public ProfileWriter -{ -public: - virtual QString getPath(const Profile::Ptr profile); - virtual bool writeProfile(const QString& path , const Profile::Ptr profile); - -private: - void writeProperties(KConfig& config, const Profile::Ptr profile, - const Profile::PropertyInfo* properties); -}; - /** * Parses an input string consisting of property names * and assigned values and returns a table of properties diff --git a/src/ProfileReader.cpp b/src/ProfileReader.cpp new file mode 100644 index 000000000..de8b876c9 --- /dev/null +++ b/src/ProfileReader.cpp @@ -0,0 +1,95 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2006-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +// Own +#include "ProfileReader.h" + +// Qt +#include + +// KDE +#include +#include +#include +#include + +// Konsole +#include "ShellCommand.h" + +using namespace Konsole; + +// FIXME: A dup line from Profile.cpp - redo these +static const char GENERAL_GROUP[] = "General"; + +QStringList KDE4ProfileReader::findProfiles() +{ + return KGlobal::dirs()->findAllResources("data", "konsole/*.profile", + KStandardDirs::NoDuplicates); +} +void KDE4ProfileReader::readProperties(const KConfig& config, Profile::Ptr profile, + const Profile::PropertyInfo* properties) +{ + const char* groupName = 0; + KConfigGroup group; + + while (properties->name != 0) { + if (properties->group != 0) { + if (groupName == 0 || qstrcmp(groupName, properties->group) != 0) { + group = config.group(properties->group); + groupName = properties->group; + } + + QString name(properties->name); + + if (group.hasKey(name)) + profile->setProperty(properties->property, + group.readEntry(name, QVariant(properties->type))); + + } + + properties++; + } +} + +bool KDE4ProfileReader::readProfile(const QString& path , Profile::Ptr profile , QString& parentProfile) +{ + if (!QFile::exists(path)) + return false; + + KConfig config(path, KConfig::NoGlobals); + + KConfigGroup general = config.group(GENERAL_GROUP); + if (general.hasKey("Parent")) + parentProfile = general.readEntry("Parent"); + + if (general.hasKey("Command")) { + ShellCommand shellCommand(general.readEntry("Command")); + + profile->setProperty(Profile::Command, shellCommand.command()); + profile->setProperty(Profile::Arguments, shellCommand.arguments()); + } + + // Read remaining properties + readProperties(config, profile, Profile::DefaultPropertyNames); + + return true; +} + diff --git a/src/ProfileReader.h b/src/ProfileReader.h new file mode 100644 index 000000000..00c7368fd --- /dev/null +++ b/src/ProfileReader.h @@ -0,0 +1,65 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2007-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#ifndef PROFILEREADER_H +#define PROFILEREADER_H + +#include "Profile.h" + +namespace Konsole +{ + +/** Interface for all classes which can load profile settings from a file. */ +class ProfileReader +{ +public: + virtual ~ProfileReader() {} + /** Returns a list of paths to profiles which this reader can read. */ + virtual QStringList findProfiles() { + return QStringList(); + } + /** + * Attempts to read a profile from @p path and + * save the property values described into @p profile. + * + * Returns true if the profile was successfully read or false otherwise. + * + * @param path Path to the profile to read + * @param profile Pointer to the Profile the settings will be read into + * @param parentProfile Receives the name of the parent profile + */ + virtual bool readProfile(const QString& path , Profile::Ptr profile , QString& parentProfile) = 0; +}; + +/** Reads a KDE 4 .profile file. */ +class KDE4ProfileReader : public ProfileReader +{ +public: + virtual QStringList findProfiles(); + virtual bool readProfile(const QString& path , Profile::Ptr profile, QString& parentProfile); +private: + void readProperties(const KConfig& config, Profile::Ptr profile, + const Profile::PropertyInfo* properties); +}; + +} + +#endif // PROFILEREADER_H diff --git a/src/ProfileWriter.cpp b/src/ProfileWriter.cpp new file mode 100644 index 000000000..477f419ea --- /dev/null +++ b/src/ProfileWriter.cpp @@ -0,0 +1,99 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2006-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +// Own +#include "ProfileWriter.h" + +// Qt +#include + +// KDE +#include +#include +#include +#include + +// Konsole +#include "ShellCommand.h" + +using namespace Konsole; + +// FIXME: A dup line from Profile.cpp - redo these +static const char GENERAL_GROUP[] = "General"; + +QString KDE4ProfileWriter::getPath(const Profile::Ptr info) +{ + QString newPath; + + if (info->isPropertySet(Profile::Path) && + info->path().startsWith(KGlobal::dirs()->saveLocation("data", "konsole/"))) { + newPath = info->path(); + } else { + // use the profile name + ".profile" and save it in $KDEHOME + newPath = KGlobal::dirs()->saveLocation("data", "konsole/") + info->name() + ".profile"; + } + + return newPath; +} +void KDE4ProfileWriter::writeProperties(KConfig& config, + const Profile::Ptr profile, + const Profile::PropertyInfo* properties) +{ + const char* groupName = 0; + KConfigGroup group; + + while (properties->name != 0) { + if (properties->group != 0) { + if (groupName == 0 || qstrcmp(groupName, properties->group) != 0) { + group = config.group(properties->group); + groupName = properties->group; + } + + if (profile->isPropertySet(properties->property)) + group.writeEntry(QString(properties->name), + profile->property(properties->property)); + } + + properties++; + } +} +bool KDE4ProfileWriter::writeProfile(const QString& path , const Profile::Ptr profile) +{ + KConfig config(path, KConfig::NoGlobals); + + KConfigGroup general = config.group(GENERAL_GROUP); + + // Parent profile if set, when loading the profile in future, the parent + // must be loaded as well if it exists. + if (profile->parent()) + general.writeEntry("Parent", profile->parent()->path()); + + if (profile->isPropertySet(Profile::Command) + || profile->isPropertySet(Profile::Arguments)) + general.writeEntry("Command", + ShellCommand(profile->command(), profile->arguments()).fullCommand()); + + // Write remaining properties + writeProperties(config, profile, Profile::DefaultPropertyNames); + + return true; +} + diff --git a/src/ProfileWriter.h b/src/ProfileWriter.h new file mode 100644 index 000000000..43d4389a1 --- /dev/null +++ b/src/ProfileWriter.h @@ -0,0 +1,62 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2007-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#ifndef PROFILEWRITER_H +#define PROFILEWRITER_H + +#include "Profile.h" + +namespace Konsole +{ + +/** Interface for all classes which can write profile settings to a file. */ +class ProfileWriter +{ +public: + virtual ~ProfileWriter() {} + /** + * Returns a suitable path-name for writing + * @p profile to. The path-name should be accepted by + * the corresponding ProfileReader class. + */ + virtual QString getPath(const Profile::Ptr profile) = 0; + /** + * Writes the properties and values from @p profile to the file specified + * by @p path. This profile should be readable by the corresponding + * ProfileReader class. + */ + virtual bool writeProfile(const QString& path , const Profile::Ptr profile) = 0; +}; +/** Writes a KDE 4 .profile file. */ +class KDE4ProfileWriter : public ProfileWriter +{ +public: + virtual QString getPath(const Profile::Ptr profile); + virtual bool writeProfile(const QString& path , const Profile::Ptr profile); + +private: + void writeProperties(KConfig& config, const Profile::Ptr profile, + const Profile::PropertyInfo* properties); +}; + +} + +#endif // PROFILEWRITER_H diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp index de6677621..22c25265f 100644 --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -42,6 +42,8 @@ // Konsole #include "Session.h" #include "History.h" +#include "ProfileReader.h" +#include "ProfileWriter.h" using namespace Konsole;