summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9e28a5661421183d7d3b3fbe898ca6a34b5e9059 (plain)
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
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
/*
 * main.c
 *
 * Copyright (C) 2002 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Glynn Foster <glynn.foster@sun.com>
 */

#include <config.h>

#include "zenity.h"
#include <stdlib.h>
#include <locale.h>
#include <popt.h>
#include <langinfo.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

typedef enum {
  MODE_CALENDAR,
  MODE_ENTRY,
  MODE_ERROR,
  MODE_FILE,
  MODE_LIST,
  MODE_PROGRESS,
  MODE_QUESTION,
  MODE_TEXTINFO,
  MODE_WARNING,
  MODE_INFO,
  MODE_ABOUT,
  MODE_LAST
} ZenityDialogMode;

typedef enum {
  ERROR_DUPLICATE,
  ERROR_SUPPORT,
  ERROR_DIALOG,
  ERROR_LAST
} ZenityError;

typedef struct {
  ZenityDialogMode mode;
  ZenityData *data;

  ZenityCalendarData *calendar_data;
  ZenityMsgData *msg_data;
  ZenityFileData *file_data;
  ZenityEntryData *entry_data;
  ZenityProgressData *progress_data;
  ZenityTextData *text_data;
  ZenityTreeData *tree_data;
} ZenityParsingOptions;

enum {
  OPTION_CALENDAR = 1,
  OPTION_DATEFORMAT,
  OPTION_ENTRY,
  OPTION_ERROR,
  OPTION_INFO,
  OPTION_FILE,
  OPTION_LIST,
  OPTION_PROGRESS,
  OPTION_QUESTION,
  OPTION_TEXTINFO,
  OPTION_TEXTEDIT,
  OPTION_WARNING,
  OPTION_TITLE,
  OPTION_ICON,
  OPTION_WIDTH,
  OPTION_HEIGHT,
  OPTION_CALENDARTEXT,
  OPTION_DAY,
  OPTION_MONTH,
  OPTION_YEAR,
  OPTION_ENTRYTEXT,
  OPTION_INPUTTEXT,
  OPTION_HIDETEXT,
  OPTION_ERRORTEXT,
  OPTION_INFOTEXT,
  OPTION_FILENAME,
  OPTION_MULTIFILE,
  OPTION_TEXTFILENAME,
  OPTION_LISTTEXT,
  OPTION_COLUMN,
  OPTION_SEPERATOR,
  OPTION_LISTEDIT,
  OPTION_CHECKLIST,
  OPTION_RADIOLIST,
  OPTION_PROGRESSTEXT,
  OPTION_PERCENTAGE,
  OPTION_PULSATE,
  OPTION_AUTOCLOSE,
  OPTION_QUESTIONTEXT,
  OPTION_WARNINGTEXT,
  OPTION_ABOUT,
  OPTION_VERSION,
  OPTION_LAST,
};

static void zenity_parse_options_callback (poptContext              ctx,
    enum poptCallbackReason  reason,
    const struct poptOption *opt,
    const char              *arg,
    void                    *data);

struct poptOption options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "calendar",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_CALENDAR,
    N_("Display calendar dialog"),
    NULL
  },
  {
    "entry",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_ENTRY,
    N_("Display text entry dialog"),
    NULL
  },
  {
    "error",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_ERROR,
    N_("Display error dialog"),
    NULL
  },
  {
    "file-selection",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_FILE,
    N_("Display file selection dialog"),
    NULL
  },
  {
    "info",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_INFO,
    N_("Display info dialog"),
    NULL
  },
  {
    "list",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_LIST,
    N_("Display list dialog"),
    NULL
  },
  {
    "progress",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_PROGRESS,
    N_("Display progress indication dialog"),
    NULL
  },
  {
    "question",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_QUESTION,
    N_("Display question dialog"),
    NULL
  },
  {
    "text-info",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_TEXTINFO,
    N_("Display text information dialog"),
    NULL
  },
  {
    "warning",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_WARNING,
    N_("Display warning dialog"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption general_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "title",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_TITLE,
    N_("Set the dialog title"),
    N_("TITLE")
  },
  {
    "window-icon",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_ICON,
    N_("Set the window icon"),
    N_("ICONPATH")
  },
  {
    "width",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_WIDTH,
    N_("Set the width"),
    N_("WIDTH")
  },
  {
    "height",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_HEIGHT,
    N_("Set the height"),
    N_("HEIGHT")
  },
  POPT_TABLEEND
};

struct poptOption calendar_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_CALENDARTEXT,
    N_("Set the dialog text"),
    NULL
  },
  {
    "day",
    '\0',
    POPT_ARG_INT,
    NULL,
    OPTION_DAY,
    N_("Set the calendar day"),
    NULL
  },
  {
    "month",
    '\0',
    POPT_ARG_INT,
    NULL,
    OPTION_MONTH,
    N_("Set the calendar month"),
    NULL
  },
  {
    "year",
    '\0',
    POPT_ARG_INT,
    NULL,
    OPTION_YEAR,
    N_("Set the calendar year"),
    NULL
  },
  {	"date-format",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_DATEFORMAT,
    N_("Set the format for the returned date"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption entry_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_ENTRYTEXT,
    N_("Set the dialog text"),
    NULL
  },
  {
    "entry-text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_INPUTTEXT,
    N_("Set the entry text"),
    NULL
  },
  {
    "hide-text",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_HIDETEXT,
    N_("Hide the entry text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption error_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_ERRORTEXT,
    N_("Set the dialog text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption info_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_INFOTEXT,
    N_("Set the dialog text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption file_selection_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "filename",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_FILENAME,
    N_("Set the filename"),
    N_("FILENAME")
  },
  {
    "multiple",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_MULTIFILE,
    N_("Allow multiple files to be selected"),
    NULL
  },
  {
    "separator",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_SEPERATOR,
    N_("Set output separator character."),
    N_("SEPARATOR")
  },
  POPT_TABLEEND
};

struct poptOption list_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_LISTTEXT,
    N_("Set the dialog text"),
    NULL
  },
  {
    "column",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_COLUMN,
    N_("Set the column header"),
    NULL
  },
  {
    "checklist",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_CHECKLIST,
    N_("Use check boxes for first column"),
    NULL
  },
  {
    "radiolist",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_RADIOLIST,
    N_("Use radio buttons for first column"),
    NULL
  },
  {
    "separator",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_SEPERATOR,
    N_("Set output separator character"),
    N_("SEPARATOR")
  },
  {
    "editable",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_LISTEDIT,
    N_("Allow changes to text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption progress_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_PROGRESSTEXT,
    N_("Set the dialog text"),
    NULL
  },
  {
    "percentage",
    '\0',
    POPT_ARG_INT,
    NULL,
    OPTION_PERCENTAGE,
    N_("Set initial percentage"),
    NULL
  },
  {
    "pulsate",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_PULSATE,
    N_("Pulsate progress bar"),
    NULL
  },
  {
    "auto-close",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_AUTOCLOSE,
    /* xgettext: no-c-format */
    N_("Dismiss the dialog when 100% has been reached"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption question_options[] = {
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_QUESTIONTEXT,
    N_("Set the dialog text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption text_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "filename",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_TEXTFILENAME,
    N_("Open file"),
    N_("FILENAME")
  },
  {
    "editable",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_TEXTEDIT,
    N_("Allow changes to text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption warning_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "text",
    '\0',
    POPT_ARG_STRING,
    NULL,
    OPTION_WARNINGTEXT,
    N_("Set the dialog text"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption gtk_options[] = {
  {
    "gdk-debug", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0,
    N_("Gdk debugging flags to set"), 
    N_("FLAGS")
  }, 
  {
    "gdk-no-debug", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Gdk debugging flags to unset"), 
    N_("FLAGS")
  }, 
  /* X11 only */ 
  {
    "display",
    '\0',
    POPT_ARG_STRING,
    NULL,
    0, 
    N_("X display to use"),
    N_("DISPLAY")
  }, 
#ifdef HAVE_GTK_MULTIHEAD 
  /* X11 & multi-head only */ 
  {
    "screen",
    '\0', 
    POPT_ARG_INT, 
    NULL, 
    0, 
    N_("X screen to use"), 
    N_("SCREEN")
  },
#endif 
  /* X11 only */ 
  {
    "sync", 
    '\0', 
    POPT_ARG_NONE, 
    NULL, 
    0, 
    N_("Make X calls synchronous"),
    NULL
  },
  {
    "name", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Program name as used by the window manager"), 
    N_("NAME")
  }, 
  {
    "class", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Program class as used by the window manager"), 
    N_("CLASS")
  }, 
  /* X11 only */ 
  {
    "gxid-host",
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    NULL, 
    N_("HOST")
  }, 
  /* X11 only */ 
  {
    "gxid-port",
    '\0',
    POPT_ARG_STRING, 
    NULL, 
    0, 
    NULL, 
    N_("PORT")
  }, 
  {
    "gtk-debug", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Gtk+ debugging flags to set"), 
    N_("FLAGS")
  }, 
  { 
    "gtk-no-debug",
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Gtk+ debugging flags to unset"), 
    N_("FLAGS")
  }, 
  { 
    "g-fatal-warnings", 
    '\0', 
    POPT_ARG_NONE, 
    NULL, 
    0, 
    N_("Make all warnings fatal"), 
    NULL
  }, 
  { 
    "gtk-module", 
    '\0', 
    POPT_ARG_STRING, 
    NULL, 
    0, 
    N_("Load an additional Gtk module"), 
    N_("MODULE")
  },
  POPT_TABLEEND
};

struct poptOption miscellaneous_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_CALLBACK | POPT_CBFLAG_POST,
    zenity_parse_options_callback,
    0,
    NULL,
    NULL
  },
  {
    "about",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_ABOUT,
    N_("About zenity"),
    NULL
  },
  {
    "version",
    '\0',
    POPT_ARG_NONE,
    NULL,
    OPTION_VERSION,
    N_("Print version"),
    NULL
  },
  POPT_TABLEEND
};

struct poptOption application_options[] = {
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    options,
    0,
    N_("Dialog options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    general_options,
    0,
    N_("General options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    calendar_options,
    0,
    N_("Calendar options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    entry_options,
    0,
    N_("Text entry options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    error_options,
    0,
    N_("Error options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    file_selection_options,
    0,
    N_("File selection options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    info_options,
    0,
    N_("Info options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    list_options,
    0,
    N_("List options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    progress_options,
    0,
    N_("Progress options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    question_options,
    0,
    N_("Question options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    text_options,
    0,
    N_("Text options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    warning_options,
    0,
    N_("Warning options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    gtk_options,
    0,
    N_("GTK+ options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    miscellaneous_options,
    0,
    N_("Miscellaneous options"),
    NULL
  },
  {
    NULL,
    '\0',
    POPT_ARG_INCLUDE_TABLE,
    poptHelpOptions,
    0,
    N_("Help options"), 
    NULL
  },
  POPT_TABLEEND
};

ZenityParsingOptions *results;

static void
zenity_init_parsing_options (void) {

  results = g_new0 (ZenityParsingOptions, 1);

  /* Initialize the various dialog structures */
  results->mode = MODE_LAST;
  results->data = g_new0 (ZenityData, 1);
  results->calendar_data = g_new0 (ZenityCalendarData, 1);
  results->msg_data = g_new0 (ZenityMsgData, 1);
  results->file_data = g_new0 (ZenityFileData, 1);
  results->entry_data = g_new0 (ZenityEntryData, 1); 
  results->progress_data = g_new0 (ZenityProgressData, 1); 
  results->text_data = g_new0 (ZenityTextData, 1);
  results->tree_data = g_new0 (ZenityTreeData, 1);

  /* Give some sensible defaults */
  results->data->width = -1;
  results->data->height = -1;
  results->calendar_data->date_format = g_locale_to_utf8 (nl_langinfo (D_FMT),
							  -1,
							  NULL, NULL, NULL);
  results->calendar_data->day = 0;
  results->calendar_data->month = 0;
  results->calendar_data->year = 0;
  results->calendar_data->dialog_text = NULL;
  results->file_data->multi = FALSE;
  results->file_data->separator = g_strdup ("|");
  results->text_data->editable = FALSE;
  results->tree_data->separator = g_strdup ("|");
  results->progress_data->percentage = -1;
  results->progress_data->pulsate = FALSE;
  results->progress_data->autoclose = FALSE;
  results->entry_data->visible = TRUE;
  results->tree_data->dialog_text = NULL;
  results->tree_data->checkbox = FALSE;
  results->tree_data->radiobox = FALSE;
  results->tree_data->editable = FALSE;
}

static void
zenity_free_parsing_options (void) {

  /* General options */
  if (results->data->dialog_title)
    g_free (results->data->dialog_title);
  if (results->data->window_icon)
    g_free (results->data->window_icon);

  /* Dialog options */
  switch (results->mode) {
    case MODE_CALENDAR:
      if (results->calendar_data->dialog_text)
        g_free (results->calendar_data->dialog_text);
      if (results->calendar_data->date_format)
        g_free (results->calendar_data->date_format);
      break;
    case MODE_ENTRY:
      if (results->entry_data->dialog_text)
        g_free (results->entry_data->dialog_text);
      if (results->entry_data->entry_text)
        g_free (results->entry_data->entry_text);
      break;
    case MODE_ERROR:
    case MODE_QUESTION:
    case MODE_WARNING:
    case MODE_INFO:
      if (results->msg_data->dialog_text)
        g_free (results->msg_data->dialog_text);
      break;
    case MODE_FILE:
      if (results->file_data->uri)
        g_free (results->file_data->uri);
	g_free (results->file_data->separator);
      break;
    case MODE_TEXTINFO:
      if (results->text_data->uri)
        g_free (results->text_data->uri);
      break;
    case MODE_LIST:
      if (results->tree_data->dialog_text)
        g_free (results->tree_data->dialog_text);
      if (results->tree_data->columns)
        g_slist_foreach (results->tree_data->columns, (GFunc) g_free, NULL);
      if (results->tree_data->separator)
        g_free (results->tree_data->separator);
      break;
    default:
      break;
  }
}

gint 
main (gint argc, gchar **argv) {
  poptContext ctx;
  gint nextopt, retval;

#ifdef HAVE_LOCALE_H
  setlocale(LC_ALL,"");
#endif

  bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  textdomain(GETTEXT_PACKAGE);

  zenity_init_parsing_options ();

  /* FIXME: popt doesn't like passing stuff through data 
   * but it doesn't seem to cope with the data that I try
   * to pass in, not quite sure why though. If someone knows
   * what I'm doing wrong, we could probably put this back:
   * options[0].descrip = (void*) results; 
   */

  ctx = poptGetContext ("zenity", argc, (const char **)argv, application_options, 0);

  poptReadDefaultConfig(ctx, TRUE);
  while((nextopt = poptGetNextOpt(ctx)) > 0)
    /*nothing*/;

  if (nextopt != -1) {
    g_printerr (_("%s is an invalid option. See 'zenity --help' for more details\n"),
                  poptBadOption (ctx, 0));
    zenity_free_parsing_options (); 
    exit (-1); 
  }
  gtk_init (&argc, &argv);

  /* 
   * if (argc < 2) {
   *   g_printerr (_("You must specify more arguments. See zenity --help for more details\n"));
   *   zenity_free_parsing_options (); 
   *   exit (-1); 
   * }
   */

  switch (results->mode) {
    case MODE_CALENDAR:
      zenity_calendar (results->data, results->calendar_data);
      break;
    case MODE_ENTRY:
      zenity_entry (results->data, results->entry_data);
      break;
    case MODE_ERROR:
    case MODE_QUESTION:
    case MODE_WARNING:
    case MODE_INFO:
      zenity_msg (results->data, results->msg_data);
      break;
    case MODE_FILE:
      zenity_fileselection (results->data, results->file_data);
      break;
    case MODE_LIST:
      results->tree_data->data = poptGetArgs (ctx);
      zenity_tree (results->data, results->tree_data);
      break;
    case MODE_PROGRESS:
      zenity_progress (results->data, results->progress_data);
      break;
    case MODE_TEXTINFO:
      zenity_text (results->data, results->text_data);
      break;
    case MODE_ABOUT:
      zenity_about (results->data);
      break;
    case MODE_LAST:
      g_printerr (_("You must specify a dialog type. See 'zenity --help' for details\n"));
      zenity_free_parsing_options ();
      exit (-1);
    default:
      g_assert_not_reached ();
      zenity_free_parsing_options ();
      exit (-1);
  }

  retval = results->data->exit_code;
  poptFreeContext(ctx);
  zenity_free_parsing_options ();
  exit (retval);
}

static void
zenity_error (gchar *string, ZenityError error)
{
  switch (error) {
    case ERROR_DUPLICATE:
      g_printerr (_("%s given twice for the same dialog\n"), string);
      zenity_free_parsing_options ();
      exit (-1);
    case ERROR_SUPPORT:
      g_printerr (_("%s is not supported for this dialog\n"), string);
      zenity_free_parsing_options ();
      exit (-1);
    case ERROR_DIALOG:
      g_printerr (_("Two or more dialog options specified\n"));
      zenity_free_parsing_options ();
      exit (-1);
    default:
      return;
  }
}

static void 
zenity_parse_options_callback (poptContext              ctx,
                               enum poptCallbackReason  reason,
                               const struct poptOption *opt,
                               const char              *arg,
                               void                    *data)
{
  static gboolean parse_option_dateformat = FALSE;
  static gboolean parse_option_separator = FALSE;
  static gint parse_option_text = 0;
  static gint parse_option_file = 0;
  static gint parse_option_editable = 0;
  
  if (reason == POPT_CALLBACK_REASON_POST)
    return;
  else if (reason != POPT_CALLBACK_REASON_OPTION)
    return;

  switch (opt->val & POPT_ARG_MASK) {

    case OPTION_CALENDAR:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_CALENDAR;
      break;
    case OPTION_ENTRY:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_ENTRY;
      break;
    case OPTION_ERROR:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_ERROR;
      results->msg_data->mode = ZENITY_MSG_ERROR;
      break;
    case OPTION_INFO:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_INFO;
      results->msg_data->mode = ZENITY_MSG_INFO;
      break;
    case OPTION_FILE:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);
			
      results->mode = MODE_FILE;
      break;
    case OPTION_LIST:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_LIST;
      break;
    case OPTION_PROGRESS:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);
			
      results->mode = MODE_PROGRESS;
      break;
    case OPTION_QUESTION:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);
			
      results->mode = MODE_QUESTION; 
      results->msg_data->mode = ZENITY_MSG_QUESTION; 
      break;
    case OPTION_TEXTINFO:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_TEXTINFO; 
      break;
    case OPTION_WARNING:
      if (results->mode != MODE_LAST)
        zenity_error (NULL, ERROR_DIALOG);

      results->mode = MODE_WARNING; 
      results->msg_data->mode = ZENITY_MSG_WARNING; 
      break;
    case OPTION_TITLE:
      if (results->data->dialog_title != NULL)
        zenity_error ("--title", ERROR_DUPLICATE);

      results->data->dialog_title = g_strdup (arg); 
      break; 
    case OPTION_ICON: 
      if (results->data->window_icon != NULL) 
        zenity_error ("--window-icon", ERROR_DUPLICATE); 
      
      results->data->window_icon = g_strdup (arg); 
      break; 
    case OPTION_WIDTH: 
      if (results->data->width != -1) 
        zenity_error ("--width", ERROR_DUPLICATE); 
      
      results->data->width = atoi (arg); 
      break; 
    case OPTION_HEIGHT: 
      if (results->data->height != -1) 
        zenity_error ("--height", ERROR_DUPLICATE); 
      
      results->data->height = atoi (arg); 
      break; 
    case OPTION_CALENDARTEXT: 
    case OPTION_ENTRYTEXT: 
    case OPTION_ERRORTEXT: 
    case OPTION_QUESTIONTEXT: 
    case OPTION_PROGRESSTEXT: 
    case OPTION_LISTTEXT: 
    case OPTION_WARNINGTEXT: 
      
      /* FIXME: This is an ugly hack because of the way the poptOptions are 
       * ordered above. When you try and use an --option more than once 
       * parse_options_callback gets called for each option. Suckage 
       */

      if (parse_option_text > 7) 
        zenity_error ("--text", ERROR_DUPLICATE); 
     
      switch (results->mode) { 
        case MODE_CALENDAR: 
          results->calendar_data->dialog_text = g_locale_to_utf8 (g_strcompress (arg),
								  -1, NULL, NULL, NULL);
          break; 
        case MODE_ENTRY: 
          results->entry_data->dialog_text = g_locale_to_utf8 (g_strcompress (arg),
							       -1, NULL, NULL, NULL);
          break; 
        case MODE_ERROR: 
        case MODE_QUESTION: 
        case MODE_WARNING: 
        case MODE_INFO: 
          results->msg_data->dialog_text = g_locale_to_utf8 (g_strcompress (arg),
							     -1, NULL, NULL, NULL);
          break; 
        case MODE_PROGRESS: 
          results->progress_data->dialog_text = g_locale_to_utf8 (g_strcompress (arg),
								  -1, NULL, NULL, NULL);
          break; 
        case MODE_LIST: 
          results->tree_data->dialog_text = g_locale_to_utf8 (g_strcompress (arg),
						              -1, NULL, NULL, NULL);
          break; 
        default: 
          zenity_error ("--text", ERROR_SUPPORT); 
      } 
      parse_option_text++; 
      break; 
    case OPTION_DAY: 
      if (results->mode != MODE_CALENDAR) 
        zenity_error ("--day", ERROR_SUPPORT); 
      
      if (results->calendar_data->day > 0) 
        zenity_error ("--day", ERROR_DUPLICATE); 

      results->calendar_data->day = atoi (arg); 
      break; 
    case OPTION_MONTH: 
      if (results->mode != MODE_CALENDAR) 
        zenity_error ("--month", ERROR_SUPPORT); 
      
      if (results->calendar_data->month > 0) 
        zenity_error ("--day", ERROR_DUPLICATE); 
      
      results->calendar_data->month = atoi (arg); 
      break; 
    case OPTION_YEAR: 
      if (results->mode != MODE_CALENDAR) 
        zenity_error ("--year", ERROR_SUPPORT); 
      
      if (results->calendar_data->year > 0) 
        zenity_error ("--year", ERROR_DUPLICATE); 
      
      results->calendar_data->year = atoi (arg); 
      break; 
    case OPTION_DATEFORMAT: 
      if (results->mode != MODE_CALENDAR) 
        zenity_error ("--date-format", ERROR_SUPPORT); 
      
      if (parse_option_dateformat) 
        zenity_error ("--date-format", ERROR_DUPLICATE); 
      
      results->calendar_data->date_format = g_strdup (arg); 
      parse_option_dateformat = TRUE; 
      break; 
    case OPTION_INPUTTEXT: 
      if (results->mode != MODE_ENTRY) 
        zenity_error ("--entry-text", ERROR_SUPPORT); 
      
      if (results->entry_data->entry_text != NULL) 
        zenity_error ("--entry-text", ERROR_DUPLICATE); 
      
      results->entry_data->entry_text = g_strdup (arg); 
      break; 
    case OPTION_HIDETEXT: 
      if (results->mode != MODE_ENTRY) 
        zenity_error ("--hide-text", ERROR_SUPPORT); 
      
      if (!results->entry_data->visible) 
        zenity_error ("--hide-text", ERROR_DUPLICATE); 
      
      results->entry_data->visible = FALSE; 
      break; 
    case OPTION_LISTEDIT: 
    case OPTION_TEXTEDIT: 
      
      /* FIXME: This is an ugly hack because of the way the poptOptions are 
       * ordered above. When you try and use an --option more than once 
       * parse_options_callback gets called for each option. Suckage 
       */ 
      
      if (parse_option_editable > 2) 
        zenity_error ("--editable", ERROR_DUPLICATE); 
      
      switch (results->mode) { 
        case MODE_TEXTINFO: 
          results->text_data->editable = TRUE; 
          break; 
        case MODE_LIST: 
          results->tree_data->editable = TRUE; 
          break; 
        default: 
          zenity_error ("--editable", ERROR_SUPPORT); 
      } 
      parse_option_editable++; 
      break; 
    case OPTION_FILENAME: 
    case OPTION_TEXTFILENAME:
      
      /* FIXME: This is an ugly hack because of the way the poptOptions are 
       * ordered above. When you try and use an --option more than once 
       * parse_options_callback gets called for each option. Suckage 
       */ 
      
      if (parse_option_file > 2) 
        zenity_error ("--filename", ERROR_DUPLICATE); 
      
      switch (results->mode) { 
        case MODE_FILE: 
          results->file_data->uri = g_strdup (arg); 
          break; 
        case MODE_TEXTINFO: 
          results->text_data->uri = g_strdup (arg); 
          break; 
        default: 
          zenity_error ("--filename", ERROR_SUPPORT); 
      } 
      parse_option_file++; 
      break; 
    case OPTION_MULTIFILE:
      if (results->mode != MODE_FILE)
        zenity_error ("--multiple", ERROR_SUPPORT);

      results->file_data->multi = TRUE;
      break;
    case OPTION_COLUMN: 
      if (results->mode != MODE_LIST) 
        zenity_error ("--column", ERROR_SUPPORT); 
      
      results->tree_data->columns = g_slist_append (results->tree_data->columns, g_strdup (arg)); 
      break; 
    case OPTION_CHECKLIST: 
      if (results->mode != MODE_LIST) 
        zenity_error ("--checklist", ERROR_SUPPORT); 
      
      if (results->tree_data->checkbox) 
        zenity_error ("--checklist", ERROR_DUPLICATE); 
      
      results->tree_data->checkbox = TRUE; 
      break; 
    case OPTION_RADIOLIST: 
      if (results->mode != MODE_LIST) 
        zenity_error ("--radiolist", ERROR_SUPPORT); 
      if (results->tree_data->radiobox) 
        zenity_error ("--radiolist", ERROR_DUPLICATE); 
      
      results->tree_data->radiobox = TRUE; 
      break; 
    case OPTION_SEPERATOR: 
      if (parse_option_separator > 2)
	zenity_error ("--separator", ERROR_DUPLICATE);
      switch (results->mode) {
	case MODE_LIST: 
      	  results->tree_data->separator = g_strdup (g_strcompress (arg)); 
      	  parse_option_separator++; 
	  break;
	case MODE_FILE:
	  results->file_data->separator = g_strdup (g_strcompress (arg));
	  parse_option_separator++;
	  break;
	default:
	  zenity_error ("--separator", ERROR_SUPPORT);
	}
      break;
    case OPTION_PERCENTAGE: 
      if (results->mode != MODE_PROGRESS) 
        zenity_error ("--percentage", ERROR_SUPPORT); 
      
      if (results->progress_data->percentage > -1) 
        zenity_error ("--percentage", ERROR_DUPLICATE); 
      
      results->progress_data->percentage = atoi (arg); 
      break; 
    case OPTION_PULSATE: 
      if (results->mode != MODE_PROGRESS) 
        zenity_error ("--pulsate", ERROR_SUPPORT); 
      
      results->progress_data->pulsate = TRUE; 
      break; 
    case OPTION_AUTOCLOSE:
      if (results->mode != MODE_PROGRESS)
        zenity_error ("--auto-close", ERROR_SUPPORT);

      results->progress_data->autoclose = TRUE;
      break;
    case OPTION_ABOUT: 
      if (results->mode != MODE_LAST) 
        zenity_error (NULL, ERROR_DIALOG); 
      
      results->mode = MODE_ABOUT; 
      break; 
    case OPTION_VERSION: 
      if (results->mode != MODE_LAST) 
        zenity_error (NULL, ERROR_DIALOG); 
      zenity_free_parsing_options (); 
      g_print ("%s\n", VERSION); 
      exit (0); 
      break; 
    default: 
      break; 
  } 
}
bgstack15