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

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 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_FILEEXPORTER_H
0021 #define KBIBTEX_IO_FILEEXPORTER_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 Element;
0034 
0035 /**
0036  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0037  */
0038 class KBIBTEXIO_EXPORT FileExporter : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     enum class MessageSeverity {
0044         Info, ///< Messages that are of informative type
0045         Warning, ///< Messages that are of warning type
0046         Error ///< Messages that are of error type
0047     };
0048 
0049     explicit FileExporter(QObject *parent);
0050     ~FileExporter() override;
0051 
0052     static FileExporter *factory(const QFileInfo &fileInfo, const QString &exporterClassHint, QObject *parent);
0053     static FileExporter *factory(const QUrl &url, const QString &exporterClassHint, QObject *parent);
0054     static QVector<QString> exporterClasses(const QFileInfo &fileInfo);
0055     static QVector<QString> exporterClasses(const QUrl &url);
0056 
0057     /**
0058      * @brief Convert an element into a string representation.
0059      * @param[in] element Element to be converted into a string
0060      * @param[in] bibtexfile Bibliography which may provide additional insights; may be @c nullptr
0061      * @param[out] errorLog List of strings that receives error messages; may be @c nullptr
0062      * @return String representation of provided element
0063      */
0064     virtual QString toString(const QSharedPointer<const Element> &element, const File *bibtexfile);
0065 
0066     /**
0067      * @brief Convert a bibliography into a string representation.
0068      * @param[in] bibtexfile Bibliography to be converted into a string
0069      * @param[out] errorLog List of strings that receives error messages; may be @c nullptr
0070      * @return String representation of provided bibliography
0071      */
0072     virtual QString toString(const File *bibtexfile);
0073 
0074     /**
0075      * Write a bibliography into a @c QIODevice like a file.
0076      * This function requires the @c iodevice to be open for write operations (@c QIODevice::WriteOnly).
0077      * The function will not close the @c iodevice upon return.
0078      * @param[in] element Bibliography to be written into the @c QIODevice
0079      * @param[out] errorLog List of strings that receives error messages; may be @c nullptr
0080      * @return @c true if writing the bibliography succeeded, else @c false
0081      */
0082     virtual bool save(QIODevice *iodevice, const File *bibtexfile) = 0;
0083 
0084     /**
0085      * Write an element into a @c QIODevice like a file.
0086      * This function requires the @c iodevice to be open for write operations (@c QIODevice::WriteOnly).
0087      * The function will not close the @c iodevice upon return.
0088      * @param[in] element Element to be written into the @c QIODevice
0089      * @param[in] bibtexfile Bibliography which may provide additional insights; may be @c nullptr
0090      * @param[out] errorLog List of strings that receives error messages; may be @c nullptr
0091      * @return @c true if writing the element succeeded, else @c false
0092      */
0093     virtual bool save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile) = 0;
0094 
0095     static QString numberToOrdinal(const int number, bool onlyText = true);
0096 
0097     /**
0098      * Convert a textual representation of an edition string into a number.
0099      * Examples for supported string patterns include '4', '4th', or 'fourth'.
0100      * Success of the conversion is returned via the @c ok variable, where the
0101      * function caller has to provide a pointer to a boolean variable.
0102      * In case of success, the function's result is the edition, in case
0103      * of failure, i.e. @c *ok==false, the result is undefined.
0104      * @param[in] editionString A string representing an edition number
0105      * @param[out] ok Pointer to a boolean variable used to return the success (@c true) or failure (@c false) state of the conversion; must not be @c nullptr
0106      * @return In case of success, the edition as a positive int, else undefined
0107      */
0108     static int editionStringToNumber(const QString &editionString, bool *ok);
0109 
0110     /**
0111      * Convert a textual representation of a month string into a number.
0112      * Examples for supported string patterns include 'January', 'jan', or '1'.
0113      * Success of the conversion is returned via the @c ok variable, where the
0114      * function caller has to provide a pointer to a boolean variable.
0115      * In case of success, the function's result is the month in the range
0116      * 1 (January) to 12 (December), in case of failure, i.e. @c *ok==false,
0117      * the result is undefined.
0118      * @param[in] monthString A string representing a month
0119      * @param[out] ok Pointer to a boolean variable used to return the success (@c true) or failure (@c false) state of the conversion; must not be @c nullptr
0120      * @return In case of success, the month as a positive int in the range 1 to 12, else undefined
0121      */
0122     static int monthStringToNumber(const QString &monthString, bool *ok = nullptr);
0123 
0124 Q_SIGNALS:
0125     /**
0126      * @brief Signal to notify the caller of this class's functions about the progress.
0127      * @param[out] current Current progress, a non-negative number, less or equal @c total
0128      * @param[out] total Maximum value for progress, a non-negative number, greater or equal @c current
0129      */
0130     void progress(int current, int total);
0131 
0132     /**
0133      * Signal to notify the user of a FileExporter class about issues detected
0134      * during loading and parsing bibliographic data. Messages may be of various
0135      * severity levels. The message text may reveal additional information on
0136      * what the issue is and where it has been found (e.g. line number).
0137      * Implementations of FileExporter are recommended to print a similar message
0138      * as debug output.
0139      * TODO messages shall get i18n'ized if the code is compiled with/linked against
0140      * KDE Frameworks libraries.
0141      *
0142      * @param severity The message's severity level
0143      * @param messageText The message's text
0144      */
0145     void message(const FileExporter::MessageSeverity severity, const QString &messageText);
0146 
0147 public Q_SLOTS:
0148     virtual void cancel() {
0149         // nothing
0150     }
0151 };
0152 
0153 Q_DECLARE_METATYPE(FileExporter::MessageSeverity)
0154 
0155 #endif // KBIBTEX_IO_FILEEXPORTER_H