Flutter Engine
 
Loading...
Searching...
No Matches
fl_settings_portal.cc File Reference
#include "flutter/shell/platform/linux/fl_settings_portal.h"
#include <gio/gio.h>
#include <glib.h>

Go to the source code of this file.

Classes

struct  FlSetting
 
struct  _FlSettingsPortal
 

Enumerations

enum  ColorScheme {
  COLOR_SCHEME_DEFAULT ,
  COLOR_SCHEME_PREFER_DARK ,
  COLOR_SCHEME_PREFER_LIGHT
}
 

Functions

static void fl_settings_portal_iface_init (FlSettingsInterface *iface)
 
 G_DEFINE_TYPE_WITH_CODE (FlSettingsPortal, fl_settings_portal, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_settings_get_type(), fl_settings_portal_iface_init)) static gchar *format_key(const FlSetting *setting)
 
static gboolean get_value (FlSettingsPortal *portal, const FlSetting *setting, GVariant **value)
 
static void set_value (FlSettingsPortal *portal, const FlSetting *setting, GVariant *value)
 
static gboolean settings_portal_read (GDBusProxy *proxy, const gchar *ns, const gchar *key, GVariant **out)
 
static void settings_portal_changed_cb (GDBusProxy *proxy, const char *sender_name, const char *signal_name, GVariant *parameters, gpointer user_data)
 
static FlClockFormat fl_settings_portal_get_clock_format (FlSettings *settings)
 
static FlColorScheme fl_settings_portal_get_color_scheme (FlSettings *settings)
 
static gboolean fl_settings_portal_get_enable_animations (FlSettings *settings)
 
static gboolean fl_settings_portal_get_high_contrast (FlSettings *settings)
 
static gdouble fl_settings_portal_get_text_scaling_factor (FlSettings *settings)
 
static void fl_settings_portal_dispose (GObject *object)
 
static void fl_settings_portal_class_init (FlSettingsPortalClass *klass)
 
static void fl_settings_portal_init (FlSettingsPortal *self)
 
FlSettingsPortal * fl_settings_portal_new ()
 
FlSettingsPortal * fl_settings_portal_new_with_values (GVariantDict *values)
 
gboolean fl_settings_portal_start (FlSettingsPortal *self, GError **error)
 

Variables

static constexpr char kPortalName [] = "org.freedesktop.portal.Desktop"
 
static constexpr char kPortalPath [] = "/org/freedesktop/portal/desktop"
 
static constexpr char kPortalSettings [] = "org.freedesktop.portal.Settings"
 
static constexpr char kXdgAppearance [] = "org.freedesktop.appearance"
 
static const FlSetting kColorScheme
 
static constexpr char kGnomeA11yInterface []
 
static const FlSetting kHighContrast
 
static constexpr char kGnomeDesktopInterface [] = "org.gnome.desktop.interface"
 
static const FlSetting kClockFormat
 
static const FlSetting kEnableAnimations
 
static const FlSetting kGtkTheme
 
static const FlSetting kTextScalingFactor
 
static const FlSetting kAllSettings []
 
static constexpr char kClockFormat12Hour [] = "12h"
 
static constexpr char kGtkThemeDarkSuffix [] = "-dark"
 

Enumeration Type Documentation

◆ ColorScheme

Enumerator
COLOR_SCHEME_DEFAULT 
COLOR_SCHEME_PREFER_DARK 
COLOR_SCHEME_PREFER_LIGHT 

Definition at line 65 of file fl_settings_portal.cc.

Function Documentation

◆ fl_settings_portal_class_init()

static void fl_settings_portal_class_init ( FlSettingsPortalClass *  klass)
static

Definition at line 252 of file fl_settings_portal.cc.

252 {
253 GObjectClass* object_class = G_OBJECT_CLASS(klass);
254 object_class->dispose = fl_settings_portal_dispose;
255}
static void fl_settings_portal_dispose(GObject *object)

References fl_settings_portal_dispose().

◆ fl_settings_portal_dispose()

static void fl_settings_portal_dispose ( GObject *  object)
static

Definition at line 243 of file fl_settings_portal.cc.

243 {
244 FlSettingsPortal* self = FL_SETTINGS_PORTAL(object);
245
246 g_clear_object(&self->dbus_proxy);
247 g_clear_pointer(&self->values, g_variant_dict_unref);
248
249 G_OBJECT_CLASS(fl_settings_portal_parent_class)->dispose(object);
250}

References self.

Referenced by fl_settings_portal_class_init().

◆ fl_settings_portal_get_clock_format()

static FlClockFormat fl_settings_portal_get_clock_format ( FlSettings *  settings)
static

Definition at line 167 of file fl_settings_portal.cc.

167 {
168 FlSettingsPortal* self = FL_SETTINGS_PORTAL(settings);
169
170 FlClockFormat clock_format = FL_CLOCK_FORMAT_24H;
171
172 g_autoptr(GVariant) value = nullptr;
173 if (get_value(self, &kClockFormat, &value)) {
174 const gchar* clock_format_str = g_variant_get_string(value, nullptr);
175 if (g_strcmp0(clock_format_str, kClockFormat12Hour) == 0) {
176 clock_format = FL_CLOCK_FORMAT_12H;
177 }
178 }
179
180 return clock_format;
181}
int32_t value
g_autoptr(GMutexLocker) locker
FlClockFormat
Definition fl_settings.h:21
@ FL_CLOCK_FORMAT_12H
Definition fl_settings.h:22
@ FL_CLOCK_FORMAT_24H
Definition fl_settings.h:23
static gboolean get_value(FlSettingsPortal *portal, const FlSetting *setting, GVariant **value)
static const FlSetting kClockFormat
static constexpr char kClockFormat12Hour[]

References FL_CLOCK_FORMAT_12H, FL_CLOCK_FORMAT_24H, g_autoptr(), get_value(), kClockFormat, kClockFormat12Hour, self, and value.

Referenced by fl_settings_portal_iface_init().

◆ fl_settings_portal_get_color_scheme()

static FlColorScheme fl_settings_portal_get_color_scheme ( FlSettings *  settings)
static

Definition at line 183 of file fl_settings_portal.cc.

183 {
184 FlSettingsPortal* self = FL_SETTINGS_PORTAL(settings);
185
187
188 g_autoptr(GVariant) value = nullptr;
189 if (get_value(self, &kColorScheme, &value)) {
190 if (g_variant_get_uint32(value) == COLOR_SCHEME_PREFER_DARK) {
191 color_scheme = FL_COLOR_SCHEME_DARK;
192 }
193 } else if (get_value(self, &kGtkTheme, &value)) {
194 const gchar* gtk_theme_str = g_variant_get_string(value, nullptr);
195 if (g_str_has_suffix(gtk_theme_str, kGtkThemeDarkSuffix)) {
196 color_scheme = FL_COLOR_SCHEME_DARK;
197 }
198 }
199
200 return color_scheme;
201}
FlColorScheme
Definition fl_settings.h:33
@ FL_COLOR_SCHEME_DARK
Definition fl_settings.h:35
@ FL_COLOR_SCHEME_LIGHT
Definition fl_settings.h:34
static const FlSetting kColorScheme
static const FlSetting kGtkTheme
static constexpr char kGtkThemeDarkSuffix[]

References COLOR_SCHEME_PREFER_DARK, FL_COLOR_SCHEME_DARK, FL_COLOR_SCHEME_LIGHT, g_autoptr(), get_value(), kColorScheme, kGtkTheme, kGtkThemeDarkSuffix, self, and value.

Referenced by fl_settings_portal_iface_init().

◆ fl_settings_portal_get_enable_animations()

static gboolean fl_settings_portal_get_enable_animations ( FlSettings *  settings)
static

Definition at line 203 of file fl_settings_portal.cc.

203 {
204 FlSettingsPortal* self = FL_SETTINGS_PORTAL(settings);
205
206 gboolean enable_animations = true;
207
208 g_autoptr(GVariant) value = nullptr;
209 if (get_value(self, &kEnableAnimations, &value)) {
210 enable_animations = g_variant_get_boolean(value);
211 }
212
213 return enable_animations;
214}
static const FlSetting kEnableAnimations

References g_autoptr(), get_value(), kEnableAnimations, self, and value.

Referenced by fl_settings_portal_iface_init().

◆ fl_settings_portal_get_high_contrast()

static gboolean fl_settings_portal_get_high_contrast ( FlSettings *  settings)
static

Definition at line 216 of file fl_settings_portal.cc.

216 {
217 FlSettingsPortal* self = FL_SETTINGS_PORTAL(settings);
218
219 gboolean high_contrast = false;
220
221 g_autoptr(GVariant) value = nullptr;
222 if (get_value(self, &kHighContrast, &value)) {
223 high_contrast = g_variant_get_boolean(value);
224 }
225
226 return high_contrast;
227}
static const FlSetting kHighContrast

References g_autoptr(), get_value(), kHighContrast, self, and value.

Referenced by fl_settings_portal_iface_init().

◆ fl_settings_portal_get_text_scaling_factor()

static gdouble fl_settings_portal_get_text_scaling_factor ( FlSettings *  settings)
static

Definition at line 229 of file fl_settings_portal.cc.

230 {
231 FlSettingsPortal* self = FL_SETTINGS_PORTAL(settings);
232
233 gdouble scaling_factor = 1.0;
234
235 g_autoptr(GVariant) value = nullptr;
236 if (get_value(self, &kTextScalingFactor, &value)) {
237 scaling_factor = g_variant_get_double(value);
238 }
239
240 return scaling_factor;
241}
static const FlSetting kTextScalingFactor

References g_autoptr(), get_value(), kTextScalingFactor, self, and value.

Referenced by fl_settings_portal_iface_init().

◆ fl_settings_portal_iface_init()

static void fl_settings_portal_iface_init ( FlSettingsInterface *  iface)
static

Definition at line 257 of file fl_settings_portal.cc.

257 {
258 iface->get_clock_format = fl_settings_portal_get_clock_format;
259 iface->get_color_scheme = fl_settings_portal_get_color_scheme;
260 iface->get_enable_animations = fl_settings_portal_get_enable_animations;
261 iface->get_high_contrast = fl_settings_portal_get_high_contrast;
262 iface->get_text_scaling_factor = fl_settings_portal_get_text_scaling_factor;
263}
static gdouble fl_settings_portal_get_text_scaling_factor(FlSettings *settings)
static FlColorScheme fl_settings_portal_get_color_scheme(FlSettings *settings)
static gboolean fl_settings_portal_get_high_contrast(FlSettings *settings)
static FlClockFormat fl_settings_portal_get_clock_format(FlSettings *settings)
static gboolean fl_settings_portal_get_enable_animations(FlSettings *settings)

References fl_settings_portal_get_clock_format(), fl_settings_portal_get_color_scheme(), fl_settings_portal_get_enable_animations(), fl_settings_portal_get_high_contrast(), and fl_settings_portal_get_text_scaling_factor().

◆ fl_settings_portal_init()

static void fl_settings_portal_init ( FlSettingsPortal *  self)
static

Definition at line 265 of file fl_settings_portal.cc.

265{}

◆ fl_settings_portal_new()

FlSettingsPortal * fl_settings_portal_new ( )

FlSettingsPortal: #FlSettingsPortal reads settings from the XDG desktop portal. fl_settings_portal_new:

Creates a new settings portal instance.

Returns: a new #FlSettingsPortal.

Definition at line 267 of file fl_settings_portal.cc.

267 {
268 g_autoptr(GVariantDict) values = g_variant_dict_new(nullptr);
270}
FlSettingsPortal * fl_settings_portal_new_with_values(GVariantDict *values)

References fl_settings_portal_new_with_values(), and g_autoptr().

Referenced by fl_settings_new().

◆ fl_settings_portal_new_with_values()

FlSettingsPortal * fl_settings_portal_new_with_values ( GVariantDict *  values)

fl_settings_portal_new_with_values: @values: (nullable): a #GVariantDict.

Creates a new settings portal instance with initial values for testing.

Returns: a new #FlSettingsPortal.

Definition at line 272 of file fl_settings_portal.cc.

272 {
273 g_return_val_if_fail(values != nullptr, nullptr);
274 FlSettingsPortal* portal =
275 FL_SETTINGS_PORTAL(g_object_new(fl_settings_portal_get_type(), nullptr));
276 portal->values = g_variant_dict_ref(values);
277 return portal;
278}

Referenced by fl_settings_portal_new(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ fl_settings_portal_start()

gboolean fl_settings_portal_start ( FlSettingsPortal *  portal,
GError **  error 
)

fl_settings_portal_start: @portal: an #FlSettingsPortal. @error: (allow-none): #GError location to store the error occurring, or NULL

Reads the current settings and starts monitoring for changes in the desktop portal settings.

Returns: TRUE on success, or FALSE if the portal is not available.

Definition at line 280 of file fl_settings_portal.cc.

280 {
281 g_return_val_if_fail(FL_IS_SETTINGS_PORTAL(self), false);
282 g_return_val_if_fail(self->dbus_proxy == nullptr, false);
283
284 self->dbus_proxy = g_dbus_proxy_new_for_bus_sync(
285 G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, nullptr, kPortalName,
287
288 if (self->dbus_proxy == nullptr) {
289 return FALSE;
290 }
291
292 for (const FlSetting setting : kAllSettings) {
293 g_autoptr(GVariant) value = nullptr;
294 if (settings_portal_read(self->dbus_proxy, setting.ns, setting.key,
295 &value)) {
296 set_value(self, &setting, value);
297 }
298 }
299
300 g_signal_connect_object(self->dbus_proxy, "g-signal",
301 G_CALLBACK(settings_portal_changed_cb), self,
302 static_cast<GConnectFlags>(0));
303
304 return true;
305}
const uint8_t uint32_t uint32_t GError ** error
static gboolean settings_portal_read(GDBusProxy *proxy, const gchar *ns, const gchar *key, GVariant **out)
static void set_value(FlSettingsPortal *portal, const FlSetting *setting, GVariant *value)
static void settings_portal_changed_cb(GDBusProxy *proxy, const char *sender_name, const char *signal_name, GVariant *parameters, gpointer user_data)
static const FlSetting kAllSettings[]
static constexpr char kPortalPath[]
static constexpr char kPortalName[]
static constexpr char kPortalSettings[]

References error, g_autoptr(), kAllSettings, kPortalName, kPortalPath, kPortalSettings, self, set_value(), settings_portal_changed_cb(), settings_portal_read(), and value.

Referenced by fl_settings_new().

◆ G_DEFINE_TYPE_WITH_CODE()

G_DEFINE_TYPE_WITH_CODE ( FlSettingsPortal  ,
fl_settings_portal  ,
G_TYPE_OBJECT  ,
G_IMPLEMENT_INTERFACE(fl_settings_get_type(), fl_settings_portal_iface_init  
) const

Definition at line 80 of file fl_settings_portal.cc.

86 {
87 return g_strconcat(setting->ns, "::", setting->key, nullptr);
88}

References FlSetting::key, and FlSetting::ns.

◆ get_value()

static gboolean get_value ( FlSettingsPortal *  portal,
const FlSetting setting,
GVariant **  value 
)
static

Definition at line 90 of file fl_settings_portal.cc.

92 {
93 g_autofree const gchar* key = format_key(setting);
94 *value = g_variant_dict_lookup_value(portal->values, key, setting->type);
95 return *value != nullptr;
96}
const GVariantType * type

References key, FlSetting::type, and value.

Referenced by fl_settings_portal_get_clock_format(), fl_settings_portal_get_color_scheme(), fl_settings_portal_get_enable_animations(), fl_settings_portal_get_high_contrast(), and fl_settings_portal_get_text_scaling_factor().

◆ set_value()

static void set_value ( FlSettingsPortal *  portal,
const FlSetting setting,
GVariant *  value 
)
static

Definition at line 98 of file fl_settings_portal.cc.

100 {
101 g_autofree const gchar* key = format_key(setting);
102
103 // ignore redundant changes from multiple XDG desktop portal backends
104 g_autoptr(GVariant) old_value =
105 g_variant_dict_lookup_value(portal->values, key, nullptr);
106 if (old_value != nullptr && value != nullptr &&
107 g_variant_equal(old_value, value)) {
108 return;
109 }
110
111 g_variant_dict_insert_value(portal->values, key, value);
112 fl_settings_emit_changed(FL_SETTINGS(portal));
113}
void fl_settings_emit_changed(FlSettings *self)

References fl_settings_emit_changed(), g_autoptr(), key, and value.

Referenced by fl_settings_portal_start(), and settings_portal_changed_cb().

◆ settings_portal_changed_cb()

static void settings_portal_changed_cb ( GDBusProxy *  proxy,
const char *  sender_name,
const char *  signal_name,
GVariant *  parameters,
gpointer  user_data 
)
static

Definition at line 151 of file fl_settings_portal.cc.

155 {
156 FlSettingsPortal* portal = FL_SETTINGS_PORTAL(user_data);
157 if (g_strcmp0(signal_name, "SettingChanged")) {
158 return;
159 }
160
161 FlSetting setting;
162 g_autoptr(GVariant) value = nullptr;
163 g_variant_get(parameters, "(&s&sv)", &setting.ns, &setting.key, &value);
164 set_value(portal, &setting, value);
165}
const gchar * key
const gchar * ns

References g_autoptr(), FlSetting::key, FlSetting::ns, set_value(), user_data, and value.

Referenced by fl_settings_portal_start().

◆ settings_portal_read()

static gboolean settings_portal_read ( GDBusProxy *  proxy,
const gchar *  ns,
const gchar *  key,
GVariant **  out 
)
static

Definition at line 117 of file fl_settings_portal.cc.

120 {
121 g_autoptr(GError) error = nullptr;
122 g_autoptr(GVariant) value =
123 g_dbus_proxy_call_sync(proxy, "Read", g_variant_new("(ss)", ns, key),
124 G_DBUS_CALL_FLAGS_NONE, G_MAXINT, nullptr, &error);
125
126 if (error) {
127 if (error->domain == G_DBUS_ERROR &&
128 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN) {
129 g_debug("XDG desktop portal unavailable: %s", error->message);
130 return FALSE;
131 }
132
133 if (error->domain == G_DBUS_ERROR &&
134 error->code == G_DBUS_ERROR_UNKNOWN_METHOD) {
135 g_debug("XDG desktop portal settings unavailable: %s", error->message);
136 return FALSE;
137 }
138
139 g_critical("Failed to read XDG desktop portal settings: %s",
140 error->message);
141 return FALSE;
142 }
143
144 g_autoptr(GVariant) child = nullptr;
145 g_variant_get(value, "(v)", &child);
146 g_variant_get(child, "v", out);
147
148 return TRUE;
149}
struct @13 g_debug
return TRUE

References error, g_autoptr(), g_debug, key, TRUE, and value.

Referenced by fl_settings_portal_start().

Variable Documentation

◆ kAllSettings

◆ kClockFormat

const FlSetting kClockFormat
static
Initial value:
= {
"clock-format",
G_VARIANT_TYPE_STRING,
}
static constexpr char kGnomeDesktopInterface[]

Definition at line 36 of file fl_settings_portal.cc.

36 {
38 "clock-format",
39 G_VARIANT_TYPE_STRING,
40};

Referenced by fl_settings_portal_get_clock_format().

◆ kClockFormat12Hour

constexpr char kClockFormat12Hour[] = "12h"
staticconstexpr

Definition at line 62 of file fl_settings_portal.cc.

Referenced by fl_settings_portal_get_clock_format().

◆ kColorScheme

const FlSetting kColorScheme
static
Initial value:
= {
"color-scheme",
G_VARIANT_TYPE_UINT32,
}
static constexpr char kXdgAppearance[]

Definition at line 21 of file fl_settings_portal.cc.

21 {
23 "color-scheme",
24 G_VARIANT_TYPE_UINT32,
25};

Referenced by fl_settings_portal_get_color_scheme().

◆ kEnableAnimations

const FlSetting kEnableAnimations
static
Initial value:
= {
"enable-animations",
G_VARIANT_TYPE_BOOLEAN,
}

Definition at line 41 of file fl_settings_portal.cc.

41 {
43 "enable-animations",
44 G_VARIANT_TYPE_BOOLEAN,
45};

Referenced by fl_settings_portal_get_enable_animations().

◆ kGnomeA11yInterface

constexpr char kGnomeA11yInterface[]
staticconstexpr
Initial value:
=
"org.gnome.desktop.a11y.interface"

Definition at line 27 of file fl_settings_portal.cc.

◆ kGnomeDesktopInterface

constexpr char kGnomeDesktopInterface[] = "org.gnome.desktop.interface"
staticconstexpr

Definition at line 35 of file fl_settings_portal.cc.

◆ kGtkTheme

const FlSetting kGtkTheme
static
Initial value:
= {
"gtk-theme",
G_VARIANT_TYPE_STRING,
}

Definition at line 46 of file fl_settings_portal.cc.

46 {
48 "gtk-theme",
49 G_VARIANT_TYPE_STRING,
50};

Referenced by fl_settings_portal_get_color_scheme().

◆ kGtkThemeDarkSuffix

constexpr char kGtkThemeDarkSuffix[] = "-dark"
staticconstexpr

Definition at line 63 of file fl_settings_portal.cc.

Referenced by fl_settings_portal_get_color_scheme().

◆ kHighContrast

const FlSetting kHighContrast
static
Initial value:
= {
"high-contrast",
G_VARIANT_TYPE_BOOLEAN,
}
static constexpr char kGnomeA11yInterface[]

Definition at line 29 of file fl_settings_portal.cc.

29 {
31 "high-contrast",
32 G_VARIANT_TYPE_BOOLEAN,
33};

Referenced by fl_settings_portal_get_high_contrast().

◆ kPortalName

constexpr char kPortalName[] = "org.freedesktop.portal.Desktop"
staticconstexpr

Definition at line 10 of file fl_settings_portal.cc.

Referenced by fl_settings_portal_start().

◆ kPortalPath

constexpr char kPortalPath[] = "/org/freedesktop/portal/desktop"
staticconstexpr

Definition at line 11 of file fl_settings_portal.cc.

Referenced by fl_settings_portal_start().

◆ kPortalSettings

constexpr char kPortalSettings[] = "org.freedesktop.portal.Settings"
staticconstexpr

Definition at line 12 of file fl_settings_portal.cc.

Referenced by fl_settings_portal_start().

◆ kTextScalingFactor

const FlSetting kTextScalingFactor
static
Initial value:
= {
"text-scaling-factor",
G_VARIANT_TYPE_DOUBLE,
}

Definition at line 51 of file fl_settings_portal.cc.

51 {
53 "text-scaling-factor",
54 G_VARIANT_TYPE_DOUBLE,
55};

Referenced by fl_settings_portal_get_text_scaling_factor().

◆ kXdgAppearance

constexpr char kXdgAppearance[] = "org.freedesktop.appearance"
staticconstexpr

Definition at line 20 of file fl_settings_portal.cc.