2014-05-15 23:34:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <glib/gi18n-lib.h>
|
2014-07-12 03:14:04 +00:00
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
#include "resource-list.h"
|
|
|
|
|
2014-07-12 03:45:18 +00:00
|
|
|
#include "gtklabel.h"
|
|
|
|
#include "gtkstack.h"
|
2014-07-12 03:14:04 +00:00
|
|
|
#include "gtktextbuffer.h"
|
|
|
|
#include "gtktreestore.h"
|
|
|
|
#include "gtktreeselection.h"
|
|
|
|
|
2014-10-11 04:58:47 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_CLOSE_DETAILS_BUTTON
|
|
|
|
};
|
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLUMN_NAME,
|
2014-07-19 05:20:28 +00:00
|
|
|
COLUMN_PATH,
|
|
|
|
COLUMN_COUNT,
|
|
|
|
COLUMN_SIZE
|
2014-05-15 23:34:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkInspectorResourceListPrivate
|
|
|
|
{
|
|
|
|
GtkTreeStore *model;
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkWidget *image;
|
|
|
|
GtkWidget *content;
|
2014-10-11 00:20:02 +00:00
|
|
|
GtkWidget *name_label;
|
2014-07-19 05:20:28 +00:00
|
|
|
GtkWidget *type;
|
2014-05-19 11:10:33 +00:00
|
|
|
GtkWidget *type_label;
|
|
|
|
GtkWidget *size_label;
|
|
|
|
GtkWidget *info_grid;
|
2014-10-11 00:20:02 +00:00
|
|
|
GtkWidget *stack;
|
2014-10-11 03:25:07 +00:00
|
|
|
GtkWidget *tree;
|
2014-10-11 04:58:47 +00:00
|
|
|
GtkWidget *close_details_button;
|
2014-10-10 23:15:02 +00:00
|
|
|
GtkTreeViewColumn *count_column;
|
|
|
|
GtkCellRenderer *count_renderer;
|
|
|
|
GtkTreeViewColumn *size_column;
|
|
|
|
GtkCellRenderer *size_renderer;
|
2014-05-15 23:34:38 +00:00
|
|
|
};
|
|
|
|
|
2014-10-11 04:58:47 +00:00
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorResourceList, gtk_inspector_resource_list, GTK_TYPE_BOX)
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_resources_recurse (GtkInspectorResourceList *sl,
|
|
|
|
GtkTreeIter *parent,
|
2014-07-19 05:20:28 +00:00
|
|
|
const gchar *path,
|
|
|
|
gint *count_out,
|
|
|
|
gsize *size_out)
|
2014-05-15 23:34:38 +00:00
|
|
|
{
|
|
|
|
gchar **names;
|
|
|
|
gint i;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
names = g_resources_enumerate_children (path, 0, NULL);
|
|
|
|
for (i = 0; names[i]; i++)
|
|
|
|
{
|
|
|
|
gint len;
|
|
|
|
gchar *p;
|
|
|
|
gboolean has_slash;
|
2014-07-19 05:20:28 +00:00
|
|
|
gint count;
|
|
|
|
gsize size;
|
2014-05-15 23:34:38 +00:00
|
|
|
|
|
|
|
p = g_strconcat (path, names[i], NULL);
|
|
|
|
|
|
|
|
len = strlen (names[i]);
|
|
|
|
has_slash = names[i][len - 1] == '/';
|
|
|
|
|
|
|
|
if (has_slash)
|
|
|
|
names[i][len - 1] = '\0';
|
|
|
|
|
|
|
|
gtk_tree_store_append (sl->priv->model, &iter, parent);
|
|
|
|
gtk_tree_store_set (sl->priv->model, &iter,
|
|
|
|
COLUMN_NAME, names[i],
|
|
|
|
COLUMN_PATH, p,
|
|
|
|
-1);
|
|
|
|
|
2014-07-19 05:20:28 +00:00
|
|
|
count = 0;
|
|
|
|
size = 0;
|
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
if (has_slash)
|
2014-07-19 05:20:28 +00:00
|
|
|
{
|
|
|
|
load_resources_recurse (sl, &iter, p, &count, &size);
|
2014-10-10 23:15:02 +00:00
|
|
|
*count_out += count;
|
|
|
|
*size_out += size;
|
2014-07-19 05:20:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-10 23:15:02 +00:00
|
|
|
count = 0;
|
2014-07-19 05:20:28 +00:00
|
|
|
g_resources_get_info (p, 0, &size, NULL, NULL);
|
2014-10-10 23:15:02 +00:00
|
|
|
*count_out += 1;
|
|
|
|
*size_out += size;
|
2014-07-19 05:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_store_set (sl->priv->model, &iter,
|
|
|
|
COLUMN_COUNT, count,
|
|
|
|
COLUMN_SIZE, size,
|
|
|
|
-1);
|
2014-05-15 23:34:38 +00:00
|
|
|
|
|
|
|
g_free (p);
|
|
|
|
}
|
|
|
|
g_strfreev (names);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
static gboolean
|
|
|
|
populate_details (GtkInspectorResourceList *rl,
|
|
|
|
GtkTreePath *tree_path)
|
2014-05-15 23:34:38 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
2014-10-11 00:20:02 +00:00
|
|
|
gchar *path;
|
|
|
|
gchar *name;
|
|
|
|
GBytes *bytes;
|
|
|
|
gchar *type;
|
|
|
|
gconstpointer data;
|
|
|
|
gint count;
|
|
|
|
gsize size;
|
|
|
|
GError *error = NULL;
|
|
|
|
gchar *markup;
|
|
|
|
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter (GTK_TREE_MODEL (rl->priv->model), &iter, tree_path);
|
|
|
|
|
|
|
|
gtk_tree_model_get (GTK_TREE_MODEL (rl->priv->model), &iter,
|
|
|
|
COLUMN_PATH, &path,
|
|
|
|
COLUMN_NAME, &name,
|
|
|
|
COLUMN_COUNT, &count,
|
|
|
|
COLUMN_SIZE, &size,
|
|
|
|
-1);
|
|
|
|
|
|
|
|
if (g_str_has_suffix (path, "/"))
|
|
|
|
{
|
|
|
|
g_free (path);
|
|
|
|
g_free (name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
markup = g_strconcat ("<span face='Monospace' size='small'>", path, "</span>", NULL);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (rl->priv->name_label), markup);
|
|
|
|
g_free (markup);
|
|
|
|
|
|
|
|
bytes = g_resources_lookup_data (path, 0, &error);
|
|
|
|
if (bytes == NULL)
|
2014-05-15 23:34:38 +00:00
|
|
|
{
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_text_buffer_set_text (rl->priv->buffer, error->message, -1);
|
|
|
|
g_error_free (error);
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gchar *text;
|
2014-05-19 11:10:33 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
data = g_bytes_get_data (bytes, &size);
|
|
|
|
type = g_content_type_guess (name, data, size, NULL);
|
2014-07-19 05:20:28 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
text = g_content_type_get_description (type);
|
|
|
|
gtk_label_set_text (GTK_LABEL (rl->priv->type_label), text);
|
|
|
|
g_free (text);
|
2014-07-19 05:20:28 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
text = g_format_size (size);
|
|
|
|
gtk_label_set_text (GTK_LABEL (rl->priv->size_label), text);
|
|
|
|
g_free (text);
|
2014-07-19 05:20:28 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
if (g_content_type_is_a (type, "text/*"))
|
|
|
|
{
|
|
|
|
gtk_text_buffer_set_text (rl->priv->buffer, data, -1);
|
2014-05-15 23:34:38 +00:00
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
|
|
|
|
}
|
2014-10-11 00:20:02 +00:00
|
|
|
else if (g_content_type_is_a (type, "image/*"))
|
2014-05-15 23:34:38 +00:00
|
|
|
{
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_image_set_from_resource (GTK_IMAGE (rl->priv->image), path);
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "image");
|
2014-05-15 23:34:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_text_buffer_set_text (rl->priv->buffer, "", 0);
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
|
|
|
|
}
|
2014-05-19 11:10:33 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
g_free (type);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
}
|
2014-05-19 11:10:33 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
g_free (path);
|
|
|
|
g_free (name);
|
2014-05-19 11:10:33 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_activated (GtkTreeView *treeview,
|
|
|
|
GtkTreePath *path,
|
|
|
|
GtkTreeViewColumn *column,
|
|
|
|
GtkInspectorResourceList *sl)
|
|
|
|
{
|
|
|
|
if (populate_details (sl, path))
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (sl->priv->stack), "details");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_details (GtkWidget *button,
|
|
|
|
GtkInspectorResourceList *sl)
|
|
|
|
{
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (sl->priv->stack), "list");
|
2014-05-15 23:34:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 04:58:47 +00:00
|
|
|
static void
|
|
|
|
visible_child_name_changed (GObject *obj, GParamSpec *pspec, GtkInspectorResourceList *sl)
|
|
|
|
{
|
|
|
|
const gchar *child;
|
|
|
|
gboolean resources_visible;
|
|
|
|
gboolean resource_details_visible;
|
|
|
|
|
|
|
|
child = gtk_stack_get_visible_child_name (GTK_STACK (gtk_widget_get_parent (GTK_WIDGET (sl))));
|
|
|
|
resources_visible = g_strcmp0 (child, "resources") == 0;
|
|
|
|
child = gtk_stack_get_visible_child_name (GTK_STACK (sl->priv->stack));
|
|
|
|
resource_details_visible = g_strcmp0 (child, "details") == 0;
|
|
|
|
|
|
|
|
gtk_widget_set_visible (sl->priv->close_details_button, resources_visible && resource_details_visible);
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
static void
|
|
|
|
load_resources (GtkInspectorResourceList *sl)
|
|
|
|
{
|
2014-07-19 05:20:28 +00:00
|
|
|
gint count = 0;
|
|
|
|
gsize size = 0;
|
2014-05-15 23:34:38 +00:00
|
|
|
|
2014-07-19 05:20:28 +00:00
|
|
|
load_resources_recurse (sl, NULL, "/", &count, &size);
|
2014-05-15 23:34:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:15:02 +00:00
|
|
|
static void
|
|
|
|
count_data_func (GtkTreeViewColumn *col,
|
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gint count;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, COLUMN_COUNT, &count, -1);
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
text = g_strdup_printf ("%d", count);
|
|
|
|
g_object_set (cell, "text", text, NULL);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_object_set (cell, "text", "", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
size_data_func (GtkTreeViewColumn *col,
|
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gint size;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, COLUMN_SIZE, &size, -1);
|
|
|
|
text = g_format_size (size);
|
|
|
|
g_object_set (cell, "text", text, NULL);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
static void
|
|
|
|
on_map (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkInspectorResourceList *sl = GTK_INSPECTOR_RESOURCE_LIST (widget);
|
|
|
|
|
2014-10-11 03:25:07 +00:00
|
|
|
gtk_tree_view_expand_all (GTK_TREE_VIEW (sl->priv->tree));
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (sl->priv->stack), "list");
|
|
|
|
}
|
|
|
|
|
2014-10-11 04:58:47 +00:00
|
|
|
static void
|
|
|
|
parent_set (GtkWidget *widget, GtkWidget *old_parent)
|
|
|
|
{
|
|
|
|
if (old_parent)
|
|
|
|
g_signal_handlers_disconnect_by_func (old_parent, visible_child_name_changed, widget);
|
|
|
|
g_signal_connect (gtk_widget_get_parent (widget), "notify::visible-child-name",
|
|
|
|
G_CALLBACK (visible_child_name_changed), widget);
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
static void
|
|
|
|
gtk_inspector_resource_list_init (GtkInspectorResourceList *sl)
|
|
|
|
{
|
|
|
|
sl->priv = gtk_inspector_resource_list_get_instance_private (sl);
|
2014-10-11 04:58:47 +00:00
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (sl));
|
2014-10-11 04:58:47 +00:00
|
|
|
|
2014-10-10 23:15:02 +00:00
|
|
|
gtk_tree_view_column_set_cell_data_func (sl->priv->count_column,
|
|
|
|
sl->priv->count_renderer,
|
|
|
|
count_data_func, sl, NULL);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (sl->priv->size_column,
|
|
|
|
sl->priv->size_renderer,
|
|
|
|
size_data_func, sl, NULL);
|
2014-10-11 04:58:47 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
g_signal_connect (sl, "map", G_CALLBACK (on_map), NULL);
|
2014-10-11 04:58:47 +00:00
|
|
|
g_signal_connect (sl->priv->stack, "notify::visible-child-name",
|
|
|
|
G_CALLBACK (visible_child_name_changed), sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
constructed (GObject *object)
|
|
|
|
{
|
|
|
|
GtkInspectorResourceList *rl = GTK_INSPECTOR_RESOURCE_LIST (object);
|
|
|
|
|
|
|
|
g_signal_connect (rl->priv->close_details_button, "clicked",
|
|
|
|
G_CALLBACK (close_details), rl);
|
|
|
|
|
|
|
|
load_resources (rl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkInspectorResourceList *rl = GTK_INSPECTOR_RESOURCE_LIST (object);
|
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_CLOSE_DETAILS_BUTTON:
|
|
|
|
g_value_take_object (value, rl->priv->close_details_button);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkInspectorResourceList *rl = GTK_INSPECTOR_RESOURCE_LIST (object);
|
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_CLOSE_DETAILS_BUTTON:
|
|
|
|
rl->priv->close_details_button = g_value_get_object (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-15 23:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_inspector_resource_list_class_init (GtkInspectorResourceListClass *klass)
|
|
|
|
{
|
2014-10-11 04:58:47 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2014-05-15 23:34:38 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2014-10-11 04:58:47 +00:00
|
|
|
object_class->get_property = get_property;
|
|
|
|
object_class->set_property = set_property;
|
|
|
|
object_class->constructed = constructed;
|
|
|
|
|
|
|
|
widget_class->parent_set = parent_set;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_CLOSE_DETAILS_BUTTON,
|
|
|
|
g_param_spec_object ("close-details-button", NULL, NULL,
|
|
|
|
GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
2014-05-15 23:34:38 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/resource-list.ui");
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, model);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, buffer);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, content);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, image);
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, name_label);
|
2014-05-19 11:10:33 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, type_label);
|
2014-07-19 05:20:28 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, type);
|
2014-05-19 11:10:33 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, size_label);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, info_grid);
|
2014-10-10 23:15:02 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, count_column);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, count_renderer);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, size_column);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, size_renderer);
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, stack);
|
2014-10-11 03:25:07 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, tree);
|
2014-05-15 23:34:38 +00:00
|
|
|
|
2014-10-11 00:20:02 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, row_activated);
|
2014-05-15 23:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: set et sw=2 ts=2:
|