File indexing completed on 2024-04-14 03:53:17

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2021 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KTERMINALLAUNCHERJOB_H
0009 #define KTERMINALLAUNCHERJOB_H
0010 
0011 #include <KIO/CommandLauncherJob>
0012 #include <memory>
0013 
0014 class KTerminalLauncherJobPrivate;
0015 
0016 /**
0017  * @class KTerminalLauncherJob kterminallauncherjob.h <KTerminalLauncherJob>
0018  *
0019  * @brief KTerminalLauncherJob starts a terminal application,
0020  * either for the user to use interactively, or to execute a command.
0021  *
0022  * It creates a startup notification and finishes it on success or on error (for the taskbar).
0023  * It also emits an error message if necessary (e.g. "program not found").
0024  *
0025  * The job finishes when the application is successfully started.
0026  * For error handling, either connect to the result() signal, or for a simple messagebox on error,
0027  * you can do
0028  * @code
0029  *    job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
0030  * @endcode
0031  *
0032  * @since 5.83
0033  */
0034 class KIOGUI_EXPORT KTerminalLauncherJob : public KJob
0035 {
0036     Q_OBJECT
0037 public:
0038     /**
0039      * Creates a KTerminalLauncherJob.
0040      * @param command the command to execute in a terminal, can be empty.
0041      * @param parent the parent QObject
0042      */
0043     explicit KTerminalLauncherJob(const QString &command, QObject *parent = nullptr);
0044 
0045     /**
0046      * Destructor
0047      *
0048      * Note that jobs auto-delete themselves after emitting result
0049      */
0050     ~KTerminalLauncherJob() override;
0051 
0052     /**
0053      * Sets the working directory from which to run the command.
0054      * @param workingDirectory path of a local directory
0055      */
0056     void setWorkingDirectory(const QString &workingDirectory);
0057 
0058     /**
0059      * Sets the platform-specific startup id of the command launch.
0060      * @param startupId startup id, if any (otherwise "").
0061      * For X11, this would be the id for the Startup Notification protocol.
0062      * For Wayland, this would be the token for the XDG Activation protocol.
0063      */
0064     void setStartupId(const QByteArray &startupId);
0065 
0066     /**
0067      * Can be used to pass environment variables to the child process.
0068      * @param environment set of environment variables to pass to the child process
0069      * @see QProcessEnvironment
0070      */
0071     void setProcessEnvironment(const QProcessEnvironment &environment);
0072 
0073     /**
0074      * Starts the job.
0075      * You must call this, after having called all the necessary setters.
0076      */
0077     void start() override;
0078 
0079 private:
0080     friend class KTerminalLauncherJobTest;
0081     void determineFullCommand(bool fallbackToKonsoleService = true); // for the unittest
0082     QString fullCommand() const; // for the unittest
0083 
0084     KIOGUI_NO_EXPORT void emitDelayedResult();
0085 
0086     friend class KTerminalLauncherJobPrivate;
0087     std::unique_ptr<KTerminalLauncherJobPrivate> d;
0088 };
0089 
0090 #endif // KTERMINALLAUNCHERJOB_H