Update Finnish translation
[nomnom.git] / src / settings / nsettingsdialog_commands.cpp
blob9523d6c7cfac633a1d551ee6e753f8a40d5d6b03
1 /* NomNom
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/>.
18 #include "config.h"
20 #include <QCoreApplication>
21 #include <QVBoxLayout>
22 #include <QHBoxLayout>
23 #include <QComboBox>
24 #include <QLineEdit>
25 #include <QLabel>
27 #ifdef ENABLE_VERBOSE
28 #include <QDebug>
29 #endif
31 #include <NSettingsMutator>
32 #include <NSettingsDialog>
33 #include <NDetect>
35 extern nn::NSettingsMutator settings; // main.cpp
37 namespace nn
40 static void detect_type(const DetectType t, QComboBox *c)
42 NDetectResultList r;
43 if (detect::find(t, r))
45 foreach (const NDetectResult p, r)
47 c->addItem(p.first, p.second.first);
48 #ifdef ENABLE_VERBOSE
49 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << p;
50 #endif
53 c->addItem(qApp->translate("nn::NSettingsCommands", "Other"));
56 static QVBoxLayout* newVBox(QWidget *p,
57 const QString& t,
58 QComboBox **c,
59 QLineEdit **e,
60 const char *f,
61 const QString& tt)
62 #ifdef _1
63 const QString& r,
64 const QStringList& specs=QStringList())
65 #endif
67 *c = new QComboBox;
68 p->connect(*c, SIGNAL(currentIndexChanged(QString)), p, f);
70 *e = new QLineEdit;
71 (*e)->hide();
73 #ifdef _1
74 QString tooltip = r;
75 foreach (const QString s, specs)
77 tooltip += "\n" + s;
79 #endif
80 (*e)->setToolTip(tt);
82 QLabel *l = new QLabel(t);
83 l->setBuddy(*c);
85 QHBoxLayout *hbox = new QHBoxLayout;
86 hbox->addWidget(l);
87 hbox->addWidget(*c);
89 QVBoxLayout *vbox = new QVBoxLayout;
90 vbox->addLayout(hbox);
91 vbox->addWidget(*e);
93 return vbox;
96 typedef enum
98 Recommended = 0x1,
99 SpecF = 0x2,
100 SpecU = 0x4,
101 SpecM = 0x8
102 } TooltipType;
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);
123 if (t & SpecF)
124 s += "\n" + NSettingsCommands::tr(str[1]);
126 if (t & SpecU)
127 s += "\n" + NSettingsCommands::tr(str[2]);
129 if (t & SpecM)
130 s += "\n" + NSettingsCommands::tr(str[3]);
132 return s;
135 NSettingsCommands::NSettingsCommands(QWidget *parent/*=NULL*/)
136 : NSettingsWidget(parent)
139 // Parse media stream URLs using
141 QVBoxLayout *parseUsingBox =
142 newVBox(this,
143 tr("Pa&rse media stream URLs using"),
144 &parseUsingCombo,
145 &parseUsingEdit,
146 SLOT(parseUsingChanged(QString)),
147 tooltip(SpecU, "quvi --category-http %u")
149 detect_type(MediaParser, parseUsingCombo);
151 // Download using
153 QVBoxLayout *downloadUsingBox =
154 newVBox(this,
155 tr("Do&wnload using"),
156 &downloadUsingCombo,
157 &downloadUsingEdit,
158 SLOT(downloadUsingChanged(QString)),
159 tooltip(SpecU|SpecF,
160 "curl -L -C - -o %f %u --user-agent Mozilla/5.0")
162 detect_type(Downloader, downloadUsingCombo);
164 // Play using
166 QVBoxLayout *playUsingBox =
167 newVBox(this,
168 tr("Pl&ay using"),
169 &playUsingCombo,
170 &playUsingEdit,
171 SLOT(playUsingChanged(QString)),
172 tooltip(SpecM, "xdg-open %m")
174 detect_type(MediaPlayer, playUsingCombo);
176 // Feed using
178 QVBoxLayout *feedUsingBox =
179 newVBox(this,
180 tr("Read &YouTube feeds using"),
181 &feedUsingCombo,
182 &feedUsingEdit,
183 SLOT(feedUsingChanged(QString)),
184 tooltip(Recommended, "umph")
186 detect_type(FeedParser, feedUsingCombo);
188 // Layout
190 QVBoxLayout *box = new QVBoxLayout;
191 box->addLayout(parseUsingBox);
192 box->addLayout(downloadUsingBox);
193 box->addLayout(playUsingBox);
194 box->addLayout(feedUsingBox);
195 box->addStretch(0);
196 setLayout(box);
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");
211 edit->setFocus();
213 return msg.isEmpty();
216 bool NSettingsCommands::verify(QString& msg)
218 bool r = _verify(parseUsingCombo, parseUsingEdit, msg);
219 if (r)
220 r = _verify(playUsingCombo, playUsingEdit, msg);
221 if (r)
222 r = _verify(downloadUsingCombo, downloadUsingEdit, msg);
223 if (r)
224 r = _verify(feedUsingCombo, feedUsingEdit, msg);
225 return r;
228 static void _write(SettingKey comboKey,
229 QComboBox *combo,
230 SettingKey editKey,
231 QLineEdit *edit)
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"))
241 ct = "other";
243 settings.setValue(comboKey, QString("%1%2%3")
244 .arg(ct)
245 .arg(NSETTINGS_CMDPATH_SEPARATOR)
246 .arg(v.toString()));
247 settings.setValue(editKey, edit->text());
250 void NSettingsCommands::write()
252 // Parse using
253 _write(ParseUsing, parseUsingCombo,
254 ParseUsingOther, parseUsingEdit);
255 // Download using
256 _write(DownloadUsing, downloadUsingCombo,
257 DownloadUsingOther, downloadUsingEdit);
258 // Play using
259 _write(PlayUsing, playUsingCombo,
260 PlayUsingOther, playUsingEdit);
261 // Feed using
262 _write(FeedUsing, feedUsingCombo,
263 FeedUsingOther, feedUsingEdit);
266 static void _read(SettingKey comboKey,
267 QComboBox *combo,
268 SettingKey editKey,
269 QLineEdit *edit)
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.
277 if (a[0] == "other")
278 a[0] = qApp->translate("nn::NSettingsCommands", "Other");
280 const int n = combo->findText(a[0]);
281 if (n != -1)
283 #ifdef ENABLE_VERBOSE
284 qDebug() << __PRETTY_FUNCTION__ << __LINE__
285 << n
286 << a[0]
287 << a[1];
288 #endif
289 combo->setItemData(n, a[1]);
290 combo->setCurrentIndex(n);
292 #ifdef _1
293 else
295 if (!text.isEmpty())
297 qWarning() << __PRETTY_FUNCTION__ << __LINE__
298 << text
299 << "not found in combobox values";
302 #endif // _1
303 text = settings.value(editKey).toString();
304 edit->setText(text);
307 void NSettingsCommands::read()
309 // Parse using
310 _read(ParseUsing, parseUsingCombo,
311 ParseUsingOther, parseUsingEdit);
312 // Download using
313 _read(DownloadUsing, downloadUsingCombo,
314 DownloadUsingOther, downloadUsingEdit);
315 // Play using
316 _read(PlayUsing, playUsingCombo,
317 PlayUsingOther, playUsingEdit);
318 // Feed using
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"));
343 } // namespace nn
345 /* vim: set ts=2 sw=2 tw=72 expandtab: */