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 <KIcon>
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 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
00110
00111 if ( processHelpArgs(args) )
00112 return 0;
00113
00114
00115 MainWindow* window = processWindowArgs(args);
00116
00117
00118 processProfileSelectArgs(args,window);
00119
00120
00121
00122 processProfileChangeArgs(args,window);
00123
00124
00125 createSession( window->defaultProfile() , QString() , window->viewManager() );
00126
00127
00128
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
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
00198 if( args->isSet("workdir") )
00199 {
00200 newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
00201 }
00202
00203
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
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
00252
00253
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
00288
00289
00290 view->createView(session);
00291 session->run();
00292 }
00293
00294 #include "Application.moc"