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

Konsole

  • sources
  • kde-4.12
  • applications
  • konsole
  • src
Application.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-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 "Application.h"
22 
23 // Qt
24 #include <QtCore/QHashIterator>
25 #include <QtCore/QFileInfo>
26 #include <QtCore/QDir>
27 
28 // KDE
29 #include <KAction>
30 #include <KActionCollection>
31 #include <KCmdLineArgs>
32 #include <KDebug>
33 
34 // Konsole
35 #include "SessionManager.h"
36 #include "ProfileManager.h"
37 #include "MainWindow.h"
38 #include "Session.h"
39 
40 using namespace Konsole;
41 
42 Application::Application() : KUniqueApplication()
43 {
44  init();
45 }
46 
47 void Application::init()
48 {
49 #if defined(Q_WS_MAC)
50  // this ensures that Ctrl and Meta are not swapped, so CTRL-C and friends
51  // will work correctly in the terminal
52  setAttribute(Qt::AA_MacDontSwapCtrlAndMeta);
53 
54  // KDE's menuBar()->isTopLevel() hasn't worked in a while.
55  // For now, put menus inside Konsole window; this also make
56  // the keyboard shortcut to show menus look reasonable.
57  setAttribute(Qt::AA_DontUseNativeMenuBar);
58 #endif
59 }
60 
61 Application::~Application()
62 {
63  SessionManager::instance()->closeAllSessions();
64  ProfileManager::instance()->saveSettings();
65 }
66 
67 MainWindow* Application::newMainWindow()
68 {
69  MainWindow* window = new MainWindow();
70 
71  connect(window, SIGNAL(newWindowRequest(Profile::Ptr,QString)),
72  this, SLOT(createWindow(Profile::Ptr,QString)));
73  connect(window, SIGNAL(viewDetached(Session*)),
74  this, SLOT(detachView(Session*)));
75 
76  return window;
77 }
78 
79 void Application::createWindow(Profile::Ptr profile, const QString& directory)
80 {
81  MainWindow* window = newMainWindow();
82  window->createSession(profile, directory);
83  window->show();
84 }
85 
86 void Application::detachView(Session* session)
87 {
88  MainWindow* window = newMainWindow();
89  window->createView(session);
90  // Since user is dragging and dropping, move dnd window to where
91  // the user has the cursor (correct multiple monitor setups).
92  window->move(QCursor::pos());
93  window->show();
94 }
95 
96 int Application::newInstance()
97 {
98  static bool firstInstance = true;
99 
100  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
101 
102  // handle session management
103  if ((args->count() != 0) || !firstInstance || !isSessionRestored()) {
104  // check for arguments to print help or other information to the
105  // terminal, quit if such an argument was found
106  if (processHelpArgs(args))
107  return 0;
108 
109  // create a new window or use an existing one
110  MainWindow* window = processWindowArgs(args);
111 
112  if (args->isSet("tabs-from-file")) {
113  // create new session(s) as described in file
114  processTabsFromFileArgs(args, window);
115  } else {
116  // select profile to use
117  Profile::Ptr baseProfile = processProfileSelectArgs(args);
118 
119  // process various command-line options which cause a property of the
120  // selected profile to be changed
121  Profile::Ptr newProfile = processProfileChangeArgs(args, baseProfile);
122 
123  // create new session
124  Session* session = window->createSession(newProfile, QString());
125 
126  if (!args->isSet("close")) {
127  session->setAutoClose(false);
128  }
129  }
130 
131  // Qt constrains top-level windows which have not been manually
132  // resized (via QWidget::resize()) to a maximum of 2/3rds of the
133  // screen size.
134  //
135  // This means that the terminal display might not get the width/
136  // height it asks for. To work around this, the widget must be
137  // manually resized to its sizeHint().
138  //
139  // This problem only affects the first time the application is run.
140  // run. After that KMainWindow will have manually resized the
141  // window to its saved size at this point (so the Qt::WA_Resized
142  // attribute will be set)
143  if (!window->testAttribute(Qt::WA_Resized))
144  window->resize(window->sizeHint());
145 
146  window->show();
147  }
148 
149  firstInstance = false;
150  args->clear();
151  return 0;
152 }
153 
154 /* Documentation for tab file:
155  *
156  * ;; is the token separator
157  * # at the beginning of line results in line being ignored.
158  * supported tokens are title, command and profile.
159  *
160  * Note that the title is static and the tab will close when the
161  * command is complete (do not use --noclose). You can start new tabs.
162  *
163  * Examples:
164 title: This is the title;; command: ssh jupiter
165 title: Top this!;; command: top
166 #this line is comment
167 command: ssh earth
168 profile: Zsh
169 */
170 void Application::processTabsFromFileArgs(KCmdLineArgs* args,
171  MainWindow* window)
172 {
173  // Open tab configuration file
174  const QString tabsFileName(args->getOption("tabs-from-file"));
175  QFile tabsFile(tabsFileName);
176  if (!tabsFile.open(QFile::ReadOnly)) {
177  kWarning() << "ERROR: Cannot open tabs file "
178  << tabsFileName.toLocal8Bit().data();
179  quit();
180  }
181 
182  unsigned int sessions = 0;
183  while (!tabsFile.atEnd()) {
184  QString lineString(tabsFile.readLine().trimmed());
185  if ((lineString.isEmpty()) || (lineString[0] == '#'))
186  continue;
187 
188  QHash<QString, QString> lineTokens;
189  QStringList lineParts = lineString.split(";;", QString::SkipEmptyParts);
190 
191  for (int i = 0; i < lineParts.size(); ++i) {
192  QString key = lineParts.at(i).section(':', 0, 0).trimmed().toLower();
193  QString value = lineParts.at(i).section(':', 1, -1).trimmed();
194  lineTokens[key] = value;
195  }
196  // should contain at least one of 'command' and 'profile'
197  if (lineTokens.contains("command") || lineTokens.contains("profile")) {
198  createTabFromArgs(args, window, lineTokens);
199  sessions++;
200  } else {
201  kWarning() << "Each line should contain at least one of 'command' and 'profile'.";
202  }
203  }
204  tabsFile.close();
205 
206  if (sessions < 1) {
207  kWarning() << "No valid lines found in "
208  << tabsFileName.toLocal8Bit().data();
209  quit();
210  }
211 }
212 
213 void Application::createTabFromArgs(KCmdLineArgs* args, MainWindow* window,
214  const QHash<QString, QString>& tokens)
215 {
216  const QString& title = tokens["title"];
217  const QString& command = tokens["command"];
218  const QString& profile = tokens["profile"];
219  const QString& workdir = tokens["workdir"];
220 
221  Profile::Ptr baseProfile;
222  if (!profile.isEmpty()) {
223  baseProfile = ProfileManager::instance()->loadProfile(profile);
224  }
225  if (!baseProfile) {
226  // fallback to default profile
227  baseProfile = ProfileManager::instance()->defaultProfile();
228  }
229 
230  Profile::Ptr newProfile = Profile::Ptr(new Profile(baseProfile));
231  newProfile->setHidden(true);
232 
233  // FIXME: the method of determining whether to use newProfile does not
234  // scale well when we support more fields in the future
235  bool shouldUseNewProfile = false;
236 
237  if (!command.isEmpty()) {
238  newProfile->setProperty(Profile::Command, command);
239  newProfile->setProperty(Profile::Arguments, command.split(' '));
240  shouldUseNewProfile = true;
241  }
242 
243  if (!title.isEmpty()) {
244  newProfile->setProperty(Profile::LocalTabTitleFormat, title);
245  newProfile->setProperty(Profile::RemoteTabTitleFormat, title);
246  shouldUseNewProfile = true;
247  }
248 
249  if (args->isSet("workdir")) {
250  newProfile->setProperty(Profile::Directory, args->getOption("workdir"));
251  shouldUseNewProfile = true;
252  }
253 
254  if (!workdir.isEmpty()) {
255  newProfile->setProperty(Profile::Directory, workdir);
256  shouldUseNewProfile = true;
257  }
258 
259  // Create the new session
260  Profile::Ptr theProfile = shouldUseNewProfile ? newProfile : baseProfile;
261  Session* session = window->createSession(theProfile, QString());
262 
263  if (!args->isSet("close")) {
264  session->setAutoClose(false);
265  }
266 
267  if (!window->testAttribute(Qt::WA_Resized)) {
268  window->resize(window->sizeHint());
269  }
270 
271  // FIXME: this ugly hack here is to make the session start running, so that
272  // its tab title is displayed as expected.
273  //
274  // This is another side effect of the commit fixing BKO 176902.
275  window->show();
276  window->hide();
277 }
278 
279 MainWindow* Application::processWindowArgs(KCmdLineArgs* args)
280 {
281  MainWindow* window = 0;
282  if (args->isSet("new-tab")) {
283  QListIterator<QWidget*> iter(topLevelWidgets());
284  iter.toBack();
285  while (iter.hasPrevious()) {
286  window = qobject_cast<MainWindow*>(iter.previous());
287  if (window != 0)
288  break;
289  }
290  }
291 
292  if (window == 0) {
293  window = newMainWindow();
294 
295  // override default menubar visibility
296  if (args->isSet("show-menubar")) {
297  window->setMenuBarInitialVisibility(true);
298  }
299  if (args->isSet("hide-menubar")) {
300  window->setMenuBarInitialVisibility(false);
301  }
302  if (args->isSet("fullscreen")) {
303  window->viewFullScreen(true);
304  }
305 
306  // override default tabbbar visibility
307  // FIXME: remove those magic number
308  // see ViewContainer::NavigationVisibility
309  if (args->isSet("show-tabbar")) {
310  // always show
311  window->setNavigationVisibility(0);
312  }
313  if (args->isSet("hide-tabbar")) {
314  // never show
315  window->setNavigationVisibility(2);
316  }
317  }
318  return window;
319 }
320 
321 Profile::Ptr Application::processProfileSelectArgs(KCmdLineArgs* args)
322 {
323  Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
324 
325  if (args->isSet("profile")) {
326  Profile::Ptr profile = ProfileManager::instance()->loadProfile(
327  args->getOption("profile"));
328  if (profile)
329  return profile;
330  } else if (args->isSet("fallback-profile")) {
331  Profile::Ptr profile = ProfileManager::instance()->loadProfile("FALLBACK/");
332  if (profile)
333  return profile;
334  }
335 
336  return defaultProfile;
337 }
338 
339 bool Application::processHelpArgs(KCmdLineArgs* args)
340 {
341  if (args->isSet("list-profiles")) {
342  listAvailableProfiles();
343  return true;
344  } else if (args->isSet("list-profile-properties")) {
345  listProfilePropertyInfo();
346  return true;
347  }
348  return false;
349 }
350 
351 void Application::listAvailableProfiles()
352 {
353  QStringList paths = ProfileManager::instance()->availableProfilePaths();
354 
355  foreach(const QString& path, paths) {
356  QFileInfo info(path);
357  printf("%s\n", info.completeBaseName().toLocal8Bit().constData());
358  }
359 
360  quit();
361 }
362 
363 void Application::listProfilePropertyInfo()
364 {
365  Profile::Ptr tempProfile = ProfileManager::instance()->defaultProfile();
366  const QStringList names = tempProfile->propertiesInfoList();
367 
368  foreach(const QString& name, names) {
369  printf("%s\n", name.toLocal8Bit().constData());
370  }
371 
372  quit();
373 }
374 
375 Profile::Ptr Application::processProfileChangeArgs(KCmdLineArgs* args, Profile::Ptr baseProfile)
376 {
377  bool shouldUseNewProfile = false;
378 
379  Profile::Ptr newProfile = Profile::Ptr(new Profile(baseProfile));
380  newProfile->setHidden(true);
381 
382  // change the initial working directory
383  if (args->isSet("workdir")) {
384  newProfile->setProperty(Profile::Directory, args->getOption("workdir"));
385  shouldUseNewProfile = true;
386  }
387 
388  // temporary changes to profile options specified on the command line
389  foreach(const QString & value , args->getOptionList("p")) {
390  ProfileCommandParser parser;
391 
392  QHashIterator<Profile::Property, QVariant> iter(parser.parse(value));
393  while (iter.hasNext()) {
394  iter.next();
395  newProfile->setProperty(iter.key(), iter.value());
396  }
397 
398  shouldUseNewProfile = true;
399  }
400 
401  // run a custom command
402  if (args->isSet("e")) {
403  QString commandExec = args->getOption("e");
404  QStringList commandArguments;
405 
406  //commandArguments << args->getOption("e");
407  commandArguments << commandExec;
408 
409  // Note: KCmdLineArgs::count() return the number of arguments
410  // that aren't options.
411  for ( int i = 0 ; i < args->count() ; i++ )
412  commandArguments << args->arg(i);
413 
414 
415  if (commandExec.startsWith(QLatin1String("./")))
416  commandExec = QDir::currentPath() + commandExec.mid(1);
417 
418  newProfile->setProperty(Profile::Command, commandExec);
419  newProfile->setProperty(Profile::Arguments, commandArguments);
420 
421  shouldUseNewProfile = true;
422  }
423 
424  if (shouldUseNewProfile) {
425  return newProfile;
426  } else {
427  return baseProfile;
428  }
429 }
430 
431 #include "Application.moc"
432 
Session.h
Konsole::SessionManager::instance
static SessionManager * instance()
Returns the session manager instance.
Definition: SessionManager.cpp:69
Konsole::Session
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: Session.h:67
Konsole::ProfileManager::availableProfilePaths
QStringList availableProfilePaths() const
Searches for available profiles on-disk and returns a list of paths of profiles which can be loaded...
Definition: ProfileManager.cpp:190
Konsole::ProfileCommandParser
Parses an input string consisting of property names and assigned values and returns a table of proper...
Definition: Profile.h:680
Konsole::Application::~Application
virtual ~Application()
Definition: Application.cpp:61
Application.h
Konsole::MainWindow::setMenuBarInitialVisibility
void setMenuBarInitialVisibility(bool visible)
Set the initial visibility of the menubar.
Definition: MainWindow.cpp:761
Konsole::Profile::RemoteTabTitleFormat
(QString) The format used for tab titles when the session is running a remote command (eg...
Definition: Profile.h:116
MainWindow.h
Konsole::Application::newInstance
virtual int newInstance()
Creates a new main window and opens a default terminal session.
Definition: Application.cpp:96
Konsole::Application::newMainWindow
MainWindow * newMainWindow()
Creates a new, empty main window and connects to its newSessionRequest() and newWindowRequest() signa...
Definition: Application.cpp:67
Konsole::Profile
Represents a terminal set-up which can be used to set the initial state of new terminal sessions or a...
Definition: Profile.h:60
Konsole::Profile::Directory
(QString) The initial working directory for sessions created using this profile.
Definition: Profile.h:108
Konsole::Profile::Command
(QString) The command to execute ( excluding arguments ) when creating a new terminal session using t...
Definition: Profile.h:93
Konsole::ProfileManager::defaultProfile
Profile::Ptr defaultProfile() const
Returns a Profile object describing the default profile.
Definition: ProfileManager.cpp:308
Konsole::ProfileManager::saveSettings
void saveSettings()
Saves settings (favorites, shortcuts, default profile etc.) to disk.
Definition: ProfileManager.cpp:272
Konsole::MainWindow::createView
void createView(Session *session)
create view for the specified session
Definition: MainWindow.cpp:503
Konsole::ProfileManager::instance
static ProfileManager * instance()
Returns the profile manager instance.
Definition: ProfileManager.cpp:114
Konsole::SessionManager::closeAllSessions
void closeAllSessions()
Kill all running sessions.
Definition: SessionManager.cpp:74
Konsole::Profile::Arguments
(QStringList) The arguments which are passed to the program specified by the Command property when cr...
Definition: Profile.h:98
Konsole::MainWindow::viewFullScreen
void viewFullScreen(bool fullScreen)
Definition: MainWindow.cpp:346
Konsole::MainWindow
The main window.
Definition: MainWindow.h:57
Konsole::ProfileCommandParser::parse
QHash< Profile::Property, QVariant > parse(const QString &input)
Parses an input string consisting of property names and assigned values and returns a table of proper...
Definition: Profile.cpp:307
Konsole::Profile::Ptr
KSharedPtr< Profile > Ptr
Definition: Profile.h:67
ProfileManager.h
Konsole::Session::setAutoClose
void setAutoClose(bool close)
Specifies whether to close the session automatically when the terminal process terminates.
Definition: Session.cpp:1136
KUniqueApplication
SessionManager.h
Konsole::Application::Application
Application()
Constructs a new Konsole application.
Definition: Application.cpp:42
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::MainWindow::setNavigationVisibility
void setNavigationVisibility(int visibility)
Definition: MainWindow.cpp:697
Konsole::Profile::LocalTabTitleFormat
(QString) The format used for tab titles when running normal commands.
Definition: Profile.h:112
Konsole::MainWindow::createSession
Session * createSession(Profile::Ptr profile, const QString &directory)
Create a new session.
Definition: MainWindow.cpp:453
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:23 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
  • Applications
  •   Libraries
  •     libkonq
  • 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