File: persistence.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (1259 lines) | stat: -rw-r--r-- 37,585 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
186
187
188
189
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
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
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 2003 Lars Clausen
 *
 * 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.
 */

/*!
 * \file persistence.c -- functions that handle persistent stores, such as 
 * window positions and sizes, font menu, document history etc.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "persistence.h"
#include "dia_dirs.h"
#include "dia_xml_libxml.h"
#include "dia_xml.h"
#include "message.h" /* only for dia_log_message() */
#include "diacontext.h"
#include "intl.h"

#include <gtk/gtk.h>
#include <libxml/tree.h>

/* private data structures */
/*!
 * \brief A persistently stored list of strings.
 *
 * The persitent list contains no duplicates.
 * If sorted is FALSE, any string added will be placed in front of the list
 * (possibly removing it from further down), thus making it an LRU list.
 * The list is not tied to any particular GTK widget, as it has uses
 * in a number of different places (though mostly in menus)
 */
struct _PersistentList {
  const gchar *role;
  gboolean sorted;
  gint max_members;
  GList *glist;
  GList *listeners;
};

/*! \brief Some storage window information */
typedef struct {
  int x, y;
  int width, height;
  gboolean isopen;
  GtkWindow *window;
} PersistentWindow;

/* Hash table from window role (string) to PersistentWindow structure.
 */
static GHashTable *persistent_windows, *persistent_entrystrings, *persistent_lists;
static GHashTable *persistent_integers, *persistent_reals;
static GHashTable *persistent_booleans, *persistent_strings;
static GHashTable *persistent_colors;

static GHashTable *
_dia_hash_table_str_any_new (void)
{
  /* the key is const, the value gets freed */
  return g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
}

/* *********************** LOADING FUNCTIONS *********************** */

typedef void (*PersistenceLoadFunc)(gchar *role, xmlNodePtr node, DiaContext *ctx);

static void
persistence_load_window(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;
  PersistentWindow *wininfo = g_new0(PersistentWindow, 1);

  attr = composite_find_attribute(node, "xpos");
  if (attr != NULL)
    wininfo->x = data_int(attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "ypos");
  if (attr != NULL)
    wininfo->y = data_int(attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "width");
  if (attr != NULL)
    wininfo->width = data_int(attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "height");
  if (attr != NULL)
    wininfo->height = data_int(attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "isopen");
  if (attr != NULL)
    wininfo->isopen = data_boolean(attribute_first_data(attr), ctx);

  g_hash_table_insert(persistent_windows, role, wininfo);
}

/** Load a persistent string into the strings hashtable */
static void
persistence_load_entrystring(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;
  gchar *string = NULL;

  /* Find the contents? */
  attr = composite_find_attribute(node, "stringvalue");
  if (attr != NULL)
    string = data_string(attribute_first_data(attr), ctx);
  else 
    return;

  if (string != NULL)
    g_hash_table_insert(persistent_entrystrings, role, string);
}

static void
persistence_load_list(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;
  gchar *string = NULL;

  /* Find the contents? */
  attr = composite_find_attribute(node, "listvalue");
  if (attr != NULL)
    string = data_string(attribute_first_data(attr), ctx);
  else 
    return;

  if (string != NULL) {
    gchar **strings = g_strsplit(string, "\n", -1);
    PersistentList *plist;
    GList *list = NULL;
    int i;
    for (i = 0; strings[i] != NULL; i++) {
      list = g_list_append(list, g_strdup(strings[i]));
    }
    /* This frees the strings, too? */
    g_strfreev(strings);
    /* yes but not the other one --hb */
    g_free (string);
    plist = g_new(PersistentList, 1);
    plist->glist = list;
    plist->role = role;
    plist->sorted = FALSE;
    plist->max_members = G_MAXINT;
    g_hash_table_insert(persistent_lists, role, plist);
  }
}

static void
persistence_load_integer(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;

  /* Find the contents? */
  attr = composite_find_attribute(node, "intvalue");
  if (attr != NULL) {
    gint *integer = g_new(gint, 1);
    *integer = data_int(attribute_first_data(attr), ctx);
    g_hash_table_insert(persistent_integers, role, integer);
  }
}

static void
persistence_load_real(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;

  /* Find the contents? */
  attr = composite_find_attribute(node, "realvalue");
  if (attr != NULL) {
    real *realval = g_new(real, 1);
    *realval = data_real(attribute_first_data(attr), ctx);
    g_hash_table_insert(persistent_reals, role, realval);
  }
}

static void
persistence_load_boolean(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;

  /* Find the contents? */
  attr = composite_find_attribute(node, "booleanvalue");
  if (attr != NULL) {
    gboolean *booleanval = g_new(gboolean, 1);
    *booleanval = data_boolean(attribute_first_data(attr), ctx);
    g_hash_table_insert(persistent_booleans, role, booleanval);
  }
}

static void
persistence_load_string(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;

  /* Find the contents? */
  attr = composite_find_attribute(node, "stringvalue");
  if (attr != NULL) {
    gchar *stringval = data_string(attribute_first_data(attr), ctx);
    g_hash_table_insert(persistent_strings, role, stringval);
  }
}

static void
persistence_load_color(gchar *role, xmlNodePtr node, DiaContext *ctx)
{
  AttributeNode attr;

  /* Find the contents? */
  attr = composite_find_attribute(node, "colorvalue");
  if (attr != NULL) {
    Color *colorval = g_new(Color, 1);
    data_color(attribute_first_data(attr), colorval, ctx);
    g_hash_table_insert(persistent_colors, role, colorval);
  }
}

static GHashTable *type_handlers;

/** Load the named type of entries using the given function.
 * func is a void (*func)(gchar *role, xmlNodePtr *node, DiaContext *ctx)
 */
static void
persistence_load_type(xmlNodePtr node, DiaContext *ctx)
{
  const gchar *typename = (gchar *) node->name;
  gchar *name;

  PersistenceLoadFunc func =
    (PersistenceLoadFunc)g_hash_table_lookup(type_handlers, typename);
  if (func == NULL) {
    return;
  }

  name = (gchar *)xmlGetProp(node, (const xmlChar *)"role");
  if (name == NULL) {
    return;
  }
  
  (*func)(name, node, ctx);
}

static void
persistence_set_type_handler(gchar *name, PersistenceLoadFunc func)
{
  if (type_handlers == NULL)
    type_handlers = g_hash_table_new(g_str_hash,g_str_equal);

  g_hash_table_insert(type_handlers, name, (gpointer)func);
}

static void
persistence_init()
{
  persistence_set_type_handler("window", persistence_load_window);
  persistence_set_type_handler("entrystring", persistence_load_entrystring);
  persistence_set_type_handler("list", persistence_load_list);
  persistence_set_type_handler("integer", persistence_load_integer);
  persistence_set_type_handler("real", persistence_load_real);
  persistence_set_type_handler("boolean", persistence_load_boolean);
  persistence_set_type_handler("string", persistence_load_string);
  persistence_set_type_handler("color", persistence_load_color);

  if (persistent_windows == NULL) {
    persistent_windows = _dia_hash_table_str_any_new();
  }
  if (persistent_entrystrings == NULL) {
    persistent_entrystrings = _dia_hash_table_str_any_new();
  }
  if (persistent_lists == NULL) {
    persistent_lists = _dia_hash_table_str_any_new();
  }
  if (persistent_integers == NULL) {
    persistent_integers = _dia_hash_table_str_any_new();
  }
  if (persistent_reals == NULL) {
    persistent_reals = _dia_hash_table_str_any_new();
  }
  if (persistent_booleans == NULL) {
    persistent_booleans = _dia_hash_table_str_any_new();
  }
  if (persistent_strings == NULL) {
    persistent_strings = _dia_hash_table_str_any_new();
  }
  if (persistent_colors == NULL) {
    persistent_colors = _dia_hash_table_str_any_new();
  }
}

/* Load all persistent data. */
void
persistence_load()
{
  xmlDocPtr doc;
  gchar *filename = dia_config_filename("persistence");
  DiaContext *ctx;

  persistence_init();

  if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
    g_free (filename);
    return;
  }
  ctx = dia_context_new(_("Persistence"));
  dia_context_set_filename(ctx, filename);
  doc = diaXmlParseFile(filename, ctx, FALSE);
  if (doc != NULL) {
    if (doc->xmlRootNode != NULL) {
      xmlNsPtr namespace = xmlSearchNs(doc, doc->xmlRootNode, (const xmlChar *)"dia");
      if (!xmlStrcmp (doc->xmlRootNode->name, (const xmlChar *)"persistence") &&
	  namespace != NULL) {
	xmlNodePtr child_node = doc->xmlRootNode->children;
	for (; child_node != NULL; child_node = child_node->next) {
	  persistence_load_type(child_node, ctx);
	}
      }
    }
    xmlFreeDoc(doc);
  }
  g_free(filename);
  dia_context_release(ctx);
}

/* *********************** SAVING FUNCTIONS *********************** */
typedef struct
{
  xmlNodePtr  tree;
  DiaContext *ctx;
} PersitenceUserData;

/* Save the position of a window  */
static void
persistence_save_window(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  PersistentWindow *window_pos = (PersistentWindow *)value;
  ObjectNode window;

  window = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"window", NULL);
  
  xmlSetProp(window, (const xmlChar *)"role", (xmlChar *) key);
  data_add_int(new_attribute(window, "xpos"), window_pos->x, ctx);
  data_add_int(new_attribute(window, "ypos"), window_pos->y, ctx);
  data_add_int(new_attribute(window, "width"), window_pos->width, ctx);
  data_add_int(new_attribute(window, "height"), window_pos->height, ctx);
  data_add_boolean(new_attribute(window, "isopen"), window_pos->isopen, ctx);

}

/* Save the contents of a string  */
static void
persistence_save_list(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode listnode;
  GString *buf;
  GList *items;

  listnode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"list", NULL);

  xmlSetProp(listnode, (const xmlChar *)"role", (xmlChar *) key);
  /* Make a string out of the list */
  buf = g_string_new("");
  for (items = ((PersistentList*)value)->glist; items != NULL;
       items = g_list_next(items)) {
    g_string_append(buf, (gchar *)items->data);
    if (g_list_next(items) != NULL) 
      g_string_append(buf, "\n");
  }
  
  data_add_string(new_attribute(listnode, "listvalue"), buf->str, ctx);
  g_string_free(buf, TRUE);
}

static void
persistence_save_integer(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode integernode;

  integernode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"integer", NULL);

  xmlSetProp(integernode, (const xmlChar *)"role", (xmlChar *)key);
  data_add_int(new_attribute(integernode, "intvalue"), *(gint *)value, ctx);
}

static void
persistence_save_real(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode realnode;

  realnode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"real", NULL);

  xmlSetProp(realnode, (const xmlChar *)"role", (xmlChar *)key);
  data_add_real(new_attribute(realnode, "realvalue"), *(real *)value, ctx);
}

static void
persistence_save_boolean(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode booleannode;

  booleannode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"boolean", NULL);

  xmlSetProp(booleannode, (const xmlChar *)"role", (xmlChar *)key);
  data_add_boolean(new_attribute(booleannode, "booleanvalue"), *(gboolean *)value, ctx);
}

static void
persistence_save_string(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode stringnode;

  stringnode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"string", NULL);

  xmlSetProp(stringnode, (const xmlChar *)"role", (xmlChar *)key);
  data_add_string(new_attribute(stringnode, "stringvalue"), (gchar *)value, ctx);
}

static void
persistence_save_color(gpointer key, gpointer value, gpointer data)
{
  PersitenceUserData *ud = (PersitenceUserData *)data;
  xmlNodePtr tree = ud->tree;
  DiaContext *ctx = ud->ctx;
  ObjectNode colornode;

  colornode = (ObjectNode)xmlNewChild(tree, NULL, (const xmlChar *)"color", NULL);

  xmlSetProp(colornode, (const xmlChar *)"role", (xmlChar *)key);
  data_add_color(new_attribute(colornode, "colorvalue"), (Color *)value, ctx);
}

static void
persistence_save_type(xmlDocPtr doc, DiaContext *ctx, GHashTable *entries, GHFunc func)
{
  PersitenceUserData ud;
  ud.tree = doc->xmlRootNode;
  ud.ctx = ctx;

  if (entries != NULL && g_hash_table_size(entries) != 0) {
    g_hash_table_foreach(entries, func, &ud);
  }
}

/* Save all persistent data. */
void
persistence_save()
{
  xmlDocPtr doc;
  xmlNs *name_space;
  DiaContext *ctx;
  gchar *filename = dia_config_filename("persistence");

  ctx = dia_context_new ("Persistence");
  doc = xmlNewDoc((const xmlChar *)"1.0");
  doc->encoding = xmlStrdup((const xmlChar *)"UTF-8");
  doc->xmlRootNode = xmlNewDocNode(doc, NULL, (const xmlChar *)"persistence", NULL);

  name_space = xmlNewNs(doc->xmlRootNode, 
                        (const xmlChar *) DIA_XML_NAME_SPACE_BASE,
			(const xmlChar *)"dia");
  xmlSetNs(doc->xmlRootNode, name_space);

  persistence_save_type(doc, ctx, persistent_windows, persistence_save_window);
  persistence_save_type(doc, ctx, persistent_entrystrings, persistence_save_string);
  persistence_save_type(doc, ctx, persistent_lists, persistence_save_list);
  persistence_save_type(doc, ctx, persistent_integers, persistence_save_integer);
  persistence_save_type(doc, ctx, persistent_reals, persistence_save_real);
  persistence_save_type(doc, ctx, persistent_booleans, persistence_save_boolean);
  persistence_save_type(doc, ctx, persistent_strings, persistence_save_string);
  persistence_save_type(doc, ctx, persistent_colors, persistence_save_color);

  xmlDiaSaveFile(filename, doc);
  g_free(filename);
  xmlFreeDoc(doc);
  dia_context_release (ctx);
}

/* *********************** USAGE FUNCTIONS *********************** */

/* ********* WINDOWS ********* */

/* Returns the name used for a window in persistence.
 */
static const gchar *
persistence_get_window_name(GtkWindow *window)
{
  const gchar *name = gtk_window_get_role(window);
  if (name == NULL) {
    g_warning("Internal:  Window %s has no role.", gtk_window_get_title(window));
    return NULL;
  }
  return name;
}

static void
persistence_store_window_info(GtkWindow *window, PersistentWindow *wininfo,
			      gboolean isclosed)
{
  /* Drawable means visible & mapped, what we usually think of as open. */
  if (!isclosed) {
    gtk_window_get_position(window, &wininfo->x, &wininfo->y);
    gtk_window_get_size(window, &wininfo->width, &wininfo->height);
    wininfo->isopen = TRUE;
  } else {
    wininfo->isopen = FALSE;
  }
}

/*!
 * \brief Update the persistent information for a window.
 * @param window The GTK window object being stored.
 * @param isclosed Whether the window should be stored as closed or not.
 * In some cases, the window's open/close state is not updated by the time
 * the handler is called.
 */
static void
persistence_update_window(GtkWindow *window, gboolean isclosed)
{
  const gchar *name = persistence_get_window_name(window);
  PersistentWindow *wininfo;

  if (name == NULL) return;

  if (persistent_windows == NULL) {
    persistent_windows = _dia_hash_table_str_any_new();
  }    
  wininfo = (PersistentWindow *)g_hash_table_lookup(persistent_windows, name);

  if (wininfo != NULL) {  
    persistence_store_window_info(window, wininfo, isclosed);
  } else {
    wininfo = g_new0(PersistentWindow, 1);
    persistence_store_window_info(window, wininfo, FALSE);
    g_hash_table_insert(persistent_windows, (gchar *)name, wininfo);
  }
  if (wininfo->window != NULL && wininfo->window != window) {
    g_object_unref(wininfo->window);
    wininfo->window = NULL;
  }
  if (wininfo->window == NULL) {
    wininfo->window = window;
    g_object_ref(window);
  }
  /* catch the transistion */
  wininfo->isopen = !isclosed;
}

/*!
 * \brief Event handler for window persitence
 *
 * Handler for window-related events that should cause persistent storage changes.
 * @param window The GTK window to store for.
 * @param event the GDK event that caused us to be called.  Note that the 
 * window state hasn't been updated by the event yet.
 * @param data Userdata passed when adding signal handler.
 * @return Always FALSE to continue processing of events
 */
static gboolean
persistence_window_event_handler(GtkWindow *window, GdkEvent *event, gpointer data)
{
  switch (event->type) {
  case GDK_UNMAP : 
    dia_log_message ("unmap (%s)", persistence_get_window_name(window)); 
    break;
  case GDK_MAP : 
    dia_log_message  ("map (%s)", persistence_get_window_name(window)); 
    break;
  case GDK_CONFIGURE : 
    dia_log_message ("configure (%s)", persistence_get_window_name(window)); 
    break;
  default :
    /* silence gcc */
    break;
  }
#if GTK_CHECK_VERSION(2,20,0)
  persistence_update_window(window, !gtk_widget_get_mapped(GTK_WIDGET(window)));
#else
  persistence_update_window(window, !(GTK_WIDGET_MAPPED(window)));
#endif
  /* continue processing */
  return FALSE;
}

/*!
 * \brief Handler for when a window has been opened or closed.
 *
 * @param window The GTK window to store for.
 * @param data Userdata passed when adding signal handler.
 */
static gboolean
persistence_hide_show_window(GtkWindow *window, gpointer data)
{
#if GTK_CHECK_VERSION(2,20,0)
  persistence_update_window(window, !(gtk_widget_get_mapped(GTK_WIDGET(window))));
#else
  persistence_update_window(window, !GTK_WIDGET_MAPPED(window));
#endif
  return FALSE;
}

/*!
 * \brief Check stored window information against screen size
 *
 * If the screen size has changed some persistent info maybe out of the visible area.
 * This function checks that stored coordinates are at least paritally visible on some
 * monitor. In GDK parlance a screen can have multiple monitors.
 */
static gboolean
wininfo_in_range (const PersistentWindow *wininfo)
{
  GdkScreen *screen = gdk_screen_get_default ();
  gint num_monitors = gdk_screen_get_n_monitors (screen), i;
  GdkRectangle rwin = {wininfo->x, wininfo->y, wininfo->width, wininfo->height};
  GdkRectangle rres = {0, 0, 0, 0};
  
  for (i = 0; i < num_monitors; ++i) {
    GdkRectangle rmon;
    
    gdk_screen_get_monitor_geometry (screen, i, &rmon);
    
    gdk_rectangle_intersect (&rwin, &rmon, &rres);
    if (rres.width * rres.height > 0)
      break;
  }

  return (rres.width * rres.height > 0);
}

/*!
 * \brief Register a window with a role for persitence
 *
 * Call this function after the window has a role assigned to use any
 * persistence information about the window.
 */
void
persistence_register_window(GtkWindow *window)
{
  const gchar *name = persistence_get_window_name(window);
  PersistentWindow *wininfo;

  if (name == NULL) return;
  if (persistent_windows == NULL) {
    persistent_windows = _dia_hash_table_str_any_new();
  }    
  wininfo = (PersistentWindow *)g_hash_table_lookup(persistent_windows, name);
  if (wininfo != NULL) {
    if (wininfo_in_range (wininfo)) {
      /* only restore position if partially visible */
      gtk_window_move(window, wininfo->x, wininfo->y);
      gtk_window_resize(window, wininfo->width, wininfo->height);
    }
    if (wininfo->isopen) gtk_widget_show(GTK_WIDGET(window));
  } else {
    wininfo = g_new0(PersistentWindow, 1);
    gtk_window_get_position(window, &wininfo->x, &wininfo->y);
    gtk_window_get_size(window, &wininfo->width, &wininfo->height);
    /* Drawable means visible & mapped, what we usually think of as open. */
#if GTK_CHECK_VERSION(2,18,0)
    wininfo->isopen = gtk_widget_is_drawable(GTK_WIDGET(window));
#else
    wininfo->isopen = GTK_WIDGET_DRAWABLE(GTK_WIDGET(window));
#endif
    g_hash_table_insert(persistent_windows, (gchar *)name, wininfo);
  }
  if (wininfo->window != NULL && wininfo->window != window) {
    g_object_unref(wininfo->window);
    wininfo->window = NULL;
  }
  if (wininfo->window == NULL) {
    wininfo->window = window;
    g_object_ref(window);
  }

  g_signal_connect(G_OBJECT(window), "configure-event",
		   G_CALLBACK(persistence_window_event_handler), NULL);
  g_signal_connect(G_OBJECT(window), "map-event",
		   G_CALLBACK(persistence_window_event_handler), NULL);
  g_signal_connect(G_OBJECT(window), "unmap-event",
		   G_CALLBACK(persistence_window_event_handler), NULL);

  g_signal_connect(G_OBJECT(window), "hide",
		   G_CALLBACK(persistence_hide_show_window), NULL);
  g_signal_connect(G_OBJECT(window), "show",
		   G_CALLBACK(persistence_hide_show_window), NULL);
}

/*!
 * \brief Restore a window position from it's stored information
 *
 * Call this function at start-up to have a window creation function
 * called if the window should be opened at startup.
 * If no persistence information is available for the given role,
 * nothing happens.
 * @arg role The role of the window, as will be set by gtk_window_set_role()
 * @arg func A 0-argument function that creates the window.  This
 * function will be called if the persistence information indicates that the
 * window should be open.  The function should create and show the window.
 */
void
persistence_register_window_create(gchar *role, NullaryFunc *func)
{
  PersistentWindow *wininfo;

  if (role == NULL) return;
  if (persistent_windows == NULL) return;
  wininfo = (PersistentWindow *)g_hash_table_lookup(persistent_windows, role);
  if (wininfo != NULL) {
    (*func)();
  }
}


/* ********* STRING ENTRIES ********** */

static gboolean
persistence_update_string_entry(GtkWidget *widget, GdkEvent *event,
				gpointer userdata)
{
  gchar *role = (gchar*)userdata;

  if (event->type == GDK_FOCUS_CHANGE) {
    gchar *string = (gchar *)g_hash_table_lookup(persistent_entrystrings, role);
    const gchar *entrystring = gtk_entry_get_text(GTK_ENTRY(widget));
    if (string == NULL || strcmp(string, entrystring) != 0) {
      g_hash_table_insert(persistent_entrystrings, role, g_strdup(entrystring));
    }
  }

  return FALSE;
}

/*!
 * \brief Cancel modification of a persistent string entry
 *
 * Change the contents of the persistently stored string entry.
 * If widget is non-null, it is updated to reflect the change.
 * This can be used e.g. for when a dialog is cancelled and the old
 * contents should be restored.
 */
gboolean
persistence_change_string_entry(gchar *role, gchar *string,
				GtkWidget *widget)
{
  gchar *old_string = (gchar*)g_hash_table_lookup(persistent_entrystrings, role);
  if (old_string != NULL) {
    if (widget != NULL) {
      gtk_entry_set_text(GTK_ENTRY(widget), string);
    }
    g_hash_table_insert(persistent_entrystrings, role, g_strdup(string));
  }

  return FALSE;
}

/*!
 * \brief Register a string in a GtkEntry for persistence.
 *
 * This should include not only a unique name, but some way to update
 * whereever the string is used.
 */
void
persistence_register_string_entry(gchar *role, GtkWidget *entry)
{
  gchar *string;
  if (role == NULL) return;
  if (persistent_entrystrings == NULL) {
    persistent_entrystrings = _dia_hash_table_str_any_new();
  }    
  string = (gchar *)g_hash_table_lookup(persistent_entrystrings, role);
  if (string != NULL) {
    gtk_entry_set_text(GTK_ENTRY(entry), string);
  } else {
    string = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
    g_hash_table_insert(persistent_entrystrings, role, string);
  }
  g_signal_connect(G_OBJECT(entry), "event", 
		   G_CALLBACK(persistence_update_string_entry), role);
}

/* ********* LISTS ********** */
/* Lists are used for e.g. recent files, selected fonts, etc. 
 * Anywhere where the user occasionally picks from a long list and
 * is likely to reuse the items.
 */

PersistentList *
persistence_register_list(const gchar *role)
{
  PersistentList *list;
  if (role == NULL) return NULL;
  if (persistent_lists == NULL) {
    persistent_lists = _dia_hash_table_str_any_new();
  } else {   
    list = (PersistentList *)g_hash_table_lookup(persistent_lists, role);
    if (list != NULL) {
      return list;
    }
  }
  list = g_new(PersistentList, 1);
  list->role = role;
  list->glist = NULL;
  list->sorted = FALSE;
  list->max_members = G_MAXINT;
  g_hash_table_insert(persistent_lists, (gchar *)role, list);
  return list;
}

PersistentList *
persistent_list_get(const gchar *role)
{
  PersistentList *list;
  if (role == NULL) return NULL;
  if (persistent_lists != NULL) {
    list = (PersistentList *)g_hash_table_lookup(persistent_lists, role);
    if (list != NULL) {
      return list;
    }
  }
  /* Not registered! */
  return NULL;
}

GList *
persistent_list_get_glist(const gchar *role)
{
  PersistentList *plist = persistent_list_get(role);
  if (plist == NULL) return NULL;
  return plist->glist;
}

static GList *
persistent_list_cut_length(GList *list, guint length)
{
  while (g_list_length(list) > length) {
    GList *last = g_list_last(list);
    /* Leaking data?  See not in persistent_list_add */
    list = g_list_remove_link(list, last);
    g_list_free(last);
  }
  return list;
}

/*!
 * \brief Add a new entry to this persistent list.
 * @param role The name of a persistent list.
 * @param item An entry to add.
 * @return FALSE if the entry already existed in the list, TRUE otherwise.
 */
gboolean
persistent_list_add(const gchar *role, const gchar *item)
{
  PersistentList *plist = persistent_list_get(role);
  if(plist == NULL) {
    g_warning("Can't find list for %s when adding %s", role, item);
    return TRUE;
  }
  if (plist->sorted) {
    /* Sorting not implemented yet. */
    return TRUE;
  } else {
    gboolean existed = FALSE;
    GList *tmplist = plist->glist;
    GList *old_elem = g_list_find_custom(tmplist, item, (GCompareFunc)g_ascii_strcasecmp);
    while (old_elem != NULL) {
      tmplist = g_list_remove_link(tmplist, old_elem);
      /* Don't free this, as it makes recent_files go boom after
       * selecting a file there several times.  Yes, it should be strdup'd,
       * but it isn't.
       */
      /*g_free(old_elem->data);*/
      g_list_free_1(old_elem);
      old_elem = g_list_find_custom(tmplist, item, (GCompareFunc)g_ascii_strcasecmp);
      existed = TRUE;
    }
    tmplist = g_list_prepend(tmplist, g_strdup(item));
    tmplist = persistent_list_cut_length(tmplist, plist->max_members);
    plist->glist = tmplist;
    return existed;
  }
}

void
persistent_list_set_max_length(const gchar *role, gint max)
{
  PersistentList *plist = persistent_list_get(role);
  plist->max_members = max;
  plist->glist = persistent_list_cut_length(plist->glist, max);
}

/*!
 * \brief Remove an item from the persistent list.
 * @param role The name of the persistent list.
 * @param item The entry to remove.
 * @returns TRUE if the item existed in the list, FALSE otherwise.
 */
gboolean
persistent_list_remove(const gchar *role, const gchar *item)
{
  PersistentList *plist = persistent_list_get(role);
  /* Leaking data?  See not in persistent_list_add */
  GList *entry = g_list_find_custom(plist->glist, item, (GCompareFunc)g_ascii_strcasecmp);
  if (entry != NULL) {
    plist->glist = g_list_remove_link(plist->glist, entry);
    g_free(entry->data);
    return TRUE;
  } else return FALSE;
}

void
persistent_list_remove_all(const gchar *role)
{
  PersistentList *plist = persistent_list_get(role);
  persistent_list_cut_length(plist->glist, 0);
  plist->glist = NULL;
}

typedef struct {
  PersistenceCallback func;
  GObject *watch;
  gpointer userdata;
} ListenerData;

/*!
 * \brief Add a listener to a persitence list
 *
 * Add a listener to updates on the list, so that if another
 * instance changes the list, menus and such can be updated.
 * @param role The name of the persistent list to watch.
 * @param func A function to call when the list is updated, takes
 * the given userdata.
 * @param watch GObject to watch
 * @param userdata Data passed back into the callback function.
 */
void
persistent_list_add_listener(const gchar *role, PersistenceCallback func, 
			     GObject *watch, gpointer userdata)
{
  PersistentList *plist = persistent_list_get(role);
  ListenerData *listener;

  if (plist != NULL) {
    listener = g_new(ListenerData, 1);
    listener->func = func;
    listener->watch = watch;
    g_object_add_weak_pointer(watch, (gpointer)&listener->watch);
    listener->userdata = userdata;
    plist->listeners = g_list_append(plist->listeners, listener);
  }
}

/*!
 * \brief Empty the list
 */
void
persistent_list_clear(const gchar *role)
{
  PersistentList *plist = persistent_list_get(role);

  g_list_foreach(plist->glist, (GFunc)g_free, NULL);
  g_list_free(plist->glist);
  plist->glist = NULL;
}

/* ********* INTEGERS ********** */
gint
persistence_register_integer(gchar *role, int defaultvalue)
{
  gint *integer;
  if (role == NULL) return 0;
  if (persistent_integers == NULL) {
    persistent_integers = _dia_hash_table_str_any_new();
  }    
  integer = (gint *)g_hash_table_lookup(persistent_integers, role);
  if (integer == NULL) {
    integer = g_new(gint, 1);
    *integer = defaultvalue;
    g_hash_table_insert(persistent_integers, role, integer);
  }
  return *integer;
}

gint
persistence_get_integer(gchar *role)
{
  gint *integer;
  if (persistent_integers == NULL) {
    g_warning("No persistent integers to get for %s!", role);
    return 0;
  }
  integer = (gint *)g_hash_table_lookup(persistent_integers, role);
  if (integer != NULL) return *integer;
  g_warning("No integer to get for %s", role);
  return 0;
}

void
persistence_set_integer(gchar *role, gint newvalue)
{
  gint *integer;
  if (persistent_integers == NULL) {
    g_warning("No persistent integers yet for %s!", role);
    return;
  }
  integer = (gint *)g_hash_table_lookup(persistent_integers, role);
  if (integer != NULL) 
    *integer = newvalue;
  else 
    g_warning("No integer to set for %s", role);
}

/* ********* REALS ********** */
real
persistence_register_real(gchar *role, real defaultvalue)
{
  real *realval;
  if (role == NULL) return 0;
  if (persistent_reals == NULL) {
    persistent_reals = _dia_hash_table_str_any_new();
  }    
  realval = (real *)g_hash_table_lookup(persistent_reals, role);
  if (realval == NULL) {
    realval = g_new(real, 1);
    *realval = defaultvalue;
    g_hash_table_insert(persistent_reals, role, realval);
  }
  return *realval;
}

real
persistence_get_real(gchar *role)
{
  real *realval;
  if (persistent_reals == NULL) {
    g_warning("No persistent reals to get for %s!", role);
    return 0;
  }
  realval = (real *)g_hash_table_lookup(persistent_reals, role);
  if (realval != NULL) return *realval;
  g_warning("No real to get for %s", role);
  return 0.0;
}

void
persistence_set_real(gchar *role, real newvalue)
{
  real *realval;
  if (persistent_reals == NULL) {
    g_warning("No persistent reals yet for %s!", role);
    return;
  }
  realval = (real *)g_hash_table_lookup(persistent_reals, role);
  if (realval != NULL) 
    *realval = newvalue;
  else 
    g_warning("No real to set for %s", role);
}


/* ********* BOOLEANS ********** */
/*! \brief Returns true if the given role has been registered. */
gboolean
persistence_boolean_is_registered(const gchar *role)
{
  gboolean *booleanval;
  if (role == NULL) return 0;
  if (persistent_booleans == NULL) {
    persistent_booleans = _dia_hash_table_str_any_new();
  }    
  booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role);
  return booleanval != NULL;
}

gboolean
persistence_register_boolean(const gchar *role, gboolean defaultvalue)
{
  gboolean *booleanval;
  if (role == NULL) return 0;
  if (persistent_booleans == NULL) {
    persistent_booleans = _dia_hash_table_str_any_new();
  }    
  booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role);
  if (booleanval == NULL) {
    booleanval = g_new(gboolean, 1);
    *booleanval = defaultvalue;
    g_hash_table_insert(persistent_booleans, role, booleanval);
  }
  return *booleanval;
}

gboolean
persistence_get_boolean(const gchar *role)
{
  gboolean *booleanval;
  if (persistent_booleans == NULL) {
    g_warning("No persistent booleans to get for %s!", role);
    return FALSE;
  }
  booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role);
  if (booleanval != NULL) return *booleanval;
  g_warning("No boolean to get for %s", role);
  return FALSE;
}

void
persistence_set_boolean(const gchar *role, gboolean newvalue)
{
  gboolean *booleanval;
  if (persistent_booleans == NULL) {
    g_warning("No persistent booleans yet for %s!", role);
    return;
  }
  booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role);
  if (booleanval != NULL) 
    *booleanval = newvalue;
  else 
    g_warning("No boolean to set for %s", role);
}

/* ********* STRINGS ********** */
/*!
 * \brief Register a string in persistence.
 * @param role The name used to refer to the string.  Must be unique within
 *             registered strings (and preferably with all registered items)
 * @param defaultvalue A value to use if the role does not exist yet.
 * @returns The value that role has after registering.  The caller is
 *          responsible for freeing this memory.  It will never be the same
 *          memory as defaultvalue.
 */
gchar *
persistence_register_string(gchar *role, gchar *defaultvalue)
{
  gchar *stringval;
  if (role == NULL) return 0;
  if (persistent_strings == NULL) {
    persistent_strings = _dia_hash_table_str_any_new();
  }    
  stringval = (gchar *)g_hash_table_lookup(persistent_strings, role);
  if (stringval == NULL) {
    stringval = g_strdup(defaultvalue);
    g_hash_table_insert(persistent_strings, role, stringval);
  }
  return g_strdup(stringval);
}

gchar *
persistence_get_string(gchar *role)
{
  gchar *stringval;
  if (persistent_strings == NULL) {
    g_warning("No persistent strings to get for %s!", role);
    return NULL;
  }
  stringval = (gchar *)g_hash_table_lookup(persistent_strings, role);
  if (stringval != NULL) return g_strdup(stringval);
  g_warning("No string to get for %s", role);
  return NULL;
}

void
persistence_set_string(gchar *role, const gchar *newvalue)
{
  gchar *stringval;
  if (persistent_strings == NULL) {
    g_warning("No persistent strings yet for %s!", role);
    return;
  }
  stringval = (gchar *)g_hash_table_lookup(persistent_strings, role);
  if (stringval != NULL) {
    g_hash_table_insert(persistent_strings, role, g_strdup(newvalue));
  } else {
    g_hash_table_remove(persistent_strings, role);
  }
}

/* ********* COLORS ********** */
/*!
 * \brief Register a _Color for persistence
 *
 * Remember that colors returned are private, not to be deallocated.
 * They will be smashed in some undefined way by persistence_set_color */
Color *
persistence_register_color(gchar *role, Color *defaultvalue)
{
  Color *colorval;
  if (role == NULL) return 0;
  if (persistent_colors == NULL) {
    persistent_colors = _dia_hash_table_str_any_new();
  }    
  colorval = (Color *)g_hash_table_lookup(persistent_colors, role);
  if (colorval == NULL) {
    colorval = g_new(Color, 1);
    *colorval = *defaultvalue;
    g_hash_table_insert(persistent_colors, role, colorval);
  }
  return colorval;
}

Color *
persistence_get_color(gchar *role)
{
  Color *colorval;
  if (persistent_colors == NULL) {
    g_warning("No persistent colors to get for %s!", role);
    return 0;
  }
  colorval = (Color *)g_hash_table_lookup(persistent_colors, role);
  if (colorval != NULL) return colorval;
  g_warning("No color to get for %s", role);
  return 0;
}

void
persistence_set_color(gchar *role, Color *newvalue)
{
  Color *colorval;
  if (persistent_colors == NULL) {
    g_warning("No persistent colors yet for %s!", role);
    return;
  }
  colorval = (Color *)g_hash_table_lookup(persistent_colors, role);
  if (colorval != NULL) 
    *colorval = *newvalue;
  else 
    g_warning("No color to set for %s", role);
}