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_ENTRY_H
0021 #define KBIBTEX_DATA_ENTRY_H
0022 
0023 #include <QMap>
0024 
0025 #include <Element>
0026 #include <Value>
0027 
0028 class File;
0029 
0030 /**
0031  * This class represents an entry in a BibTeX file such as an article
0032  * or a book. This class is essentially a map from keys such as title,
0033  * year or other bibliography data to corresponding values.
0034  * @see Value
0035  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0036  */
0037 class KBIBTEXDATA_EXPORT Entry : public Element, public QMap<QString, Value>
0038 {
0039 public:
0040     /** Representation of the BibTeX field key "abstract" */
0041     static const QString ftAbstract;
0042     /** Representation of the BibTeX field key "address" */
0043     static const QString ftAddress;
0044     /** Representation of the BibTeX field key "author" */
0045     static const QString ftAuthor;
0046     /** Representation of the BibTeX field key "booktitle" */
0047     static const QString ftBookTitle;
0048     /** Representation of the BibTeX field key "chapter" */
0049     static const QString ftChapter;
0050     /** Representation of the BibTeX field key "x-color" */
0051     static const QString ftColor;
0052     /** Representation of the BibTeX field key "comment" */
0053     static const QString ftComment;
0054     /** Representation of the BibTeX field key "crossref" */
0055     static const QString ftCrossRef;
0056     /** Representation of the BibTeX field key "doi" */
0057     static const QString ftDOI;
0058     /** Representation of the BibTeX field key "edition" */
0059     static const QString ftEdition;
0060     /** Representation of the BibTeX field key "editor" */
0061     static const QString ftEditor;
0062     /** Representation of the BibTeX field key "file" */
0063     static const QString ftFile;
0064     /** Representation of the BibTeX field key "issn" */
0065     static const QString ftISSN;
0066     /** Representation of the BibTeX field key "isbn" */
0067     static const QString ftISBN;
0068     /** Representation of the BibTeX field key "journal" */
0069     static const QString ftJournal;
0070     /** Representation of the BibTeX field key "keywords" */
0071     static const QString ftKeywords;
0072     /** Representation of the BibTeX field key "localfile" */
0073     static const QString ftLocalFile;
0074     /** Representation of the BibTeX field key "location" */
0075     static const QString ftLocation;
0076     /** Representation of the BibTeX field key "month" */
0077     static const QString ftMonth;
0078     /** Representation of the BibTeX field key "note" */
0079     static const QString ftNote;
0080     /** Representation of the BibTeX field key "number" */
0081     static const QString ftNumber;
0082     /** Representation of the BibTeX field key "pages" */
0083     static const QString ftPages;
0084     /** Representation of the BibTeX field key "publisher" */
0085     static const QString ftPublisher;
0086     /** Representation of the BibTeX field key "school" */
0087     static const QString ftSchool;
0088     /** Representation of the BibTeX field key "series" */
0089     static const QString ftSeries;
0090     /** Representation of the BibTeX field key "x-stars" */
0091     static const QString ftStarRating;
0092     /** Representation of the BibTeX field key "title" */
0093     static const QString ftTitle;
0094     /** Representation of the BibTeX field key "url" */
0095     static const QString ftUrl;
0096     /** Representation of the BibLaTeX field key "urldate" */
0097     static const QString ftUrlDate;
0098     /** Representation of the BibTeX field key "volume" */
0099     static const QString ftVolume;
0100     /** Representation of the BibTeX field key "year" */
0101     static const QString ftYear;
0102 
0103     /** Representation of the BibLaTeX field key "year" */
0104     static const QString ftXData;
0105 
0106     /** Representation of the BibTeX entry type "Article" */
0107     static const QString etArticle;
0108     /** Representation of the BibTeX entry type "Book" */
0109     static const QString etBook;
0110     /** Representation of the BibTeX entry type "InBook" */
0111     static const QString etInBook;
0112     /** Representation of the BibTeX entry type "InProceedings" */
0113     static const QString etInProceedings;
0114     /** Representation of the BibTeX entry type "Proceedings" */
0115     static const QString etProceedings;
0116     /** Representation of the BibTeX entry type "Misc" */
0117     static const QString etMisc;
0118     /** Representation of the BibTeX entry type "TechReport" */
0119     static const QString etTechReport;
0120     /** Representation of the BibTeX entry type "PhDThesis" */
0121     static const QString etPhDThesis;
0122     /** Representation of the BibTeX entry type "MastersThesis" */
0123     static const QString etMastersThesis;
0124     /** Representation of the BibTeX entry type "Unpublished" */
0125     static const QString etUnpublished;
0126 
0127     /**
0128      * Create a new entry type. Both type and id are optionally,
0129      * allowing to call the constructor as Entry() only.
0130      * Both type and id can be set and retrieved later.
0131      * @param type type of this entry
0132      * @param id identifier of this entry
0133      */
0134     explicit Entry(const QString &type = QString(), const QString &id = QString());
0135 
0136     /**
0137      * Copy constructor cloning another entry object.
0138      * @param other entry object to clone
0139      */
0140     Entry(const Entry &other);
0141 
0142     ~Entry() override;
0143 
0144     bool operator==(const Entry &other) const;
0145     bool operator!=(const Entry &other) const;
0146 
0147     /**
0148      * Assignment operator, working similar to a copy constructor,
0149      * but overwrites the current object's values.
0150      */
0151     Entry &operator= (const Entry &other);
0152 
0153     /**
0154      * Set the type of this entry. Common values are "article" or "book".
0155      * @param type type of this entry
0156      */
0157     void setType(const QString &type);
0158 
0159     /**
0160      * Retrieve the type of this entry. Common values are "article" or "book".
0161      * @return type of this entry
0162      */
0163     QString type() const;
0164 
0165     /**
0166      * Set the id of this entry. In LaTeX, this id is used to refer to a BibTeX
0167      * entry using the "ref" command.
0168      * @param id id of this entry
0169      */
0170     void setId(const QString &id);
0171 
0172     /**
0173      * Retrieve the id of this entry. In LaTeX, this id is used to refer to a BibTeX
0174      * entry using the "ref" command.
0175      * @return id of this entry
0176      */
0177     QString id() const;
0178 
0179     /**
0180      * Re-implementation of QMap's value function, but performing a case-insensitive
0181      * match on the key. E.g. querying for key "title" will find a key-value pair with
0182      * key "TITLE".
0183      * @see contains
0184      * @param key field name to search for
0185      * @return found value or Value() if nothing found
0186      */
0187     const Value value(const QString &key) const;
0188 
0189     int remove(const QString &key);
0190 
0191     /**
0192      * Re-implementation of QMap's contains function, but performing a case-insensitive
0193      * match on the key. E.g. querying for key "title" will find a key-value pair with
0194      * key "TITLE".
0195      * @see value
0196      * @param key field name to search for
0197      * @return true if value with key found, else false
0198      */
0199     bool contains(const QString &key) const;
0200 
0201     /**
0202      * Resolve cross references in an entry. This function evaluates known cross
0203      * reference fields such as 'crossref' from BibTeX and 'xdata' from BibLaTeX.
0204      * Fields that exist in cross-referenced entries will be copied in the entry
0205      * that this function will eventually return.
0206      * This function always returns a new Entry that must be deleted by the caller,
0207      * even if no cross reference fields existed in the original entry or no fields
0208      * were copied from cross-referenced entries.
0209      * This new Entry will not be part of any File object, not even the one passed
0210      * to this function.
0211      * Tip: As the returned Entry is most often only used temporary, it may be a
0212      * good idea to wrap it into a QScopedPointer<const Entry> at the caller's side.
0213      * @param bibTeXfile bibliography to search for entries being cross-referenced
0214      * @return New entry object with known cross references resolved
0215      */
0216     QSharedPointer<Entry> resolveCrossref(const File *bibTeXfile) const;
0217 
0218     static QStringList authorsLastName(const Entry &entry);
0219     QStringList authorsLastName() const;
0220 
0221     quint64 uniqueId() const;
0222 
0223     /**
0224      * Cheap and fast test if another Element is a Entry object.
0225      * @param other another Element object to test
0226      * @return true if Element is actually a Entry
0227      */
0228     static bool isEntry(const Element &other);
0229 
0230 private:
0231     /// Unique numeric identifier
0232     const quint64 internalUniqueId;
0233     /// Keeping track of next available unique numeric identifier
0234     static quint64 internalUniqueIdCounter;
0235 
0236     class EntryPrivate;
0237     EntryPrivate *const d;
0238 };
0239 
0240 Q_DECLARE_METATYPE(Entry*)
0241 
0242 KBIBTEXDATA_EXPORT QDebug operator<<(QDebug dbg, const Entry *Entry);
0243 
0244 /**
0245  * Comparison operator, necessary for QMap operations.
0246  */
0247 static inline bool operator< (const QSharedPointer<Entry> &a, const QSharedPointer<Entry> &b)
0248 {
0249     return a->uniqueId() < b->uniqueId();
0250 }
0251 
0252 #endif // KBIBTEX_DATA_ENTRY_H