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

Konsole

Application.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2006-2007 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "Application.h"
00022 
00023 // std
00024 #include <iostream>
00025 
00026 #include "kdebug.h"
00027 
00028 // Qt
00029 #include <QHashIterator>
00030 #include <QFileInfo>
00031 
00032 // KDE
00033 #include <KAction>
00034 #include <KCmdLineArgs>
00035 #include <KIcon>
00036 #include <KWindowSystem>
00037 
00038 // Konsole
00039 #include "ColorScheme.h"
00040 #include "ProfileList.h"
00041 #include "SessionManager.h"
00042 #include "KeyboardTranslator.h"
00043 #include "MainWindow.h"
00044 #include "Session.h"
00045 #include "TerminalDisplay.h"
00046 #include "ViewManager.h"
00047 
00048 using namespace Konsole;
00049 
00050 #ifdef Q_WS_X11
00051 Application::Application(Display* display , Qt::HANDLE visual, Qt::HANDLE colormap)
00052     : KUniqueApplication(display,visual,colormap) 
00053 {
00054     init();
00055 }
00056 #endif
00057 
00058 Application::Application() : KUniqueApplication()
00059 {
00060     init();
00061 }
00062 
00063 void Application::init()
00064 {
00065     _sessionList = 0;
00066     _backgroundInstance = 0;
00067 
00068     // check for compositing functionality
00069     TerminalDisplay::setTransparencyEnabled( KWindowSystem::compositingActive() );
00070 
00071     setWindowIcon(KIcon("utilities-terminal"));
00072 }
00073 
00074 Application* Application::self()
00075 {
00076     return (Application*)KApp;
00077 }
00078 
00079 MainWindow* Application::newMainWindow()
00080 {
00081     MainWindow* window = new MainWindow();
00082     window->setSessionList( new ProfileList(true,window) );
00083 
00084     connect( window , SIGNAL(newSessionRequest(const QString&,const QString&,ViewManager*)), 
00085                       this , SLOT(createSession(const QString&,const QString&,ViewManager*)));
00086     connect( window , SIGNAL(newWindowRequest(const QString&,const QString&)),
00087                       this , SLOT(createWindow(const QString&,const QString&)) );
00088     connect( window->viewManager() , SIGNAL(viewDetached(Session*)) , this , SLOT(detachView(Session*)) );
00089 
00090     return window;
00091 }
00092 
00093 void Application::listAvailableProfiles()
00094 {
00095     QList<QString> paths = SessionManager::instance()->availableProfilePaths();
00096     QListIterator<QString> iter(paths);
00097 
00098     while ( iter.hasNext() )
00099     {
00100         QFileInfo info(iter.next());
00101         std::cout << info.baseName().toLocal8Bit().data() << std::endl;
00102     }
00103 }
00104 
00105 int Application::newInstance()
00106 {
00107     KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00108 
00109     // check for arguments to print help or other information to the terminal,
00110     // quit if such an argument was found
00111     if ( processHelpArgs(args) ) 
00112         return 0;
00113 
00114     // create a new window or use an existing one 
00115     MainWindow* window = processWindowArgs(args);
00116   
00117     // select profile to use 
00118     processProfileSelectArgs(args,window);
00119    
00120     // process various command-line options which cause a property of the 
00121     // default profile to be changed 
00122     processProfileChangeArgs(args,window);
00123 
00124     // create new session
00125     createSession( window->defaultProfile() , QString() , window->viewManager() );
00126 
00127     // if the background-mode argument is supplied, start the background session
00128     // ( or bring to the front if it already exists )
00129     if ( args->isSet("background-mode") )
00130         startBackgroundMode(window);
00131     else
00132         window->show();
00133 
00134     return 0;
00135 }
00136 
00137 MainWindow* Application::processWindowArgs(KCmdLineArgs* args)
00138 {
00139     MainWindow* window = 0;
00140     if ( args->isSet("new-tab") )
00141     {
00142         QListIterator<QWidget*> iter(topLevelWidgets());
00143         iter.toBack();
00144         while ( iter.hasPrevious() )
00145         {
00146             window = qobject_cast<MainWindow*>(iter.previous());
00147             if ( window != 0 )
00148                 break;
00149         } 
00150     }
00151     
00152     if ( window == 0 )
00153     {
00154         window = newMainWindow();
00155     }
00156     return window;
00157 }
00158 
00159 void Application::processProfileSelectArgs(KCmdLineArgs* args,MainWindow* window)
00160 {
00161     if ( args->isSet("profile") )
00162     {
00163         QString key = SessionManager::instance()->loadProfile(args->getOption("profile"));
00164         window->setDefaultProfile(key);
00165     }
00166 }
00167 
00168 bool Application::processHelpArgs(KCmdLineArgs* args)
00169 {
00170     if ( args->isSet("list-profiles") )
00171     {
00172         listAvailableProfiles();
00173         return true;
00174     }
00175     return false;
00176 }
00177 void Application::processProfileChangeArgs(KCmdLineArgs* args,MainWindow* window) 
00178 {
00179     SessionManager* const manager = SessionManager::instance();
00180 
00181     Profile* defaultProfile = manager->profile(window->defaultProfile());
00182     Profile* const newProfile = new Profile(defaultProfile);
00183     newProfile->setHidden(true);
00184 
00185     // run a custom command
00186     if ( args->isSet("e") ) 
00187     {
00188         QStringList arguments;
00189         arguments << args->getOption("e");
00190         for ( int i = 0 ; i < args->count() ; i++ )
00191            arguments << args->arg(i); 
00192    
00193         newProfile->setProperty(Profile::Command,args->getOption("e"));
00194         newProfile->setProperty(Profile::Arguments,arguments);
00195     }
00196 
00197     // change the initial working directory
00198     if( args->isSet("workdir") )
00199     {
00200         newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
00201     }
00202 
00203     // temporary changes to profile options specified on the command line
00204     foreach( QString value , args->getOptionList("p") ) 
00205     {
00206         ProfileCommandParser parser;
00207         
00208         QHashIterator<Profile::Property,QVariant> iter(parser.parse(value));
00209         while ( iter.hasNext() )
00210         {
00211             iter.next();
00212             newProfile->setProperty(iter.key(),iter.value());
00213         }        
00214     }
00215 
00216     if (newProfile->isEmpty())
00217     {
00218         delete newProfile;
00219     }
00220     else
00221     {
00222         window->setDefaultProfile(SessionManager::instance()->addProfile(newProfile));
00223     }   
00224 }
00225 
00226 void Application::startBackgroundMode(MainWindow* window)
00227 {
00228         if ( _backgroundInstance )
00229         {
00230             return;
00231         }
00232 
00233         KAction* action = new KAction(window);
00234         KShortcut shortcut = action->shortcut();
00235         action->setObjectName("Konsole Background Mode");
00236         //TODO - Customisable key sequence for this
00237         action->setGlobalShortcut( KShortcut(QKeySequence(Qt::Key_F12)) );
00238 
00239         _backgroundInstance = window;
00240         
00241         connect( action , SIGNAL(triggered()) , this , SLOT(toggleBackgroundInstance()) );
00242 }
00243 
00244 void Application::toggleBackgroundInstance()
00245 {
00246     Q_ASSERT( _backgroundInstance );
00247 
00248     if ( !_backgroundInstance->isVisible() )
00249     {
00250         _backgroundInstance->show();
00251         // ensure that the active terminal display has the focus.
00252         // without this, an odd problem occurred where the focus widgetwould change
00253         // each time the background instance was shown 
00254         _backgroundInstance->viewManager()->activeView()->setFocus();
00255     }
00256     else 
00257     {
00258         _backgroundInstance->hide();
00259     }
00260 }
00261 
00262 Application::~Application()
00263 {
00264 }
00265 
00266 void Application::detachView(Session* session)
00267 {
00268     MainWindow* window = newMainWindow();
00269     window->viewManager()->createView(session);
00270     window->show();
00271 }
00272 
00273 void Application::createWindow(const QString& key , const QString& directory)
00274 {
00275     MainWindow* window = newMainWindow();
00276     window->setDefaultProfile(key);
00277     createSession(key,directory,window->viewManager());
00278     window->show();
00279 }
00280 
00281 void Application::createSession(const QString& key , const QString& directory , ViewManager* view)
00282 {
00283     Session* session = SessionManager::instance()->createSession(key);
00284     if (!directory.isEmpty() && session->initialWorkingDirectory().isEmpty())
00285         session->setInitialWorkingDirectory(directory);
00286 
00287     // create view before starting the session process so that the session doesn't suffer
00288     // a change in terminal size right after the session starts.  some applications such as GNU Screen
00289     // and Midnight Commander don't like this happening
00290     view->createView(session);
00291     session->run();
00292 }
00293 
00294 #include "Application.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