File: show-manual.c

package info (click to toggle)
dvdisaster 0.79.5-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 42,960 kB
  • sloc: ansic: 33,411; sh: 4,898; makefile: 138
file content (316 lines) | stat: -rw-r--r-- 8,578 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
/*  dvdisaster: Additional error correction for optical media.
 *  Copyright (C) 2004-2015 Carsten Gnoerlich.
 *
 *  Email: carsten@dvdisaster.org  -or-  cgnoerlich@fsfe.org
 *  Project homepage: http://www.dvdisaster.org
 *
 *  This file is part of dvdisaster.
 *
 *  dvdisaster 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  dvdisaster 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 dvdisaster. If not, see <http://www.gnu.org/licenses/>.
 */

#include "dvdisaster.h"

#include <sys/wait.h>

/***
 *** Ask user to specify his viewer
 ***/

#define SEARCH_BUTTON 1

typedef struct
{  GtkWidget *dialog;
   GtkWidget *entry;
   GtkWidget *search;
   GtkWidget *filesel;
   GtkWidget *fileok;
   GtkWidget *filecancel;
   char *path;
} viewer_dialog_info;

static void response_cb(GtkWidget *widget, int response, gpointer data)
{  viewer_dialog_info *bdi = (viewer_dialog_info*)data; 

   switch(response)
   {  case GTK_RESPONSE_ACCEPT:
	if(Closure->viewer) g_free(Closure->viewer);
	Closure->viewer = g_strdup(gtk_entry_get_text(GTK_ENTRY(bdi->entry)));
	ShowPDF(bdi->path);
	break;

      case GTK_RESPONSE_REJECT:
	if(bdi->path) g_free(bdi->path);
        break;
   }
   gtk_widget_destroy(widget);
   if(bdi->filesel)
     gtk_widget_destroy(bdi->filesel);
   g_free(bdi);
}

static void search_cb(GtkWidget *widget, gpointer data)
{  viewer_dialog_info *bdi = (viewer_dialog_info*)data; 

   if(widget == bdi->search) 
   {  bdi->filesel = gtk_file_selection_new(_utf("windowtitle|Choose a PDF viewer"));
      bdi->fileok = GTK_FILE_SELECTION(bdi->filesel)->ok_button;
      bdi->filecancel = GTK_FILE_SELECTION(bdi->filesel)->cancel_button;
      ReverseCancelOK(GTK_DIALOG(bdi->filesel));
      gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(bdi->filesel));
      g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(bdi->filesel)->ok_button), "clicked", 
		       G_CALLBACK(search_cb), bdi);
    
      g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(bdi->filesel)->cancel_button), "clicked", 
		       G_CALLBACK(search_cb), bdi);
      
      gtk_widget_show(bdi->filesel);
   }

   if(widget == bdi->fileok)
   {
      if(Closure->viewer) g_free(Closure->viewer);
      Closure->viewer = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(bdi->filesel)));
      ShowPDF(bdi->path);
      gtk_widget_destroy(bdi->filesel);
      gtk_widget_destroy(bdi->dialog);
      g_free(bdi);
      return;
   }

   if(widget == bdi->filecancel)
   {  gtk_widget_destroy(bdi->filesel);
      bdi->filesel = NULL;
   }
}

static void viewer_dialog(char *path)
{  GtkWidget *dialog, *vbox, *hbox, *label, *entry, *button;
   viewer_dialog_info *bdi = g_malloc0(sizeof(viewer_dialog_info));

   /* Create the dialog */

   dialog = gtk_dialog_new_with_buttons(_utf("windowtitle|PDF viewer required"), 
				       Closure->window, GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, 
				       GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
   bdi->dialog = dialog;
   if(path)
   {  bdi->path = g_strdup(path);
   }

   vbox = gtk_vbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox, FALSE, FALSE, 0);
   gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);

   /* Insert the contents */

   label = gtk_label_new(NULL);
   gtk_label_set_markup(GTK_LABEL(label), _utf("<b>Could not find a suitable PDF viewer.</b>\n\n"
                                               "Which PDF viewer would you like to use\n"
                                               "for reading the online documentation?\n\n"
			                       "Please enter its name (e.g. xpdf) or\n"
			                       "use the \"Search\" button for a file dialog.\n")),
			      gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 10);

   hbox = gtk_hbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 10);

   bdi->entry = entry = gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 10);

   bdi->search = button = gtk_button_new_with_label(_utf("Search"));
   g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(search_cb), bdi);
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);

   /* Show it */

   g_signal_connect(dialog, "response", G_CALLBACK(response_cb), bdi);

   gtk_widget_show_all(dialog);
}

/***
 *** Show the manual in an external viewer
 ***/

/*
 * Check the child processes exit status
 * to find whether the viewer could be invoked.
 */

typedef struct
{  pid_t pid;
   char *path;
   GtkWidget *msg;
   int seconds;
} viewer_info;


static void msg_destroy_cb(GtkWidget *widget, gpointer data)
{  viewer_info *bi = (viewer_info*)data;

   bi->msg = NULL; 
}

/* 
 * The following list of viewers 
 * will be tried one at a time until one entry succeeds by:
 * - returning zero
 * - not returning within 60 seconds
 */

static int viewer_index;
static void try_viewer(viewer_info*);

static char *viewers[] = 
{  "evince",
   "xpdf",
   "okular",
   "gv",
   "mupdf",
   "pdfcube",
   "zathura",
   NULL
};

static gboolean viewer_timeout_func(gpointer data)
{  viewer_info *bi = (viewer_info*)data;
   int status;

   waitpid(bi->pid, &status, WNOHANG);

   /* At least mozilla returns random values under FreeBSD on success,
      so we can't rely on the return value exept our own 110 one. */

   if(WIFEXITED(status))
   {
      switch(WEXITSTATUS(status))
      {  case 110: /* viewer did not execute */
	   viewer_index++;
	   if(!viewers[viewer_index]) /* all viewers from the list failed */
	   {  viewer_dialog(bi->path);

	      if(bi->msg) 
		gtk_widget_destroy(bi->msg);
	      if(bi->path) 
		g_free(bi->path);
	      g_free(bi);
	   }
	   else                        /* try next viewer from list */
	   {  bi->seconds = 0;  
	      try_viewer(bi);
	   }
	   return FALSE;

         case 0:  /* viewer assumed to be successful */
         default:
	   if(bi->msg) 
	     gtk_widget_destroy(bi->msg);
	   if(bi->path) 
	     g_free(bi->path);
	   g_free(bi);
	   return FALSE;
      }
   }

   bi->seconds++;
   if(bi->seconds == 10 && bi->msg)
   {  gtk_widget_destroy(bi->msg);
      bi->msg = NULL;
   }

   return bi->seconds > 60 ? FALSE : TRUE;
}

/*
 * Invoke the viewer
 */

static void try_viewer(viewer_info *bi)
{  pid_t pid;

   bi->pid = pid = fork();

   if(pid == -1)
   {  printf("fork failed\n");
      return;
   }

   /* make the parent remember and wait() for the viewer */

   if(pid > 0)  
   {  g_timeout_add(1000, viewer_timeout_func, (gpointer)bi);

      if(viewer_index)
      {  g_free(Closure->viewer);
	 Closure->viewer = g_strdup(viewers[viewer_index]);
      }
   }

   /* try calling the viewer */

   if(pid == 0)
   {  char *argv[10];
      int argc = 0;

      argv[argc++] = viewer_index ? viewers[viewer_index] : Closure->viewer;
      argv[argc++] = bi->path;
      argv[argc++] = NULL;
      execvp(argv[0], argv);

      _exit(110); /* couldn't execute */
   }
}

void ShowPDF(char *target)
{  viewer_info *bi = g_malloc0(sizeof(viewer_info));
   guint64 ignore;

   if(!Closure->docDir)
   {  
      CreateMessage(_("Documentation not installed."), GTK_MESSAGE_ERROR);
      g_free(bi);
      return;
   }

   /* If no target is given, show the manual. */

   if(!target)
   {    bi->path = g_strdup_printf("%s/manual.pdf.gz",Closure->docDir);
   }
   else 
      if(*target != '/') bi->path = g_strdup_printf("%s/%s",Closure->docDir, target); 
      else               bi->path = g_strdup(target); 

   if(!LargeStat(bi->path, &ignore))
   {  
      CreateMessage(_("Documentation file\n%s\nnot found.\n"
	       "Please install the dvdisaster-doc package.\n"), GTK_MESSAGE_ERROR, bi->path);
      g_free(bi->path);
      g_free(bi);
      return;
   }

   /* Lock the help button and show a message for 10 seconds. */

   TimedInsensitive(Closure->helpButton, 10000);
   bi->msg = CreateMessage(_("Please hang on until the viewer comes up!"), GTK_MESSAGE_INFO);
   g_signal_connect(G_OBJECT(bi->msg), "destroy", G_CALLBACK(msg_destroy_cb), bi);

   /* Try the first viwer */

   viewer_index = 0;
   try_viewer(bi);
}