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

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_DATA_FILE_H
0021 #define KBIBTEX_DATA_FILE_H
0022 
0023 #include <QList>
0024 #include <QStringList>
0025 #include <QSharedPointer>
0026 
0027 #ifdef HAVE_KF
0028 #include "kbibtexdata_export.h"
0029 #endif // HAVE_KF
0030 
0031 class Element;
0032 
0033 /**
0034  * This class represents a bibliographic file such as a BibTeX file
0035  * (or any other format after proper conversion). The file's content
0036  * can be accessed using the inherited QList interface (for example
0037  * list iterators).
0038  * @see Element
0039  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0040  */
0041 class KBIBTEXDATA_EXPORT File : public QList<QSharedPointer<Element> >
0042 {
0043 public:
0044     /// enum and flags to differ between entries, macros etc
0045     /// used for @see #allKeys() and @see #containsKey()
0046     enum class ElementType {
0047         Entry = 0x1, Macro = 0x2, All = Entry | Macro
0048     };
0049     Q_DECLARE_FLAGS(ElementTypes, ElementType)
0050 
0051     /// used for property map
0052     const static QString Url;
0053     const static QString Encoding;
0054     const static QString StringDelimiter;
0055     const static QString CommentContext;
0056     const static QString CommentPrefix;
0057     const static QString KeywordCasing;
0058     const static QString ProtectCasing;
0059     const static QString NameFormatting;
0060     const static QString ListSeparator;
0061     const static QString SortedByIdentifier;
0062 
0063     explicit File();
0064     explicit File(const File &other);
0065     explicit File(File &&other);
0066     ~File();
0067 
0068     /// Copy-assignment operator.
0069     File &operator= (const File &other);
0070     /// Move-assignment operator.
0071     File &operator= (File &&other);
0072 
0073     bool operator== (const File &other) const;
0074     bool operator!= (const File &other) const;
0075 
0076     /**
0077      * Check if a given key (e.g. a key for a macro or an id for an entry)
0078      * is contained in the file object.
0079      * @see allKeys
0080      * @return the object addressed by the key, @c nullptr if no such file has been found
0081      */
0082     const QSharedPointer<Element> containsKey(const QString &key, ElementTypes elementTypes = ElementType::All) const;
0083 
0084     /**
0085      * Retrieves a list of all keys for example from macros or entries.
0086      * @see containsKey
0087      * @return list of keys
0088      */
0089     QStringList allKeys(ElementTypes elementTypes = ElementType::All) const;
0090 
0091     /**
0092      * Retrieves a set of all unique values (as text) for a specified
0093      * field from all entries
0094      * @param fieldName field name to scan, e.g. "volume"
0095      * @return list of unique values
0096      */
0097     QSet<QString> uniqueEntryValuesSet(const QString &fieldName) const;
0098 
0099     /**
0100      * Retrieves a list of all unique values (as text) for a specified
0101      * field from all entries
0102      * @param fieldName field name to scan, e.g. "volume"
0103      * @return list of unique values
0104      */
0105     QStringList uniqueEntryValuesList(const QString &fieldName) const;
0106 
0107     void setProperty(const QString &key, const QVariant &value);
0108     QVariant property(const QString &key) const;
0109     QVariant property(const QString &key, const QVariant &defaultValue) const;
0110     bool hasProperty(const QString &key) const;
0111     void setPropertiesToDefault();
0112 
0113     /**
0114      * Check if this File object is valid by its own assessment.
0115      * No high-level checks, such as on the File instance's content,
0116      * are performed.
0117      * @return True if validity checks succeed, false otherwise
0118      */
0119     bool checkValidity() const;
0120 
0121     /**
0122      * Sort a BibTeX file by identifier if comparing two entries
0123      * @param bibtexfile The original File object, will not be modified
0124      * @return Sorted copy of the original File object
0125      */
0126     static const File *sortByIdentifier(const File *bibtexfile);
0127 
0128 private:
0129     class FilePrivate;
0130     FilePrivate *d;
0131 };
0132 
0133 Q_DECLARE_METATYPE(File*)
0134 
0135 KBIBTEXDATA_EXPORT QDebug operator<<(QDebug dbg, const File *file);
0136 
0137 #endif // KBIBTEX_DATA_FILE_H