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

0001 /**
0002  * \file playlistcreator.h
0003  * Playlist creator.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 21 Sep 2009
0008  *
0009  * Copyright (C) 2009-2018  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 <QString>
0030 #include <QMap>
0031 #include <QScopedPointer>
0032 #include "playlistconfig.h"
0033 
0034 class QModelIndex;
0035 class QPersistentModelIndex;
0036 class TaggedFile;
0037 class ImportTrackData;
0038 
0039 /**
0040  * Playlist creator.
0041  * Creates playlists from added items according to a playlist configuration.
0042  */
0043 class PlaylistCreator {
0044 public:
0045   /**
0046    * An item from the file list which can be added to a playlist.
0047    * The item will only be added to the playlist if add() is called.
0048    */
0049   class Item {
0050   public:
0051     /**
0052      * Constructor.
0053      *
0054      * @param index model index
0055      * @param ctr  associated playlist creator
0056      */
0057     Item(const QModelIndex& index, PlaylistCreator& ctr);
0058 
0059     /**
0060      * Destructor.
0061      */
0062     ~Item() = default;
0063 
0064     /**
0065      * Check if item is a directory.
0066      * @return true if item is directory.
0067      */
0068     bool isDir() const { return m_isDir; }
0069 
0070     /**
0071      * Check if item is a tagged file.
0072      * @return true if item is file.
0073      */
0074     bool isFile() const { return m_taggedFile != nullptr; }
0075 
0076     /**
0077      * Get the directory of the item.
0078      * @return directory path with trailing separator.
0079      */
0080     QString getDirName() const { return m_dirName; }
0081 
0082     /**
0083      * Add item to playlist.
0084      * This operation will write a playlist if the configuration is set to write
0085      * a playlist in every directory and a new directory is entered.
0086      *
0087      * @return true if ok.
0088      */
0089     bool add();
0090 
0091     /**
0092      * Get additional information for item.
0093      * @param info additional information is returned here
0094      * @param duration the duration of the track is returned here
0095      */
0096     void getInfo(QString& info, unsigned long& duration);
0097 
0098   private:
0099     /**
0100      * Format string using tags and properties of item.
0101      *
0102      * @param format format string
0103      *
0104      * @return string with percent codes replaced.
0105      */
0106     QString formatString(const QString& format);
0107 
0108     PlaylistCreator& m_ctr;
0109     TaggedFile* m_taggedFile;
0110     QScopedPointer<ImportTrackData> m_trackData;
0111     QString m_dirName;
0112     bool m_isDir;
0113   };
0114 
0115   /**
0116    * Constructor.
0117    *
0118    * @param topLevelDir top-level directory of playlist
0119    * @param cfg         playlist configuration
0120    */
0121   PlaylistCreator(const QString& topLevelDir, const PlaylistConfig& cfg);
0122 
0123   /**
0124    * Write playlist containing added Entry elements.
0125    *
0126    * @return true if ok.
0127    */
0128   bool write();
0129 
0130   /**
0131    * Write a playlist from a list of model indexes.
0132    * @param playlistPath file path to be used for playlist
0133    * @param indexes indexes in FileProxyModel
0134    * @return true if ok.
0135    */
0136   bool write(const QString& playlistPath,
0137              const QList<QPersistentModelIndex>& indexes);
0138 
0139   /**
0140    * Read playlist from file
0141    * @param playlistPath path to playlist file
0142    * @param filePaths absolute paths to the playlist files are returned here
0143    * @param format the playlist format is returned here
0144    * @param hasFullPath true is returned here if the files use absolute paths
0145    * @param hasInfo true is returned here if the playlist contains additional
0146    *                information
0147    * @return true if ok.
0148    */
0149   bool read(const QString& playlistPath, QStringList& filePaths,
0150             PlaylistConfig::PlaylistFormat& format,
0151             bool& hasFullPath, bool& hasInfo) const;
0152 
0153 private:
0154   friend class Item;
0155 
0156   struct Entry {
0157     Entry() : duration(0) {}
0158     unsigned long duration;
0159     QString filePath;
0160     QString info;
0161   };
0162 
0163   bool write(const QList<Entry>& entries);
0164 
0165   const PlaylistConfig& m_cfg;
0166   QString m_playlistDirName;
0167   QString m_playlistFileName;
0168   QMap<QString, Entry> m_entries;
0169 };