NOISSUE add missing files

This commit is contained in:
Petr Mrázek 2015-09-27 22:31:52 +02:00
parent 271ad9e4fd
commit 33c3850b40
4 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,37 @@
#include "IconResourceHandler.h"
#include <xdgicon.h>
#include <QDir>
#include <QDebug>
QList<std::weak_ptr<IconResourceHandler>> IconResourceHandler::m_iconHandlers;
IconResourceHandler::IconResourceHandler(const QString &key)
: m_key(key)
{
}
void IconResourceHandler::setTheme(const QString &theme)
{
// notify everyone
for (auto handler : m_iconHandlers)
{
std::shared_ptr<IconResourceHandler> ptr = handler.lock();
if (ptr)
{
ptr->setResult(ptr->get());
}
}
}
void IconResourceHandler::init(std::shared_ptr<ResourceHandler> &ptr)
{
m_iconHandlers.append(std::dynamic_pointer_cast<IconResourceHandler>(ptr));
// we always have a result, so lets report it now!
setResult(get());
}
QVariant IconResourceHandler::get() const
{
return XdgIcon::fromTheme(m_key);
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <memory>
#include <resources/ResourceHandler.h>
#include "multimc_logic_export.h"
class MULTIMC_LOGIC_EXPORT IconResourceHandler : public ResourceHandler
{
public:
explicit IconResourceHandler(const QString &key);
/// Sets the current theme and notifies all IconResourceHandlers of the change
static void setTheme(const QString &theme);
private:
// we need to keep track of all IconResourceHandlers so that we can update them if the theme changes
void init(std::shared_ptr<ResourceHandler> &ptr) override;
static QList<std::weak_ptr<IconResourceHandler>> m_iconHandlers;
QString m_key;
// the workhorse, returns QVariantMap (filename => size) for m_key/m_theme
QVariant get() const;
};

View File

@ -0,0 +1,68 @@
#include "WebResourceHandler.h"
#include "net/CacheDownload.h"
#include "net/HttpMetaCache.h"
#include "net/NetJob.h"
#include "FileSystem.h"
#include "Env.h"
//FIXME: wrong. needs to be done elsewhere.
QMap<QString, NetJob *> WebResourceHandler::m_activeDownloads;
WebResourceHandler::WebResourceHandler(const QString &url)
: QObject(), m_url(url)
{
MetaEntryPtr entry = ENV.metacache()->resolveEntry("icons", url);
if (!entry->stale)
{
setResultFromFile(entry->getFullPath());
}
else if (m_activeDownloads.contains(url))
{
NetJob *job = m_activeDownloads.value(url);
connect(job, &NetJob::succeeded, this, &WebResourceHandler::succeeded);
connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());});
connect(job, &NetJob::progress, this, &WebResourceHandler::progress);
}
else
{
NetJob *job = new NetJob("Icon download");
job->addNetAction(CacheDownload::make(QUrl(url), entry));
connect(job, &NetJob::succeeded, this, &WebResourceHandler::succeeded);
connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());});
connect(job, &NetJob::progress, this, &WebResourceHandler::progress);
connect(job, &NetJob::finished, job, [job](){m_activeDownloads.remove(m_activeDownloads.key(job));job->deleteLater();});
m_activeDownloads.insert(url, job);
job->start();
}
}
void WebResourceHandler::succeeded()
{
MetaEntryPtr entry = ENV.metacache()->resolveEntry("icons", m_url);
setResultFromFile(entry->getFullPath());
m_activeDownloads.remove(m_activeDownloads.key(qobject_cast<NetJob *>(sender())));
}
void WebResourceHandler::progress(qint64 current, qint64 total)
{
if (total == 0)
{
setProgress(101);
}
else
{
setProgress(current / total);
}
}
void WebResourceHandler::setResultFromFile(const QString &file)
{
try
{
setResult(FS::read(file));
}
catch (Exception &e)
{
setFailure(e.cause());
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <QObject>
#include <resources/ResourceHandler.h>
class NetJob;
class WebResourceHandler : public QObject, public ResourceHandler
{
public:
explicit WebResourceHandler(const QString &url);
private slots:
void succeeded();
void progress(qint64 current, qint64 total);
private:
static QMap<QString, NetJob *> m_activeDownloads;
QString m_url;
void setResultFromFile(const QString &file);
};