File indexing completed on 2024-05-19 04:55:57

0001 /**
0002  * \file tagconfig.h
0003  * Tag related configuration.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Jun 2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QStringList>
0030 #include <QScopedPointer>
0031 #include <QVariantList>
0032 #include "generalconfig.h"
0033 #include "kid3api.h"
0034 
0035 class StarRatingMapping;
0036 
0037 /**
0038  * Tag related configuration.
0039  */
0040 class KID3_CORE_EXPORT TagConfig : public StoredConfig<TagConfig> {
0041   Q_OBJECT
0042   /** features provided by metadata plugins */
0043   Q_PROPERTY(int taggedFileFeatures READ taggedFileFeatures
0044              WRITE setTaggedFileFeatures NOTIFY taggedFileFeaturesChanged)
0045   /** true to mark truncated ID3v1.1 fields */
0046   Q_PROPERTY(bool markTruncations READ markTruncations WRITE setMarkTruncations
0047              NOTIFY markTruncationsChanged)
0048   /** true to mark oversized pictures */
0049   Q_PROPERTY(bool markOversizedPictures READ markOversizedPictures
0050              WRITE setMarkOversizedPictures NOTIFY markOversizedPicturesChanged)
0051   /** Maximum size of picture in bytes */
0052   Q_PROPERTY(int maximumPictureSize READ maximumPictureSize
0053              WRITE setMaximumPictureSize NOTIFY maximumPictureSizeChanged)
0054   /** true to mark standard violations */
0055   Q_PROPERTY(bool markStandardViolations READ markStandardViolations
0056              WRITE setMarkStandardViolations NOTIFY markStandardViolationsChanged)
0057   /** true to write total number of tracks into track fields */
0058   Q_PROPERTY(bool enableTotalNumberOfTracks READ enableTotalNumberOfTracks
0059              WRITE setEnableTotalNumberOfTracks NOTIFY enableTotalNumberOfTracksChanged)
0060   /** true to write genres as text instead of numeric string */
0061   Q_PROPERTY(bool genreNotNumeric READ genreNotNumeric WRITE setGenreNotNumeric
0062              NOTIFY genreNotNumericChanged)
0063   /** true to use "id3 " instead of "ID3 " chunk names in WAV files */
0064   Q_PROPERTY(bool lowercaseId3RiffChunk READ lowercaseId3RiffChunk
0065              WRITE setLowercaseId3RiffChunk NOTIFY lowercaseId3RiffChunkChanged)
0066   /** field name used for Vorbis comment entries */
0067   Q_PROPERTY(QString commentName READ commentName WRITE setCommentName
0068              NOTIFY commentNameChanged)
0069   /** index of field name used for Vorbis picture entries */
0070   Q_PROPERTY(int pictureNameIndex READ pictureNameIndex
0071              WRITE setPictureNameIndex NOTIFY pictureNameIndexChanged)
0072   /** field name used for RIFF track entries */
0073   Q_PROPERTY(QString riffTrackName READ riffTrackName WRITE setRiffTrackName
0074              NOTIFY riffTrackNameChanged)
0075   /** custom genres for ID3v2.3 */
0076   Q_PROPERTY(QStringList customGenres READ customGenres WRITE setCustomGenres
0077              NOTIFY customGenresChanged)
0078   /** custom frame names */
0079   Q_PROPERTY(QStringList customFrames READ customFrames WRITE setCustomFrames
0080              NOTIFY customFramesChanged)
0081   /** version used for new ID3v2 tags */
0082   Q_PROPERTY(int id3v2Version READ id3v2Version WRITE setId3v2Version
0083              NOTIFY id3v2VersionChanged)
0084   /** text encoding used for new ID3v1 tags */
0085   Q_PROPERTY(QString textEncodingV1 READ textEncodingV1 WRITE setTextEncodingV1
0086              NOTIFY textEncodingV1Changed)
0087   /** text encoding used for new ID3v1 tags */
0088   Q_PROPERTY(int textEncodingV1Index READ textEncodingV1Index
0089              WRITE setTextEncodingV1Index NOTIFY textEncodingV1Changed)
0090   /** text encoding used for new ID3v2 tags */
0091   Q_PROPERTY(int textEncoding READ textEncoding WRITE setTextEncoding
0092              NOTIFY textEncodingChanged)
0093   /** frames which are displayed for Tag 2 even if not present */
0094   Q_PROPERTY(quint64 quickAccessFrames READ quickAccessFrames
0095              WRITE setQuickAccessFrames NOTIFY quickAccessFramesChanged)
0096   /** order of frames which are displayed for Tag 2 even if not present */
0097   Q_PROPERTY(QList<int> quickAccessFrameOrder READ quickAccessFrameOrder
0098              WRITE setQuickAccessFrameOrder NOTIFY quickAccessFrameOrderChanged)
0099   /** number of digits in track number */
0100   Q_PROPERTY(int trackNumberDigits READ trackNumberDigits
0101              WRITE setTrackNumberDigits NOTIFY trackNumberDigitsChanged)
0102   /** true to show only custom genres in combo boxes */
0103   Q_PROPERTY(bool onlyCustomGenres READ onlyCustomGenres
0104              WRITE setOnlyCustomGenres NOTIFY onlyCustomGenresChanged)
0105   /** the order in which meta data plugins are tried when opening a file */
0106   Q_PROPERTY(QStringList pluginOrder READ pluginOrder
0107              WRITE setPluginOrder NOTIFY pluginOrderChanged)
0108   /** disabled plugins */
0109   Q_PROPERTY(QStringList disabledPlugins READ disabledPlugins
0110              WRITE setDisabledPlugins NOTIFY disabledPluginsChanged)
0111   /** list of available plugins. */
0112   Q_PROPERTY(QStringList availablePlugins READ availablePlugins
0113              WRITE setAvailablePlugins NOTIFY availablePluginsChanged)
0114   /** mapping between star count and rating values. */
0115   Q_PROPERTY(QStringList starRatingMappingStrings READ starRatingMappingStrings
0116              WRITE setStarRatingMappingStrings NOTIFY starRatingMappingsChanged)
0117   /** default value for Email field in POPM frame. */
0118   Q_PROPERTY(QString defaultPopmEmail READ defaultPopmEmail
0119              NOTIFY starRatingMappingsChanged)
0120   Q_ENUMS(Id3v2Version)
0121   Q_ENUMS(TextEncoding)
0122   Q_ENUMS(VorbisPictureName)
0123 public:
0124   /** The ID3v2 version used for new tags. */
0125   enum Id3v2Version {
0126     ID3v2_3_0 = 0,
0127     ID3v2_4_0 = 1
0128   };
0129 
0130   /** Encoding used for ID3v2 frames. */
0131   enum TextEncoding {
0132     TE_ISO8859_1,
0133     TE_UTF16,
0134     TE_UTF8
0135   };
0136 
0137   /** Name for Vorbis picture. */
0138   enum VorbisPictureName {
0139     VP_METADATA_BLOCK_PICTURE,
0140     VP_COVERART
0141   };
0142 
0143   /**
0144    * Constructor.
0145    */
0146   TagConfig();
0147 
0148   /**
0149    * Destructor.
0150    */
0151   ~TagConfig() override;
0152 
0153   /**
0154    * Persist configuration.
0155    *
0156    * @param config configuration
0157    */
0158   void writeToConfig(ISettings* config) const override;
0159 
0160   /**
0161    * Read persisted configuration.
0162    *
0163    * @param config configuration
0164    */
0165   void readFromConfig(ISettings* config) override;
0166 
0167   /**
0168    * Get features provided by metadata plugins.
0169    * @return bit mask with TaggedFile::Feature flags set.
0170    * @remark This information is not stored in the configuration, it is
0171    * registered at initialization time using setTaggedFileFeatures().
0172    */
0173   int taggedFileFeatures() const {
0174     return m_taggedFileFeatures;
0175   }
0176 
0177   /**
0178    * Set features provided by metadata plugins.
0179    * @param taggedFileFeatures bit mask with TaggedFile::Feature flags set
0180    */
0181   void setTaggedFileFeatures(int taggedFileFeatures);
0182 
0183   /** true to mark truncated ID3v1.1 fields */
0184   bool markTruncations() const { return m_markTruncations; }
0185 
0186   /** Set true to mark truncated ID3v1.1 fields. */
0187   void setMarkTruncations(bool markTruncations);
0188 
0189   /** true to mark oversized pictures */
0190   bool markOversizedPictures() const { return m_markOversizedPictures; }
0191 
0192   /** Set true to mark oversized pictures. */
0193   void setMarkOversizedPictures(bool markOversizedPictures);
0194 
0195   /** Maximum size of picture in bytes */
0196   int maximumPictureSize() const { return m_maximumPictureSize; }
0197 
0198   /** Set maximum size of picture in bytes. */
0199   void setMaximumPictureSize(int maximumPictureSize);
0200 
0201   /** true to mark standard violations */
0202   bool markStandardViolations() const { return m_markStandardViolations; }
0203 
0204   /** Set true to mark standard violations. */
0205   void setMarkStandardViolations(bool markStandardViolations);
0206 
0207   /** true to write total number of tracks into track fields */
0208   bool enableTotalNumberOfTracks() const { return m_enableTotalNumberOfTracks; }
0209 
0210   /** Set true to write total number of tracks into track fields. */
0211   void setEnableTotalNumberOfTracks(bool enableTotalNumberOfTracks);
0212 
0213   /** true to write genres as text instead of numeric string */
0214   bool genreNotNumeric() const { return m_genreNotNumeric; }
0215 
0216   /** Set true to write genres as text instead of numeric string. */
0217   void setGenreNotNumeric(bool genreNotNumeric);
0218 
0219   /** true to use "id3 " instead of "ID3 " chunk names in WAV files */
0220   bool lowercaseId3RiffChunk() const { return m_lowercaseId3RiffChunk; }
0221 
0222   /** Set true to use "id3 " instead of "ID3 " chunk names in WAV files */
0223   void setLowercaseId3RiffChunk(bool lowercaseId3RiffChunk);
0224 
0225   /** field name used for Vorbis comment entries */
0226   QString commentName() const { return m_commentName; }
0227 
0228   /** Set field name used for Vorbis comment entries. */
0229   void setCommentName(const QString& commentName);
0230 
0231   /** index of field name used for Vorbis picture entries */
0232   int pictureNameIndex() const { return m_pictureNameItem; }
0233 
0234   /** Set index of field name used for Vorbis picture entries. */
0235   void setPictureNameIndex(int pictureNameIndex);
0236 
0237   /** field name used for RIFF track entries */
0238   QString riffTrackName() const { return m_riffTrackName; }
0239 
0240   /** Set field name used for RIFF track entries. */
0241   void setRiffTrackName(const QString& riffTrackName);
0242 
0243   /** custom genres for ID3v2.3 */
0244   QStringList customGenres() const { return m_customGenres; }
0245 
0246   /** Set custom genres for ID3v2.3. */
0247   void setCustomGenres(const QStringList& customGenres);
0248 
0249   /** custom frame names */
0250   QStringList customFrames() const { return m_customFrames; }
0251 
0252   /** Set custom frame names. */
0253   void setCustomFrames(const QStringList& customFrames);
0254 
0255   /** version used for new ID3v2 tags */
0256   int id3v2Version() const;
0257 
0258   /** Set version used for new ID3v2 tags. */
0259   void setId3v2Version(int id3v2Version);
0260 
0261   /** text encoding used for new ID3v1 tags */
0262   QString textEncodingV1() const { return m_textEncodingV1; }
0263 
0264   /** Set text encoding used for new ID3v1 tags. */
0265   void setTextEncodingV1(const QString& textEncodingV1);
0266 
0267   /** index of ID3v1 text encoding in getTextCodecNames() */
0268   int textEncodingV1Index() const;
0269 
0270   /** Set ID3v1 text encoding from index in getTextCodecNames(). */
0271   void setTextEncodingV1Index(int index);
0272 
0273   /** text encoding used for new ID3v2 tags */
0274   int textEncoding() const { return m_textEncoding; }
0275 
0276   /** Set text encoding used for new ID3v2 tags. */
0277   void setTextEncoding(int textEncoding);
0278 
0279   /** frames which are displayed for Tag 2 even if not present */
0280   quint64 quickAccessFrames() const {
0281     return m_quickAccessFrames;
0282   }
0283 
0284   /** Set frames which are displayed for Tag 2 even if not present. */
0285   void setQuickAccessFrames(quint64 quickAccessFrames);
0286 
0287   /** order of frames which are displayed for Tag 2 even if not present. */
0288   QList<int> quickAccessFrameOrder() const {
0289     return m_quickAccessFrameOrder;
0290   }
0291 
0292   /**
0293    * Available and selected quick access frames.
0294    */
0295   Q_INVOKABLE QVariantList selectedQuickAccessFrames() const;
0296 
0297   /**
0298    * Set selected quick access frames.
0299    * @param namesSelected list of maps with name, selected and type fields
0300    */
0301   Q_INVOKABLE void setSelectedQuickAccessFrames(
0302       const QVariantList& namesSelected);
0303 
0304   /** Set order of frames which are displayed for Tag 2 even if not present. */
0305   void setQuickAccessFrameOrder(const QList<int>& frameTypes);
0306 
0307   /** number of digits in track number */
0308   int trackNumberDigits() const { return m_trackNumberDigits; }
0309 
0310   /** Set number of digits in track number. */
0311   void setTrackNumberDigits(int trackNumberDigits);
0312 
0313   /** true to show only custom genres in combo boxes */
0314   bool onlyCustomGenres() const { return m_onlyCustomGenres; }
0315 
0316   /** Set true to show only custom genres in combo boxes. */
0317   void setOnlyCustomGenres(bool onlyCustomGenres);
0318 
0319   /** The order in which meta data plugins are tried when opening a file */
0320   QStringList pluginOrder() const { return m_pluginOrder; }
0321 
0322   /** Set the order in which meta data plugins are tried when opening a file. */
0323   void setPluginOrder(const QStringList& pluginOrder);
0324 
0325   /** Disabled plugins */
0326   QStringList disabledPlugins() const { return m_disabledPlugins; }
0327 
0328   /** Set list of disabled plugins. */
0329   void setDisabledPlugins(const QStringList& disabledPlugins);
0330 
0331   /**
0332    * Get list of available plugins.
0333    * @return available plugins.
0334    */
0335   QStringList availablePlugins() const { return m_availablePlugins; }
0336 
0337   /**
0338    * Set list of available plugins.
0339    * @param availablePlugins available plugins
0340    */
0341   void setAvailablePlugins(const QStringList& availablePlugins);
0342 
0343   /**
0344    * Clear list of available plugins.
0345    */
0346   void clearAvailablePlugins() { m_availablePlugins.clear(); }
0347 
0348   /**
0349    * Get list of star count rating mappings.
0350    * @return star count rating mappings as a list of strings.
0351    */
0352   QStringList starRatingMappingStrings() const;
0353 
0354   /**
0355    * Set list of star count rating mappings.
0356    * @param mappings star count rating mappings
0357    */
0358   void setStarRatingMappingStrings(const QStringList& mappings);
0359 
0360   /**
0361    * Get list of star count rating mappings.
0362    * @return star count rating mappings.
0363    */
0364   const QList<QPair<QString, QVector<int> > >& starRatingMappings() const;
0365 
0366   /**
0367    * Set list of star count rating mappings.
0368    * @param maps star count rating mappings
0369    */
0370   void setStarRatingMappings(const QList<QPair<QString, QVector<int> > >& maps);
0371 
0372   /**
0373    * Get star count from rating value.
0374    * @param rating rating value stored in tag frame
0375    * @param type rating type containing frame name and optionally field value,
0376    * e.g. "POPM.Windows Media Player 9 Series" or "RATING"
0377    * @return number of stars (1..5).
0378    */
0379   Q_INVOKABLE int starCountFromRating(int rating, const QString& type) const;
0380 
0381   /**
0382    * Get rating value from star count.
0383    * @param starCount number of stars (1..5)
0384    * @param type rating type containing frame name and optionally field value,
0385    * e.g. "POPM.Windows Media Player 9 Series" or "RATING"
0386    * @return rating value stored in tag frame, usually a value between 1 and 255
0387    * or 1 and 100.
0388    */
0389   Q_INVOKABLE int starCountToRating(int starCount, const QString& type) const;
0390 
0391   /**
0392    * Get default value for Email field in POPM frame.
0393    * @return value for Email field in first POPM entry of star rating mappings.
0394    */
0395   QString defaultPopmEmail() const;
0396 
0397   /**
0398    * String list of encodings for ID3v2.
0399    */
0400   Q_INVOKABLE static QStringList getTextEncodingNames();
0401 
0402   /**
0403    * String list of possible versions used for new ID3v2 tags.
0404    */
0405   Q_INVOKABLE static QStringList getId3v2VersionNames();
0406 
0407   /**
0408    * String list with suggested field names used for Vorbis comment entries.
0409    */
0410   Q_INVOKABLE static QStringList getCommentNames();
0411 
0412   /**
0413    * String list with possible field names used for Vorbis picture entries.
0414    */
0415   Q_INVOKABLE static QStringList getPictureNames();
0416 
0417   /**
0418    * String list with suggested field names used for RIFF track entries.
0419    */
0420   Q_INVOKABLE static QStringList getRiffTrackNames();
0421 
0422   /**
0423    * Convert list of custom frame names to display names.
0424    * @param names custom frame names
0425    * @return possibly translated display representations of @a names.
0426    */
0427   Q_INVOKABLE static QStringList customFrameNamesToDisplayNames(
0428       const QStringList& names);
0429 
0430   /**
0431    * Convert list of display names to custom frame names.
0432    * @param displayNames displayed frame names
0433    * @return internal representations of @a displayNames.
0434    */
0435   Q_INVOKABLE static QStringList customFrameNamesFromDisplayNames(
0436       const QStringList& displayNames);
0437 
0438   /**
0439    * Get the available and selected quick access frames.
0440    * @param types ordered frame types as in quickAccessFrameOrder()
0441    * @param frameMask quick access frame selection as in quickAccessFrames()
0442    * @param customFrameNames list of custom frame names as in customFrames()
0443    * @return list of name/type/selected maps.
0444    */
0445   static QVariantList getQuickAccessFrameSelection(
0446       const QList<int>& types, quint64 frameMask,
0447       const QStringList& customFrameNames);
0448 
0449   /**
0450    * Set the selected quick access frames.
0451    * @param namesSelected list of name/type/selected maps
0452    * @param frameTypes ordered frame types are returned here,
0453    *        suitable for setQuickAccessFrameOrder()
0454    * @param frameMask the quick access frame selection is returned here,
0455    *        suitable for setQuickAccessFrames()
0456    */
0457   static void setQuickAccessFrameSelection(
0458       const QVariantList& namesSelected,
0459       QList<int>& frameTypes, quint64& frameMask);
0460 
0461   /**
0462    * Set default plugin order.
0463    */
0464   void setDefaultPluginOrder();
0465 
0466 signals:
0467   /** Emitted when @a taggedFileFeatures changed. */
0468   void taggedFileFeaturesChanged(int taggedFileFeatures);
0469 
0470   /** Emitted when @a markOversizedPictures changed. */
0471   void markTruncationsChanged(bool markTruncations);
0472 
0473   /** Emitted when @a maximumPictureSize changed. */
0474   void maximumPictureSizeChanged(int maximumPictureSize);
0475 
0476   /** Emitted when @a markTruncations changed. */
0477   void markOversizedPicturesChanged(bool markOversizedPictures);
0478 
0479   /** Emitted when @a markStandardViolations changed. */
0480   void markStandardViolationsChanged(bool markStandardViolations);
0481 
0482   /** Emitted when @a enableTotalNumberOfTracks changed. */
0483   void enableTotalNumberOfTracksChanged(bool enableTotalNumberOfTracks);
0484 
0485   /** Emitted when @a genreNotNumeric changed. */
0486   void genreNotNumericChanged(bool genreNotNumeric);
0487 
0488   /** Emitted when @a lowercaseId3RiffChunk changed. */
0489   void lowercaseId3RiffChunkChanged(bool lowercaseId3RiffChunk);
0490 
0491   /** Emitted when @a commentName changed. */
0492   void commentNameChanged(const QString& commentName);
0493 
0494   /** Emitted when @a pictureNameIndex changed. */
0495   void pictureNameIndexChanged(int pictureNameIndex);
0496 
0497   /** Emitted when @a riffTrackName changed. */
0498   void riffTrackNameChanged(const QString& riffTrackName);
0499 
0500   /** Emitted when @a customGenres changed. */
0501   void customGenresChanged(const QStringList& customGenres);
0502 
0503   /** Emitted when @a customFrames changed. */
0504   void customFramesChanged(const QStringList& customFrames);
0505 
0506   /** Emitted when @a id3v2Version changed. */
0507   void id3v2VersionChanged(int id3v2Version);
0508 
0509   /** Emitted when @a textEncodingV1 changed. */
0510   void textEncodingV1Changed(const QString& textEncodingV1);
0511 
0512   /** Emitted when @a textEncoding changed. */
0513   void textEncodingChanged(int textEncoding);
0514 
0515   /** Emitted when @a quickAccessFrames changed. */
0516   void quickAccessFramesChanged(quint64 quickAccessFrames);
0517 
0518   /** Emitted when @a quickAccessFrameOrder changed. */
0519   void quickAccessFrameOrderChanged(const QList<int>& frameTypes);
0520 
0521   /** Emitted when @a  changed. */
0522   void trackNumberDigitsChanged(int trackNumberDigits);
0523 
0524   /** Emitted when @a onlyCustomGenres changed. */
0525   void onlyCustomGenresChanged(bool onlyCustomGenres);
0526 
0527   /** Emitted when @a pluginOrder changed. */
0528   void pluginOrderChanged(const QStringList& pluginOrder);
0529 
0530   /** Emitted when @a disabledPlugins changed. */
0531   void disabledPluginsChanged(const QStringList& disabledPlugins);
0532 
0533   /** Emitted when @a availablePlugins changed. */
0534   void availablePluginsChanged(const QStringList& availablePlugins);
0535 
0536   /** Emitted when star count rating mappings changed. */
0537   void starRatingMappingsChanged();
0538 
0539 private:
0540   friend TagConfig& StoredConfig<TagConfig>::instance();
0541 
0542   QScopedPointer<StarRatingMapping> m_starRatingMapping;
0543 
0544   QString m_commentName;
0545   QString m_riffTrackName;
0546   int m_pictureNameItem;
0547   QStringList m_customGenres;
0548   QStringList m_customFrames;
0549   int m_id3v2Version;
0550   QString m_textEncodingV1;
0551   int m_textEncoding;
0552   quint64 m_quickAccessFrames;
0553   QList<int> m_quickAccessFrameOrder;
0554   int m_trackNumberDigits;
0555   QStringList m_pluginOrder;
0556   QStringList m_disabledPlugins;
0557   QStringList m_availablePlugins;
0558   int m_taggedFileFeatures;
0559   int m_maximumPictureSize;
0560   bool m_markOversizedPictures;
0561   bool m_markStandardViolations;
0562   bool m_onlyCustomGenres;
0563   bool m_markTruncations;
0564   bool m_enableTotalNumberOfTracks;
0565   bool m_genreNotNumeric;
0566   bool m_lowercaseId3RiffChunk;
0567 
0568   /** Index in configuration storage */
0569   static int s_index;
0570 };