1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more detailexampleSong.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "filenameschemedialog.h"
25 #include "mpd-interface/song.h"
26 #include "support/messagebox.h"
27 
28 static const char * constVariableProperty="cantata-var";
FilenameSchemeDialog(QWidget * parent)29 FilenameSchemeDialog::FilenameSchemeDialog(QWidget *parent)
30     : Dialog(parent)
31 {
32     setButtons(Ok|Cancel);
33     setCaption(tr("Filename Scheme"));
34     setWindowModality(Qt::WindowModal);
35     QWidget *mainWidet = new QWidget(this);
36     setupUi(mainWidet);
37     setMainWidget(mainWidet);
38     connect(pattern, SIGNAL(textChanged(const QString &)), this, SLOT(enableOkButton()));
39     connect(pattern, SIGNAL(textChanged(const QString &)), this, SLOT(updateExample()));
40     connect(help, SIGNAL(leftClickedUrl()), this, SLOT(showHelp()));
41     connect(albumArtist, SIGNAL(clicked()), this, SLOT(insertVariable()));
42     connect(composer, SIGNAL(clicked()), this, SLOT(insertVariable()));
43     connect(albumTitle, SIGNAL(clicked()), this, SLOT(insertVariable()));
44     connect(trackArtist, SIGNAL(clicked()), this, SLOT(insertVariable()));
45     connect(trackTitle, SIGNAL(clicked()), this, SLOT(insertVariable()));
46     connect(trackArtistAndTitle, SIGNAL(clicked()), this, SLOT(insertVariable()));
47     connect(trackNo, SIGNAL(clicked()), this, SLOT(insertVariable()));
48     connect(cdNo, SIGNAL(clicked()), this, SLOT(insertVariable()));
49     connect(genre, SIGNAL(clicked()), this, SLOT(insertVariable()));
50     connect(year, SIGNAL(clicked()), this, SLOT(insertVariable()));
51     albumArtist->setProperty(constVariableProperty, DeviceOptions::constAlbumArtist);
52     albumTitle->setProperty(constVariableProperty, DeviceOptions::constAlbumTitle);
53     composer->setProperty(constVariableProperty, DeviceOptions::constComposer);
54     trackArtist->setProperty(constVariableProperty, DeviceOptions::constTrackArtist);
55     trackTitle->setProperty(constVariableProperty, DeviceOptions::constTrackTitle);
56     trackArtistAndTitle->setProperty(constVariableProperty, DeviceOptions::constTrackArtistAndTitle);
57     trackNo->setProperty(constVariableProperty, DeviceOptions::constTrackNumber);
58     cdNo->setProperty(constVariableProperty, DeviceOptions::constCdNumber);
59     year->setProperty(constVariableProperty, DeviceOptions::constYear);
60     genre->setProperty(constVariableProperty, DeviceOptions::constGenre);
61     exampleSong.albumartist=tr("Various Artists", "Example album artist");
62     exampleSong.artist=tr("Wibble", "Example artist");
63     exampleSong.setComposer(tr("Vivaldi", "Example composer"));
64     exampleSong.album=tr("Now 5001", "Example album");
65     exampleSong.title=tr("Wobble", "Example song name");
66     exampleSong.genres[0]=tr("Dance", "Example genre");
67     exampleSong.track=1;
68     exampleSong.disc=2;
69     exampleSong.year=2001;
70     exampleSong.file="wibble.mp3";
71     adjustSize();
72     setMaximumHeight(height());
73 }
74 
show(const DeviceOptions & opts)75 void FilenameSchemeDialog::show(const DeviceOptions &opts)
76 {
77     origOpts=opts;
78     pattern->setText(opts.scheme);
79     example->setText(origOpts.createFilename(exampleSong));
80     Dialog::show();
81     enableButtonOk(false);
82 }
83 
slotButtonClicked(int button)84 void FilenameSchemeDialog::slotButtonClicked(int button)
85 {
86     switch (button) {
87     case Ok:
88         emit scheme(pattern->text().trimmed());
89         break;
90     case Cancel:
91         reject();
92         break;
93     default:
94         break;
95     }
96 
97     if (Ok==button) {
98         accept();
99     }
100 
101     Dialog::slotButtonClicked(button);
102 }
103 
stripAccelerator(QString str)104 static QString stripAccelerator(QString str)
105 {
106     return str.replace("&&", "____").replace("&", "").replace("____", "&");
107 }
108 
tableEntry(const QAbstractButton * widget)109 static QString tableEntry(const QAbstractButton *widget)
110 {
111     return QLatin1String("<tr><td>")+widget->property(constVariableProperty).toString()+
112            QLatin1String("</td><td>")+stripAccelerator(widget->text())+
113            QLatin1String("</td><td>")+widget->toolTip()+
114            QLatin1String("</td></tr>");
115 }
116 
showHelp()117 void FilenameSchemeDialog::showHelp()
118 {
119     MessageBox::information(this,
120                            tr("The following variables will be replaced with their corresponding meaning for each track name.")+
121                            QLatin1String("<br/><br/>")+
122                            #ifdef Q_OS_MAC
123                            QLatin1String("<small>")+
124                            #endif
125                            QLatin1String("<table border=\"1\">")+
126                            tr("<tr><th><em>Variable</em></th><th><em>Button</em></th><th><em>Description</em></th></tr>")+
127                            tableEntry(albumArtist)+tableEntry(albumTitle)+tableEntry(composer)+tableEntry(trackArtist)+
128                            tableEntry(trackTitle)+tableEntry(trackArtistAndTitle)+tableEntry(trackNo)+tableEntry(cdNo)+
129                            tableEntry(year)+tableEntry(genre)+
130                            QLatin1String("</table>")
131                            #ifdef Q_OS_MAC
132                            +QLatin1String("</small>")
133                            #endif
134                            );
135 }
136 
enableOkButton()137 void FilenameSchemeDialog::enableOkButton()
138 {
139     QString scheme=pattern->text().trimmed();
140     enableButtonOk(!scheme.isEmpty() && scheme!=origOpts.scheme);
141 }
142 
insertVariable()143 void FilenameSchemeDialog::insertVariable()
144 {
145     QString text(pattern->text());
146     QString insert=sender()->property(constVariableProperty).toString();
147     int pos=pattern->cursorPosition();
148     text.insert(pos, insert);
149     pattern->setText(text);
150     pattern->setCursorPosition(pos+insert.length());
151     pattern->setFocus();
152     updateExample();
153 }
154 
updateExample()155 void FilenameSchemeDialog::updateExample()
156 {
157     QString saveScheme=origOpts.scheme;
158     origOpts.scheme=pattern->text();
159     QString exampleStr=origOpts.createFilename(exampleSong);
160     if (!exampleStr.endsWith(QLatin1String(".mp3"))) {
161         exampleStr+=QLatin1String(".mp3");
162     }
163     example->setText(exampleStr);
164     origOpts.scheme=saveScheme;
165 }
166 
167 #include "moc_filenameschemedialog.cpp"
168