File indexing completed on 2024-04-28 05:08:18

0001 /***************************************************************************
0002     Copyright (C) 2001-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_ENTRY_H
0026 #define TELLICO_ENTRY_H
0027 
0028 #include "datavectors.h"
0029 #include "fieldformat.h"
0030 
0031 #include <QStringList>
0032 #include <QHash>
0033 
0034 namespace Tellico {
0035 
0036   namespace Data {
0037     class Collection;
0038     class EntryGroup;
0039 
0040 /**
0041  * The Entry class represents a book, a CD, or whatever is the basic entity
0042  * in the collection.
0043  *
0044  * Each Entry object has a set of field values, such as title, artist, or format,
0045  * and must belong to a collection. A unique id number identifies each entry.
0046  *
0047  * @see Field
0048  *
0049  * @author Robby Stephenson
0050  */
0051 class Entry : public QSharedData {
0052 
0053 public:
0054   /**
0055    * The constructor, which automatically sets the id to the current number
0056    * of entries in the collection.
0057    *
0058    * @param coll A pointer to the parent collection object
0059    */
0060   Entry(CollPtr coll);
0061   Entry(CollPtr coll, ID id);
0062   /**
0063    * The copy constructor, needed since the id must be different.
0064    */
0065   Entry(const Entry& entry);
0066   /**
0067    * The assignment operator is overloaded, since the id must be different.
0068    */
0069   Entry& operator=(const Entry& other);
0070 
0071   ~Entry();
0072 
0073   /**
0074    * Every entry has a title.
0075    *
0076    * @return The entry title
0077    */
0078   QString title(FieldFormat::Request formatted = FieldFormat::DefaultFormat) const;
0079   /**
0080    * Returns the value of the field with a given key name.
0081    *
0082    * @param fieldName The field name
0083    * @return The value of the field
0084    */
0085   QString field(const QString& fieldName) const;
0086   QString field(Data::FieldPtr field) const;
0087   /**
0088    * Returns the formatted value of the field with a given key name.
0089    *
0090    * @param field The field
0091    * @return The formatted value of the field
0092    */
0093   QString formattedField(const QString& fieldName,
0094                          FieldFormat::Request formatted = FieldFormat::DefaultFormat) const;
0095   QString formattedField(Data::FieldPtr field,
0096                          FieldFormat::Request formatted = FieldFormat::DefaultFormat) const;
0097   /**
0098    * Sets the value of an field for the entry. The method first verifies that
0099    * the value is allowed for that particular key.
0100    *
0101    * @param fieldName The name of the field
0102    * @param value The value of the field
0103    * @param updateMDate Whether to update the modified date of the entry
0104    * @return A boolean indicating whether or not the field was successfully set
0105    */
0106   bool setField(const QString& fieldName, const QString& value, bool updateMDate=true);
0107   bool setField(Data::FieldPtr field, const QString& value, bool updateMDate=true);
0108   /**
0109    * Returns a pointer to the parent collection of the entry.
0110    *
0111    * @return The collection pointer
0112    */
0113   CollPtr collection() const;
0114   /**
0115    * Changes the collection owner of the entry
0116    */
0117   void setCollection(CollPtr coll);
0118   /**
0119    * Returns the id of the entry
0120    *
0121    * @return The id
0122    */
0123   ID id() const { return m_id; }
0124   void setId(ID id) { m_id = id; }
0125   /**
0126    * Adds the entry to a group. The group list within the entry is updated
0127    * and the entry is added to the group.
0128    *
0129    * @param group The group
0130    * @return a bool indicating if it was successfully added. If the entry was already
0131    * in the group, the return value is false
0132    */
0133   bool addToGroup(EntryGroup* group);
0134   /**
0135    * Removes the entry from a group. The group list within the entry is updated
0136    * and the entry is removed from the group.
0137    *
0138    * @param group The group
0139    * @return a bool indicating if the group was successfully removed
0140    */
0141   bool removeFromGroup(EntryGroup* group);
0142   void clearGroups();
0143   /**
0144    * Returns a list of the groups to which the entry belongs
0145    *
0146    * @return The list of groups
0147    */
0148   const QList<EntryGroup*>& groups() const { return m_groups; }
0149   /**
0150    * Returns a list containing the names of the groups for
0151    * a certain field to which the entry belongs
0152    *
0153    * @param fieldName The name of the field
0154    * @return The list of names
0155    */
0156   QStringList groupNamesByFieldName(const QString& fieldName) const;
0157   /**
0158    * Returns a list of all the field values contained in the entry.
0159    *
0160    * @return The list of field values
0161    */
0162   QStringList fieldValues() const { return m_fieldValues.values(); }
0163   /**
0164    * Returns a list of all the formatted field values contained in the entry.
0165    *
0166    * @return The list of field values
0167    */
0168   QStringList formattedFieldValues() const { return m_formattedFields.values(); }
0169   /**
0170    * Returns a boolean indicating if the entry's parent collection recognizes
0171    * it existence, that is, the parent collection has this entry in its list.
0172    *
0173    * @return Whether the entry is owned or not
0174    */
0175   bool isOwned();
0176   /**
0177    * Removes the formatted value of the field from the map. This should be used when
0178    * the field's format flag has changed.
0179    *
0180    * @param name The name of the field that changed. an empty string means invalidate all fields.
0181    */
0182   void invalidateFormattedFieldValue(const QString& name=QString());
0183 
0184 private:
0185   // not used
0186   Entry();
0187 
0188   bool operator==(const Entry& other) const;
0189 
0190   bool setFieldImpl(const QString& fieldName, const QString& value);
0191 
0192   CollPtr m_coll;
0193   ID m_id;
0194   QHash<QString, QString> m_fieldValues;
0195   mutable QHash<QString, QString> m_formattedFields;
0196   QList<EntryGroup*> m_groups;
0197 };
0198 
0199 class EntryCmp {
0200 
0201 public:
0202   EntryCmp(const QString& field) : m_field(field) {}
0203 
0204   bool operator()(EntryPtr e1, EntryPtr e2) const {
0205     return e1->field(m_field) < e2->field(m_field);
0206   }
0207 
0208 private:
0209   QString m_field;
0210 };
0211 
0212   } // end namespace
0213 } // end namespace
0214 
0215 Q_DECLARE_METATYPE(Tellico::Data::EntryPtr)
0216 
0217 #endif