File indexing completed on 2024-04-28 04:37:25

0001 /*
0002     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_SESSIONCONTROLLER_H
0009 #define KDEVPLATFORM_SESSIONCONTROLLER_H
0010 
0011 #include "shellexport.h"
0012 
0013 #include "session.h"
0014 #include <interfaces/isessionlock.h>
0015 
0016 #include <QObject>
0017 
0018 #include <KXMLGUIClient>
0019 
0020 namespace KDevelop
0021 {
0022 
0023 class SessionControllerPrivate;
0024 
0025 struct SessionRunInfo
0026 {
0027     SessionRunInfo()
0028     {}
0029     bool operator==(const SessionRunInfo& o) const
0030     {
0031         return isRunning == o.isRunning && holderPid == o.holderPid
0032                 && holderApp == o.holderApp && holderHostname == o.holderHostname;
0033     }
0034     bool operator!=(const SessionRunInfo& o) const
0035     {
0036         return !(operator==(o));
0037     }
0038     // if this is true, this session is currently running in an external process
0039     bool isRunning = false;
0040     // if the session is running, this contains the PID of its process
0041     qint64 holderPid = -1;
0042     // if the session is running, this contains the name of its process
0043     QString holderApp;
0044     // if the session is running, this contains the host name where the process runs
0045     QString holderHostname;
0046 };
0047 
0048 struct TryLockSessionResult
0049 {
0050     explicit TryLockSessionResult(const ISessionLock::Ptr& _lock)
0051     : lock(_lock)
0052     {}
0053     explicit TryLockSessionResult(const SessionRunInfo& _runInfo)
0054     : runInfo(_runInfo)
0055     {}
0056     // if this is non-null then the session was locked
0057     ISessionLock::Ptr lock;
0058     // otherwise this contains information about who is locking the session
0059     SessionRunInfo runInfo;
0060 };
0061 
0062 class KDEVPLATFORMSHELL_EXPORT SessionController : public QObject, public KXMLGUIClient
0063 {
0064     Q_OBJECT
0065     Q_CLASSINFO("D-Bus Interface", "org.kdevelop.SessionController")
0066 
0067 public:
0068     explicit SessionController( QObject *parent = nullptr );
0069     ~SessionController() override;
0070     void initialize( const QString& session );
0071     void cleanup();
0072 
0073     /// Returns whether the given session can be locked (i. e., is not locked currently).
0074     /// @param doLocking whether to really lock the session or just "dry-run" the locking process
0075     static TryLockSessionResult tryLockSession(const QString& id, bool doLocking=true);
0076 
0077     /**
0078      * @return true when the given session is currently running, false otherwise
0079      */
0080     static bool isSessionRunning(const QString& id);
0081 
0082     /**
0083      * @return information about whether the session @p id is running
0084      */
0085     static SessionRunInfo sessionRunInfo(const QString& id);
0086 
0087     /// The application should call this on startup to tell the
0088     /// session-controller about the received arguments.
0089     /// Some of them may need to be passed to newly opened sessions.
0090     static void setArguments(int argc, char** argv);
0091 
0092     ///Finds a session by its name or by its UUID
0093     Session* session( const QString& nameOrId ) const;
0094     virtual ISession* activeSession() const;
0095     ISessionLock::Ptr activeSessionLock() const;
0096     QList<QString> sessionNames() const;
0097     Session* createSession( const QString& name );
0098 
0099     QList<const Session*> sessions() const;
0100 
0101     void loadDefaultSession( const QString& session );
0102 
0103     void startNewSession();
0104 
0105     void loadSession( const QString& nameOrId );
0106     void deleteSession( const ISessionLock::Ptr& lock );
0107     static void deleteSessionFromDisk( const ISessionLock::Ptr& lock );
0108     QString cloneSession( const QString& nameOrid );
0109     /**
0110      * Path to session directory for the session with the given @p sessionId.
0111      */
0112     static QString sessionDirectory( const QString& sessionId );
0113     static QString cfgSessionGroup();
0114     static QString cfgActiveSessionEntry();
0115 
0116     static SessionInfos availableSessionInfos();
0117 
0118     /**
0119      * Shows a dialog where the user can choose the session
0120      * @param headerText an additional text that will be shown at the top in a label
0121      * @param onlyRunning whether only currently running sessions should be shown
0122      * @return UUID on success, empty string in any other case
0123      */
0124     static QString showSessionChooserDialog(const QString& headerText = QString(), bool onlyRunning = false);
0125 
0126     /// Should be called if session to be opened is locked.
0127     /// It attempts to bring existing instance's window up via a DBus call; if that succeeds, empty string is returned.
0128     /// Otherwise (if the app did not respond) it shows a dialog where the user may choose
0129     /// 1) to force-remove the lockfile and continue,
0130     /// 2) to select another session via @ref showSessionChooserDialog,
0131     /// 3) to quit the current (starting-up) instance.
0132     /// @param sessionName session name (for the message)
0133     /// @param currentSessionId current session GUID (to return if user chooses force-removal)
0134     /// @param runInfo the run information about the session
0135     /// @return new session GUID to try or an empty string if application startup shall be aborted
0136     static QString handleLockedSession( const QString& sessionName, const QString& currentSessionId, const SessionRunInfo& runInfo );
0137 
0138     void updateXmlGuiActionList();
0139 
0140     void emitQuitSession()
0141     {
0142         emit quitSession();
0143     }
0144 
0145 public Q_SLOTS:
0146     // Returns the pretty name of the currently active session (used in the shell integration)
0147     virtual Q_SCRIPTABLE QString sessionName();
0148     // Returns the directory associated to the active session (used in the shell integration)
0149     virtual Q_SCRIPTABLE QString sessionDir();
0150 
0151 Q_SIGNALS:
0152     void sessionLoaded( ISession* );
0153     void sessionDeleted( const QString& id);
0154     void quitSession();
0155 
0156 private:
0157     const QScopedPointer<class SessionControllerPrivate> d_ptr;
0158     Q_DECLARE_PRIVATE(SessionController)
0159 };
0160 
0161 
0162 }
0163 #endif
0164