summaryrefslogtreecommitdiffstats
path: root/src/gs-repo-row.c
blob: 72f0416aa961c933db220474ca9bb14da2709ff5 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "config.h"

#include "gs-repo-row.h"

#include <glib/gi18n.h>

typedef struct
{
	GsApp		*repo;
	GtkWidget	*button;
	GtkWidget	*name_label;
	GtkWidget	*comment_label;
	GtkWidget	*details_revealer;
	GtkWidget	*status_label;
	GtkWidget	*url_box;
	GtkWidget	*url_label;
	guint		 refresh_idle_id;
} GsRepoRowPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (GsRepoRow, gs_repo_row, GTK_TYPE_LIST_BOX_ROW)

enum {
	SIGNAL_BUTTON_CLICKED,
	SIGNAL_LAST
};

static guint signals [SIGNAL_LAST] = { 0 };

void
gs_repo_row_set_name (GsRepoRow *row, const gchar *name)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_label_set_text (GTK_LABEL (priv->name_label), name);
	gtk_widget_set_visible (priv->name_label, name != NULL);
}

void
gs_repo_row_set_comment (GsRepoRow *row, const gchar *comment)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_label_set_markup (GTK_LABEL (priv->comment_label), comment);
	gtk_widget_set_visible (priv->comment_label, comment != NULL);
}

void
gs_repo_row_set_url (GsRepoRow *row, const gchar *url)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_label_set_text (GTK_LABEL (priv->url_label), url);
	gtk_widget_set_visible (priv->url_box, url != NULL);
}

static gboolean
repo_supports_removal (GsApp *repo)
{
	const gchar *management_plugin = gs_app_get_management_plugin (repo);

	/* can't remove a repo, only enable/disable existing ones */
	if (g_strcmp0 (management_plugin, "fwupd") == 0 ||
	    g_strcmp0 (management_plugin, "packagekit") == 0 ||
	    g_strcmp0 (management_plugin, "rpm-ostree") == 0)
		return FALSE;

	return TRUE;
}

static void
refresh_ui (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	if (priv->repo == NULL) {
		gtk_widget_set_visible (priv->button, FALSE);
		return;
	}

	gtk_widget_set_visible (priv->button, TRUE);

	/* set button text */
	switch (gs_app_get_state (priv->repo)) {
	case AS_APP_STATE_AVAILABLE:
	case AS_APP_STATE_AVAILABLE_LOCAL:
		/* TRANSLATORS: this is a button in the software repositories
		   dialog for enabling a repo */
		gtk_button_set_label (GTK_BUTTON (priv->button), _("_Enable"));
		/* enable button */
		gtk_widget_set_sensitive (priv->button, TRUE);
		break;
	case AS_APP_STATE_INSTALLED:
		if (repo_supports_removal (priv->repo)) {
			/* TRANSLATORS: this is a button in the software repositories dialog
			   for removing a repo. The ellipsis indicates that further
			   steps are required */
			gtk_button_set_label (GTK_BUTTON (priv->button), _("_Remove…"));
		} else {
			/* TRANSLATORS: this is a button in the software repositories dialog
			   for disabling a repo. The ellipsis indicates that further
			   steps are required */
			gtk_button_set_label (GTK_BUTTON (priv->button), _("_Disable…"));
		}
		/* enable button */
		gtk_widget_set_sensitive (priv->button, TRUE);
		break;
	case AS_APP_STATE_INSTALLING:
		/* TRANSLATORS: this is a button in the software repositories dialog
		   that shows the status of a repo being enabled */
		gtk_button_set_label (GTK_BUTTON (priv->button), _("Enabling"));
		/* disable button */
		gtk_widget_set_sensitive (priv->button, FALSE);
		break;
	case AS_APP_STATE_REMOVING:
		if (repo_supports_removal (priv->repo)) {
			/* TRANSLATORS: this is a button in the software repositories dialog
			   that shows the status of a repo being removed */
			gtk_button_set_label (GTK_BUTTON (priv->button), _("Removing"));
		} else {
			/* TRANSLATORS: this is a button in the software repositories dialog
			   that shows the status of a repo being disabled */
			gtk_button_set_label (GTK_BUTTON (priv->button), _("Disabling"));
		}
		/* disable button */
		gtk_widget_set_sensitive (priv->button, FALSE);
		break;
	default:
		break;
	}

	/* set enabled/disabled label */
	switch (gs_app_get_state (priv->repo)) {
	case AS_APP_STATE_INSTALLED:
		/* TRANSLATORS: this is a label in the software repositories
		   dialog that indicates that a repo is enabled. */
		gtk_label_set_text (GTK_LABEL (priv->status_label), _("Enabled"));
		break;
	case AS_APP_STATE_AVAILABLE:
	case AS_APP_STATE_AVAILABLE_LOCAL:
		/* TRANSLATORS: this is a label in the software repositories
		   dialog that indicates that a repo is disabled. */
		gtk_label_set_text (GTK_LABEL (priv->status_label), _("Disabled"));
		break;
	default:
		break;
	}
}

static gboolean
refresh_idle (gpointer user_data)
{
	g_autoptr(GsRepoRow) row = (GsRepoRow *) user_data;
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	refresh_ui (row);

	priv->refresh_idle_id = 0;
	return G_SOURCE_REMOVE;
}

static void
repo_state_changed_cb (GsApp *repo, GParamSpec *pspec, GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	if (priv->refresh_idle_id > 0)
		return;
	priv->refresh_idle_id = g_idle_add (refresh_idle, g_object_ref (row));
}

void
gs_repo_row_set_repo (GsRepoRow *row, GsApp *repo)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	g_assert (priv->repo == NULL);

	priv->repo = g_object_ref (repo);
	g_signal_connect_object (priv->repo, "notify::state",
	                         G_CALLBACK (repo_state_changed_cb),
	                         row, 0);
	refresh_ui (row);
}

GsApp *
gs_repo_row_get_repo (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);
	return priv->repo;
}

void
gs_repo_row_show_details (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (row), FALSE);
	gtk_revealer_set_reveal_child (GTK_REVEALER (priv->details_revealer), TRUE);
}

void
gs_repo_row_hide_details (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (row), TRUE);
	gtk_revealer_set_reveal_child (GTK_REVEALER (priv->details_revealer), FALSE);
}

void
gs_repo_row_show_status (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);
	gtk_widget_set_visible (priv->status_label, TRUE);
}

static void
button_clicked_cb (GtkWidget *widget, GsRepoRow *row)
{
	g_signal_emit (row, signals[SIGNAL_BUTTON_CLICKED], 0);
}

static void
gs_repo_row_destroy (GtkWidget *object)
{
	GsRepoRow *row = GS_REPO_ROW (object);
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	if (priv->repo != NULL) {
		g_signal_handlers_disconnect_by_func (priv->repo, repo_state_changed_cb, row);
		g_clear_object (&priv->repo);
	}

	if (priv->refresh_idle_id != 0) {
		g_source_remove (priv->refresh_idle_id);
		priv->refresh_idle_id = 0;
	}

	GTK_WIDGET_CLASS (gs_repo_row_parent_class)->destroy (object);
}

static void
gs_repo_row_init (GsRepoRow *row)
{
	GsRepoRowPrivate *priv = gs_repo_row_get_instance_private (row);

	gtk_widget_init_template (GTK_WIDGET (row));
	g_signal_connect (priv->button, "clicked",
	                  G_CALLBACK (button_clicked_cb), row);
}

static void
gs_repo_row_class_init (GsRepoRowClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	widget_class->destroy = gs_repo_row_destroy;

	signals [SIGNAL_BUTTON_CLICKED] =
		g_signal_new ("button-clicked",
		              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (GsRepoRowClass, button_clicked),
		              NULL, NULL, g_cclosure_marshal_VOID__VOID,
		              G_TYPE_NONE, 0);

	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-repo-row.ui");

	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, button);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, name_label);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, comment_label);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, details_revealer);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, status_label);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, url_box);
	gtk_widget_class_bind_template_child_private (widget_class, GsRepoRow, url_label);
}

GtkWidget *
gs_repo_row_new (void)
{
	return g_object_new (GS_TYPE_REPO_ROW, NULL);
}