File indexing completed on 2024-05-12 04:58:21

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2017  Razi Alavizadeh <s.r.alavizadeh@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef SESSIONMANAGER_H
0019 #define SESSIONMANAGER_H
0020 
0021 #include "qzcommon.h"
0022 
0023 class QAction;
0024 class QMenu;
0025 class QFileInfo;
0026 
0027 class FALKON_EXPORT SessionManager : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     struct SessionMetaData {
0033         QString name;
0034         QString filePath;
0035         bool isActive = false;
0036         bool isDefault = false;
0037         bool isBackup = false;
0038     };
0039 
0040     enum SessionFlag {
0041         SwitchSession = 1,
0042         CloneSession = 2,
0043         ReplaceSession = SwitchSession | 4
0044     };
0045     Q_DECLARE_FLAGS(SessionFlags, SessionFlag)
0046 
0047     explicit SessionManager(QObject* parent = nullptr);
0048 
0049     void loadSettings();
0050     void saveSettings();
0051 
0052     static QString defaultSessionPath();
0053     QString lastActiveSessionPath() const;
0054     QString askSessionFromUser();
0055 
0056     void backupSavedSessions();
0057     void writeCurrentSession(const QString &filePath);
0058 
0059 Q_SIGNALS:
0060     void sessionsMetaDataChanged();
0061 
0062 public Q_SLOTS:
0063     void autoSaveLastSession();
0064     void openSessionManagerDialog();
0065 
0066 private Q_SLOTS:
0067     void aboutToShowSessionsMenu();
0068     void sessionsDirectoryChanged();
0069     void openSession(QString sessionFilePath = QString(), SessionManager::SessionFlags flags = {});
0070     void renameSession(QString sessionFilePath = QString(), SessionManager::SessionFlags flags = {});
0071     void saveSession();
0072 
0073     void replaceSession(const QString &filePath);
0074     void switchToSession(const QString &filePath);
0075     void cloneSession(const QString &filePath);
0076     void deleteSession(const QString &filePath);
0077     void newSession();
0078 
0079     QList<SessionManager::SessionMetaData> sessionMetaData(bool withBackups = true);
0080 
0081 private:
0082     bool isActive(const QString &filePath) const;
0083     bool isActive(const QFileInfo &fileInfo) const;
0084     void fillSessionsMetaDataListIfNeeded();
0085 
0086     QList<SessionMetaData> m_sessionsMetaDataList;
0087 
0088     QString m_firstBackupSession;
0089     QString m_secondBackupSession;
0090     QString m_lastActiveSessionPath;
0091 
0092     friend class SessionManagerDialog;
0093 };
0094 
0095 Q_DECLARE_OPERATORS_FOR_FLAGS(SessionManager::SessionFlags)
0096 
0097 #endif // SESSIONMANAGER_H