~ubuntu-branches/ubuntu/karmic/kid3/karmic

« back to all changes in this revision

Viewing changes to kid3/browsecoverartdialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-20 16:12:30 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090520161230-qetp532r8ydujkz2
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file browsecoverartdialog.cpp
 
3
 * Browse cover art dialog.
 
4
 *
 
5
 * \b Project: Kid3
 
6
 * \author Urs Fleisch
 
7
 * \date 11-Jan-2009
 
8
 *
 
9
 * Copyright (C) 2009  Urs Fleisch
 
10
 *
 
11
 * This file is part of Kid3.
 
12
 *
 
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.
 
17
 *
 
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.
 
22
 *
 
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/>.
 
25
 */
 
26
 
 
27
#include "browsecoverartdialog.h"
 
28
#include "kid3.h"
 
29
#include "externalprocess.h"
 
30
#include "configtable.h"
 
31
#include <qlayout.h>
 
32
#include <qpushbutton.h>
 
33
#include <qtextedit.h>
 
34
#include <qstring.h>
 
35
#include <qlineedit.h>
 
36
#include <qcombobox.h>
 
37
#include <qtooltip.h>
 
38
#include <qmessagebox.h>
 
39
#include <qregexp.h>
 
40
#include <qurl.h>
 
41
#include "qtcompatmac.h"
 
42
#if QT_VERSION >= 0x040000
 
43
#include <QGroupBox>
 
44
#include <QHBoxLayout>
 
45
#include <QVBoxLayout>
 
46
#else
 
47
#include <qgroupbox.h>
 
48
#endif
 
49
 
 
50
/**
 
51
 * Get help text for supported format codes.
 
52
 *
 
53
 * @return help text.
 
54
 */
 
55
static QString getToolTip()
 
56
{
 
57
        QString str = "<table>\n";
 
58
        str += FrameFormatReplacer::getToolTip(true);
 
59
 
 
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";
 
63
 
 
64
        str += "</table>\n";
 
65
        return str;
 
66
}
 
67
 
 
68
/**
 
69
 * Constructor.
 
70
 *
 
71
 * @param parent parent widget
 
72
 */
 
73
BrowseCoverArtDialog::BrowseCoverArtDialog(QWidget* parent) :
 
74
        QDialog(parent), m_process(0)
 
75
{
 
76
        setModal(true);
 
77
        QCM_setWindowTitle(i18n("Browse Cover Art"));
 
78
 
 
79
        QVBoxLayout* vlayout = new QVBoxLayout(this);
 
80
        if (vlayout) {
 
81
                vlayout->setMargin(6);
 
82
                vlayout->setSpacing(6);
 
83
 
 
84
                m_edit = new QTextEdit(this);
 
85
                if (m_edit) {
 
86
                        m_edit->setReadOnly(true);
 
87
                        vlayout->addWidget(m_edit);
 
88
                }
 
89
 
 
90
#if QT_VERSION >= 0x040000
 
91
                QGroupBox* artistAlbumBox = new QGroupBox(i18n("&Artist/Album"), this);
 
92
#else
 
93
                QGroupBox* artistAlbumBox = new QGroupBox(2, Qt::Horizontal,
 
94
                                                          i18n("&Artist/Album"), this);
 
95
#endif
 
96
                if (artistAlbumBox) {
 
97
                        m_artistLineEdit = new QLineEdit(artistAlbumBox);
 
98
                        m_albumLineEdit = new QLineEdit(artistAlbumBox);
 
99
#if QT_VERSION >= 0x040000
 
100
                        QHBoxLayout* hbox = new QHBoxLayout;
 
101
                        hbox->setMargin(2);
 
102
                        hbox->addWidget(m_artistLineEdit);
 
103
                        hbox->addWidget(m_albumLineEdit);
 
104
                        artistAlbumBox->setLayout(hbox);
 
105
#endif
 
106
                        vlayout->addWidget(artistAlbumBox);
 
107
                        connect(m_artistLineEdit, SIGNAL(returnPressed()),
 
108
                                                        this, SLOT(showPreview()));
 
109
                        connect(m_albumLineEdit, SIGNAL(returnPressed()),
 
110
                                                        this, SLOT(showPreview()));
 
111
                }
 
112
 
 
113
#if QT_VERSION >= 0x040000
 
114
                QGroupBox* srcbox = new QGroupBox(i18n("&Source"), this);
 
115
#else
 
116
                QGroupBox* srcbox = new QGroupBox(2, Qt::Vertical, i18n("&Source"), this);
 
117
#endif
 
118
                if (srcbox) {
 
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;
 
125
                        vbox->setMargin(2);
 
126
                        vbox->addWidget(m_sourceComboBox);
 
127
                        vbox->addWidget(m_urlLineEdit);
 
128
                        srcbox->setLayout(vbox);
 
129
#endif
 
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()));
 
135
                }
 
136
 
 
137
#if QT_VERSION >= 0x040000
 
138
                QGroupBox* tabbox = new QGroupBox(i18n("&URL extraction"), this);
 
139
#else
 
140
                QGroupBox* tabbox = new QGroupBox(1, Qt::Vertical,
 
141
                                                  i18n("&URL extraction"), this);
 
142
#endif
 
143
                if (tabbox) {
 
144
                        m_matchUrlTable = new ConfigTable(
 
145
                                QStringList()
 
146
                                << i18n("Match")
 
147
                                << i18n("Picture URL"),
 
148
                                tabbox);
 
149
#if QT_VERSION >= 0x040000
 
150
                        QVBoxLayout* tablayout = new QVBoxLayout;
 
151
                        tablayout->setMargin(2);
 
152
                        tablayout->addWidget(m_matchUrlTable);
 
153
                        tabbox->setLayout(tablayout);
 
154
#endif
 
155
                        vlayout->addWidget(tabbox);
 
156
                }
 
157
                QHBoxLayout* hlayout = new QHBoxLayout;
 
158
                if (hlayout) {
 
159
                        hlayout->setSpacing(6);
 
160
                        QPushButton* helpButton = new QPushButton(i18n("&Help"), this);
 
161
                        if (helpButton) {
 
162
                                helpButton->setAutoDefault(false);
 
163
                                hlayout->addWidget(helpButton);
 
164
                                connect(helpButton, SIGNAL(clicked()), this, SLOT(showHelp()));
 
165
                        }
 
166
                        QPushButton* saveButton = new QPushButton(i18n("&Save Settings"), this);
 
167
                        if (saveButton) {
 
168
                                saveButton->setAutoDefault(false);
 
169
                                hlayout->addWidget(saveButton);
 
170
                                connect(saveButton, SIGNAL(clicked()), this, SLOT(saveConfig()));
 
171
                        }
 
172
                        QSpacerItem* hspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
 
173
                                                                                                                                                                                 QSizePolicy::Minimum);
 
174
                        hlayout->addItem(hspacer);
 
175
 
 
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()));
 
185
                        }
 
186
                        vlayout->addLayout(hlayout);
 
187
                }
 
188
        }
 
189
}
 
190
 
 
191
/**
 
192
 * Destructor.
 
193
 */
 
194
BrowseCoverArtDialog::~BrowseCoverArtDialog()
 
195
{
 
196
        delete m_process;
 
197
}
 
198
 
 
199
/**
 
200
 * Set the format lineedits to the format selected in the combo box.
 
201
 *
 
202
 * @param index current index of the combo box
 
203
 */
 
204
void BrowseCoverArtDialog::setSourceLineEdit(int index)
 
205
{
 
206
        if (index < static_cast<int>(m_urls.size())) {
 
207
                m_urlLineEdit->setText(m_urls[index]);
 
208
        } else {
 
209
                m_urlLineEdit->clear();
 
210
        }
 
211
        showPreview();
 
212
}
 
213
 
 
214
/**
 
215
 * Show browse command as preview.
 
216
 */
 
217
void BrowseCoverArtDialog::showPreview()
 
218
{
 
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;
 
229
        txt += " ";
 
230
        txt += m_url;
 
231
        txt += "</tt></p><p><b>";
 
232
        txt += i18n("Then drag the picture from the browser to Kid3.");
 
233
        txt += "</b></p>";
 
234
        m_edit->clear();
 
235
        m_edit->append(txt);
 
236
}
 
237
 
 
238
/**
 
239
 * Set frames for which picture has to be found.
 
240
 *
 
241
 * @param frames track data
 
242
 */
 
243
void BrowseCoverArtDialog::setFrames(const FrameCollection& frames)
 
244
{
 
245
        m_frames = frames;
 
246
 
 
247
        m_artistLineEdit->setText(m_frames.getArtist());
 
248
        m_albumLineEdit->setText(m_frames.getAlbum());
 
249
 
 
250
        showPreview();
 
251
}
 
252
 
 
253
/**
 
254
 * Set the source combo box and line edits from the configuration.
 
255
 */
 
256
void BrowseCoverArtDialog::setSourceFromConfig()
 
257
{
 
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);
 
263
}
 
264
 
 
265
/**
 
266
 * Read the local settings from the configuration.
 
267
 */
 
268
void BrowseCoverArtDialog::readConfig()
 
269
{
 
270
        setSourceFromConfig();
 
271
        m_matchUrlTable->fromMap(Kid3App::s_genCfg.m_matchPictureUrlMap);
 
272
 
 
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);
 
277
        }
 
278
}
 
279
 
 
280
/**
 
281
 * Save the local settings to the configuration.
 
282
 */
 
283
void BrowseCoverArtDialog::saveConfig()
 
284
{
 
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();
 
292
        } else {
 
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());
 
297
        }
 
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();
 
301
 
 
302
        setSourceFromConfig();
 
303
}
 
304
 
 
305
/**
 
306
 * Show help.
 
307
 */
 
308
void BrowseCoverArtDialog::showHelp()
 
309
{
 
310
        Kid3App::displayHelp("browse_pictures");
 
311
}
 
312
 
 
313
/**
 
314
 * Hide modal dialog, start browse command.
 
315
 */
 
316
void BrowseCoverArtDialog::accept()
 
317
{
 
318
        if (!m_process) {
 
319
                m_process = new ExternalProcess(this);
 
320
        }
 
321
        if (m_process) {
 
322
                m_process->launchCommand(
 
323
                        i18n("Browse Cover Art"),
 
324
                        QStringList() << Kid3App::s_miscCfg.m_browser << m_url);
 
325
        }
 
326
        QDialog::accept();
 
327
}
 
328
 
 
329
/**
 
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.
 
333
 *
 
334
 * @param url URL from image drag
 
335
 *
 
336
 * @return URL of image file, empty if no image URL found.
 
337
 */
 
338
QString BrowseCoverArtDialog::getImageUrl(const QString& url)
 
339
{
 
340
        QString imgurl;
 
341
        if (url.startsWith("http://")) {
 
342
                if (url.endsWith(".jpg", QCM_CaseInsensitive) ||
 
343
                                url.endsWith(".jpeg", QCM_CaseInsensitive) ||
 
344
                                url.endsWith(".png", QCM_CaseInsensitive)) {
 
345
                        imgurl = url;
 
346
                }
 
347
                else {
 
348
                        for (QMap<QString, QString>::ConstIterator it =
 
349
                                                 Kid3App::s_genCfg.m_matchPictureUrlMap.begin();
 
350
                                         it != Kid3App::s_genCfg.m_matchPictureUrlMap.end();
 
351
                                         ++it) {
 
352
                                QRegExp re(it.key());
 
353
                                if (re.exactMatch(url)) {
 
354
                                        QString pictureUrl(url);
 
355
                                        imgurl = url;
 
356
                                        imgurl.replace(re, *it);
 
357
                                        if (imgurl.QCM_indexOf("%25") != -1) {
 
358
                                                // double URL encoded: first decode
 
359
                                                QCM_QUrl_decode(imgurl);
 
360
                                        }
 
361
                                        if (imgurl.QCM_indexOf("%2F") != -1) {
 
362
                                                // URL encoded: decode
 
363
                                                QCM_QUrl_decode(imgurl);
 
364
                                        }
 
365
                                        break;
 
366
                                }
 
367
                        }
 
368
                }
 
369
        }
 
370
        return imgurl;
 
371
}