scan: add log view. fix scan pattern.
[abby.git] / src / formatdlg.cpp
blobbbf59eca2582149a163905dd2cf973930f41fafc
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 <QDebug>
22 #include "formatdlg.h"
24 /* The contents of this dialog changes dynamically based on
25 * c/clive --hosts output. abby tries to make switching
26 * between cclive and clive as transparent as possible as
27 * there may be differences in supported websites. */
29 /* Because QComboBox does not have setCurrentText we have to
30 * use two maps to keep track of the selections instead of
31 * only one. See sel and selN. The former stores selected
32 * "format ID", and the latter the matching QComboBox index
33 * to that ID (needed for setCurrentIndex, due to lack of
34 * setCurrentText). */
36 FormatDialog::FormatDialog(QWidget *parent)
37 : QDialog(parent)
39 setupUi(this);
40 readSettings();
43 void
44 FormatDialog::resetHosts() {
45 hostBox->clear();
48 void
49 FormatDialog::parseHosts(const QStringMap& hosts) {
50 this->hosts = hosts;
51 for (QStringMap::const_iterator iter = hosts.begin();
52 iter != hosts.end(); ++iter)
54 // Fill host combo with supported hosts.
55 hostBox->addItem(iter.key());
57 readFormatSettings();
58 updateFormats();
61 void
62 FormatDialog::updateFormats() {
64 formatBox->clear();
66 const QString curr = hostBox->currentText();
68 if (curr.isEmpty())
69 return;
71 const QStringList formats = hosts[curr].split("|");
73 typedef unsigned int _uint;
74 const register _uint size = formats.size();
76 for (register _uint i=0; i<size; ++i)
77 formatBox->addItem(formats[i]);
79 if (size > 1)
80 formatBox->addItem("best");
82 if (!sel[curr].isEmpty()) {
83 // Qt should provide setCurrentText for QComboBox.
84 // Instead we have to use a workaround in its absence.
85 if (selN[curr] == -1)
86 selN[curr] = 0;
87 formatBox->setCurrentIndex(selN[curr]);
91 void
92 FormatDialog::saveCurrent() {
94 if (hostBox->count() == 0)
95 return;
97 QString fmt = formatBox->currentText();
98 if (fmt.isEmpty())
99 fmt = "flv";
101 if (lastHost.isEmpty())
102 lastHost = hosts.begin().key();
104 sel[lastHost] = fmt;
105 selN[lastHost] = formatBox->currentIndex();
107 // qDebug() << "saved:" << lastHost << sel[lastHost] << selN[lastHost];
110 void
111 FormatDialog::onHostChanged(const QString& host) {
112 saveCurrent();
113 updateFormats();
114 lastHost = host;
117 void
118 FormatDialog::writeSettings() {
119 QSettings s;
120 s.beginGroup("FormatDialog");
121 s.setValue("size", size());
122 writeFormatSettings(s);
123 s.endGroup();
126 void
127 FormatDialog::writeFormatSettings(QSettings& s) {
128 s.beginGroup("hosts");
129 for (QStringMap::const_iterator iter = sel.begin();
130 iter != sel.end(); ++iter)
132 const QString curr = iter.key();
133 s.setValue(iter.key(),
134 QString("%1|%2").arg(sel[curr]).arg(selN[curr]));
136 s.endGroup();
139 void
140 FormatDialog::readSettings() {
141 QSettings s;
142 s.beginGroup("FormatDialog");
143 resize( s.value("size", QSize(400,140)).toSize() );
144 s.endGroup();
147 void
148 FormatDialog::readFormatSettings() {
149 QSettings s;
151 s.beginGroup("FormatDialog");
152 s.beginGroup("hosts");
154 for (QStringMap::const_iterator iter = hosts.begin();
155 iter != hosts.end(); ++iter)
157 const QString curr = iter.key();
158 const QString v = s.value(iter.key()).toString();
159 const QStringList tmp = v.split("|");
161 if (tmp.size() == 2) {
162 sel[curr] = tmp[0]; // format string
163 selN[curr] = tmp[1].toInt(); // last known index in combobox
167 s.endGroup();
168 s.endGroup();
171 const QString
172 FormatDialog::getFormatSetting(const QString& url) const {
173 for (QStringMap::const_iterator iter = sel.begin();
174 iter != sel.end(); ++iter)
176 QRegExp re(iter.key());
177 if (re.indexIn(url) != -1)
178 return sel[iter.key()];
180 return "flv";