File indexing completed on 2024-04-28 16:26:31

0001 /***************************************************************************************************
0002     begin                : Mon Dec 22 2003
0003     copyright            : (C) 2001 - 2003 by Brachet Pascal
0004                                2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
0005                                2007-2023 by Michel Ludwig (michel.ludwig@kdemail.net)
0006  ***************************************************************************************************/
0007 
0008 /***************************************************************************
0009  *                                                                         *
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  ***************************************************************************/
0016 
0017 #include "widgets/konsolewidget.h"
0018 
0019 #include "kileinfo.h"
0020 
0021 #include <QFileInfo>
0022 
0023 #include <QShowEvent>
0024 #include <QVBoxLayout>
0025 
0026 #include <KLocalizedString>
0027 #include <KPluginFactory>
0028 #include <KShell>
0029 #include <QUrl>
0030 
0031 #include <KParts/Part>
0032 #include <KParts/PartLoader>
0033 #include <KTextEditor/Document>
0034 #include <KTextEditor/View>
0035 
0036 #include <kde_terminal_interface.h>
0037 
0038 namespace KileWidget
0039 {
0040 Konsole::Konsole(KileInfo * info, QWidget *parent) :
0041     QFrame(parent),
0042     m_part(Q_NULLPTR),
0043     m_ki(info)
0044 {
0045     setLayout(new QVBoxLayout(this));
0046     layout()->setContentsMargins(0, 0, 0, 0);
0047     setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
0048     spawn();
0049 }
0050 
0051 Konsole::~Konsole()
0052 {
0053 }
0054 
0055 void Konsole::spawn()
0056 {
0057     KILE_DEBUG_MAIN << "void Konsole::spawn()";
0058 
0059     KPluginFactory *factory = KPluginFactory::loadFactory(QStringLiteral("konsolepart")).plugin;
0060 
0061     if(!factory) {
0062         KILE_DEBUG_MAIN << "No factory for konsolepart";
0063         return;
0064     }
0065 
0066     m_part = factory->create<KParts::ReadOnlyPart>(this);
0067 
0068     if(!m_part) {
0069         KILE_DEBUG_MAIN << "Could not create konsolepart";
0070         return;
0071     }
0072 
0073     if(!qobject_cast<TerminalInterface*>(m_part)) {
0074         KILE_DEBUG_MAIN << "Did not find the TerminalInterface";
0075         delete m_part;
0076         m_part = Q_NULLPTR;
0077         return;
0078     }
0079 
0080     layout()->addWidget(m_part->widget());
0081     setFocusProxy(m_part->widget());
0082     connect(m_part, SIGNAL(destroyed()), this, SLOT(slotDestroyed()));
0083 
0084     // necessary as older versions of Konsole (4.5) might not show a proper prompt otherwise
0085     qobject_cast<TerminalInterface*>(m_part)->showShellInDir(QDir::currentPath());
0086 }
0087 
0088 
0089 void Konsole::sync()
0090 {
0091     if(!KileConfig::syncConsoleDirWithTabs()) {
0092         return;
0093     }
0094 
0095     KTextEditor::Document *doc = m_ki->activeTextDocument();
0096     KTextEditor::View *view = Q_NULLPTR;
0097 
0098     if(doc) {
0099         view = doc->views().first();
0100     }
0101 
0102     if(view) {
0103         QUrl url = view->document()->url();
0104 
0105         if(url.path().isEmpty()) {
0106             return;
0107         }
0108 
0109         QFileInfo fic(url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
0110         if(fic.isReadable()) {
0111             setDirectory(url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
0112         }
0113     }
0114 }
0115 
0116 void Konsole::setDirectory(const QString &directory)
0117 {
0118     TerminalInterface *m_term = qobject_cast<TerminalInterface*>(m_part);
0119     if(!m_term || m_term->foregroundProcessId() >= 0) { // check if a foreground process is running
0120         return;
0121     }
0122 
0123     //FIXME: KonsolePart should be extended in such a way that it isn't necessary
0124     //       anymore to send 'cd' commands
0125     if(m_term && !directory.isEmpty() && directory != m_currentDir) {
0126         m_term->sendInput(QChar(0x05)); // clear the shell command prompt by sending Ctrl+E and
0127         m_term->sendInput(QChar(0x15)); // Ctrl+U (#301653)
0128         m_term->sendInput(" cd " + KShell::quoteArg(directory) + '\n');
0129         m_term->sendInput(" clear\n");
0130         m_currentDir = directory;
0131     }
0132 }
0133 
0134 void Konsole::showEvent(QShowEvent *ev)
0135 {
0136     QWidget::showEvent(ev);
0137     activate();
0138 }
0139 
0140 void Konsole::activate()
0141 {
0142     if (m_part) {
0143         m_part->widget()->show();
0144         m_part->widget()->setFocus();
0145     }
0146 }
0147 
0148 void Konsole::slotDestroyed ()
0149 {
0150     // there is no need to remove the widget from the layout as this is done
0151     // automatically when the widget is destroyed
0152     m_part = Q_NULLPTR;
0153     spawn();
0154 }
0155 }
0156