glade-project.c 140 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1
2
/*
 * Copyright (C) 2001 Ximian, Inc.
3
 * Copyright (C) 2008 Tristan Van Berkom
Jose Maria Celorio's avatar
Jose Maria Celorio committed
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * 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
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
18
19
20
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
21
 *   Tristan Van Berkom <tvb@gnome.org>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
22
23
 */

24
25
#include <config.h>

26
27
28
29
30
31
32
33
34
/**
 * SECTION:glade-project
 * @Short_Description: The Glade document hub and Load/Save interface.
 *
 * This object owns all project objects and is responsable for loading and
 * saving the glade document, you can monitor the project state via this
 * object and its signals.
 */

35
#include <string.h>
36
#include <stdlib.h>
37
#include <glib.h>
38
#include <glib/gi18n-lib.h>
39
#include <glib/gstdio.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
40
41

#include "glade.h"
Chema Celorio's avatar
Chema Celorio committed
42
#include "glade-widget.h"
43
#include "glade-id-allocator.h"
44
#include "glade-app.h"
45
#include "glade-marshallers.h"
46
47
48
#include "glade-catalog.h"

#include "glade-project.h"
49
#include "glade-command.h"
50
#include "glade-name-context.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
51

52
53

#define VALID_ITER(project, iter) ((iter)!= NULL && G_IS_OBJECT ((iter)->user_data) && ((GladeProject*)(project))->priv->stamp == (iter)->stamp)
54

Jose Maria Celorio's avatar
Jose Maria Celorio committed
55
56
enum
{
57
58
59
60
61
62
63
64
65
66
67
  ADD_WIDGET,
  REMOVE_WIDGET,
  WIDGET_NAME_CHANGED,
  SELECTION_CHANGED,
  CLOSE,
  CHANGED,
  PARSE_BEGAN,
  PARSE_FINISHED,
  TARGETS_CHANGED,
  LOAD_PROGRESS,
  LAST_SIGNAL
Jose Maria Celorio's avatar
Jose Maria Celorio committed
68
69
};

70
71
enum
{
72
73
74
75
76
  PROP_0,
  PROP_MODIFIED,
  PROP_HAS_SELECTION,
  PROP_PATH,
  PROP_READ_ONLY,
77
78
  PROP_ADD_ITEM,
  PROP_POINTER_MODE
79
80
};

81
82
struct _GladeProjectPrivate
{
83
84
85
86
  gchar *path;                  /* The full canonical path of the glade file for this project */

  gint unsaved_number;          /* A unique number for this project if it is untitled */

87
88
  GladeWidgetAdaptor *add_item; /* The next item to add to the project.
				 */
89
90
91
92
93
94
95
96
97
98
99
100

  gint stamp;                   /* A a random int per instance of project used to stamp/check the
                                 * GtkTreeIter->stamps */
  GList *tree;                  /* List of toplevel Objects in this projects */
  GList *objects;               /* List of all objects in this project */

  GList *selection;             /* We need to keep the selection in the project
                                 * because we have multiple projects and when the
                                 * user switchs between them, he will probably
                                 * not want to loose the selection. This is a list
                                 * of #GtkWidget items.
                                 */
101
  guint selection_changed_id;
102

103
  GladeNameContext *widget_names; /* Context for uniqueness of names */
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


  GList *undo_stack;            /* A stack with the last executed commands */
  GList *prev_redo_item;        /* Points to the item previous to the redo items */

  GList *first_modification;    /* we record the first modification, so that we
                                 * can set "modification" to FALSE when we
                                 * undo this modification
                                 */

  GtkAccelGroup *accel_group;

  gchar *comment;               /* XML comment, Glade will preserve whatever comment was
                                 * in file, so users can delete or change it.
                                 */

  time_t mtime;                 /* last UTC modification time of file, or 0 if it could not be read */

  GHashTable *target_versions_major;    /* target versions by catalog */
  GHashTable *target_versions_minor;    /* target versions by catalog */

  gchar *resource_path;         /* Indicates where to load resources from for this project 
                                 * (full or relative path, null means project directory).
                                 */

129
  /* Control on the properties dialog to update buttons etc when properties change */
130
131
132
133
134
135
136
137
138
139
140
  GtkWidget *prefs_dialog;
  GtkWidget *project_wide_radio;
  GtkWidget *toplevel_contextual_radio;
  GHashTable *target_radios;

  GtkWidget *resource_default_radio;
  GtkWidget *resource_relative_radio;
  GtkWidget *resource_fullpath_radio;
  GtkWidget *relative_path_entry;
  GtkWidget *full_path_button;

141
142
143
  /* Store preview processes, so we can kill them on close */
  GHashTable *preview_channels;

144
145
146
147
  /* For the loading progress bars ("load-progress" signal) */
  gint progress_step;
  gint progress_full;

148
149
150
151
152
153
154
155
156
157
158
159
  /* Flags */
  guint load_cancel : 1;
  guint first_modification_is_na : 1;  /* indicates that the first_modification item has been lost */
  guint has_selection : 1;       /* Whether the project has a selection */
  guint readonly : 1;            /* A flag that is set if the project is readonly */
  guint loading : 1;             /* A flags that is set when the project is loading */
  guint modified : 1;            /* A flag that is set when a project has unsaved modifications
				  * if this flag is not set we don't have to query
				  * for confirmation after a close or exit is
				  * requested
				  */
  guint pointer_mode : 2;        /* The currently effective GladePointerMode */
160
161
};

162
163
164
165
typedef struct
{
  gchar *stock;
  gchar *filename;
166
167
} StockFilePair;

168
169
170
171
typedef struct
{
  GIOChannel *channel;
  guint watch;
172
173
} ChannelWatchPair;

174

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
GType
glade_pointer_mode_get_type (void)
{
  static GType etype = 0;

  if (etype == 0)
    {
      static const GEnumValue values[] = {
        {GLADE_POINTER_SELECT, "select", "Select widgets"},
        {GLADE_POINTER_ADD_WIDGET, "add", "Add widgets"},
        {GLADE_POINTER_DRAG_RESIZE, "drag-resize", "Drag and resize widgets"},
        {0, NULL, NULL}
      };
      etype = g_enum_register_static ("GladePointerMode", values);
    }
  return etype;
}

193
194
195
static void glade_project_set_target_version (GladeProject * project,
                                              const gchar * catalog,
                                              guint16 major, guint16 minor);
196

197
198
199
200
static void glade_project_target_version_for_adaptor (GladeProject * project,
                                                      GladeWidgetAdaptor *
                                                      adaptor, gint * major,
                                                      gint * minor);
201

202
203
static void glade_project_set_readonly (GladeProject * project,
                                        gboolean readonly);
204
205


206
static gboolean glade_project_verify (GladeProject * project, gboolean saving);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
207
208
static void     glade_project_verify_properties    (GladeWidget        *widget);
static void     glade_project_verify_project_for_ui(GladeProject       *project);
209

210
211
212
213
214
215
216
static void glade_project_verify_adaptor (GladeProject * project,
                                          GladeWidgetAdaptor * adaptor,
                                          const gchar * path_name,
                                          GString * string,
                                          gboolean saving,
                                          gboolean forwidget,
                                          GladeSupportMask * mask);
217

218
static GtkWidget *glade_project_build_prefs_dialog (GladeProject * project);
219

220
221
static void target_button_clicked (GtkWidget * widget, GladeProject * project);
static void update_prefs_for_resource_path (GladeProject * project);
222

223
static guint glade_project_signals[LAST_SIGNAL] = { 0 };
224

225
static GladeIDAllocator *unsaved_number_allocator = NULL;
226

227
static void gtk_tree_model_iface_init (GtkTreeModelIface * iface);
228

229
230
231
static void glade_project_model_get_iter_for_object (GladeProject * project,
                                                     GObject * object,
                                                     GtkTreeIter * iter);
232

233
234
235

#define GLADE_PROJECT_LARGE_PROJECT 40

236
237
G_DEFINE_TYPE_WITH_CODE (GladeProject, glade_project, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
238
                                                gtk_tree_model_iface_init))
239
240


241
242
243
/*******************************************************************
                            GObjectClass
 *******************************************************************/
244
static GladeIDAllocator *get_unsaved_number_allocator (void)
245
{
246
247
248
249
  if (unsaved_number_allocator == NULL)
    unsaved_number_allocator = glade_id_allocator_new ();

  return unsaved_number_allocator;
250
251
}

252
static void
253
glade_project_list_unref (GList * original_list)
254
{
255
256
257
  GList *l;
  for (l = original_list; l; l = l->next)
    g_object_unref (G_OBJECT (l->data));
258

259
260
  if (original_list != NULL)
    g_list_free (original_list);
261
262
}

263
static void
264
unparent_objects_recurse (GladeWidget * widget)
265
{
266
267
  GladeWidget *child;
  GList *children, *list;
268

269
270
271
272
273
274
  /* Unparent all children */
  if ((children = glade_widget_get_children (widget)) != NULL)
    {
      for (list = children; list; list = list->next)
        {
          child = glade_widget_get_from_gobject (list->data);
275

276
          unparent_objects_recurse (child);
277

278
          if (!glade_widget_get_internal (child))
279
280
281
282
            glade_widget_remove_child (widget, child);
        }
      g_list_free (children);
    }
283
284
}

285
static void
286
glade_project_dispose (GObject * object)
287
{
288
289
290
291
292
293
  GladeProject *project = GLADE_PROJECT (object);
  GList *list, *tree;

  /* Emit close signal */
  g_signal_emit (object, glade_project_signals[CLOSE], 0);

294
295
296
297
  if (project->priv->selection_changed_id > 0)
    project->priv->selection_changed_id = 
      (g_source_remove (project->priv->selection_changed_id), 0);

298
  glade_project_selection_clear (project, TRUE);
299

300
301
  glade_project_list_unref (project->priv->undo_stack);
  project->priv->undo_stack = NULL;
302

303
304
305
306
307
  /* Remove objects from the project */
  tree = g_list_copy (project->priv->tree);
  for (list = tree; list; list = list->next)
    {
      GladeWidget *gwidget = glade_widget_get_from_gobject (list->data);
308

309
310
311
      unparent_objects_recurse (gwidget);
    }
  g_list_free (tree);
312

313
314
315
316
  while (project->priv->tree)
    glade_project_remove_object (project, project->priv->tree->data);

  while (project->priv->objects)
317
    glade_project_remove_object (project, project->priv->objects->data);
318

319
320
  g_assert (project->priv->tree == NULL);
  g_assert (project->priv->objects == NULL);
321

322
  G_OBJECT_CLASS (glade_project_parent_class)->dispose (object);
323
324
325
}

static void
326
glade_project_finalize (GObject * object)
327
{
328
  GladeProject *project = GLADE_PROJECT (object);
329

330
  gtk_widget_destroy (project->priv->prefs_dialog);
331

332
333
  g_free (project->priv->path);
  g_free (project->priv->comment);
334

335
336
337
  if (project->priv->unsaved_number > 0)
    glade_id_allocator_release (get_unsaved_number_allocator (),
                                project->priv->unsaved_number);
338

339
340
341
  g_hash_table_destroy (project->priv->target_versions_major);
  g_hash_table_destroy (project->priv->target_versions_minor);
  g_hash_table_destroy (project->priv->target_radios);
342

343
  glade_name_context_destroy (project->priv->widget_names);
344

345
  G_OBJECT_CLASS (glade_project_parent_class)->finalize (object);
346
}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
347

348
static void
349
350
351
352
353
354
355
glade_project_get_property (GObject * object,
                            guint prop_id, GValue * value, GParamSpec * pspec)
{
  GladeProject *project = GLADE_PROJECT (object);

  switch (prop_id)
    {
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    case PROP_MODIFIED:
      g_value_set_boolean (value, project->priv->modified);
      break;
    case PROP_HAS_SELECTION:
      g_value_set_boolean (value, project->priv->has_selection);
      break;
    case PROP_PATH:
      g_value_set_string (value, project->priv->path);
      break;
    case PROP_READ_ONLY:
      g_value_set_boolean (value, project->priv->readonly);
      break;
    case PROP_ADD_ITEM:
      g_value_set_object (value, project->priv->add_item);
      break;
    case PROP_POINTER_MODE:
      g_value_set_enum (value, project->priv->pointer_mode);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
377
    }
378
379
}

380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
 * glade_project_set_modified:
 * @project: a #GladeProject
 * @modified: Whether the project should be set as modified or not
 * @modification: The first #GladeCommand which caused the project to have unsaved changes
 *
 * Set's whether a #GladeProject should be flagged as modified or not. This is useful
 * for indicating that a project has unsaved changes. If @modified is #TRUE, then
 * @modification will be recorded as the first change which caused the project to 
 * have unsaved changes. @modified is #FALSE then @modification will be ignored.
 *
 * If @project is already flagged as modified, then calling this method with
 * @modified as #TRUE, will have no effect. Likewise, if @project is unmodified
 * then calling this method with @modified as #FALSE, will have no effect.
 *
 */
396
static void
397
glade_project_set_modified (GladeProject * project, gboolean modified)
398
{
399
400
401
402
403
  GladeProjectPrivate *priv;

  g_return_if_fail (GLADE_IS_PROJECT (project));

  priv = project->priv;
404

405
406
407
408
409
410
411
412
413
  if (priv->modified != modified)
    {
      priv->modified = !priv->modified;

      if (!priv->modified)
        {
          priv->first_modification = project->priv->prev_redo_item;
          priv->first_modification_is_na = FALSE;
        }
414

415
416
      g_object_notify (G_OBJECT (project), "modified");
    }
417
}
418

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
 * glade_project_get_modified:
 * @project: a #GladeProject
 *
 * Get's whether the project has been modified since it was last saved.
 *
 * Returns: %TRUE if the project has been modified since it was last saved
 */
gboolean
glade_project_get_modified (GladeProject * project)
{
  g_return_val_if_fail (GLADE_IS_PROJECT (project), FALSE);

  return project->priv->modified;
}

void
glade_project_set_pointer_mode (GladeProject       *project,
				GladePointerMode    mode)
{
  g_return_if_fail (GLADE_IS_PROJECT (project));

  if (project->priv->pointer_mode != mode)
    {
      project->priv->pointer_mode = mode;

      g_object_notify (G_OBJECT (project), "pointer-mode");
    }
}

GladePointerMode
glade_project_get_pointer_mode (GladeProject *project)
{
  g_return_val_if_fail (GLADE_IS_PROJECT (project), FALSE);

  return project->priv->pointer_mode;
}


void
glade_project_set_add_item (GladeProject       *project,
			    GladeWidgetAdaptor *adaptor)
{
  GladeProjectPrivate *priv;

  g_return_if_fail (GLADE_IS_PROJECT (project));

  priv = project->priv;

  if (priv->add_item != adaptor)
    {
      priv->add_item = adaptor;

      g_object_notify (G_OBJECT (project), "add-item");
    }
}

GladeWidgetAdaptor *
glade_project_get_add_item (GladeProject *project)
{
  g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);

  return project->priv->add_item;
}

484
485
486
487
/*******************************************************************
                          GladeProjectClass
 *******************************************************************/
static void
488
glade_project_walk_back (GladeProject * project)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
489
{
490
491
  if (project->priv->prev_redo_item)
    project->priv->prev_redo_item = project->priv->prev_redo_item->prev;
492
}
493

494
static void
495
glade_project_walk_forward (GladeProject * project)
496
{
497
498
499
500
  if (project->priv->prev_redo_item)
    project->priv->prev_redo_item = project->priv->prev_redo_item->next;
  else
    project->priv->prev_redo_item = project->priv->undo_stack;
501
502
503
}

static void
504
glade_project_undo_impl (GladeProject * project)
505
{
506
  GladeCommand *cmd, *next_cmd;
507

508
509
510
  while ((cmd = glade_project_next_undo_item (project)) != NULL)
    {
      glade_command_undo (cmd);
511

512
      glade_project_walk_back (project);
513

514
515
      g_signal_emit (G_OBJECT (project),
                     glade_project_signals[CHANGED], 0, cmd, FALSE);
516

517
      if ((next_cmd = glade_project_next_undo_item (project)) != NULL &&
518
519
          (glade_command_group_id (next_cmd) == 0 || 
	   glade_command_group_id (next_cmd) != glade_command_group_id (cmd)))
520
521
        break;
    }
522
}
523

524
static void
525
glade_project_redo_impl (GladeProject * project)
526
{
527
528
529
530
531
  GladeCommand *cmd, *next_cmd;

  while ((cmd = glade_project_next_redo_item (project)) != NULL)
    {
      glade_command_execute (cmd);
532

533
      glade_project_walk_forward (project);
534

535
536
      g_signal_emit (G_OBJECT (project),
                     glade_project_signals[CHANGED], 0, cmd, TRUE);
537

538
      if ((next_cmd = glade_project_next_redo_item (project)) != NULL &&
539
540
          (glade_command_group_id (next_cmd) == 0 || 
	   glade_command_group_id (next_cmd) != glade_command_group_id (cmd)))
541
542
        break;
    }
543
544
545
}

static GladeCommand *
546
glade_project_next_undo_item_impl (GladeProject * project)
547
{
548
  GList *l;
549

550
551
  if ((l = project->priv->prev_redo_item) == NULL)
    return NULL;
552

553
  return GLADE_COMMAND (l->data);
554
555
556
}

static GladeCommand *
557
glade_project_next_redo_item_impl (GladeProject * project)
558
{
559
  GList *l;
560

561
562
563
564
565
  if ((l = project->priv->prev_redo_item) == NULL)
    return project->priv->undo_stack ?
        GLADE_COMMAND (project->priv->undo_stack->data) : NULL;
  else
    return l->next ? GLADE_COMMAND (l->next->data) : NULL;
566
567
}

568
static GList *
569
glade_project_free_undo_item (GladeProject * project, GList * item)
570
{
571
  g_assert (item->data);
572

573
574
  if (item == project->priv->first_modification)
    project->priv->first_modification_is_na = TRUE;
575

576
  g_object_unref (G_OBJECT (item->data));
577

578
  return g_list_next (item);
579
580
}

581
static void
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
glade_project_push_undo_impl (GladeProject * project, GladeCommand * cmd)
{
  GladeProjectPrivate *priv = project->priv;
  GList *tmp_redo_item;

  /* We should now free all the "redo" items */
  tmp_redo_item = g_list_next (priv->prev_redo_item);
  while (tmp_redo_item)
    tmp_redo_item = glade_project_free_undo_item (project, tmp_redo_item);

  if (priv->prev_redo_item)
    {
      g_list_free (g_list_next (priv->prev_redo_item));
      priv->prev_redo_item->next = NULL;
    }
  else
    {
      g_list_free (priv->undo_stack);
      priv->undo_stack = NULL;
    }

  /* Try to unify only if group depth is 0 and the project has not been recently saved */
  if (glade_command_get_group_depth () == 0 &&
      priv->prev_redo_item != NULL &&
      project->priv->prev_redo_item != project->priv->first_modification)
    {
      GladeCommand *cmd1 = priv->prev_redo_item->data;

      if (glade_command_unifies (cmd1, cmd))
        {
          glade_command_collapse (cmd1, cmd);
          g_object_unref (cmd);

          if (glade_command_unifies (cmd1, NULL))
            {
              tmp_redo_item = priv->prev_redo_item;
              glade_project_walk_back (project);
              glade_project_free_undo_item (project, tmp_redo_item);
              priv->undo_stack =
                  g_list_delete_link (priv->undo_stack, tmp_redo_item);
622
623

	      cmd1 = NULL;
624
625
626
            }

          g_signal_emit (G_OBJECT (project),
627
                         glade_project_signals[CHANGED], 0, cmd1, TRUE);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
          return;
        }
    }

  /* and then push the new undo item */
  priv->undo_stack = g_list_append (priv->undo_stack, cmd);

  if (project->priv->prev_redo_item == NULL)
    priv->prev_redo_item = priv->undo_stack;
  else
    priv->prev_redo_item = g_list_next (priv->prev_redo_item);

  g_signal_emit (G_OBJECT (project),
                 glade_project_signals[CHANGED], 0, cmd, TRUE);
642
643
}

644
645
646
static void
glade_project_preview_exits (GPid pid, gint status, gpointer data)
{
647
648
649
650
  GladeProject *project = (GladeProject *) data;
  ChannelWatchPair *channel_watch;
  GIOChannel *channel;
  gchar *pidstr = g_strdup_printf ("%d", pid);
651

652
653
654
655
  channel_watch = g_hash_table_lookup (project->priv->preview_channels, pidstr);
  channel = channel_watch->channel;
  g_io_channel_unref (channel);
  g_hash_table_remove (project->priv->preview_channels, pidstr);
656

657
658
  g_free (pidstr);
  g_free (channel_watch);
659
660
661
}

static void
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
glade_project_kill_previews (gpointer key, gpointer value, gpointer user_data)
{
  const gchar *quit = "<quit>";
  GIOChannel *channel;
  ChannelWatchPair *channel_watch = (ChannelWatchPair *) value;
  GError *error = NULL;
  gsize size;

  channel = channel_watch->channel;
  /* Removing watch, since the child will commit suicide */
  g_source_remove (channel_watch->watch);
  g_io_channel_write_chars (channel, quit, strlen (quit), &size, &error);

  if (size != strlen (quit) && error != NULL)
    {
      g_printerr ("Error passing quit signal trough pipe: %s", error->message);
      g_error_free (error);
    }

  g_io_channel_flush (channel, &error);
  if (error != NULL)
    {
      g_printerr ("Error flushing channel: %s", error->message);
      g_error_free (error);
    }

  g_io_channel_shutdown (channel, TRUE, &error);
  if (error != NULL)
    {
      g_printerr ("Error shutting down channel: %s", error->message);
      g_error_free (error);
    }

  g_io_channel_unref (channel);
  g_free (channel_watch);
697
698
699
}

static void
700
glade_project_close_impl (GladeProject * project)
701
{
702
703
704
  g_hash_table_foreach (project->priv->preview_channels,
                        glade_project_kill_previews, project);
  g_hash_table_unref (project->priv->preview_channels);
705
706
}

707
static void
708
709
glade_project_changed_impl (GladeProject * project,
                            GladeCommand * command, gboolean forward)
710
{
711
712
713
714
715
716
717
718
719
720
721
  if (!project->priv->loading)
    {
      /* if this command is the first modification to cause the project
       * to have unsaved changes, then we can now flag the project as unmodified
       */
      if (!project->priv->first_modification_is_na &&
          project->priv->prev_redo_item == project->priv->first_modification)
        glade_project_set_modified (project, FALSE);
      else
        glade_project_set_modified (project, TRUE);
    }
722
723
}

724

725
/*******************************************************************
726
                          Class Initializers
727
728
 *******************************************************************/
static void
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
glade_project_init (GladeProject * project)
{
  GladeProjectPrivate *priv;
  GList *list;

  project->priv = priv =
      G_TYPE_INSTANCE_GET_PRIVATE ((project), GLADE_TYPE_PROJECT,
                                   GladeProjectPrivate);

  priv->path = NULL;
  priv->readonly = FALSE;
  priv->tree = NULL;
  priv->selection = NULL;
  priv->has_selection = FALSE;
  priv->undo_stack = NULL;
  priv->prev_redo_item = NULL;
  priv->first_modification = NULL;
  priv->first_modification_is_na = FALSE;

  priv->preview_channels =
      g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
750
  priv->widget_names = glade_name_context_new ();
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785

  priv->accel_group = NULL;

  priv->unsaved_number =
      glade_id_allocator_allocate (get_unsaved_number_allocator ());

  do
    {                           /* Get a random non-zero TreeIter stamper */
      priv->stamp = g_random_int ();
    }
  while (priv->stamp == 0);

  priv->target_versions_major = g_hash_table_new_full (g_str_hash,
                                                       g_str_equal,
                                                       g_free, NULL);
  priv->target_versions_minor = g_hash_table_new_full (g_str_hash,
                                                       g_str_equal,
                                                       g_free, NULL);

  for (list = glade_app_get_catalogs (); list; list = list->next)
    {
      GladeCatalog *catalog = list->data;

      /* Set default target to catalog version */
      glade_project_set_target_version (project,
                                        glade_catalog_get_name (catalog),
                                        glade_catalog_get_major_version
                                        (catalog),
                                        glade_catalog_get_minor_version
                                        (catalog));
    }

  priv->target_radios = g_hash_table_new_full (g_str_hash, g_str_equal,
                                               g_free, NULL);
  priv->prefs_dialog = glade_project_build_prefs_dialog (project);
786

787
}
788

789
790
791
792
static void
glade_project_class_init (GladeProjectClass * klass)
{
  GObjectClass *object_class;
793

794
  object_class = G_OBJECT_CLASS (klass);
795

796
797
798
  object_class->get_property = glade_project_get_property;
  object_class->finalize = glade_project_finalize;
  object_class->dispose = glade_project_dispose;
799

800
801
802
803
804
805
806
  klass->add_object = NULL;
  klass->remove_object = NULL;
  klass->undo = glade_project_undo_impl;
  klass->redo = glade_project_redo_impl;
  klass->next_undo_item = glade_project_next_undo_item_impl;
  klass->next_redo_item = glade_project_next_redo_item_impl;
  klass->push_undo = glade_project_push_undo_impl;
807

808
809
810
811
  klass->widget_name_changed = NULL;
  klass->selection_changed = NULL;
  klass->close = glade_project_close_impl;
  klass->changed = glade_project_changed_impl;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
812

813
814
815
816
817
818
819
  /**
   * GladeProject::add-widget:
   * @gladeproject: the #GladeProject which received the signal.
   * @arg1: the #GladeWidget that was added to @gladeproject.
   *
   * Emitted when a widget is added to a project.
   */
820
821
822
823
824
825
826
827
828
  glade_project_signals[ADD_WIDGET] =
      g_signal_new ("add_widget",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeProjectClass, add_object),
                    NULL, NULL,
                    g_cclosure_marshal_VOID__OBJECT,
                    G_TYPE_NONE, 1, GLADE_TYPE_WIDGET);

829
830
831
832
833
834
835
  /**
   * GladeProject::remove-widget:
   * @gladeproject: the #GladeProject which received the signal.
   * @arg1: the #GladeWidget that was removed from @gladeproject.
   * 
   * Emitted when a widget is removed from a project.
   */
836
837
838
839
840
841
842
843
844
845
  glade_project_signals[REMOVE_WIDGET] =
      g_signal_new ("remove_widget",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeProjectClass, remove_object),
                    NULL, NULL,
                    g_cclosure_marshal_VOID__OBJECT,
                    G_TYPE_NONE, 1, GLADE_TYPE_WIDGET);


846
847
848
849
850
851
852
  /**
   * GladeProject::widget-name-changed:
   * @gladeproject: the #GladeProject which received the signal.
   * @arg1: the #GladeWidget who's name changed.
   *
   * Emitted when @gwidget's name changes.
   */
853
854
855
856
857
858
859
860
861
862
  glade_project_signals[WIDGET_NAME_CHANGED] =
      g_signal_new ("widget_name_changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeProjectClass, widget_name_changed),
                    NULL, NULL,
                    g_cclosure_marshal_VOID__OBJECT,
                    G_TYPE_NONE, 1, GLADE_TYPE_WIDGET);


863
  /**
864
865
866
867
868
   * GladeProject::selection-changed:
   * @gladeproject: the #GladeProject which received the signal.
   *
   * Emitted when @gladeproject selection list changes.
   */
869
870
871
872
873
874
875
876
  glade_project_signals[SELECTION_CHANGED] =
      g_signal_new ("selection_changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeProjectClass, selection_changed),
                    NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);


877
878
879
880
881
882
883
  /**
   * GladeProject::close:
   * @gladeproject: the #GladeProject which received the signal.
   *
   * Emitted when a project is closing (a good time to clean up
   * any associated resources).
   */
884
885
886
887
888
889
890
  glade_project_signals[CLOSE] =
      g_signal_new ("close",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeProjectClass, close),
                    NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

891
892
893
894
895
896
897
898
  /**
   * GladeProject::changed:
   * @gladeproject: the #GladeProject which received the signal.
   * @arg1: the #GladeCommand that was executed
   * @arg2: whether the command was executed or undone.
   *
   * Emitted when a @gladeproject's state changes via a #GladeCommand.
   */
899
900
901
902
903
904
905
906
907
  glade_project_signals[CHANGED] =
      g_signal_new ("changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_FIRST,
                    G_STRUCT_OFFSET (GladeProjectClass, changed),
                    NULL, NULL,
                    glade_marshal_VOID__OBJECT_BOOLEAN,
                    G_TYPE_NONE, 2, GLADE_TYPE_COMMAND, G_TYPE_BOOLEAN);

908
909
910
911
912
913
  /**
   * GladeProject::parse-began:
   * @gladeproject: the #GladeProject which received the signal.
   *
   * Emitted when @gladeproject parsing starts.
   */
914
915
916
917
918
919
920
  glade_project_signals[PARSE_BEGAN] =
      g_signal_new ("parse-began",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_FIRST,
                    0, NULL, NULL,
                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

921
922
923
924
925
926
  /**
   * GladeProject::parse-finished:
   * @gladeproject: the #GladeProject which received the signal.
   *
   * Emitted when @gladeproject parsing has finished.
   */
927
928
929
930
931
932
  glade_project_signals[PARSE_FINISHED] =
      g_signal_new ("parse-finished",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_FIRST,
                    G_STRUCT_OFFSET (GladeProjectClass, parse_finished),
                    NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
933

934
935
936
937
938
939
  /**
   * GladeProject::targets-changed:
   * @gladeproject: the #GladeProject which received the signal.
   *
   * Emitted when @gladeproject target versions change.
   */
940
941
942
943
944
945
946
  glade_project_signals[TARGETS_CHANGED] =
      g_signal_new ("targets-changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_FIRST,
                    0, NULL, NULL,
                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

947
948
949
950
951
952
953
954
  /**
   * GladeProject::load-progress:
   * @gladeproject: the #GladeProject which received the signal.
   * @objects_total: the total amount of objects to load
   * @objects_loaded: the current amount of loaded objects
   *
   * Emitted while @project is loading.
   */
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
  glade_project_signals[LOAD_PROGRESS] =
      g_signal_new ("load-progress",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_FIRST,
                    0, NULL, NULL,
                    glade_marshal_VOID__INT_INT,
                    G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);

  g_object_class_install_property (object_class,
                                   PROP_MODIFIED,
                                   g_param_spec_boolean ("modified",
                                                         "Modified",
                                                         _
                                                         ("Whether project has been modified since it was last saved"),
                                                         FALSE,
                                                         G_PARAM_READABLE));

  g_object_class_install_property (object_class,
                                   PROP_HAS_SELECTION,
                                   g_param_spec_boolean ("has-selection",
                                                         _("Has Selection"),
                                                         _
                                                         ("Whether project has a selection"),
                                                         FALSE,
                                                         G_PARAM_READABLE));

  g_object_class_install_property (object_class,
                                   PROP_PATH,
                                   g_param_spec_string ("path",
                                                        _("Path"),
                                                        _
                                                        ("The filesystem path of the project"),
                                                        NULL,
                                                        G_PARAM_READABLE));

  g_object_class_install_property (object_class,
                                   PROP_READ_ONLY,
                                   g_param_spec_boolean ("read-only",
                                                         _("Read Only"),
                                                         _
                                                         ("Whether project is read-only"),
                                                         FALSE,
                                                         G_PARAM_READABLE));

999
1000
1001
1002
1003
1004
1005
  g_object_class_install_property (object_class,
                                   PROP_ADD_ITEM,
                                   g_param_spec_object ("add-item",
							_("Add Item"),
							_("The current item to add to the project"),
							GLADE_TYPE_WIDGET_ADAPTOR,
							G_PARAM_READABLE));
1006

1007
1008
1009
1010
1011
1012
1013
1014
  g_object_class_install_property (object_class,
                                   PROP_POINTER_MODE,
                                   g_param_spec_enum ("pointer-mode",
						      _("Pointer Mode"),
						      _("The currently effective GladePointerMode"),
						      GLADE_TYPE_POINTER_MODE,
						      GLADE_POINTER_SELECT,
						      G_PARAM_READABLE));
1015
1016

  g_type_class_add_private (klass, sizeof (GladeProjectPrivate));
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1017
1018
}

1019
/*******************************************************************
1020
                    Loading project code here
1021
 *******************************************************************/
1022

1023
1024
1025
/**
 * glade_project_new:
 *
1026
 * Creates a new #GladeProject.
1027
1028
1029
 *
 * Returns: a new #GladeProject
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1030
GladeProject *
1031
glade_project_new (void)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1032
{
1033
1034
  GladeProject *project = g_object_new (GLADE_TYPE_PROJECT, NULL);
  return project;
1035
1036
}

1037
1038
/* Called when finishing loading a glade file to resolve object type properties
 */
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
static void
glade_project_fix_object_props (GladeProject * project)
{
  GList *l, *ll, *objects;
  GValue *value;
  GladeWidget *gwidget;
  GladeProperty *property;
  gchar *txt;

  objects = g_list_copy (project->priv->objects);
  for (l = objects; l; l = l->next)
    {
      gwidget = glade_widget_get_from_gobject (l->data);

1053
      for (ll = glade_widget_get_properties (gwidget); ll; ll = ll->next)
1054
        {
1055
1056
	  GladePropertyClass *klass;

1057
          property = GLADE_PROPERTY (ll->data);
1058
	  klass    = glade_property_get_class (property);
1059

1060
          if (glade_property_class_is_object (klass) &&
1061
1062
1063
1064
1065
1066
              (txt = g_object_get_data (G_OBJECT (property),
                                        "glade-loaded-object")) != NULL)
            {
              /* Parse the object list and set the property to it
               * (this magicly works for both objects & object lists)
               */
1067
              value = glade_property_class_make_gvalue_from_string (klass, txt, project);
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

              glade_property_set_value (property, value);

              g_value_unset (value);
              g_free (value);

              g_object_set_data (G_OBJECT (property),
                                 "glade-loaded-object", NULL);
            }
        }
    }
  g_list_free (objects);
1080
}
1081

1082
static gchar *
1083
1084
glade_project_read_requires_from_comment (GladeXmlNode * comment,
                                          guint16 * major, guint16 * minor)
1085
{
1086
1087
1088
  gint maj, min;
  gchar *value, buffer[256];
  gchar *required_lib = NULL;
1089

1090
1091
  if (!glade_xml_node_is_comment (comment))
    return NULL;
1092

1093
  value = glade_xml_get_content (comment);
1094

1095
1096
1097
  if (value &&
      !strncmp (" interface-requires", value, strlen (" interface-requires")))
    {
1098
      if (sscanf (value, " interface-requires %s %d.%d", buffer, &maj, &min) == 3)
1099
1100
1101
1102
1103
1104
1105
1106
1107
        {
          if (major)
            *major = maj;
          if (minor)
            *minor = min;
          required_lib = g_strdup (buffer);
        }
    }
  g_free (value);
1108

1109
  return required_lib;
1110
}
1111

1112
1113

static gboolean
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
glade_project_read_requires (GladeProject * project,
                             GladeXmlNode * root_node,
                             const gchar * path, gboolean * has_gtk_dep)
{

  GString *string = g_string_new (NULL);
  GladeXmlNode *node;
  gchar *required_lib = NULL;
  gboolean loadable = TRUE;
  guint16 major, minor;

  for (node = glade_xml_node_get_children_with_comments (root_node);
       node; node = glade_xml_node_next_with_comments (node))
    {
      /* Skip non "requires" tags */
      if (!(glade_xml_node_verify_silent (node, GLADE_XML_TAG_REQUIRES) ||
            (required_lib =
             glade_project_read_requires_from_comment (node, &major, &minor))))
        continue;

      if (!required_lib)
        {
          required_lib =
              glade_xml_get_property_string_required (node, GLADE_XML_TAG_LIB,
                                                      NULL);
          glade_xml_get_property_version (node, GLADE_XML_TAG_VERSION,
                                          &major, &minor);
        }

      if (!required_lib)
        continue;

      /* Dont mention gtk+ as a required lib in 
       * the generated glade file
       */
      if (!glade_catalog_is_loaded (required_lib))
        {
          if (!loadable)
            g_string_append (string, ", ");

          g_string_append (string, required_lib);
          loadable = FALSE;
        }
      else
        {
          if (has_gtk_dep && strcmp (required_lib, "gtk+") == 0)
            *has_gtk_dep = TRUE;

          glade_project_set_target_version
              (project, required_lib, major, minor);
        }

      g_free (required_lib);
    }

  if (!loadable)
    glade_util_ui_message (glade_app_get_window (),
                           GLADE_UI_ERROR, NULL,
                           _("Failed to load %s.\n"
                             "The following required catalogs are unavailable: %s"),
                           path, string->str);
  g_string_free (string, TRUE);
  return loadable;
1177
1178
}

1179
static gchar *
1180
glade_project_read_resource_path_from_comment (GladeXmlNode * comment)
1181
{
1182
  gchar *value, buffer[FILENAME_MAX], *path = NULL;
1183

1184
1185
  if (!glade_xml_node_is_comment (comment))
    return FALSE;
1186

1187
1188
1189
1190
1191
1192
1193
1194
1195
  value = glade_xml_get_content (comment);
  if (value &&
      !strncmp (" interface-local-resource-path", value,
                strlen (" interface-local-resource-path")))
    {
      if (sscanf (value, " interface-local-resource-path %s", buffer) == 1)
        path = g_strdup (buffer);
    }
  g_free (value);
1196

1197
  return path;
1198
1199
}

1200
static void
1201
update_project_for_resource_path (GladeProject * project)
1202
{
1203
1204
1205
  GladeWidget *widget;
  GladeProperty *property;
  GList *l, *list;
1206

1207
1208
  for (l = project->priv->objects; l; l = l->next)
    {
1209

1210
      widget = glade_widget_get_from_gobject (l->data);
1211

1212
      for (list = glade_widget_get_properties (widget); list; list = list->next)
1213
        {
1214
	  GladePropertyClass *klass;
1215
	  GParamSpec         *pspec;
1216

1217
          property = list->data;
1218
	  klass    = glade_property_get_class (property);
1219
	  pspec    = glade_property_class_get_pspec (klass);
1220
1221
1222
1223
1224
1225

          /* XXX We should have a "resource" flag on properties that need
           *   to be loaded from the resource path, but that would require
           * that they can serialize both ways (custom properties are only
           * required to generate unique strings for value comparisons).
           */
1226
          if (pspec->value_type == GDK_TYPE_PIXBUF)
1227
1228
            {
              GValue *value;
1229
              gchar  *string;
1230

1231
              string = glade_property_make_string (property);
1232
              value  = glade_property_class_make_gvalue_from_string (klass, string, project);
1233
1234
1235
1236
1237
1238
1239
1240
1241

              glade_property_set_value (property, value);

              g_value_unset (value);
              g_free (value);
              g_free (string);
            }
        }
    }
1242
1243
}

1244
1245
1246

/* This function assumes ownership of 'path'. */
static void
1247
glade_project_set_resource_path (GladeProject * project, gchar * path)
1248
{
1249
1250
  g_free (project->priv->resource_path);
  project->priv->resource_path = path;
1251

1252
1253
  update_project_for_resource_path (project);
  update_prefs_for_resource_path (project);
1254
1255
1256
}

static void
1257
1258
glade_project_read_resource_path (GladeProject * project,
                                  GladeXmlNode * root_node)
1259
{
1260
1261
  GladeXmlNode *node;
  gchar *path = NULL;
1262

1263
1264
1265
1266
1267
1268
1269
  for (node = glade_xml_node_get_children_with_comments (root_node);
       node; node = glade_xml_node_next_with_comments (node))
    {
      /* Skip non "requires" tags */
      if ((path = glade_project_read_resource_path_from_comment (node)) != NULL)
        break;
    }
1270

1271
  glade_project_set_resource_path (project, path);
1272
1273
1274
}


1275
static void
1276
glade_project_read_comment (GladeProject * project, Gla