1/*
2 This source file is part of Konsole, a terminal emulator.
3
4 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20*/
21
22#ifndef PROFILEMANAGER_H
23#define PROFILEMANAGER_H
24
25// Qt
26#include <QtGui/QKeySequence>
27#include <QtCore/QHash>
28#include <QtCore/QList>
29#include <QtCore/QSet>
30#include <QtCore/QStringList>
31#include <QtCore/QVariant>
32#include <QtCore/QStack>
33
34// Konsole
35#include "Profile.h"
36
37namespace Konsole
38{
39/**
40 * Manages profiles which specify various settings for terminal sessions
41 * and their displays.
42 *
43 * Profiles in the manager have a concept of favorite status, which can be used
44 * by widgets and dialogs in the application decide which profiles to list and
45 * how to display them. The favorite status of a profile can be altered using
46 * setFavorite() and retrieved using isFavorite()
47 */
48class KONSOLEPRIVATE_EXPORT ProfileManager : public QObject
49{
50 Q_OBJECT
51
52public:
53 /**
54 * Constructs a new profile manager and loads information about the available
55 * profiles.
56 */
57 ProfileManager();
58
59 /**
60 * Destroys the ProfileManager.
61 */
62 virtual ~ProfileManager();
63
64 /**
65 * Returns the profile manager instance.
66 */
67 static ProfileManager* instance();
68
69 /**
70 * Returns a list of all available profiles
71 *
72 * Initially only the profile currently set as the default is loaded.
73 *
74 * Favorite profiles are loaded automatically when findFavorites() is called.
75 *
76 * When this method is called, it calls loadAllProfiles() internally to
77 * ensure all available profiles are loaded and usable.
78 */
79 QList<Profile::Ptr> allProfiles();
80
81 /**
82 * Returns a list of already loaded profiles
83 */
84 QList<Profile::Ptr> loadedProfiles() const;
85
86 /**
87 * Loads all available profiles. This involves reading each
88 * profile configuration file from disk and parsing it.
89 * Therefore it should only be done when necessary.
90 */
91 void loadAllProfiles();
92
93 /**
94 * Loads a profile from the specified path and registers
95 * it with the ProfileManager.
96 *
97 * @p path may be relative or absolute. The path may just be the
98 * base name of the profile to load (eg. if the profile's full path
99 * is "<konsole data dir>/My Profile.profile" then both
100 * "konsole/My Profile.profile" , "My Profile.profile" and
101 * "My Profile" will be accepted)
102 *
103 * @return Pointer to a profile which can be passed to
104 * SessionManager::createSession() to create a new session using
105 * this profile.
106 */
107 Profile::Ptr loadProfile(const QString& path);
108
109 /**
110 * Searches for available profiles on-disk and returns a list
111 * of paths of profiles which can be loaded.
112 */
113 QStringList availableProfilePaths() const;
114
115 /**
116 * Returns a list of names of all available profiles
117 */
118 QStringList availableProfileNames() const;
119
120 /**
121 * Registers a new type of session.
122 * The favorite status of the session ( as returned by isFavorite() ) is set to false by default.
123 */
124 void addProfile(Profile::Ptr type);
125
126 /**
127 * Deletes the configuration file used to store a profile.
128 * The profile will continue to exist while sessions are still using it. The profile
129 * will be marked as hidden (see Profile::setHidden() ) so that it does not show
130 * up in profile lists and future changes to the profile are not stored to disk.
131 *
132 * Returns true if the profile was successfully deleted or false otherwise.
133 */
134 bool deleteProfile(Profile::Ptr profile);
135
136 /**
137 * Updates a @p profile with the changes specified in @p propertyMap.
138 *
139 * All sessions currently using the profile will be updated to reflect the new settings.
140 *
141 * After the profile is updated, the profileChanged() signal will be emitted.
142 *
143 * @param profile The profile to change
144 * @param propertyMap A map between profile properties and values describing the changes
145 * @param persistent If true, the changes are saved to the profile's configuration file,
146 * set this to false if you want to preview possible changes to a profile but do not
147 * wish to make them permanent.
148 */
149 void changeProfile(Profile::Ptr profile , QHash<Profile::Property, QVariant> propertyMap,
150 bool persistent = true);
151
152 /**
153 * Sets the @p profile as the default profile for creating new sessions
154 */
155 void setDefaultProfile(Profile::Ptr profile);
156
157 /**
158 * Returns a Profile object describing the default profile
159 */
160 Profile::Ptr defaultProfile() const;
161
162 /**
163 * Returns a Profile object with hard-coded settings which is always available.
164 * This can be used as a parent for new profiles which provides suitable default settings
165 * for all properties.
166 */
167 Profile::Ptr fallbackProfile() const;
168
169 /**
170 * Specifies whether a profile should be included in the user's
171 * list of favorite profiles.
172 */
173 void setFavorite(Profile::Ptr profile , bool favorite);
174
175 /**
176 * Returns the set of the user's favorite profiles.
177 */
178 QSet<Profile::Ptr> findFavorites();
179
180 QList<Profile::Ptr> sortedFavorites();
181
182 /*
183 * Sorts the profile list by menuindex; those without an menuindex, sort by name.
184 * The menuindex list is first and then the non-menuindex list.
185 *
186 * @param list The profile list to sort
187 */
188 void sortProfiles(QList<Profile::Ptr>& list);
189
190 /**
191 * Associates a shortcut with a particular profile.
192 */
193 void setShortcut(Profile::Ptr profile , const QKeySequence& shortcut);
194
195 /** Returns the shortcut associated with a particular profile. */
196 QKeySequence shortcut(Profile::Ptr profile) const;
197
198 /**
199 * Returns the list of shortcut key sequences which
200 * can be used to create new sessions based on
201 * existing profiles
202 *
203 * When one of the shortcuts is activated,
204 * use findByShortcut() to load the profile associated
205 * with the shortcut.
206 */
207 QList<QKeySequence> shortcuts();
208
209 /**
210 * Finds and loads the profile associated with
211 * the specified @p shortcut key sequence and returns a pointer to it.
212 */
213 Profile::Ptr findByShortcut(const QKeySequence& shortcut);
214
215signals:
216
217 /** Emitted when a profile is added to the manager. */
218 void profileAdded(Profile::Ptr ptr);
219 /** Emitted when a profile is removed from the manager. */
220 void profileRemoved(Profile::Ptr ptr);
221 /** Emitted when a profile's properties are modified. */
222 void profileChanged(Profile::Ptr ptr);
223
224 /**
225 * Emitted when the favorite status of a profile changes.
226 *
227 * @param profile The profile to change
228 * @param favorite Specifies whether the profile is a favorite or not
229 */
230 void favoriteStatusChanged(Profile::Ptr profile , bool favorite);
231
232 /**
233 * Emitted when the shortcut for a profile is changed.
234 *
235 * @param profile The profile whose status was changed
236 * @param newShortcut The new shortcut key sequence for the profile
237 */
238 void shortcutChanged(Profile::Ptr profile , const QKeySequence& newShortcut);
239
240public slots:
241 /** Saves settings (favorites, shortcuts, default profile etc.) to disk. */
242 void saveSettings();
243
244protected slots:
245
246private slots:
247
248private:
249 // loads the mappings between shortcut key sequences and
250 // profile paths
251 void loadShortcuts();
252 // saves the mappings between shortcut key sequences and
253 // profile paths
254 void saveShortcuts();
255
256 //loads the set of favorite profiles
257 void loadFavorites();
258 //saves the set of favorite profiles
259 void saveFavorites();
260
261 // records which profile is set as the default profile
262 // Note: it does not save the profile itself into disk. That is
263 // what saveProfile() does.
264 void saveDefaultProfile();
265
266 // saves a profile to a file
267 // returns the path to which the profile was saved, which will
268 // be the same as the path property of profile if valid or a newly generated path
269 // otherwise
270 QString saveProfile(Profile::Ptr profile);
271
272 QSet<Profile::Ptr> _profiles; // list of all loaded profiles
273 QSet<Profile::Ptr> _favorites; // list of favorite profiles
274
275 Profile::Ptr _defaultProfile;
276 Profile::Ptr _fallbackProfile;
277
278 bool _loadedAllProfiles; // set to true after loadAllProfiles has been called
279 bool _loadedFavorites; // set to true after loadFavorites has been called
280
281 struct ShortcutData {
282 Profile::Ptr profileKey;
283 QString profilePath;
284 };
285 QMap<QKeySequence, ShortcutData> _shortcuts; // shortcut keys -> profile path
286};
287
288/**
289 * PopStackOnExit is a utility to remove all values from a QStack which are added during
290 * the lifetime of a PopStackOnExit instance.
291 *
292 * When a PopStackOnExit instance is destroyed, elements are removed from the stack
293 * until the stack count is reduced the value when the PopStackOnExit instance was created.
294 */
295template <class T>
296class PopStackOnExit
297{
298public:
299 explicit PopStackOnExit(QStack<T>& stack) : _stack(stack) , _count(stack.count()) {}
300 ~PopStackOnExit() {
301 while (_stack.count() > _count)
302 _stack.pop();
303 }
304private:
305 QStack<T>& _stack;
306 int _count;
307};
308}
309#endif //PROFILEMANAGER_H
310