File indexing completed on 2024-04-21 08:48:08

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2008 Eduardo Robles Elvira <edulix@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef KONQSESSIONMANAGER_H
0009 #define KONQSESSIONMANAGER_H
0010 
0011 #include <QObject>
0012 #include <QTimer>
0013 #include <QStringList>
0014 #include <QString>
0015 
0016 #include <kconfig.h>
0017 #include <QDialog>
0018 #include <konqprivate_export.h>
0019 #include <config-konqueror.h>
0020 #include <KX11Extras>
0021 
0022 class KonqMainWindow;
0023 class QDialogButtonBox;
0024 class QTreeWidget;
0025 class QTreeWidgetItem;
0026 class QSessionManager;
0027 
0028 #ifdef KActivities_FOUND
0029 class ActivityManager;
0030 #endif
0031 
0032 class SessionRestoreDialog : public QDialog
0033 {
0034     Q_OBJECT
0035 public:
0036     explicit SessionRestoreDialog(const QStringList &sessionFilePaths, QWidget *parent = nullptr);
0037     ~SessionRestoreDialog() override;
0038 
0039     bool isEmpty() const;
0040 
0041     /**
0042      * Returns the list of session discarded/unselected by the user.
0043      */
0044     QStringList discardedSessionList() const;
0045 
0046     /**
0047      * Returns true if the don't show checkbox is checked.
0048      */
0049     bool isDontShowChecked() const;
0050 
0051     /**
0052      * Returns true if the corresponding session restore dialog should be shown.
0053      *
0054      * @param dontShowAgainName the name that identify the session restore dialog box.
0055      * @param result if not null, it will be set to the result that was chosen the last
0056      * time the dialog box was shown. This is only useful if the restore dialog box should
0057      * be shown.
0058      */
0059     static bool shouldBeShown(const QString &dontShowAgainName, int *result);
0060 
0061     /**
0062      * Save the fact that the session restore dialog should not be shown again.
0063      *
0064      * @param dontShowAgainName the name that identify the session restore dialog. If
0065      * empty, this method does nothing.
0066      * @param result the value (Yes or No) that should be used as the result
0067      * for the message box.
0068      */
0069     static void saveDontShow(const QString &dontShowAgainName, int result);
0070 
0071 private Q_SLOTS:
0072     void slotClicked(bool);
0073     void slotItemChanged(QTreeWidgetItem *, int);
0074 
0075 private:
0076     QTreeWidget *m_treeWidget;
0077     QStringList m_discardedSessionList;
0078     QHash<QTreeWidgetItem *, int> m_checkedSessionItems;
0079     int m_sessionItemsCount;
0080     QDialogButtonBox *m_buttonBox;
0081     bool m_dontShowChecked;
0082 };
0083 
0084 /**
0085  * This class is a singleton. It does some session related tasks:
0086  *  - Autosave current session every X seconds
0087  *  - Restore a saved session if konqueror crashed
0088  *  - Restore a given session manually
0089  */
0090 class KONQ_TESTS_EXPORT KonqSessionManager : public QObject
0091 {
0092     Q_OBJECT
0093 public:
0094     friend class KonqSessionManagerPrivate;
0095 
0096     static KonqSessionManager *self();
0097 
0098     void restoreSessionSavedAtLogout();
0099 
0100     /**
0101      * @brief Restores the application state saved by saveSessionBeforeClosingApplication if the corresponding option is enabled
0102      *
0103      * @return `true` if at least one `KonqMainWindow` exists and `false` if no `KonqMainWindow` exists
0104      */
0105     bool restoreSessionSavedAtExit();
0106 
0107     /**
0108      * Restore saved session(s).
0109      *
0110      * @param sessionFilePathsList list of session files to restore.
0111      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
0112      * in current window or not. False by default.
0113      * @param parent indicates in which window the tabs will be opened if
0114      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
0115      */
0116     void restoreSessions(const QStringList &sessionFilePathsList, bool
0117                          openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
0118 
0119     /**
0120      * Restore saved session(s).
0121      *
0122      * @param sessionsDir directory containing the session files to
0123      * restore.
0124      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
0125      * in current window or not. False by default.
0126      * @param parent indicates in which window the tabs will be opened if
0127      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
0128      */
0129     void restoreSessions(const QString &sessionsDir, bool
0130                          openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
0131 
0132     /**
0133      * Restore saved session.
0134      * @param sessionFilePath session file to restore.
0135      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
0136      * in current window or not. False by default.
0137      * @param parent indicates in which window the tabs will be opened if
0138      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
0139      */
0140     void restoreSession(const QString &sessionFilePath, bool
0141                         openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
0142 
0143     /**
0144      * Disable the autosave feature. It's called when a konqueror instance is
0145      * being preloaded
0146      */
0147     void disableAutosave();
0148 
0149     /**
0150      * Enable the autosave feature. It's called when a konqueror instance stops
0151      * being preloaded and starts having a window showed to the user.
0152      */
0153     void enableAutosave();
0154 
0155     /**
0156      * Removes the owned_by directory and all its files inside (which were
0157      * referencing the owned sessions).
0158      */
0159     void deleteOwnedSessions();
0160 
0161     /**
0162      * Save current session in a given path (absolute path to a file)
0163      * @param mainWindow if 0, all windows will be saved, else only the given one
0164      */
0165     void saveCurrentSessionToFile(const QString &sessionConfigPath, KonqMainWindow *mainWindow = nullptr);
0166 
0167     /**
0168      * Returns the autosave directory
0169      */
0170     QString autosaveDirectory() const;
0171 
0172     void setPreloadedWindowsNumber(const QList<int> &numbers);
0173 
0174     void registerMainWindow(KonqMainWindow *window);
0175 
0176 #ifdef KActivities_FOUND
0177     ActivityManager* activityManager();
0178 #endif
0179 
0180 public Q_SLOTS:
0181     /**
0182      * Ask the user with a dialog if session should be restored
0183      */
0184     bool askUserToRestoreAutosavedAbandonedSessions();
0185 
0186     /**
0187      * Saves current session.
0188      * This is function is called by the autosave timer, but you can call it too
0189      * if you want. It won't do anything if m_autosaveEnabled is false.
0190      */
0191     void autoSaveSession();
0192 
0193     /**
0194      * Restore owned sessions
0195      */
0196     //void restoreSessions();
0197 
0198     /**
0199      * Save current sessions of all konqueror instances (propagated via a
0200      * dbus signal).
0201      */
0202     void saveCurrentSessions(const QString &path);
0203 
0204     /**
0205      * @brief Saves the session when the application is about to be closed and the user chose to restore last state
0206      *
0207      * This does nothing when saving session at logout (according to `QGuiApplication::savingSession`, as that situation
0208      * is handled separately.
0209      *
0210      * The session is saved in `last_state` in `QStandardPaths::ApplicationDataDir`
0211      */
0212     void saveSessionAtExit();
0213 
0214 private Q_SLOTS:
0215     void slotCommitData(QSessionManager &sm);
0216 
0217 private:
0218     KonqSessionManager();
0219 
0220     ~KonqSessionManager() override;
0221 
0222     /**
0223      * Creates the owned_by directory with files inside referencing the owned
0224      * sessions and returns the list of filepaths with sessions to restore.
0225      * Returns an empty list if there is nothing to restore.
0226      */
0227     QStringList takeSessionsOwnership();
0228 
0229     QString dirForMyOwnedSessionFiles() const
0230     {
0231         return m_autosaveDir + "/owned_by" + m_baseService;
0232     }
0233 
0234     void saveCurrentSessionToFile(KConfig *config, const QList<KonqMainWindow *> &mainWindows = QList<KonqMainWindow *>());
0235 
0236 private:
0237     QTimer m_autoSaveTimer;
0238     QString m_autosaveDir;
0239     QString m_baseService;
0240     bool m_autosaveEnabled;
0241     bool m_createdOwnedByDir;
0242     KConfig *m_sessionConfig;
0243     QList<int> m_preloadedWindowsNumber;
0244 #ifdef KActivities_FOUND
0245     ActivityManager *m_activityManager;
0246 #endif
0247 
0248 Q_SIGNALS: // DBUS signals
0249     /**
0250      * Save current session of all konqueror running instances in a given
0251      * directory
0252      */
0253     void saveCurrentSession(const QString &path);
0254 private Q_SLOTS:// connected to DBUS signals
0255     void slotSaveCurrentSession(const QString &path);
0256 };
0257 
0258 #endif /* KONQSESSIONMANAGER_H */