File indexing completed on 2024-05-19 05:05:52

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2020 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #ifndef KBIBTEX_PROGRAM_OPENFILEINFO_H
0021 #define KBIBTEX_PROGRAM_OPENFILEINFO_H
0022 
0023 #include <QObject>
0024 #include <QList>
0025 #include <QDateTime>
0026 #include <QUrl>
0027 #include <QVariant>
0028 
0029 #include <KPluginMetaData>
0030 #include <FileInfo>
0031 
0032 namespace KParts
0033 {
0034 class ReadOnlyPart;
0035 }
0036 
0037 class OpenFileInfoManager;
0038 
0039 class OpenFileInfo : public QObject
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     enum class StatusFlag {
0045         Open = 0x1,
0046         RecentlyUsed = 0x2,
0047         Favorite = 0x4,
0048         HasName = 0x8
0049     };
0050     Q_DECLARE_FLAGS(StatusFlags, StatusFlag)
0051 
0052     ~OpenFileInfo() override;
0053 
0054     KParts::ReadOnlyPart *part(QWidget *parent, const KPluginMetaData &service = {});
0055 
0056     QString shortCaption() const;
0057     QString fullCaption() const;
0058     QString mimeType() const;
0059     QUrl url() const;
0060     bool isModified() const;
0061     bool save();
0062 
0063     /**
0064      * Close the current file. The user may interrupt the closing
0065      * if the file is modified and he/she presses "Cancel" when asked
0066      * to close the modified file.
0067      *
0068      * @return returns true if the closing was successful, otherwise false
0069      */
0070     bool close();
0071 
0072     StatusFlags flags() const;
0073     void setFlags(StatusFlags statusFlags);
0074     void addFlags(StatusFlags statusFlags);
0075     void removeFlags(StatusFlags statusFlags);
0076 
0077     QDateTime lastAccess() const;
0078     void setLastAccess(const QDateTime &dateTime = QDateTime::currentDateTime());
0079 
0080     QVector<KPluginMetaData> listOfServices();
0081     KPluginMetaData defaultService();
0082     KPluginMetaData currentService();
0083 
0084     friend class OpenFileInfoManager;
0085 
0086 Q_SIGNALS:
0087     void flagsChanged(OpenFileInfo::StatusFlags statusFlags);
0088 
0089 protected:
0090     OpenFileInfo(OpenFileInfoManager *openFileInfoManager, const QUrl &url);
0091     OpenFileInfo(OpenFileInfoManager *openFileInfoManager, const QString &mimeType = FileInfo::mimetypeBibTeX);
0092     void setUrl(const QUrl &url);
0093 
0094 private:
0095     class OpenFileInfoPrivate;
0096     OpenFileInfoPrivate *d;
0097 };
0098 
0099 Q_DECLARE_METATYPE(OpenFileInfo*)
0100 
0101 
0102 class OpenFileInfoManager: public QObject
0103 {
0104     Q_OBJECT
0105 
0106 public:
0107     typedef QVector<OpenFileInfo *> OpenFileInfoList;
0108 
0109     static OpenFileInfoManager &instance();
0110     ~OpenFileInfoManager() override;
0111 
0112     OpenFileInfo *createNew(const QString &mimeType = FileInfo::mimetypeBibTeX);
0113 
0114     /**
0115      * Open the given bibliography file as specified in the URL.
0116      * If the file is already open, an existing OpenFileInfo pointer will be
0117      * returned. If the file was not yet open, a new OpenFileInfo object will
0118      * be created and returned.
0119      * There shall be no two different OpenFileInfo objects representing
0120      * the same file.
0121      * @param url URL to bibliography file to open
0122      * @return an OpenFileInfo object representing the opened file
0123      */
0124     OpenFileInfo *open(const QUrl &url);
0125 
0126     OpenFileInfo *contains(const QUrl &url) const;
0127     OpenFileInfo *currentFile() const;
0128     bool changeUrl(OpenFileInfo *openFileInfo, const QUrl &url);
0129     bool close(OpenFileInfo *openFileInfo);
0130 
0131     /**
0132      * Try to close all open files. If a file is modified, the user will be asked
0133      * if the file shall be saved first. Depending on the KPart, the user may opt
0134      * to cancel the closing operation for any modified file.
0135      * If the user chooses to cancel, this function quits the closing process and
0136      * returns false. Files closed so far stay closed, the remaining open files stay
0137      * open.
0138      * If the user does not interrupt this function (e.g. no file was modified or
0139      * the user confirmed to save or to discard changes), all files get closed.
0140      * However, all files that were open before will be marked as opened
0141      * again. This could render the user interface inconsistent (therefore this
0142      * function should be called only when exiting KBibTeX), but it allows to
0143      * easily reopen in the next session all files that were open at the time
0144      * this function was called in this session.
0145      * @return true if all files could be closed successfully, else false.
0146      */
0147     bool queryCloseAll();
0148 
0149     void setCurrentFile(OpenFileInfo *openFileInfo, const KPluginMetaData &service = {});
0150     OpenFileInfoList filteredItems(OpenFileInfo::StatusFlag required, OpenFileInfo::StatusFlags forbidden = OpenFileInfo::StatusFlags());
0151 
0152     friend class OpenFileInfo;
0153 
0154 Q_SIGNALS:
0155     void currentChanged(OpenFileInfo *, const KPluginMetaData &service);
0156     void flagsChanged(OpenFileInfo::StatusFlags statusFlags);
0157 
0158 protected:
0159     explicit OpenFileInfoManager(QObject *parent);
0160 
0161 private:
0162     class OpenFileInfoManagerPrivate;
0163     OpenFileInfoManagerPrivate *d;
0164 
0165 private Q_SLOTS:
0166     void deferredListsChanged();
0167 };
0168 
0169 #endif // KBIBTEX_PROGRAM_OPENFILEINFO_H