Changeset View
Changeset View
Standalone View
Standalone View
addons/konsole/kateconsole.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> | 2 | Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> | ||
3 | Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org> | 3 | Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org> | ||
4 | Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk> | 4 | Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk> | ||
5 | Copyright (C) 2007 Anders Lund <anders@alweb.dk> | 5 | Copyright (C) 2007 Anders Lund <anders@alweb.dk> | ||
6 | Copyright (C) 2017 Ederag <edera@gmx.fr> | ||||
6 | 7 | | |||
7 | This library is free software; you can redistribute it and/or | 8 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Library General Public | 9 | modify it under the terms of the GNU Library General Public | ||
9 | License version 2 as published by the Free Software Foundation. | 10 | License version 2 as published by the Free Software Foundation. | ||
10 | 11 | | |||
11 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | 15 | Library General Public License for more details. | ||
15 | 16 | | |||
16 | You should have received a copy of the GNU Library General Public License | 17 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | 18 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
19 | Boston, MA 02110-1301, USA. | 20 | Boston, MA 02110-1301, USA. | ||
20 | */ | 21 | */ | ||
21 | 22 | | |||
22 | #include "kateconsole.h" | 23 | #include "kateconsole.h" | ||
23 | 24 | | |||
24 | #include <klocalizedstring.h> | 25 | #include <klocalizedstring.h> | ||
25 | #include <ktexteditor/document.h> | 26 | #include <ktexteditor/document.h> | ||
26 | #include <ktexteditor/view.h> | 27 | #include <ktexteditor/view.h> | ||
28 | #include <ktexteditor/message.h> | ||||
27 | 29 | | |||
28 | #include <kde_terminal_interface.h> | 30 | #include <kde_terminal_interface.h> | ||
29 | #include <kshell.h> | 31 | #include <kshell.h> | ||
30 | #include <kparts/part.h> | 32 | #include <kparts/part.h> | ||
31 | #include <QAction> | 33 | #include <QAction> | ||
32 | #include <kactioncollection.h> | 34 | #include <kactioncollection.h> | ||
33 | 35 | | |||
34 | #include <kmessagebox.h> | 36 | #include <kmessagebox.h> | ||
35 | 37 | | |||
36 | #include <QApplication> | 38 | #include <QApplication> | ||
37 | #include <QStyle> | 39 | #include <QStyle> | ||
38 | #include <QIcon> | 40 | #include <QIcon> | ||
39 | #include <QShowEvent> | 41 | #include <QShowEvent> | ||
40 | #include <QLabel> | 42 | #include <QLabel> | ||
41 | #include <QCheckBox> | 43 | #include <QCheckBox> | ||
44 | #include <QGroupBox> | ||||
45 | #include <QLineEdit> | ||||
46 | #include <QPushButton> | ||||
42 | #include <QVBoxLayout> | 47 | #include <QVBoxLayout> | ||
43 | #include <QFileInfo> | 48 | #include <QFileInfo> | ||
44 | 49 | | |||
45 | #include <KPluginLoader> | 50 | #include <KPluginLoader> | ||
46 | #include <KPluginFactory> | 51 | #include <KPluginFactory> | ||
47 | #include <kaboutdata.h> | 52 | #include <kaboutdata.h> | ||
48 | #include <kpluginfactory.h> | 53 | #include <kpluginfactory.h> | ||
49 | #include <KAuthorized> | 54 | #include <KAuthorized> | ||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | 128 | { | |||
127 | // make sure we have a vertical layout | 132 | // make sure we have a vertical layout | ||
128 | new QVBoxLayout(this); | 133 | new QVBoxLayout(this); | ||
129 | layout()->setContentsMargins(0, 0, 0, 0); | 134 | layout()->setContentsMargins(0, 0, 0, 0); | ||
130 | 135 | | |||
131 | QAction* a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_pipe_to_terminal")); | 136 | QAction* a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_pipe_to_terminal")); | ||
132 | a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); | 137 | a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); | ||
133 | a->setText(i18nc("@action", "&Pipe to Terminal")); | 138 | a->setText(i18nc("@action", "&Pipe to Terminal")); | ||
134 | connect(a, &QAction::triggered, this, &KateConsole::slotPipeToConsole); | 139 | connect(a, &QAction::triggered, this, &KateConsole::slotPipeToConsole); | ||
140 | | ||||
135 | a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_sync")); | 141 | a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_sync")); | ||
136 | a->setText(i18nc("@action", "S&ynchronize Terminal with Current Document")); | 142 | a->setText(i18nc("@action", "S&ynchronize Terminal with Current Document")); | ||
137 | connect(a, &QAction::triggered, this, &KateConsole::slotManualSync); | 143 | connect(a, &QAction::triggered, this, &KateConsole::slotManualSync); | ||
138 | 144 | | |||
145 | a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_run")); | ||||
146 | a->setText(i18nc("@action", "Run Current Document")); | ||||
147 | connect(a, SIGNAL(triggered()), this, SLOT(slotRun())); | ||||
148 | | ||||
139 | a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_toggle_focus")); | 149 | a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_toggle_focus")); | ||
140 | a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); | 150 | a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); | ||
141 | a->setText(i18nc("@action", "&Focus Terminal")); | 151 | a->setText(i18nc("@action", "&Focus Terminal")); | ||
142 | connect(a, &QAction::triggered, this, &KateConsole::slotToggleFocus); | 152 | connect(a, &QAction::triggered, this, &KateConsole::slotToggleFocus); | ||
143 | 153 | | |||
144 | m_mw->guiFactory()->addClient (this); | 154 | m_mw->guiFactory()->addClient (this); | ||
145 | 155 | | |||
146 | readConfig(); | 156 | readConfig(); | ||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Line(s) | |||||
287 | 297 | | |||
288 | void KateConsole::slotManualSync() | 298 | void KateConsole::slotManualSync() | ||
289 | { | 299 | { | ||
290 | m_currentPath.clear (); | 300 | m_currentPath.clear (); | ||
291 | slotSync(); | 301 | slotSync(); | ||
292 | if ( ! m_part || ! m_part->widget()->isVisible() ) | 302 | if ( ! m_part || ! m_part->widget()->isVisible() ) | ||
293 | m_mw->showToolView( parentWidget() ); | 303 | m_mw->showToolView( parentWidget() ); | ||
294 | } | 304 | } | ||
305 | | ||||
306 | void KateConsole::slotRun() | ||||
307 | { | ||||
308 | if ( m_mw->activeView() ) { | ||||
309 | KTextEditor::Document *document = m_mw->activeView()->document(); | ||||
310 | QUrl u = document->url(); | ||||
311 | if ( ! u.isLocalFile() ) { | ||||
312 | QPointer<KTextEditor::Message> message = | ||||
313 | new KTextEditor::Message( | ||||
314 | i18n("Not a local file: '%1'", u.path()), | ||||
315 | KTextEditor::Message::Error | ||||
316 | ); | ||||
317 | // auto hide is enabled and set to a sane default value of several seconds. | ||||
318 | message->setAutoHide(2000); | ||||
319 | message->setAutoHideMode( KTextEditor::Message::Immediate ); | ||||
320 | document->postMessage( message ); | ||||
321 | return; | ||||
322 | } | ||||
323 | // ensure that file is saved | ||||
324 | if ( document->isModified() ) { | ||||
325 | document->save(); | ||||
326 | } | ||||
327 | | ||||
328 | // The string that should be output to terminal, upon acceptance | ||||
329 | QString output_str; | ||||
330 | // prefix first | ||||
331 | output_str += KConfigGroup( KSharedConfig::openConfig(), | ||||
332 | "Konsole" | ||||
333 | ).readEntry("RunPrefix", ""); | ||||
334 | // then filename | ||||
335 | if ( KConfigGroup(KSharedConfig::openConfig(), | ||||
336 | "Konsole" | ||||
337 | ).readEntry("RemoveExtension", true) ) { | ||||
338 | // append filename without extension (i.e. keep only the basename) | ||||
339 | output_str += QFileInfo( u.path() ).baseName() + QLatin1Char('\n'); | ||||
340 | } else { | ||||
341 | // append filename to the terminal | ||||
342 | output_str += QFileInfo( u.path() ).fileName() + QLatin1Char('\n'); | ||||
343 | } | ||||
344 | | ||||
345 | if ( KMessageBox::Continue != | ||||
346 | KMessageBox::warningContinueCancel( | ||||
347 | m_mw->window(), | ||||
348 | i18n("Do you really want to Run the document ?\n" | ||||
349 | "This will execute the following command,\n" | ||||
350 | "with your user rights, in the terminal:\n" | ||||
351 | "'%1'", output_str), | ||||
352 | i18n("Run in Terminal?"), | ||||
353 | KGuiItem( i18n("Run") ), | ||||
354 | KStandardGuiItem::cancel(), | ||||
355 | QStringLiteral("Konsole: Run in Terminal Warning") ) | ||||
356 | ) { | ||||
357 | return; | ||||
358 | } | ||||
359 | // echo to terminal | ||||
360 | sendInput( output_str ); | ||||
361 | } | ||||
362 | } | ||||
363 | | ||||
295 | void KateConsole::slotToggleFocus() | 364 | void KateConsole::slotToggleFocus() | ||
296 | { | 365 | { | ||
297 | QAction *action = actionCollection()->action(QStringLiteral("katekonsole_tools_toggle_focus")); | 366 | QAction *action = actionCollection()->action(QStringLiteral("katekonsole_tools_toggle_focus")); | ||
298 | if ( ! m_part ) { | 367 | if ( ! m_part ) { | ||
299 | m_mw->showToolView( parentWidget() ); | 368 | m_mw->showToolView( parentWidget() ); | ||
300 | action->setText( i18n("Defocus Terminal") ); | 369 | action->setText( i18n("Defocus Terminal") ); | ||
301 | return; // this shows and focuses the konsole | 370 | return; // this shows and focuses the konsole | ||
302 | } | 371 | } | ||
Show All 31 Lines | 402 | KateKonsoleConfigPage::KateKonsoleConfigPage( QWidget* parent, KateKonsolePlugin *plugin ) | |||
334 | : KTextEditor::ConfigPage( parent ) | 403 | : KTextEditor::ConfigPage( parent ) | ||
335 | , mPlugin( plugin ) | 404 | , mPlugin( plugin ) | ||
336 | { | 405 | { | ||
337 | QVBoxLayout *lo = new QVBoxLayout( this ); | 406 | QVBoxLayout *lo = new QVBoxLayout( this ); | ||
338 | lo->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing)); | 407 | lo->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing)); | ||
339 | 408 | | |||
340 | cbAutoSyncronize = new QCheckBox( i18n("&Automatically synchronize the terminal with the current document when possible"), this ); | 409 | cbAutoSyncronize = new QCheckBox( i18n("&Automatically synchronize the terminal with the current document when possible"), this ); | ||
341 | lo->addWidget( cbAutoSyncronize ); | 410 | lo->addWidget( cbAutoSyncronize ); | ||
411 | | ||||
412 | QVBoxLayout *vboxRun = new QVBoxLayout; | ||||
413 | QGroupBox *groupRun = new QGroupBox( i18n("Run in terminal"), this ); | ||||
414 | // Remove extension | ||||
415 | cbRemoveExtension = new QCheckBox( i18n("&Remove extension"), this ); | ||||
416 | vboxRun->addWidget( cbRemoveExtension ); | ||||
417 | // Prefix | ||||
418 | QFrame *framePrefix = new QFrame( this ); | ||||
419 | QHBoxLayout *hboxPrefix = new QHBoxLayout( framePrefix ); | ||||
420 | QLabel *label = new QLabel( i18n("Prefix:"), framePrefix ); | ||||
421 | hboxPrefix->addWidget( label ); | ||||
422 | lePrefix = new QLineEdit( framePrefix ); | ||||
423 | hboxPrefix->addWidget( lePrefix ); | ||||
424 | vboxRun->addWidget( framePrefix ); | ||||
425 | // show warning next time | ||||
426 | QFrame *frameWarn = new QFrame( this ); | ||||
427 | QHBoxLayout *hboxWarn = new QHBoxLayout( frameWarn ); | ||||
428 | QPushButton *buttonWarn = new QPushButton( i18n("&Show warning next time"), frameWarn); | ||||
429 | buttonWarn->setWhatsThis ( | ||||
430 | i18n ( "The next time '%1' is executed, " | ||||
431 | "make sure a warning window will pop up, " | ||||
432 | "displaying the command to be sent to terminal, " | ||||
433 | "for review.", | ||||
434 | i18n ("Run in terminal") | ||||
435 | ) | ||||
436 | ); | ||||
437 | connect( buttonWarn, &QPushButton::pressed, | ||||
438 | this, &KateKonsoleConfigPage::slotEnableRunWarning ); | ||||
439 | hboxWarn->addWidget( buttonWarn ); | ||||
440 | vboxRun->addWidget( frameWarn ); | ||||
441 | groupRun->setLayout( vboxRun ); | ||||
442 | lo->addWidget( groupRun ); | ||||
443 | | ||||
342 | cbSetEditor = new QCheckBox( i18n("Set &EDITOR environment variable to 'kate -b'"), this ); | 444 | cbSetEditor = new QCheckBox( i18n("Set &EDITOR environment variable to 'kate -b'"), this ); | ||
343 | lo->addWidget( cbSetEditor ); | 445 | lo->addWidget( cbSetEditor ); | ||
344 | QLabel *tmp = new QLabel(this); | 446 | QLabel *tmp = new QLabel(this); | ||
345 | tmp->setText(i18n("Important: The document has to be closed to make the console application continue")); | 447 | tmp->setText(i18n("Important: The document has to be closed to make the console application continue")); | ||
346 | lo->addWidget(tmp); | 448 | lo->addWidget(tmp); | ||
347 | reset(); | 449 | reset(); | ||
348 | lo->addStretch(); | 450 | lo->addStretch(); | ||
349 | connect(cbAutoSyncronize, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); | 451 | connect(cbAutoSyncronize, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); | ||
452 | connect( cbRemoveExtension, SIGNAL(stateChanged(int)), SIGNAL(changed()) ); | ||||
453 | connect( lePrefix, &QLineEdit::textChanged, | ||||
454 | this, &KateKonsoleConfigPage::changed ); | ||||
350 | connect(cbSetEditor, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); | 455 | connect(cbSetEditor, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); | ||
351 | } | 456 | } | ||
352 | 457 | | |||
458 | void KateKonsoleConfigPage::slotEnableRunWarning () | ||||
459 | { | ||||
460 | KMessageBox::enableMessage(QStringLiteral("Konsole: Run in Terminal Warning")); | ||||
461 | } | ||||
462 | | ||||
353 | QString KateKonsoleConfigPage::name() const | 463 | QString KateKonsoleConfigPage::name() const | ||
354 | { | 464 | { | ||
355 | return i18n("Terminal"); | 465 | return i18n("Terminal"); | ||
356 | } | 466 | } | ||
357 | 467 | | |||
358 | QString KateKonsoleConfigPage::fullName() const | 468 | QString KateKonsoleConfigPage::fullName() const | ||
359 | { | 469 | { | ||
360 | return i18n("Terminal Settings"); | 470 | return i18n("Terminal Settings"); | ||
361 | } | 471 | } | ||
362 | 472 | | |||
363 | QIcon KateKonsoleConfigPage::icon() const | 473 | QIcon KateKonsoleConfigPage::icon() const | ||
364 | { | 474 | { | ||
365 | return QIcon::fromTheme(QStringLiteral("utilities-terminal")); | 475 | return QIcon::fromTheme(QStringLiteral("utilities-terminal")); | ||
366 | } | 476 | } | ||
367 | 477 | | |||
368 | void KateKonsoleConfigPage::apply() | 478 | void KateKonsoleConfigPage::apply() | ||
369 | { | 479 | { | ||
370 | KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); | 480 | KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); | ||
371 | config.writeEntry("AutoSyncronize", cbAutoSyncronize->isChecked()); | 481 | config.writeEntry("AutoSyncronize", cbAutoSyncronize->isChecked()); | ||
482 | config.writeEntry("RemoveExtension", cbRemoveExtension->isChecked()); | ||||
483 | config.writeEntry("RunPrefix", lePrefix->text()); | ||||
372 | config.writeEntry("SetEditor", cbSetEditor->isChecked()); | 484 | config.writeEntry("SetEditor", cbSetEditor->isChecked()); | ||
373 | config.sync(); | 485 | config.sync(); | ||
374 | mPlugin->readConfig(); | 486 | mPlugin->readConfig(); | ||
375 | } | 487 | } | ||
376 | 488 | | |||
377 | void KateKonsoleConfigPage::reset() | 489 | void KateKonsoleConfigPage::reset() | ||
378 | { | 490 | { | ||
379 | KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); | 491 | KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); | ||
380 | cbAutoSyncronize->setChecked(config.readEntry("AutoSyncronize", false)); | 492 | cbAutoSyncronize->setChecked(config.readEntry("AutoSyncronize", false)); | ||
493 | cbRemoveExtension->setChecked(config.readEntry("RemoveExtension", false)); | ||||
494 | lePrefix->setText(config.readEntry("RunPrefix", "")); | ||||
381 | cbSetEditor->setChecked(config.readEntry("SetEditor", false)); | 495 | cbSetEditor->setChecked(config.readEntry("SetEditor", false)); | ||
382 | } | 496 | } | ||
383 | 497 | | |||
384 | #include "kateconsole.moc" | 498 | #include "kateconsole.moc" | ||
385 | 499 | | |||
386 | // kate: space-indent on; indent-width 2; replace-tabs on; | 500 | // kate: space-indent on; indent-width 2; replace-tabs on; | ||
387 | 501 | |