summaryrefslogtreecommitdiffstats
path: root/src/gs-app-tile.c
blob: 01449a7c4059081d8a9d5590cccf6f22454e2acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2013 Matthias Clasen <mclasen@redhat.com>
 * Copyright (C) 2019 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "config.h"

#include "gs-app-tile.h"
#include "gs-common.h"

typedef struct {
	GsApp				*app;
	guint				 app_notify_idle_id;
} GsAppTilePrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GsAppTile, gs_app_tile, GTK_TYPE_BUTTON)

typedef enum {
	PROP_APP = 1,
} GsAppTileProperty;

static GParamSpec *obj_props[PROP_APP + 1] = { NULL, };

static void
gs_app_tile_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	GsAppTile *self = GS_APP_TILE (object);
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);

	switch ((GsAppTileProperty) prop_id) {
	case PROP_APP:
		g_value_set_object (value, priv->app);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_app_tile_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	GsAppTile *self = GS_APP_TILE (object);

	switch ((GsAppTileProperty) prop_id) {
	case PROP_APP:
		gs_app_tile_set_app (self, g_value_get_object (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_app_tile_dispose (GObject *object)
{
	GsAppTile *self = GS_APP_TILE (object);

	gs_app_tile_set_app (self, NULL);

	G_OBJECT_CLASS (gs_app_tile_parent_class)->dispose (object);
}

/**
 * gs_app_tile_get_app:
 * @self: a #GsAppTile
 *
 * Get the value of #GsAppTile:app.
 *
 * Returns: (nullable) (transfer none): the #GsAppTile:app property
 */
GsApp *
gs_app_tile_get_app (GsAppTile *self)
{
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);
	g_return_val_if_fail (GS_IS_APP_TILE (self), NULL);
	return priv->app;
}

static gboolean
gs_app_tile_app_notify_idle_cb (gpointer user_data)
{
	GsAppTile *self = GS_APP_TILE (user_data);
	GsAppTileClass *klass = GS_APP_TILE_GET_CLASS (self);
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);

	priv->app_notify_idle_id = 0;
	klass->refresh (self);

	return G_SOURCE_REMOVE;
}

static void
gs_app_tile_app_notify_cb (GsApp *app, GParamSpec *pspec, GsAppTile *self)
{
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);

	/* Already pending */
	if (priv->app_notify_idle_id != 0)
		return;

	priv->app_notify_idle_id = g_idle_add (gs_app_tile_app_notify_idle_cb, self);
}

/**
 * gs_app_tile_set_app:
 * @self: a #GsAppTile
 * @app: (transfer none) (nullable): the new value for #GsAppTile:app
 *
 * Set the value of #GsAppTile:app.
 */
void
gs_app_tile_set_app (GsAppTile *self, GsApp *app)
{
	GsAppTileClass *klass = GS_APP_TILE_GET_CLASS (self);
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);

	g_return_if_fail (GS_IS_APP_TILE (self));
	g_return_if_fail (!app || GS_IS_APP (app));

	/* cancel pending refresh */
	g_clear_handle_id (&priv->app_notify_idle_id, g_source_remove);

	/* disconnect old app */
	if (priv->app != NULL)
		g_signal_handlers_disconnect_by_func (priv->app, gs_app_tile_app_notify_cb, self);
	g_set_object (&priv->app, app);

	/* optional refresh */
	if (klass->refresh != NULL && priv->app != NULL) {
		g_signal_connect (app, "notify",
				  G_CALLBACK (gs_app_tile_app_notify_cb), self);
		klass->refresh (self);
	}

	g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_APP]);
}

void
gs_app_tile_class_init (GsAppTileClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = gs_app_tile_get_property;
	object_class->set_property = gs_app_tile_set_property;
	object_class->dispose = gs_app_tile_dispose;

	/**
	 * GsAppTile:app: (nullable)
	 *
	 * The app to display in this tile.
	 *
	 * Set this to %NULL to display a loading/empty tile.
	 *
	 * Since: 41
	 */
	obj_props[PROP_APP] =
		g_param_spec_object ("app", "App",
				     "The app to display in this tile.",
				     GS_TYPE_APP,
				     G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, G_N_ELEMENTS (obj_props), obj_props);
}

void
gs_app_tile_init (GsAppTile *self)
{
	GsAppTilePrivate *priv = gs_app_tile_get_instance_private (self);
	priv->app_notify_idle_id = 0;
}