Update Finnish translation
[nomnom.git] / src / settings / nsettingsdialog_download.cpp
blob95d5e7d6558fa3336cd936a4894de3fad2e0df5e
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 <QFileDialog>
22 #include <QGridLayout>
23 #include <QVBoxLayout>
24 #include <QHBoxLayout>
25 #include <QComboBox>
26 #include <QLineEdit>
27 #include <QGroupBox>
28 #include <QLabel>
30 #include <NSettingsMutator>
31 #include <NSettingsDialog>
32 #include <NTripwireEdit>
34 extern nn::NSettingsMutator settings; // main.cpp
36 namespace nn
39 NSettingsDownload::NSettingsDownload(QWidget *parent/*=NULL*/)
40 : NSettingsWidget(parent),
41 fnfmtCombo(NULL),
42 fnfmtEdit(NULL),
43 regexpCombo(NULL),
44 regexpEdit(NULL),
45 exampleEdit(NULL),
46 savedirEdit(NULL)
49 // Save as
51 QGroupBox *fngBox = new QGroupBox(tr("Save downloaded media as"));
53 QGridLayout *g = new QGridLayout;
55 addGridRow(g, tr("&Filename format:"),
56 &fnfmtCombo, &fnfmtEdit, SLOT(fnfmtChanged(int)));
58 addGridRow(g, tr("&Regular expression:"),
59 &regexpCombo, &regexpEdit, SLOT(regexpChanged(int)));
61 addGridRow(g, tr("Example:"), NULL, &exampleEdit, NULL);
63 fngBox->setLayout(g);
65 fnfmtCombo->addItem(tr("Default"), "%t.%s");
66 fnfmtCombo->addItem(tr("With media ID"), "%t_%i.%s");
67 fnfmtCombo->addItem(tr("With ID, domain"), "%t_%i_%d.%s");
69 regexpCombo->addItem(tr("Default"), "/(\\w|\\s)/g");
70 regexpCombo->addItem(tr("Word characters"), "/(\\w)/g");
71 regexpCombo->addItem(tr("Digit characters"), "/(\\d)/g");
73 exampleEdit->setReadOnly(true);
75 // Save in
77 savedirEdit = new NTripwireEdit;
78 savedirEdit->setReadOnly(true);
80 connect(savedirEdit, SIGNAL(entered()), this, SLOT(browseDir()));
82 QHBoxLayout *dirBox = new QHBoxLayout;
83 dirBox->addWidget(savedirEdit);
84 dirBox->addStretch(0);
86 QGroupBox *dirgBox = new QGroupBox(tr("&Save downloaded media in"));
87 dirgBox->setLayout(dirBox);
89 // Layout
91 QVBoxLayout *box = new QVBoxLayout;
92 box->addWidget(fngBox);
93 box->addWidget(dirgBox);
94 box->addStretch(0);
95 setLayout(box);
98 void NSettingsDownload::init()
100 if (fnfmtEdit->text().isEmpty())
101 fnfmtChanged(0);
103 if (regexpEdit->text().isEmpty())
104 regexpChanged(0);
106 updateExample();
108 if (savedirEdit->text().isEmpty())
109 savedirEdit->setText(QDir::homePath());
112 static bool _verify(QLineEdit *edit, QString& msg)
114 if (edit->text().isEmpty())
116 msg = qApp->translate("nn::NSettingsDownload",
117 "Please fill the required fields");
119 return msg.isEmpty();
122 bool NSettingsDownload::verify(QString& msg)
124 bool r = _verify(fnfmtEdit, msg);
125 if (r)
126 r = _verify(regexpEdit, msg);
127 return r;
130 static void _write(SettingKey k, QLineEdit *e)
132 settings.setValue(k, e->text());
135 void NSettingsDownload::write()
137 // Filename
138 _write(FilenameFormat, fnfmtEdit);
139 _write(FilenameRegExp, regexpEdit);
140 // Directory
141 _write(SaveMediaDirectory, savedirEdit);
144 static void _read(SettingKey key, QLineEdit *edit, QComboBox *combo)
146 const QString text = settings.value(key).toString();
148 if (combo)
150 const int n = combo->findData(text);
151 if (n != -1)
152 combo->setCurrentIndex(n);
155 edit->setText(text);
158 void NSettingsDownload::read()
160 // Filename
161 _read(FilenameFormat, fnfmtEdit, fnfmtCombo);
162 _read(FilenameRegExp, regexpEdit, regexpCombo);
163 // Directory
164 _read(SaveMediaDirectory, savedirEdit, NULL);
167 void NSettingsDownload::addGridRow(
168 QGridLayout *g,
169 const QString& text,
170 QComboBox **c,
171 QLineEdit **e,
172 const char *slot)
174 QLabel *l = new QLabel(text);
175 *e = new QLineEdit;
176 l->setBuddy(*e);
178 if (c)
179 *c = new QComboBox;
181 if (slot)
183 connect(*c, SIGNAL(currentIndexChanged(int)),
184 this, slot);
187 connect(*e, SIGNAL(editingFinished()),
188 this, SLOT(updateExample()));
190 const int row = g->rowCount() + 1;
192 g->addWidget(l, row, 0);
193 g->addWidget(*e, row, 1);
194 if (c)
195 g->addWidget(*c, row, 2);
198 void NSettingsDownload::fnfmtChanged(int n)
200 fnfmtEdit->setText(fnfmtCombo->itemData(n).toString());
201 updateExample();
204 void NSettingsDownload::regexpChanged(int n)
206 regexpEdit->setText(regexpCombo->itemData(n).toString());
207 updateExample();
210 static const QString e_title = "Foo bar";
211 static const QString e_domain = "baz";
212 static const QString e_media_id = "abcd1234";
213 static const QString e_suffix = "flv";
215 void NSettingsDownload::updateExample()
217 if (!fnfmtEdit || !regexpEdit || !exampleEdit)
218 return;
220 QString fname = fnfmtEdit->text();
221 const bool r = format_filename(regexpEdit->text(),
222 e_title,
223 e_media_id,
224 e_domain,
225 e_suffix,
226 fname);
227 if (!r)
228 return;
230 exampleEdit->setText(fname);
233 void NSettingsDownload::browseDir()
235 const QString dir = QFileDialog::getExistingDirectory(
236 this,
237 tr("Open directory"),
238 QDir::homePath(),
239 QFileDialog::ShowDirsOnly
240 | QFileDialog::DontResolveSymlinks);
241 if (!dir.isEmpty())
242 savedirEdit->setText(dir);
245 } // namespace nn
247 /* vim: set ts=2 sw=2 tw=72 expandtab: */