File: misc.c

package info (click to toggle)
dvdisaster 0.72.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 20,088 kB
  • ctags: 6,526
  • sloc: ansic: 29,301; php: 3,324; sh: 370; xml: 296; makefile: 73; cs: 33
file content (1100 lines) | stat: -rw-r--r-- 24,915 bytes parent folder | download | duplicates (2)
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
/*  dvdisaster: Additional error correction for optical media.
 *  Copyright (C) 2004-2012 Carsten Gnoerlich.
 *  Project home page: http://www.dvdisaster.com
 *  Email: carsten@dvdisaster.com  -or-  cgnoerlich@fsfe.org
 *
 *  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,
 *  or direct your browser at http://www.gnu.org.
 */

#include "dvdisaster.h"

/***
 *** gettext() convenience
 ***/

/*
 * The usual gettext() wrapper so that we can use strings
 * like "file!quit" in menus.
 */

char *sgettext(char *msgid)
{  char *msgval;

#ifdef WITH_NLS_YES
   msgval = gettext(msgid);
#else
   msgval = msgid;
#endif

   /*** If translating menu labels fails, try to make the default nicer. */
   
   if(msgval == msgid)  
   {  msgval = strrchr(msgid, '|');
      if(msgval) return msgval + 1;
      else       return msgid;
   }

   return msgval;
}

char *sgettext_utf8(char *msgid)
{  static gchar ringbuf[20][1024];
   static int ringptr;
   char *msgval;

#ifdef WITH_NLS_YES
   msgval = gettext(msgid);
#else
   msgval = msgid;
#endif

   /*** If translating menu labels fails, try to make the default nicer. */
   
   if(msgval == msgid)  
   {  msgval = strrchr(msgid, '|');
      if(msgval) return msgval + 1;
      else       return msgid;
   }

   /*** If we are running the GUI, convert to UTF8 for Gtk+ */

   if(Closure->guiMode)
   {  char *msg_utf8 = g_locale_to_utf8(msgval, -1, NULL, NULL, NULL);

      ringptr = (ringptr + 1) % 20;
      g_strlcpy(ringbuf[ringptr], msg_utf8, 1023);
      g_free(msg_utf8);
      
      return ringbuf[ringptr];
   }

   return msgval;
}

/***
 *** Format conversion
 ***/

/*
 * Convert between 8 bytes of unsigned char and gint64.
 * Needed since the EccHeader contains misaligned 64bit values
 * for historical reasons.
 * The uchar version uses a little endian representation.
 */

gint64 uchar_to_gint64(unsigned char *bytes)
{  gint64 out;

   memcpy(&out, bytes, sizeof(gint64));

#ifdef HAVE_BIG_ENDIAN
   return SwapBytes64(out);
#else
   return out;
#endif
}

void gint64_to_uchar(unsigned char *out, gint64 in)
{
#ifdef HAVE_BIG_ENDIAN
  in = SwapBytes64(in);
#endif

   memcpy(out, &in, sizeof(in));
}

/*
 * Calculate total number of sectors (including the last sector)
 * and the remaining bytes in the last sector for a given medium size.
 */

void CalcSectors(gint64 size, gint64 *sectors, int *in_last)
{
   *sectors = size/2048;
   *in_last = size & 0x7ff; 
   if(*in_last>0) (*sectors)++;
   else *in_last = 2048;
}

/***
 *** Message output
 ***/

/*
 * Append message to the log window.
 */

#define MAX_LOG_WIN_SIZE 10240

static void clamp_gstring(GString *string)
{  gchar *ptr;
   int cut;


   if(string->len < MAX_LOG_WIN_SIZE)
      return;

   /* Remove head of the string so that it gets smaller than
      the maximum size, and cut off until the next newline */

   ptr = string->str;
   cut = string->len - MAX_LOG_WIN_SIZE;
   ptr += cut;

   while(*ptr && *ptr != '\n')
   {  ptr++;
      cut++;
   }

   g_string_erase(string, 0, cut);
}

static void log_window_vprintf(char *format, va_list argp)
{  char *tmp,*utf_tmp;

   tmp = g_strdup_vprintf(format, argp);
   utf_tmp = g_locale_to_utf8(tmp, -1, NULL, NULL, NULL);

   g_mutex_lock(Closure->logLock);
   g_string_append(Closure->logString, utf_tmp);
   clamp_gstring(Closure->logString);
   g_mutex_unlock(Closure->logLock);

   UpdateLog();
   
   g_free(tmp);
   g_free(utf_tmp);
}

static void log_window_append(char *text)
{  char *utf_tmp = g_locale_to_utf8(text, -1, NULL, NULL, NULL);

   g_mutex_lock(Closure->logLock);
   g_string_append(Closure->logString, utf_tmp);
   clamp_gstring(Closure->logString);
   g_mutex_unlock(Closure->logLock);

   UpdateLog();
   
   g_free(utf_tmp);
}

/*
 * Output of the greetings is delayed until the first message is printed.
 */

static void print_greetings(FILE *where)
{  static int greetings_shown;
   
   if(greetings_shown) return;

   greetings_shown = 1;
   g_fprintf(where, _("dvdisaster-%s%sCopyright 2004-2012 Carsten Gnoerlich.\n"),
	     VERSION, strstr(VERSION,"pl") ? " " : "  ");
   /* TRANSLATORS: Excluding all kinds of warranty might be harmful under your
      legislature. If in doubt, just translate the following like "This is free
      software; please refer to the conditions of the GNU GENERAL PUBLIC LICENSE
      in the source code." Avoid making any legal statements by your own.*/
   g_fprintf(where, "%s",
	            _("This software comes with  ABSOLUTELY NO WARRANTY.  This\n"
		      "is free software and you are welcome to redistribute it\n"
		      "under the conditions of the GNU GENERAL PUBLIC LICENSE.\n"  
		      "See the file \"COPYING\" for further information.\n"));
}

/*
 * Print to stdout if run from the command line;
 * do nothing in GUI mode unless Closure->verbose is set.
 */

void PrintCLI(char *format, ...)
{  va_list argp;

   if(Closure->logFileEnabled)
   {  va_start(argp, format);
      VPrintLogFile(format, argp);
      va_end(argp);
   }

   if(Closure->guiMode)
   {  if(Closure->verbose)
      {  va_start(argp, format);
	 log_window_vprintf(format, argp);
	 va_end(argp);
      }
      return;
   }

   va_start(argp, format);
   g_vfprintf(stdout, format, argp);
   va_end(argp);

   fflush(stdout);   /* at least needed for Windows */
}

/*
 * Print progress to stderr.
 * Returns cursor to first character in the line
 * if the message contains no newlines. 
 * Does nothing in GUI mode.
 */

void PrintProgress(char *format, ...)
{  static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
   char msg[256];
   va_list argp;
   int n;

   if(Closure->guiMode)
     return;
  
   print_greetings(stderr);

   va_start(argp, format);
   g_vsnprintf(msg, 256, format, argp);
#ifdef SYS_MINGW
   n = strlen(msg);  /* UTF-8 is broken under Windows */
#else
   n = g_utf8_strlen(msg,-1);
#endif
   va_end(argp);

   if(n>255) 
   {  n = 255;
      msg[255] = 0;
   }

   if(strchr(msg, '\n'))
      g_fprintf(stderr, "%s", msg);
   else
   {  g_static_mutex_lock(&mutex);
      Closure->bs[n] = 0;
      g_fprintf(stderr, "%s%s", msg, Closure->bs);
      Closure->bs[n] = '\b';
      g_static_mutex_unlock(&mutex);
   }

   fflush(stderr);   /* at least needed for Windows */
}

/*
 * Print a message to both stderr and the log window
 */

void PrintLog(char *format, ...)
{  va_list argp;

   if(Closure->logFileEnabled)
   {  va_start(argp, format);
      VPrintLogFile(format, argp);
      va_end(argp);
   }

   va_start(argp, format);

   if(Closure->guiMode)
      log_window_vprintf(format, argp);
   else 
   {
      print_greetings(stderr);
      g_vfprintf(stderr, format, argp);

      fflush(stderr);   /* at least needed for Windows */
   }

   va_end(argp);
}

/*
 * Same as PrintLog(), but does nothing unless Closure->verbose is true
 */

void Verbose(char *format, ...)
{  va_list argp;

   if(Closure->logFileEnabled)
   {  va_start(argp, format);
      VPrintLogFile(format, argp);
      va_end(argp);
   }

   if(!Closure->verbose)
    return;

   va_start(argp, format);

   if(Closure->guiMode)
      log_window_vprintf(format, argp);
   else 
   {
      print_greetings(stderr);
      g_vfprintf(stderr, format, argp);

      fflush(stderr);   /* at least needed for Windows */
   }

   va_end(argp);
}

/*
 * Print timing results to console and log window 
 */

void PrintTimeToLog(GTimer *timer, char *format, ...)
{  va_list argp;
   gulong ignore;
   double elapsed = g_timer_elapsed(timer, &ignore); 
   double seconds = fmod(elapsed,60.0);
   int minutes = (int)fmod(elapsed / 60.0, 60.0);
   int hours = (int)(elapsed / 3600.0);
   char *tmp1,*tmp2;

   if(!Closure->verbose)
     return;

   va_start(argp, format);
   tmp1 = g_strdup_vprintf(format, argp);
   tmp2 = g_strdup_printf("%02d:%02d:%04.1f %s", hours, minutes, seconds, tmp1);
   va_end(argp);

   if(Closure->guiMode)
   { 
      log_window_append(tmp2);
   }
   else
   {  g_fprintf(stderr, "%s", tmp2);

      fflush(stderr);   /* at least needed for Windows */
   }

   g_free(tmp1);
   g_free(tmp2);
}

/*
 * Print a message either to the console
 * or show it in the given label
 */

void PrintCLIorLabel(GtkLabel *label, char *format, ...)
{  va_list argp;

   if(Closure->logFileEnabled)
   {  va_start(argp, format);
      VPrintLogFile(format, argp);
      va_end(argp);
   }

   va_start(argp, format);
      
   if(Closure->guiMode)
   {  char *c,*tmp;
	
      tmp = g_strdup_vprintf(format, argp);

      if(Closure->verbose)
	 log_window_append(tmp);

      c   = tmp + strlen(tmp) - 1; 
      while(*c == '\n')          /* Remove trailing newlines */
	*c-- = 0;

      SetLabelText(label, tmp);  /* converts to utf8 by itself */
   
      g_free(tmp);
   }
   else
   {  g_vprintf(format, argp);

      fflush(stdout);   /* at least needed for Windows */
   }

   va_end(argp);
}

/*
 * Issue a warning message to stderr and the log
 */

static void vlog_warning(char *format, va_list argp)
{  char *warn = _("Warning"); 
   int len = strlen(warn)+4;
   char prefix[len+1]; 
   char *c,*line;
   char *body;
   GString *str;

   body = g_strdup_vprintf(format, argp);

   line = body;
   memset(prefix, ' ', len);
   prefix[0] = '*';
   prefix[len] = 0;

   str = g_string_sized_new(256);
   g_string_append_printf(str,"* %s:%c", warn, Closure->guiMode ? '\n' : ' ');
   do
   {  c = strchr(line,'\n');
      if(c) *c=0;
      if(Closure->guiMode) g_string_append_printf(str,"* %s\n",line);
      else
      {  if(line != body)
	       g_string_append_printf(str,"%s%s\n",prefix,line);
         else  g_string_append_printf(str,"%s\n",line);
      }
      if(c) line = c+1;
   } while(c && *line);

   if(Closure->logFileEnabled)
      PrintLogFile("%s", str->str);

   if(Closure->guiMode)
   {  log_window_append(str->str);
   }
   else
   {  print_greetings(stderr);
      g_fprintf(stderr, "%s", str->str);
      fflush(stderr);   /* at least needed for Windows */
   }

   g_string_free(str, TRUE);
   g_free(body);
}

void LogWarning(char *format, ...)
{  va_list argp;

   va_start(argp, format);
   vlog_warning(format, argp);
   va_end(argp);
}

/*
 * Tell user that current action was aborted due to a serious error.
 */

void Stop(char *format, ...)
{  va_list argp;

   /*** Show message depending on commandline / GUI mode  */ 

   if(Closure->logFileEnabled)
   {  va_start(argp, format);
      PrintLogFile(_("\n*\n* dvdisaster - can not continue:\n*\n"));
      VPrintLogFile(format, argp);
      va_end(argp);
   }

   if(!Closure->guiMode) 
   {  g_fprintf(stderr, "%s", _("\n*\n* dvdisaster - can not continue:\n*\n"));
      va_start(argp, format);
      g_vfprintf(stderr, format, argp);
      va_end(argp);
      g_fprintf(stderr, "\n\n");
      fflush(stderr);
   }
   else
   {  char *titled,*msg,*utf_msg;
      int idx;

      va_start(argp, format);
      msg = g_strdup_vprintf(format, argp);
      va_end(argp);

      if(Closure->errorTitle)
      {
	 titled = g_strdup_printf("<b>%s</b>\n\n%s", Closure->errorTitle,msg);
	 g_free(msg);
	 msg = titled;
      }

      idx = strlen(msg);  /* Remove trailing newline */
      if(msg[idx-1] == '\n')
	msg[idx-1] = 0;

      utf_msg = g_locale_to_utf8(msg, -1, NULL, NULL, NULL);

      ShowMessage(Closure->window, utf_msg, GTK_MESSAGE_ERROR);
      g_free(msg);
      g_free(utf_msg);
   }

   /* The cleanup procedure is supposed to terminate any running
      threads except for the main thread.
      Since we are running from a "side" thread in GUI mode,
      this will usually not return. */

#if 0
   if(Closure->mainThread)
   {  GThread *me = g_thread_self();

      if(me != Closure->mainThread)
	   printf("Stop() called from sub thread\n");
      else printf("Stop() called from main() thread\n");
   }
   else printf("Stop(): Closure->mainThread not set\n");
#endif

   if(Closure->cleanupProc)
     Closure->cleanupProc(Closure->cleanupData);

   /* Safety check; this indicates broken code.
      Reminder to myself: Use CreateMessage() instead ;-) */
   
   if(Closure->mainThread)
   {  GThread *me = g_thread_self();

      if(me != Closure->mainThread)
	printf("*\n* Warning: unterminated sub thread in Stop()\n*\n");
   }

   /* see above: possibly unreachable in GUI mode! */

   if(!Closure->guiMode)
   {    FreeClosure();
        exit(EXIT_FAILURE);
   }
}

/*
 * Provide some means of cleaning up 
 * when the current threads are terminated during a Stop() call.
 * Does not currently work if there are multiple threads besides the main thread.
 */

void RegisterCleanup(char *error_title, void (*cleanup)(gpointer), gpointer data)
{  
   Closure->cleanupProc = cleanup;
   Closure->cleanupData = data;
   if(Closure->errorTitle) g_free(Closure->errorTitle);
   Closure->errorTitle  = g_strdup(error_title);
}

/***
 *** Thread utilities
 ***/

/*
 * Thread creation.
 * Linux and Windows seem to provide at least 2MB of stack size per thread,
 * but FreeBSD has insanely small defaults. If this turns out to be too low,
 * we need the currently commented out alternative.
 */

GThread* CreateGThread(GThreadFunc f, gpointer data)
{  GError *err = NULL;
   GThread *t;

   t = g_thread_create(f, data, FALSE, &err);

#if 0
   t = g_thread_create_full(f, data, MY_MINIMUM_STACKSIZE,
			    FALSE, FALSE, G_THREAD_PRIORITY_NORMAL, &err);
#endif

   if(!t) Stop("Could not create thread: %s\n",err->message);

   return t;
}

/***
 *** Graphical user interface convenience
 ***/

/* 
 * Show the given widget
 */

static gboolean show_idle_func(gpointer data)
{  
   gtk_widget_show(GTK_WIDGET(data));

   return FALSE;
}

void ShowWidget(GtkWidget *widget)
{
  g_idle_add(show_idle_func, (gpointer)widget);
}


/*
 * Activation / Deactivation of the action buttons
 */

static gboolean allow_actions_idle_func(gpointer data)
{  gboolean s = (data != NULL);

   if(Closure->deviceNodes->len) 
   {  gtk_widget_set_sensitive(Closure->readButton, s);
      gtk_widget_set_sensitive(Closure->scanButton, s);
   }
   gtk_widget_set_sensitive(Closure->createButton, s);
   gtk_widget_set_sensitive(Closure->fixButton, s);
   gtk_widget_set_sensitive(Closure->testButton, s);

   gtk_widget_set_sensitive(Closure->prefsButton, s);
   if(!s && Closure->prefsWindow)
     HidePreferences();

   Closure->stopActions = FALSE;

   return FALSE;
}

void AllowActions(gboolean s)
{
  g_idle_add(allow_actions_idle_func, GINT_TO_POINTER(s));
}

/*
 * Dispatch a non-modal message dialog
 */

typedef struct
{  char *msg;
   GtkMessageType type;
   GtkWindow *window;
} message_info;

static gboolean message_idle_func(gpointer data)
{  message_info *mi = (message_info*)data;
   GtkWidget *dialog;

   dialog = gtk_message_dialog_new_with_markup(mi->window,
					       GTK_DIALOG_DESTROY_WITH_PARENT,
					       mi->type,
					       GTK_BUTTONS_CLOSE,
					       mi->msg, NULL);

   gtk_label_set_line_wrap(GTK_LABEL(((struct _GtkMessageDialog*)dialog)->label), FALSE);
   g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
   gtk_widget_show(dialog);

   g_free(mi->msg);
   g_free(mi);

   return FALSE;
}

void ShowMessage(GtkWindow *parent, char *msg, GtkMessageType type)
{  message_info *mi = g_malloc(sizeof(message_info));

   mi->msg    = g_strdup(msg);
   mi->type   = type;
   mi->window = parent;

   g_idle_add(message_idle_func, mi);
}

/*
 * Creates a message from the main thread
 */

GtkWidget* CreateMessage(char *format, GtkMessageType type, ...)
{  GtkWidget *dialog;
   va_list argp;
   char *text,*utf8; 

   va_start(argp, type);
   text = g_strdup_vprintf(format, argp);
   va_end(argp);
   utf8 = g_locale_to_utf8(text, -1, NULL, NULL, NULL);

   dialog = gtk_message_dialog_new(Closure->window, 
				   GTK_DIALOG_DESTROY_WITH_PARENT,
				   type,
				   GTK_BUTTONS_CLOSE,
				   utf8, NULL);

   gtk_label_set_line_wrap(GTK_LABEL(((struct _GtkMessageDialog*)dialog)->label), FALSE);
   g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
   gtk_widget_show(dialog);
   g_free(text);
   g_free(utf8);

   return dialog;
}

/*
 * Label convenience functions.
 * Sets the label text from another thread.
 */

typedef struct
{  GtkLabel *label;
   char *text;
} label_info;

static gboolean label_idle_func(gpointer data)
{  label_info *li = (label_info*)data;

   gtk_label_set_markup(li->label, li->text);

   g_free(li->text);
   g_free(li);

   return FALSE;
}

void SetLabelText(GtkLabel *label, char *format, ...)
{  label_info *li = g_malloc(sizeof(label_info));
   va_list argp;

   li->label = label;

   va_start(argp, format);
   if(format)
   {  char *tmp  = g_strdup_vprintf(format, argp);

      if(!tmp) tmp=g_strdup_printf("SetLabelText(%s) failed",format);
      li->text = g_locale_to_utf8(tmp, -1, NULL, NULL, NULL);
      g_free(tmp);
   }
   else li->text = g_locale_to_utf8("(null)", -1, NULL, NULL, NULL);
   va_end(argp);
   g_idle_add(label_idle_func, li);
}

/*
 * Progress bar convenience function.
 * Percentage is given as a multiple of 0.1 percent.
 */

typedef struct
{  GtkWidget *pbar;
   int percent;
   int max;
} progress_info;

static gboolean progress_idle_func(gpointer data)
{  progress_info *pi = (progress_info*)data;
   gdouble val = (gdouble)pi->percent / (gdouble)pi->max;
   char text[20];

   switch(pi->max)
   {  case  100: g_sprintf(text, "%3d%%",pi->percent); break;
      case 1000: g_sprintf(text, "%3d.%1d%%",pi->percent/10,pi->percent%10); break;
   }

   gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pi->pbar), val);
   gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pi->pbar), text);

   g_free(pi);

   return FALSE;
}

void SetProgress(GtkWidget *pbar, int percent, int max)
{  progress_info *pi = g_malloc(sizeof(progress_info));

   pi->pbar    = pbar;
   pi->percent = percent;
   pi->max     = max;

   g_idle_add(progress_idle_func, pi);
}

/*
 * Perform a modal dialog.
 * Note that the thread running the dialog is different
 * from the one blocking/waiting for the response!
 */

typedef struct
{  GMutex *mutex;
   GCond *cond;
   char *msg;
   int ret;
   GtkMessageType message_type;
   GtkButtonsType button_type;
   void (*button_fn)(GtkDialog*);
} modal_info;

static gboolean modal_idle_func(gpointer data)
{  modal_info *mi = (modal_info*)data;
   GtkWidget *dialog;
   int response;

   dialog = gtk_message_dialog_new(Closure->window,
				   GTK_DIALOG_DESTROY_WITH_PARENT,
				   mi->message_type,
				   mi->button_type,
				   "%s", mi->msg);
   gtk_label_set_line_wrap(GTK_LABEL(((struct _GtkMessageDialog*)dialog)->label), FALSE);

   if(mi->button_fn)
         mi->button_fn(GTK_DIALOG(dialog));
   else  ReverseCancelOK(GTK_DIALOG(dialog));

   response = gtk_dialog_run(GTK_DIALOG(dialog));

   g_mutex_lock(mi->mutex);
   if(mi->button_fn)
     mi->ret = response;
   else switch(response)
        {  case GTK_RESPONSE_OK:
	     mi->ret = 1;
	   break;

           default:
	     mi->ret = 0;
	   break;
	}

   g_cond_signal(mi->cond);
   g_mutex_unlock(mi->mutex);

   gtk_widget_destroy(dialog);

   return FALSE;
}

static int vmodal_dialog(GtkMessageType mt, GtkButtonsType bt, 
			 void(*button_fn)(GtkDialog*), char *msg, va_list argp)
{  modal_info *mi = g_malloc(sizeof(modal_info));
   char *tmp;
   int idx,ret;

   mi->message_type = mt;
   mi->button_type = bt;
   mi->button_fn = button_fn;
   mi->mutex = g_mutex_new();
   mi->cond  = g_cond_new();

   tmp = g_strdup_vprintf(msg, argp);
   idx = strlen(tmp);  /* Remove trailing newline */
   if(tmp[idx-1] == '\n')
     tmp[idx-1] = 0;
   mi->msg   = g_locale_to_utf8(tmp, -1, NULL, NULL, NULL);
   g_free(tmp);

   mi->ret = -1;

   g_idle_add(modal_idle_func, mi);

   g_mutex_lock(mi->mutex);
   while(mi->ret == -1)
     g_cond_wait(mi->cond, mi->mutex);

   ret = mi->ret;
   g_mutex_unlock(mi->mutex);

   g_free(mi->msg);
   g_mutex_free(mi->mutex);
   g_cond_free(mi->cond);
   g_free(mi);

   return ret;
}

int ModalDialog(GtkMessageType mt, GtkButtonsType bt, 
		void(*button_fn)(GtkDialog*), char *msg, ...)
{  va_list argp;
   int result;

   va_start(argp, msg);
   result = vmodal_dialog(mt, bt, button_fn, msg, argp);
   va_end(argp);

   return result;
}

/*
 * A wrapper around ModalDialog() to create a logged warning.
 * Note that in CLI mode the answer is always "yes",
 * so warnings will never abort CLI mode.
 */

int ModalWarning(GtkMessageType mt, GtkButtonsType bt, 
		 void(*button_fn)(GtkDialog*), char *msg, ...)
{  va_list argp;
   int result = 1;  

   va_start(argp, msg);
   vlog_warning(msg, argp);
   va_end(argp);
   
   if(Closure->guiMode)
   { va_start(argp, msg);
     result = vmodal_dialog(mt, bt, button_fn, msg, argp);
     va_end(argp);
   }

   return result;
}

/*
 * Set the text in the pango layout and retrieve its extents.
 */

void SetText(PangoLayout *layout, char *text, int *w, int *h)
{  PangoRectangle rect;
   char *t = g_locale_to_utf8(text, -1, NULL, NULL, NULL);

   pango_layout_set_text(layout, t, -1);
   pango_layout_get_pixel_extents(layout, NULL, &rect);

   g_free(t);

   *w = rect.width;
   *h = rect.height;
}

/*
 * Switch a notebook to another page and set the text in a label.
 * Used in some footlines in the GUI.
 */

typedef struct
{  GtkWidget *notebook;
   int newPage;
   GtkWidget *label;
   char *newText;
} footline_info;

static gboolean footline_idle_func(gpointer data)
{  footline_info *fi = (footline_info*)data;

   if(fi->label) 
     gtk_label_set_markup(GTK_LABEL(fi->label), fi->newText);
   gtk_notebook_set_current_page(GTK_NOTEBOOK(fi->notebook), fi->newPage);

   if(fi->newText)
     g_free(fi->newText);
   g_free(fi);

   return FALSE;
}

void SwitchAndSetFootline(GtkWidget *notebook, int page, GtkWidget *label, char *format, ...)
{  va_list argp;
   char *tmp;
   footline_info *fi = g_malloc0(sizeof(footline_info));
   int len;

   fi->notebook = notebook;
   fi->newPage  = page;
   fi->label    = label;

   if(label)
   {  va_start(argp, format);
      tmp  = g_strdup_vprintf(format, argp);
      len = strlen(tmp);
      if(tmp[len-1] == '\n') tmp[len-1]=0;
      fi->newText = g_locale_to_utf8(tmp, -1, NULL, NULL, NULL);
      g_free(tmp);
      va_end(argp);
   }

   g_idle_add(footline_idle_func, fi);
}

/*
 * Rearrange buttons to OK Cancel order
 * in file dialogs
 * 
 * gtk_dialog_set_alternative_button_order()
 * has been introduced since gtk+2.6,
 * but does not seem to work correctly.
 */

void ReverseCancelOK(GtkDialog *dialog)
{  GtkWidget *box, *button ;

   if(!Closure->reverseCancelOK)
      return;

   box = dialog->action_area; 
   button = ((GtkBoxChild*)(g_list_first(GTK_BOX(box)->children)->data))->widget;

   gtk_box_reorder_child(GTK_BOX(box), button, 1);

#if 0
   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog),
					   GTK_RESPONSE_OK,
					   GTK_RESPONSE_CANCEL,
					   -1);
#endif
}

/*
 * Make widget insensitive for a given amount of time.
 */

static gboolean insensitive_timeout_func(gpointer data)
{  
   gtk_widget_set_sensitive((GtkWidget*)data, TRUE);

   return FALSE;
}


void TimedInsensitive(GtkWidget *widget, int delay)
{

  gtk_widget_set_sensitive(widget, FALSE);

  g_timeout_add(delay, insensitive_timeout_func, (gpointer)widget);
}

/*
 * Get the width of a label text
 */

int GetLabelWidth(GtkLabel *label, char *format, ...)
{  PangoLayout *layout;
   PangoRectangle rect;
   va_list argp;
   char *text;

   va_start(argp, format);
   text = g_strdup_vprintf(format, argp);
   va_end(argp);

   layout = gtk_label_get_layout(label);
   pango_layout_set_text(layout, text, -1);
   pango_layout_get_pixel_extents(layout, NULL, &rect);

   g_free(text);

   return rect.width;
}
 

/*
 * Lock the size of a label to that of the given sample text.
 */

void LockLabelSize(GtkLabel *label, char *format, ...)
{  PangoLayout *layout;
   PangoRectangle rect;
   va_list argp;
   char *text;

   va_start(argp, format);
   text = g_strdup_vprintf(format, argp);
   va_end(argp);

   layout = gtk_label_get_layout(label);
   pango_layout_set_text(layout, text, -1);
   pango_layout_get_pixel_extents(layout, NULL, &rect);

   gtk_widget_set_size_request(GTK_WIDGET(label), rect.width, rect.height);
   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);

   g_free(text);
}