Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konsole / src / ProfileList.cpp
blob2fa9ec24e6d705b4ba0511e7301d910b2a9fe84c
1 /*
2 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "ProfileList.h"
23 // Qt
24 #include <QtGui/QAction>
25 #include <QtGui/QActionGroup>
26 #include <KDebug>
28 // KDE
29 #include <KIcon>
30 #include <KLocalizedString>
32 // Konsole
33 #include "SessionManager.h"
35 using namespace Konsole;
37 ProfileList::ProfileList(bool addShortcuts , QObject* parent)
38 : QObject(parent)
39 , _addShortcuts(addShortcuts)
40 , _emptyListAction(0)
42 SessionManager* manager = SessionManager::instance();
44 // construct the list of favorite session types
45 _group = new QActionGroup(this);
47 // disabled action to be shown only when the list is empty
48 _emptyListAction = new QAction(i18n("No profiles available"),_group);
49 _emptyListAction->setEnabled(false);
51 // TODO Sort list in alphabetical order
52 QList<Profile::Ptr> list = manager->findFavorites().toList();
53 QListIterator<Profile::Ptr> iter(list);
55 while (iter.hasNext())
57 favoriteChanged(iter.next(),true);
60 connect( _group , SIGNAL(triggered(QAction*)) , this , SLOT(triggered(QAction*)) );
63 // listen for future changes to the session list
64 connect( manager , SIGNAL(favoriteStatusChanged(Profile::Ptr,bool)) , this ,
65 SLOT(favoriteChanged(Profile::Ptr,bool)) );
66 connect( manager , SIGNAL(shortcutChanged(Profile::Ptr,QKeySequence)) , this ,
67 SLOT(shortcutChanged(Profile::Ptr,QKeySequence)) );
68 connect( manager , SIGNAL(profileChanged(Profile::Ptr)) , this ,
69 SLOT(profileChanged(Profile::Ptr)) );
71 void ProfileList::updateEmptyAction()
73 Q_ASSERT( _group );
74 Q_ASSERT( _emptyListAction );
76 // show empty list action when it is the only action
77 // in the group
78 const bool showEmptyAction = _group->actions().count() == 1;
80 if ( showEmptyAction != _emptyListAction->isVisible() )
81 _emptyListAction->setVisible(showEmptyAction);
83 QAction* ProfileList::actionForKey(Profile::Ptr key) const
85 QListIterator<QAction*> iter(_group->actions());
86 while ( iter.hasNext() )
88 QAction* next = iter.next();
89 if ( next->data().value<Profile::Ptr>() == key )
90 return next;
92 return 0; // not found
95 void ProfileList::profileChanged(Profile::Ptr key)
97 QAction* action = actionForKey(key);
98 if ( action )
99 updateAction(action,key);
102 void ProfileList::updateAction(QAction* action , Profile::Ptr info)
104 Q_ASSERT(action);
105 Q_ASSERT(info);
107 action->setText(info->name());
108 action->setIcon(KIcon(info->icon()));
110 void ProfileList::shortcutChanged(Profile::Ptr info,const QKeySequence& sequence)
112 if ( !_addShortcuts )
113 return;
115 QAction* action = actionForKey(info);
117 if ( action )
119 action->setShortcut(sequence);
122 void ProfileList::syncWidgetActions(QWidget* widget, bool sync)
124 if (!sync)
126 _registeredWidgets.remove(widget);
127 return;
130 _registeredWidgets.insert(widget);
132 const QList<QAction*> currentActions = widget->actions();
133 foreach(QAction* currentAction, currentActions)
134 widget->removeAction(currentAction);
136 widget->addActions(_group->actions());
138 void ProfileList::favoriteChanged(Profile::Ptr info,bool isFavorite)
140 SessionManager* manager = SessionManager::instance();
142 if ( isFavorite )
144 QAction* action = new QAction(_group);
145 action->setData( QVariant::fromValue(info) );
147 if ( _addShortcuts )
149 action->setShortcut(manager->shortcut(info));
152 updateAction(action,info);
154 foreach(QWidget* widget,_registeredWidgets)
155 widget->addAction(action);
156 emit actionsChanged(_group->actions());
158 else
160 QAction* action = actionForKey(info);
162 if ( action )
164 _group->removeAction(action);
165 foreach(QWidget* widget,_registeredWidgets)
166 widget->removeAction(action);
167 emit actionsChanged(_group->actions());
171 updateEmptyAction();
173 void ProfileList::triggered(QAction* action)
175 emit profileSelected( action->data().value<Profile::Ptr>() );
178 QList<QAction*> ProfileList::actions()
180 return _group->actions();
183 #include "ProfileList.moc"