diff --git a/qt/branding.cpp b/qt/branding.cpp new file mode 100644 index 00000000..e8a779fc --- /dev/null +++ b/qt/branding.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2024 Carl Schwan + * + * 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 "branding.h" +#include "appstream.h" +#include "chelpers.h" + +using namespace AppStream; + +class AppStream::BrandingData : public QSharedData +{ +public: + BrandingData() + { + brandingp = as_branding_new(); + } + + BrandingData(AsBranding *branding) + : brandingp(branding) + { + g_object_ref(brandingp); + } + + ~BrandingData() + { + g_object_unref(brandingp); + } + + bool operator==(const BrandingData &rd) const + { + return rd.brandingp == brandingp; + } + + AsBranding *brandingp; +}; + +Branding::Branding() + : d(new BrandingData) +{ +} + +Branding::Branding(_AsBranding *brandingp) + : d(new BrandingData(brandingp)) +{ +} + +Branding::Branding(const Branding &branding) = default; + +Branding::~Branding() = default; + +Branding &Branding::operator=(const Branding &branding) = default; + +bool Branding::operator==(const Branding &other) const +{ + if (this->d == other.d) { + return true; + } + if (this->d && other.d) { + return *(this->d) == *other.d; + } + return false; +} + +_AsBranding *Branding::cPtr() const +{ + return d->brandingp; +} + +QString Branding::colorKindToString(Branding::ColorKind colorKind) +{ + return valueWrap(as_color_kind_to_string(static_cast(colorKind))); +} + +Branding::ColorKind Branding::colorKindfromString(const QString &string) +{ + return static_cast(as_color_kind_from_string(qPrintable(string))); +} + +QString Branding::colorSchemeToString(ColorSchemeKind colorScheme) +{ + return valueWrap(as_color_scheme_kind_to_string(static_cast(colorScheme))); +} + +Branding::ColorSchemeKind Branding::colorSchemefromString(const QString &string) +{ + return static_cast(as_color_scheme_kind_from_string(qPrintable(string))); +} + +void Branding::setColor(Branding::ColorKind kind, Branding::ColorSchemeKind scheme, const QString &color) +{ + as_branding_set_color(d->brandingp, static_cast(kind), static_cast(scheme), qPrintable(color)); +} + +void Branding::removeColor(Branding::ColorKind kind, Branding::ColorSchemeKind scheme) +{ + as_branding_remove_color(d->brandingp, static_cast(kind), static_cast(scheme)); +} + +QString Branding::color(Branding::ColorKind kind, Branding::ColorSchemeKind scheme) +{ + return valueWrap(as_branding_get_color(d->brandingp, static_cast(kind), static_cast(scheme))); +} diff --git a/qt/branding.h b/qt/branding.h new file mode 100644 index 00000000..810cf417 --- /dev/null +++ b/qt/branding.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2024 Carl Schwan + * + * 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 . + */ + +#pragma once + +#include +#include +#include "appstreamqt_export.h" + +struct _AsBranding; + +namespace AppStream +{ + +class BrandingData; + +class APPSTREAMQT_EXPORT Branding +{ + Q_GADGET + +public: + enum class ColorSchemeKind { + Unknown, + Light, + Dark, + }; + + enum class ColorKind { + Unknow, + Primary, + }; + + Branding(); + Branding(_AsBranding *); + Branding(const Branding &branding); + ~Branding(); + + Branding &operator=(const Branding &branding); + bool operator==(const Branding &r) const; + + /** + * Converts the ColorKind enumerated value to an text representation. + */ + static QString colorKindToString(ColorKind colorKind); + + /** + * Converts the text representation to an ColorKind enumerated value. + */ + static ColorKind colorKindfromString(const QString &string); + + /** + * Converts the ColorScheme enumerated value to an text representation. + */ + static QString colorSchemeToString(ColorSchemeKind colorScheme); + + /** + * Converts the text representation to an ColorScheme enumerated value. + */ + static ColorSchemeKind colorSchemefromString(const QString &string); + + /** + * Sets a new accent color. If a color of the given kind with the given scheme preference already exists, + * it will be overriden with the new color code. + */ + void setColor(ColorKind kind, ColorSchemeKind scheme, const QString &color); + + /** + * Deletes a color that matches the given type and scheme preference. + */ + void removeColor(ColorKind kind, ColorSchemeKind scheme); + + /** + * Retrieve a color of the given @kind that matches @scheme_kind. + * If a color has no scheme preference defined, it will be returned for either scheme type, + * unless a more suitable color was found. + */ + QString color(ColorKind kind, ColorSchemeKind scheme); + + /** + * \returns the internally stored AsBranding + */ + _AsBranding *cPtr() const; + +private: + QSharedDataPointer d; +}; + +}; diff --git a/qt/component.cpp b/qt/component.cpp index 2f1dbc49..59e7acce 100644 --- a/qt/component.cpp +++ b/qt/component.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "branding.h" #include "chelpers.h" #include "icon.h" #include "screenshot.h" @@ -761,6 +762,19 @@ void Component::setNameVariantSuffix(const QString &variantSuffix, const QString lang.isEmpty() ? NULL : qPrintable(lang)); } +Branding Component::branding() const +{ + auto branding = as_component_get_branding(d->cpt); + if (branding == NULL) + return Branding(); + return Branding(branding); +} + +void Component::setBranding(const Branding &branding) +{ + as_component_set_branding(d->cpt, branding.cPtr()); +} + bool Component::hasTag(const QString &ns, const QString &tagName) { return as_component_has_tag(d->cpt, qPrintable(ns), qPrintable(tagName)); diff --git a/qt/component.h b/qt/component.h index b52f69c7..c0c220c6 100644 --- a/qt/component.h +++ b/qt/component.h @@ -43,6 +43,7 @@ class Icon; class Screenshot; class Suggested; class Developer; +class Branding; class RelationCheckResult; class ComponentData; @@ -264,6 +265,9 @@ class APPSTREAMQT_EXPORT Component QString nameVariantSuffix() const; void setNameVariantSuffix(const QString &variantSuffix, const QString &lang = {}); + Branding branding() const; + void setBranding(const Branding &branding); + bool hasTag(const QString &ns, const QString &tagName); bool addTag(const QString &ns, const QString &tagName); void removeTag(const QString &ns, const QString &tagName); diff --git a/qt/meson.build b/qt/meson.build index 1909bcf7..7d9ed7d5 100644 --- a/qt/meson.build +++ b/qt/meson.build @@ -27,6 +27,7 @@ qt_dep = dependency( asqt_src = [ 'bundle.cpp', + 'branding.cpp', 'category.cpp', 'component-box.cpp', 'component.cpp', @@ -53,6 +54,7 @@ asqt_src = [ asqt_pub_headers = [ 'appstreamqt_export.h', + 'branding.h', 'bundle.h', 'category.h', 'component-box.h',