Simple docset updating, and docset metadata.

This commit is contained in:
MichaelBeeu 2014-03-03 15:47:26 -06:00
parent 80ce8320a8
commit 6b6167af3b
8 changed files with 211 additions and 5 deletions

View File

@ -26,7 +26,8 @@ SOURCES += main.cpp\
zealsettingsdialog.cpp \
zealnetworkaccessmanager.cpp \
zealsearchquery.cpp \
progressitemdelegate.cpp
progressitemdelegate.cpp \
zealdocsetmetadata.cpp
HEADERS += mainwindow.h \
zeallistmodel.h \
@ -41,7 +42,8 @@ HEADERS += mainwindow.h \
xcb_keysym.h \
zealnetworkaccessmanager.h \
zealsearchquery.h \
progressitemdelegate.h
progressitemdelegate.h \
zealdocsetmetadata.h
FORMS += mainwindow.ui \
zealsettingsdialog.ui

View File

@ -0,0 +1,85 @@
#include "zealdocsetmetadata.h"
ZealDocsetMetadata::ZealDocsetMetadata()
{
}
void ZealDocsetMetadata::read(const QJsonObject &json){
version = json["version"].toString();
feedUrl = json["feed_url"].toString();
QJsonArray urlArray = json["urls"].toArray();
foreach(auto url, urlArray){
urls.append( url.toString() );
}
}
void ZealDocsetMetadata::read(QXmlStreamReader &xml){
while(!xml.atEnd()){
QXmlStreamReader::TokenType token = xml.readNext();
if(token == QXmlStreamReader::StartDocument){
continue;
}
// Try to pull out the relevant data
if(token == QXmlStreamReader::StartElement){
if(xml.name() == "version"){
if(xml.readNext()!=QXmlStreamReader::Characters) continue;
version = xml.text().toString();
} else if(xml.name() == "url"){
if(xml.readNext()!=QXmlStreamReader::Characters) continue;
urls.append( xml.text().toString() );
}
}
}
}
void ZealDocsetMetadata::read(const QString &path) {
QFile file(path);
if(file.open(QIODevice::ReadOnly)){
QByteArray data = file.readAll();
QJsonDocument doc( QJsonDocument::fromJson(data) );
read( doc.object() );
file.close();
}
}
void ZealDocsetMetadata::write(const QString &path) const {
QFile file(path);
if(file.open(QIODevice::WriteOnly)){
QJsonObject meta;
write(meta);
QJsonDocument doc(meta);
file.write(doc.toJson());
file.close();
}
}
void ZealDocsetMetadata::write(QJsonObject &json) const {
json["version"] = version;
QJsonArray urlArray;
foreach(auto url , urls){
urlArray.append(url);
}
json["urls"] = urlArray;
json["feed_url"] = feedUrl;
}
QList<QString> ZealDocsetMetadata::getUrls() const {
return urls;
}
int ZealDocsetMetadata::getNumUrls() const {
return urls.length();
}
QString ZealDocsetMetadata::getVersion() const {
return version;
}
QString ZealDocsetMetadata::getFeedURL() const {
return feedUrl;
}
void ZealDocsetMetadata::setFeedURL(const QString &url){
feedUrl = url;
}

36
zeal/zealdocsetmetadata.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef ZEALDOCSETMETADATA_H
#define ZEALDOCSETMETADATA_H
#include <QDebug>
#include <QCoreApplication>
#include <QXmlStreamReader>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QString>
#include <QFile>
class ZealDocsetMetadata //: public QObject
{
//Q_OBJECT
public:
ZealDocsetMetadata();
void read(const QJsonObject &json);
void read(QXmlStreamReader &xml);
void read(const QString &path);
void write(const QString &path) const;
void write(QJsonObject &json) const;
QList<QString> getUrls() const;
int getNumUrls() const;
QString getVersion() const;
QString getFeedURL() const;
void setFeedURL(const QString &url);
private:
QList<QString> urls;
QString version;
QString feedUrl;
};
Q_DECLARE_METATYPE(ZealDocsetMetadata);
#endif // ZEALDOCSETMETADATA_H

View File

@ -37,6 +37,12 @@ void ZealDocsetsRegistry::addDocset(const QString& path) {
}
dbs.insert(name, db);
dirs.insert(name, dir);
// Read metadata
ZealDocsetMetadata meta;
meta.read(path+"/meta.json");
metadata.insert(name, meta);
qDebug()<<name<<": "<<meta.getVersion();
}
ZealDocsetsRegistry::ZealDocsetsRegistry() :

View File

@ -9,8 +9,10 @@
#include <QIcon>
#include <QMap>
#include <QSettings>
#include <QJsonObject>
#include "zealsearchresult.h"
#include "zealdocsetmetadata.h"
typedef enum {ZEAL, DASH, ZDASH} DocSetType;
@ -47,6 +49,10 @@ public:
return dirs[name];
}
const ZealDocsetMetadata& meta(const QString& name){
return metadata[name];
}
QIcon icon(const QString& name) {
QIcon icon(dir(name).absoluteFilePath("favicon.ico"));
if(icon.availableSizes().isEmpty()) {
@ -114,6 +120,7 @@ private:
QMap<QString, QSqlDatabase> dbs;
QMap<QString, QDir> dirs;
QMap<QString, DocSetType> types;
QMap<QString, ZealDocsetMetadata> metadata;
QList<ZealSearchResult> queryResults;
QSettings settings;
int lastQuery;

View File

@ -127,6 +127,22 @@ void ZealSettingsDialog::endTasks(qint8 tasks = 1)
}
void ZealSettingsDialog::updateDocsets(){
QStringList docsetNames = docsets->names();
foreach(auto name, docsetNames){
ZealDocsetMetadata metadata = docsets->meta(name);
qDebug()<<name<<" Ver: "<<metadata.getVersion();
QString feedUrl = metadata.getFeedURL();
if(!feedUrl.isEmpty()){
auto reply2 = naManager.get(QNetworkRequest(feedUrl));
reply2->setProperty("old_metadata", QVariant::fromValue(metadata));
connect(reply2, &QNetworkReply::downloadProgress, this, &ZealSettingsDialog::on_downloadProgress);
replies.insert(reply2, 0);
}
}
}
void ZealSettingsDialog::DownloadCompleteCb(QNetworkReply *reply){
qint8 remainingRetries = replies.take(reply);
if (reply->error() != QNetworkReply::NoError) {
@ -156,7 +172,11 @@ void ZealSettingsDialog::DownloadCompleteCb(QNetworkReply *reply){
auto name = name_list[name_list.count()-1].replace(".tgz", "");
name = name.replace(".tar.bz2", "");
if(name != "" && !docsets->names().contains(name)) {
urls[name] = url;
auto feedUrl = url;
if( url.contains("feeds") ){
feedUrl = url.section("/",0,-2) + "/" + name + ".xml"; // Attempt to generate a docset feed url.
}
urls[name] = feedUrl;
auto url_list = url.split("/");
auto iconfile = url_list[url_list.count()-1].replace(".tgz", ".png");
iconfile = iconfile.replace(".tar.bz2", ".png");
@ -213,7 +233,33 @@ void ZealSettingsDialog::DownloadCompleteCb(QNetworkReply *reply){
replies.insert(reply3, 1);
connect(reply3, &QNetworkReply::downloadProgress, this, &ZealSettingsDialog::on_downloadProgress);
} else {
if(reply->request().url().path().endsWith("tgz") || reply->request().url().path().endsWith("tar.bz2")) {
if(reply->request().url().path().endsWith("xml")){
QXmlStreamReader feed(reply->readAll() );
ZealDocsetMetadata metadata;
ZealDocsetMetadata oldMetadata;
metadata.read(feed);
if(!metadata.getNumUrls()){
QMessageBox::critical(this, "Zeal", "Could not read docset feed!");
} else {
QVariant oldMeta = reply->property("old_metadata");
if(oldMeta.isValid()){
oldMetadata = oldMeta.value<ZealDocsetMetadata>();
}
qDebug()<<oldMetadata.getVersion()<<" : " <<metadata.getVersion();
if(oldMetadata.getVersion() != metadata.getVersion()){
metadata.setFeedURL(reply->request().url().toString());
qDebug()<<"Download from: "<<metadata.getUrls()[0];
auto reply2 = naManager.get(QNetworkRequest(metadata.getUrls()[0]));
reply2->setProperty("listItem", itemId);
reply2->setProperty("metadata", QVariant::fromValue(metadata));
connect(reply2, &QNetworkReply::downloadProgress, this, &ZealSettingsDialog::on_downloadProgress);
replies.insert(reply2, remainingRetries - 1);
}
}
} else if(reply->request().url().path().endsWith("tgz") || reply->request().url().path().endsWith("tar.bz2")) {
auto dataDir = QDir(docsets->docsetsDir());
if(!dataDir.exists()) {
QMessageBox::critical(this, "No docsets directory found",
@ -272,6 +318,14 @@ void ZealSettingsDialog::DownloadCompleteCb(QNetworkReply *reply){
dataDir2.rename(outDir, docsetName);
}
// Write metadata about docset
ZealDocsetMetadata metadata;
QVariant metavariant = reply->property("metadata");
if(metavariant.isValid()){
metadata = metavariant.value<ZealDocsetMetadata>();
}
metadata.write(dataDir.absoluteFilePath(docsetName)+"/meta.json");
// FIXME C&P (see "FIXME C&P" below)
QMetaObject::invokeMethod(docsets, "addDocset", Qt::BlockingQueuedConnection,
Q_ARG(QString, dataDir.absoluteFilePath(docsetName)));
@ -555,3 +609,8 @@ void ZealSettingsDialog::on_buttonBox_clicked(QAbstractButton *button)
saveSettings();
}
}
void ZealSettingsDialog::on_updateButton_clicked()
{
updateDocsets();
}

View File

@ -8,6 +8,7 @@
#include "ui_zealsettingsdialog.h"
#include "zeallistmodel.h"
#include "zealdocsetsregistry.h"
#include "zealdocsetmetadata.h"
class ZealSettingsDialog : public QDialog
{
@ -29,6 +30,7 @@ private:
void endTasks(qint8 tasks);
void displayProgress();
void loadSettings();
void updateDocsets();
void DownloadCompleteCb(QNetworkReply *reply);
void resetProgress();
void stopDownloads();
@ -68,6 +70,8 @@ private slots:
void on_buttonBox_clicked(QAbstractButton *button);
void on_updateButton_clicked();
private:
ZealListModel &zealList;
QNetworkAccessManager naManager;

View File

@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tab_3">
<attribute name="title">
@ -147,6 +147,13 @@
</item>
<item>
<layout class="QHBoxLayout" name="docsetsActions">
<item>
<widget class="QPushButton" name="updateButton">
<property name="text">
<string>Update docsets</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">