• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
Part.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301 USA.
18 */
19 
20 // Own
21 #include "Part.h"
22 
23 // Qt
24 #include <QtCore/QStringList>
25 #include <QtCore/QDir>
26 #include <QtGui/QKeyEvent>
27 
28 // KDE
29 #include <KAction>
30 #include <KActionCollection>
31 #include <KLocale>
32 #include <KPluginFactory>
33 #include <kde_file.h>
34 
35 // Konsole
36 #include "EditProfileDialog.h"
37 #include "Emulation.h"
38 #include "ManageProfilesDialog.h"
39 #include "Session.h"
40 #include "SessionController.h"
41 #include "SessionManager.h"
42 #include "ProfileManager.h"
43 #include "TerminalDisplay.h"
44 #include "ViewManager.h"
45 
46 using namespace Konsole;
47 
48 K_PLUGIN_FACTORY(KonsolePartFactory, registerPlugin<Konsole::Part>();)
49 K_EXPORT_PLUGIN(KonsolePartFactory("konsole"))
50 
51 Part::Part(QWidget* parentWidget , QObject* parent, const QVariantList&)
52  : KParts::ReadOnlyPart(parent)
53  , _viewManager(0)
54  , _pluggedController(0)
55  , _manageProfilesAction(0)
56 {
57  // make sure the konsole catalog is loaded
58  KGlobal::locale()->insertCatalog("konsole");
59  // make sure the libkonq catalog is loaded( needed for drag & drop )
60  KGlobal::locale()->insertCatalog("libkonq");
61 
62  // setup global actions
63  createGlobalActions();
64 
65  // create view widget
66  _viewManager = new ViewManager(this, actionCollection());
67  _viewManager->setNavigationMethod(ViewManager::NoNavigation);
68 
69  connect(_viewManager, SIGNAL(activeViewChanged(SessionController*)), this ,
70  SLOT(activeViewChanged(SessionController*)));
71  connect(_viewManager, SIGNAL(empty()), this, SLOT(terminalExited()));
72  connect(_viewManager, SIGNAL(newViewRequest()), this, SLOT(newTab()));
73 
74  _viewManager->widget()->setParent(parentWidget);
75 
76  setWidget(_viewManager->widget());
77  actionCollection()->addAssociatedWidget(_viewManager->widget());
78  foreach(QAction* action, actionCollection()->actions()) {
79  action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
80  }
81 
82  // Enable translucency support.
83  _viewManager->widget()->setAttribute(Qt::WA_TranslucentBackground, true);
84 
85  // create basic session
86  createSession();
87 }
88 
89 Part::~Part()
90 {
91  ProfileManager::instance()->saveSettings();
92 }
93 
94 void Part::createGlobalActions()
95 {
96  _manageProfilesAction = new KAction(i18n("Manage Profiles..."), this);
97  connect(_manageProfilesAction, SIGNAL(triggered()), this, SLOT(showManageProfilesDialog()));
98 }
99 
100 void Part::setupActionsForSession(SessionController* controller)
101 {
102  KActionCollection* collection = controller->actionCollection();
103  collection->addAction("manage-profiles", _manageProfilesAction);
104 }
105 
106 bool Part::openFile()
107 {
108  return false;
109 }
110 
111 void Part::terminalExited()
112 {
113  deleteLater();
114 }
115 
116 void Part::newTab()
117 {
118  createSession();
119 }
120 
121 Session* Part::activeSession() const
122 {
123  if (_viewManager->activeViewController()) {
124  Q_ASSERT(_viewManager->activeViewController()->session());
125 
126  return _viewManager->activeViewController()->session();
127  } else {
128  return 0;
129  }
130 }
131 void Part::startProgram(const QString& program,
132  const QStringList& arguments)
133 {
134  Q_ASSERT(activeSession());
135 
136  // do nothing if the session has already started running
137  if (activeSession()->isRunning())
138  return;
139 
140  if (!program.isEmpty() && !arguments.isEmpty()) {
141  activeSession()->setProgram(program);
142  activeSession()->setArguments(arguments);
143  }
144 
145  activeSession()->run();
146 }
147 
148 void Part::openTeletype(int fd)
149 {
150  Q_ASSERT(activeSession());
151 
152  activeSession()->openTeletype(fd);
153 }
154 
155 void Part::showShellInDir(const QString& dir)
156 {
157  Q_ASSERT(activeSession());
158 
159  // do nothing if the session has already started running
160  if (activeSession()->isRunning())
161  return;
162 
163  if (!dir.isEmpty())
164  activeSession()->setInitialWorkingDirectory(dir);
165 
166  activeSession()->run();
167 }
168 
169 void Part::sendInput(const QString& text)
170 {
171  Q_ASSERT(activeSession());
172  activeSession()->sendText(text);
173 }
174 
175 int Part::terminalProcessId()
176 {
177  Q_ASSERT(activeSession());
178 
179  return activeSession()->processId();
180 }
181 
182 int Part::foregroundProcessId()
183 {
184  Q_ASSERT(activeSession());
185 
186  if (activeSession()->isForegroundProcessActive()) {
187  return activeSession()->foregroundProcessId();
188  } else {
189  return -1;
190  }
191 }
192 
193 QString Part::foregroundProcessName()
194 {
195  Q_ASSERT(activeSession());
196 
197  if (activeSession()->isForegroundProcessActive()) {
198  return activeSession()->foregroundProcessName();
199  } else {
200  return "";
201  }
202 }
203 
204 QString Part::currentWorkingDirectory() const
205 {
206  Q_ASSERT(activeSession());
207 
208  return activeSession()->currentWorkingDirectory();
209 }
210 
211 void Part::createSession(const QString& profileName, const QString& directory)
212 {
213  Profile::Ptr profile = ProfileManager::instance()->defaultProfile();
214  if (!profileName.isEmpty())
215  profile = ProfileManager::instance()->loadProfile(profileName);
216 
217  Q_ASSERT(profile);
218 
219  Session* session = SessionManager::instance()->createSession(profile);
220 
221  // override the default directory specified in the profile
222  if (!directory.isEmpty() && profile->startInCurrentSessionDir())
223  session->setInitialWorkingDirectory(directory);
224 
225  _viewManager->createView(session);
226 }
227 
228 QStringList Part::profileNameList() const
229 {
230  return ProfileManager::instance()->availableProfileNames();
231 }
232 
233 void Part::activeViewChanged(SessionController* controller)
234 {
235  Q_ASSERT(controller);
236  Q_ASSERT(controller->view());
237 
238  // remove existing controller
239  if (_pluggedController) {
240  removeChildClient(_pluggedController);
241  disconnect(_pluggedController, SIGNAL(titleChanged(ViewProperties*)), this,
242  SLOT(activeViewTitleChanged(ViewProperties*)));
243  disconnect(_pluggedController, SIGNAL(currentDirectoryChanged(QString)), this,
244  SIGNAL(currentDirectoryChanged(QString)));
245  }
246 
247  // insert new controller
248  insertChildClient(controller);
249  setupActionsForSession(controller);
250 
251  connect(controller, SIGNAL(titleChanged(ViewProperties*)), this,
252  SLOT(activeViewTitleChanged(ViewProperties*)));
253  activeViewTitleChanged(controller);
254  connect(controller, SIGNAL(currentDirectoryChanged(QString)), this,
255  SIGNAL(currentDirectoryChanged(QString)));
256 
257  const char* displaySignal = SIGNAL(overrideShortcutCheck(QKeyEvent*,bool&));
258  const char* partSlot = SLOT(overrideTerminalShortcut(QKeyEvent*,bool&));
259 
260  disconnect(controller->view(), displaySignal, this, partSlot);
261  connect(controller->view(), displaySignal, this, partSlot);
262 
263  // set the current session's search bar
264  controller->setSearchBar(_viewManager->searchBar());
265 
266  _pluggedController = controller;
267 }
268 
269 void Part::overrideTerminalShortcut(QKeyEvent* event, bool& override)
270 {
271  // Shift+Insert is commonly used as the alternate shortcut for
272  // pasting in KDE apps(including konsole), so it deserves some
273  // special treatment.
274  if ((event->modifiers() & Qt::ShiftModifier) &&
275  (event->key() == Qt::Key_Insert)) {
276  override = false;
277  return;
278  }
279 
280  // override all shortcuts in the embedded terminal by default
281  override = true;
282  emit overrideShortcut(event, override);
283 }
284 
285 void Part::activeViewTitleChanged(ViewProperties* properties)
286 {
287  emit setWindowCaption(properties->title());
288 }
289 
290 void Part::showManageProfilesDialog()
291 {
292  showManageProfilesDialog(_viewManager->widget());
293 }
294 
295 void Part::showManageProfilesDialog(QWidget* parent)
296 {
297  ManageProfilesDialog* dialog = new ManageProfilesDialog(parent);
298  dialog->setAttribute(Qt::WA_DeleteOnClose);
299  dialog->setShortcutEditorVisible(false);
300  dialog->show();
301 }
302 
303 void Part::showEditCurrentProfileDialog(QWidget* parent)
304 {
305  Q_ASSERT(activeSession());
306 
307  EditProfileDialog* dialog = new EditProfileDialog(parent);
308  dialog->setAttribute(Qt::WA_DeleteOnClose);
309  dialog->setProfile(SessionManager::instance()->sessionProfile(activeSession()));
310  dialog->show();
311 }
312 
313 void Part::changeSessionSettings(const QString& text)
314 {
315  Q_ASSERT(activeSession());
316 
317  // send a profile change command, the escape code format
318  // is the same as the normal X-Term commands used to change the window title or icon,
319  // but with a magic value of '50' for the parameter which specifies what to change
320  QString command = QString("\033]50;%1\a").arg(text);
321 
322  sendInput(command);
323 }
324 
325 // Konqueror integration
326 bool Part::openUrl(const KUrl& aUrl)
327 {
328  if (url() == aUrl) {
329  emit completed();
330  return true;
331  }
332 
333  setUrl(aUrl);
334  emit setWindowCaption(aUrl.pathOrUrl());
335  //kdDebug() << "Set Window Caption to " << url.pathOrUrl();
336  emit started(0);
337 
338  if (aUrl.isLocalFile() /*&& b_openUrls*/) {
339  KDE_struct_stat buff;
340  if (KDE::stat(QFile::encodeName(aUrl.path()), &buff) == 0) {
341  QString text = (S_ISDIR(buff.st_mode) ? aUrl.path() : aUrl.directory());
342  showShellInDir(text);
343  } else {
344  showShellInDir(QDir::homePath());
345  }
346  } else {
347  showShellInDir(QDir::homePath());
348  }
349 
350  emit completed();
351  return true;
352 }
353 
354 void Part::setMonitorSilenceEnabled(bool enabled)
355 {
356  Q_ASSERT(activeSession());
357 
358  if (enabled) {
359  activeSession()->setMonitorSilence(true);
360  connect(activeSession(), SIGNAL(stateChanged(int)), this, SLOT(sessionStateChanged(int)), Qt::UniqueConnection);
361  } else {
362  activeSession()->setMonitorSilence(false);
363  disconnect(activeSession(), SIGNAL(stateChanged(int)), this, SLOT(sessionStateChanged(int)));
364  }
365 }
366 
367 void Part::setMonitorActivityEnabled(bool enabled)
368 {
369  Q_ASSERT(activeSession());
370 
371  if (enabled) {
372  activeSession()->setMonitorActivity(true);
373  connect(activeSession(), SIGNAL(stateChanged(int)), this, SLOT(sessionStateChanged(int)), Qt::UniqueConnection);
374  } else {
375  activeSession()->setMonitorActivity(false);
376  disconnect(activeSession(), SIGNAL(stateChanged(int)), this, SLOT(sessionStateChanged(int)));
377  }
378 }
379 
380 void Part::sessionStateChanged(int state)
381 {
382  if (state == NOTIFYSILENCE)
383  emit silenceDetected();
384  else if (state == NOTIFYACTIVITY)
385  emit activityDetected();
386 }
387 
388 #include "Part.moc"
Konsole::ManageProfilesDialog
A dialog which lists the available types of profiles and allows the user to add new profiles...
Definition: ManageProfilesDialog.h:50
Session.h
Konsole::Part::terminalProcessId
virtual int terminalProcessId()
Reimplemented from TerminalInterfaceV2.
Definition: Part.cpp:175
Konsole::NOTIFYSILENCE
Definition: Emulation.h:65
Konsole::Session::run
void run()
Starts the terminal session.
Definition: Session.cpp:427
Konsole::Part::overrideShortcut
void overrideShortcut(QKeyEvent *event, bool &override)
Emitted when the key sequence for a shortcut, which is also a valid terminal key sequence, is pressed while the terminal has focus.
Konsole::SessionManager::instance
static SessionManager * instance()
Returns the session manager instance.
Definition: SessionManager.cpp:69
QWidget
Konsole::Part::setMonitorSilenceEnabled
void setMonitorSilenceEnabled(bool enabled)
Toggles monitoring for silence in the active session.
Definition: Part.cpp:354
Konsole::Session::setMonitorActivity
Q_SCRIPTABLE void setMonitorActivity(bool)
Enables monitoring for activity in the session.
Definition: Session.cpp:1101
QKeyEvent::modifiers
Qt::KeyboardModifiers modifiers() const
Konsole::Session
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: Session.h:78
Konsole::SessionController
Provides the menu actions to manipulate a single terminal session and view pair.
Definition: SessionController.h:85
Konsole::ViewProperties::title
QString title() const
Returns the title associated with a view.
Definition: ViewProperties.cpp:85
Konsole::Session::foregroundProcessId
Q_SCRIPTABLE int foregroundProcessId()
Returns the process id of the terminal's foreground process.
Definition: Session.cpp:1393
QAction::setShortcutContext
void setShortcutContext(Qt::ShortcutContext context)
Part.h
Konsole::Part::startProgram
virtual void startProgram(const QString &program, const QStringList &arguments)
Reimplemented from TerminalInterface.
Definition: Part.cpp:131
Konsole::Part::showManageProfilesDialog
void showManageProfilesDialog(QWidget *parent)
Shows the dialog used to manage profiles in Konsole.
Definition: Part.cpp:295
Konsole::Part::~Part
virtual ~Part()
Definition: Part.cpp:89
Konsole::Session::openTeletype
void openTeletype(int masterFd)
Connect to an existing terminal.
Definition: Session.cpp:170
Konsole::ManageProfilesDialog::setShortcutEditorVisible
void setShortcutEditorVisible(bool visible)
Specifies whether the shortcut editor should be show.
Definition: ManageProfilesDialog.cpp:441
Konsole::Part::foregroundProcessId
virtual int foregroundProcessId()
Reimplemented from TerminalInterfaceV2.
Definition: Part.cpp:182
Emulation.h
Konsole::Part::foregroundProcessName
virtual QString foregroundProcessName()
Reimplemented from TerminalInterfaceV2.
Definition: Part.cpp:193
Konsole::SessionController::view
QPointer< TerminalDisplay > view()
Returns the view associated with this controller.
Definition: SessionController.h:112
Konsole::Part::showEditCurrentProfileDialog
void showEditCurrentProfileDialog(QWidget *parent)
Shows the dialog used to edit the profile used by the active session.
Definition: Part.cpp:303
Konsole::Part::setMonitorActivityEnabled
void setMonitorActivityEnabled(bool enabled)
Toggles monitoring for activity in the active session.
Definition: Part.cpp:367
Konsole::Part::openFile
virtual bool openFile()
Reimplemented from KParts::PartBase.
Definition: Part.cpp:106
Konsole::Session::setProgram
void setProgram(const QString &program)
Sets the program to be executed when run() is called.
Definition: Session.cpp:267
QDir::homePath
QString homePath()
Konsole::Part::activityDetected
void activityDetected()
Emitted when activity has been detected in the active session.
Konsole::Session::currentWorkingDirectory
QString currentWorkingDirectory()
Returns the current directory of the foreground process in the session.
Definition: Session.cpp:282
Konsole::EditProfileDialog
A dialog which allows the user to edit a profile.
Definition: EditProfileDialog.h:61
Konsole::ViewManager
Manages the terminal display widgets in a Konsole window or part.
Definition: ViewManager.h:66
Konsole::ProfileManager::defaultProfile
Profile::Ptr defaultProfile() const
Returns a Profile object describing the default profile.
Definition: ProfileManager.cpp:308
Konsole::Session::setMonitorSilence
Q_SCRIPTABLE void setMonitorSilence(bool)
Enables monitoring for silence in the session.
Definition: Session.cpp:1115
Konsole::NOTIFYACTIVITY
The emulation is currently receiving data from its terminal input.
Definition: Emulation.h:62
Konsole::Part::createSession
void createSession(const QString &profileName=QString(), const QString &directory=QString())
creates and run a session using the specified profile and directory
Definition: Part.cpp:211
Konsole::ProfileManager::saveSettings
void saveSettings()
Saves settings (favorites, shortcuts, default profile etc.) to disk.
Definition: ProfileManager.cpp:272
Konsole::Part::openUrl
virtual bool openUrl(const KUrl &url)
Definition: Part.cpp:326
Konsole::Part::openTeletype
void openTeletype(int ptyMasterFd)
Connects to an existing pseudo-teletype.
Definition: Part.cpp:148
TerminalDisplay.h
Konsole::ProfileManager::instance
static ProfileManager * instance()
Returns the profile manager instance.
Definition: ProfileManager.cpp:114
QObject
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
Konsole::Session::processId
int processId
Definition: Session.h:85
Konsole::Session::setInitialWorkingDirectory
void setInitialWorkingDirectory(const QString &dir)
Sets the initial working directory for the session when it is run This has no effect once the session...
Definition: Session.cpp:277
Konsole::SessionController::session
QPointer< Session > session()
Returns the session associated with this controller.
Definition: SessionController.h:108
ManageProfilesDialog.h
Konsole::Part
A re-usable terminal emulator component using the KParts framework which can be used to embed termina...
Definition: Part.h:48
EditProfileDialog.h
QString
Konsole::Part::currentWorkingDirectory
QString currentWorkingDirectory() const
Returns the current working directory of the active session.
Definition: Part.cpp:204
QStringList
Konsole::Part::currentDirectoryChanged
void currentDirectoryChanged(const QString &dir)
Emitted when the current working directory of the active session has changed.
Konsole::ViewProperties
Encapsulates user-visible information about the terminal session currently being displayed in a view...
Definition: ViewProperties.h:44
QKeyEvent::key
int key() const
Konsole::Profile::Ptr
KSharedPtr< Profile > Ptr
Definition: Profile.h:67
QKeyEvent
Konsole::ViewManager::createView
void createView(Session *session)
Creates a new view to display the output from and deliver input to session.
Definition: ViewManager.cpp:587
Konsole::Part::profileNameList
QStringList profileNameList() const
Returns a list of names of all available profiles.
Definition: Part.cpp:228
Konsole::SessionController::setSearchBar
void setSearchBar(IncrementalSearchBar *searchBar)
Sets the widget used for searches through the session's output.
Definition: SessionController.cpp:521
ProfileManager.h
ViewManager.h
QAction
Konsole::Session::setArguments
void setArguments(const QStringList &arguments)
Sets the command line arguments which the session's program will be passed when run() is called...
Definition: Session.cpp:272
Konsole::EditProfileDialog::setProfile
void setProfile(Profile::Ptr profile)
Initializes the dialog with the settings for the specified session type.
Definition: EditProfileDialog.cpp:177
Konsole::ViewManager::activeViewController
SessionController * activeViewController() const
Returns the controller for the active view.
Definition: ViewManager.cpp:533
Konsole::ViewManager::NoNavigation
The container has no navigation widget.
Definition: ViewManager.h:124
Konsole::Part::sendInput
virtual void sendInput(const QString &text)
Reimplemented from TerminalInterface.
Definition: Part.cpp:169
SessionManager.h
Konsole::ViewManager::searchBar
IncrementalSearchBar * searchBar() const
Returns the search bar.
Definition: ViewManager.cpp:538
Konsole::ProfileManager::availableProfileNames
QStringList availableProfileNames() const
Returns a list of names of all available profiles.
Definition: ProfileManager.cpp:202
Konsole::SessionManager::createSession
Session * createSession(Profile::Ptr profile=Profile::Ptr())
Creates a new session using the settings specified by the specified profile.
Definition: SessionManager.cpp:88
Konsole::ProfileManager::loadProfile
Profile::Ptr loadProfile(const QString &path)
Loads a profile from the specified path and registers it with the ProfileManager. ...
Definition: ProfileManager.cpp:119
Konsole::Session::foregroundProcessName
QString foregroundProcessName()
Returns the name of the current foreground process.
Definition: Session.cpp:1411
Konsole::ViewManager::widget
QWidget * widget() const
Return the main widget for the view manager which holds all of the views managed by this ViewManager ...
Definition: ViewManager.cpp:128
Konsole::Session::sendText
Q_SCRIPTABLE void sendText(const QString &text) const
Sends text to the current foreground terminal program.
Definition: Session.cpp:819
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
SessionController.h
Konsole::Part::changeSessionSettings
void changeSessionSettings(const QString &text)
Sends a profile change command to the active session.
Definition: Part.cpp:313
QFile::encodeName
QByteArray encodeName(const QString &fileName)
Konsole::Part::showShellInDir
virtual void showShellInDir(const QString &dir)
Reimplemented from TerminalInterface.
Definition: Part.cpp:155
Konsole::Part::silenceDetected
void silenceDetected()
Emitted when silence has been detected in the active session.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal