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 #ifndef PROFILEMANAGER_H
0010 #define PROFILEMANAGER_H
0011 
0012 // Qt
0013 #include <QKeySequence>
0014 #include <QList>
0015 #include <QLoggingCategory>
0016 #include <QStringList>
0017 
0018 #include <KSharedConfig>
0019 
0020 #include <vector>
0021 
0022 // Konsole
0023 #include "Profile.h"
0024 
0025 Q_DECLARE_LOGGING_CATEGORY(KonsoleProfileDebug)
0026 
0027 namespace Konsole
0028 {
0029 /**
0030  * Manages profiles which specify various settings for terminal sessions
0031  * and their displays.
0032  */
0033 class KONSOLEPRIVATE_EXPORT ProfileManager : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     using Iterator = std::vector<Profile::Ptr>::const_iterator;
0039 
0040     /**
0041      * Constructs a new profile manager and loads information about the available
0042      * profiles.
0043      */
0044     ProfileManager();
0045 
0046     /**
0047      * Destroys the ProfileManager.
0048      */
0049     ~ProfileManager() override;
0050 
0051     /**
0052      * Returns the profile manager instance.
0053      */
0054     static ProfileManager *instance();
0055 
0056     /**
0057      * Returns a list of all available profiles
0058      *
0059      * Initially only the profile currently set as the default is loaded.
0060      *
0061      * When this method is called, it calls loadAllProfiles() internally to
0062      * ensure all available profiles are loaded and usable.
0063      */
0064     QList<Profile::Ptr> allProfiles();
0065 
0066     /**
0067      * Returns a list of already loaded profiles
0068      */
0069     QList<Profile::Ptr> loadedProfiles() const;
0070 
0071     /**
0072      * Loads a profile from the specified path and registers
0073      * it with the ProfileManager.
0074      *
0075      * @p path may be relative or absolute.  The path may just be the
0076      * base name of the profile to load (eg. if the profile's full path
0077      * is "<konsole data dir>/My Profile.profile" then any of the
0078      * "konsole/My Profile.profile", "My Profile.profile" and
0079      * "My Profile" will be accepted)
0080      *
0081      * @return Pointer to a profile which can be passed to
0082      * SessionManager::createSession() to create a new session using
0083      * this profile.
0084      */
0085     Profile::Ptr loadProfile(const QString &shortPath);
0086 
0087     /**
0088      * Initialize built-in profile. It's shown as "Built-in". This is a
0089      * special profile as it's not saved on disk but rather created from
0090      * code in the Profile class, based on the default profile settings.
0091      */
0092     void initBuiltinProfile();
0093 
0094     /**
0095      * Searches for available profiles on-disk and returns a list
0096      * of paths of profiles which can be loaded.
0097      */
0098     QStringList availableProfilePaths() const;
0099 
0100     /**
0101      * Returns a list of names of all available profiles
0102      */
0103     QStringList availableProfileNames() const;
0104 
0105     /**
0106      * Registers a new type of session.
0107      */
0108     void addProfile(const Profile::Ptr &profile);
0109 
0110     /**
0111      * Deletes the configuration file used to store a profile.
0112      * The profile will continue to exist while sessions are still using it.  The profile
0113      * will be marked as hidden (see Profile::setHidden() ) so that it does not show
0114      * up in profile lists and future changes to the profile are not stored to disk.
0115      *
0116      * Returns true if the profile was successfully deleted or false otherwise.
0117      */
0118     bool deleteProfile(Profile::Ptr profile);
0119 
0120     /**
0121      * Updates a @p profile with the changes specified in @p propertyMap.
0122      *
0123      * All sessions currently using the profile will be updated to reflect the new settings.
0124      *
0125      * After the profile is updated, the profileChanged() signal will be emitted.
0126      *
0127      * @param profile The profile to change
0128      * @param propertyMap A map between profile properties and values describing the changes
0129      * @param persistent If true, the changes are saved to the profile's configuration file,
0130      * set this to false if you want to preview possible changes to a profile but do not
0131      * wish to make them permanent.
0132      */
0133     void changeProfile(Profile::Ptr profile, const Profile::PropertyMap &propertyMap, bool persistent = true);
0134 
0135     /**
0136      * Sets the @p profile as the default profile for creating new sessions
0137      */
0138     void setDefaultProfile(const Profile::Ptr &profile);
0139 
0140     /**
0141      * Returns the current default profile.
0142      */
0143     Profile::Ptr defaultProfile() const;
0144 
0145     /**
0146      * Returns a Profile object with some built-in sane defaults.
0147      * It is always available, and it is NOT loaded from or saved to a file.
0148      * This can be used as a parent for new profiles.
0149      */
0150     Profile::Ptr builtinProfile() const;
0151 
0152     /**
0153      * Associates a shortcut with a particular profile.
0154      */
0155     void setShortcut(Profile::Ptr profile, const QKeySequence &keySequence);
0156 
0157     /** Returns the shortcut associated with a particular profile. */
0158     QKeySequence shortcut(Profile::Ptr profile) const;
0159 
0160     /**
0161      * Creates a unique name for a new profile, e.g. "Profile 1", "Profile 2" ...etc.
0162      */
0163     QString generateUniqueName() const;
0164 
0165 Q_SIGNALS:
0166 
0167     /** Emitted when a profile is added to the manager. */
0168     void profileAdded(const Profile::Ptr &ptr);
0169     /** Emitted when a profile is removed from the manager. */
0170     void profileRemoved(const Profile::Ptr &ptr);
0171     /** Emitted when a profile's properties are modified. */
0172     void profileChanged(const Profile::Ptr &ptr);
0173 
0174     /**
0175      * Emitted when the shortcut for a profile is changed.
0176      *
0177      * @param profile The profile whose status was changed
0178      * @param newShortcut The new shortcut key sequence for the profile
0179      */
0180     void shortcutChanged(const Profile::Ptr &profile, const QKeySequence &newShortcut);
0181 
0182 public Q_SLOTS:
0183     /** Saves settings (currently only profile shortcuts) to disk. */
0184     void saveSettings();
0185 
0186 protected Q_SLOTS:
0187 
0188 private Q_SLOTS:
0189 
0190 private:
0191     Q_DISABLE_COPY(ProfileManager)
0192 
0193     /**
0194      * Loads all available profiles. This involves reading each
0195      * profile configuration file from disk and parsing it.
0196      * Therefore it should only be done when necessary.
0197      */
0198     void loadAllProfiles(const QString &defaultProfileFileName = {});
0199 
0200     // loads the mappings between shortcut key sequences and
0201     // profile paths
0202     void loadShortcuts();
0203     // saves the mappings between shortcut key sequences and
0204     // profile paths
0205     void saveShortcuts();
0206 
0207     // records which profile is set as the default profile
0208     // Note: it does not save the profile itself into disk. That is
0209     // what saveProfile() does.
0210     void saveDefaultProfile();
0211 
0212     // saves a profile to a file
0213     // returns the path to which the profile was saved, which will
0214     // be the same as the path property of profile if valid or a newly generated path
0215     // otherwise
0216     QString saveProfile(const Profile::Ptr &profile);
0217 
0218     // Sorts _profiles by profile name
0219     void sortProfiles();
0220 
0221     // Uses std::find to find "profile" _profiles
0222     Iterator findProfile(const Profile::Ptr &profile) const;
0223 
0224     // A list of all loaded profiles, sorted by profile name
0225     std::vector<Profile::Ptr> _profiles;
0226 
0227     Profile::Ptr _defaultProfile;
0228     Profile::Ptr _builtinProfile;
0229 
0230     struct ShortcutData {
0231         Profile::Ptr profileKey;
0232         QKeySequence keySeq;
0233     };
0234     std::vector<ShortcutData> _shortcuts;
0235 
0236     // Set to true when setShortcut() is called so that when the ProfileSettings
0237     // dialog is accepted the profiles shortcut changes are saved
0238     bool _profileShortcutsChanged = false;
0239 
0240     KSharedConfigPtr m_config;
0241 };
0242 
0243 }
0244 
0245 #endif // PROFILEMANAGER_H