File indexing completed on 2025-01-19 03:53:36

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-09-19
0007  * Description : Access to comments of an item in the database
0008  *
0009  * SPDX-FileCopyrightText: 2007-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2008      by Patrick Spendrin <ps_ml at gmx dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_ITEM_COMMENTS_H
0018 #define DIGIKAM_ITEM_COMMENTS_H
0019 
0020 // Qt includes
0021 
0022 #include <QString>
0023 #include <QDateTime>
0024 #include <QSharedDataPointer>
0025 #include <QSharedData>
0026 #include <QSet>
0027 
0028 // Local includes
0029 
0030 #include "digikam_export.h"
0031 #include "coredbalbuminfo.h"
0032 #include "captionvalues.h"
0033 #include "coredbaccess.h"
0034 
0035 namespace Digikam
0036 {
0037 
0038 class DIGIKAM_DATABASE_EXPORT ItemComments
0039 {
0040 public:
0041 
0042     /**
0043      * The ItemComments class shall provide short-lived objects that provide read/write access
0044      * to the comments stored in the database. It is a mere wrapper around the less
0045      * convenient access methods in CoreDB.
0046      * Database results are cached, but the object will not listen to database changes from other places.
0047      *
0048      * Changes are applied to the database only after calling apply(), which you can call any time
0049      * and which will in any case be called from the destructor.
0050      */
0051 
0052     enum LanguageChoiceBehavior
0053     {
0054         /**
0055          *  Return only a comment if the language code
0056          *  (at least the language code, the country part may differ)
0057          *  is identical. Else returns a null QString.
0058          */
0059         ReturnMatchingLanguageOnly,
0060 
0061         /**
0062          * If no matching language as above is found, return the default language.
0063          */
0064         ReturnMatchingOrDefaultLanguage,
0065 
0066         /**
0067          * If no matching or default language is found, return the first comment.
0068          * Returns a null string only if no comment is available.
0069          */
0070         ReturnMatchingDefaultOrFirstLanguage
0071     };
0072 
0073     enum UniqueBehavior
0074     {
0075         /**
0076          * Allow only one comment per language. Default setting.
0077          */
0078         UniquePerLanguage,
0079 
0080         /**
0081          * Allow multiple comments per language, each with a different author
0082          */
0083         UniquePerLanguageAndAuthor
0084     };
0085 
0086 public:
0087 
0088     /**
0089      * Create a null ItemComments object
0090      */
0091     ItemComments();
0092 
0093     /**
0094      * Create a ItemComments object for the image with the specified id.
0095      */
0096     explicit ItemComments(qlonglong imageid);
0097 
0098     /**
0099      * Create a ItemComments object for the image with the specified id.
0100      * The existing CoreDbAccess object will be used to access the database.
0101      */
0102     ItemComments(const CoreDbAccess& access, qlonglong imageid);
0103 
0104     ItemComments(const ItemComments& other);
0105     ~ItemComments();
0106 
0107     ItemComments& operator=(const ItemComments& other);
0108 
0109     bool isNull()                                                                                               const;
0110 
0111     /**
0112      * Changes the behavior to unique comments per language, see the enum above for possible
0113      * values.
0114      * Default value is UniquePerLanguage.
0115      * Note: This is _not_ a property of the database, but only of this single ItemComments object,
0116      */
0117     void setUniqueBehavior(UniqueBehavior behavior);
0118 
0119     /**
0120      * This methods presents one of the comment strings of the available comment
0121      * as the default value, when you just want to have one string.
0122      * Optionally also returns the index with which you can access further information about the comment.
0123      */
0124     QString defaultComment(DatabaseComment::Type type = DatabaseComment::Comment)                               const;
0125 
0126     QString defaultComment(int* const index, Digikam::DatabaseComment::Type type = DatabaseComment::Comment)    const;
0127 
0128     /**
0129      * Returns a comment for the specified language.
0130      * Matching behavior can be specified.
0131      * Optionally also returns the index with which you can access further information about the comment.
0132      */
0133     QString commentForLanguage(const QString& languageCode, int* const index = nullptr,
0134                                LanguageChoiceBehavior behavior = ReturnMatchingDefaultOrFirstLanguage)          const;
0135 
0136     /**
0137      * Returns the number of comments available.
0138      */
0139     int numberOfComments()                                                                                      const;
0140 
0141     /**
0142      * Access individual properties. Please ensure that the specified index is a valid index
0143      */
0144     DatabaseComment::Type type(int index)                                                                       const;
0145 
0146     /**
0147      * RFC 3066 notation, or "x-default"
0148      */
0149     QString language(int index)                                                                                 const;
0150 
0151     QString author(int index)                                                                                   const;
0152     QDateTime date(int index)                                                                                   const;
0153     QString comment(int index)                                                                                  const;
0154 
0155     /**
0156      * Add a new comment to the list of normal image comments, specified with language and author.
0157      * Checking for unique comments is done as set by setUniqueBehavior.
0158      * If you pass a null string as language, it will be translated to the language code designating
0159      * the default language ("x-default").
0160      * If you just want to change the one comment of the image, call addComment(myComment);
0161      */
0162     void addComment(const QString& comment,
0163                     const QString& language = QString(),
0164                     const QString& author = QString(),
0165                     const QDateTime& date = QDateTime(),
0166                     DatabaseComment::Type type = DatabaseComment::Comment);
0167 
0168     /**
0169      * Convenience method to add a comment of type Headline. Calls addComment, see above for more info.
0170      */
0171     void addHeadline(const QString& headline,
0172                      const QString& language = QString(),
0173                      const QString& author = QString(),
0174                      const QDateTime& date = QDateTime());
0175 
0176     /**
0177      * Convenience method to add a comment of type Headline. Calls addComment, see above for more info.
0178      */
0179     void addTitle(const QString& title,
0180                   const QString& language = QString(),
0181                   const QString& author = QString(),
0182                   const QDateTime& date = QDateTime());
0183 
0184     /**
0185      * Replaces all existing comments with the given set of comments and associated language.
0186      * Optionally date and author can be specified in CaptionsMap container.
0187      */
0188     void replaceComments(const CaptionsMap& comments,
0189                          DatabaseComment::Type type = DatabaseComment::Comment);
0190 
0191     /**
0192      * Remove the entry referred to by index.
0193      */
0194     void remove(int index);
0195 
0196     /**
0197      * Remove all entries of the given type
0198      */
0199     void removeAll(DatabaseComment::Type type);
0200 
0201     /**
0202      * Convenience method: remove all entries of type Comment
0203      */
0204     void removeAllComments();
0205 
0206     /**
0207      * Remove all entries of all types: Comments, Headlines, Titles
0208      */
0209     void removeAll();
0210 
0211     /**
0212      * Access individual properties.
0213      * Please ensure that the specified index is a valid index
0214      */
0215     void changeComment(int index, const QString& comment);
0216     void changeLanguage(int index, const QString& language);
0217     void changeAuthor(int index, const QString& author);
0218     void changeDate(int index, const QDateTime& date);
0219     void changeType(int index, DatabaseComment::Type type);
0220 
0221     /**
0222      * Apply all changes.
0223      * Also called in destructor, so you typically do not need to call this.
0224      */
0225     void apply();
0226     void apply(CoreDbAccess& access);
0227 
0228     /**
0229      * Returns all entries of the given type in a CaptionsMap container.
0230      */
0231     CaptionsMap toCaptionsMap(DatabaseComment::Type = DatabaseComment::Comment)                                 const;
0232 
0233     /**
0234      * Replaces all entries in this object with all entries from source.
0235      */
0236     void replaceFrom(const ItemComments& source);
0237 
0238 protected:
0239 
0240     void addCommentDirectly(const QString& comment,
0241                             const QString& language,
0242                             const QString& author,
0243                             DatabaseComment::Type type,
0244                             const QDateTime& date);
0245 public:
0246 
0247     class Private;
0248 
0249 protected:
0250 
0251     QSharedDataPointer<Private> d;
0252 };
0253 
0254 } // namespace Digikam
0255 
0256 #endif // DIGIKAM_ITEM_COMMENTS_H