comparison gui/konsole/Profile.h @ 14242:637675470c58 gui

Added OctaveDEs konsole sources.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Sun, 22 Jan 2012 14:18:05 +0100
parents
children
comparison
equal deleted inserted replaced
14240:a9992bc3c3f7 14242:637675470c58
1 /*
2 This source file is part of Konsole, a terminal emulator.
3
4 Copyright 2007-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 PROFILE_H
23 #define PROFILE_H
24
25 // Qt
26 #include <QtCore/QHash>
27 #include <QtCore/QObject>
28 #include <QtCore/QPointer>
29 #include <QtCore/QStringList>
30 #include <QtCore/QVariant>
31
32 #include <QtGui/QFont>
33
34 // KDE
35 #include <KSharedPtr>
36 #include <KDebug>
37
38 // Konsole
39 #include "konsole_export.h"
40
41 class KConfig;
42
43 namespace Konsole
44 {
45 class ProfileGroup;
46
47 /**
48 * Represents a terminal set-up which can be used to
49 * set the initial state of new terminal sessions or applied
50 * to existing sessions. Profiles consist of a number of named
51 * properties, which can be retrieved using property() and
52 * set using setProperty(). isPropertySet() can be used to check
53 * whether a particular property has been set in a profile.
54 *
55 * Profiles support a simple form of inheritance. When a new Profile
56 * is constructed, a pointer to a parent profile can be passed to
57 * the constructor. When querying a particular property of a profile
58 * using property(), the profile will return its own value for that
59 * property if one has been set or otherwise it will return the
60 * parent's value for that property.
61 *
62 * Profiles can be loaded from disk using ProfileReader instances
63 * and saved to disk using ProfileWriter instances.
64 */
65 class KONSOLEPRIVATE_EXPORT Profile : public QSharedData
66 {
67
68 friend class KDE4ProfileReader;
69 friend class KDE4ProfileWriter;
70 friend class ProfileGroup;
71
72 public:
73 typedef KSharedPtr<Profile> Ptr;
74 typedef KSharedPtr<ProfileGroup> GroupPtr;
75
76 /**
77 * This enum describes the available properties
78 * which a Profile may consist of.
79 *
80 * Properties can be set using setProperty() and read
81 * using property()
82 */
83 enum Property
84 {
85 /** (QString) Path to the profile's configuration file on-disk. */
86 Path,
87 /** (QString) The descriptive name of this profile. */
88 Name,
89 /** (QString) Title of this profile that will be displayed. */
90 Title,
91 /** (QString) The name of the icon associated with this profile. This
92 * is used in menus and tabs to represent the profile.
93 */
94 Icon,
95 /** (QString) The command to execute ( excluding arguments ) when creating a new terminal
96 * session using this profile.
97 */
98 Command,
99 /** (QStringList) The arguments which are passed to the program specified by
100 * the Command property when creating a new terminal session using this profile.
101 */
102 Arguments,
103 /** (QStringList) Additional environment variables ( in the form of NAME=VALUE pairs )
104 * which are passed to the program specified by the Command property
105 * when creating a new terminal session using this profile.
106 */
107 Environment,
108 /** (QString) The initial working directory for sessions created using this profile. */
109 Directory,
110 /** (QString) The format used for tab titles when running normal commands. */
111 LocalTabTitleFormat,
112 /** (QString) The format used for tab titles when the session is running
113 * a remote command (eg. SSH) */
114 RemoteTabTitleFormat,
115 /** (bool) Specifies whether the menu bar should be shown in the main application window. */
116 ShowMenuBar,
117 SaveGeometryOnExit,
118 /** (TabBarModeEnum) Specifies when the tab bar should be shown in
119 * the main application window. */
120 TabBarMode,
121 /** (QFont) The font to use in terminal displays using this profile. */
122 Font,
123 /** (QString)
124 * The name of the color scheme to use in terminal displays using this profile.
125 * Color schemes are managed by the ColorSchemeManager class.
126 */
127 ColorScheme,
128 /** (QString) The name of the key bindings.
129 * Key bindings are managed by the KeyboardTranslatorManager class.
130 */
131 KeyBindings,
132 /** (HistoryModeEnum) Specifies the storage type used for keeping the output produced
133 * by terminal sessions using this profile.
134 */
135 HistoryMode,
136 /** (int) Specifies the number of lines of output to remember in terminal sessions
137 * using this profile. Once the limit is reached, the oldest lines are lost.
138 * Only applicable if the HistoryMode property is FixedSizeHistory
139 */
140 HistorySize,
141 /** (ScrollBarPositionEnum) Specifies the position of the scroll bar in
142 * terminal displays using this profile.
143 */
144 ScrollBarPosition,
145 /** (bool) Specifies whether the terminal will enable Bidirectional text display */
146 BidiRenderingEnabled,
147 /** (bool) Specifies whether text in terminal displays is allowed to blink. */
148 BlinkingTextEnabled,
149 /** (bool) Specifies whether the flow control keys ( typically Ctrl+S , Ctrl+Q )
150 * have any effect. Also known as Xon/Xoff
151 */
152 FlowControlEnabled,
153 /** (bool) Specifies whether programs running in the terminal are allowed to
154 * resize the terminal display.
155 */
156 AllowProgramsToResizeWindow,
157 /** (bool) Specifies whether the cursor blinks ( in a manner similar
158 * to text editing applications )
159 */
160 BlinkingCursorEnabled,
161 /** (bool) If true, terminal displays use a fixed color to draw the cursor,
162 * specified by the CustomCursorColor property. Otherwise the cursor changes
163 * color to match the character underneath it.
164 */
165 UseCustomCursorColor,
166 /** (CursorShapeEnum) The shape used by terminal displays to represent the cursor. */
167 CursorShape,
168 /** (QColor) The color used by terminal displays to draw the cursor. Only applicable
169 * if the UseCustomCursorColor property is true. */
170 CustomCursorColor,
171 /** (QString) A string consisting of the characters used to delimit words when
172 * selecting text in the terminal display.
173 */
174 WordCharacters,
175 /** (TabBarPositionEnum) Position of the tab-bar relative to the terminal displays. */
176 TabBarPosition,
177 /** (bool) If true, the triple click selects from current word onwards. Otherwise
178 * selects whole line.
179 */
180 TripleClickMode,
181 /** (String) Default text codec */
182 DefaultEncoding,
183 /** (bool) Whether fonts should be aliased or not */
184 AntiAliasFonts,
185 /** (bool) Whether new sessions should be started in the same directory as the
186 * currently active session. */
187 BoldIntense,
188 /** (bool) Whether character with intense colors should be rendered in bold font
189 * or just in bright color. */
190 StartInCurrentSessionDir,
191 /** (bool) Whether a 'New Tab' and 'Close Tab' buttons should be shown on the tab bar */
192 ShowNewAndCloseTabButtons,
193 /** Index of profile in the File Menu
194 * In future, format will be #.#.# to account for levels
195 */
196 MenuIndex
197 };
198
199 /**
200 * This enum describes the available modes for showing or hiding the tab bar.
201 */
202 enum TabBarModeEnum
203 {
204 /** The tab bar is never shown. */
205 AlwaysHideTabBar = 0,
206 /** The tab bar is shown if there are multiple tabs open or hidden otherwise. */
207 ShowTabBarAsNeeded = 1,
208 /** The tab bar is always shown. */
209 AlwaysShowTabBar = 2
210 };
211
212 /**
213 * This enum describes the available tab bar positions.
214 */
215 enum TabBarPositionEnum
216 {
217 /** Show tab bar below displays. */
218 TabBarBottom = 0,
219 /** Show tab bar above displays. */
220 TabBarTop = 1
221 };
222
223 /**
224 * This enum describes the modes available to remember lines of output produced
225 * by the terminal.
226 */
227 enum HistoryModeEnum
228 {
229 /** No output is remembered. As soon as lines of text are scrolled off-screen they are lost. */
230 DisableHistory = 0,
231 /** A fixed number of lines of output are remembered. Once the limit is reached, the oldest
232 * lines are lost. */
233 FixedSizeHistory = 1,
234 /** All output is remembered for the duration of the session.
235 * Typically this means that lines are recorded to
236 * a file as they are scrolled off-screen.
237 */
238 UnlimitedHistory = 2
239 };
240
241 /**
242 * This enum describes the positions where the terminal display's scroll bar may be placed.
243 */
244 enum ScrollBarPositionEnum
245 {
246 /** Show the scroll-bar on the left of the terminal display. */
247 ScrollBarLeft = 0,
248 /** Show the scroll-bar on the right of the terminal display. */
249 ScrollBarRight = 1,
250 /** Do not show the scroll-bar. */
251 ScrollBarHidden = 2
252 };
253
254 /** This enum describes the shapes used to draw the cursor in terminal displays. */
255 enum CursorShapeEnum
256 {
257 /** Use a solid rectangular block to draw the cursor. */
258 BlockCursor = 0,
259 /** Use an 'I' shape, similar to that used in text editing applications, to draw the cursor. */
260 IBeamCursor = 1,
261 /** Draw a line underneath the cursor's position. */
262 UnderlineCursor = 2
263 };
264
265 /**
266 * Constructs a new profile
267 *
268 * @param parent The parent profile. When querying the value of a property
269 * using property(), if the property has not been set in this profile then
270 * the parent's value for the property will be returned instead.
271 */
272 explicit Profile(Ptr parent = Ptr());
273 virtual ~Profile();
274
275 /**
276 * Copies all properties except Name and Path from the specified @p profile
277 * into this profile
278 *
279 * @param profile The profile to copy properties from
280 * @param differentOnly If true, only properties in @p profile which have a
281 * different value from this profile's current value (either set via
282 * setProperty() or inherited from the parent profile) will be set.
283 */
284 void clone(Ptr profile, bool differentOnly = true);
285
286 /**
287 * Changes the parent profile. When calling the property() method,
288 * if the specified property has not been set for this profile,
289 * the parent's value for the property will be returned instead.
290 */
291 void setParent(Ptr parent);
292
293 /** Returns the parent profile. */
294 const Ptr parent() const;
295
296 /** Returns this profile as a group or null if this profile is not a group. */
297 const GroupPtr asGroup() const;
298 GroupPtr asGroup();
299
300 /**
301 * Returns the current value of the specified @p property, cast to type T.
302 * Internally properties are stored using the QVariant type and cast to T
303 * using QVariant::value<T>();
304 *
305 * If the specified @p property has not been set in this profile,
306 * and a non-null parent was specified in the Profile's constructor,
307 * the parent's value for @p property will be returned.
308 */
309 template <class T>
310 T property(Property property) const;
311
312 /** Sets the value of the specified @p property to @p value. */
313 virtual void setProperty(Property property,const QVariant& value);
314 /** Returns true if the specified property has been set in this Profile instance. */
315 virtual bool isPropertySet(Property property) const;
316
317 /** Returns a map of the properties set in this Profile instance. */
318 virtual QHash<Property,QVariant> setProperties() const;
319
320 /** Returns true if no properties have been set in this Profile instance. */
321 bool isEmpty() const;
322
323 /**
324 * Returns true if this is a 'hidden' profile which should not be displayed
325 * in menus or saved to disk.
326 *
327 * This is used for the fallback profile, in case there are no profiles on
328 * disk which can be loaded, or for overlay profiles created to handle
329 * command-line arguments which change profile properties.
330 */
331 bool isHidden() const;
332
333 /** Specifies whether this is a hidden profile. See isHidden() */
334 void setHidden(bool hidden);
335
336 //
337 // Convenience methods for property() and setProperty() go here
338 //
339
340 /** Convenience method for property<QString>(Profile::Path) */
341 QString path() const { return property<QString>(Profile::Path); }
342
343 /** Convenience method for property<QString>(Profile::Name) */
344 QString name() const { return property<QString>(Profile::Name); }
345
346 /** Convenience method for property<QString>(Profile::Directory) */
347 QString defaultWorkingDirectory() const
348 { return property<QString>(Profile::Directory); }
349
350 /** Convenience method for property<QString>(Profile::Icon) */
351 QString icon() const { return property<QString>(Profile::Icon); }
352
353 /** Convenience method for property<QString>(Profile::Command) */
354 QString command() const { return property<QString>(Profile::Command); }
355
356 /** Convenience method for property<QStringList>(Profile::Arguments) */
357 QStringList arguments() const { return property<QStringList>(Profile::Arguments); }
358
359 /** Convenience method for property<QFont>(Profile::Font) */
360 QFont font() const { return property<QFont>(Profile::Font); }
361
362 /** Convenience method for property<QString>(Profile::ColorScheme) */
363 QString colorScheme() const { return property<QString>(Profile::ColorScheme); }
364
365 /** Convenience method for property<QStringList>(Profile::Environment) */
366 QStringList environment() const { return property<QStringList>(Profile::Environment); }
367
368 /** Convenience method for property<QString>(Profile::MenuIndex) */
369 QString menuIndex() const { return property<QString>(Profile::MenuIndex); }
370
371 int menuIndexAsInt() const;
372
373 /**
374 * Returns true if @p name has been associated with an element
375 * from the Property enum or false otherwise.
376 */
377 static bool isNameRegistered(const QString& name);
378
379 /**
380 * Returns the element from the Property enum associated with the
381 * specified @p name.
382 *
383 * @param name The name of the property to look for, this is case insensitive.
384 */
385 static Property lookupByName(const QString& name);
386 /**
387 * Returns the string names associated with the specified @p property from
388 * the Property enum, in the order the associations were created using
389 * registerName()
390 */
391 static QList<QString> namesForProperty(Property property);
392
393 /**
394 * Returns the primary name for the specified @p property.
395 * TODO More documentation
396 */
397 static QString primaryNameForProperty(Property property);
398
399 private:
400 struct PropertyInfo;
401 // Defines a new property, this property is then available
402 // to all Profile instances.
403 static void registerProperty(const PropertyInfo& info);
404
405 // fills the table with default names for profile properties
406 // the first time it is called.
407 // subsequent calls return immediately
408 static void fillTableWithDefaultNames();
409
410 // returns true if the property can be inherited
411 static bool canInheritProperty(Property property);
412
413 QHash<Property,QVariant> _propertyValues;
414 Ptr _parent;
415
416 bool _hidden;
417
418 static QHash<QString,PropertyInfo> _propertyInfoByName;
419 static QHash<Property,PropertyInfo> _infoByProperty;
420
421 // Describes a property. Each property has a name and group
422 // which is used when saving/loading the profile.
423 struct PropertyInfo
424 {
425 Property property;
426 const char* name;
427 const char* group;
428 QVariant::Type type;
429 };
430 static const PropertyInfo DefaultPropertyNames[];
431 };
432
433 template <class T>
434 inline T Profile::property(Property theProperty) const
435 {
436 return property<QVariant>(theProperty).value<T>();
437 }
438 template <>
439 inline QVariant Profile::property(Property property) const
440 {
441 if ( _propertyValues.contains(property) ) {
442 return _propertyValues[property];
443 }
444 else if ( _parent && canInheritProperty(property) ) {
445 return _parent->property<QVariant>(property);
446 }
447 else {
448 return QVariant();
449 }
450 }
451 inline bool Profile::canInheritProperty(Property property)
452 { return property != Name && property != Path; }
453
454
455 /**
456 * A profile which contains a number of default settings for various properties.
457 * This can be used as a parent for other profiles or a fallback in case
458 * a profile cannot be loaded from disk.
459 */
460 class KONSOLEPRIVATE_EXPORT FallbackProfile : public Profile
461 {
462 public:
463 FallbackProfile();
464 };
465
466 /**
467 * A composite profile which allows a group of profiles to be treated as one.
468 * When setting a property, the new value is applied to all profiles in the group.
469 * When reading a property, if all profiles in the group have the same value
470 * then that value is returned, otherwise the result is null.
471 *
472 * Profiles can be added to the group using addProfile(). When all profiles
473 * have been added updateValues() must be called
474 * to sync the group's property values with those of the group's profiles.
475 *
476 * The Profile::Name and Profile::Path properties are unique to individual profiles,
477 * setting these properties on a ProfileGroup has no effect.
478 */
479 class KONSOLEPRIVATE_EXPORT ProfileGroup : public Profile
480 {
481 public:
482 typedef KSharedPtr<ProfileGroup> Ptr;
483
484 /** Construct a new profile group, which is hidden by default. */
485 ProfileGroup(Profile::Ptr parent = Profile::Ptr());
486
487 /** Add a profile to the group. Calling setProperty() will update this profile.
488 * When creating a group, add the profiles to the group then call updateValues() to
489 * make the group's property values reflect the profiles currently in the group. */
490 void addProfile(Profile::Ptr profile)
491 { _profiles.append(profile); }
492
493 /** Remove a profile from the group. Calling setProperty() will no longer
494 * affect this profile. */
495 void removeProfile(Profile::Ptr profile)
496 { _profiles.removeAll(profile); }
497
498 /** Returns the profiles in this group .*/
499 QList<Profile::Ptr> profiles() const
500 { return _profiles; }
501
502 /**
503 * Updates the property values in this ProfileGroup to match those from
504 * the group's profiles()
505 *
506 * For each available property, if each profile in the group has the same value then
507 * the ProfileGroup will use that value for the property. Otherwise the value for the property
508 * will be set to a null QVariant
509 *
510 * Some properties such as the name and the path of the profile
511 * will always be set to null if the group has more than one profile.
512 */
513 void updateValues();
514
515 /** Sets the value of @p property in each of the group's profiles to @p value. */
516 void setProperty(Property property, const QVariant& value);
517
518 private:
519 QList<Profile::Ptr> _profiles;
520 };
521 inline ProfileGroup::ProfileGroup(Profile::Ptr parent)
522 : Profile(parent)
523 {
524 setHidden(true);
525 }
526 inline const Profile::GroupPtr Profile::asGroup() const
527 {
528 const Profile::GroupPtr ptr(dynamic_cast<ProfileGroup*>(
529 const_cast<Profile*>(this)));
530 return ptr;
531 }
532 inline Profile::GroupPtr Profile::asGroup()
533 {
534 return Profile::GroupPtr(dynamic_cast<ProfileGroup*>(this));
535 }
536
537 /** Interface for all classes which can load profile settings from a file. */
538 class ProfileReader
539 {
540 public:
541 virtual ~ProfileReader() {}
542 /** Returns a list of paths to profiles which this reader can read. */
543 virtual QStringList findProfiles() { return QStringList(); }
544 /**
545 * Attempts to read a profile from @p path and
546 * save the property values described into @p profile.
547 *
548 * Returns true if the profile was successfully read or false otherwise.
549 *
550 * @param path Path to the profile to read
551 * @param profile Pointer to the Profile the settings will be read into
552 * @param parentProfile Receives the name of the parent profile specified in
553 */
554 virtual bool readProfile(const QString& path , Profile::Ptr profile , QString& parentProfile) = 0;
555 };
556 /** Reads a KDE 3 profile .desktop file. */
557 class KDE3ProfileReader : public ProfileReader
558 {
559 public:
560 virtual QStringList findProfiles();
561 virtual bool readProfile(const QString& path , Profile::Ptr profile, QString& parentProfile);
562 };
563 /** Reads a KDE 4 .profile file. */
564 class KDE4ProfileReader : public ProfileReader
565 {
566 public:
567 virtual QStringList findProfiles();
568 virtual bool readProfile(const QString& path , Profile::Ptr profile, QString& parentProfile);
569 private:
570 void readProperties(const KConfig& config, Profile::Ptr profile,
571 const Profile::PropertyInfo* properties);
572 };
573 /** Interface for all classes which can write profile settings to a file. */
574 class ProfileWriter
575 {
576 public:
577 virtual ~ProfileWriter() {}
578 /**
579 * Returns a suitable path-name for writing
580 * @p profile to. The path-name should be accepted by
581 * the corresponding ProfileReader class.
582 */
583 virtual QString getPath(const Profile::Ptr profile) = 0;
584 /**
585 * Writes the properties and values from @p profile to the file specified by
586 * @p path. This profile should be readable by the corresponding ProfileReader class.
587 */
588 virtual bool writeProfile(const QString& path , const Profile::Ptr profile) = 0;
589 };
590 /** Writes a KDE 4 .profile file. */
591 class KDE4ProfileWriter : public ProfileWriter
592 {
593 public:
594 virtual QString getPath(const Profile::Ptr profile);
595 virtual bool writeProfile(const QString& path , const Profile::Ptr profile);
596
597 private:
598 void writeProperties(KConfig& config, const Profile::Ptr profile,
599 const Profile::PropertyInfo* properties);
600 };
601
602 /**
603 * Parses an input string consisting of property names
604 * and assigned values and returns a table of properties
605 * and values.
606 *
607 * The input string will typically look like this:
608 *
609 * @code
610 * PropertyName=Value;PropertyName=Value ...
611 * @endcode
612 *
613 * For example:
614 *
615 * @code
616 * Icon=konsole;Directory=/home/bob
617 * @endcode
618 */
619 class KONSOLEPRIVATE_EXPORT ProfileCommandParser
620 {
621 public:
622 /**
623 * Parses an input string consisting of property names
624 * and assigned values and returns a table of
625 * properties and values.
626 */
627 QHash<Profile::Property,QVariant> parse(const QString& input);
628
629 };
630
631 }
632 Q_DECLARE_METATYPE(Konsole::Profile::Ptr)
633
634 #endif // PROFILE_H