From bc9d65ec79e907dc955ea12a60953b02a00b2b75 Mon Sep 17 00:00:00 2001 From: Kurt Hindenburg Date: Sun, 26 Apr 2009 15:40:18 +0000 Subject: [PATCH] Add support for reading the profiles' menu index (favorites) from the profile file. This patch doesn't write the index, only reads. CCBUG: 18530 svn path=/trunk/KDE/kdebase/apps/konsole/; revision=959584 --- src/Profile.cpp | 10 ++++++ src/Profile.h | 11 ++++++- src/ProfileList.cpp | 14 ++------ src/SessionManager.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++ src/SessionManager.h | 13 +++++++- 5 files changed, 109 insertions(+), 14 deletions(-) diff --git a/src/Profile.cpp b/src/Profile.cpp index 93d51e730..0efaced48 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -80,6 +80,7 @@ const Profile::PropertyInfo Profile::DefaultPropertyNames[] = , { TabBarPosition , "TabBarPosition" , GENERAL_GROUP , QVariant::Int } , { StartInCurrentSessionDir , "StartInCurrentSessionDir" , GENERAL_GROUP , QVariant::Bool } , { ShowNewAndCloseTabButtons, "ShowNewAndCloseTabButtons" , GENERAL_GROUP , QVariant::Bool } + , { MenuIndex, "MenuIndex" , GENERAL_GROUP , QVariant::String } // Appearance , { Font , "Font" , APPEARANCE_GROUP , QVariant::Font } @@ -155,6 +156,7 @@ FallbackProfile::FallbackProfile() setProperty(ShowMenuBar,true); setProperty(StartInCurrentSessionDir,true); setProperty(ShowNewAndCloseTabButtons,false); + setProperty(MenuIndex,"0"); setProperty(KeyBindings,"default"); setProperty(ColorScheme,"DarkPastels"); @@ -276,6 +278,14 @@ void Profile::registerProperty(const PropertyInfo& info) _infoByProperty.insert(info.property,info); } +int Profile::menuIndexAsInt() const +{ + bool ok; + int index = menuIndex().toInt(&ok, 10); + if (ok) return index; + return 0; +} + QString KDE4ProfileWriter::getPath(const Profile::Ptr info) { QString newPath; diff --git a/src/Profile.h b/src/Profile.h index 7cc38f94f..503e26ae6 100644 --- a/src/Profile.h +++ b/src/Profile.h @@ -182,7 +182,11 @@ public: * currently active session. */ StartInCurrentSessionDir, /** (bool) Whether a 'New Tab' and 'Close Tab' buttons should be shown on the tab bar */ - ShowNewAndCloseTabButtons + ShowNewAndCloseTabButtons, + /** Index of profile in the File Menu + * In future, format will be #.#.# to account for levels + */ + MenuIndex }; /** @@ -354,6 +358,11 @@ public: /** Convenience method for property(Profile::Environment) */ QStringList environment() const { return property(Profile::Environment); } + /** Convenience method for property(Profile::MenuIndex) */ + QString menuIndex() const { return property(Profile::MenuIndex); } + + int menuIndexAsInt() const; + /** * Returns true if @p name has been associated with an element * from the Property enum or false otherwise. diff --git a/src/ProfileList.cpp b/src/ProfileList.cpp index ad588f3d1..c57f4db96 100644 --- a/src/ProfileList.cpp +++ b/src/ProfileList.cpp @@ -34,16 +34,6 @@ using namespace Konsole; -bool profileNameLessThan(const Profile::Ptr &p1, const Profile::Ptr &p2) -{ - return QString::localeAwareCompare(p1->name(), p2->name()) <= 0; -} - -static void sortProfileList(QList &list) -{ - qStableSort(list.begin(), list.end(), profileNameLessThan); -} - ProfileList::ProfileList(bool addShortcuts , QObject* parent) : QObject(parent) , _addShortcuts(addShortcuts) @@ -59,8 +49,8 @@ ProfileList::ProfileList(bool addShortcuts , QObject* parent) _emptyListAction->setEnabled(false); // TODO - Handle re-sorts when user changes profile names - QList list = manager->findFavorites().toList(); - sortProfileList(list); + + QList list = manager->sortedFavorites(); QListIterator iter(list); diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp index 00ba68d4f..682722704 100644 --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -68,6 +68,26 @@ bool Profile::isAvailable() const } #endif +bool profileIndexLessThan(const Profile::Ptr &p1, const Profile::Ptr &p2) +{ + return p1->menuIndexAsInt() <= p2->menuIndexAsInt(); +} + +bool profileNameLessThan(const Profile::Ptr &p1, const Profile::Ptr &p2) +{ + return QString::localeAwareCompare(p1->name(), p2->name()) <= 0; +} + +static void sortByIndexProfileList(QList &list) +{ + qStableSort(list.begin(), list.end(), profileIndexLessThan); +} + +static void sortByNameProfileList(QList &list) +{ + qStableSort(list.begin(), list.end(), profileNameLessThan); +} + SessionManager::SessionManager() : _loadedAllProfiles(false) , _loadedFavorites(false) @@ -207,6 +227,53 @@ void SessionManager::loadAllProfiles() _loadedAllProfiles = true; } + +void SessionManager::sortProfiles(QList &list) +{ + + QList lackingIndices; + QList havingIndices; + + for (int i = 0; i < list.size(); ++i) + { + // dis-regard the fallback profile + if (list.at(i)->path() == _fallbackProfile->property(Profile::Path)) + continue; + + if (list.at(i)->menuIndexAsInt() == 0) + lackingIndices.append(list.at(i)); + else + havingIndices.append(list.at(i)); + } + + // sort by index + sortByIndexProfileList(havingIndices); + + // sort alphabetically those w/o an index + sortByNameProfileList(lackingIndices); + + // Put those with indices in sequential order w/o any gaps + int i = 0; + for (i = 0; i < havingIndices.size(); ++i) + { + Profile::Ptr tempProfile = havingIndices.at(i); + tempProfile->setProperty(Profile::MenuIndex, QString::number(i+1)); + havingIndices.replace(i, tempProfile); + } + // Put those w/o indices in sequential order + for (int j = 0; j < lackingIndices.size(); ++j) + { + Profile::Ptr tempProfile = lackingIndices.at(j); + tempProfile->setProperty(Profile::MenuIndex, QString::number(j+1+i)); + lackingIndices.replace(j, tempProfile); + } + + // combine the 2 list: first those who had indices + list.clear(); + list.append(havingIndices); + list.append(lackingIndices); +} + void SessionManager::saveState() { // save default profile @@ -298,6 +365,14 @@ void SessionManager::sessionTerminated(QObject* sessionObject) session->deleteLater(); } +QList SessionManager::sortedFavorites() +{ + QList favorites = findFavorites().toList(); + + sortProfiles(favorites); + return favorites; +} + QList SessionManager::loadedProfiles() const { return _types.toList(); diff --git a/src/SessionManager.h b/src/SessionManager.h index 85c8a0a66..ccca52b19 100644 --- a/src/SessionManager.h +++ b/src/SessionManager.h @@ -66,7 +66,8 @@ public: * profiles. */ SessionManager(); - + void setMenuOrder(); + /** * Destroys the SessionManager. All running sessions should be closed (via closeAll()) and the * SessionManager's state should be saved via saveState() before the SessionManager is destroyed. @@ -91,6 +92,16 @@ public: */ QList loadedProfiles() const; + QList sortedFavorites(); + + /* + * Sorts the profile list by menuindex; those without an menuindex, sort by name. + * The menuindex list is first and then the non-menuindex list. + * + * @param list The profile list to sort + */ + void sortProfiles(QList &list); + /** * Searches for available profiles on-disk and returns a list * of paths of profiles which can be loaded.