• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

SessionManager.cpp

Go to the documentation of this file.
00001 /*
00002     This source file is part of Konsole, a terminal emulator.
00003 
00004     Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301  USA.
00020 */
00021 
00022 // Own
00023 #include "SessionManager.h"
00024 
00025 // Qt
00026 #include <QtCore/QDir>
00027 #include <QtCore/QFileInfo>
00028 #include <QtCore/QList>
00029 #include <QtCore/QSignalMapper>
00030 #include <QtCore/QString>
00031 #include <QtCore/QTextCodec>
00032 
00033 // KDE
00034 #include <klocale.h>
00035 #include <kicon.h>
00036 #include <krun.h>
00037 #include <kshell.h>
00038 #include <kconfig.h>
00039 #include <kglobal.h>
00040 #include <kdebug.h>
00041 #include <kconfiggroup.h>
00042 #include <kstandarddirs.h>
00043 #include <kdesktopfile.h>
00044 
00045 // Konsole
00046 #include "ColorScheme.h"
00047 #include "Session.h"
00048 #include "History.h"
00049 #include "ShellCommand.h"
00050 
00051 using namespace Konsole;
00052 
00053 #if 0
00054 
00055 bool Profile::isAvailable() const
00056 {
00057     //TODO:  Is it necessary to cache the result of the search?
00058 
00059     QString binary = KRun::binaryName( command(true) , false );
00060     binary = KShell::tildeExpand(binary);
00061 
00062     QString fullBinaryPath = KGlobal::dirs()->findExe(binary);
00063 
00064     if ( fullBinaryPath.isEmpty() )
00065         return false;
00066     else
00067         return true;
00068 }
00069 #endif
00070 
00071 SessionManager::SessionManager()
00072     : _loadedAllProfiles(false)
00073     , _loadedFavorites(false)
00074 {
00075     //map finished() signals from sessions
00076     _sessionMapper = new QSignalMapper(this);
00077     connect( _sessionMapper , SIGNAL(mapped(QObject*)) , this ,
00078             SLOT(sessionTerminated(QObject*)) );
00079 
00080     //load fallback profile
00081     _fallbackProfile = Profile::Ptr(new FallbackProfile);
00082     addProfile(_fallbackProfile);
00083 
00084     //locate and load default profile
00085     KSharedConfigPtr appConfig = KGlobal::config();
00086     const KConfigGroup group = appConfig->group( "Desktop Entry" );
00087     QString defaultSessionFilename = group.readEntry("DefaultProfile","Shell.profile");
00088 
00089     QString path = KGlobal::dirs()->findResource("data","konsole/"+defaultSessionFilename);
00090     if (!path.isEmpty())
00091     {
00092         Profile::Ptr profile = loadProfile(path);
00093         if ( profile )
00094             _defaultProfile = profile;
00095     }
00096 
00097     Q_ASSERT( _types.count() > 0 );
00098     Q_ASSERT( _defaultProfile );
00099 
00100     // get shortcuts and paths of profiles associated with
00101     // them - this doesn't load the shortcuts themselves,
00102     // that is done on-demand.
00103     loadShortcuts();
00104 }
00105 Profile::Ptr SessionManager::loadProfile(const QString& shortPath)
00106 {
00107     // the fallback profile has a 'special' path name, "FALLBACK/"
00108     if (shortPath == _fallbackProfile->property<QString>(Profile::Path))
00109         return _fallbackProfile;
00110 
00111     QString path = shortPath;
00112 
00113     // add a suggested suffix and relative prefix if missing
00114     QFileInfo fileInfo(path);
00115     if ( fileInfo.suffix().isEmpty() )
00116         path.append(".profile");
00117     if ( fileInfo.path().isEmpty() || fileInfo.path() == "." )
00118         path.prepend(QString("konsole")+QDir::separator());
00119 
00120     // if the file is not an absolute path, look it up 
00121     if ( !fileInfo.isAbsolute() )
00122         path = KStandardDirs::locate("data",path);
00123 
00124     // check that we have not already loaded this profile
00125     QSetIterator<Profile::Ptr> iter(_types);
00126     while ( iter.hasNext() )
00127     {
00128         Profile::Ptr profile = iter.next();
00129         if ( profile->path() == path )
00130             return profile;
00131     }
00132 
00133     // guard to prevent problems if a profile specifies itself as its parent
00134     // or if there is recursion in the "inheritance" chain
00135     // (eg. two profiles, A and B, specifying each other as their parents)
00136     static QStack<QString> recursionGuard;
00137     PopStackOnExit<QString> popGuardOnExit(recursionGuard);
00138 
00139     if (recursionGuard.contains(path))
00140     {
00141         kWarning() << "Ignoring attempt to load profile recursively from" << path;
00142         return Profile::Ptr();
00143     }
00144     else
00145         recursionGuard.push(path);
00146 
00147     // load the profile
00148     ProfileReader* reader = 0;
00149     if ( path.endsWith(".desktop") )
00150         reader = 0; // new KDE3ProfileReader;
00151     else
00152         reader = new KDE4ProfileReader;
00153 
00154     if (!reader)
00155     {
00156         kWarning() << "Could not create loader to read profile from" << path;
00157         return Profile::Ptr();
00158     }
00159 
00160     Profile::Ptr newProfile = Profile::Ptr(new Profile(defaultProfile()));
00161     newProfile->setProperty(Profile::Path,path);
00162 
00163     QString parentProfilePath;
00164     bool result = reader->readProfile(path,newProfile,parentProfilePath);
00165 
00166     if ( !parentProfilePath.isEmpty() )
00167     {
00168         Profile::Ptr parentProfile = loadProfile(parentProfilePath);
00169         newProfile->setParent(parentProfile);
00170     }
00171 
00172     delete reader;
00173 
00174     if (!result)
00175     {
00176         kWarning() << "Could not load profile from " << path;
00177         return Profile::Ptr();
00178     }
00179     else
00180     {
00181         addProfile(newProfile);
00182         return newProfile;
00183     }
00184 }
00185 QStringList SessionManager::availableProfilePaths() const
00186 {
00187     KDE3ProfileReader kde3Reader;
00188     KDE4ProfileReader kde4Reader;
00189 
00190     QStringList profiles;
00191     profiles += kde3Reader.findProfiles();
00192     profiles += kde4Reader.findProfiles();
00193 
00194     return profiles;
00195 }
00196 
00197 void SessionManager::loadAllProfiles()
00198 {
00199     if ( _loadedAllProfiles )
00200         return;
00201 
00202     QStringList profiles = availableProfilePaths();
00203     
00204     QListIterator<QString> iter(profiles);
00205     while (iter.hasNext())
00206         loadProfile(iter.next());
00207 
00208     _loadedAllProfiles = true;
00209 }
00210 void SessionManager::saveState()
00211 {
00212     // save default profile
00213     setDefaultProfile( _defaultProfile );
00214 
00215     // save shortcuts
00216     saveShortcuts();
00217 
00218     // save favorites
00219     saveFavorites();
00220 }
00221 void SessionManager::closeAll()
00222 {
00223     // close remaining sessions
00224     foreach( Session* session , _sessions )
00225     {
00226         session->close();
00227     }
00228     _sessions.clear();  
00229 }
00230 SessionManager::~SessionManager()
00231 {
00232     if (_sessions.count() > 0)
00233     {
00234         kWarning() << "Konsole SessionManager destroyed with sessions still alive";
00235         // ensure that the Session doesn't later try to call back and do things to the 
00236         // SessionManager
00237         foreach(Session* session , _sessions)
00238             disconnect(session , 0 , this , 0);
00239     }
00240 }
00241 
00242 const QList<Session*> SessionManager::sessions()
00243 {
00244     return _sessions;
00245 }
00246 
00247 void SessionManager::updateSession(Session* session)
00248 {
00249     Profile::Ptr info = _sessionProfiles[session]; 
00250 
00251     Q_ASSERT( info );
00252 
00253     applyProfile(session,info,false);
00254 
00255     // FIXME - This may update a lot more than just the session
00256     // of interest. 
00257     emit sessionUpdated(session);
00258 }
00259 
00260 Session* SessionManager::createSession(Profile::Ptr info)
00261 {
00262     Session* session = 0;
00263     
00264     if (!info)
00265         info = defaultProfile();
00266    
00267     if (!_types.contains(info))
00268         addProfile(info);
00269 
00270     //configuration information found, create a new session based on this
00271     session = new Session();
00272     applyProfile(session,info,false);
00273 
00274     connect( session , SIGNAL(profileChangeCommandReceived(QString)) , this ,
00275             SLOT(sessionProfileCommandReceived(QString)) );
00276 
00277     //ask for notification when session dies
00278     _sessionMapper->setMapping(session,session);
00279     connect( session , SIGNAL(finished()) , _sessionMapper , 
00280              SLOT(map()) );
00281 
00282     //add session to active list
00283     _sessions << session;
00284     _sessionProfiles.insert(session,info);
00285 
00286     Q_ASSERT( session );
00287 
00288     return session;
00289 }
00290 
00291 void SessionManager::sessionTerminated(QObject* sessionObject)
00292 {
00293     Session* session = qobject_cast<Session*>(sessionObject);
00294 
00295     Q_ASSERT( session );
00296 
00297     _sessions.removeAll(session);
00298     session->deleteLater();
00299 }
00300 
00301 QList<Profile::Ptr> SessionManager::loadedProfiles() const
00302 {
00303     return _types.toList();
00304 }
00305 
00306 Profile::Ptr SessionManager::defaultProfile() const
00307 {
00308     return _defaultProfile;
00309 }
00310 Profile::Ptr SessionManager::fallbackProfile() const
00311 { return _fallbackProfile; }
00312 
00313 QString SessionManager::saveProfile(Profile::Ptr info)
00314 {
00315     ProfileWriter* writer = new KDE4ProfileWriter;
00316 
00317     QString newPath = writer->getPath(info);
00318 
00319     writer->writeProfile(newPath,info);
00320 
00321     delete writer;
00322 
00323     return newPath;
00324 }
00325 
00326 void SessionManager::changeProfile(Profile::Ptr info , 
00327                                    QHash<Profile::Property,QVariant> propertyMap, bool persistant)
00328 {
00329     Q_ASSERT(info); 
00330 
00331     // insert the changes into the existing Profile instance
00332     QListIterator<Profile::Property> iter(propertyMap.keys());
00333     while ( iter.hasNext() )
00334     {
00335         const Profile::Property property = iter.next();
00336         info->setProperty(property,propertyMap[property]);
00337     }
00338     
00339     // when changing a group, iterate through the profiles
00340     // in the group and call changeProfile() on each of them
00341     //
00342     // this is so that each profile in the group, the profile is 
00343     // applied, a change notification is emitted and the profile
00344     // is saved to disk
00345     ProfileGroup::Ptr group = info->asGroup();
00346     if (group)
00347     {
00348         foreach(Profile::Ptr profile, group->profiles())
00349             changeProfile(profile,propertyMap,persistant);
00350         return;
00351     }
00352     
00353     // apply the changes to existing sessions
00354     applyProfile(info,true);
00355 
00356     // notify the world about the change
00357     emit profileChanged(info);
00358 
00359     // save changes to disk, unless the profile is hidden, in which case
00360     // it has no file on disk 
00361     if ( persistant && !info->isHidden() )
00362     {
00363         info->setProperty(Profile::Path,saveProfile(info));
00364     }
00365 }
00366 void SessionManager::applyProfile(Profile::Ptr info , bool modifiedPropertiesOnly)
00367 {
00368     QListIterator<Session*> iter(_sessions);
00369     while ( iter.hasNext() )
00370     {
00371         Session* next = iter.next();
00372         if ( _sessionProfiles[next] == info )
00373             applyProfile(next,info,modifiedPropertiesOnly);        
00374     }
00375 }
00376 Profile::Ptr SessionManager::sessionProfile(Session* session) const
00377 {
00378     return _sessionProfiles[session];
00379 }
00380 void SessionManager::setSessionProfile(Session* session, Profile::Ptr profile)
00381 {
00382     _sessionProfiles[session] = profile;
00383     updateSession(session);
00384 }
00385 void SessionManager::applyProfile(Session* session, const Profile::Ptr info , bool modifiedPropertiesOnly)
00386 {
00387     Q_ASSERT(info);
00388 
00389     _sessionProfiles[session] = info;
00390 
00391     ShouldApplyProperty apply(info,modifiedPropertiesOnly);
00392 
00393     // Basic session settings
00394     if ( apply.shouldApply(Profile::Name) )
00395         session->setTitle(Session::NameRole,info->name());
00396 
00397     if ( apply.shouldApply(Profile::Command) )
00398         session->setProgram(info->command());
00399 
00400     if ( apply.shouldApply(Profile::Arguments) )
00401         session->setArguments(info->arguments());
00402 
00403     if ( apply.shouldApply(Profile::Directory) )
00404         session->setInitialWorkingDirectory(info->defaultWorkingDirectory());
00405 
00406     if ( apply.shouldApply(Profile::Environment) )
00407     {
00408         // add environment variable containing home directory of current profile
00409         // (if specified)
00410         QStringList environment = info->property<QStringList>(Profile::Environment);
00411         environment << QString("PROFILEHOME=%1").arg(info->defaultWorkingDirectory());
00412 
00413         session->setEnvironment(environment);
00414     }
00415 
00416     if ( apply.shouldApply(Profile::Icon) )
00417         session->setIconName(info->icon());
00418 
00419     // Key bindings
00420     if ( apply.shouldApply(Profile::KeyBindings) )
00421         session->setKeyBindings(info->property<QString>(Profile::KeyBindings));
00422 
00423     // Tab formats
00424     if ( apply.shouldApply(Profile::LocalTabTitleFormat) )
00425         session->setTabTitleFormat( Session::LocalTabTitle ,
00426                                     info->property<QString>(Profile::LocalTabTitleFormat));
00427     if ( apply.shouldApply(Profile::RemoteTabTitleFormat) )
00428         session->setTabTitleFormat( Session::RemoteTabTitle ,
00429                                     info->property<QString>(Profile::RemoteTabTitleFormat));
00430 
00431     // Scrollback / history
00432     if ( apply.shouldApply(Profile::HistoryMode) || apply.shouldApply(Profile::HistorySize) ) 
00433     {
00434         int mode = info->property<int>(Profile::HistoryMode);
00435         switch ((Profile::HistoryModeEnum)mode)
00436         {
00437             case Profile::DisableHistory:
00438                     session->setHistoryType( HistoryTypeNone() );
00439                 break;
00440             case Profile::FixedSizeHistory:
00441                 {
00442                     int lines = info->property<int>(Profile::HistorySize);
00443                     session->setHistoryType( HistoryTypeBuffer(lines) );
00444                 }
00445                 break;
00446             case Profile::UnlimitedHistory:
00447                     session->setHistoryType( HistoryTypeFile() );
00448                 break;
00449         }
00450     }
00451 
00452     // Terminal features
00453     if ( apply.shouldApply(Profile::FlowControlEnabled) )
00454         session->setFlowControlEnabled( info->property<bool>(Profile::FlowControlEnabled) );
00455 
00456     // Encoding
00457     if ( apply.shouldApply(Profile::DefaultEncoding) )
00458     {
00459         QByteArray name = info->property<QString>(Profile::DefaultEncoding).toUtf8();
00460         session->setCodec( QTextCodec::codecForName(name) );
00461     } 
00462 }
00463 
00464 void SessionManager::addProfile(Profile::Ptr type)
00465 {
00466     if ( _types.isEmpty() )
00467         _defaultProfile = type;
00468  
00469     _types.insert(type);
00470 
00471     emit profileAdded(type);
00472 }
00473 
00474 bool SessionManager::deleteProfile(Profile::Ptr type)
00475 {
00476     bool wasDefault = ( type == defaultProfile() );
00477 
00478     if ( type )
00479     {
00480         // try to delete the config file
00481         if ( type->isPropertySet(Profile::Path) && QFile::exists(type->path()) )
00482         {
00483             if (!QFile::remove(type->path()))
00484             {
00485                 kWarning() << "Could not delete profile: " << type->path()
00486                     << "The file is most likely in a directory which is read-only.";
00487 
00488                 return false;
00489             }
00490         }
00491 
00492         // remove from favorites, profile list, shortcut list etc.
00493         setFavorite(type,false);
00494         setShortcut(type,QKeySequence());
00495         _types.remove(type);
00496 
00497         // mark the profile as hidden so that it does not show up in the 
00498         // Manage Profiles dialog and is not saved to disk
00499         type->setHidden(true);
00500     }
00501 
00502     // if we just deleted the default session type,
00503     // replace it with a random type from the list
00504     if ( wasDefault )
00505     {
00506         setDefaultProfile( _types.toList().first() );
00507     }
00508 
00509     emit profileRemoved(type);
00510 
00511     return true; 
00512 }
00513 void SessionManager::setDefaultProfile(Profile::Ptr info)
00514 {
00515    Q_ASSERT ( _types.contains(info) );
00516 
00517    _defaultProfile = info;
00518 
00519    QString path = info->path();  
00520    
00521    if ( path.isEmpty() )
00522        path = KDE4ProfileWriter().getPath(info);
00523 
00524    QFileInfo fileInfo(path);
00525 
00526    KSharedConfigPtr config = KGlobal::config();
00527    KConfigGroup group = config->group("Desktop Entry");
00528    group.writeEntry("DefaultProfile",fileInfo.fileName());
00529 }
00530 QSet<Profile::Ptr> SessionManager::findFavorites() 
00531 {
00532     if (!_loadedFavorites)
00533         loadFavorites();
00534 
00535     return _favorites;
00536 }
00537 void SessionManager::setFavorite(Profile::Ptr info , bool favorite)
00538 {
00539     if (!_types.contains(info))
00540         addProfile(info);
00541 
00542     if ( favorite && !_favorites.contains(info) )
00543     {
00544         _favorites.insert(info);
00545         emit favoriteStatusChanged(info,favorite);
00546     }
00547     else if ( !favorite && _favorites.contains(info) )
00548     {
00549         _favorites.remove(info);
00550         emit favoriteStatusChanged(info,favorite);
00551     }
00552 }
00553 void SessionManager::loadShortcuts()
00554 {
00555     KSharedConfigPtr appConfig = KGlobal::config();
00556     KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
00557 
00558     QMap<QString,QString> entries = shortcutGroup.entryMap();
00559 
00560     QMapIterator<QString,QString> iter(entries);
00561     while ( iter.hasNext() )
00562     {
00563         iter.next();
00564 
00565         QKeySequence shortcut = QKeySequence::fromString(iter.key());
00566         QString profilePath = iter.value();
00567 
00568         ShortcutData data;
00569         data.profilePath = profilePath;
00570 
00571         _shortcuts.insert(shortcut,data);
00572     }
00573 }
00574 void SessionManager::saveShortcuts()
00575 {
00576     KSharedConfigPtr appConfig = KGlobal::config();
00577     KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
00578     shortcutGroup.deleteGroup();
00579 
00580     QMapIterator<QKeySequence,ShortcutData> iter(_shortcuts);
00581     while ( iter.hasNext() )
00582     {
00583         iter.next();
00584 
00585         QString shortcutString = iter.key().toString();
00586 
00587         shortcutGroup.writeEntry(shortcutString,
00588                 iter.value().profilePath);
00589     }    
00590 }
00591 void SessionManager::setShortcut(Profile::Ptr info , 
00592                                  const QKeySequence& keySequence )
00593 {
00594     QKeySequence existingShortcut = shortcut(info);
00595     _shortcuts.remove(existingShortcut);
00596 
00597     if (keySequence.isEmpty())
00598         return;
00599 
00600     ShortcutData data;
00601     data.profileKey = info;
00602     data.profilePath = info->path();
00603     // TODO - This won't work if the profile doesn't 
00604     // have a path yet
00605     _shortcuts.insert(keySequence,data);
00606 
00607     emit shortcutChanged(info,keySequence);
00608 }
00609 void SessionManager::loadFavorites()
00610 {
00611     KSharedConfigPtr appConfig = KGlobal::config();
00612     KConfigGroup favoriteGroup = appConfig->group("Favorite Profiles");
00613 
00614     QSet<QString> favoriteSet;
00615 
00616     if ( favoriteGroup.hasKey("Favorites") )
00617     {
00618        QStringList list = favoriteGroup.readEntry("Favorites", QStringList());
00619        favoriteSet = QSet<QString>::fromList(list);
00620     }
00621     else
00622     {
00623        // if there is no favorites key at all, mark the 
00624        // supplied 'Shell.profile' as the only favorite
00625        favoriteSet << "Shell.profile";
00626     }
00627 
00628     // look for favorites amongst those already loaded
00629     QSetIterator<Profile::Ptr> iter(_types);
00630     while ( iter.hasNext() )
00631     {
00632          Profile::Ptr profile = iter.next();
00633          const QString& path = profile->path();
00634          if ( favoriteSet.contains( path ) )
00635          {
00636              _favorites.insert( profile );
00637              favoriteSet.remove(path);
00638          }
00639     }
00640     // load any remaining favorites
00641     QSetIterator<QString> unloadedFavoriteIter(favoriteSet);
00642     while ( unloadedFavoriteIter.hasNext() )
00643     {
00644           Profile::Ptr profile = loadProfile(unloadedFavoriteIter.next());
00645           if (profile)
00646               _favorites.insert(profile);
00647     }
00648 
00649     _loadedFavorites = true;
00650 }
00651 void SessionManager::saveFavorites()
00652 {
00653     KSharedConfigPtr appConfig = KGlobal::config();
00654     KConfigGroup favoriteGroup = appConfig->group("Favorite Profiles");
00655 
00656     QStringList paths;
00657     QSetIterator<Profile::Ptr> keyIter(_favorites);
00658     while ( keyIter.hasNext() )
00659     {
00660         Profile::Ptr profile = keyIter.next();
00661 
00662         Q_ASSERT( _types.contains(profile) && profile );
00663 
00664         paths << profile->path();
00665     }
00666 
00667     favoriteGroup.writeEntry("Favorites",paths);
00668 }
00669 
00670 QList<QKeySequence> SessionManager::shortcuts() 
00671 {
00672     return _shortcuts.keys();
00673 }
00674 
00675 Profile::Ptr SessionManager::findByShortcut(const QKeySequence& shortcut)
00676 {
00677     Q_ASSERT( _shortcuts.contains(shortcut) );
00678 
00679     if ( !_shortcuts[shortcut].profileKey )
00680     {
00681         Profile::Ptr key = loadProfile(_shortcuts[shortcut].profilePath);
00682         if (!key)
00683         {
00684             _shortcuts.remove(shortcut);
00685             return Profile::Ptr();
00686         }
00687         _shortcuts[shortcut].profileKey = key;
00688     }
00689 
00690     return _shortcuts[shortcut].profileKey;
00691 }
00692 
00693 void SessionManager::sessionProfileCommandReceived(const QString& text)
00694 {
00695     // FIXME: This is inefficient, it creates a new profile instance for
00696     // each set of changes applied.  Instead a new profile should be created
00697     // only the first time changes are applied to a session
00698 
00699     Session* session = qobject_cast<Session*>(sender());
00700     Q_ASSERT( session );
00701 
00702     ProfileCommandParser parser;
00703     QHash<Profile::Property,QVariant> changes = parser.parse(text);
00704 
00705     Profile::Ptr newProfile = Profile::Ptr(new Profile(_sessionProfiles[session]));
00706     
00707     QHashIterator<Profile::Property,QVariant> iter(changes);
00708     while ( iter.hasNext() )
00709     {
00710         iter.next();
00711         newProfile->setProperty(iter.key(),iter.value());
00712     } 
00713 
00714     _sessionProfiles[session] = newProfile;
00715     applyProfile(newProfile,true);
00716     emit sessionUpdated(session);
00717 }
00718 
00719 QKeySequence SessionManager::shortcut(Profile::Ptr info) const
00720 {
00721     QMapIterator<QKeySequence,ShortcutData> iter(_shortcuts);
00722     while (iter.hasNext())
00723     {
00724         iter.next();
00725         if ( iter.value().profileKey == info 
00726              || iter.value().profilePath == info->path() )
00727             return iter.key();
00728     }
00729     
00730     return QKeySequence();
00731 }
00732 
00733 K_GLOBAL_STATIC( SessionManager , theSessionManager )
00734 SessionManager* SessionManager::instance()
00735 {
00736     return theSessionManager;
00737 }
00738 
00739 SessionListModel::SessionListModel(QObject* parent)
00740 : QAbstractListModel(parent)
00741 {
00742 }
00743 
00744 void SessionListModel::setSessions(const QList<Session*>& sessions)
00745 {
00746     _sessions = sessions;
00747 
00748     foreach(Session* session, sessions)
00749         connect(session,SIGNAL(finished()),this,SLOT(sessionFinished()));
00750 
00751     reset();
00752 }
00753 QVariant SessionListModel::data(const QModelIndex& index, int role) const
00754 {
00755     Q_ASSERT(index.isValid());
00756     
00757     int row = index.row();
00758     int column = index.column();
00759 
00760     Q_ASSERT( row >= 0 && row < _sessions.count() );
00761     Q_ASSERT( column >= 0 && column < 2 );
00762 
00763     switch (role)
00764     {
00765         case Qt::DisplayRole:
00766             if (column == 1)
00767                 return _sessions[row]->title(Session::DisplayedTitleRole);
00768             else if (column == 0)
00769                 return _sessions[row]->sessionId();
00770             break;
00771         case Qt::DecorationRole:
00772             if (column == 1)
00773                 return KIcon(_sessions[row]->iconName());
00774             else
00775                 return QVariant();
00776     }
00777 
00778     return QVariant();
00779 }
00780 QVariant SessionListModel::headerData(int section, Qt::Orientation orientation, 
00781                         int role) const
00782 {
00783     if (role != Qt::DisplayRole)
00784         return QVariant();
00785 
00786     if (orientation == Qt::Vertical)
00787         return QVariant();
00788     else
00789     {
00790         switch (section)
00791         {
00792             case 0:
00793                 return i18n("Number");
00794             case 1:
00795                 return i18n("Title");
00796             default:
00797                 return QVariant();
00798         }   
00799     }
00800 }
00801 
00802 int SessionListModel::columnCount(const QModelIndex&) const
00803 {
00804     return 2;
00805 }
00806 int SessionListModel::rowCount(const QModelIndex&) const
00807 {
00808     return _sessions.count();
00809 }
00810 QModelIndex SessionListModel::parent(const QModelIndex&) const
00811 {
00812     return QModelIndex();
00813 }
00814 void SessionListModel::sessionFinished()
00815 {
00816     Session* session = qobject_cast<Session*>(sender());
00817     int row = _sessions.indexOf(session);
00818     
00819     if (row != -1)
00820     {
00821         beginRemoveRows(QModelIndex(),row,row);
00822         sessionRemoved(session);
00823         _sessions.removeAt(row);
00824         endRemoveRows();
00825     }
00826 }
00827 QModelIndex SessionListModel::index(int row, int column, const QModelIndex& parent) const
00828 {
00829     if (hasIndex(row,column,parent))
00830         return createIndex(row,column,_sessions[row]);
00831     else
00832         return QModelIndex();
00833 }
00834 
00835 #include "SessionManager.moc"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal