2 * Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <QCoreApplication>
21 #include <QVBoxLayout>
22 #include <QHBoxLayout>
31 #include <NSettingsMutator>
32 #include <NSettingsDialog>
35 extern nn::NSettingsMutator settings
; // main.cpp
40 static void detect_type(const DetectType t
, QComboBox
*c
)
43 if (detect::find(t
, r
))
45 foreach (const NDetectResult p
, r
)
47 c
->addItem(p
.first
, p
.second
.first
);
49 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< p
;
53 c
->addItem(qApp
->translate("nn::NSettingsCommands", "Other"));
56 static QVBoxLayout
* newVBox(QWidget
*p
,
64 const QStringList
& specs
=QStringList())
68 p
->connect(*c
, SIGNAL(currentIndexChanged(QString
)), p
, f
);
75 foreach (const QString s
, specs
)
82 QLabel
*l
= new QLabel(t
);
85 QHBoxLayout
*hbox
= new QHBoxLayout
;
89 QVBoxLayout
*vbox
= new QVBoxLayout
;
90 vbox
->addLayout(hbox
);
104 static QString
tooltip(const int t
, const QString
& arg1
)
106 static const char *str
[] =
108 QT_TRANSLATE_NOOP("nn::NSettingsCommands",
109 "Recommended value: \"%1\""),
111 QT_TRANSLATE_NOOP("nn::NSettingsCommands",
112 " %f .. Path to downloaded media file"),
114 QT_TRANSLATE_NOOP("nn::NSettingsCommands",
115 " %u .. Media page URL"),
117 QT_TRANSLATE_NOOP("nn::NSettingsCommands",
118 " %m .. Media stream URL or path to downloaded media file"),
121 QString s
= NSettingsCommands::tr(str
[0]).arg(arg1
);
124 s
+= "\n" + NSettingsCommands::tr(str
[1]);
127 s
+= "\n" + NSettingsCommands::tr(str
[2]);
130 s
+= "\n" + NSettingsCommands::tr(str
[3]);
135 NSettingsCommands::NSettingsCommands(QWidget
*parent
/*=NULL*/)
136 : NSettingsWidget(parent
)
139 // Parse media stream URLs using
141 QVBoxLayout
*parseUsingBox
=
143 tr("Pa&rse media stream URLs using"),
146 SLOT(parseUsingChanged(QString
)),
147 tooltip(SpecU
, "quvi --category-http %u")
149 detect_type(MediaParser
, parseUsingCombo
);
153 QVBoxLayout
*downloadUsingBox
=
155 tr("Do&wnload using"),
158 SLOT(downloadUsingChanged(QString
)),
160 "curl -L -C - -o %f %u --user-agent Mozilla/5.0")
162 detect_type(Downloader
, downloadUsingCombo
);
166 QVBoxLayout
*playUsingBox
=
171 SLOT(playUsingChanged(QString
)),
172 tooltip(SpecM
, "xdg-open %m")
174 detect_type(MediaPlayer
, playUsingCombo
);
178 QVBoxLayout
*feedUsingBox
=
180 tr("Read &YouTube feeds using"),
183 SLOT(feedUsingChanged(QString
)),
184 tooltip(Recommended
, "umph")
186 detect_type(FeedParser
, feedUsingCombo
);
190 QVBoxLayout
*box
= new QVBoxLayout
;
191 box
->addLayout(parseUsingBox
);
192 box
->addLayout(downloadUsingBox
);
193 box
->addLayout(playUsingBox
);
194 box
->addLayout(feedUsingBox
);
199 void NSettingsCommands::init()
203 static bool _verify(QComboBox
*combo
, QLineEdit
*edit
, QString
& msg
)
205 const QString text
= combo
->currentText();
206 if (text
== qApp
->translate("nn::NSettingsCommands", "Other")
207 && edit
->text().isEmpty())
209 msg
= qApp
->translate("nn::NSettingsCommands",
210 "Please enter a custom command");
213 return msg
.isEmpty();
216 bool NSettingsCommands::verify(QString
& msg
)
218 bool r
= _verify(parseUsingCombo
, parseUsingEdit
, msg
);
220 r
= _verify(playUsingCombo
, playUsingEdit
, msg
);
222 r
= _verify(downloadUsingCombo
, downloadUsingEdit
, msg
);
224 r
= _verify(feedUsingCombo
, feedUsingEdit
, msg
);
228 static void _write(SettingKey comboKey
,
233 const int n
= combo
->currentIndex();
234 const QVariant v
= combo
->itemData(n
);
236 // Config must use a static "other" (non-translatable) string instead
237 // of using the combobox UI string which may be a translated string.
239 QString ct
= combo
->currentText();
240 if (ct
== qApp
->translate("nn::NSettingsCommands", "Other"))
243 settings
.setValue(comboKey
, QString("%1%2%3")
245 .arg(NSETTINGS_CMDPATH_SEPARATOR
)
247 settings
.setValue(editKey
, edit
->text());
250 void NSettingsCommands::write()
253 _write(ParseUsing
, parseUsingCombo
,
254 ParseUsingOther
, parseUsingEdit
);
256 _write(DownloadUsing
, downloadUsingCombo
,
257 DownloadUsingOther
, downloadUsingEdit
);
259 _write(PlayUsing
, playUsingCombo
,
260 PlayUsingOther
, playUsingEdit
);
262 _write(FeedUsing
, feedUsingCombo
,
263 FeedUsingOther
, feedUsingEdit
);
266 static void _read(SettingKey comboKey
,
271 QString text
= settings
.value(comboKey
).toString();
272 QStringList a
= text
.split(NSETTINGS_CMDPATH_SEPARATOR
);
274 // Config must use a static "other" (non-translatable) string instead
275 // of using the combobox UI string which may be a translated string.
278 a
[0] = qApp
->translate("nn::NSettingsCommands", "Other");
280 const int n
= combo
->findText(a
[0]);
283 #ifdef ENABLE_VERBOSE
284 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
289 combo
->setItemData(n
, a
[1]);
290 combo
->setCurrentIndex(n
);
297 qWarning() << __PRETTY_FUNCTION__
<< __LINE__
299 << "not found in combobox values";
303 text
= settings
.value(editKey
).toString();
307 void NSettingsCommands::read()
310 _read(ParseUsing
, parseUsingCombo
,
311 ParseUsingOther
, parseUsingEdit
);
313 _read(DownloadUsing
, downloadUsingCombo
,
314 DownloadUsingOther
, downloadUsingEdit
);
316 _read(PlayUsing
, playUsingCombo
,
317 PlayUsingOther
, playUsingEdit
);
319 _read(FeedUsing
, feedUsingCombo
,
320 FeedUsingOther
, feedUsingEdit
);
323 void NSettingsCommands::parseUsingChanged(QString text
)
325 parseUsingEdit
->setVisible(text
== tr("Other"));
328 void NSettingsCommands::downloadUsingChanged(QString text
)
330 downloadUsingEdit
->setVisible(text
== tr("Other"));
333 void NSettingsCommands::playUsingChanged(QString text
)
335 playUsingEdit
->setVisible(text
== tr("Other"));
338 void NSettingsCommands::feedUsingChanged(QString text
)
340 feedUsingEdit
->setVisible(text
== tr("Other"));
345 /* vim: set ts=2 sw=2 tw=72 expandtab: */