From 942ecb745cf99e87335e5f03c2d6feb6718eda4e Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 2 Jun 2007 02:17:14 +0000 Subject: [PATCH] Make new sessions start in the same directory as the previously active session. Does not apply when creating a new tab from a specific profile. svn path=/trunk/KDE/kdebase/apps/konsole/; revision=670566 --- src/Application.cpp | 19 +++++++++++-------- src/Application.h | 4 ++-- src/MainWindow.cpp | 16 ++++++++++++---- src/MainWindow.h | 13 +++++++++++-- src/SessionController.cpp | 14 ++++++++++++++ src/SessionController.h | 1 + src/ViewProperties.cpp | 5 ++++- src/ViewProperties.h | 8 ++++++++ 8 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 9650cd416..36a0a5f67 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -78,10 +78,10 @@ MainWindow* Application::newMainWindow() MainWindow* window = new MainWindow(); window->setSessionList( new ProfileList(true,window) ); - connect( window , SIGNAL(newSessionRequest(const QString&,ViewManager*)), - this , SLOT(createSession(const QString&,ViewManager*))); - connect( window , SIGNAL(newWindowRequest(const QString&)), - this , SLOT(createWindow(const QString&)) ); + connect( window , SIGNAL(newSessionRequest(const QString&,const QString&,ViewManager*)), + this , SLOT(createSession(const QString&,const QString&,ViewManager*))); + connect( window , SIGNAL(newWindowRequest(const QString&,const QString&)), + this , SLOT(createWindow(const QString&,const QString&)) ); connect( window->viewManager() , SIGNAL(viewDetached(Session*)) , this , SLOT(detachView(Session*)) ); return window; @@ -118,7 +118,7 @@ int Application::newInstance() window->setDefaultProfile(key); } - createSession( window->defaultProfile() , window->viewManager() ); + createSession( window->defaultProfile() , QString() , window->viewManager() ); // if the background-mode argument is supplied, start the background session // ( or bring to the front if it already exists ) @@ -184,18 +184,21 @@ void Application::detachView(Session* session) window->show(); } -void Application::createWindow(const QString& key) +void Application::createWindow(const QString& key , const QString& directory) { MainWindow* window = newMainWindow(); window->setDefaultProfile(key); - createSession(key,window->viewManager()); + createSession(key,directory,window->viewManager()); window->show(); } -void Application::createSession(const QString& key , ViewManager* view) +void Application::createSession(const QString& key , const QString& directory , ViewManager* view) { Session* session = SessionManager::instance()->createSession(key); + if (!directory.isEmpty()) + session->setInitialWorkingDirectory(directory); + // create view before starting the session process so that the session doesn't suffer // a change in terminal size right after the session starts. some applications such as GNU Screen // and Midnight Commander don't like this happening diff --git a/src/Application.h b/src/Application.h index 4e7b2f8d8..6ef164754 100644 --- a/src/Application.h +++ b/src/Application.h @@ -69,8 +69,8 @@ public: static Application* self(); private slots: - void createSession(const QString& key, ViewManager* view); - void createWindow(const QString& key); + void createSession(const QString& key, const QString& directory , ViewManager* view); + void createWindow(const QString& key , const QString& directory); void detachView(Session* session); void toggleBackgroundInstance(); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3ed3ddc08..b8db85ac0 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -239,14 +239,22 @@ void MainWindow::sessionListChanged(const QList& actions) plugActionList("favorite-profiles",actions); } +QString MainWindow::activeSessionDir() const +{ + if ( _pluggedController ) + return _pluggedController->currentDir(); + else + return QString(); +} + void MainWindow::newTab() { - emit newSessionRequest( _defaultProfile , _viewManager); + emit newSessionRequest( _defaultProfile , activeSessionDir() , _viewManager); } void MainWindow::newWindow() { - emit newWindowRequest( _defaultProfile ); + emit newWindowRequest( _defaultProfile , activeSessionDir() ); } void MainWindow::showShortcutsDialog() @@ -257,7 +265,7 @@ void MainWindow::showShortcutsDialog() void MainWindow::newFromProfile(const QString& key) { - emit newSessionRequest(key,_viewManager); + emit newSessionRequest(key,QString(),_viewManager); } void MainWindow::showManageProfilesDialog() { @@ -269,7 +277,7 @@ void MainWindow::showRemoteConnectionDialog() { RemoteConnectionDialog dialog(this); if ( dialog.exec() == QDialog::Accepted ) - emit newSessionRequest(dialog.sessionKey(),_viewManager); + emit newSessionRequest(dialog.sessionKey(),QString(),_viewManager); } void MainWindow::setupWidgets() diff --git a/src/MainWindow.h b/src/MainWindow.h index af257187f..f7508307d 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -100,9 +100,13 @@ class MainWindow : public KXmlGuiWindow * Emitted by the main window to request the creation of a new session. * * @param key The key for the profile to use to create the new session. + * @param directory Initial working directory for the new session or empty + * if the default working directory associated with the profile should be used. * @param view The view manager owned by this main window */ - void newSessionRequest(const QString& key , ViewManager* view); + void newSessionRequest(const QString& key , + const QString& directory, + ViewManager* view); /** * Emitted by the main window to request the creation of a @@ -110,8 +114,12 @@ class MainWindow : public KXmlGuiWindow * * @param key The key for the profile to use to create the * first session in the new window. + * @param directory Initial working directory for the new window or empty + * if the default working directory associated with the profile should + * be used. */ - void newWindowRequest(const QString& key); + void newWindowRequest(const QString& key, + const QString& directory); private slots: void newTab(); @@ -129,6 +137,7 @@ class MainWindow : public KXmlGuiWindow private: void setupActions(); void setupWidgets(); + QString activeSessionDir() const; private: ViewManager* _viewManager; diff --git a/src/SessionController.cpp b/src/SessionController.cpp index 3b6f66001..e314e5da3 100644 --- a/src/SessionController.cpp +++ b/src/SessionController.cpp @@ -192,6 +192,20 @@ void SessionController::snapshot() _session->setTitle(Session::DisplayedTitleRole,_session->title(Session::NameRole)); } +QString SessionController::currentDir() const +{ + ProcessInfo* info = ProcessInfo::newInstance(_session->processId()); + info->update(); + + bool ok = false; + QString path = info->currentDir(&ok); + + if ( ok ) + return path; + else + return QString(); +} + KUrl SessionController::url() const { ProcessInfo* info = ProcessInfo::newInstance(_session->processId()); diff --git a/src/SessionController.h b/src/SessionController.h index 2633c6576..aac04fd98 100644 --- a/src/SessionController.h +++ b/src/SessionController.h @@ -111,6 +111,7 @@ public: // reimplemented virtual KUrl url() const; + virtual QString currentDir() const; // Reimplemented to watch for events happening to the view virtual bool eventFilter(QObject* watched , QEvent* event); diff --git a/src/ViewProperties.cpp b/src/ViewProperties.cpp index e67a03359..16ffa2b6e 100644 --- a/src/ViewProperties.cpp +++ b/src/ViewProperties.cpp @@ -33,7 +33,10 @@ KUrl ViewProperties::url() const { return KUrl(); } - +QString ViewProperties::currentDir() const +{ + return QString(); +} void ViewProperties::fireActivity() { emit activity(this); diff --git a/src/ViewProperties.h b/src/ViewProperties.h index 78bd0dbdd..d72575f0a 100644 --- a/src/ViewProperties.h +++ b/src/ViewProperties.h @@ -55,6 +55,14 @@ public: */ virtual KUrl url() const; + /** + * Returns the current directory associated with a view. + * This may be the same as url() + * + * The default implementation returns an empty string. + */ + virtual QString currentDir() const; + /** * A unique identifier representing the data displayed by the view associated with this * ViewProperties instance.