~ubuntu-branches/ubuntu/precise/konsole/precise

« back to all changes in this revision

Viewing changes to src/Application.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-16 13:14:43 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20111216131443-33q8agi3dii2gfqd
Tags: 4:4.7.90-0ubuntu1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
// std
24
24
#include <iostream>
25
25
 
26
 
#include "kdebug.h"
27
 
 
28
26
// Qt
29
 
#include <QHashIterator>
30
 
#include <QFileInfo>
31
 
#include <QDir>
 
27
#include <QtCore/QHashIterator>
 
28
#include <QtCore/QFileInfo>
 
29
#include <QtCore/QDir>
32
30
 
33
31
// KDE
34
32
#include <KAction>
35
33
#include <KCmdLineArgs>
 
34
#include <KUrl>
36
35
#include <KDebug>
37
36
#include <KWindowSystem>
38
37
 
55
54
 
56
55
void Application::init()
57
56
{
58
 
    _sessionList = 0;
59
57
    _backgroundInstance = 0;
60
58
 
61
59
    // check for compositing functionality
62
60
    TerminalDisplay::setTransparencyEnabled( KWindowSystem::compositingActive() );
 
61
 
63
62
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040600
64
63
    // this ensures that Ctrl and Meta are not swapped, so CTRL-C and friends
65
64
    // will work correctly in the terminal
66
65
    setAttribute(Qt::AA_MacDontSwapCtrlAndMeta);
 
66
 
 
67
    // KDE's menuBar()->isTopLevel() hasn't worked in a while.
 
68
    // For now, put menus inside Konsole window; this also make
 
69
    // the keyboard shortcut to show menus look reasonable.
 
70
    setAttribute(Qt::AA_DontUseNativeMenuBar);
67
71
#endif
68
72
}
69
73
 
77
81
    MainWindow* window = new MainWindow();
78
82
    window->setSessionList( new ProfileList(true,window) );
79
83
 
80
 
    connect( window , SIGNAL(newSessionRequest(Profile::Ptr,const QString&,ViewManager*)), 
81
 
                      this , SLOT(createSession(Profile::Ptr,const QString&,ViewManager*)));
82
 
    connect( window , SIGNAL(newSSHSessionRequest(Profile::Ptr,const KUrl&,ViewManager*)),
83
 
                      this , SLOT(createSSHSession(Profile::Ptr,const KUrl&,ViewManager*)));
84
 
    connect( window , SIGNAL(newWindowRequest(Profile::Ptr,const QString&)),
85
 
                      this , SLOT(createWindow(Profile::Ptr,const QString&)) );
86
 
    connect( window->viewManager() , SIGNAL(viewDetached(Session*)) , this , SLOT(detachView(Session*)) );
 
84
    connect( window , SIGNAL(newSessionRequest(Profile::Ptr,QString,ViewManager*)),
 
85
            this , SLOT(createSession(Profile::Ptr,QString,ViewManager*)));
 
86
    connect( window , SIGNAL(newSSHSessionRequest(Profile::Ptr,KUrl,ViewManager*)),
 
87
            this , SLOT(createSSHSession(Profile::Ptr,KUrl,ViewManager*)));
 
88
    connect( window , SIGNAL(newWindowRequest(Profile::Ptr,QString)),
 
89
            this , SLOT(createWindow(Profile::Ptr,QString)) );
 
90
    connect( window->viewManager() , SIGNAL(viewDetached(Session*)) ,
 
91
            this , SLOT(detachView(Session*)) );
87
92
 
88
93
    return window;
89
94
}
133
138
        // default profile to be changed
134
139
        processProfileChangeArgs(args,window);
135
140
 
136
 
        if ( !args->isSet("tabs-from-file") ) {
 
141
        if ( !args->isSet("tabs-from-file") )
 
142
        {
137
143
            // create new session
138
144
            Session* session = createSession(window->defaultProfile(),
139
145
                                             QString(),
141
147
            if ( !args->isSet("close") ) {
142
148
                session->setAutoClose(false);
143
149
            }
 
150
 
 
151
            // run a custom command, don't store it in the default profile.
 
152
            // Otherwise it will become the default for new tabs.
 
153
            if ( args->isSet("e") )
 
154
            {
 
155
                QStringList arguments;
 
156
                arguments << args->getOption("e");
 
157
                for ( int i = 0 ; i < args->count() ; i++ )
 
158
                    arguments << args->arg(i);
 
159
 
 
160
                QString exec = args->getOption("e");
 
161
                if (exec.startsWith(QLatin1String("./")))
 
162
                    exec = QDir::currentPath() + exec.mid(1);
 
163
 
 
164
                session->setProgram(exec);
 
165
                session->setArguments(arguments);
 
166
            }
144
167
        }
145
 
        else {
 
168
        else
 
169
        {
146
170
            // create new session(s) as described in file
147
171
            processTabsFromFileArgs(args, window);
148
172
        }
179
203
 * ;; is the token separator
180
204
 * # at the beginning of line results in line being ignored
181
205
 * tokens are title:, command:, profile: (not used currently)
182
 
 * Note that the title is static and the tab will close when the 
 
206
 * Note that the title is static and the tab will close when the
183
207
 * command is complete (do not use --noclose).  You can start new tabs.
184
208
 * Examples:
185
209
title: This is the title;; command: ssh jupiter
197
221
                   << tabsFileName.toLocal8Bit().data();
198
222
        quit();
199
223
    }
200
 
    
 
224
 
201
225
    unsigned int sessions = 0;
202
226
    while (!tabsFile.atEnd()) {
203
227
        QString lineString(tabsFile.readLine());
236
260
    QString command = tokens["command"];
237
261
    QString profile = tokens["profile"]; // currently not used
238
262
 
239
 
    // TODO: A lot of duplicate code below
 
263
    // FIXME: A lot of duplicate code below
240
264
 
241
265
    // Get the default profile
242
266
    Profile::Ptr defaultProfile = window->defaultProfile();
253
277
        newProfile->setProperty(Profile::Directory,args->getOption("workdir"));
254
278
    }
255
279
    if(!newProfile->isEmpty()) {
256
 
        window->setDefaultProfile(newProfile); 
257
 
    }    
258
 
                    
 
280
        window->setDefaultProfile(newProfile);
 
281
    }
 
282
 
259
283
    // Create the new session
260
284
    Session* session = createSession(window->defaultProfile(), QString(), window->viewManager());
261
285
    session->setTabTitleFormat(Session::LocalTabTitle, title);
267
291
    if (!window->testAttribute(Qt::WA_Resized)) {
268
292
        window->resize(window->sizeHint());
269
293
    }
270
 
                    
 
294
 
271
295
    // Reset the profile to default. Otherwise, the next manually
272
296
    // created tab would have the command above!
273
297
    newProfile = Profile::Ptr(new Profile(defaultProfile));
274
298
    newProfile->setHidden(true);
275
 
    window->setDefaultProfile(newProfile); 
276
 
                
 
299
    window->setDefaultProfile(newProfile);
 
300
 
277
301
}
278
302
 
279
303
MainWindow* Application::processWindowArgs(KCmdLineArgs* args)
288
312
            window = qobject_cast<MainWindow*>(iter.previous());
289
313
            if ( window != 0 )
290
314
                break;
291
 
        } 
 
315
        }
292
316
    }
293
 
    
 
317
 
294
318
    if ( window == 0 )
295
319
    {
296
320
        window = newMainWindow();
324
348
    }
325
349
    return false;
326
350
}
327
 
void Application::processProfileChangeArgs(KCmdLineArgs* args,MainWindow* window) 
 
351
void Application::processProfileChangeArgs(KCmdLineArgs* args,MainWindow* window)
328
352
{
329
353
    Profile::Ptr defaultProfile = window->defaultProfile();
330
354
    if (!defaultProfile)
331
355
        defaultProfile = SessionManager::instance()->defaultProfile();
332
356
    Profile::Ptr newProfile = Profile::Ptr(new Profile(defaultProfile));
333
357
    newProfile->setHidden(true);
334
 
    // run a custom command
335
 
    if ( args->isSet("e") ) 
336
 
    {
337
 
        QStringList arguments;
338
 
        arguments << args->getOption("e");
339
 
        for ( int i = 0 ; i < args->count() ; i++ )
340
 
           arguments << args->arg(i); 
341
 
 
342
 
        QString exec = args->getOption("e");
343
 
        if (exec.startsWith(QLatin1String("./")))
344
 
          exec = QDir::currentPath() + exec.mid(1);
345
 
        newProfile->setProperty(Profile::Command,exec);
346
 
        newProfile->setProperty(Profile::Arguments,arguments);
347
 
    }
348
358
 
349
359
    // change the initial working directory
350
360
    if( args->isSet("workdir") )
353
363
    }
354
364
 
355
365
    // temporary changes to profile options specified on the command line
356
 
    foreach( const QString &value , args->getOptionList("p") ) 
 
366
    foreach( const QString& value , args->getOptionList("p") )
357
367
    {
358
368
        ProfileCommandParser parser;
359
 
        
 
369
 
360
370
        QHashIterator<Profile::Property,QVariant> iter(parser.parse(value));
361
371
        while ( iter.hasNext() )
362
372
        {
363
373
            iter.next();
364
374
            newProfile->setProperty(iter.key(),iter.value());
365
 
        }        
 
375
        }
366
376
    }
367
377
 
368
378
    if (!newProfile->isEmpty())
369
379
    {
370
 
        window->setDefaultProfile(newProfile); 
371
 
    }    
 
380
        window->setDefaultProfile(newProfile);
 
381
    }
372
382
}
373
383
 
374
384
void Application::startBackgroundMode(MainWindow* window)
375
385
{
376
 
        if ( _backgroundInstance )
377
 
        {
378
 
            return;
379
 
        }
380
 
 
381
 
        KAction* action = new KAction(window);
382
 
        KShortcut shortcut = action->shortcut();
383
 
        action->setObjectName( QLatin1String("Konsole Background Mode" ));
384
 
        //TODO - Customizable key sequence for this
385
 
        action->setGlobalShortcut( KShortcut(QKeySequence(Qt::Key_F12)) );
386
 
 
387
 
        _backgroundInstance = window;
388
 
        
389
 
        connect( action , SIGNAL(triggered()) , this , SLOT(toggleBackgroundInstance()) );
 
386
    if ( _backgroundInstance )
 
387
    {
 
388
        return;
 
389
    }
 
390
 
 
391
    KAction* action = new KAction(window);
 
392
    action->setObjectName( QLatin1String("Konsole Background Mode" ));
 
393
    //TODO - Customizable key sequence for this
 
394
    action->setGlobalShortcut( KShortcut(QKeySequence(Qt::Key_F12)) );
 
395
 
 
396
    _backgroundInstance = window;
 
397
 
 
398
    connect( action , SIGNAL(triggered()) , this , SLOT(toggleBackgroundInstance()) );
390
399
}
391
400
 
392
401
void Application::toggleBackgroundInstance()
397
406
    {
398
407
        _backgroundInstance->show();
399
408
        // ensure that the active terminal display has the focus.
400
 
        // without this, an odd problem occurred where the focus widgetwould change
401
 
        // each time the background instance was shown 
 
409
        // without this, an odd problem occurred where the focus widget would change
 
410
        // each time the background instance was shown
402
411
        _backgroundInstance->viewManager()->activeView()->setFocus();
403
412
    }
404
 
    else 
 
413
    else
405
414
    {
406
415
        _backgroundInstance->hide();
407
416
    }
410
419
Application::~Application()
411
420
{
412
421
    SessionManager::instance()->closeAll();
413
 
    SessionManager::instance()->saveState();
 
422
    SessionManager::instance()->saveSettings();
414
423
}
415
424
 
416
425
void Application::detachView(Session* session)
428
437
    window->show();
429
438
}
430
439
 
431
 
Session* Application::createSession(Profile::Ptr profile, const QString& directory , ViewManager* view)
 
440
Session* Application::createSession(Profile::Ptr profile, const QString& directory , ViewManager* viewManager)
432
441
{
433
442
    if (!profile)
434
443
        profile = SessionManager::instance()->defaultProfile();
439
448
        session->setInitialWorkingDirectory(directory);
440
449
 
441
450
    // create view before starting the session process so that the session doesn't suffer
442
 
    // a change in terminal size right after the session starts.  some applications such as GNU Screen
 
451
    // a change in terminal size right after the session starts.  Some applications such as GNU Screen
443
452
    // and Midnight Commander don't like this happening
444
 
    view->createView(session);
445
 
    session->run();
 
453
    viewManager->createView(session);
446
454
 
447
455
    return session;
448
456
}
449
457
 
450
 
Session* Application::createSSHSession(Profile::Ptr profile, const KUrl& url, ViewManager* view)
 
458
Session* Application::createSSHSession(Profile::Ptr profile, const KUrl& url, ViewManager* viewManager)
451
459
{
452
460
    if (!profile)
453
461
        profile = SessionManager::instance()->defaultProfile();
466
474
    // create view before starting the session process so that the session doesn't suffer
467
475
    // a change in terminal size right after the session starts.  some applications such as GNU Screen
468
476
    // and Midnight Commander don't like this happening
469
 
    view->createView(session);
 
477
    viewManager->createView(session);
470
478
    session->run();
471
479
 
472
480
    return session;