2
* \file browsecoverartdialog.cpp
3
* Browse cover art dialog.
9
* Copyright (C) 2009 Urs Fleisch
11
* This file is part of Kid3.
13
* Kid3 is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* Kid3 is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
#include "browsecoverartdialog.h"
29
#include "externalprocess.h"
30
#include "configtable.h"
32
#include <qpushbutton.h>
33
#include <qtextedit.h>
35
#include <qlineedit.h>
36
#include <qcombobox.h>
38
#include <qmessagebox.h>
41
#include "qtcompatmac.h"
42
#if QT_VERSION >= 0x040000
44
#include <QHBoxLayout>
45
#include <QVBoxLayout>
47
#include <qgroupbox.h>
51
* Get help text for supported format codes.
55
static QString getToolTip()
57
QString str = "<table>\n";
58
str += FrameFormatReplacer::getToolTip(true);
60
str += "<tr><td>%ua...</td><td>%u{artist}...</td><td>";
61
str += QCM_translate(I18N_NOOP("Encode as URL"));
62
str += "</td></tr>\n";
71
* @param parent parent widget
73
BrowseCoverArtDialog::BrowseCoverArtDialog(QWidget* parent) :
74
QDialog(parent), m_process(0)
77
QCM_setWindowTitle(i18n("Browse Cover Art"));
79
QVBoxLayout* vlayout = new QVBoxLayout(this);
81
vlayout->setMargin(6);
82
vlayout->setSpacing(6);
84
m_edit = new QTextEdit(this);
86
m_edit->setReadOnly(true);
87
vlayout->addWidget(m_edit);
90
#if QT_VERSION >= 0x040000
91
QGroupBox* artistAlbumBox = new QGroupBox(i18n("&Artist/Album"), this);
93
QGroupBox* artistAlbumBox = new QGroupBox(2, Qt::Horizontal,
94
i18n("&Artist/Album"), this);
97
m_artistLineEdit = new QLineEdit(artistAlbumBox);
98
m_albumLineEdit = new QLineEdit(artistAlbumBox);
99
#if QT_VERSION >= 0x040000
100
QHBoxLayout* hbox = new QHBoxLayout;
102
hbox->addWidget(m_artistLineEdit);
103
hbox->addWidget(m_albumLineEdit);
104
artistAlbumBox->setLayout(hbox);
106
vlayout->addWidget(artistAlbumBox);
107
connect(m_artistLineEdit, SIGNAL(returnPressed()),
108
this, SLOT(showPreview()));
109
connect(m_albumLineEdit, SIGNAL(returnPressed()),
110
this, SLOT(showPreview()));
113
#if QT_VERSION >= 0x040000
114
QGroupBox* srcbox = new QGroupBox(i18n("&Source"), this);
116
QGroupBox* srcbox = new QGroupBox(2, Qt::Vertical, i18n("&Source"), this);
119
m_sourceComboBox = new QComboBox(srcbox);
120
m_sourceComboBox->setEditable(true);
121
m_urlLineEdit = new QLineEdit(srcbox);
122
QCM_setToolTip(m_urlLineEdit, getToolTip());
123
#if QT_VERSION >= 0x040000
124
QVBoxLayout* vbox = new QVBoxLayout;
126
vbox->addWidget(m_sourceComboBox);
127
vbox->addWidget(m_urlLineEdit);
128
srcbox->setLayout(vbox);
130
vlayout->addWidget(srcbox);
131
connect(m_sourceComboBox, SIGNAL(activated(int)), this,
132
SLOT(setSourceLineEdit(int)));
133
connect(m_urlLineEdit, SIGNAL(returnPressed()),
134
this, SLOT(showPreview()));
137
#if QT_VERSION >= 0x040000
138
QGroupBox* tabbox = new QGroupBox(i18n("&URL extraction"), this);
140
QGroupBox* tabbox = new QGroupBox(1, Qt::Vertical,
141
i18n("&URL extraction"), this);
144
m_matchUrlTable = new ConfigTable(
147
<< i18n("Picture URL"),
149
#if QT_VERSION >= 0x040000
150
QVBoxLayout* tablayout = new QVBoxLayout;
151
tablayout->setMargin(2);
152
tablayout->addWidget(m_matchUrlTable);
153
tabbox->setLayout(tablayout);
155
vlayout->addWidget(tabbox);
157
QHBoxLayout* hlayout = new QHBoxLayout;
159
hlayout->setSpacing(6);
160
QPushButton* helpButton = new QPushButton(i18n("&Help"), this);
162
helpButton->setAutoDefault(false);
163
hlayout->addWidget(helpButton);
164
connect(helpButton, SIGNAL(clicked()), this, SLOT(showHelp()));
166
QPushButton* saveButton = new QPushButton(i18n("&Save Settings"), this);
168
saveButton->setAutoDefault(false);
169
hlayout->addWidget(saveButton);
170
connect(saveButton, SIGNAL(clicked()), this, SLOT(saveConfig()));
172
QSpacerItem* hspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
173
QSizePolicy::Minimum);
174
hlayout->addItem(hspacer);
176
QPushButton* browseButton = new QPushButton(i18n("&Browse"), this);
177
QPushButton* cancelButton = new QPushButton(i18n("&Cancel"), this);
178
if (browseButton && cancelButton) {
179
browseButton->setAutoDefault(false);
180
cancelButton->setAutoDefault(false);
181
hlayout->addWidget(browseButton);
182
hlayout->addWidget(cancelButton);
183
connect(browseButton, SIGNAL(clicked()), this, SLOT(accept()));
184
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
186
vlayout->addLayout(hlayout);
194
BrowseCoverArtDialog::~BrowseCoverArtDialog()
200
* Set the format lineedits to the format selected in the combo box.
202
* @param index current index of the combo box
204
void BrowseCoverArtDialog::setSourceLineEdit(int index)
206
if (index < static_cast<int>(m_urls.size())) {
207
m_urlLineEdit->setText(m_urls[index]);
209
m_urlLineEdit->clear();
215
* Show browse command as preview.
217
void BrowseCoverArtDialog::showPreview()
219
m_frames.setArtist(m_artistLineEdit->text());
220
m_frames.setAlbum(m_albumLineEdit->text());
221
FrameFormatReplacer fmt(m_frames, m_urlLineEdit->text());
222
fmt.replaceEscapedChars();
223
fmt.replacePercentCodes(FormatReplacer::FSF_SupportUrlEncode);
224
m_url = fmt.getString();
225
QString txt("<p><b>");
226
txt += i18n("Click Browse to start");
227
txt += "</b></p><p><tt>";
228
txt += Kid3App::s_miscCfg.m_browser;
231
txt += "</tt></p><p><b>";
232
txt += i18n("Then drag the picture from the browser to Kid3.");
239
* Set frames for which picture has to be found.
241
* @param frames track data
243
void BrowseCoverArtDialog::setFrames(const FrameCollection& frames)
247
m_artistLineEdit->setText(m_frames.getArtist());
248
m_albumLineEdit->setText(m_frames.getAlbum());
254
* Set the source combo box and line edits from the configuration.
256
void BrowseCoverArtDialog::setSourceFromConfig()
258
m_urls = Kid3App::s_genCfg.m_pictureSourceUrls;
259
m_sourceComboBox->clear();
260
m_sourceComboBox->QCM_addItems(Kid3App::s_genCfg.m_pictureSourceNames);
261
m_sourceComboBox->QCM_setCurrentIndex(Kid3App::s_genCfg.m_pictureSourceIdx);
262
setSourceLineEdit(Kid3App::s_genCfg.m_pictureSourceIdx);
266
* Read the local settings from the configuration.
268
void BrowseCoverArtDialog::readConfig()
270
setSourceFromConfig();
271
m_matchUrlTable->fromMap(Kid3App::s_genCfg.m_matchPictureUrlMap);
273
if (Kid3App::s_genCfg.m_browseCoverArtWindowWidth > 0 &&
274
Kid3App::s_genCfg.m_browseCoverArtWindowHeight > 0) {
275
resize(Kid3App::s_genCfg.m_browseCoverArtWindowWidth,
276
Kid3App::s_genCfg.m_browseCoverArtWindowHeight);
281
* Save the local settings to the configuration.
283
void BrowseCoverArtDialog::saveConfig()
285
Kid3App::s_genCfg.m_pictureSourceIdx = m_sourceComboBox->QCM_currentIndex();
286
if (Kid3App::s_genCfg.m_pictureSourceIdx <
287
static_cast<int>(Kid3App::s_genCfg.m_pictureSourceNames.size())) {
288
Kid3App::s_genCfg.m_pictureSourceNames[Kid3App::s_genCfg.m_pictureSourceIdx] =
289
m_sourceComboBox->currentText();
290
Kid3App::s_genCfg.m_pictureSourceUrls[Kid3App::s_genCfg.m_pictureSourceIdx] =
291
m_urlLineEdit->text();
293
Kid3App::s_genCfg.m_pictureSourceIdx =
294
Kid3App::s_genCfg.m_pictureSourceNames.size();
295
Kid3App::s_genCfg.m_pictureSourceNames.append(m_sourceComboBox->currentText());
296
Kid3App::s_genCfg.m_pictureSourceUrls.append(m_urlLineEdit->text());
298
m_matchUrlTable->toMap(Kid3App::s_genCfg.m_matchPictureUrlMap);
299
Kid3App::s_genCfg.m_browseCoverArtWindowWidth = size().width();
300
Kid3App::s_genCfg.m_browseCoverArtWindowHeight = size().height();
302
setSourceFromConfig();
308
void BrowseCoverArtDialog::showHelp()
310
Kid3App::displayHelp("browse_pictures");
314
* Hide modal dialog, start browse command.
316
void BrowseCoverArtDialog::accept()
319
m_process = new ExternalProcess(this);
322
m_process->launchCommand(
323
i18n("Browse Cover Art"),
324
QStringList() << Kid3App::s_miscCfg.m_browser << m_url);
330
* Get the URL of an image file.
331
* The input URL is transformed using the match picture URL table to
332
* get the URL of an image file.
334
* @param url URL from image drag
336
* @return URL of image file, empty if no image URL found.
338
QString BrowseCoverArtDialog::getImageUrl(const QString& url)
341
if (url.startsWith("http://")) {
342
if (url.endsWith(".jpg", QCM_CaseInsensitive) ||
343
url.endsWith(".jpeg", QCM_CaseInsensitive) ||
344
url.endsWith(".png", QCM_CaseInsensitive)) {
348
for (QMap<QString, QString>::ConstIterator it =
349
Kid3App::s_genCfg.m_matchPictureUrlMap.begin();
350
it != Kid3App::s_genCfg.m_matchPictureUrlMap.end();
352
QRegExp re(it.key());
353
if (re.exactMatch(url)) {
354
QString pictureUrl(url);
356
imgurl.replace(re, *it);
357
if (imgurl.QCM_indexOf("%25") != -1) {
358
// double URL encoded: first decode
359
QCM_QUrl_decode(imgurl);
361
if (imgurl.QCM_indexOf("%2F") != -1) {
362
// URL encoded: decode
363
QCM_QUrl_decode(imgurl);