Konsole
Application.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "Application.h"
00022
00023
00024 #include <iostream>
00025
00026 #include "kdebug.h"
00027
00028
00029 #include <QHashIterator>
00030 #include <QFileInfo>
00031
00032
00033 #include <KAction>
00034 #include <KCmdLineArgs>
00035 #include <KDebug>
00036 #include <KWindowSystem>
00037
00038
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
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
00109 if ((args->count() != 0) || !firstInstance || !isSessionRestored())
00110 {
00111
00112
00113 if ( processHelpArgs(args) )
00114 return 0;
00115
00116
00117 MainWindow* window = processWindowArgs(args);
00118
00119
00120 processProfileSelectArgs(args,window);
00121
00122
00123
00124 processProfileChangeArgs(args,window);
00125
00126
00127 Session* session = createSession( window->defaultProfile() , QString() , window->viewManager() );
00128 if ( !args->isSet("close") )
00129 session->setAutoClose(false);
00130
00131
00132
00133 if ( args->isSet("background-mode") )
00134 startBackgroundMode(window);
00135 else
00136 {
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
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
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
00222 if( args->isSet("workdir") )
00223 {
00224 newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
00225 }
00226
00227
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
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
00272
00273
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
00314
00315
00316 view->createView(session);
00317 session->run();
00318
00319 return session;
00320 }
00321
00322 #include "Application.moc"
00323
00324
00325
00326
00327
00328
00329
00330
00331