1 /***************************************************************************
2  *   Copyright (C) 2004-2018 by Thomas Fischer <fischer@unix-ag.uni-kl.de> *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
16  ***************************************************************************/
17 
18 #ifndef KBIBTEX_IO_FILE_H
19 #define KBIBTEX_IO_FILE_H
20 
21 #include <QList>
22 #include <QStringList>
23 #include <QSharedPointer>
24 
25 #include "element.h"
26 
27 #ifdef HAVE_KF5
28 #include "kbibtexdata_export.h"
29 #endif // HAVE_KF5
30 
31 class Element;
32 
33 /**
34  * This class represents a bibliographic file such as a BibTeX file
35  * (or any other format after proper conversion). The file's content
36  * can be accessed using the inherited QList interface (for example
37  * list iterators).
38  * @see Element
39  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
40  */
41 class KBIBTEXDATA_EXPORT File : public QList<QSharedPointer<Element> >
42 {
43 public:
44     /// enum and flags to differ between entries, macros etc
45     /// used for @see #allKeys() and @see #containsKey()
46     enum ElementType {
47         etEntry = 0x1, etMacro = 0x2, etAll = 0x3
48     };
49     Q_DECLARE_FLAGS(ElementTypes, ElementType)
50 
51     /// used for property map
52     const static QString Url;
53     const static QString Encoding;
54     const static QString StringDelimiter;
55     const static QString QuoteComment;
56     const static QString KeywordCasing;
57     const static QString ProtectCasing;
58     const static QString NameFormatting;
59     const static QString ListSeparator;
60 
61     explicit File();
62     explicit File(const File &other);
63     explicit File(File &&other);
64     ~File();
65 
66     /// Copy-assignment operator.
67     File &operator= (const File &other);
68     /// Move-assignment operator.
69     File &operator= (File &&other);
70 
71     bool operator== (const File &other) const;
72     bool operator!= (const File &other) const;
73 
74     /**
75      * Check if a given key (e.g. a key for a macro or an id for an entry)
76      * is contained in the file object.
77      * @see #allKeys() const
78      * @return @c the object addressed by the key @c, NULL if no such file has been found
79      */
80     const QSharedPointer<Element> containsKey(const QString &key, ElementTypes elementTypes = etAll) const;
81 
82     /**
83      * Retrieves a list of all keys for example from macros or entries.
84      * @see #const containsKey(const QString &) const
85      * @return list of keys
86      */
87     QStringList allKeys(ElementTypes elementTypes = etAll) const;
88 
89     /**
90      * Retrieves a set of all unique values (as text) for a specified
91      * field from all entries
92      * @param fieldName field name to scan, e.g. "volume"
93      * @return list of unique values
94      */
95     QSet<QString> uniqueEntryValuesSet(const QString &fieldName) const;
96 
97     /**
98      * Retrieves a list of all unique values (as text) for a specified
99      * field from all entries
100      * @param fieldName field name to scan, e.g. "volume"
101      * @return list of unique values
102      */
103     QStringList uniqueEntryValuesList(const QString &fieldName) const;
104 
105     void setProperty(const QString &key, const QVariant &value);
106     QVariant property(const QString &key) const;
107     QVariant property(const QString &key, const QVariant &defaultValue) const;
108     bool hasProperty(const QString &key) const;
109     void setPropertiesToDefault();
110 
111     /**
112      * Check if this File object is valid by its own assessment.
113      * No high-level checks, such as on the File instance's content,
114      * are performed.
115      * @return True if validity checks succeed, false otherwise
116      */
117     bool checkValidity() const;
118 
119 private:
120     class FilePrivate;
121     FilePrivate *d;
122 };
123 
124 Q_DECLARE_METATYPE(File *)
125 
126 QDebug operator<<(QDebug dbg, const File &file);
127 
128 #endif // KBIBTEX_IO_FILE_H
129