1 /*
2     This file is part of the KDE project
3     SPDX-FileCopyrightText: 2008 Eduardo Robles Elvira <edulix@gmail.com>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef KONQSESSIONMANAGER_H
9 #define KONQSESSIONMANAGER_H
10 
11 #include <QObject>
12 #include <QTimer>
13 #include <QStringList>
14 #include <QString>
15 
16 #include <kconfig.h>
17 #include <QDialog>
18 #include <konqprivate_export.h>
19 
20 class KonqMainWindow;
21 class QDialogButtonBox;
22 class QTreeWidget;
23 class QTreeWidgetItem;
24 class QSessionManager;
25 
26 class SessionRestoreDialog : public QDialog
27 {
28     Q_OBJECT
29 public:
30     explicit SessionRestoreDialog(const QStringList &sessionFilePaths, QWidget *parent = nullptr);
31     ~SessionRestoreDialog() override;
32 
33     bool isEmpty() const;
34 
35     /**
36      * Returns the list of session discarded/unselected by the user.
37      */
38     QStringList discardedSessionList() const;
39 
40     /**
41      * Returns true if the don't show checkbox is checked.
42      */
43     bool isDontShowChecked() const;
44 
45     /**
46      * Returns true if the corresponding session restore dialog should be shown.
47      *
48      * @param dontShowAgainName the name that identify the session restore dialog box.
49      * @param result if not null, it will be set to the result that was chosen the last
50      * time the dialog box was shown. This is only useful if the restore dialog box should
51      * be shown.
52      */
53     static bool shouldBeShown(const QString &dontShowAgainName, int *result);
54 
55     /**
56      * Save the fact that the session restore dialog should not be shown again.
57      *
58      * @param dontShowAgainName the name that identify the session restore dialog. If
59      * empty, this method does nothing.
60      * @param result the value (Yes or No) that should be used as the result
61      * for the message box.
62      */
63     static void saveDontShow(const QString &dontShowAgainName, int result);
64 
65 private Q_SLOTS:
66     void slotClicked(bool);
67     void slotItemChanged(QTreeWidgetItem *, int);
68 
69 private:
70     QTreeWidget *m_treeWidget;
71     QStringList m_discardedSessionList;
72     QHash<QTreeWidgetItem *, int> m_checkedSessionItems;
73     int m_sessionItemsCount;
74     QDialogButtonBox *m_buttonBox;
75     bool m_dontShowChecked;
76 };
77 
78 /**
79  * This class is a singleton. It does some session related tasks:
80  *  - Autosave current session every X seconds
81  *  - Restore a saved session if konqueror crashed
82  *  - Restore a given session manually
83  */
84 class KONQ_TESTS_EXPORT KonqSessionManager : public QObject
85 {
86     Q_OBJECT
87 public:
88     friend class KonqSessionManagerPrivate;
89 
90     static KonqSessionManager *self();
91 
92     /**
93      * Restore saved session(s).
94      *
95      * @param sessionFilePathsList list of session files to restore.
96      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
97      * in current window or not. False by default.
98      * @param parent indicates in which window the tabs will be opened if
99      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
100      */
101     void restoreSessions(const QStringList &sessionFilePathsList, bool
102                          openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
103 
104     /**
105      * Restore saved session(s).
106      *
107      * @param sessionsDir directory containing the session files to
108      * restore.
109      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
110      * in current window or not. False by default.
111      * @param parent indicates in which window the tabs will be opened if
112      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
113      */
114     void restoreSessions(const QString &sessionsDir, bool
115                          openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
116 
117     /**
118      * Restore saved session.
119      * @param sessionFilePath session file to restore.
120      * @param openTabsInsideCurrentWindow indicates if you want to open the tabs
121      * in current window or not. False by default.
122      * @param parent indicates in which window the tabs will be opened if
123      * openTabsInsideCurrentWindow is set to true. Otherwise it won't be used.
124      */
125     void restoreSession(const QString &sessionFilePath, bool
126                         openTabsInsideCurrentWindow = false, KonqMainWindow *parent = nullptr);
127 
128     /**
129      * Disable the autosave feature. It's called when a konqueror instance is
130      * being preloaded
131      */
132     void disableAutosave();
133 
134     /**
135      * Enable the autosave feature. It's called when a konqueror instance stops
136      * being preloaded and starts having a window showed to the user.
137      */
138     void enableAutosave();
139 
140     /**
141      * Removes the owned_by directory and all its files inside (which were
142      * referencing the owned sessions).
143      */
144     void deleteOwnedSessions();
145 
146     /**
147      * Save current session in a given path (absolute path to a file)
148      * @param mainWindow if 0, all windows will be saved, else only the given one
149      */
150     void saveCurrentSessionToFile(const QString &sessionConfigPath, KonqMainWindow *mainWindow = nullptr);
151 
152     /**
153      * Returns the autosave directory
154      */
155     QString autosaveDirectory() const;
156 
157 public Q_SLOTS:
158     /**
159      * Ask the user with a dialog if session should be restored
160      */
161     bool askUserToRestoreAutosavedAbandonedSessions();
162 
163     /**
164      * Saves current session.
165      * This is function is called by the autosave timer, but you can call it too
166      * if you want. It won't do anything if m_autosaveEnabled is false.
167      */
168     void autoSaveSession();
169 
170     /**
171      * Restore owned sessions
172      */
173     //void restoreSessions();
174 
175     /**
176      * Save current sessions of all konqueror instances (propagated via a
177      * dbus signal).
178      */
179     void saveCurrentSessions(const QString &path);
180 
181 private Q_SLOTS:
182     void slotCommitData(QSessionManager &sm);
183 
184 private:
185     KonqSessionManager();
186 
187     ~KonqSessionManager() override;
188 
189     /**
190      * Creates the owned_by directory with files inside referencing the owned
191      * sessions and returns the list of filepaths with sessions to restore.
192      * Returns an empty list if there is nothing to restore.
193      */
194     QStringList takeSessionsOwnership();
195 
dirForMyOwnedSessionFiles()196     QString dirForMyOwnedSessionFiles() const
197     {
198         return m_autosaveDir + "/owned_by" + m_baseService;
199     }
200 
201     void saveCurrentSessionToFile(KConfig *config, const QList<KonqMainWindow *> &mainWindows = QList<KonqMainWindow *>());
202 private:
203     QTimer m_autoSaveTimer;
204     QString m_autosaveDir;
205     QString m_baseService;
206     bool m_autosaveEnabled;
207     bool m_createdOwnedByDir;
208     KConfig *m_sessionConfig;
209 
210 Q_SIGNALS: // DBUS signals
211     /**
212      * Save current session of all konqueror running instances in a given
213      * directory
214      */
215     void saveCurrentSession(const QString &path);
216 private Q_SLOTS:// connected to DBUS signals
217     void slotSaveCurrentSession(const QString &path);
218 };
219 
220 #endif /* KONQSESSIONMANAGER_H */
221