File: wikipediasettings.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (254 lines) | stat: -rw-r--r-- 6,904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "wikipediasettings.h"
#include "gui/settings.h"
#include "network/networkaccessmanager.h"
#include "support/action.h"
#include "support/icon.h"
#include "support/spinner.h"
#include "support/thread.h"
#include "support/utils.h"
#include "wikipediaengine.h"
#include <QFile>
#include <QUrlQuery>
#include <QXmlStreamReader>
#ifdef BUNDLED_KARCHIVE
#include <kcompressiondevice.h>
#else
#include <KCompressionDevice>
#endif

static const QString constOldFileName = QLatin1String("wikipedia-available.xml.gz");
static const QString constFileName = QLatin1String("languages.xml.gz");

QString WikipediaSettings::constSubDir = QLatin1String("wikipedia");

static QString localeFile()
{
	return Utils::cacheDir(WikipediaSettings::constSubDir, true) + constFileName;
}

WikipediaLoader::WikipediaLoader()
	: QObject(nullptr)
{
	thread = new Thread(metaObject()->className());
	moveToThread(thread);
	thread->start();
}

WikipediaLoader::~WikipediaLoader()
{
	thread->stop();
}

void WikipediaLoader::load(const QByteArray& data)
{
	QStringList preferred = WikipediaEngine::getPreferedLangs();
	QXmlStreamReader xml(data);

	while (!xml.atEnd() && !xml.hasError()) {
		xml.readNext();
		if (xml.isStartElement() && QLatin1String("iw") == xml.name()) {
			const QXmlStreamAttributes& a = xml.attributes();
			if (a.hasAttribute(QLatin1String("prefix")) && a.hasAttribute(QLatin1String("language")) && a.hasAttribute(QLatin1String("url"))) {
				// The urlPrefix is the lang code infront of the wikipedia host
				// url. It is mostly the same as the "prefix" attribute but in
				// some weird cases they differ, so we can't just use "prefix".
				QString prefix = a.value(QLatin1String("prefix")).toString();
				QString urlPrefix = QUrl(a.value(QLatin1String("url")).toString()).host().remove(QLatin1String(".wikipedia.org"));
				emit entry(prefix, urlPrefix, a.value(QLatin1String("language")).toString(), preferred.indexOf(prefix + ":" + urlPrefix));
			}
		}
	}
	emit finished();
}

WikipediaSettings::WikipediaSettings(QWidget* p)
	: ToggleList(p), state(Initial), job(nullptr), spinner(nullptr), loader(nullptr)
{
	label->setText(tr("Choose the wikipedia languages you want to use when searching for artist and album information."));
	reload = new Action(tr("Reload"), this);
	connect(reload, SIGNAL(triggered()), this, SLOT(getLangs()));
	available->addAction(reload);
	available->setContextMenuPolicy(Qt::ActionsContextMenu);
}

WikipediaSettings::~WikipediaSettings()
{
	if (loader) {
		loader->deleteLater();
	}
}

void WikipediaSettings::showEvent(QShowEvent* e)
{
	if (Initial == state) {
		state = Loading;
		QByteArray data;
		QString fileName = localeFile();
		if (QFile::exists(fileName)) {
			KCompressionDevice f(fileName, KCompressionDevice::GZip);
			if (f.open(QIODevice::ReadOnly)) {
			    data = f.readAll();
			}
		}

		if (data.isEmpty()) {
			getLangs();
		}
		else {
			showSpinner();
			parseLangs(data);
		}
	}
	QWidget::showEvent(e);
}

void WikipediaSettings::load()
{
}

void WikipediaSettings::save()
{
	if (Loaded != state) {
		return;
	}
	QStringList pref;
	for (int i = 0; i < selected->count(); ++i) {
		pref.append(selected->item(i)->data(Qt::UserRole).toString());
	}
	if (pref.isEmpty()) {
		pref.append("en:en");
	}
	Settings::self()->saveWikipediaLangs(pref);
	WikipediaEngine::setPreferedLangs(pref);
}

void WikipediaSettings::cancel()
{
	if (job) {
		disconnect(job, SIGNAL(finished()), this, SLOT(parseLangs()));
		job->deleteLater();
		job = nullptr;
	}
}

void WikipediaSettings::getLangs()
{
	state = Loading;
	showSpinner();
	available->clear();
	selected->clear();
	reload->setEnabled(false);
	cancel();
	QUrl url("http://en.wikipedia.org/w/api.php");
	QUrlQuery q;

	q.addQueryItem(QLatin1String("action"), QLatin1String("query"));
	q.addQueryItem(QLatin1String("meta"), QLatin1String("siteinfo"));
	q.addQueryItem(QLatin1String("siprop"), QLatin1String("interwikimap"));
	q.addQueryItem(QLatin1String("sifilteriw"), QLatin1String("local"));
	q.addQueryItem(QLatin1String("format"), QLatin1String("xml"));

	url.setQuery(q);

	job = NetworkAccessManager::self()->get(url);
	connect(job, SIGNAL(finished()), this, SLOT(parseLangs()));
}

void WikipediaSettings::parseLangs()
{
	NetworkJob* reply = qobject_cast<NetworkJob*>(sender());
	if (!reply) {
		return;
	}
	reload->setEnabled(true);
	reply->deleteLater();
	if (reply != job) {
		return;
	}
	job = nullptr;
	QByteArray data = reply->readAll();
	parseLangs(data);
	KCompressionDevice f(localeFile(), KCompressionDevice::GZip);
	if (f.open(QIODevice::WriteOnly)) {
	    f.write(data);
	}
}

void WikipediaSettings::parseLangs(const QByteArray& data)
{
	prefMap.clear();
	if (!loader) {
		loader = new WikipediaLoader();
		connect(loader, SIGNAL(entry(QString, QString, QString, int)), SLOT(addEntry(QString, QString, QString, int)));
		connect(loader, SIGNAL(finished()), SLOT(loaderFinished()));
		connect(this, SIGNAL(load(QByteArray)), loader, SLOT(load(QByteArray)));
	}
	emit load(data);
}

void WikipediaSettings::addEntry(const QString& prefix, const QString& urlPrefix, const QString& lang, int prefIndex)
{
	QString entry = prefix + ":" + urlPrefix;
	QListWidgetItem* item = new QListWidgetItem(-1 == prefIndex ? available : selected);
	item->setText(QString("[%1] %2").arg(prefix).arg(lang));
	item->setData(Qt::UserRole, entry);
	if (-1 != prefIndex) {
		prefMap[prefIndex] = item;
	}
}

void WikipediaSettings::loaderFinished()
{
	QMap<int, QListWidgetItem*>::ConstIterator it(prefMap.constBegin());
	QMap<int, QListWidgetItem*>::ConstIterator end(prefMap.constEnd());
	for (; it != end; ++it) {
		int row = selected->row(it.value());
		if (row != it.key()) {
			selected->insertItem(it.key(), selected->takeItem(row));
		}
	}

	hideSpinner();
	state = Loaded;
}

void WikipediaSettings::showSpinner()
{
	if (!spinner) {
		spinner = new Spinner(available);
		spinner->setWidget(available);
	}
	spinner->start();
}

void WikipediaSettings::hideSpinner()
{
	if (spinner) {
		spinner->stop();
	}
}

#include "moc_wikipediasettings.cpp"