Warning, file /multimedia/kid3/src/core/tags/trackdata.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file trackdata.h
0003  * Track data, frames with association to tagged file.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 7 Jul 2005
0008  *
0009  * Copyright (C) 2005-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 <QVector>
0030 #include <QString>
0031 #include <QSet>
0032 #include <QUrl>
0033 #include "frame.h"
0034 #include "taggedfile.h"
0035 #include "kid3api.h"
0036 
0037 /**
0038  * Track data, frames with association to tagged file.
0039  */
0040 class KID3_CORE_EXPORT TrackData : public FrameCollection {
0041 public:
0042   /**
0043    * Constructor.
0044    */
0045   TrackData();
0046 
0047   /**
0048    * Constructor.
0049    * All fields are set from the tagged file,
0050    * which should be read using readTags() before.
0051    *
0052    * @param taggedFile tagged file providing track data
0053    * @param tagVersion source of frames
0054    */
0055   TrackData(TaggedFile& taggedFile, Frame::TagVersion tagVersion);
0056 
0057   /**
0058    * Get duration of file.
0059    * @return duration of file.
0060    */
0061   int getFileDuration() const;
0062 
0063   /**
0064    * Get absolute filename.
0065    *
0066    * @return absolute file path.
0067    */
0068   QString getAbsFilename() const;
0069 
0070   /**
0071    * Get filename.
0072    *
0073    * @return filename.
0074    */
0075   QString getFilename() const;
0076 
0077   /**
0078    * Get directory name.
0079    *
0080    * @return directory name.
0081    */
0082   QString getDirname() const;
0083 
0084   /**
0085    * Get file extension including the dot.
0086    *
0087    * @param preferFromFilename true to prefer extension from current filename
0088    *                           over default extension for file type
0089    *
0090    * @return file extension, e.g. ".mp3".
0091    */
0092   QString getFileExtension(bool preferFromFilename = false) const;
0093 
0094   /**
0095    * Get the total number of tracks in the directory.
0096    *
0097    * @return total number of tracks, -1 if unavailable.
0098    */
0099   int getTotalNumberOfTracksInDir() const;
0100 
0101   /**
0102    * Get the tag format.
0103    *
0104    * @param tagNr tag number
0105    * @return string describing format of tag,
0106    *         e.g. "ID3v1.1", "ID3v2.3", "Vorbis", "APE",
0107    *         QString::null if unknown.
0108    */
0109   QString getTagFormat(Frame::TagNumber tagNr) const;
0110 
0111   /**
0112    * Get detail info.
0113    * @param info the detail information is returned here
0114    */
0115   void getDetailInfo(TaggedFile::DetailInfo& info) const;
0116 
0117   /**
0118    * Format a string from track data.
0119    * Supported format fields:
0120    * Those supported by TrackDataFormatReplacer::getReplacement()
0121    *
0122    * @param format    format specification
0123    *
0124    * @return formatted string.
0125    */
0126   QString formatString(const QString& format) const;
0127 
0128   /**
0129    * Create filename from tags according to format string.
0130    *
0131    * @param str       format string containing codes supported by
0132    *                  TrackDataFormatReplacer::getReplacement()
0133    * @param isDirname true to generate a directory name
0134    *
0135    * @return format string with format codes replaced by tags.
0136    */
0137   QString formatFilenameFromTags(QString str, bool isDirname = false) const;
0138 
0139   /**
0140    * Transform string to file name.
0141    * The directory part is removed and a file extension added.
0142    * @param str string to transform
0143    */
0144   void transformToFilename(QString& str) const;
0145 
0146   /**
0147    * Get frames.
0148    * @return frames.
0149    */
0150   FrameCollection& getFrameCollection() {
0151     return *this;
0152   }
0153 
0154   /**
0155    * Set frames.
0156    * @param frames frames
0157    */
0158   void setFrameCollection(const FrameCollection& frames) {
0159     *(static_cast<FrameCollection*>(this)) = frames; // clazy:exclude=unneeded-cast
0160   }
0161 
0162   /**
0163    * Get tagged file associated with this track data.
0164    * @return tagged file, 0 if none assigned.
0165    */
0166   TaggedFile* getTaggedFile() const;
0167 
0168   /**
0169    * Get help text for format codes supported by formatString().
0170    *
0171    * @param onlyRows if true only the tr elements are returned,
0172    *                 not the surrounding table
0173    *
0174    * @return help text.
0175    */
0176   static QString getFormatToolTip(bool onlyRows = false);
0177 
0178 private:
0179   QPersistentModelIndex m_taggedFileIndex;
0180 };
0181 
0182 /**
0183  * Track data used for import.
0184  */
0185 class ImportTrackData : public TrackData {
0186 public:
0187   /**
0188    * Constructor.
0189    */
0190   ImportTrackData() : m_importDuration(0), m_enabled(true) {}
0191 
0192   /**
0193    * Constructor.
0194    * All fields except the import duration are set from the tagged file,
0195    * which should be read using readTags() before.
0196    *
0197    * @param taggedFile tagged file providing track data
0198    * @param tagVersion source of frames
0199    */
0200   ImportTrackData(TaggedFile& taggedFile, Frame::TagVersion tagVersion)
0201     : TrackData(taggedFile, tagVersion), m_importDuration(0), m_enabled(true) {}
0202 
0203   /**
0204    * Get duration of import.
0205    * @return duration of import.
0206    */
0207   int getImportDuration() const { return m_importDuration; }
0208 
0209   /**
0210    * Set duration of import.
0211    * @param duration duration of import
0212    */
0213   void setImportDuration(int duration) { m_importDuration = duration; }
0214 
0215   /**
0216    * Check if track is enabled.
0217    * @return true if enabled (true is default).
0218    */
0219   bool isEnabled() const { return m_enabled; }
0220 
0221   /**
0222    * Enable or disable track.
0223    * @param enabled true to enable
0224    */
0225   void setEnabled(bool enabled) { m_enabled = enabled; }
0226 
0227   /**
0228    * Get the difference between the imported duration and the track's duration.
0229    * @return absolute value of time difference in seconds, -1 if not available.
0230    */
0231   int getTimeDifference() const;
0232 
0233   /**
0234    * Get words of file name.
0235    * @return lower case words found in file name.
0236    */
0237   QSet<QString> getFilenameWords() const;
0238 
0239   /**
0240    * Get words of title.
0241    * @return lower case words found in title.
0242    */
0243   QSet<QString> getTitleWords() const;
0244 
0245 private:
0246   int m_importDuration;
0247   bool m_enabled;
0248 };
0249 
0250 /**
0251  * Vector containing tracks to import and artist, album names.
0252  */
0253 class KID3_CORE_EXPORT ImportTrackDataVector : public QVector<ImportTrackData> {
0254 public:
0255   /**
0256    * Clear vector and associated data.
0257    */
0258   void clearData();
0259 
0260   /**
0261    * Get album artist.
0262    * @return album artist.
0263    */
0264   QString getArtist() const;
0265 
0266   /**
0267    * Get album title.
0268    * @return album title.
0269    */
0270   QString getAlbum() const;
0271 
0272   /**
0273    * Check if tag is supported in the first track.
0274    * @param tagNr tag number
0275    * @return true if tag is supported.
0276    */
0277   bool isTagSupported(Frame::TagNumber tagNr) const;
0278 
0279   /**
0280    * Get cover art URL.
0281    * @return cover art URL.
0282    */
0283   QUrl getCoverArtUrl() const { return m_coverArtUrl; }
0284 
0285   /**
0286    * Set cover art URL.
0287    * @param coverArtUrl cover art URL
0288    */
0289   void setCoverArtUrl(const QUrl& coverArtUrl) { m_coverArtUrl = coverArtUrl; }
0290 
0291   /**
0292    * Read the tags from the files.
0293    * This can be used to fill the track data with another tag version.
0294    *
0295    * @param tagVersion tag version to read
0296    */
0297   void readTags(Frame::TagVersion tagVersion);
0298 
0299 #ifndef QT_NO_DEBUG
0300   /**
0301    * Dump contents of tracks to debug console.
0302    */
0303   void dump() const;
0304 #endif
0305 
0306 private:
0307   /**
0308    * Get frame from first track.
0309    * @param type frame type
0310    * @return value of frame.
0311    */
0312   QString getFrame(Frame::Type type) const;
0313 
0314   QUrl m_coverArtUrl;
0315 };
0316 
0317 
0318 /**
0319  * Replaces track data format codes in a string.
0320  */
0321 class KID3_CORE_EXPORT TrackDataFormatReplacer : public FrameFormatReplacer {
0322 public:
0323   /**
0324    * Constructor.
0325    *
0326    * @param trackData track data
0327    * @param str       string with format codes
0328    */
0329   explicit TrackDataFormatReplacer(
0330     const TrackData& trackData,
0331     const QString& str = QString());
0332 
0333   /**
0334    * Destructor.
0335    */
0336   ~TrackDataFormatReplacer() override = default;
0337 
0338   TrackDataFormatReplacer(const TrackDataFormatReplacer& other) = delete;
0339   TrackDataFormatReplacer &operator=(const TrackDataFormatReplacer& other) = delete;
0340 
0341   /**
0342    * Get help text for supported format codes.
0343    *
0344    * @param onlyRows if true only the tr elements are returned,
0345    *                 not the surrounding table
0346    *
0347    * @return help text.
0348    */
0349   static QString getToolTip(bool onlyRows = false);
0350 
0351 protected:
0352   /**
0353    * Replace a format code (one character %c or multiple characters %{chars}).
0354    * Supported format fields:
0355    * Those supported by FrameFormatReplacer::getReplacement()
0356    * %f filename
0357    * %p path to file
0358    * %u URL of file
0359    * %d duration in minutes:seconds
0360    * %D duration in seconds
0361    * %n number of tracks
0362    *
0363    * @param code format code
0364    *
0365    * @return replacement string,
0366    *         QString::null if code not found.
0367    */
0368   QString getReplacement(const QString& code) const override;
0369 
0370 private:
0371   const TrackData& m_trackData;
0372 };