From aebfa3fc2d52c2ec19ff24036ffe6db7678639d2 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Thu, 24 Nov 2016 17:29:49 +0100 Subject: [PATCH] Introduce AppStream::Suggested in the Qt frontend --- qt/CMakeLists.txt | 2 + qt/component.cpp | 14 +++++++ qt/component.h | 3 ++ qt/suggested.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++ qt/suggested.h | 75 ++++++++++++++++++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 qt/suggested.cpp create mode 100644 qt/suggested.h diff --git a/qt/CMakeLists.txt b/qt/CMakeLists.txt index 0c4ad8d2b..827780185 100644 --- a/qt/CMakeLists.txt +++ b/qt/CMakeLists.txt @@ -31,6 +31,7 @@ set(APPSTREAMQT_SRC provided.cpp release.cpp bundle.cpp + suggested.cpp ) set(APPSTREAMQT_PUBLIC_HEADERS @@ -43,6 +44,7 @@ set(APPSTREAMQT_PUBLIC_HEADERS provided.h release.h bundle.h + suggested.h ) include_directories(${CMAKE_CURRENT_SOURCE_DIR} diff --git a/qt/component.cpp b/qt/component.cpp index 26bed3830..c5b305747 100644 --- a/qt/component.cpp +++ b/qt/component.cpp @@ -30,6 +30,7 @@ #include "screenshot.h" #include "release.h" #include "bundle.h" +#include "suggested.h" using namespace AppStream; @@ -385,6 +386,19 @@ Bundle Component::bundle(Bundle::Kind kind) const return Bundle(bundle); } +QList AppStream::Component::suggested() const +{ + QList res; + + auto suggestions = as_component_get_suggested(m_cpt); + res.reserve(suggestions->len); + for (uint i = 0; i < suggestions->len; i++) { + auto suggestion = AS_SUGGESTED (g_ptr_array_index (suggestions, i)); + res.append(Suggested(suggestion)); + } + return res; +} + bool Component::isValid() const { return as_component_is_valid(m_cpt); diff --git a/qt/component.h b/qt/component.h index 22704a61c..bd225ef4f 100644 --- a/qt/component.h +++ b/qt/component.h @@ -35,6 +35,7 @@ namespace AppStream { class Icon; class Screenshot; class Release; +class Suggested; class ComponentData; @@ -147,6 +148,8 @@ Q_GADGET QList bundles() const; AppStream::Bundle bundle(Bundle::Kind kind) const; + QList suggested() const; + bool isValid() const; private: diff --git a/qt/suggested.cpp b/qt/suggested.cpp new file mode 100644 index 000000000..972a21f9a --- /dev/null +++ b/qt/suggested.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2016 Aleix Pol Gonzalez + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2.1 of the license, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include "appstream.h" +#include "suggested.h" + +#include +#include +#include +#include +#include "chelpers.h" + +using namespace AppStream; + +class AppStream::SuggestedData : public QSharedData { +public: + SuggestedData() + { + m_suggested = as_suggested_new(); + } + + SuggestedData(AsSuggested *suggested) + : m_suggested(suggested) + { + g_object_ref(m_suggested); + } + + ~SuggestedData() + { + g_object_unref(m_suggested); + } + + bool operator==(const SuggestedData& rd) const + { + return rd.m_suggested == m_suggested; + } + + AsSuggested *m_suggested; +}; + +Suggested::Suggested(const Suggested& other) + : d(other.d) +{} + +Suggested::Suggested() + : d(new SuggestedData) +{} + +Suggested::Suggested(_AsSuggested *suggested) + : d(new SuggestedData(suggested)) +{} + +Suggested::~Suggested() +{} + +Suggested& Suggested::operator=(const Suggested& other) +{ + this->d = other.d; + return *this; +} + +Suggested::Kind Suggested::kind() const +{ + return Suggested::Kind(as_suggested_get_kind(d->m_suggested)); +} + +void Suggested::setKind(Suggested::Kind kind) +{ + as_suggested_set_kind(d->m_suggested, (AsSuggestedKind) kind); +} + +const QStringList AppStream::Suggested::ids() const +{ + return valueWrap(as_suggested_get_ids(d->m_suggested)); +} + +void AppStream::Suggested::addSuggested(const QString& id) +{ + as_suggested_add_id(d->m_suggested, qPrintable(id)); +} + +QDebug operator<<(QDebug s, const AppStream::Suggested& image) { + s.nospace() << "AppStream::Suggested(" << image.ids() << ")"; + return s.space(); +} diff --git a/qt/suggested.h b/qt/suggested.h new file mode 100644 index 000000000..d29fab631 --- /dev/null +++ b/qt/suggested.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 Aleix Pol Gonzalez + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2.1 of the license, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef APPSTREAMQT_SUGGESTED_H +#define APPSTREAMQT_SUGGESTED_H + +#include +#include +#include "appstreamqt_export.h" + +class QUrl; +class QString; +struct _AsSuggested; +namespace AppStream { + +class SuggestedData; + +/** + * This class provides a list of other component-ids suggested by a software component, as well + * as an origin of the suggestion (manually suggested by the upstream project, or + * automatically determined by heuristics).. + */ +class APPSTREAMQT_EXPORT Suggested { + Q_GADGET + public: + enum Kind { + KindUnknown, + KindUpstream, + KindHeuristic + }; + Q_ENUM(Kind) + + Suggested(); + Suggested(_AsSuggested *suggested); + Suggested(const Suggested& other); + ~Suggested(); + + Suggested& operator=(const Suggested& other); + + /** + * \return the kind of suggestion + */ + Kind kind() const; + void setKind(Kind kind); + + /** + * \return the local or remote url for this suggested + */ + const QStringList ids() const; + void addSuggested(const QString &id); + + private: + QSharedDataPointer d; +}; +} + +APPSTREAMQT_EXPORT QDebug operator<<(QDebug s, const AppStream::Suggested& suggested); + +#endif // APPSTREAMQT_SUGGESTED_H