File indexing completed on 2024-04-28 15:29:20
0001 /* 0002 interface.h 0003 SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #ifndef KDELIBS_KDE_TERMINAL_INTERFACE_H 0009 #define KDELIBS_KDE_TERMINAL_INTERFACE_H 0010 0011 #include <QObject> 0012 #include <QStringList> 0013 0014 /** 0015 * TerminalInterface is an interface implemented by KonsolePart to 0016 * allow developers access to the KonsolePart in ways that are not 0017 * possible through the normal KPart interface. 0018 * 0019 * Note that besides the functions below here, KonsolePart also has 0020 * some signals you can connect to. They aren't in this class cause 0021 * we can't have signals without having a QObject, which 0022 * TerminalInterface is not. 0023 * 0024 * These are some signals you can connect to: 0025 * void currentDirectoryChanged(const QString& dir); 0026 * 0027 * See the example code below for how to connect to these. 0028 * 0029 * Use it like this: 0030 * \code 0031 * // Query the .desktop file to get service information about konsolepart 0032 * KService::Ptr service = KService::serviceByDesktopName("konsolepart"); 0033 * 0034 * if (!service) { 0035 * QMessageBox::critical(this, tr("Konsole not installed"), tr("Please install the kde konsole and try again!"), QMessageBox::Ok); 0036 * return ; 0037 * } 0038 * 0039 * // Create one instance of konsolepart 0040 * KParts::ReadOnlyPart *part = service->createInstance<KParts::ReadOnlyPart>(parent, parentWidget, QVariantList()); 0041 * 0042 * if (!part) { 0043 * return; 0044 * } 0045 * 0046 * // Cast the konsolepart to the TerminalInterface.. 0047 * TerminalInterface *terminalIface = qobject_cast<TerminalInterface *>(part); 0048 * 0049 * if (!terminalIface) { 0050 * return; 0051 * } 0052 * 0053 * // Now use the interface in all sorts of ways, e.g. 0054 * // terminalIface->showShellInDir(QDir::home().path()); 0055 * // Or: 0056 * // QStringList list; 0057 * // list.append("python"); 0058 * // terminalIface->startProgram( QString::fromUtf8( "/usr/bin/python" ), list); 0059 * // Or connect to one of the signals. Connect to the Part object, 0060 * // not to the TerminalInterface, since the latter is not a QObject, 0061 * // and as such does not have signals..: 0062 * connect(part, &KParts::ReadOnlyPart::currentDirectoryChanged, this, [this](const QString &dirPath) { 0063 * currentDirectoryChanged(dirPath); 0064 * }); 0065 * // etc. 0066 * 0067 * \endcode 0068 * 0069 * @author Dominique Devriese <devriese@kde.org> 0070 */ 0071 class TerminalInterface 0072 { 0073 public: 0074 virtual ~TerminalInterface() 0075 { 0076 } 0077 /** 0078 * This starts @p program, with arguments @p args 0079 */ 0080 virtual void startProgram(const QString &program, const QStringList &args) = 0; 0081 /** 0082 * If no shell is running, this starts a shell with the 0083 * @dir as the starting directory. 0084 * If a shell is already running, nothing is done. 0085 */ 0086 virtual void showShellInDir(const QString &dir) = 0; 0087 0088 /** 0089 * This sends @param text as input to the currently running 0090 * program.. 0091 */ 0092 virtual void sendInput(const QString &text) = 0; 0093 0094 /** 0095 * Return terminal PID. If no process is currently running, returns 0. 0096 */ 0097 virtual int terminalProcessId() = 0; 0098 0099 /** 0100 * Return foregound PID, If there is no foreground process running, returns -1 0101 */ 0102 virtual int foregroundProcessId() = 0; 0103 0104 /** 0105 * Returns sub process name. If there is no sub process running, returns empty QString 0106 */ 0107 virtual QString foregroundProcessName() = 0; 0108 0109 /** 0110 * Returns the current working directory 0111 */ 0112 virtual QString currentWorkingDirectory() const = 0; 0113 }; 0114 0115 class TerminalInterfaceV2 : public TerminalInterface 0116 { 0117 public: 0118 /** 0119 * Returns the names of available profiles. 0120 */ 0121 virtual QStringList availableProfiles() const = 0; 0122 0123 /** 0124 * Returns the name of the currently active profile. 0125 */ 0126 virtual QString currentProfileName() const = 0; 0127 0128 /** 0129 * Changes the currently active profile to @p profileName. 0130 * @returns Returns true if setting the profile was successful 0131 */ 0132 virtual bool setCurrentProfile(const QString &profileName) = 0; 0133 0134 /** 0135 * Returns the property @p profileProperty of the currently active profile. 0136 */ 0137 virtual QVariant profileProperty(const QString &profileProperty) const = 0; 0138 }; 0139 0140 Q_DECLARE_INTERFACE(TerminalInterfaceV2, "org.kde.TerminalInterfaceV2") 0141 Q_DECLARE_INTERFACE(TerminalInterface, "org.kde.TerminalInterface") 0142 0143 #endif