planner-assignment-model.c 10.9 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
/* -*- 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>
 *
 * 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
Josué Abarca's avatar
Josué Abarca committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Richard Hult's avatar
Richard Hult committed
21 22 23
 */

#include <config.h>
24
#include <gtk/gtk.h>
25
#include <glib/gi18n.h>
Richard Hult's avatar
Richard Hult committed
26 27 28 29
#include <libplanner/mrp-assignment.h>
#include <libplanner/mrp-resource.h>
#include "planner-assignment-model.h"

Richard Hult's avatar
Richard Hult committed
30
struct _PlannerAssignmentModelPriv {
Richard Hult's avatar
Richard Hult committed
31 32 33 34 35 36
	MrpProject *project;
	MrpTask    *task;
};

#define G_LIST(x) ((GList *) x)

Richard Hult's avatar
Richard Hult committed
37 38
static void       mam_init                  (PlannerAssignmentModel      *model);
static void       mam_class_init            (PlannerAssignmentModelClass *class);
Richard Hult's avatar
Richard Hult committed
39 40 41 42 43 44 45 46 47 48 49 50

static void       mam_finalize              (GObject              *object);
static gint       mam_get_n_columns         (GtkTreeModel         *treemodel);
static GType      mam_get_column_type       (GtkTreeModel         *treemodel,
					     gint                  index);

static void       mam_get_value             (GtkTreeModel         *treemodel,
					     GtkTreeIter          *iter,
					     gint                  column,
					     GValue               *value);
static void       mam_assignment_changed_cb (MrpTask              *task,
					     MrpAssignment        *assignment,
Richard Hult's avatar
Richard Hult committed
51
					     PlannerAssignmentModel    *model);
52
static void       mam_resource_added_cb     (MrpProject           *project,
Richard Hult's avatar
Richard Hult committed
53
					     MrpResource          *assignment,
Richard Hult's avatar
Richard Hult committed
54
					     PlannerAssignmentModel    *model);
55
static void       mam_resource_removed_cb   (MrpProject           *project,
Richard Hult's avatar
Richard Hult committed
56
					     MrpResource          *resource,
Richard Hult's avatar
Richard Hult committed
57
					     PlannerAssignmentModel    *model);
Richard Hult's avatar
Richard Hult committed
58 59
static void       mam_resource_notify_cb    (MrpResource          *resource,
					     GParamSpec           *pspec,
Richard Hult's avatar
Richard Hult committed
60
					     PlannerAssignmentModel    *model);
Richard Hult's avatar
Richard Hult committed
61 62


Richard Hult's avatar
Richard Hult committed
63
static PlannerListModelClass *parent_class = NULL;
Richard Hult's avatar
Richard Hult committed
64 65 66 67 68 69


GType
planner_assignment_model_get_type (void)
{
        static GType type = 0;
70

Richard Hult's avatar
Richard Hult committed
71 72 73
        if (!type) {
                static const GTypeInfo info =
                        {
Richard Hult's avatar
Richard Hult committed
74
                                sizeof (PlannerAssignmentModelClass),
Richard Hult's avatar
Richard Hult committed
75 76 77 78 79
                                NULL,		/* base_init */
                                NULL,		/* base_finalize */
                                (GClassInitFunc) mam_class_init,
                                NULL,		/* class_finalize */
                                NULL,		/* class_data */
Richard Hult's avatar
Richard Hult committed
80
                                sizeof (PlannerAssignmentModel),
Richard Hult's avatar
Richard Hult committed
81 82 83 84
                                0,
                                (GInstanceInitFunc) mam_init,
                        };

Richard Hult's avatar
Richard Hult committed
85 86
                type = g_type_register_static (PLANNER_TYPE_LIST_MODEL,
					       "PlannerAssignmentModel",
Richard Hult's avatar
Richard Hult committed
87 88
					       &info, 0);
        }
89

Richard Hult's avatar
Richard Hult committed
90 91 92 93
        return type;
}

static void
Richard Hult's avatar
Richard Hult committed
94
mam_class_init (PlannerAssignmentModelClass *klass)
Richard Hult's avatar
Richard Hult committed
95 96
{
        GObjectClass     *object_class;
Richard Hult's avatar
Richard Hult committed
97
	PlannerListModelClass *lm_class;
98

Richard Hult's avatar
Richard Hult committed
99 100
        parent_class = g_type_class_peek_parent (klass);
        object_class = G_OBJECT_CLASS (klass);
Richard Hult's avatar
Richard Hult committed
101
	lm_class     = PLANNER_LIST_MODEL_CLASS (klass);
102

Richard Hult's avatar
Richard Hult committed
103 104 105 106 107 108 109 110
        object_class->finalize = mam_finalize;

        lm_class->get_n_columns   = mam_get_n_columns;
        lm_class->get_column_type = mam_get_column_type;
        lm_class->get_value       = mam_get_value;
}

static void
Richard Hult's avatar
Richard Hult committed
111
mam_init (PlannerAssignmentModel *model)
Richard Hult's avatar
Richard Hult committed
112
{
Richard Hult's avatar
Richard Hult committed
113
        PlannerAssignmentModelPriv *priv;
114

Richard Hult's avatar
Richard Hult committed
115
        priv = g_new0 (PlannerAssignmentModelPriv, 1);
116

Richard Hult's avatar
Richard Hult committed
117 118 119 120 121 122 123 124
	priv->project = NULL;

        model->priv = priv;
}

static void
mam_finalize (GObject *object)
{
Richard Hult's avatar
Richard Hult committed
125
	PlannerAssignmentModel *model = PLANNER_ASSIGNMENT_MODEL (object);
Richard Hult's avatar
Richard Hult committed
126 127 128 129 130 131 132 133 134 135 136 137

        if (model->priv) {
		if (model->priv->project) {
			g_object_unref (model->priv->project);
		}
		if (model->priv->task) {
			g_object_unref (model->priv->task);
		}

                g_free (model->priv);
                model->priv = NULL;
        }
138

Richard Hult's avatar
Richard Hult committed
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
	if (G_OBJECT_CLASS (parent_class)->finalize) {
		(* G_OBJECT_CLASS (parent_class)->finalize) (object);
	}
}

static gint
mam_get_n_columns (GtkTreeModel *tree_model)
{
        return NUMBER_OF_RESOURCE_ASSIGNMENT_COLS;
}

static GType
mam_get_column_type (GtkTreeModel *tree_model,
                     gint          column)
{
        switch (column) {
        case RESOURCE_ASSIGNMENT_COL_NAME:
                return G_TYPE_STRING;
        case RESOURCE_ASSIGNMENT_COL_UNITS:
                return G_TYPE_INT;
        case RESOURCE_ASSIGNMENT_COL_COST_STD:
        case RESOURCE_ASSIGNMENT_COL_COST_OVT:
                return G_TYPE_FLOAT;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED:
		return G_TYPE_BOOLEAN;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED_UNITS:
		return G_TYPE_INT;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED_UNITS_EDITABLE:
		return G_TYPE_BOOLEAN;
        default:
		return G_TYPE_INVALID;
        }
}

static void
mam_get_value (GtkTreeModel *tree_model,
               GtkTreeIter  *iter,
               gint          column,
               GValue       *value)
178
{
Richard Hult's avatar
Richard Hult committed
179
	PlannerAssignmentModel *model;
Richard Hult's avatar
Richard Hult committed
180 181 182 183 184
	MrpAssignment     *assignment;
	MrpResource       *resource;
        gchar             *str;
        gint               units;
        gfloat             rate;
185

Richard Hult's avatar
Richard Hult committed
186
        g_return_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (tree_model));
Richard Hult's avatar
Richard Hult committed
187 188
        g_return_if_fail (iter != NULL);

Richard Hult's avatar
Richard Hult committed
189
	model    = PLANNER_ASSIGNMENT_MODEL (tree_model);
Richard Hult's avatar
Richard Hult committed
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
	resource = MRP_RESOURCE (G_LIST(iter->user_data)->data);

        switch (column) {
        case RESOURCE_ASSIGNMENT_COL_NAME:
                g_object_get (resource, "name", &str, NULL);
		if (str == NULL) {
			str = g_strdup ("");
		}
                g_value_init (value, G_TYPE_STRING);
                g_value_set_string (value, str);
                g_free (str);
                break;
        case RESOURCE_ASSIGNMENT_COL_UNITS:
                g_object_get (resource, "units", &units, NULL);
                g_value_init (value, G_TYPE_INT);
                g_value_set_int (value, units);
                break;
        case RESOURCE_ASSIGNMENT_COL_COST_STD:
                mrp_object_get (resource, "cost", &rate, NULL);
                g_value_init (value, G_TYPE_FLOAT);
                g_value_set_int (value, rate);
                break;
        case RESOURCE_ASSIGNMENT_COL_COST_OVT:
                mrp_object_get (resource, "cost_overtime", &rate, NULL);
                g_value_init (value, G_TYPE_FLOAT);
                g_value_set_int (value, rate);
                break;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED:
		g_value_init (value, G_TYPE_BOOLEAN);
		if (mrp_task_get_assignment (model->priv->task, resource)) {
			g_value_set_boolean (value, TRUE);
		} else {
			g_value_set_boolean (value, FALSE);
		}
		break;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED_UNITS:
		g_value_init (value, G_TYPE_INT);
		assignment = mrp_task_get_assignment (model->priv->task, resource);
		if (assignment) {
			g_value_set_int (value, mrp_assignment_get_units (assignment));
		} else {
			g_value_set_int (value, 0);
		}
		break;
	case RESOURCE_ASSIGNMENT_COL_ASSIGNED_UNITS_EDITABLE:
		g_value_init (value, G_TYPE_BOOLEAN);
		assignment = mrp_task_get_assignment (model->priv->task, resource);
		if (assignment) {
			MrpTaskSched sched;
239

Richard Hult's avatar
Richard Hult committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
			g_object_get (model->priv->task, "sched", &sched, NULL);
			g_value_set_boolean (value, sched == MRP_TASK_SCHED_FIXED_WORK);
		} else {
			g_value_set_boolean (value, FALSE);
		}
		break;
        default:
                g_warning ("Bad column %d requested", column);
        }
}

static void
mam_assignment_changed_cb (MrpTask           *task,
			   MrpAssignment     *assignment,
Richard Hult's avatar
Richard Hult committed
254
			   PlannerAssignmentModel *model)
Richard Hult's avatar
Richard Hult committed
255 256
{
	MrpResource *resource;
257

Richard Hult's avatar
Richard Hult committed
258
	g_return_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (model));
Richard Hult's avatar
Richard Hult committed
259
	g_return_if_fail (MRP_IS_ASSIGNMENT (assignment));
260

Richard Hult's avatar
Richard Hult committed
261 262
	resource = mrp_assignment_get_resource (assignment);

Richard Hult's avatar
Richard Hult committed
263
	planner_list_model_update (PLANNER_LIST_MODEL (model), MRP_OBJECT (resource));
Richard Hult's avatar
Richard Hult committed
264 265 266
}

static void
267
mam_resource_added_cb (MrpProject        *project,
Richard Hult's avatar
Richard Hult committed
268
		       MrpResource       *resource,
Richard Hult's avatar
Richard Hult committed
269
		       PlannerAssignmentModel *model)
Richard Hult's avatar
Richard Hult committed
270
{
Richard Hult's avatar
Richard Hult committed
271
	g_return_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (model));
Richard Hult's avatar
Richard Hult committed
272 273
	g_return_if_fail (MRP_IS_RESOURCE (resource));

Richard Hult's avatar
Richard Hult committed
274
	planner_list_model_append (PLANNER_LIST_MODEL (model), MRP_OBJECT (resource));
275 276

	g_signal_connect_object (resource,
Richard Hult's avatar
Richard Hult committed
277 278 279 280 281 282
				 "notify",
				 G_CALLBACK (mam_resource_notify_cb),
				 model, 0);
}

static void
283
mam_resource_removed_cb (MrpProject        *project,
Richard Hult's avatar
Richard Hult committed
284
			 MrpResource       *resource,
Richard Hult's avatar
Richard Hult committed
285
			 PlannerAssignmentModel *model)
Richard Hult's avatar
Richard Hult committed
286
{
Richard Hult's avatar
Richard Hult committed
287
	g_return_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (model));
Richard Hult's avatar
Richard Hult committed
288 289
	g_return_if_fail (MRP_IS_RESOURCE (resource));

Richard Hult's avatar
Richard Hult committed
290
	planner_list_model_remove (PLANNER_LIST_MODEL (model), MRP_OBJECT (resource));
291 292 293 294

	g_signal_handlers_disconnect_by_func (resource,
                                              mam_resource_notify_cb,
                                              model);
Richard Hult's avatar
Richard Hult committed
295 296 297 298 299
}

static void
mam_resource_notify_cb (MrpResource       *resource,
			GParamSpec        *pspec,
Richard Hult's avatar
Richard Hult committed
300
			PlannerAssignmentModel *model)
Richard Hult's avatar
Richard Hult committed
301
{
Richard Hult's avatar
Richard Hult committed
302
	g_return_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (model));
Richard Hult's avatar
Richard Hult committed
303
	g_return_if_fail (MRP_IS_RESOURCE (resource));
304

Richard Hult's avatar
Richard Hult committed
305
	planner_list_model_update (PLANNER_LIST_MODEL (model), MRP_OBJECT (resource));
Richard Hult's avatar
Richard Hult committed
306 307
}

Richard Hult's avatar
Richard Hult committed
308
PlannerAssignmentModel *
Richard Hult's avatar
Richard Hult committed
309 310
planner_assignment_model_new (MrpTask *task)
{
Richard Hult's avatar
Richard Hult committed
311 312
        PlannerAssignmentModel     *model;
        PlannerAssignmentModelPriv *priv;
Richard Hult's avatar
Richard Hult committed
313 314
	GList                 *node;
	GList                 *resources;
315

Richard Hult's avatar
Richard Hult committed
316
        model = g_object_new (PLANNER_TYPE_ASSIGNMENT_MODEL, NULL);
317

Richard Hult's avatar
Richard Hult committed
318
	g_return_val_if_fail (PLANNER_IS_ASSIGNMENT_MODEL (model), NULL);
Richard Hult's avatar
Richard Hult committed
319 320 321 322 323 324 325

        priv = model->priv;

        priv->task    = g_object_ref (task);
	g_object_get (priv->task, "project", &priv->project, NULL);

	resources = mrp_project_get_resources (priv->project);
Richard Hult's avatar
Richard Hult committed
326
	planner_list_model_set_data (PLANNER_LIST_MODEL (model), resources);
Richard Hult's avatar
Richard Hult committed
327 328 329 330 331 332 333 334 335 336 337 338

	for (node = resources; node; node = node->next) {
		g_signal_connect_object (node->data,
					 "notify",
					 G_CALLBACK (mam_resource_notify_cb),
					 model, 0);
	}

	g_signal_connect_object (priv->task,
				 "assignment_added",
				 G_CALLBACK (mam_assignment_changed_cb),
				 model, 0);
339

Richard Hult's avatar
Richard Hult committed
340 341 342 343 344
	g_signal_connect_object (priv->task,
				 "assignment_removed",
				 G_CALLBACK (mam_assignment_changed_cb),
				 model, 0);

345 346 347
	g_signal_connect_object (priv->project,
				 "resource_added",
				 G_CALLBACK (mam_resource_added_cb),
Richard Hult's avatar
Richard Hult committed
348 349
				 model, 0);

350 351 352
	g_signal_connect_object (priv->project,
				 "resource_removed",
				 G_CALLBACK (mam_resource_removed_cb),
Richard Hult's avatar
Richard Hult committed
353 354 355 356
				 model, 0);

        return model;
}