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

0001 /**
0002  * \file textimporter.h
0003  * Import tags from text.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 19 Jun 2011
0008  *
0009  * Copyright (C) 2011-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 <QString>
0030 #include <QScopedPointer>
0031 #include "kid3api.h"
0032 
0033 class ImportTrackDataVector;
0034 class ImportParser;
0035 class TrackDataModel;
0036 class TrackData;
0037 /**
0038  * Import tags from text.
0039  */
0040 class KID3_CORE_EXPORT TextImporter {
0041 public:
0042   /**
0043    * Constructor.
0044    *
0045    * @param trackDataModel track data to be filled with imported values
0046    */
0047   explicit TextImporter(TrackDataModel* trackDataModel);
0048 
0049   /**
0050    * Destructor.
0051    */
0052   ~TextImporter();
0053 
0054   /**
0055    * Update track data list with imported tags.
0056    *
0057    * @param text text to import
0058    * @param headerFormat header format
0059    * @param trackFormat track format
0060    *
0061    * @return true if tags were found.
0062    */
0063   bool updateTrackData(const QString& text,
0064                        const QString& headerFormat, const QString& trackFormat);
0065 
0066   /**
0067    * Import text from tags to other tags.
0068    *
0069    * @param sourceFormat format to create source text
0070    * @param extractionFormat regular expression to extract other tags
0071    * @param trackDataVector track data to process
0072    */
0073   static void importFromTags(
0074     const QString& sourceFormat,
0075     const QString& extractionFormat,
0076     ImportTrackDataVector& trackDataVector);
0077 
0078   /**
0079    * Import text from tags to other tags.
0080    *
0081    * @param sourceFormat format to create source text
0082    * @param parser import parser which is initialized with extraction format
0083    * @param trackData track data to process
0084    */
0085   static void importFromTags(
0086       const QString& sourceFormat, ImportParser& parser, TrackData& trackData);
0087 
0088 private:
0089   Q_DISABLE_COPY(TextImporter)
0090 
0091   /**
0092    * Look for album specific information (artist, album, year, genre) in
0093    * a header.
0094    *
0095    * @param frames frames to put resulting values in,
0096    *           fields which are not found are not touched.
0097    *
0098    * @return true if one or more field were found.
0099    */
0100   bool parseHeader(TrackData& frames);
0101 
0102   /**
0103    * Get next line as frames from imported file or clipboard.
0104    *
0105    * @param frames frames
0106    * @param start true to start with the first line, false for all
0107    *              other lines
0108    *
0109    * @return true if ok (result in st),
0110    *         false if end of file reached.
0111    */
0112   bool getNextTags(TrackData& frames, bool start);
0113 
0114   /**
0115    * Get list with track durations.
0116    *
0117    * @return list with track durations,
0118    *         empty if no track durations found.
0119    */
0120   QList<int> getTrackDurations() const;
0121 
0122   /** contents of imported file/clipboard */
0123   QString m_text;
0124   /** header format */
0125   QString m_headerFormat;
0126   /** track format */
0127   QString m_trackFormat;
0128   /** header parser object */
0129   QScopedPointer<ImportParser> m_headerParser;
0130   /** track parser object */
0131   QScopedPointer<ImportParser> m_trackParser;
0132   /** track data */
0133   TrackDataModel* m_trackDataModel;
0134 };