/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2013 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2013 Matthias Clasen <mclasen@redhat.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 <string.h>
#include <glib/gi18n.h>

#include "gs-shell.h"
#include "gs-shell-installed.h"
#include "gs-app.h"
#include "gs-utils.h"
#include "gs-app-widget.h"
#include "gs-app-folder-dialog.h"
#include "gs-folders.h"

#define INSTALL_DATE_QUEUED     (G_MAXUINT - 1)
#define INSTALL_DATE_INSTALLING (G_MAXUINT - 2)

static void	gs_shell_installed_finalize	(GObject	*object);
static void	gs_shell_installed_remove_row	(GtkListBox	*list_box,
						 GtkWidget	*child);

#define GS_SHELL_INSTALLED_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_SHELL_INSTALLED, GsShellInstalledPrivate))

struct GsShellInstalledPrivate
{
	GsPluginLoader		*plugin_loader;
	GtkBuilder		*builder;
	GCancellable		*cancellable;
	GtkListBox		*list_box_installed;
	GtkRevealer 		*bottom_bar;
	GtkSizeGroup		*sizegroup_image;
	GtkSizeGroup		*sizegroup_name;
	gboolean		 cache_valid;
	gboolean		 waiting;
	GsShell			*shell;
	gboolean 		 selection_mode;
};

G_DEFINE_TYPE (GsShellInstalled, gs_shell_installed, G_TYPE_OBJECT)

static void gs_shell_installed_pending_apps_changed_cb (GsPluginLoader *plugin_loader,
							GsShellInstalled *shell_installed);
static void set_selection_mode (GsShellInstalled *shell_installed, gboolean selection_mode);

/**
 * gs_shell_installed_invalidate:
 **/
void
gs_shell_installed_invalidate (GsShellInstalled *shell_installed)
{
	shell_installed->priv->cache_valid = FALSE;
}

static void
gs_shell_installed_app_widget_activated_cb (GtkListBox *list_box,
					    GtkListBoxRow *row,
					    GsShellInstalled *shell_installed)
{
	GsAppWidget *app_widget;

	app_widget = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (row)));

	if (shell_installed->priv->selection_mode) {
		gboolean selected;
		selected = gs_app_widget_get_selected (app_widget);
		gs_app_widget_set_selected (app_widget, !selected);
	} else {
		GsApp *app;
		app = gs_app_widget_get_app (app_widget);
		gs_shell_show_app (shell_installed->priv->shell, app);
	}
}

typedef struct {
	GsAppWidget		*app_widget;
	GsShellInstalled	*shell_installed;
} GsShellInstalledHelper;

static void
row_unrevealed (GObject *revealer, GParamSpec *pspec, gpointer data)
{
	GtkWidget *row, *list;

	row = gtk_widget_get_parent (GTK_WIDGET (revealer));
	list = gtk_widget_get_parent (row);

	gtk_container_remove (GTK_CONTAINER (list), row);
}

static void
gs_shell_installed_remove_row (GtkListBox *list_box, GtkWidget *child)
{
	GtkWidget *row, *revealer;

	gtk_widget_set_sensitive (child, FALSE);
	row = gtk_widget_get_parent (child);
	revealer = gtk_revealer_new ();
	gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
	gtk_widget_show (revealer);
	gtk_widget_reparent (child, revealer);
	gtk_container_add (GTK_CONTAINER (row), revealer);
	g_signal_connect (revealer, "notify::child-revealed",
			  G_CALLBACK (row_unrevealed), NULL);
	gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), FALSE);
}

/**
 * gs_shell_installed_app_removed_cb:
 **/
static void
gs_shell_installed_app_removed_cb (GObject *source,
				   GAsyncResult *res,
				   gpointer user_data)
{
	GError *error = NULL;
	GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source);
	GsShellInstalledHelper *helper = (GsShellInstalledHelper *) user_data;
	GsShellInstalledPrivate *priv = helper->shell_installed->priv;
	GsApp *app;
	gboolean ret;

	ret = gs_plugin_loader_app_action_finish (plugin_loader,
						  res,
						  &error);
	if (!ret) {
		app = gs_app_widget_get_app (helper->app_widget);
		g_warning ("failed to remove %s: %s",
			   gs_app_get_id (app),
			   error->message);
		gs_app_notify_failed_modal (priv->builder,
					    app,
					    GS_PLUGIN_LOADER_ACTION_REMOVE,
					    error);
		g_error_free (error);
	} else {
		/* remove from the list */
		app = gs_app_widget_get_app (helper->app_widget);
		g_debug ("removed %s", gs_app_get_id (app));
		gs_shell_installed_remove_row (GTK_LIST_BOX (priv->list_box_installed),
					       GTK_WIDGET (helper->app_widget));
	}

	g_object_unref (helper->app_widget);
	g_object_unref (helper->shell_installed);
	g_free (helper);
}

/**
 * gs_shell_installed_app_remove_cb:
 **/
static void
gs_shell_installed_app_remove_cb (GsAppWidget *app_widget,
				  GsShellInstalled *shell_installed)
{
	GsApp *app;
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GString *markup;
	GtkResponseType response;
	GtkWidget *dialog;
	GtkWindow *window;
	GsShellInstalledHelper *helper;

	window = GTK_WINDOW (gtk_builder_get_object (priv->builder, "window_software"));
	markup = g_string_new ("");
	app = gs_app_widget_get_app (app_widget);
	g_string_append_printf (markup,
				/* TRANSLATORS: this is a prompt message, and
				 * '%s' is an application summary, e.g. 'GNOME Clocks' */
				_("Are you sure you want to remove %s?"),
				gs_app_get_name (app));
	g_string_prepend (markup, "<b>");
	g_string_append (markup, "</b>");
	dialog = gtk_message_dialog_new (window,
					 GTK_DIALOG_MODAL,
					 GTK_MESSAGE_QUESTION,
					 GTK_BUTTONS_CANCEL,
					 NULL);
	gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), markup->str);
	gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
						    /* TRANSLATORS: longer dialog text */
						    _("%s will be removed, and you will have to install it to use it again."),
						    gs_app_get_name (app));
	/* TRANSLATORS: this is button text to remove the application */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Remove"), GTK_RESPONSE_OK);
	if (gs_app_get_state (app) == GS_APP_STATE_QUEUED)
		response = GTK_RESPONSE_OK; /* pending install */
	else
		response = gtk_dialog_run (GTK_DIALOG (dialog));
	if (response == GTK_RESPONSE_OK) {
		g_debug ("removing %s", gs_app_get_id (app));
		helper = g_new0 (GsShellInstalledHelper, 1);
		helper->shell_installed = g_object_ref (shell_installed);
		helper->app_widget = g_object_ref (app_widget);
		gs_plugin_loader_app_action_async (priv->plugin_loader,
						   app,
						   GS_PLUGIN_LOADER_ACTION_REMOVE,
						   priv->cancellable,
						   gs_shell_installed_app_removed_cb,
						   helper);
	}
	g_string_free (markup, TRUE);
	gtk_widget_destroy (dialog);
}

/**
 * gs_shell_installed_notify_state_changed_cb:
 **/
static void
gs_shell_installed_notify_state_changed_cb (GsApp *app,
					    GParamSpec *pspec,
					    GsShellInstalled *shell)
{
	gtk_list_box_invalidate_sort (shell->priv->list_box_installed);
}

static void selection_changed (GsShellInstalled *shell);

static void
gs_shell_installed_add_app (GsShellInstalled *shell, GsApp *app)
{
	GsShellInstalledPrivate *priv = shell->priv;
	GtkWidget *widget;

	widget = gs_app_widget_new ();
	gs_app_widget_set_colorful (GS_APP_WIDGET (widget), FALSE);
	g_signal_connect (widget, "button-clicked",
			  G_CALLBACK (gs_shell_installed_app_remove_cb), shell);
	g_signal_connect_object (app, "notify::state",
				 G_CALLBACK (gs_shell_installed_notify_state_changed_cb),
				 shell, 0);
	g_signal_connect_swapped (widget, "notify::selected",
			 	  G_CALLBACK (selection_changed), shell);
	gs_app_widget_set_app (GS_APP_WIDGET (widget), app);
	gtk_container_add (GTK_CONTAINER (priv->list_box_installed), widget);
	gs_app_widget_set_size_groups (GS_APP_WIDGET (widget),
				       priv->sizegroup_image,
				       priv->sizegroup_name);

	gs_app_widget_set_selectable (GS_APP_WIDGET (widget),
				      priv->selection_mode);

	gtk_widget_show (widget);
}

/**
 * gs_shell_installed_get_installed_cb:
 **/
static void
gs_shell_installed_get_installed_cb (GObject *source_object,
				     GAsyncResult *res,
				     gpointer user_data)
{
	GError *error = NULL;
	GList *l;
	GList *list;
	GsApp *app;
	GsShellInstalled *shell_installed = GS_SHELL_INSTALLED (user_data);
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
	GtkWidget *widget;

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "spinner_install"));
	gs_stop_spinner (GTK_SPINNER (widget));
	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "stack_install"));
	gtk_stack_set_visible_child_name (GTK_STACK (widget), "view");

	priv->waiting = FALSE;
	priv->cache_valid = TRUE;

	list = gs_plugin_loader_get_installed_finish (plugin_loader,
						      res,
						      &error);
	if (list == NULL) {
		g_warning ("failed to get installed apps: %s", error->message);
		g_error_free (error);
		goto out;
	}
	for (l = list; l != NULL; l = l->next) {
		app = GS_APP (l->data);
		gs_shell_installed_add_app (shell_installed, app);
	}
out:
	gs_plugin_list_free (list);
	gs_shell_installed_pending_apps_changed_cb (plugin_loader, shell_installed);
}

/**
 * gs_shell_installed_refresh:
 **/
void
gs_shell_installed_refresh (GsShellInstalled *shell_installed, gboolean scroll_up)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GtkWidget *widget;
	GtkSpinner *spinner;

	if (gs_shell_get_mode (priv->shell) == GS_SHELL_MODE_INSTALLED) {
		set_selection_mode (shell_installed, FALSE);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "buttonbox_main"));
		gtk_widget_show (widget);

		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_select"));
		gtk_widget_show (widget);
	}

	gtk_list_box_invalidate_sort (priv->list_box_installed);

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "scrolledwindow_install"));
	if (scroll_up) {
		GtkAdjustment *adj;
		adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
		gtk_adjustment_set_value (adj, gtk_adjustment_get_lower (adj));
	}
	if (gs_shell_get_mode (priv->shell) == GS_SHELL_MODE_INSTALLED) {
		gs_grab_focus_when_mapped (widget);
	}

	/* no need to refresh */
	if (priv->cache_valid)
		return;

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "spinner_install"));
	gtk_widget_show (widget);

	if (priv->waiting)
		return;

	/* remove old entries */
	gs_container_remove_all (GTK_CONTAINER (priv->list_box_installed));

	/* get popular apps */
	gs_plugin_loader_get_installed_async (priv->plugin_loader,
					      GS_PLUGIN_REFINE_FLAGS_DEFAULT |
					      GS_PLUGIN_REFINE_FLAGS_REQUIRE_HISTORY |
					      GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION |
					      GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION |
					      GS_PLUGIN_REFINE_FLAGS_REQUIRE_RATING,
					      priv->cancellable,
					      gs_shell_installed_get_installed_cb,
					      shell_installed);

	spinner = GTK_SPINNER (gtk_builder_get_object (shell_installed->priv->builder, "spinner_install"));
	gs_start_spinner (spinner);
	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "stack_install"));
	gtk_stack_set_visible_child_name (GTK_STACK (widget), "spinner");

	priv->waiting = TRUE;
}

/**
 * gs_shell_installed_get_app_sort_key:
 *
 * Get a sort key to achive this:
 *
 * 1. state:installing applications
 * 2. state:removing applications
 * 3. kind:normal applications
 * 4. kind:system applications
 *
 * Within each of these groups, they are sorted by the install date and then
 * by name.
 **/
static gchar *
gs_shell_installed_get_app_sort_key (GsApp *app)
{
	GString *key;

	key = g_string_sized_new (64);

	/* sort installed, removing, other */
	switch (gs_app_get_state (app)) {
	case GS_APP_STATE_INSTALLING:
		g_string_append (key, "1:");
		break;
	case GS_APP_STATE_REMOVING:
		g_string_append (key, "2:");
		break;
	default:
		g_string_append (key, "3:");
		break;
	}

	/* sort desktop files, then addons */
	switch (gs_app_get_id_kind (app)) {
	case GS_APP_ID_KIND_DESKTOP:
	case GS_APP_ID_KIND_WEBAPP:
		g_string_append (key, "1:");
		break;
	default:
		g_string_append (key, "2:");
		break;
	}

	/* sort normal, system, other */
	switch (gs_app_get_kind (app)) {
	case GS_APP_KIND_NORMAL:
		g_string_append (key, "1:");
		break;
	case GS_APP_KIND_SYSTEM:
		g_string_append (key, "2:");
		break;
	default:
		g_string_append (key, "3:");
		break;
	}

	/* sort by install date */
	g_string_append_printf (key, "%09" G_GUINT64_FORMAT ":",
				G_MAXUINT64 - gs_app_get_install_date (app));

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

/**
 * gs_shell_installed_sort_func:
 **/
static gint
gs_shell_installed_sort_func (GtkListBoxRow *a,
			      GtkListBoxRow *b,
			      gpointer user_data)
{
	GsApp *a1, *a2;
	GsAppWidget *aw1, *aw2;
	gchar *key1 = NULL;
	gchar *key2 = NULL;
	gint retval = 0;

	/* check valid */
	if (!GTK_IS_BIN(a) || !GTK_IS_BIN(b)) {
		g_warning ("GtkListBoxRow not valid");
		goto out;
	}

	aw1 = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (a)));
	aw2 = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (b)));
	a1 = gs_app_widget_get_app (aw1);
	a2 = gs_app_widget_get_app (aw2);
	key1 = gs_shell_installed_get_app_sort_key (a1);
	key2 = gs_shell_installed_get_app_sort_key (a2);

	/* compare the keys according to the algorithm above */
	retval = g_strcmp0 (key1, key2);
out:
	g_free (key1);
	g_free (key2);
	return retval;
}

/**
 * gs_shell_installed_is_addon_id_kind
 **/
static gboolean
gs_shell_installed_is_addon_id_kind (GsApp *app)
{
	GsAppIdKind id_kind;
	id_kind = gs_app_get_id_kind (app);
	if (id_kind == GS_APP_ID_KIND_DESKTOP)
		return FALSE;
	if (id_kind == GS_APP_ID_KIND_WEBAPP)
		return FALSE;
	return TRUE;
}

/**
 * gs_shell_installed_list_header_func
 **/
static void
gs_shell_installed_list_header_func (GtkListBoxRow *row,
				     GtkListBoxRow *before,
				     gpointer user_data)
{
	GsAppWidget *aw1;
	GsAppWidget *aw2;
	GtkStyleContext *context;
	GtkWidget *header;

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

	/* desktop -> addons */
	aw1 = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (before)));
	aw2 = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (row)));
	if (!gs_shell_installed_is_addon_id_kind (gs_app_widget_get_app (aw1)) &&
	    gs_shell_installed_is_addon_id_kind (gs_app_widget_get_app (aw2))) {
		/* TRANSLATORS: This is the header dividing the normal
		 * applications and the addons */
		header = gtk_label_new (_("Add-ons"));
		g_object_set (header,
			      "xalign", 0.0,
			      NULL);
		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);
}

/**
 * gs_shell_installed_pending_apps_changed_cb:
 */
static void
gs_shell_installed_pending_apps_changed_cb (GsPluginLoader *plugin_loader,
					    GsShellInstalled *shell_installed)
{
	GPtrArray *pending;
	GsApp *app;
	GtkWidget *widget;
	gchar *label;
	guint i;

	widget = GTK_WIDGET (gtk_builder_get_object (shell_installed->priv->builder,
						     "button_installed_counter"));
	pending = gs_plugin_loader_get_pending (plugin_loader);
	if (pending->len == 0) {
		gtk_widget_hide (widget);
	} else {
		gtk_widget_show (widget);
		label = g_strdup_printf ("%d", pending->len);
		gtk_label_set_label (GTK_LABEL (widget), label);
		g_free (label);
	}
	for (i = 0; i < pending->len; i++) {
		app = GS_APP (g_ptr_array_index (pending, i));
		/* Sort installing apps above removing and
		 * installed apps. Be careful not to add
		 * pending apps more than once.
		 */
		if (gs_app_get_state (app) == GS_APP_STATE_QUEUED) {
			if (gs_app_get_install_date (app) != INSTALL_DATE_QUEUED) {
				gs_app_set_install_date (app, INSTALL_DATE_QUEUED);
				gs_shell_installed_add_app (shell_installed, app);
			}
		} else if (gs_app_get_state (app) == GS_APP_STATE_INSTALLING) {
			if (gs_app_get_install_date (app) != INSTALL_DATE_INSTALLING) {
				gs_app_set_install_date (app, INSTALL_DATE_INSTALLING);
				gs_shell_installed_add_app (shell_installed, app);
			}
		}
	}

	g_ptr_array_unref (pending);
}

static void
set_selection_mode (GsShellInstalled *shell_installed, gboolean selection_mode)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GList *children, *l;
	GtkWidget *row;
	GtkWidget *app_widget;
	GtkWidget *header;
	GtkWidget *widget;
	GtkStyleContext *context;
	
	if (priv->selection_mode == selection_mode)
		return;

	priv->selection_mode = selection_mode;

	header = GTK_WIDGET (gtk_builder_get_object (priv->builder, "header"));
	context = gtk_widget_get_style_context (header);
	if (priv->selection_mode) {
		gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header), FALSE);
		gtk_style_context_add_class (context, "selection-mode");
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_select"));
		gtk_button_set_image (GTK_BUTTON (widget), NULL);
		gtk_button_set_label (GTK_BUTTON (widget), _("_Cancel"));
		gtk_button_set_use_underline (GTK_BUTTON (widget), TRUE);
		gtk_widget_show (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "buttonbox_main"));
		gtk_widget_hide (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "header_selection_menu_button"));
		gtk_widget_show (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "header_selection_label"));
		gtk_label_set_label (GTK_LABEL (widget), _("Click on items to select them"));
	} else {
		gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header), TRUE);
		gtk_style_context_remove_class (context, "selection-mode");
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_select"));
		gtk_button_set_image (GTK_BUTTON (widget), gtk_image_new_from_icon_name ("object-select-symbolic", GTK_ICON_SIZE_MENU));
		gtk_button_set_label (GTK_BUTTON (widget), NULL);
		gtk_widget_show (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "buttonbox_main"));
		gtk_widget_show (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "header_selection_menu_button"));
		gtk_widget_hide (widget);

		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_add"));
		gtk_widget_hide (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_move"));
		gtk_widget_hide (widget);
		widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_remove"));
		gtk_widget_hide (widget);
	}

	children = gtk_container_get_children (GTK_CONTAINER (priv->list_box_installed));
	for (l = children; l; l = l->next) {
		row = l->data;
		app_widget = gtk_bin_get_child (GTK_BIN (row));
		gs_app_widget_set_selectable (GS_APP_WIDGET (app_widget),
					      priv->selection_mode);
	}
	g_list_free (children);

	gtk_revealer_set_reveal_child (priv->bottom_bar, priv->selection_mode);
}

static void
selection_mode_cb (GtkButton *button, GsShellInstalled *shell_installed)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;

	set_selection_mode (shell_installed, !priv->selection_mode);
}

static GList *
get_selected_apps (GsShellInstalled *shell_installed)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GList *children, *l, *list;

	list = NULL;
	children = gtk_container_get_children (GTK_CONTAINER (priv->list_box_installed));
	for (l = children; l; l = l->next) {
		GtkListBoxRow *row = l->data;
		GsAppWidget *app_widget = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (row)));
		if (gs_app_widget_get_selected (app_widget)) {
			list = g_list_prepend (list, gs_app_widget_get_app (app_widget));	
		}
	}
	g_list_free (children);

	return list;
}

static void
selection_changed (GsShellInstalled *shell_installed)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GsFolders *folders;
	GList *apps, *l;
	GsApp *app;
	gboolean has_folders, has_nonfolders;
	GtkWidget *button;

	folders = gs_folders_get ();
	has_folders = has_nonfolders = FALSE;
	apps = get_selected_apps (shell_installed);
	for (l = apps; l; l = l->next) {
		app = l->data;
		if (gs_folders_get_app_folder (folders,
				 	       gs_app_get_id_full (app),
					       gs_app_get_categories (app))) {
			has_folders = TRUE;
		} else {
			has_nonfolders = TRUE;
		}
	}
	g_list_free (apps);
	g_object_unref (folders);

	button = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_add"));
	gtk_widget_set_visible (button, has_nonfolders);

	button = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_move"));
	gtk_widget_set_visible (button, has_folders && !has_nonfolders);

	button = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_remove"));
	gtk_widget_set_visible (button, has_folders);
}

static void
folder_dialog_done (GsShellInstalled *shell_installed)
{
	set_selection_mode (shell_installed, FALSE);
}

static void
show_folder_dialog (GtkButton *button, GsShellInstalled *shell_installed)
{
	GtkWidget *toplevel;
	GtkWidget *dialog;
	GList *apps;

	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
	apps = get_selected_apps (shell_installed);
	dialog = gs_app_folder_dialog_new (GTK_WINDOW (toplevel), apps);
	g_list_free (apps);
	gtk_window_present (GTK_WINDOW (dialog));
	g_signal_connect_swapped (dialog, "delete-event",
				  G_CALLBACK (folder_dialog_done), shell_installed);
}

static void
remove_folders (GtkButton *button, GsShellInstalled *shell_installed)
{
	GList *apps, *l;
	GsFolders *folders;
	GsApp *app;

	folders = gs_folders_get ();
	apps = get_selected_apps (shell_installed);
	for (l = apps; l; l = l->next) {
		app = l->data;
		gs_folders_set_app_folder (folders,
					   gs_app_get_id_full (app),
					   gs_app_get_categories (app),
					   NULL);
	}
	g_list_free (apps);

	gs_folders_save (folders);
	g_object_unref (folders);

	set_selection_mode (shell_installed, FALSE);
}

static void
select_all_cb (GtkMenuItem *item, GsShellInstalled *shell_installed)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GList *children, *l;

	children = gtk_container_get_children (GTK_CONTAINER (priv->list_box_installed));
	for (l = children; l; l = l->next) {
		GtkListBoxRow *row = l->data;
		GsAppWidget *app_widget = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (row)));
		gs_app_widget_set_selected (app_widget, TRUE);
	}
	g_list_free (children);
}

static void
select_none_cb (GtkMenuItem *item, GsShellInstalled *shell_installed)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GList *children, *l;

	children = gtk_container_get_children (GTK_CONTAINER (priv->list_box_installed));
	for (l = children; l; l = l->next) {
		GtkListBoxRow *row = l->data;
		GsAppWidget *app_widget = GS_APP_WIDGET (gtk_bin_get_child (GTK_BIN (row)));
		gs_app_widget_set_selected (app_widget, FALSE);
	}
	g_list_free (children);
}

/**
 * gs_shell_installed_setup:
 */
void
gs_shell_installed_setup (GsShellInstalled *shell_installed,
			  GsShell *shell,
			  GsPluginLoader *plugin_loader,
			  GtkBuilder *builder,
			  GCancellable *cancellable)
{
	GsShellInstalledPrivate *priv = shell_installed->priv;
	GtkWidget *widget;

	g_return_if_fail (GS_IS_SHELL_INSTALLED (shell_installed));

	priv->shell = shell;
	priv->plugin_loader = g_object_ref (plugin_loader);
	g_signal_connect (priv->plugin_loader, "pending-apps-changed",
			  G_CALLBACK (gs_shell_installed_pending_apps_changed_cb),
			  shell_installed);

	priv->builder = g_object_ref (builder);
	priv->cancellable = g_object_ref (cancellable);

	/* setup installed */
	priv->list_box_installed = GTK_LIST_BOX (gtk_builder_get_object (priv->builder, "list_box_install"));
	g_signal_connect (priv->list_box_installed, "row-activated",
			  G_CALLBACK (gs_shell_installed_app_widget_activated_cb), shell_installed);
	gtk_list_box_set_header_func (priv->list_box_installed,
				      gs_shell_installed_list_header_func,
				      shell_installed, NULL);
	gtk_list_box_set_sort_func (priv->list_box_installed,
				    gs_shell_installed_sort_func,
				    shell_installed, NULL);

	priv->bottom_bar = GTK_REVEALER (gtk_builder_get_object (priv->builder, "bottom_install"));

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_add"));
	g_signal_connect (widget, "clicked",
			  G_CALLBACK (show_folder_dialog), shell_installed);
	
	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_move"));
	g_signal_connect (widget, "clicked",
			  G_CALLBACK (show_folder_dialog), shell_installed);
	
	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_folder_remove"));
	g_signal_connect (widget, "clicked",
			  G_CALLBACK (remove_folders), shell_installed);

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_select"));
	g_signal_connect (widget, "clicked",
			  G_CALLBACK (selection_mode_cb), shell_installed);

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "button_select"));
	gtk_button_set_image (GTK_BUTTON (widget), gtk_image_new_from_icon_name ("object-select-symbolic", GTK_ICON_SIZE_MENU));
	gtk_button_set_label (GTK_BUTTON (widget), NULL);

	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "select_all_menuitem"));
	g_signal_connect (widget, "activate",
			  G_CALLBACK (select_all_cb), shell_installed);
	widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "select_none_menuitem"));
	g_signal_connect (widget, "activate",
			  G_CALLBACK (select_none_cb), shell_installed);
}

/**
 * gs_shell_installed_class_init:
 **/
static void
gs_shell_installed_class_init (GsShellInstalledClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gs_shell_installed_finalize;

	g_type_class_add_private (klass, sizeof (GsShellInstalledPrivate));
}

/**
 * gs_shell_installed_init:
 **/
static void
gs_shell_installed_init (GsShellInstalled *shell_installed)
{
	shell_installed->priv = GS_SHELL_INSTALLED_GET_PRIVATE (shell_installed);
	shell_installed->priv->sizegroup_image = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
	shell_installed->priv->sizegroup_name = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
}

/**
 * gs_shell_installed_finalize:
 **/
static void
gs_shell_installed_finalize (GObject *object)
{
	GsShellInstalled *shell_installed = GS_SHELL_INSTALLED (object);
	GsShellInstalledPrivate *priv = shell_installed->priv;

	g_object_unref (priv->sizegroup_image);
	g_object_unref (priv->sizegroup_name);

	g_object_unref (priv->builder);
	g_object_unref (priv->plugin_loader);
	g_object_unref (priv->cancellable);

	G_OBJECT_CLASS (gs_shell_installed_parent_class)->finalize (object);
}

/**
 * gs_shell_installed_new:
 **/
GsShellInstalled *
gs_shell_installed_new (void)
{
	GsShellInstalled *shell_installed;
	shell_installed = g_object_new (GS_TYPE_SHELL_INSTALLED, NULL);
	return GS_SHELL_INSTALLED (shell_installed);
}

/* vim: set noexpandtab: */
