File indexing completed on 2024-05-19 04:55:58
0001 /** 0002 * \file textexporter.cpp 0003 * Export tags as text. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 22 Jul 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 #include "textexporter.h" 0028 #include <QFile> 0029 #include <QFileInfo> 0030 #include <QDir> 0031 #include <QTextStream> 0032 #include "exportconfig.h" 0033 #include "importconfig.h" 0034 #include "fileconfig.h" 0035 0036 /** 0037 * Constructor. 0038 * @param parent parent object 0039 */ 0040 TextExporter::TextExporter(QObject* parent) : QObject(parent) 0041 { 0042 setObjectName(QLatin1String("TextExporter")); 0043 } 0044 0045 /** 0046 * Destructor. 0047 */ 0048 TextExporter::~TextExporter() 0049 { 0050 // not inline or default to silence weak-vtables warning 0051 } 0052 0053 /** 0054 * Update text from tags. 0055 * 0056 * @param headerFormat header format 0057 * @param trackFormat track format 0058 * @param trailerFormat trailer format 0059 */ 0060 void TextExporter::updateText( 0061 const QString& headerFormat, const QString& trackFormat, 0062 const QString& trailerFormat) 0063 { 0064 m_text.clear(); 0065 const int numTracks = m_trackDataVector.size(); 0066 int trackNr = 0; 0067 for (auto it = m_trackDataVector.constBegin(); 0068 it != m_trackDataVector.constEnd(); 0069 ++it) { 0070 if (trackNr == 0 && !headerFormat.isEmpty()) { 0071 m_text.append(it->formatString(headerFormat)); 0072 m_text.append(QLatin1Char('\n')); 0073 } 0074 if (!trackFormat.isEmpty()) { 0075 m_text.append(it->formatString(trackFormat)); 0076 m_text.append(QLatin1Char('\n')); 0077 } 0078 if (trackNr == numTracks - 1 && !trailerFormat.isEmpty()) { 0079 m_text.append(it->formatString(trailerFormat)); 0080 m_text.append(QLatin1Char('\n')); 0081 } 0082 ++trackNr; 0083 } 0084 } 0085 0086 /** 0087 * Update text from tags using formats from the configuration. 0088 * 0089 * int fmtIdx index of format 0090 */ 0091 void TextExporter::updateTextUsingConfig(int fmtIdx) 0092 { 0093 const ExportConfig& exportCfg = ExportConfig::instance(); 0094 const QStringList headerFmts = exportCfg.exportFormatHeaders(); 0095 const QStringList trackFmts = exportCfg.exportFormatTracks(); 0096 const QStringList trailerFmts = exportCfg.exportFormatTrailers(); 0097 if (fmtIdx < headerFmts.size() && fmtIdx < trackFmts.size() && 0098 fmtIdx < trailerFmts.size()) { 0099 updateText(headerFmts.at(fmtIdx), trackFmts.at(fmtIdx), 0100 trailerFmts.at(fmtIdx)); 0101 } 0102 } 0103 0104 /** 0105 * Export to a file. 0106 * 0107 * @param fn file name 0108 * 0109 * @return true if ok. 0110 */ 0111 bool TextExporter::exportToFile(const QString& fn) const 0112 { 0113 if (!fn.isEmpty()) { 0114 QFile file(fn); 0115 if (file.open(QIODevice::WriteOnly)) { 0116 ImportConfig::instance().setImportDir(QFileInfo(file).dir().path()); 0117 QTextStream stream(&file); 0118 if (QString codecName = FileConfig::instance().textEncoding(); 0119 codecName != QLatin1String("System")) { 0120 #if QT_VERSION >= 0x060000 0121 if (auto encoding = QStringConverter::encodingForName(codecName.toLatin1())) { 0122 stream.setEncoding(*encoding); 0123 } 0124 #else 0125 stream.setCodec(codecName.toLatin1()); 0126 #endif 0127 } 0128 stream << m_text; 0129 file.close(); 0130 return true; 0131 } 0132 } 0133 return false; 0134 }