Commit 193777e2 authored by Jekyll Wu's avatar Jekyll Wu

Split class SessionManager into ProfileMananger and SessionManager

REVIEW: 104425
parent 1ffeb4bb
......@@ -33,6 +33,7 @@
// Konsole
#include "SessionManager.h"
#include "ProfileManager.h"
#include "MainWindow.h"
#include "Session.h"
#include "ShellCommand.h"
......@@ -63,7 +64,7 @@ void Application::init()
Application::~Application()
{
SessionManager::instance()->closeAllSessions();
SessionManager::instance()->saveSettings();
ProfileManager::instance()->saveSettings();
}
MainWindow* Application::newMainWindow()
......@@ -224,11 +225,11 @@ void Application::createTabFromArgs(KCmdLineArgs* args, MainWindow* window,
Profile::Ptr baseProfile;
if (!profile.isEmpty()) {
baseProfile = SessionManager::instance()->loadProfile(profile);
baseProfile = ProfileManager::instance()->loadProfile(profile);
}
if (!baseProfile) {
// fallback to default profile
baseProfile = SessionManager::instance()->defaultProfile();
baseProfile = ProfileManager::instance()->defaultProfile();
}
Profile::Ptr newProfile = Profile::Ptr(new Profile(baseProfile));
......@@ -297,10 +298,10 @@ MainWindow* Application::processWindowArgs(KCmdLineArgs* args)
Profile::Ptr Application::processProfileSelectArgs(KCmdLineArgs* args)
{
Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
if (args->isSet("profile")) {
Profile::Ptr profile = SessionManager::instance()->loadProfile(
Profile::Ptr profile = ProfileManager::instance()->loadProfile(
args->getOption("profile"));
if (profile)
return profile;
......@@ -323,7 +324,7 @@ bool Application::processHelpArgs(KCmdLineArgs* args)
void Application::listAvailableProfiles()
{
QList<QString> paths = SessionManager::instance()->availableProfilePaths();
QList<QString> paths = ProfileManager::instance()->availableProfilePaths();
foreach ( const QString& path, paths) {
QFileInfo info(path);
......@@ -335,7 +336,7 @@ void Application::listAvailableProfiles()
void Application::listProfilePropertyInfo()
{
Profile::Ptr tempProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr tempProfile = ProfileManager::instance()->defaultProfile();
const QStringList names = tempProfile->propertiesInfoList();
foreach ( const QString& name, names) {
......
......@@ -87,6 +87,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/tests/CTestCustom.cmake)
ProfileList.cpp
ProfileReader.cpp
ProfileWriter.cpp
ProfileManager.cpp
Pty.cpp
RenameTabDialog.cpp
RenameTabWidget.cpp
......
......@@ -52,7 +52,7 @@
#include "KeyBindingEditor.h"
#include "KeyboardTranslator.h"
#include "KeyboardTranslatorManager.h"
#include "SessionManager.h"
#include "ProfileManager.h"
#include "ShellCommand.h"
#include "WindowSystemInfo.h"
#include "Enumeration.h"
......@@ -102,7 +102,7 @@ void EditProfileDialog::save()
if (_tempProfile->isEmpty())
return;
SessionManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
// ensure that these settings are not undone by a call
// to unpreview()
......@@ -595,7 +595,7 @@ void EditProfileDialog::unpreviewAll()
// undo any preview changes
if (!map.isEmpty())
SessionManager::instance()->changeProfile(_profile, map, false);
ProfileManager::instance()->changeProfile(_profile, map, false);
}
void EditProfileDialog::unpreview(int aProperty)
{
......@@ -606,7 +606,7 @@ void EditProfileDialog::unpreview(int aProperty)
QHash<Profile::Property, QVariant> map;
map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
SessionManager::instance()->changeProfile(_profile, map, false);
ProfileManager::instance()->changeProfile(_profile, map, false);
_previewedProperties.remove(aProperty);
}
......@@ -650,7 +650,7 @@ void EditProfileDialog::preview(int aProperty , const QVariant& value)
}
// temporary change to color scheme
SessionManager::instance()->changeProfile(_profile , map , false);
ProfileManager::instance()->changeProfile(_profile , map , false);
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
......
......@@ -51,6 +51,7 @@
#include "Session.h"
#include "ViewManager.h"
#include "SessionManager.h"
#include "ProfileManager.h"
#include "KonsoleSettings.h"
#include "settings/GeneralSettings.h"
#include "settings/TabBarSettings.h"
......@@ -367,7 +368,7 @@ void MainWindow::sessionListChanged(const QList<QAction*>& sessionActions)
newTabMenu->addAction(sessionAction);
// NOTE: defaultProfile seems to not work here, sigh.
Profile::Ptr profile = SessionManager::instance()->defaultProfile();
Profile::Ptr profile = ProfileManager::instance()->defaultProfile();
if (profile && profile->name() == sessionAction->text().remove('&')) {
sessionAction->setIcon(KIcon(profile->icon(), 0, QStringList("emblem-favorite")));
newTabMenu->setDefaultAction(sessionAction);
......@@ -379,7 +380,7 @@ void MainWindow::sessionListChanged(const QList<QAction*>& sessionActions)
} else {
KMenu* newTabMenu = _newTabMenuAction->menu();
newTabMenu->clear();
Profile::Ptr profile = SessionManager::instance()->defaultProfile();
Profile::Ptr profile = ProfileManager::instance()->defaultProfile();
// NOTE: Compare names w/o any '&'
if (sessionActions.size() == 2 && sessionActions[1]->text().remove('&') != profile->name()) {
......@@ -406,7 +407,7 @@ QString MainWindow::activeSessionDir() const
void MainWindow::openUrls(const QList<KUrl>& urls)
{
Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
foreach(const KUrl& url , urls) {
if (url.isLocalFile())
......@@ -419,7 +420,7 @@ void MainWindow::openUrls(const QList<KUrl>& urls)
void MainWindow::newTab()
{
Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
createSession(defaultProfile , activeSessionDir() );
}
......@@ -441,7 +442,7 @@ void MainWindow::cloneTab()
Session* MainWindow::createSession(Profile::Ptr profile, const QString& directory)
{
if (!profile)
profile = SessionManager::instance()->defaultProfile();
profile = ProfileManager::instance()->defaultProfile();
Session* session = SessionManager::instance()->createSession(profile);
......@@ -462,7 +463,7 @@ Session* MainWindow::createSession(Profile::Ptr profile, const QString& director
Session* MainWindow::createSSHSession(Profile::Ptr profile, const KUrl& url)
{
if (!profile)
profile = SessionManager::instance()->defaultProfile();
profile = ProfileManager::instance()->defaultProfile();
Session* session = SessionManager::instance()->createSession(profile);
......@@ -500,7 +501,7 @@ void MainWindow::setFocus()
void MainWindow::newWindow()
{
Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
emit newWindowRequest(defaultProfile , activeSessionDir());
}
......
......@@ -30,7 +30,7 @@
// Konsole
#include "EditProfileDialog.h"
#include "SessionManager.h"
#include "ProfileManager.h"
#include "ui_ManageProfilesDialog.h"
using namespace Konsole;
......@@ -43,7 +43,7 @@ ManageProfilesDialog::ManageProfilesDialog(QWidget* aParent)
setButtons(KDialog::Close);
connect(this, SIGNAL(finished()),
SessionManager::instance(), SLOT(saveSettings()));
ProfileManager::instance(), SLOT(saveSettings()));
_ui = new Ui::ManageProfilesDialog();
_ui->setupUi(mainWidget());
......@@ -60,13 +60,13 @@ ManageProfilesDialog::ManageProfilesDialog(QWidget* aParent)
populateTable();
// listen for changes to profiles
connect(SessionManager::instance(), SIGNAL(profileAdded(Profile::Ptr)), this,
connect(ProfileManager::instance(), SIGNAL(profileAdded(Profile::Ptr)), this,
SLOT(addItems(Profile::Ptr)));
connect(SessionManager::instance(), SIGNAL(profileRemoved(Profile::Ptr)), this,
connect(ProfileManager::instance(), SIGNAL(profileRemoved(Profile::Ptr)), this,
SLOT(removeItems(Profile::Ptr)));
connect(SessionManager::instance(), SIGNAL(profileChanged(Profile::Ptr)), this,
connect(ProfileManager::instance(), SIGNAL(profileChanged(Profile::Ptr)), this,
SLOT(updateItems(Profile::Ptr)));
connect(SessionManager::instance() ,
connect(ProfileManager::instance() ,
SIGNAL(favoriteStatusChanged(Profile::Ptr,bool)), this,
SLOT(updateFavoriteStatus(Profile::Ptr,bool)));
......@@ -117,7 +117,7 @@ void ManageProfilesDialog::itemDataChanged(QStandardItem* item)
{
if (item->column() == ShortcutColumn) {
QKeySequence sequence = QKeySequence::fromString(item->text());
SessionManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(),
ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(),
sequence);
} else if (item->column() == ProfileNameColumn) {
QString newName = item->text();
......@@ -128,7 +128,7 @@ void ManageProfilesDialog::itemDataChanged(QStandardItem* item)
QHash<Profile::Property, QVariant> properties;
properties.insert(Profile::Name, newName);
SessionManager::instance()->changeProfile(profile, properties);
ProfileManager::instance()->changeProfile(profile, properties);
}
}
......@@ -175,7 +175,7 @@ void ManageProfilesDialog::updateItemsForProfile(const Profile::Ptr profile, QLi
items[ProfileNameColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole);
// Favorite Status
const bool isFavorite = SessionManager::instance()->findFavorites().contains(profile);
const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile);
if (isFavorite)
items[FavoriteStatusColumn]->setData(KIcon("dialog-ok-apply"), Qt::DecorationRole);
else
......@@ -183,7 +183,7 @@ void ManageProfilesDialog::updateItemsForProfile(const Profile::Ptr profile, QLi
items[FavoriteStatusColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole);
// Shortcut
QString shortcut = SessionManager::instance()->shortcut(profile).toString();
QString shortcut = ProfileManager::instance()->shortcut(profile).toString();
items[ShortcutColumn]->setText(shortcut);
items[ShortcutColumn]->setData(QVariant::fromValue(profile), ShortcutRole);
}
......@@ -211,8 +211,8 @@ void ManageProfilesDialog::populateTable()
<< i18nc("@title:column Display profile in file menu", "Show in Menu")
<< i18nc("@title:column Profile shortcut text", "Shortcut"));
QList<Profile::Ptr> profiles = SessionManager::instance()->allProfiles();
SessionManager::instance()->sortProfiles(profiles);
QList<Profile::Ptr> profiles = ProfileManager::instance()->allProfiles();
ProfileManager::instance()->sortProfiles(profiles);
foreach(const Profile::Ptr& profile, profiles) {
addItems(profile);
......@@ -235,7 +235,7 @@ void ManageProfilesDialog::populateTable()
}
void ManageProfilesDialog::updateDefaultItem()
{
Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
const int rowCount = _sessionModel->rowCount();
for (int i = 0; i < rowCount; i++) {
......@@ -258,7 +258,7 @@ void ManageProfilesDialog::updateDefaultItem()
void ManageProfilesDialog::tableSelectionChanged(const QItemSelection&)
{
const int selectedRows = _ui->sessionTable->selectionModel()->selectedRows().count();
const SessionManager* manager = SessionManager::instance();
const ProfileManager* manager = ProfileManager::instance();
const bool isNotDefault = (selectedRows > 0) && currentProfile() != manager->defaultProfile();
const bool isDeletable = (selectedRows > 1) ||
(selectedRows == 1 && isProfileDeletable(currentProfile()));
......@@ -273,13 +273,13 @@ void ManageProfilesDialog::tableSelectionChanged(const QItemSelection&)
void ManageProfilesDialog::deleteSelected()
{
foreach(const Profile::Ptr & profile, selectedProfiles()) {
if (profile != SessionManager::instance()->defaultProfile())
SessionManager::instance()->deleteProfile(profile);
if (profile != ProfileManager::instance()->defaultProfile())
ProfileManager::instance()->deleteProfile(profile);
}
}
void ManageProfilesDialog::setSelectedAsDefault()
{
SessionManager::instance()->setDefaultProfile(currentProfile());
ProfileManager::instance()->setDefaultProfile(currentProfile());
// do not allow the new default session type to be removed
_ui->deleteProfileButton->setEnabled(false);
_ui->setAsDefaultButton->setEnabled(false);
......@@ -316,13 +316,13 @@ void ManageProfilesDialog::createProfile()
Profile::Ptr selectedProfile = currentProfile();
if (!selectedProfile)
sourceProfile = SessionManager::instance()->defaultProfile();
sourceProfile = ProfileManager::instance()->defaultProfile();
else
sourceProfile = selectedProfile;
Q_ASSERT(sourceProfile);
Profile::Ptr newProfile = Profile::Ptr(new Profile(SessionManager::instance()->fallbackProfile()));
Profile::Ptr newProfile = Profile::Ptr(new Profile(ProfileManager::instance()->fallbackProfile()));
newProfile->clone(sourceProfile, true);
newProfile->setProperty(Profile::Name, i18nc("@item This will be used as part of the file name", "New Profile"));
newProfile->setProperty(Profile::MenuIndex, QString("0"));
......@@ -332,9 +332,9 @@ void ManageProfilesDialog::createProfile()
dialog.data()->selectProfileName();
if (dialog.data()->exec() == QDialog::Accepted) {
SessionManager::instance()->addProfile(newProfile);
SessionManager::instance()->setFavorite(newProfile, true);
SessionManager::instance()->changeProfile(newProfile, newProfile->setProperties());
ProfileManager::instance()->addProfile(newProfile);
ProfileManager::instance()->setFavorite(newProfile, true);
ProfileManager::instance()->changeProfile(newProfile, newProfile->setProperties());
}
delete dialog.data();
}
......@@ -447,9 +447,9 @@ bool FavoriteItemDelegate::editorEvent(QEvent* aEvent, QAbstractItemModel*,
aEvent->type() == QEvent::KeyPress ||
aEvent->type() == QEvent::MouseButtonDblClick) {
Profile::Ptr profile = index.data(ManageProfilesDialog::ProfileKeyRole).value<Profile::Ptr>();
const bool isFavorite = SessionManager::instance()->findFavorites().contains(profile);
const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile);
SessionManager::instance()->setFavorite(profile, !isFavorite);
ProfileManager::instance()->setFavorite(profile, !isFavorite);
}
return true;
......
......@@ -39,6 +39,7 @@
#include "Session.h"
#include "SessionController.h"
#include "SessionManager.h"
#include "ProfileManager.h"
#include "TerminalDisplay.h"
#include "ViewManager.h"
......@@ -87,7 +88,7 @@ Part::Part(QWidget* parentWidget , QObject* parent, const QVariantList&)
Part::~Part()
{
SessionManager::instance()->saveSettings();
ProfileManager::instance()->saveSettings();
}
void Part::createGlobalActions()
......
......@@ -30,7 +30,7 @@
#include <KDebug>
// Konsole
#include "SessionManager.h"
#include "ProfileManager.h"
using Konsole::ProfileList;
......@@ -47,7 +47,7 @@ ProfileList::ProfileList(bool addShortcuts , QObject* parent)
_emptyListAction = new QAction(i18n("Default profile"), _group);
// TODO - Handle re-sorts when user changes profile names
SessionManager* manager = SessionManager::instance();
ProfileManager* manager = ProfileManager::instance();
QList<Profile::Ptr> favoriteProfiles = manager->sortedFavorites();
foreach ( const Profile::Ptr& profile, favoriteProfiles ) {
......@@ -128,7 +128,7 @@ void ProfileList::syncWidgetActions(QWidget* widget, bool sync)
}
void ProfileList::favoriteChanged(Profile::Ptr profile, bool isFavorite)
{
SessionManager* manager = SessionManager::instance();
ProfileManager* manager = ProfileManager::instance();
if (isFavorite) {
QAction* action = new QAction(_group);
......
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "ProfileManager.h"
// Qt
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QList>
#include <QtCore/QString>
// KDE
#include <KConfig>
#include <KGlobal>
#include <KDebug>
#include <KConfigGroup>
#include <KStandardDirs>
// Konsole
#include "ProfileReader.h"
#include "ProfileWriter.h"
using namespace Konsole;
static bool profileIndexLessThan(const Profile::Ptr& p1, const Profile::Ptr& p2)
{
return p1->menuIndexAsInt() <= p2->menuIndexAsInt();
}
static bool profileNameLessThan(const Profile::Ptr& p1, const Profile::Ptr& p2)
{
return QString::localeAwareCompare(p1->name(), p2->name()) <= 0;
}
static void sortByIndexProfileList(QList<Profile::Ptr>& list)
{
qStableSort(list.begin(), list.end(), profileIndexLessThan);
}
static void sortByNameProfileList(QList<Profile::Ptr>& list)
{
qStableSort(list.begin(), list.end(), profileNameLessThan);
}
ProfileManager::ProfileManager()
: _loadedAllProfiles(false)
, _loadedFavorites(false)
{
//load fallback profile
_fallbackProfile = Profile::Ptr(new FallbackProfile);
addProfile(_fallbackProfile);
//locate and load default profile
KSharedConfigPtr konsoleConfig = KSharedConfig::openConfig("konsolerc");
const KConfigGroup group = konsoleConfig->group("Desktop Entry");
const QString defaultProfileFileName = group.readEntry("DefaultProfile", "Shell.profile");
const QString path = KStandardDirs::locate("data", "konsole/" + defaultProfileFileName);
if (!path.isEmpty()) {
Profile::Ptr profile = loadProfile(path);
if (profile)
_defaultProfile = profile;
}
Q_ASSERT(_profiles.count() > 0);
Q_ASSERT(_defaultProfile);
// get shortcuts and paths of profiles associated with
// them - this doesn't load the shortcuts themselves,
// that is done on-demand.
loadShortcuts();
}
ProfileManager::~ProfileManager()
{
}
K_GLOBAL_STATIC(ProfileManager , theProfileManager)
ProfileManager* ProfileManager::instance()
{
return theProfileManager;
}
Profile::Ptr ProfileManager::loadProfile(const QString& shortPath)
{
// the fallback profile has a 'special' path name, "FALLBACK/"
if (shortPath == _fallbackProfile->path())
return _fallbackProfile;
QString path = shortPath;
// add a suggested suffix and relative prefix if missing
QFileInfo fileInfo(path);
if (fileInfo.isDir())
return Profile::Ptr();
if (fileInfo.suffix() != "profile")
path.append(".profile");
if (fileInfo.path().isEmpty() || fileInfo.path() == ".")
path.prepend(QString("konsole") + QDir::separator());
// if the file is not an absolute path, look it up
if (!fileInfo.isAbsolute())
path = KStandardDirs::locate("data", path);
// if the file is not found, return imediately
if ( path.isEmpty() ) {
return Profile::Ptr();
}
// check that we have not already loaded this profile
foreach ( const Profile::Ptr& profile, _profiles) {
if (profile->path() == path)
return profile;
}
// guard to prevent problems if a profile specifies itself as its parent
// or if there is recursion in the "inheritance" chain
// (eg. two profiles, A and B, specifying each other as their parents)
static QStack<QString> recursionGuard;
PopStackOnExit<QString> popGuardOnExit(recursionGuard);
if (recursionGuard.contains(path)) {
kWarning() << "Ignoring attempt to load profile recursively from" << path;
return _fallbackProfile;
} else
recursionGuard.push(path);
// load the profile
ProfileReader* reader = new KDE4ProfileReader;
Profile::Ptr newProfile = Profile::Ptr(new Profile(fallbackProfile()));
newProfile->setProperty(Profile::Path, path);
QString parentProfilePath;
bool result = reader->readProfile(path, newProfile, parentProfilePath);
if (!parentProfilePath.isEmpty()) {
Profile::Ptr parentProfile = loadProfile(parentProfilePath);
newProfile->setParent(parentProfile);
}
delete reader;
if (!result) {
kWarning() << "Could not load profile from " << path;
return Profile::Ptr();
} else {
addProfile(newProfile);
return newProfile;
}
}
QStringList ProfileManager::availableProfilePaths() const
{
KDE4ProfileReader kde4Reader;
QStringList profiles;
profiles += kde4Reader.findProfiles();
return profiles;
}
void ProfileManager::loadAllProfiles()
{
if (_loadedAllProfiles)
return;
const QStringList& paths = availableProfilePaths();
foreach ( const QString& path, paths) {
loadProfile(path);
}
_loadedAllProfiles = true;
}
void ProfileManager::sortProfiles(QList<Profile::Ptr>& list)
{
QList<Profile::Ptr> lackingIndices;
QList<Profile::Ptr> havingIndices;
for (int i = 0; i < list.size(); ++i) {
// dis-regard the fallback profile
if (list.at(i)->path() == _fallbackProfile->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 ProfileManager::saveSettings()
{
// save default profile
saveDefaultProfile();
// save shortcuts
saveShortcuts();
// save favorites
saveFavorites();
// ensure shortcuts/favorites settings are synced into disk
KSharedConfigPtr appConfig = KGlobal::config();
appConfig->sync();
// FIXME: another workaround for using 'DefaultProfile' entry in 'konsolerc'
KSharedConfigPtr konsoleConfig = KSharedConfig::openConfig("konsolerc");
konsoleConfig->sync();
}
QList<Profile::Ptr> ProfileManager::sortedFavorites()
{
QList<Profile::Ptr> favorites = findFavorites().toList();
sortProfiles(favorites);
return favorites;