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

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2022 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_IO_FILEIMPORTER_H
0021 #define KBIBTEX_IO_FILEIMPORTER_H
0022 
0023 #include <QObject>
0024 
0025 #ifdef HAVE_KF
0026 #include "kbibtexio_export.h"
0027 #endif // HAVE_KF
0028 
0029 class QIODevice;
0030 class QFileInfo;
0031 
0032 class File;
0033 class Person;
0034 
0035 /**
0036 @author Thomas Fischer
0037  */
0038 class KBIBTEXIO_EXPORT FileImporter : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     enum class MessageSeverity {
0044         Info, ///< Messages that are of informative type, such as additional comma for last key-value pair in BibTeX entry
0045         Warning, ///< Messages that are of warning type, such as automatic corrections of BibTeX code without loss of information
0046         Error ///< Messages that are of error type, which point to issue where information may get lost, e.g. invalid syntax or incomplete data
0047     };
0048 
0049     explicit FileImporter(QObject *parent);
0050     ~FileImporter() override;
0051 
0052     static FileImporter *factory(const QFileInfo &fileInfo, QObject *parent);
0053     static FileImporter *factory(const QUrl &url, QObject *parent);
0054 
0055     /**
0056      * @brief Load a bibliography from a textual representation.
0057      * @param text textual representation of the bibliography
0058      * @return bibliography object if sucessful, @c nullptr on failure
0059      */
0060     virtual File *fromString(const QString &text);
0061 
0062     /**
0063      * @brief Load a bibliography from a @c QIODevice like file.
0064      * @param iodevice Device to read the bibliography's data from
0065      * @return bibliography object if sucessful, @c nullptr on failure
0066      */
0067     virtual File *load(QIODevice *iodevice) = 0;
0068 
0069     /**
0070       * When importing data, show a dialog where the user may select options on the
0071       * import process such as selecting encoding. Re-implementing this function is
0072       * optional and should only be done if user interaction is necessary at import
0073       * actions.
0074       * Return true if the configuration step was successful and the application
0075       * may proceed. If returned false, the import process has to be stopped.
0076       * The importer may store configurations done here for future use (e.g. set default
0077       * values based on user input).
0078       * A calling application should call this function before calling load() or similar
0079       * functions.
0080       * The implementer may choose to show or not show a dialog, depending on e.g. if
0081       * additional information is necessary or not.
0082       */
0083     virtual bool showImportDialog(QWidget *parent) {
0084         Q_UNUSED(parent)
0085         return true;
0086     }
0087 
0088     static bool guessCanDecode(const QString &) {
0089         return false;
0090     }
0091 
0092     /**
0093       * Split a person's name into its parts and construct a Person object from them.
0094       * This is a rather general functions and takes e.g. the curly brackets used in
0095       * (La)TeX not into account.
0096       * @param name The persons name
0097       * @return A Person object containing the name
0098       * @see Person
0099       */
0100     static Person *splitName(const QString &name);
0101 
0102 private:
0103     static bool looksLikeSuffix(const QString &suffix);
0104 
0105 Q_SIGNALS:
0106     void progress(int current, int total);
0107 
0108     /**
0109      * Signal to notify the user of a FileImporter class about issues detected
0110      * during loading and parsing bibliographic data. Messages may be of various
0111      * severity levels. The message text may reveal additional information on
0112      * what the issue is and where it has been found (e.g. line number).
0113      * Implementations of FileImporter are recommended to print a similar message
0114      * as debug output.
0115      * TODO messages shall get i18n'ized if the code is compiled with/linked against
0116      * KDE Frameworks libraries.
0117      *
0118      * @param severity The message's severity level
0119      * @param messageText The message's text
0120      */
0121     void message(const FileImporter::MessageSeverity severity, const QString &messageText);
0122 
0123 public Q_SLOTS:
0124     virtual void cancel() {
0125         // nothing
0126     }
0127 };
0128 
0129 Q_DECLARE_METATYPE(FileImporter::MessageSeverity)
0130 
0131 #endif // KBIBTEX_IO_FILEIMPORTER_H