planner-property-model.c 4.47 KB
Newer Older
Richard Hult's avatar
Richard Hult committed
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 CodeFactory AB
 * Copyright (C) 2002 Richard Hult <richard@imendio.com>
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 * Copyright (C) 2002 Alvaro del Castillo <acs@barrapunto.com>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <string.h>
#include <libgnome/gnome-i18n.h>
#include <gtk/gtkliststore.h>
#include <libplanner/mrp-property.h>
#include "planner-property-model.h"


static void
property_model_property_added_cb (MrpProject   *project,
				  GType         object_type,
				  MrpProperty  *property,
				  GtkListStore *store)
{
	GtkTreeIter     iter;
	MrpPropertyType type;

	type = mrp_property_get_property_type (property);
	
	gtk_list_store_append (store, &iter);

	gtk_list_store_set (store, &iter,
			    COL_NAME, mrp_property_get_name (property),
			    COL_LABEL, mrp_property_get_label (property),
			    COL_TYPE, mrp_property_type_as_string (type),
			    COL_PROPERTY, property,
			    -1);
}

static gboolean
property_model_property_removed_helper (GtkTreeModel *model,
					GtkTreePath  *path,
					GtkTreeIter  *iter,
					gpointer      data)
{
	gchar *name;
	
	gtk_tree_model_get (model, iter,
			    COL_NAME, &name,
			    -1);
	
	if (!strcmp (name, data)) {
		gtk_list_store_remove (GTK_LIST_STORE (model), iter);
		
		g_free (name);
		return TRUE;
	}

	g_free (name);

	return FALSE;
}

static void
property_model_property_removed_cb (MrpProject   *project,
				    MrpProperty  *property,
				    GtkTreeModel *model)
{
	gtk_tree_model_foreach (model,
				property_model_property_removed_helper,
				(gchar *)mrp_property_get_name (property));
}

static gboolean 
property_model_property_changed_helper (GtkTreeModel *model,
					GtkTreePath  *path,
					GtkTreeIter  *iter,
					gpointer      data)
{
	MrpProperty *property;
	const gchar *property_name;
	gchar       *name;

	g_return_val_if_fail (data != NULL, FALSE);
	
	property      = MRP_PROPERTY (data);
	property_name = mrp_property_get_name (property);
	
	gtk_tree_model_get (model, iter,
			    COL_NAME, &name,
			    -1);
	
	if (!strcmp (name, property_name)) {
		gtk_list_store_set (GTK_LIST_STORE (model), iter,
				    COL_LABEL, mrp_property_get_label (property),
				    -1);
		return TRUE;
	}

	return FALSE;
}

static void
property_model_property_changed_cb (MrpProject   *project,
				    MrpProperty  *property,
				    GtkTreeModel *model)
{
	/* Find the iter and update it */
	gtk_tree_model_foreach (model, 
				property_model_property_changed_helper,
				property);
}

GtkTreeModel *
planner_property_model_new (MrpProject *project,
		       GType       owner_type)
{
	GtkListStore    *store;
	GList           *properties, *l;
	MrpProperty     *property;
	MrpPropertyType  type;
	GtkTreeIter      iter;
	
	store = gtk_list_store_new (5,
				    G_TYPE_STRING,
				    G_TYPE_STRING,
				    G_TYPE_STRING,
				    G_TYPE_POINTER,
				    G_TYPE_POINTER);
	
	properties = mrp_project_get_properties_from_type (project, 
							   owner_type);

	for (l = properties; l; l = l->next) {
		property = l->data;

		type = mrp_property_get_property_type (property);
		
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store,
				    &iter,
				    COL_NAME, mrp_property_get_name (property),
				    COL_LABEL, mrp_property_get_label (property),
				    COL_TYPE, mrp_property_type_as_string (type),
				    COL_PROPERTY, property,
				    -1);
	}

	g_signal_connect (project,
			  "property_added",
			  G_CALLBACK (property_model_property_added_cb),
			  store);
	
	g_signal_connect (project,
			  "property_removed",
			  G_CALLBACK (property_model_property_removed_cb),
			  store);

	g_signal_connect (project,
			  "property_changed",
			  G_CALLBACK (property_model_property_changed_cb),
			  store);

	return GTK_TREE_MODEL (store);
}