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

Konsole

Application.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2006-2008 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 <KDebug>
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 
00072 Application* Application::self()
00073 {
00074     return (Application*)KApp;
00075 }
00076 
00077 MainWindow* Application::newMainWindow()
00078 {
00079     MainWindow* window = new MainWindow();
00080     window->setSessionList( new ProfileList(true,window) );
00081 
00082     connect( window , SIGNAL(newSessionRequest(Profile::Ptr,const QString&,ViewManager*)), 
00083                       this , SLOT(createSession(Profile::Ptr,const QString&,ViewManager*)));
00084     connect( window , SIGNAL(newWindowRequest(Profile::Ptr,const QString&)),
00085                       this , SLOT(createWindow(Profile::Ptr,const QString&)) );
00086     connect( window->viewManager() , SIGNAL(viewDetached(Session*)) , this , SLOT(detachView(Session*)) );
00087 
00088     return window;
00089 }
00090 
00091 void Application::listAvailableProfiles()
00092 {
00093     QList<QString> paths = SessionManager::instance()->availableProfilePaths();
00094     QListIterator<QString> iter(paths);
00095 
00096     while ( iter.hasNext() )
00097     {
00098         QFileInfo info(iter.next());
00099         std::cout << info.baseName().toLocal8Bit().data() << std::endl;
00100     }
00101 }
00102 
00103 int Application::newInstance()
00104 {
00105     KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00106 
00107     // check for arguments to print help or other information to the terminal,
00108     // quit if such an argument was found
00109     if ( processHelpArgs(args) ) 
00110         return 0;
00111 
00112     // create a new window or use an existing one 
00113     MainWindow* window = processWindowArgs(args);
00114   
00115     // select profile to use 
00116     processProfileSelectArgs(args,window);
00117    
00118     // process various command-line options which cause a property of the 
00119     // default profile to be changed 
00120     processProfileChangeArgs(args,window);
00121 
00122     // create new session
00123     Session* session = createSession( window->defaultProfile() , QString() , window->viewManager() );
00124     if ( !args->isSet("close") )
00125         session->setAutoClose(false);
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     {
00133         // Qt constrains top-level windows which have not been manually resized
00134         // (via QWidget::resize()) to a maximum of 2/3rds of the screen size.
00135         //
00136         // This means that the terminal display might not get the width/height
00137         // it asks for.  To work around this, the widget must be manually resized
00138         // to its sizeHint().
00139         //
00140         // This problem only affects the first time the application is run.  After
00141         // that KMainWindow will have manually resized the window to its saved size
00142         // at this point (so the Qt::WA_Resized attribute will be set)
00143         if (!window->testAttribute(Qt::WA_Resized))
00144             window->resize(window->sizeHint());
00145 
00146         window->show();
00147     }
00148 
00149     return 0;
00150 }
00151 
00152 MainWindow* Application::processWindowArgs(KCmdLineArgs* args)
00153 {
00154     MainWindow* window = 0;
00155     if ( args->isSet("new-tab") )
00156     {
00157         QListIterator<QWidget*> iter(topLevelWidgets());
00158         iter.toBack();
00159         while ( iter.hasPrevious() )
00160         {
00161             window = qobject_cast<MainWindow*>(iter.previous());
00162             if ( window != 0 )
00163                 break;
00164         } 
00165     }
00166     
00167     if ( window == 0 )
00168     {
00169         window = newMainWindow();
00170     }
00171     return window;
00172 }
00173 
00174 void Application::processProfileSelectArgs(KCmdLineArgs* args,MainWindow* window)
00175 {
00176     if ( args->isSet("profile") )
00177     {
00178         Profile::Ptr profile = SessionManager::instance()->loadProfile(args->getOption("profile"));
00179         if (!profile)
00180             profile = SessionManager::instance()->defaultProfile();
00181 
00182          window->setDefaultProfile(profile);
00183     }
00184 }
00185 
00186 bool Application::processHelpArgs(KCmdLineArgs* args)
00187 {
00188     if ( args->isSet("list-profiles") )
00189     {
00190         listAvailableProfiles();
00191         return true;
00192     }
00193     return false;
00194 }
00195 void Application::processProfileChangeArgs(KCmdLineArgs* args,MainWindow* window) 
00196 {
00197     Profile::Ptr defaultProfile = window->defaultProfile();
00198     if (!defaultProfile)
00199         defaultProfile = SessionManager::instance()->defaultProfile();
00200     Profile::Ptr newProfile = Profile::Ptr(new Profile(defaultProfile));
00201     newProfile->setHidden(true);
00202     // run a custom command
00203     if ( args->isSet("e") ) 
00204     {
00205         QStringList arguments;
00206         arguments << args->getOption("e");
00207         for ( int i = 0 ; i < args->count() ; i++ )
00208            arguments << args->arg(i); 
00209    
00210         newProfile->setProperty(Profile::Command,args->getOption("e"));
00211         newProfile->setProperty(Profile::Arguments,arguments);
00212     }
00213 
00214     // change the initial working directory
00215     if( args->isSet("workdir") )
00216     {
00217         newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
00218     }
00219 
00220     // temporary changes to profile options specified on the command line
00221     foreach( const QString &value , args->getOptionList("p") ) 
00222     {
00223         ProfileCommandParser parser;
00224         
00225         QHashIterator<Profile::Property,QVariant> iter(parser.parse(value));
00226         while ( iter.hasNext() )
00227         {
00228             iter.next();
00229             newProfile->setProperty(iter.key(),iter.value());
00230         }        
00231     }
00232 
00233     if (!newProfile->isEmpty())
00234     {
00235         window->setDefaultProfile(newProfile); 
00236     }   
00237 }
00238 
00239 void Application::startBackgroundMode(MainWindow* window)
00240 {
00241         if ( _backgroundInstance )
00242         {
00243             return;
00244         }
00245 
00246         KAction* action = new KAction(window);
00247         KShortcut shortcut = action->shortcut();
00248         action->setObjectName("Konsole Background Mode");
00249         //TODO - Customisable key sequence for this
00250         action->setGlobalShortcut( KShortcut(QKeySequence(Qt::Key_F12)) );
00251 
00252         _backgroundInstance = window;
00253         
00254         connect( action , SIGNAL(triggered()) , this , SLOT(toggleBackgroundInstance()) );
00255 }
00256 
00257 void Application::toggleBackgroundInstance()
00258 {
00259     Q_ASSERT( _backgroundInstance );
00260 
00261     if ( !_backgroundInstance->isVisible() )
00262     {
00263         _backgroundInstance->show();
00264         // ensure that the active terminal display has the focus.
00265         // without this, an odd problem occurred where the focus widgetwould change
00266         // each time the background instance was shown 
00267         _backgroundInstance->viewManager()->activeView()->setFocus();
00268     }
00269     else 
00270     {
00271         _backgroundInstance->hide();
00272     }
00273 }
00274 
00275 Application::~Application()
00276 {
00277     SessionManager::instance()->closeAll();
00278     SessionManager::instance()->saveState();
00279 }
00280 
00281 void Application::detachView(Session* session)
00282 {
00283     MainWindow* window = newMainWindow();
00284     window->viewManager()->createView(session);
00285     window->show();
00286 }
00287 
00288 void Application::createWindow(Profile::Ptr profile , const QString& directory)
00289 {
00290     MainWindow* window = newMainWindow();
00291     window->setDefaultProfile(profile);
00292     createSession(profile,directory,window->viewManager());
00293     window->show();
00294 }
00295 
00296 Session* Application::createSession(Profile::Ptr profile, const QString& directory , ViewManager* view)
00297 {
00298     if (!profile)
00299         profile = SessionManager::instance()->defaultProfile();
00300 
00301     Session* session = SessionManager::instance()->createSession(profile);
00302 
00303     if (!directory.isEmpty() && profile->property<bool>(Profile::StartInCurrentSessionDir))
00304         session->setInitialWorkingDirectory(directory);
00305 
00306     // create view before starting the session process so that the session doesn't suffer
00307     // a change in terminal size right after the session starts.  some applications such as GNU Screen
00308     // and Midnight Commander don't like this happening
00309     view->createView(session);
00310     session->run();
00311 
00312     return session;
00313 }
00314 
00315 #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