update todo.
[abby.git] / src / prefsdlg.cpp
blob71179df26db91c8e4d1452e1c7b1ddf241a124f6
1 /*
2 * abby Copyright (C) 2009 Toni Gundogdu.
3 * This file is part of abby.
5 * abby is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * abby is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <QDialog>
19 #include <QSettings>
20 #include <QFileDialog>
21 #include <QTranslator>
22 #include <QMessageBox>
24 #include "prefsdlg.h"
25 #include "util.h"
27 PreferencesDialog::PreferencesDialog(QWidget *parent)
28 : QDialog(parent)
30 setupUi(this);
31 readSettings();
33 langCombo->addItem("English");
35 int sel = 0;
36 qmFiles = findQmFiles();
37 typedef unsigned int _uint;
38 const register _uint size = qmFiles.size();
39 for (register _uint i=0; i<size; ++i) {
40 langCombo->addItem(langName(qmFiles[i]));
41 if (qmFile == qmFiles[i])
42 sel = i+1;
44 langCombo->setCurrentIndex(sel);
46 connect(langCombo, SIGNAL(currentIndexChanged(int)),
47 this, SLOT(onLangChanged(int)));
49 #ifdef WIN32
50 streamLabel ->setHidden(true);
51 streamEdit ->setHidden(true);
52 streamButton->setHidden(true);
53 #endif
55 langLabel->setHidden(true);
56 langCombo->setHidden(true);
59 void
60 PreferencesDialog::onFinished(int) {
61 writeSettings();
64 void
65 PreferencesDialog::writeSettings() {
66 QSettings s;
68 s.beginGroup("PreferencesDialog");
70 s.setValue("size", size());
72 s.setValue("savedirEdit", savedirEdit->text());
73 s.setValue("streamEdit", streamEdit->text());
74 s.setValue("commandEdit", commandEdit->text());
75 s.setValue("ccliveEdit", ccliveEdit->text());
77 s.setValue("proxyCombo", proxyCombo->currentIndex());
78 s.setValue("proxyEdit", proxyEdit->text());
79 s.setValue("limitBox", limitBox->checkState());
80 s.setValue("limitSpin", limitSpin->value());
81 s.setValue("timeoutBox", timeoutBox->checkState());
82 s.setValue("timeoutSpin", timeoutSpin->value());
83 s.setValue("socksBox", socksBox->checkState());
85 s.setValue("qmFile", qmFile);
87 s.setValue("mintrayBox", mintrayBox->checkState());
89 s.endGroup();
92 void
93 PreferencesDialog::readSettings() {
94 QSettings s;
96 s.beginGroup("PreferencesDialog");
98 resize( s.value("size", QSize(525,205)).toSize() );
100 savedirEdit->setText( s.value("savedirEdit").toString() );
101 streamEdit->setText( s.value("streamEdit").toString() );
102 commandEdit->setText( s.value("commandEdit").toString() );
103 ccliveEdit->setText( s.value("ccliveEdit").toString() );
105 proxyCombo->setCurrentIndex( s.value("proxyCombo").toInt() );
106 proxyEdit->setText( s.value("proxyEdit").toString() );
107 limitBox->setCheckState(
108 s.value("limitBox").toBool()
109 ? Qt::Checked
110 : Qt::Unchecked);
111 limitSpin->setValue( s.value("limitSpin").toInt() );
112 timeoutBox->setCheckState(
113 s.value("timeoutBox").toBool()
114 ? Qt::Checked
115 : Qt::Unchecked);
116 timeoutSpin->setValue( s.value("timeoutSpin").toInt() );
117 socksBox->setCheckState(
118 s.value("socksBox").toBool()
119 ? Qt::Checked
120 : Qt::Unchecked);
122 qmFile = s.value("qmFile").toString();
124 mintrayBox->setCheckState(
125 s.value("mintrayBox").toBool()
126 ? Qt::Checked
127 : Qt::Unchecked
130 s.endGroup();
133 QStringList
134 PreferencesDialog::findQmFiles() {
135 QDir dir(":/i18n");
137 QStringList fnames =
138 dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
140 QMutableStringListIterator iter(fnames);
141 while (iter.hasNext()) {
142 iter.next();
143 iter.setValue(dir.filePath(iter.value()));
145 return fnames;
148 QString
149 PreferencesDialog::langName(const QString& qmFile) {
150 QTranslator transl;
151 transl.load(qmFile);
152 return transl.translate("MainWindow","English");
156 // Slots
158 void
159 PreferencesDialog::onProxyChanged(int index) {
160 proxyEdit->setEnabled(index == 1);
163 void
164 PreferencesDialog::onLimitStateChanged(int state) {
165 limitSpin->setEnabled(state != 0);
168 void
169 PreferencesDialog::onBrowseSaveDir() {
170 QString path =
171 QFileDialog::getExistingDirectory(
172 this,
173 tr("Directory for saved videos"),
174 QDir::currentPath()
177 if (!path.isEmpty())
178 savedirEdit->setText(path);
181 void
182 PreferencesDialog::onBrowseStreamCommand() {
183 QString fname = QFileDialog::getOpenFileName(this,tr("Choose command"));
184 if (!fname.isEmpty())
185 streamEdit->setText(fname);
188 void
189 PreferencesDialog::onBrowseCommand() {
190 QString fname = QFileDialog::getOpenFileName(this,tr("Choose command"));
191 if (!fname.isEmpty())
192 commandEdit->setText(fname);
195 void
196 PreferencesDialog::onBrowseCclive() {
197 QString fname = QFileDialog::getOpenFileName(this,tr("Choose cclive"));
198 if (!fname.isEmpty())
199 ccliveEdit->setText(fname);
202 static void
203 dump_detection_results(
204 QWidget *parent,
205 const QString& path,
206 const QString& version,
207 const QString& libVersion,
208 const QString& libName,
209 const bool isCcliveFlag,
210 const bool showPathFlag=false)
212 QString msg = QString(
213 QObject::tr("Detected: %1 version %2 with %3 %4")
214 .arg(isCcliveFlag ? "cclive" : "clive")
215 .arg(version)
216 .arg(libName)
217 .arg(libVersion)
220 if (showPathFlag)
221 msg += QString(QObject::tr("\nPath: %1")).arg(path);
223 QMessageBox::information(
224 parent,
225 QCoreApplication::applicationName(),
230 void
231 PreferencesDialog::onVerifyCclive() {
232 try {
233 QString path, version, libVersion, libName;
234 bool isCcliveFlag;
236 path = ccliveEdit->text();
238 Util::verifyCclivePath(
239 path,
240 version,
241 libVersion,
242 libName,
243 &isCcliveFlag
246 dump_detection_results(
247 this,
248 path,
249 version,
250 libVersion,
251 libName,
252 isCcliveFlag
255 catch (const NoCcliveException& x) {
256 QMessageBox::warning(this, tr("Warning"), x.what());
260 void
261 PreferencesDialog::onAutodetectCclive() {
262 try {
263 QString path, version, libVersion, libName;
264 bool isCcliveFlag = false;
266 Util::detectCclive(
267 path,
268 version,
269 libVersion,
270 libName,
271 &isCcliveFlag
274 ccliveEdit->setText(path);
276 dump_detection_results(
277 this,
278 path,
279 version,
280 libVersion,
281 libName,
282 isCcliveFlag,
283 true
286 catch (const NoCcliveException& x) {
287 QMessageBox::warning(this, tr("Warning"), x.what());
291 void
292 PreferencesDialog::onLangChanged(int index) {
293 qmFile = "";
294 if (index > 0)
295 qmFile = qmFiles[index-1];
297 QMessageBox::information(this, QCoreApplication::applicationName(),
298 tr("You need to restart the application for the language change "
299 "to take effect."));
302 void
303 PreferencesDialog::onTimeoutStateChanged(int state) {
304 timeoutSpin->setEnabled(state != 0);
305 socksBox->setEnabled(state != 0);