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
00107
00108
00109 if ( processHelpArgs(args) )
00110 return 0;
00111
00112
00113 MainWindow* window = processWindowArgs(args);
00114
00115
00116 processProfileSelectArgs(args,window);
00117
00118
00119
00120 processProfileChangeArgs(args,window);
00121
00122
00123 Session* session = createSession( window->defaultProfile() , QString() , window->viewManager() );
00124 if ( !args->isSet("close") )
00125 session->setAutoClose(false);
00126
00127
00128
00129 if ( args->isSet("background-mode") )
00130 startBackgroundMode(window);
00131 else
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
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
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
00215 if( args->isSet("workdir") )
00216 {
00217 newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
00218 }
00219
00220
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
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
00265
00266
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
00307
00308
00309 view->createView(session);
00310 session->run();
00311
00312 return session;
00313 }
00314
00315 #include "Application.moc"