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>
24 #include "Preferences.h"
26 #define QSETTINGS_GROUP "Preferences"
30 extern SharedPreferences shPrefs
;
34 Preferences::Preferences (QWidget
*parent
)
35 : QDialog (parent
), _restartAfter (false)
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);
78 shPrefs
.get (SharedPreferences::Regexp
).toString ());
80 if (regexpEdit
->text ().isEmpty ())
83 minToTrayBox
->setChecked (
84 shPrefs
.get (SharedPreferences::MinToTray
).toBool ()
89 stayOnTopBox
->setChecked (
90 shPrefs
.get (SharedPreferences::StayOnTop
).toBool ()
95 playWhenDoneBox
->setChecked (
96 shPrefs
.get (SharedPreferences::PlayWhenDone
).toBool ()
101 showReminderBox
->setChecked (
102 shPrefs
.get (SharedPreferences::ShowReminder
).toBool ()
107 dontPromptFilenameBox
->setChecked (
108 shPrefs
.get (SharedPreferences::DontPromptFilename
).toBool ()
116 bool Preferences::restartAfter () const { return _restartAfter
; }
119 browse (QWidget
*p
, QLineEdit
*w
) {
122 QFileDialog::getOpenFileName (p
, QObject::tr ("Browse"));
128 // Slot: browse player.
131 Preferences::onBrowsePlayer ()
132 { browse (this, playerPathEdit
); }
134 // Slot: browse quvi.
137 Preferences::onBrowseQuvi ()
138 { browse (this, quviPathEdit
); }
140 // Slot: browse curl.
143 Preferences::onBrowseCurl()
144 { browse (this, curlPathEdit
); }
146 // Slot: browse umph.
149 Preferences::onBrowseUmph ()
150 { browse (this, umphPathEdit
); }
152 // Slot: browse save dir.
155 Preferences::onBrowseSaveDir () {
157 const QString dir
= QFileDialog::getExistingDirectory (
159 tr ("Open directory"),
161 QFileDialog::ShowDirsOnly
162 | QFileDialog::DontResolveSymlinks
166 saveDirEdit
->setText (dir
);
170 // Slot: filename format (current index) changed.
173 Preferences::onFilenameFormatChanged (int n
) {
175 static const QString fmts
[] = {
182 sizeof (fmts
) / sizeof (QString
);
187 filenameFormatEdit
->setText (fmts
[n
]);
192 // Slot: regexp (current index) changed.
195 Preferences::onRegexpChanged (int n
) {
197 static const QString regexps
[] = {
198 "/(\\w|\\s)/g", // Default
207 sizeof (regexps
) / sizeof (QString
);
212 regexpEdit
->setText (regexps
[n
]);
217 // Slot: filename format editing finished.
220 Preferences::onFilenameFormatEditingFinished ()
224 // Slot: regexp editing finished.
227 Preferences::onRegexpEditingFinished ()
230 // Slot: refresh clicked.
233 Preferences::onRefresh () {
235 QString title
= "Foo bar";
236 QString host
= "baz";
238 QString suffix
= "flv";
239 QString preview
= filenameFormatEdit
->text ().simplified ();
241 const bool ok
= NomNom::format_filename (
243 regexpEdit
->text ().simplified (),
252 previewEdit
->setText (preview
);
258 extern History history
;
260 // Slot: clear history.
263 Preferences::onClearHistory ()
264 { history
.clear (); }
269 Preferences::onLanguage () {
273 if (!NomNom::choose_lang (this, langName
))
277 s
.setValue ("language", langName
);
279 if (NomNom::ask (this,
280 tr ("The language will be changed after you restart NomNom. "
287 _restartAfter
= true;
289 done (QDialog::Accepted
);
293 append (QLineEdit
*w
, const QStringList
& l
= QStringList() << "%u") {
295 QString s
= w
->text ();
300 foreach (QString w
, l
) {
308 // Done. QDialog and closeEvent design glitch workaround.
311 Preferences::done (int r
) {
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
,
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 ());
376 Preferences::closeEvent (QCloseEvent
*e
)
377 { QDialog::closeEvent(e
); }
379 #undef QSETTINGS_GROUP
380 #define QSETTINGS_GROUP "SharedPreferences"
382 // SharedPreferences: write.
385 SharedPreferences::write () const {
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
);
406 // SharedPreferences: read.
409 SharedPreferences::read () {
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 ();
430 // SharedPreferences: set.
433 SharedPreferences::set (Option opt
, const QVariant
& v
) {
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;
449 << __PRETTY_FUNCTION__
451 << "invalid option id"
457 // SharedPreferences: get.
460 SharedPreferences::get (Option opt
) const {
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
);
476 << __PRETTY_FUNCTION__
478 << "invalid option id"