httpmgr: fix: emit finished if != http/200.
[abby.git] / src / prefsdlg.cpp
blob8f4f642a8e85dd733f770d302307d8ea728d11de
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.endGroup();
90 void
91 PreferencesDialog::readSettings() {
92 QSettings s;
94 s.beginGroup("PreferencesDialog");
96 resize( s.value("size", QSize(525,205)).toSize() );
98 savedirEdit->setText( s.value("savedirEdit").toString() );
99 streamEdit->setText( s.value("streamEdit").toString() );
100 commandEdit->setText( s.value("commandEdit").toString() );
101 ccliveEdit->setText( s.value("ccliveEdit").toString() );
103 proxyCombo->setCurrentIndex( s.value("proxyCombo").toInt() );
104 proxyEdit->setText( s.value("proxyEdit").toString() );
105 limitBox->setCheckState(
106 s.value("limitBox").toBool()
107 ? Qt::Checked
108 : Qt::Unchecked);
109 limitSpin->setValue( s.value("limitSpin").toInt() );
110 timeoutBox->setCheckState(
111 s.value("timeoutBox").toBool()
112 ? Qt::Checked
113 : Qt::Unchecked);
114 timeoutSpin->setValue( s.value("timeoutSpin").toInt() );
115 socksBox->setCheckState(
116 s.value("socksBox").toBool()
117 ? Qt::Checked
118 : Qt::Unchecked);
120 qmFile = s.value("qmFile").toString();
122 s.endGroup();
125 QStringList
126 PreferencesDialog::findQmFiles() {
127 QDir dir(":/i18n");
129 QStringList fnames =
130 dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
132 QMutableStringListIterator iter(fnames);
133 while (iter.hasNext()) {
134 iter.next();
135 iter.setValue(dir.filePath(iter.value()));
137 return fnames;
140 QString
141 PreferencesDialog::langName(const QString& qmFile) {
142 QTranslator transl;
143 transl.load(qmFile);
144 return transl.translate("MainWindow","English");
148 // Slots
150 void
151 PreferencesDialog::onProxyChanged(int index) {
152 proxyEdit->setEnabled(index == 1);
155 void
156 PreferencesDialog::onLimitStateChanged(int state) {
157 limitSpin->setEnabled(state != 0);
160 void
161 PreferencesDialog::onBrowseSaveDir() {
162 QString path =
163 QFileDialog::getExistingDirectory(
164 this,
165 tr("Directory for saved videos"),
166 QDir::currentPath()
169 if (!path.isEmpty())
170 savedirEdit->setText(path);
173 void
174 PreferencesDialog::onBrowseStreamCommand() {
175 QString fname = QFileDialog::getOpenFileName(this,tr("Choose command"));
176 if (!fname.isEmpty())
177 streamEdit->setText(fname);
180 void
181 PreferencesDialog::onBrowseCommand() {
182 QString fname = QFileDialog::getOpenFileName(this,tr("Choose command"));
183 if (!fname.isEmpty())
184 commandEdit->setText(fname);
187 void
188 PreferencesDialog::onBrowseCclive() {
189 QString fname = QFileDialog::getOpenFileName(this,tr("Choose cclive"));
190 if (!fname.isEmpty())
191 ccliveEdit->setText(fname);
194 static void
195 dump_detection_results(
196 QWidget *parent,
197 const QString& path,
198 const QString& version,
199 const QString& libVersion,
200 const QString& libName,
201 const bool isCcliveFlag,
202 const bool showPathFlag=false)
204 QString msg = QString(
205 QObject::tr("Detected: %1 version %2 with %3 %4")
206 .arg(isCcliveFlag ? "cclive" : "clive")
207 .arg(version)
208 .arg(libName)
209 .arg(libVersion)
212 if (showPathFlag)
213 msg += QString(QObject::tr("\nPath: %1")).arg(path);
215 QMessageBox::information(
216 parent,
217 QCoreApplication::applicationName(),
222 void
223 PreferencesDialog::onVerifyCclive() {
224 try {
225 QString path, version, libVersion, libName;
226 bool isCcliveFlag;
228 path = ccliveEdit->text();
230 Util::verifyCclivePath(
231 path,
232 version,
233 libVersion,
234 libName,
235 &isCcliveFlag
238 dump_detection_results(
239 this,
240 path,
241 version,
242 libVersion,
243 libName,
244 isCcliveFlag
247 catch (const NoCcliveException& x) {
248 QMessageBox::warning(this, tr("Warning"), x.what());
252 void
253 PreferencesDialog::onAutodetectCclive() {
254 try {
255 QString path, version, libVersion, libName;
256 bool isCcliveFlag = false;
258 Util::detectCclive(
259 path,
260 version,
261 libVersion,
262 libName,
263 &isCcliveFlag
266 ccliveEdit->setText(path);
268 dump_detection_results(
269 this,
270 path,
271 version,
272 libVersion,
273 libName,
274 isCcliveFlag,
275 true
278 catch (const NoCcliveException& x) {
279 QMessageBox::warning(this, tr("Warning"), x.what());
283 void
284 PreferencesDialog::onLangChanged(int index) {
285 qmFile = "";
286 if (index > 0)
287 qmFile = qmFiles[index-1];
289 QMessageBox::information(this, QCoreApplication::applicationName(),
290 tr("You need to restart the application for the language change "
291 "to take effect."));
294 void
295 PreferencesDialog::onTimeoutStateChanged(int state) {
296 timeoutSpin->setEnabled(state != 0);
297 socksBox->setEnabled(state != 0);