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

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.7
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