gs-update-list.c 6.21 KB
Newer Older
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2013-2014 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib/gi18n.h>
#include <gtk/gtk.h>
26
#include <appstream-glib.h>
27 28 29 30 31 32

#include "gs-update-list.h"

#include "gs-app.h"
#include "gs-app-row.h"

33
typedef struct
34 35 36
{
	GtkSizeGroup	*sizegroup_image;
	GtkSizeGroup	*sizegroup_name;
37 38 39 40 41
} GsUpdateListPrivate;

enum {
	SIGNAL_BUTTON_CLICKED,
	SIGNAL_LAST
42 43
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static guint signals [SIGNAL_LAST] = { 0 };

G_DEFINE_TYPE_WITH_PRIVATE (GsUpdateList, gs_update_list, GTK_TYPE_LIST_BOX)

#define GET_PRIV(o)	gs_update_list_get_instance_private(o)

/**
 * gs_update_list_button_clicked_cb:
 **/
static void
gs_update_list_button_clicked_cb (GsAppRow *app_row,
				  GsUpdateList *update_list)
{
	GsApp *app = gs_app_row_get_app (app_row);
	g_signal_emit (update_list, signals[SIGNAL_BUTTON_CLICKED], 0, app);
}
60 61 62

void
gs_update_list_add_app (GsUpdateList *update_list,
63
			GsApp	*app)
64
{
65
	GsUpdateListPrivate *priv = GET_PRIV (update_list);
66 67
	GtkWidget *app_row;

68
	app_row = gs_app_row_new (app);
69
	gs_app_row_set_show_update (GS_APP_ROW (app_row), TRUE);
70 71 72
	g_signal_connect (app_row, "button-clicked",
			  G_CALLBACK (gs_update_list_button_clicked_cb),
			  update_list);
73 74
	gtk_container_add (GTK_CONTAINER (update_list), app_row);
	gs_app_row_set_size_groups (GS_APP_ROW (app_row),
75 76
				    priv->sizegroup_image,
				    priv->sizegroup_name);
77 78 79
	gtk_widget_show (app_row);
}

80
GList *
81 82
gs_update_list_get_apps (GsUpdateList *update_list)
{
83
	GList *apps = NULL;
84
	GList *l;
85
	g_autoptr(GList) children = NULL;
86 87 88

	children = gtk_container_get_children (GTK_CONTAINER (update_list));
	for (l = children; l != NULL; l = l->next) {
89 90
		GsAppRow *app_row = GS_APP_ROW (l->data);
		apps = g_list_prepend (apps, gs_app_row_get_app (app_row));
91 92 93 94
	}
	return apps;
}

95 96 97
static gboolean
is_addon_id_kind (GsApp *app)
{
98
	AsIdKind id_kind;
99
	id_kind = gs_app_get_id_kind (app);
100
	if (id_kind == AS_ID_KIND_DESKTOP)
101
		return FALSE;
102
	if (id_kind == AS_ID_KIND_WEB_APP)
103
		return FALSE;
104 105
	if (id_kind == AS_ID_KIND_FIRMWARE)
		return FALSE;
106 107 108 109 110
	return TRUE;
}

static void
list_header_func (GtkListBoxRow *row,
111 112
		  GtkListBoxRow *before,
		  gpointer user_data)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
{
	GtkStyleContext *context;
	GtkWidget *header;

	/* first entry */
	gtk_list_box_row_set_header (row, NULL);
	if (before == NULL)
		return;

	/* desktop -> addons */
	if (!is_addon_id_kind (gs_app_row_get_app (GS_APP_ROW (before))) &&
	    is_addon_id_kind (gs_app_row_get_app (GS_APP_ROW (row)))) {
		/* TRANSLATORS: This is the header dividing the normal
		 * applications and the addons */
		header = gtk_label_new (_("Add-ons"));
		g_object_set (header,
129 130
			      "xalign", 0.0,
			      NULL);
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
		context = gtk_widget_get_style_context (header);
		gtk_style_context_add_class (context, "header-label");
	} else {
		header = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
	}
	gtk_list_box_row_set_header (row, header);
}

static gchar *
get_app_sort_key (GsApp *app)
{
	GString *key;

	key = g_string_sized_new (64);

	/* sort by kind */
	switch (gs_app_get_kind (app)) {
	case GS_APP_KIND_OS_UPDATE:
		g_string_append (key, "1:");
		break;
	default:
		g_string_append (key, "2:");
		break;
	}

	/* sort desktop files, then addons */
	switch (gs_app_get_id_kind (app)) {
158
	case AS_ID_KIND_FIRMWARE:
159 160
		g_string_append (key, "1:");
		break;
161
	case AS_ID_KIND_DESKTOP:
162 163
		g_string_append (key, "2:");
		break;
164 165 166
	default:
		g_string_append (key, "3:");
		break;
167 168 169 170
	}

	/* sort by install date */
	g_string_append_printf (key, "%09" G_GUINT64_FORMAT ":",
171
				G_MAXUINT64 - gs_app_get_install_date (app));
172 173 174 175 176 177 178 179

	/* finally, sort by short name */
	g_string_append (key, gs_app_get_name (app));
	return g_string_free (key, FALSE);
}

static gint
list_sort_func (GtkListBoxRow *a,
180 181
		GtkListBoxRow *b,
		gpointer user_data)
182 183 184
{
	GsApp *a1 = gs_app_row_get_app (GS_APP_ROW (a));
	GsApp *a2 = gs_app_row_get_app (GS_APP_ROW (b));
185 186
	g_autofree gchar *key1 = get_app_sort_key (a1);
	g_autofree gchar *key2 = get_app_sort_key (a2);
187 188

	/* compare the keys according to the algorithm above */
189
	return g_strcmp0 (key1, key2);
190 191 192
}

static void
193
gs_update_list_dispose (GObject *object)
194 195
{
	GsUpdateList *update_list = GS_UPDATE_LIST (object);
196
	GsUpdateListPrivate *priv = GET_PRIV (update_list);
197

198 199
	g_clear_object (&priv->sizegroup_image);
	g_clear_object (&priv->sizegroup_name);
200

201
	G_OBJECT_CLASS (gs_update_list_parent_class)->dispose (object);
202 203 204 205 206
}

static void
gs_update_list_init (GsUpdateList *update_list)
{
207 208 209
	GsUpdateListPrivate *priv = GET_PRIV (update_list);
	priv->sizegroup_image = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
	priv->sizegroup_name = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
210 211

	gtk_list_box_set_header_func (GTK_LIST_BOX (update_list),
212 213
				      list_header_func,
				      update_list, NULL);
214
	gtk_list_box_set_sort_func (GTK_LIST_BOX (update_list),
215 216
				    list_sort_func,
				    update_list, NULL);
217 218 219 220 221 222 223
}

static void
gs_update_list_class_init (GsUpdateListClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

224 225 226 227 228 229 230
	signals [SIGNAL_BUTTON_CLICKED] =
		g_signal_new ("button-clicked",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GsUpdateListClass, button_clicked),
			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
			      G_TYPE_NONE, 1, GS_TYPE_APP);

231
	object_class->dispose = gs_update_list_dispose;
232 233 234 235 236 237 238 239 240 241 242 243 244
}

GtkWidget *
gs_update_list_new (void)
{
	GsUpdateList *update_list;

	update_list = g_object_new (GS_TYPE_UPDATE_LIST, NULL);

	return GTK_WIDGET (update_list);
}

/* vim: set noexpandtab: */