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 details.
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 <QFormLayout>
25 #include <QIcon>
26 #include <QUrl>
27 #include <QApplication>
28 #include "streamdialog.h"
29 #include "gui/settings.h"
30 #include "models/streamsmodel.h"
31 #include "widgets/icons.h"
32 #include "mpd-interface/mpdconnection.h"
33 #include "support/buddylabel.h"
34 #include "support/utils.h"
35 #include "config.h"
36
StreamDialog(QWidget * parent,bool addToPlayQueue)37 StreamDialog::StreamDialog(QWidget *parent, bool addToPlayQueue)
38 : Dialog(parent)
39 , saveCheckbox(nullptr)
40 , urlHandlers(MPDConnection::self()->urlHandlers())
41 {
42 QWidget *wid = new QWidget(this);
43 QFormLayout *layout = new QFormLayout(wid);
44
45 layout->setMargin(0);
46 urlEntry = new LineEdit(wid);
47 nameEntry = new LineEdit(wid);
48 if (addToPlayQueue) {
49 saveCheckbox=new QCheckBox(tr("Add stream to favourites"), wid);
50 }
51 statusText = new QLabel(this);
52
53 urlEntry->setMinimumWidth(300);
54 nameLabel=new BuddyLabel(tr("Name:"), wid, nameEntry);
55 BuddyLabel *urlLabel=new BuddyLabel(tr("URL:"), wid, urlEntry);
56
57 int row=0;
58 layout->setWidget(row, QFormLayout::LabelRole, urlLabel);
59 layout->setWidget(row++, QFormLayout::FieldRole, urlEntry);
60 layout->setWidget(row, QFormLayout::LabelRole, nameLabel);
61 layout->setWidget(row++, QFormLayout::FieldRole, nameEntry);
62 if (addToPlayQueue) {
63 saveCheckbox->setChecked(false);
64 layout->setWidget(row++, QFormLayout::FieldRole, saveCheckbox);
65 connect(saveCheckbox, SIGNAL(toggled(bool)), SLOT(changed()));
66 }
67 layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
68
69 layout->setWidget(row++, QFormLayout::SpanningRole, statusText);
70 setCaption(tr("Add Stream"));
71 setMainWidget(wid);
72 setButtons(Ok|Cancel);
73 enableButton(Ok, false);
74 connect(nameEntry, SIGNAL(textChanged(const QString &)), SLOT(changed()));
75 connect(urlEntry, SIGNAL(textChanged(const QString &)), SLOT(changed()));
76 urlEntry->setFocus();
77 resize(400, 100);
78 }
79
setEdit(const QString & editName,const QString & editUrl)80 void StreamDialog::setEdit(const QString &editName, const QString &editUrl)
81 {
82 setCaption(tr("Edit Stream"));
83 enableButton(Ok, false);
84 prevName=editName;
85 prevUrl=editUrl;
86 nameEntry->setText(editName);
87 urlEntry->setText(editUrl);
88 }
89
changed()90 void StreamDialog::changed()
91 {
92 QString u=url();
93 bool urlOk=u.length()>5 && u.contains(QLatin1String("://"));
94 bool validProtocol=u.isEmpty() || urlHandlers.contains(QUrl(u).scheme()) || urlHandlers.contains(u);
95 bool enableOk=false;
96
97 if (!save()) {
98 enableOk=urlOk;
99 } else {
100 QString n=name();
101 enableOk=!n.isEmpty() && urlOk && (n!=prevName || u!=prevUrl);
102 }
103 statusText->setText(validProtocol || !urlOk ? QString() : tr("<i><b>ERROR:</b> Invalid protocol</i>"));
104 enableOk=enableOk && validProtocol;
105 enableButton(Ok, enableOk);
106 }
107
108 #include "moc_streamdialog.cpp"
109