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/>.
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
36 FormatDialog::FormatDialog(QWidget
*parent
)
44 FormatDialog::resetHosts() {
49 FormatDialog::parseHosts(const QStringMap
& 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());
62 FormatDialog::updateFormats() {
66 const QString curr
= hostBox
->currentText();
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
]);
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.
87 formatBox
->setCurrentIndex(selN
[curr
]);
92 FormatDialog::saveCurrent() {
94 if (hostBox
->count() == 0)
97 QString fmt
= formatBox
->currentText();
101 if (lastHost
.isEmpty())
102 lastHost
= hosts
.begin().key();
105 selN
[lastHost
] = formatBox
->currentIndex();
107 // qDebug() << "saved:" << lastHost << sel[lastHost] << selN[lastHost];
111 FormatDialog::onHostChanged(const QString
& host
) {
118 FormatDialog::writeSettings() {
120 s
.beginGroup("FormatDialog");
121 s
.setValue("size", size());
122 writeFormatSettings(s
);
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
]));
140 FormatDialog::readSettings() {
142 s
.beginGroup("FormatDialog");
143 resize( s
.value("size", QSize(400,140)).toSize() );
148 FormatDialog::readFormatSettings() {
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
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()];