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

0001 /**
0002  * \file serverimporter.cpp
0003  * Generic baseclass to import from a server.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 09 Oct 2006
0008  *
0009  * Copyright (C) 2006-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 "serverimporter.h"
0028 #include <QRegularExpression>
0029 #include "serverimporterconfig.h"
0030 #include "importclient.h"
0031 #include "trackdata.h"
0032 
0033 /**
0034  * Constructor.
0035  *
0036  * @param netMgr network access manager
0037  * @param trackDataModel track data to be filled with imported values
0038  */
0039 ServerImporter::ServerImporter(QNetworkAccessManager* netMgr,
0040                                TrackDataModel* trackDataModel)
0041   : ImportClient(netMgr),
0042     m_albumListModel(new AlbumListModel(this)),
0043     m_trackDataModel(trackDataModel), m_standardTagsEnabled(true),
0044     m_additionalTagsEnabled(false), m_coverArtEnabled(false)
0045 {
0046   setObjectName(QLatin1String("ServerImporter"));
0047 }
0048 
0049 /** NULL-terminated array of server strings, 0 if not used */
0050 const char** ServerImporter::serverList() const { return nullptr; }
0051 
0052 /** default server, 0 to disable */
0053 const char* ServerImporter::defaultServer() const { return nullptr; }
0054 
0055 /** default CGI path, 0 to disable */
0056 const char* ServerImporter::defaultCgiPath() const { return nullptr; }
0057 
0058 /** anchor to online help, 0 to disable */
0059 const char* ServerImporter::helpAnchor() const { return nullptr; }
0060 
0061 /** configuration, 0 if not used */
0062 ServerImporterConfig* ServerImporter::config() const { return nullptr; }
0063 
0064 /** additional tags option, false if not used */
0065 bool ServerImporter::additionalTags() const { return false; }
0066 
0067 /**
0068  * Clear model data.
0069  */
0070 void ServerImporter::clear()
0071 {
0072   m_albumListModel->clear();
0073 }
0074 
0075 /**
0076  * Replace HTML entities in a string.
0077  *
0078  * @param str string with HTML entities (e.g. &quot;)
0079  *
0080  * @return string with replaced HTML entities.
0081  */
0082 QString ServerImporter::replaceHtmlEntities(QString str)
0083 {
0084   str.replace(QLatin1String("&quot;"), QLatin1String("\""));
0085   str.replace(QLatin1String("&nbsp;"), QLatin1String(" "));
0086   str.replace(QLatin1String("&lt;"), QLatin1String("<"));
0087   str.replace(QLatin1String("&gt;"), QLatin1String(">"));
0088   str.replace(QLatin1String("&amp;"), QLatin1String("&"));
0089   str.replace(QLatin1String("&times;"), QString(QChar(0xd7)));
0090   str.replace(QLatin1String("&ndash;"), QLatin1String("-"));
0091 
0092   QRegularExpression numEntityRe(QLatin1String("&#(x?\\d+);"));
0093   auto it = numEntityRe.globalMatch(str);
0094   int numCharsRemoved = 0;
0095   while (it.hasNext()) {
0096     auto match = it.next();
0097     QString codeStr = match.captured(1);
0098     int code = codeStr.startsWith(QLatin1Char('x'))
0099         ? codeStr.mid(1).toInt(nullptr, 16) : codeStr.toInt();
0100     int pos = match.capturedStart() - numCharsRemoved;
0101     int len = match.capturedLength();
0102     str.replace(pos, len, QChar(code));
0103     numCharsRemoved += len - 1;
0104   }
0105   return str;
0106 }
0107 
0108 /**
0109  * Replace HTML entities and remove HTML tags.
0110  *
0111  * @param str string containing HTML
0112  *
0113  * @return clean up string
0114  */
0115 QString ServerImporter::removeHtml(QString str)
0116 {
0117   QRegularExpression htmlTagRe(QLatin1String("<[^>]+>"));
0118   return replaceHtmlEntities(str.remove(htmlTagRe)).trimmed();
0119 }
0120 
0121 
0122 AlbumListModel::AlbumListModel(QObject* parent)
0123   : StandardTableModel(parent)
0124 {
0125 }
0126 
0127 /**
0128  * Get an album item.
0129  * @param row model row
0130  * @param text the text is returned here
0131  * @param category the category is returned here
0132  * @param id the internal ID is returned here
0133  */
0134 void AlbumListModel::getItem(int row, QString& text,
0135                              QString& category, QString& id) const
0136 {
0137   if (row < rowCount()) {
0138     QModelIndex idx = index(row, 0);
0139     text = idx.data().toString();
0140     category = idx.data(Qt::UserRole).toString();
0141     id = idx.data(Qt::UserRole + 1).toString();
0142   }
0143 }
0144 
0145 /**
0146  * Append an album item.
0147  * @param text display test
0148  * @param category category, e.g. "release"
0149  * @param id internal ID
0150  */
0151 void AlbumListModel::appendItem(const QString& text,
0152                                 const QString& category, const QString& id)
0153 {
0154   if (int row = rowCount(); insertRow(row)) {
0155     QModelIndex idx = index(row, 0);
0156     setData(idx, text);
0157     setData(idx, category, Qt::UserRole);
0158     setData(idx, id, Qt::UserRole + 1);
0159   }
0160 }