Commit a492d557 authored by LordTermor's avatar LordTermor

Removed now unnecessary qml async helpers

parent 80da5532
#pragma once
#include <QQuickItem>
#include <QGuiApplication>
#include <QCursor>
#include <memory>
#include <mutex>
class QmlFutureWatcher;
class Database;
struct QmlFutureImpl{
std::mutex mutex;
friend struct GenericQmlFuture;
void setFuture(QVariant future);
public:
bool isRunning() const;
private:
bool m_isRunning = true;
QVariant futureData;
QmlFutureWatcher* watcher = nullptr;
};
struct GenericQmlFuture{
friend class QmlFutureWatcher;
Q_GADGET
public:
GenericQmlFuture(QmlFutureImpl* implementation);
GenericQmlFuture()=default;
QVariant result() const;
void setResult(QVariant future);
bool isRunning() const;
void setRunning(bool isRunning);
private:
QmlFutureWatcher* getWatcher(){
return implementation->watcher;
}
void setWatcher(QmlFutureWatcher* watcher){
implementation->watcher=watcher;
}
std::shared_ptr<QmlFutureImpl> implementation;
};
Q_DECLARE_METATYPE(GenericQmlFuture)
class QmlFutureWatcher:public QQuickItem{
Q_OBJECT
Q_PROPERTY(GenericQmlFuture future READ future WRITE setFuture NOTIFY futureChanged)
Q_PROPERTY(bool running MEMBER m_running)
public:
explicit QmlFutureWatcher(QQuickItem* parent = nullptr):
QQuickItem (parent)
{
connect(this,&QmlFutureWatcher::finished,this,&QmlFutureWatcher::reset);QGuiApplication::restoreOverrideCursor();
}
GenericQmlFuture future() const
{
return m_future;
}
public Q_SLOTS:
void setFuture(GenericQmlFuture future);
inline Q_INVOKABLE void reset(){
this->setProperty("running",false);
while(QGuiApplication::overrideCursor()!=nullptr){
QGuiApplication::restoreOverrideCursor();
}
}
Q_SIGNALS:
void finished(QVariant result);
void futureChanged(GenericQmlFuture future);
private:
GenericQmlFuture m_future;
bool m_running;
};
#pragma once
#include <QObject>
#include <Package.h>
#include <AsyncHelpers.h>
#include <memory>
#include <Config.h>
#include <Updates.h>
......
......@@ -5,6 +5,7 @@
#include <memory>
#include <cstring>
#include "Database.h"
#include <QJSValue>
#include <SnapPackage.h>
namespace LibQPamac{
......@@ -66,7 +67,6 @@ public:
pamac_transaction_get_authorization(m_handle);
}
Q_INVOKABLE GenericQmlFuture getBuildFiles(const QString& pkgname);
Database* database() const
{
......
#include "AsyncHelpers.h"
#include "Package.h"
GenericQmlFuture::GenericQmlFuture(QmlFutureImpl *implementation):
implementation(std::shared_ptr<QmlFutureImpl>(implementation,[](QmlFutureImpl* impl){
if (impl->m_isRunning) {
impl->m_isRunning=false;
} else {
delete impl;
}
})){}
QVariant GenericQmlFuture::result() const
{
return implementation->futureData;
}
void GenericQmlFuture::setResult(QVariant future){
implementation->setFuture(std::move(future));
}
void QmlFutureImpl::setFuture(QVariant future){
this->mutex.lock();
if(isRunning()){
futureData = std::move(future);
m_isRunning = false;
if(watcher!=nullptr){
Q_EMIT watcher->finished(futureData);
}
}
this->mutex.unlock();
}
bool QmlFutureImpl::isRunning() const
{
return m_isRunning;
}
bool GenericQmlFuture::isRunning() const
{
return implementation->m_isRunning;
}
void GenericQmlFuture::setRunning(bool isRunning)
{
implementation->m_isRunning = isRunning;
}
void QmlFutureWatcher::setFuture(GenericQmlFuture future)
{
future.implementation->mutex.lock();
if (m_future.implementation == future.implementation) {
return;
}
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
future.setWatcher(this);
m_future = future;
Q_EMIT futureChanged(future);
this->setProperty("running",true);
if(!future.isRunning()) {
Q_EMIT finished(future.result());
}
future.implementation->mutex.unlock();
}
#include "Database.h"
#include <functional>
#include <QFile>
namespace LibQPamac {
Database::Database(PamacDatabase *db, QObject *parent):
......
#include "LibQPamacModule.h"
#include <QQmlEngine>
#include <AsyncHelpers.h>
#include <Database.h>
#include <AurPackageModel.h>
#include <AurPackage.h>
......@@ -19,7 +18,6 @@ void LibQPamacModule::registerTypes(const char *uri)
qRegisterMetaType<Package>("Package");
qRegisterMetaType<Updates>("Updates");
qRegisterMetaType<TransactionSummary>("TransactionSummary");
qRegisterMetaType<GenericQmlFuture>("Future");
qRegisterMetaType<QList<HistoryItem>>("QList<HistoryItem>");
qmlRegisterSingletonType<LibQPamac::Database>("QPamac.Database",1,0,"Database",
......@@ -29,9 +27,6 @@ void LibQPamacModule::registerTypes(const char *uri)
return new LibQPamac::Database("/etc/pamac.conf",scriptEngine);
});
qmlRegisterUncreatableType<GenericQmlFuture>("QPamac.Async",1,0,"Future","");
qmlRegisterType<QmlFutureWatcher>("QPamac.Async",1,0,"FutureWatcher");
qmlRegisterType<HistoryItemModel>("QPamac.History",1,0,"HistoryModel");
qmlRegisterUncreatableType<AurPackage>("QPamac.AUR.Package",1,0,"AURPackage","");
qmlRegisterUncreatableType<AlpmPackage>("QPamac.Package",1,0,"AlpmPackage","");
......
......@@ -4,35 +4,6 @@
#include <atomic>
#include <QVariant>
GenericQmlFuture LibQPamac::Transaction::getBuildFiles(const QString &pkgname){
auto future = new QmlFutureImpl;
// pamac_transaction_get_build_files(m_handle,pkgname.toUtf8(),
// [](GObject* parent,GAsyncResult* res,gpointer futurePtr){
// auto future = reinterpret_cast<QmlFutureImpl*>(futurePtr);
// if(future->isRunning()){
// auto transaction = reinterpret_cast<PamacTransaction*>(parent);
// int length = 0;
// gchar** result = pamac_transaction_get_build_files_finish(transaction,res);
// QStringList resList;
// for(int i =0;i<length;i++){
// QFileInfo file(QString::fromUtf8(result[i]));
// resList.append(file.fileName());
// g_free(result[i]);
// }
// g_free(result);
// future->setFuture(QVariant::fromValue(resList));
// }
// else {
// delete future;
// }
// },future);
return GenericQmlFuture(future);
}
bool LibQPamac::Transaction::run(const QStringList& toInstall, const QStringList& toRemove, const QStringList& toLoad,
const QStringList& toBuild, const QStringList& tempIgnore, const QStringList& overwriteFiles,
const QList<SnapPackage>& toInstallSnap, const QList<SnapPackage>& toRemoveSnap)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment