From 317c5cb15314303deb7661ec3486774ca01b8e1f Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 2 Jun 2007 03:29:42 +0000 Subject: [PATCH] Put the property enum value <-> string name mappings in a single place rather than duplicating them in the property reader and property writer. svn path=/trunk/KDE/kdebase/apps/konsole/; revision=670568 --- src/Profile.cpp | 163 ++++++++++++++++++++++++++++++++++-------------- src/Profile.h | 25 ++++++-- 2 files changed, 138 insertions(+), 50 deletions(-) diff --git a/src/Profile.cpp b/src/Profile.cpp index 0a813766..44641b2d 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -39,7 +39,59 @@ using namespace Konsole; -QHash Profile::_propertyNames; +const Profile::PropertyNamePair Profile::DefaultPropertyNames[] = +{ + { Path , "Path" } + , { Name , "Name" } + , { Title , "Title" } + , { Icon , "Icon" } + , { Command , "Command" } + , { Arguments , "Arguments" } + , { Environment , "Environment" } + , { Directory , "Directory" } + , { LocalTabTitleFormat , "LocalTabTitleFormat" } + , { RemoteTabTitleFormat , "RemoteTabTitleFormat" } + , { ShowMenuBar , "ShowMenuBar" } + , { TabBarMode , "TabBarMode" } + , { Font , "Font" } + , { ColorScheme , "ColorScheme" } + , { KeyBindings , "KeyBindings" } + , { HistoryMode , "HistoryMode" } + , { HistorySize , "HistorySize" } + , { ScrollBarPosition , "ScrollBarPosition" } + , { SelectWordCharacters , "SelectWordCharacters" } + , { BlinkingTextEnabled , "BlinkingTextEnabled" } + , { FlowControlEnabled , "FlowControlEnabled" } + , { AllowProgramsToResizeWindow , "AllowProgramsToResizeWindow" } + , { BlinkingCursorEnabled , "BlinkingCursorEnabled" } + , { UseCustomCursorColor , "UseCustomCursorColor" } + , { CursorShape , "CursorShape" } + , { CustomCursorColor , "CustomCursorColor" } + , { WordCharacters , "WordCharacters" } + , { TabBarPosition , "TabBarPosition" } + , { DefaultEncoding , "DefaultEncoding" } + , { (Property)0 , 0 } +}; + +QHash Profile::_propertyByName; +QHash Profile::_nameByProperty; + +void Profile::fillTableWithDefaultNames() +{ + static bool filledDefaults = false; + + if ( filledDefaults ) + return; + + const PropertyNamePair* iter = DefaultPropertyNames; + while ( iter->name != 0 ) + { + registerName(iter->property,iter->name); + iter++; + } + + filledDefaults = true; +} FallbackProfile::FallbackProfile() : Profile(0) @@ -122,21 +174,37 @@ bool Profile::isPropertySet(Property property) const bool Profile::isNameRegistered(const QString& name) { - return _propertyNames.contains(name); + // insert default names into table the first time this is called + fillTableWithDefaultNames(); + + return _propertyByName.contains(name); } Profile::Property Profile::lookupByName(const QString& name) { - return _propertyNames[name]; + // insert default names into table the first time this is called + fillTableWithDefaultNames(); + + return _propertyByName[name]; } +QString Profile::primaryNameForProperty(Property property) +{ + // insert default names into table the first time this is called + fillTableWithDefaultNames(); + return _nameByProperty[property]; +} QList Profile::namesForProperty(Property property) { - return _propertyNames.keys(property); + // insert default names into table the first time this is called + fillTableWithDefaultNames(); + + return QList() << primaryNameForProperty(property); } void Profile::registerName(Property property , const QString& name) { - _propertyNames.insert(name,property); + _propertyByName.insert(name,property); + _nameByProperty.insert(property,name); } QString KDE4ProfileWriter::getPath(const Profile* info) @@ -158,9 +226,11 @@ QString KDE4ProfileWriter::getPath(const Profile* info) return newPath; } -void KDE4ProfileWriter::writeStandardElement(KConfigGroup& group , char* name , const Profile* profile , +void KDE4ProfileWriter::writeStandardElement(KConfigGroup& group , const Profile* profile , Profile::Property attribute) { + QString name = Profile::primaryNameForProperty(attribute); + if ( profile->isPropertySet(attribute) ) group.writeEntry(name,profile->property(attribute)); } @@ -182,55 +252,55 @@ bool KDE4ProfileWriter::writeProfile(const QString& path , const Profile* profil if ( profile->isPropertySet(Profile::Directory) ) general.writeEntry("Directory",profile->defaultWorkingDirectory()); - writeStandardElement( general , "Icon" , profile , Profile::Icon ); + writeStandardElement( general , profile , Profile::Icon ); // Tab Titles - writeStandardElement( general , "LocalTabTitleFormat" , profile , Profile::LocalTabTitleFormat ); - writeStandardElement( general , "RemoteTabTitleFormat" , profile , Profile::RemoteTabTitleFormat ); + writeStandardElement( general , profile , Profile::LocalTabTitleFormat ); + writeStandardElement( general , profile , Profile::RemoteTabTitleFormat ); // Menu and Tab Bar - writeStandardElement( general , "TabBarMode" , profile , Profile::TabBarMode ); - writeStandardElement( general , "TabBarPosition" , profile , Profile::TabBarPosition ); - writeStandardElement( general , "ShowMenuBar" , profile , Profile::ShowMenuBar ); + writeStandardElement( general , profile , Profile::TabBarMode ); + writeStandardElement( general , profile , Profile::TabBarPosition ); + writeStandardElement( general , profile , Profile::ShowMenuBar ); // Keyboard KConfigGroup keyboard = config.group("Keyboard"); - writeStandardElement( keyboard , "KeyBindings" , profile , Profile::KeyBindings ); + writeStandardElement( keyboard , profile , Profile::KeyBindings ); // Appearance KConfigGroup appearance = config.group("Appearance"); - writeStandardElement( appearance , "ColorScheme" , profile , Profile::ColorScheme ); - writeStandardElement( appearance , "Font" , profile , Profile::Font ); + writeStandardElement( appearance , profile , Profile::ColorScheme ); + writeStandardElement( appearance , profile , Profile::Font ); // Scrolling KConfigGroup scrolling = config.group("Scrolling"); - writeStandardElement( scrolling , "HistoryMode" , profile , Profile::HistoryMode ); - writeStandardElement( scrolling , "HistorySize" , profile , Profile::HistorySize ); - writeStandardElement( scrolling , "ScrollBarPosition" , profile , Profile::ScrollBarPosition ); + writeStandardElement( scrolling , profile , Profile::HistoryMode ); + writeStandardElement( scrolling , profile , Profile::HistorySize ); + writeStandardElement( scrolling , profile , Profile::ScrollBarPosition ); // Terminal Features KConfigGroup terminalFeatures = config.group("Terminal Features"); - writeStandardElement( terminalFeatures , "FlowControl" , profile , Profile::FlowControlEnabled ); - writeStandardElement( terminalFeatures , "BlinkingCursor" , profile , Profile::BlinkingCursorEnabled ); + writeStandardElement( terminalFeatures , profile , Profile::FlowControlEnabled ); + writeStandardElement( terminalFeatures , profile , Profile::BlinkingCursorEnabled ); // Cursor KConfigGroup cursorOptions = config.group("Cursor Options"); - writeStandardElement( cursorOptions , "UseCustomCursorColor" , profile , Profile::UseCustomCursorColor ); - writeStandardElement( cursorOptions , "CustomCursorColor" , profile , Profile::CustomCursorColor ); - writeStandardElement( cursorOptions , "CursorShape" , profile , Profile::CursorShape ); + writeStandardElement( cursorOptions , profile , Profile::UseCustomCursorColor ); + writeStandardElement( cursorOptions , profile , Profile::CustomCursorColor ); + writeStandardElement( cursorOptions , profile , Profile::CursorShape ); // Interaction KConfigGroup interactionOptions = config.group("Interaction Options"); - writeStandardElement( interactionOptions , "WordCharacters" , profile , Profile::WordCharacters ); + writeStandardElement( interactionOptions , profile , Profile::WordCharacters ); // Encoding KConfigGroup encodingOptions = config.group("Encoding Options"); - writeStandardElement( encodingOptions , "DefaultEncoding" , profile , Profile::DefaultEncoding ); + writeStandardElement( encodingOptions , profile , Profile::DefaultEncoding ); return true; } @@ -262,63 +332,64 @@ bool KDE4ProfileReader::readProfile(const QString& path , Profile* profile) profile->setProperty(Profile::Arguments,shellCommand.arguments()); } - readStandardElement(general,"Directory",profile,Profile::Directory); + readStandardElement(general,profile,Profile::Directory); - readStandardElement(general,"Icon",profile,Profile::Icon); - readStandardElement(general,"LocalTabTitleFormat",profile,Profile::LocalTabTitleFormat); - readStandardElement(general,"RemoteTabTitleFormat",profile,Profile::RemoteTabTitleFormat); + readStandardElement(general,profile,Profile::Icon); + readStandardElement(general,profile,Profile::LocalTabTitleFormat); + readStandardElement(general,profile,Profile::RemoteTabTitleFormat); - readStandardElement(general,"TabBarMode",profile,Profile::TabBarMode); - readStandardElement(general,"TabBarPosition",profile,Profile::TabBarPosition); - readStandardElement(general,"ShowMenuBar",profile,Profile::ShowMenuBar); + readStandardElement(general,profile,Profile::TabBarMode); + readStandardElement(general,profile,Profile::TabBarPosition); + readStandardElement(general,profile,Profile::ShowMenuBar); // keyboard KConfigGroup keyboard = config.group("Keyboard"); - readStandardElement(keyboard,"KeyBindings",profile,Profile::KeyBindings); + readStandardElement(keyboard,profile,Profile::KeyBindings); // appearance KConfigGroup appearance = config.group("Appearance"); - readStandardElement(appearance,"ColorScheme",profile,Profile::ColorScheme); - readStandardElement(appearance,"Font",profile,Profile::Font); + readStandardElement(appearance,profile,Profile::ColorScheme); + readStandardElement(appearance,profile,Profile::Font); // scrolling KConfigGroup scrolling = config.group("Scrolling"); - readStandardElement(scrolling,"HistoryMode",profile,Profile::HistoryMode); - readStandardElement(scrolling,"HistorySize",profile,Profile::HistorySize); - readStandardElement(scrolling,"ScrollBarPosition",profile,Profile::ScrollBarPosition); + readStandardElement(scrolling,profile,Profile::HistoryMode); + readStandardElement(scrolling,profile,Profile::HistorySize); + readStandardElement(scrolling,profile,Profile::ScrollBarPosition); // terminal features KConfigGroup terminalFeatures = config.group("Terminal Features"); - readStandardElement(terminalFeatures,"FlowControl",profile,Profile::FlowControlEnabled); - readStandardElement(terminalFeatures,"BlinkingCursor",profile,Profile::BlinkingCursorEnabled); + readStandardElement(terminalFeatures,profile,Profile::FlowControlEnabled); + readStandardElement(terminalFeatures,profile,Profile::BlinkingCursorEnabled); // cursor settings KConfigGroup cursorOptions = config.group("Cursor Options"); - readStandardElement(cursorOptions,"UseCustomCursorColor",profile,Profile::UseCustomCursorColor); - readStandardElement(cursorOptions,"CustomCursorColor",profile,Profile::CustomCursorColor); - readStandardElement(cursorOptions,"CursorShape",profile,Profile::CursorShape); + readStandardElement(cursorOptions,profile,Profile::UseCustomCursorColor); + readStandardElement(cursorOptions,profile,Profile::CustomCursorColor); + readStandardElement(cursorOptions,profile,Profile::CursorShape); // interaction options KConfigGroup interactionOptions = config.group("Interaction Options"); - readStandardElement(interactionOptions,"WordCharacters",profile,Profile::WordCharacters); + readStandardElement(interactionOptions,profile,Profile::WordCharacters); // encoding KConfigGroup encodingOptions = config.group("Encoding Options"); - readStandardElement(encodingOptions,"DefaultEncoding",profile,Profile::DefaultEncoding); + readStandardElement(encodingOptions,profile,Profile::DefaultEncoding); return true; } template void KDE4ProfileReader::readStandardElement(const KConfigGroup& group , - char* name , Profile* info , Profile::Property property) { + QString name = Profile::primaryNameForProperty(property); + if ( group.hasKey(name) ) info->setProperty(property,group.readEntry(name,T())); } diff --git a/src/Profile.h b/src/Profile.h index 11e1123a..ff96fa21 100644 --- a/src/Profile.h +++ b/src/Profile.h @@ -329,7 +329,14 @@ public: * the Property enum, in the order the associations were created using * registerName() */ - static QList namesForProperty(Property property); + static QList namesForProperty(Property property); + + /** + * Returns the primary name for the specified @p property. + * TODO More documentation + */ + static QString primaryNameForProperty(Property property); + /** * Adds an association between a string @p name and a @p property. * Subsequent calls to lookupByName() with @p name as the argument @@ -338,12 +345,24 @@ public: static void registerName(Property property , const QString& name); private: + // fills the table with default names for profile properties + // the first time it is called. + // subsequent calls return immediately + static void fillTableWithDefaultNames(); + QHash _propertyValues; QPointer _parent; bool _hidden; - static QHash _propertyNames; + static QHash _propertyByName; + static QHash _nameByProperty; + struct PropertyNamePair + { + Property property; + const char* name; + }; + static const PropertyNamePair DefaultPropertyNames[]; }; /** @@ -388,7 +407,6 @@ public: private: template void readStandardElement(const KConfigGroup& group , - char* name , Profile* info , Profile::Property property); }; @@ -418,7 +436,6 @@ public: private: void writeStandardElement(KConfigGroup& group, - char* name, const Profile* profile, Profile::Property property); };