additional umph specifiers
[nomnom.git] / src / i / Preferences.cpp
bloba3829b3d04f5a1cbb732985f0eb096f4d4c8aa3a
1 /*
2 * Copyright (C) 2010 Toni Gundogdu.
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 <QFileDialog>
19 #include <QSettings>
20 #include <QDebug>
22 #include "util.h"
23 #include "History.h"
24 #include "Preferences.h"
26 #define QSETTINGS_GROUP "Preferences"
28 // main.cpp
30 extern SharedPreferences shPrefs;
32 // Ctor.
34 Preferences::Preferences (QWidget *parent)
35 : QDialog (parent), _restartAfter (false)
37 setupUi (this);
39 QSettings s;
40 NomNom::restore_size (s, this, QSETTINGS_GROUP, QSize (600,200));
42 configEdit->setText (s.fileName ());
44 quviPathEdit->setText (
45 shPrefs.get (SharedPreferences::QuviPath).toString ());
47 if (quviPathEdit->text ().isEmpty ())
48 quviPathEdit->setText ("quvi %u");
50 curlPathEdit->setText (
51 shPrefs.get (SharedPreferences::CurlPath).toString ());
53 if (curlPathEdit->text ().isEmpty ())
54 curlPathEdit->setText ("curl -L -C - -o %f %u");
56 playerPathEdit->setText (
57 shPrefs.get (SharedPreferences::PlayerPath).toString ());
59 if (playerPathEdit->text ().isEmpty ())
60 playerPathEdit->setText ("vlc --quiet %u");
62 umphPathEdit->setText (
63 shPrefs.get (SharedPreferences::UmphPath).toString ());
65 if (umphPathEdit->text ().isEmpty ())
66 umphPathEdit->setText ("umph -q --json -t %t -s %s -m %m %i");
68 saveDirEdit->setText (
69 shPrefs.get (SharedPreferences::SaveDir).toString ());
71 filenameFormatEdit->setText (
72 shPrefs.get (SharedPreferences::FilenameFormat).toString ());
74 if (filenameFormatEdit->text ().isEmpty ())
75 onFilenameFormatChanged (0);
77 regexpEdit->setText (
78 shPrefs.get (SharedPreferences::Regexp).toString ());
80 if (regexpEdit->text ().isEmpty ())
81 onRegexpChanged (0);
83 minToTrayBox->setChecked (
84 shPrefs.get (SharedPreferences::MinToTray).toBool ()
85 ? Qt::Checked
86 : Qt::Unchecked
89 stayOnTopBox->setChecked (
90 shPrefs.get (SharedPreferences::StayOnTop).toBool ()
91 ? Qt::Checked
92 : Qt::Unchecked
95 playWhenDoneBox->setChecked (
96 shPrefs.get (SharedPreferences::PlayWhenDone).toBool ()
97 ? Qt::Checked
98 : Qt::Unchecked
101 showReminderBox->setChecked (
102 shPrefs.get (SharedPreferences::ShowReminder).toBool ()
103 ? Qt::Checked
104 : Qt::Unchecked
107 dontPromptFilenameBox->setChecked (
108 shPrefs.get (SharedPreferences::DontPromptFilename).toBool ()
109 ? Qt::Checked
110 : Qt::Unchecked
113 onRefresh ();
116 bool Preferences::restartAfter () const { return _restartAfter; }
118 static void
119 browse (QWidget *p, QLineEdit *w) {
121 const QString s =
122 QFileDialog::getOpenFileName (p, QObject::tr ("Browse"));
124 if (!s.isEmpty ())
125 w->setText (s);
128 // Slot: browse player.
130 void
131 Preferences::onBrowsePlayer ()
132 { browse (this, playerPathEdit); }
134 // Slot: browse quvi.
136 void
137 Preferences::onBrowseQuvi ()
138 { browse (this, quviPathEdit); }
140 // Slot: browse curl.
142 void
143 Preferences::onBrowseCurl()
144 { browse (this, curlPathEdit); }
146 // Slot: browse umph.
148 void
149 Preferences::onBrowseUmph ()
150 { browse (this, umphPathEdit); }
152 // Slot: browse save dir.
154 void
155 Preferences::onBrowseSaveDir () {
157 const QString dir = QFileDialog::getExistingDirectory (
158 this,
159 tr ("Open directory"),
160 QDir::homePath (),
161 QFileDialog::ShowDirsOnly
162 | QFileDialog::DontResolveSymlinks
165 if (!dir.isEmpty ())
166 saveDirEdit->setText (dir);
170 // Slot: filename format (current index) changed.
172 void
173 Preferences::onFilenameFormatChanged (int n) {
175 static const QString fmts[] = {
176 "%t.%s", // Default
177 "%t %i.%s",
178 "%t %i %h.%s"
181 static const int m =
182 sizeof (fmts) / sizeof (QString);
184 if (n > m)
185 n = 0;
187 filenameFormatEdit->setText (fmts[n]);
189 onRefresh ();
192 // Slot: regexp (current index) changed.
194 void
195 Preferences::onRegexpChanged (int n) {
197 static const QString regexps[] = {
198 "/(\\w|\\s)/g", // Default
199 "/(\\w)/g",
200 "/(\\d)/g",
201 "/(\\W)/g",
202 "/(\\D)/g",
203 "/(\\S)/g"
206 static const int m =
207 sizeof (regexps) / sizeof (QString);
209 if (n > m)
210 n = 0;
212 regexpEdit->setText (regexps[n]);
214 onRefresh ();
217 // Slot: filename format editing finished.
219 void
220 Preferences::onFilenameFormatEditingFinished ()
221 { onRefresh (); }
224 // Slot: regexp editing finished.
226 void
227 Preferences::onRegexpEditingFinished ()
228 { onRefresh (); }
230 // Slot: refresh clicked.
232 void
233 Preferences::onRefresh () {
235 QString title = "Foo bar";
236 QString host = "baz";
237 QString id = "1234";
238 QString suffix = "flv";
239 QString preview = filenameFormatEdit->text ().simplified ();
241 const bool ok = NomNom::format_filename (
242 this,
243 regexpEdit->text ().simplified (),
244 title,
245 suffix,
246 host,
248 preview
251 if (ok)
252 previewEdit->setText (preview);
256 // main.cpp
258 extern History history;
260 // Slot: clear history.
262 void
263 Preferences::onClearHistory ()
264 { history.clear (); }
266 // Slot: language.
268 void
269 Preferences::onLanguage () {
271 QString langName;
273 if (!NomNom::choose_lang (this, langName))
274 return;
276 QSettings s;
277 s.setValue ("language", langName);
279 if (NomNom::ask (this,
280 tr ("The language will be changed after you restart NomNom. "
281 "Restart NomNom?"))
282 == QMessageBox::No)
284 return;
287 _restartAfter = true;
289 done (QDialog::Accepted);
292 static QString
293 append (QLineEdit *w, const QStringList& l = QStringList() << "%u") {
295 QString s = w->text ();
297 if (s.isEmpty ())
298 return QString ();
300 foreach (QString w, l) {
301 if (!s.contains (w))
302 s += " " + w;
305 return s;
308 // Done. QDialog and closeEvent design glitch workaround.
310 void
311 Preferences::done (int r) {
313 QSettings s;
314 NomNom::save_size (s, this, QSETTINGS_GROUP);
316 if (r == QDialog::Accepted) {
318 shPrefs.set (SharedPreferences::QuviPath,
319 append (quviPathEdit));
321 shPrefs.set (SharedPreferences::CurlPath,
322 curlPathEdit->text ());
324 shPrefs.set (SharedPreferences::PlayerPath,
325 append (playerPathEdit));
327 shPrefs.set (SharedPreferences::UmphPath,
328 append (umphPathEdit,
329 QStringList ()
330 << "-q"
331 << "--json"
332 << "-t"
333 << "%t"
334 << "-s"
335 << "%s"
336 << "-m"
337 << "%m"
338 << "%i"
342 shPrefs.set (SharedPreferences::SaveDir,
343 saveDirEdit->text ());
345 shPrefs.set (SharedPreferences::FilenameFormat,
346 filenameFormatEdit->text ());
348 shPrefs.set (SharedPreferences::Regexp,
349 regexpEdit->text ());
351 shPrefs.set (SharedPreferences::MinToTray,
352 minToTrayBox->isChecked ());
354 shPrefs.set (SharedPreferences::StayOnTop,
355 stayOnTopBox->isChecked ());
357 shPrefs.set (SharedPreferences::PlayWhenDone,
358 playWhenDoneBox->isChecked ());
360 shPrefs.set (SharedPreferences::ShowReminder,
361 showReminderBox->isChecked ());
363 shPrefs.set (SharedPreferences::DontPromptFilename,
364 dontPromptFilenameBox->isChecked ());
366 shPrefs.write ();
369 QDialog::done (r);
370 close ();
373 // Close.
375 void
376 Preferences::closeEvent (QCloseEvent *e)
377 { QDialog::closeEvent(e); }
379 #undef QSETTINGS_GROUP
380 #define QSETTINGS_GROUP "SharedPreferences"
382 // SharedPreferences: write.
384 void
385 SharedPreferences::write () const {
387 QSettings s;
388 s.beginGroup (QSETTINGS_GROUP);
390 s.setValue ("quviPath", quviPath);
391 s.setValue ("curlPath", curlPath);
392 s.setValue ("playerPath", playerPath);
393 s.setValue ("umphPath", umphPath);
394 s.setValue ("saveDir", saveDir);
395 s.setValue ("filenameFormat", filenameFormat);
396 s.setValue ("regexp", regexp);
397 s.setValue ("minToTray", minToTray);
398 s.setValue ("stayOnTop", stayOnTop);
399 s.setValue ("playWhenDone", playWhenDone);
400 s.setValue ("showReminder", showReminder);
401 s.setValue ("dontPromptFilename", dontPromptFilename);
403 s.endGroup ();
406 // SharedPreferences: read.
408 void
409 SharedPreferences::read () {
411 QSettings s;
412 s.beginGroup (QSETTINGS_GROUP);
414 quviPath = s.value ("quviPath").toString ();
415 curlPath = s.value ("curlPath").toString ();
416 playerPath = s.value ("playerPath").toString ();
417 umphPath = s.value ("umphPath").toString ();
418 saveDir = s.value ("saveDir").toString ();
419 filenameFormat = s.value ("filenameFormat").toString ();
420 regexp = s.value ("regexp").toString ();
421 minToTray = s.value ("minToTray").toBool ();
422 stayOnTop = s.value ("stayOnTop").toBool ();
423 playWhenDone = s.value ("playWhenDone").toBool ();
424 showReminder = s.value ("showReminder").toBool ();
425 dontPromptFilename = s.value ("dontPromptFilename").toBool ();
427 s.endGroup ();
430 // SharedPreferences: set.
432 void
433 SharedPreferences::set (Option opt, const QVariant& v) {
434 switch (opt) {
435 case QuviPath : quviPath = v.toString (); break;
436 case CurlPath : curlPath = v.toString (); break;
437 case PlayerPath : playerPath = v.toString (); break;
438 case UmphPath : umphPath = v.toString (); break;
439 case SaveDir : saveDir = v.toString (); break;
440 case FilenameFormat : filenameFormat = v.toString (); break;
441 case Regexp : regexp = v.toString (); break;
442 case MinToTray : minToTray = v.toBool (); break;
443 case StayOnTop : stayOnTop = v.toBool (); break;
444 case PlayWhenDone : playWhenDone = v.toBool (); break;
445 case ShowReminder : showReminder = v.toBool (); break;
446 case DontPromptFilename: dontPromptFilename = v.toBool (); break;
447 default:
448 qDebug()
449 << __PRETTY_FUNCTION__
450 << __LINE__
451 << "invalid option id"
452 << opt;
453 break;
457 // SharedPreferences: get.
459 QVariant
460 SharedPreferences::get (Option opt) const {
461 switch (opt) {
462 case QuviPath : return QVariant (quviPath);
463 case CurlPath : return QVariant (curlPath);
464 case PlayerPath : return QVariant (playerPath);
465 case UmphPath : return QVariant (umphPath);
466 case SaveDir : return QVariant (saveDir);
467 case FilenameFormat:return QVariant (filenameFormat);
468 case Regexp : return QVariant (regexp);
469 case MinToTray : return QVariant (minToTray);
470 case StayOnTop : return QVariant (stayOnTop);
471 case PlayWhenDone : return QVariant (playWhenDone);
472 case ShowReminder : return QVariant (showReminder);
473 case DontPromptFilename:return QVariant (dontPromptFilename);
474 default:
475 qDebug()
476 << __PRETTY_FUNCTION__
477 << __LINE__
478 << "invalid option id"
479 << opt;
480 break;
483 return QVariant ();