2 * Drivers acquiring installation utility
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "ui-gnome.h" /* self */
23 #include <glib/gmessages.h>
24 #include "moduriload.h"
26 #include <gtk/gtkmain.h>
27 #include <gtk/gtktreeviewcolumn.h>
28 #include <gtk/gtkbox.h>
30 #include <libgnomeui/gnome-app.h>
31 #include <gtk/gtktreestore.h>
32 #include <gtk/gtkframe.h>
33 #include <gtk/gtkentry.h>
34 #include <libgnomeui/gnome-druid.h>
35 #include <libgnomeui/gnome-app-util.h>
36 #include <libgnomeui/gnome-druid-page-edge.h>
38 #include <libgnomevfs/gnome-vfs-utils.h>
40 #include <captive/macros.h>
44 #define PROGRESS_UPDATE_USEC 100000
48 static GtkTreeStore *DriversTreeStore;
49 static GtkFrame *DriversFrame;
50 static GtkFrame *ProgressFrame;
51 static GtkEntry *ProgressEntry;
52 static GnomeDruid *Druid;
53 static GtkButton *DruidButtonSkip;
54 static GtkButton *DruidButtonOK;
55 static GtkTreeView *DriversTreeView;
56 static GnomeDruidPage *PageStart;
57 static GnomeDruidPage *ScanDiskPage;
58 static GnomeDruidPage *ScanPathPage;
59 static GnomeDruidPage *MicrosoftComPage;
60 static GnomeDruidPage *PageFinish;
61 static GtkEntry *ScanPathLocationComboEntry;
62 static GtkButton *MicrosoftComConfirmButton;
63 static GtkProgressBar *MicrosoftComProgress;
65 DRIVERS_TREE_STORE_COLUMN_TYPE,
66 DRIVERS_TREE_STORE_COLUMN_ID,
67 DRIVERS_TREE_STORE_COLUMN_NUM, /* total # */
69 #define DRIVERS_TREE_STORE_COLUMN_TYPE_LIST G_TYPE_STRING,G_TYPE_STRING
72 /* map: (gchar *)type-> (GtkTreeIter *) */
73 static GHashTable *DriversTreeStore_Iter_hash;
75 static void DriversTreeStore_Iter_hash_key_destroy_func(gchar *type)
77 g_return_if_fail(type!=NULL);
82 static void DriversTreeStore_Iter_hash_init(void)
84 if (DriversTreeStore_Iter_hash)
86 DriversTreeStore_Iter_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
87 (GDestroyNotify)DriversTreeStore_Iter_hash_key_destroy_func,
88 NULL); /* value_destroy_func */
91 static GtkTreeIter *DriversTreeStore_Iter_hash_get_iter(const gchar *type)
95 g_return_val_if_fail(type!=NULL,NULL);
97 DriversTreeStore_Iter_hash_init();
98 if (!(r=g_hash_table_lookup(DriversTreeStore_Iter_hash,type))) {
100 gtk_tree_store_append(DriversTreeStore,
103 g_hash_table_insert(DriversTreeStore_Iter_hash,g_strdup(type),r);
109 static gboolean some_modules_found=FALSE;
110 static gboolean in_progress=FALSE;
111 static GnomeDruidPage *page_active;
113 static void state_changed(void)
115 /* Not yet initialized? */
119 gtk_widget_set_sensitive(GTK_WIDGET(DruidButtonSkip),
120 (page_active!=PageStart && page_active!=PageFinish));
121 gtk_widget_set_sensitive(GTK_WIDGET(DruidButtonOK),some_modules_found);
124 gtk_widget_set_sensitive(Druid->next,FALSE);
125 gtk_widget_set_sensitive(GTK_WIDGET(DruidButtonOK),some_modules_found);
126 gtk_widget_set_sensitive(GTK_WIDGET(MicrosoftComConfirmButton),FALSE);
129 /* It is checked by GTK+ whether the text changed: */
130 gtk_entry_set_text(ProgressEntry,"");
131 gtk_widget_set_sensitive(Druid->next,
132 (page_active!=PageFinish && page_active!=MicrosoftComPage));
133 gtk_widget_set_sensitive(GTK_WIDGET(MicrosoftComConfirmButton),
134 (page_active==MicrosoftComPage));
135 gtk_progress_bar_set_fraction(MicrosoftComProgress,(gdouble)0);
136 gtk_progress_bar_set_text(MicrosoftComProgress,"");
140 static void ui_gnome_module_best_priority_notify(const gchar *module_type)
144 g_return_if_fail(module_type!=NULL);
146 iter=DriversTreeStore_Iter_hash_get_iter(module_type);
147 gtk_tree_store_set(DriversTreeStore,iter,
148 DRIVERS_TREE_STORE_COLUMN_TYPE,module_type,
149 DRIVERS_TREE_STORE_COLUMN_ID ,
151 || !strcmp(module_type,"ntoskrnl.exe")
152 || !strcmp(module_type,"ntfs.sys"))
153 ? _("NOT FOUND; essential module for NTFS disks access")
154 : _("not found; optional module")),
158 static void ui_gnome_module_available_notify(struct module_available *module_available)
161 static gboolean some_module_ntoskrnl_exe_found=FALSE;
162 static gboolean some_module_ntfs_sys_found=FALSE;
164 g_return_if_fail(module_available!=NULL);
165 g_return_if_fail(module_available->module!=NULL);
167 iter=DriversTreeStore_Iter_hash_get_iter(module_available->module->type);
168 gtk_tree_store_set(DriversTreeStore,iter,
169 DRIVERS_TREE_STORE_COLUMN_TYPE,module_available->module->type,
170 DRIVERS_TREE_STORE_COLUMN_ID ,module_available->module->id,
173 if (!strcmp(module_available->module->type,"ntoskrnl.exe"))
174 some_module_ntoskrnl_exe_found=TRUE;
175 if (!strcmp(module_available->module->type,"ntfs.sys"))
176 some_module_ntfs_sys_found=TRUE;
178 some_modules_found=some_module_ntoskrnl_exe_found && some_module_ntfs_sys_found;
182 static gboolean all_modules_found=FALSE;
184 static void ui_gnome_all_modules_found_notify(void)
186 all_modules_found=TRUE;
190 static gboolean aborted=FALSE;
191 static gboolean aborted_back=FALSE; /* 'Back' button was clicked. */
192 static struct timeval ProgressEntry_updated_timeval;
194 static void progress_start(void)
199 CAPTIVE_MEMZERO(&ProgressEntry_updated_timeval);
203 static void progress_end(void)
209 static gboolean ui_gnome_progress(GnomeVFSURI *uri)
211 /* 'uri' may be NULL */
214 static gchar *uri_text=NULL;
216 /* Store 'uri' on each call (not just if 'diff_timeval' permits)
217 * as we may get into long cabinet extraction phase with 'uri==NULL' calls
218 * where we want to display the currently processed 'uri'.
222 uri_text=gnome_vfs_uri_to_string(uri,GNOME_VFS_URI_HIDE_PASSWORD);
226 struct timeval now_timeval;
227 struct timeval diff_timeval;
229 gettimeofday( /* FIXME: errors ignored */
230 &now_timeval, /* tv */
232 timersub(&now_timeval,&ProgressEntry_updated_timeval,&diff_timeval);
233 if (!ProgressEntry_updated_timeval.tv_sec || diff_timeval.tv_sec>0 || diff_timeval.tv_usec>=PROGRESS_UPDATE_USEC) {
234 gtk_entry_set_text(ProgressEntry,
235 uri_text+(strncmp(uri_text,"file://",strlen("file://")) ? 0 : strlen("file://")));
236 ProgressEntry_updated_timeval=now_timeval;
240 while (gtk_events_pending())
241 gtk_main_iteration();
245 if (all_modules_found)
251 static void ui_gnome_progress_bar(gint done,gint length)
253 g_return_if_fail(done>=0);
254 g_return_if_fail(length>=0);
257 gtk_progress_bar_pulse(MicrosoftComProgress);
258 gtk_progress_bar_set_text(MicrosoftComProgress,"");
261 gchar *done_display,*length_display;
263 gtk_progress_bar_set_fraction(MicrosoftComProgress,((gdouble)done)/length);
264 done_display=gnome_vfs_format_file_size_for_display(done);
265 length_display=gnome_vfs_format_file_size_for_display(length);
266 gtk_progress_bar_set_text(MicrosoftComProgress,
267 captive_printf_alloca("%s / %s",done_display,length_display));
268 g_free(done_display);
269 g_free(length_display);
273 /* FIXME: Change it to "prepare" signal. */
274 void on_Page_map(GtkWidget *vbox_widget,GtkWidget *page_widget)
276 g_return_if_fail(vbox_widget==NULL || GTK_IS_VBOX(vbox_widget));
277 g_return_if_fail(GNOME_IS_DRUID_PAGE(page_widget));
279 page_active=GNOME_DRUID_PAGE(page_widget);
280 if (page_active==PageFinish) {
281 gnome_druid_set_show_finish(Druid,FALSE); /* set it each time */
282 /**/ if (!some_modules_found)
283 gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(PageFinish),_(
284 "We need at least some version of drivers essential for this project:"
285 " ntoskrnl.exe and ntfs.sys. Please click 'Back' button to obtain them"
286 " by several methods offered by this installer."));
290 text=final_text(all_modules_found);
291 gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(PageFinish),text);
295 if (page_active==ScanPathPage)
296 gtk_widget_grab_focus(GTK_WIDGET(ScanPathLocationComboEntry));
302 /* FIXME: 'freeze' apparently does not help 'repositioning' of
303 * 'DriversTreeView' during first 'map' of each 'Page'.
305 gtk_widget_freeze_child_notify(vbox_widget);
307 gtk_widget_reparent(GTK_WIDGET(DriversFrame),vbox_widget);
308 gtk_widget_reparent(GTK_WIDGET(ProgressFrame),vbox_widget);
310 gtk_box_reorder_child(GTK_BOX(vbox_widget),GTK_WIDGET(DriversFrame),
313 gtk_box_set_child_packing(GTK_BOX(vbox_widget),GTK_WIDGET(DriversFrame),
318 gtk_box_set_child_packing(GTK_BOX(vbox_widget),GTK_WIDGET(ProgressFrame),
324 /* FIXME: Needed to fix (0,0)-position inside parent GdkWindow. */
325 gtk_widget_queue_resize(GTK_WIDGET(DriversTreeView));
326 gtk_widget_queue_resize(GTK_WIDGET(ProgressEntry));
328 gtk_widget_thaw_child_notify(vbox_widget);
331 gboolean on_Page_next(GnomeDruidPage *gnomedruidpage,GtkWidget *widget,gpointer user_data /* unused */)
333 g_return_val_if_fail(GNOME_IS_DRUID_PAGE(gnomedruidpage),FALSE);
335 if (in_progress) /* bogus callback - we should be non-sensitive */
336 return TRUE; /* ignore button press */
338 /**/ if (page_active==PageStart) {
339 if (all_modules_found) {
340 gnome_druid_set_page(Druid,PageFinish);
341 return TRUE; /* ignore button press */
344 else if (page_active==ScanDiskPage) {
350 gnome_druid_set_page(Druid,PageStart);
351 return TRUE; /* ignore button press */
353 if (all_modules_found) {
354 gnome_druid_set_page(Druid,PageFinish);
355 return TRUE; /* ignore button press */
357 return FALSE; /* proceed to next page */
359 else if (page_active==ScanPathPage) {
360 const gchar *scan_path_uri_text=gtk_entry_get_text(ScanPathLocationComboEntry);
362 if (scan_path_uri_text && *scan_path_uri_text) {
363 GnomeVFSURI *scan_path_uri;
365 if ((scan_path_uri=gnome_vfs_uri_new(scan_path_uri_text))) {
367 mod_uri_load(scan_path_uri);
369 gnome_vfs_uri_unref(scan_path_uri);
371 gnome_druid_set_page(Druid,(all_modules_found ? PageStart : ScanDiskPage));
372 return TRUE; /* ignore button press */
374 if (all_modules_found) {
375 gnome_druid_set_page(Druid,PageFinish);
376 return TRUE; /* ignore button press */
378 gtk_entry_set_text(ScanPathLocationComboEntry,"");
381 g_warning(_("Invalid URI: %s"),scan_path_uri_text);
382 gtk_widget_grab_focus(GTK_WIDGET(ScanPathLocationComboEntry));
383 return TRUE; /* ignore button press; we cleared the URI entry */
385 return FALSE; /* proceed to next page */
388 return FALSE; /* proceed to next page */
391 void on_MicrosoftComConfirmButton_clicked(GtkButton *button,gpointer user_data)
393 g_return_if_fail(GTK_IS_BUTTON(button));
395 if (in_progress) /* bogus callback */
397 if (page_active!=MicrosoftComPage) /* bogus callback */
404 gnome_druid_set_page(Druid,(all_modules_found ? PageStart : ScanPathPage));
407 if (all_modules_found) {
408 gnome_druid_set_page(Druid,PageFinish);
412 gnome_druid_set_page(Druid,PageFinish);
415 void on_DruidButtonSkip_clicked(GtkButton *button,gpointer user_data /* unused */)
417 g_return_if_fail(GTK_IS_BUTTON(button));
424 if (all_modules_found)
425 gnome_druid_set_page(Druid,PageFinish);
426 else if (page_active==ScanDiskPage)
427 gnome_druid_set_page(Druid,ScanPathPage);
428 else if (page_active==ScanPathPage)
429 gnome_druid_set_page(Druid,MicrosoftComPage);
430 else if (page_active==MicrosoftComPage)
431 gnome_druid_set_page(Druid,PageFinish);
434 gboolean on_Page_back(GnomeDruidPage *gnomedruidpage,GtkWidget *widget,gpointer user_data)
436 g_return_val_if_fail(GNOME_IS_DRUID_PAGE(gnomedruidpage),FALSE);
439 if (all_modules_found) {
440 gnome_druid_set_page(Druid,PageStart);
441 return TRUE; /* ignore button press */
443 return FALSE; /* proceed to previous page */
450 return TRUE; /* ignore button press now; we will respect 'aborted_back' */
453 void on_Druid_cancel(GnomeDruid *gnomedruid,gpointer user_data /* unused */)
455 g_return_if_fail(GNOME_IS_DRUID(gnomedruid));
457 /* gtk_main_quit() would not abort the current operation. */
461 static void on_DruidButtonOK_clicked_dialog_callback(gint reply,gint *replyp /* data */)
463 g_return_if_fail(reply>=0);
464 g_return_if_fail(replyp!=NULL);
469 void on_DruidButtonOK_clicked(GtkButton *button,gpointer user_data /* unused */)
474 g_return_if_fail(GTK_IS_BUTTON(button));
476 if (all_modules_found)
479 /* TODO: Avoid dialog if already on Finish page. */
481 dialog=gnome_app_ok_cancel_modal(App,_(
482 "Although essential modules (\"ntoskrnl.exe\" and \"ntfs.sys\") are available "
483 "you may still want to get their better version and/or more modules. "
485 (GnomeReplyCallback)on_DruidButtonOK_clicked_dialog_callback,
487 g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
489 /* 'dialog' gets destroyed automatically */
490 if (reply==0) /* 0 for 'OK', 1 for 'Cancel', left -1 for dialog close. */
494 static void button_stock_set_label(GtkWidget *widget,const gchar *label_text_new /* callback_data */)
496 g_return_if_fail(GTK_IS_WIDGET(widget));
497 g_return_if_fail(label_text_new!=NULL);
499 /**/ if (GTK_IS_CONTAINER(widget))
500 gtk_container_foreach(GTK_CONTAINER(widget),
501 (GtkCallback)button_stock_set_label, /* callback */
502 (/* de-conts */ gchar *)label_text_new); /* callback_data */
503 else if (GTK_IS_LABEL(widget))
504 gtk_label_set_text_with_mnemonic(GTK_LABEL(widget),label_text_new);
507 /* of "ui-gnome-interface.h": */
508 GtkWidget *create_App(void);
509 /* of "ui-gnome-support.h": */
510 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
512 static void App_init(void)
514 GtkTreeViewColumn *column;
515 GtkCellRenderer *cell;
516 GtkBox *druid_button_box;
518 App=GNOME_APP(create_App());
520 DriversTreeView=GTK_TREE_VIEW(lookup_widget(GTK_WIDGET(App),"DriversTreeView"));
521 DriversFrame=GTK_FRAME(lookup_widget(GTK_WIDGET(App),"DriversFrame"));
522 ProgressFrame=GTK_FRAME(lookup_widget(GTK_WIDGET(App),"ProgressFrame"));
523 Druid=GNOME_DRUID(lookup_widget(GTK_WIDGET(App),"Druid"));
524 PageStart=GNOME_DRUID_PAGE(lookup_widget(GTK_WIDGET(App),"PageStart"));
525 ScanDiskPage=GNOME_DRUID_PAGE(lookup_widget(GTK_WIDGET(App),"ScanDiskPage"));
526 ScanPathPage=GNOME_DRUID_PAGE(lookup_widget(GTK_WIDGET(App),"ScanPathPage"));
527 MicrosoftComPage=GNOME_DRUID_PAGE(lookup_widget(GTK_WIDGET(App),"MicrosoftComPage"));
528 PageFinish=GNOME_DRUID_PAGE(lookup_widget(GTK_WIDGET(App),"PageFinish"));
529 ScanPathLocationComboEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"ScanPathLocationComboEntry"));
530 MicrosoftComConfirmButton=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"MicrosoftComConfirmButton"));
531 MicrosoftComProgress=GTK_PROGRESS_BAR(lookup_widget(GTK_WIDGET(App),"MicrosoftComProgress"));
532 ProgressEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"ProgressEntry"));
534 druid_button_box=GTK_BOX(gtk_widget_get_parent(Druid->next));
536 DriversTreeStore=gtk_tree_store_new(DRIVERS_TREE_STORE_COLUMN_NUM,DRIVERS_TREE_STORE_COLUMN_TYPE_LIST);
537 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(DriversTreeStore),
538 DRIVERS_TREE_STORE_COLUMN_TYPE,GTK_SORT_ASCENDING);
539 gtk_tree_view_set_model(DriversTreeView,GTK_TREE_MODEL(DriversTreeStore));
541 column=gtk_tree_view_column_new();
542 cell=gtk_cell_renderer_text_new();
543 gtk_tree_view_column_pack_start(column,cell,
545 gtk_tree_view_column_set_attributes(column,cell,
546 "text",DRIVERS_TREE_STORE_COLUMN_TYPE,
548 gtk_tree_view_append_column(DriversTreeView,column);
550 column=gtk_tree_view_column_new();
551 cell=gtk_cell_renderer_text_new();
552 gtk_tree_view_column_pack_start(column,cell,
554 gtk_tree_view_column_set_attributes(column,cell,
555 "text",DRIVERS_TREE_STORE_COLUMN_ID,
557 gtk_tree_view_append_column(DriversTreeView,column);
559 /* gnome_druid_set_show_finish() just replaces Next<->Finish buttons displayed. */
560 gtk_widget_hide(GTK_WIDGET(Druid->finish));
562 DruidButtonSkip=GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_REDO));
563 gtk_container_foreach(GTK_CONTAINER(DruidButtonSkip),
564 (GtkCallback)button_stock_set_label, /* callback */
565 _("_Skip")); /* callback_data */
566 gtk_box_pack_end(druid_button_box,GTK_WIDGET(DruidButtonSkip),FALSE,TRUE,0);
567 gtk_widget_show(GTK_WIDGET(DruidButtonSkip));
568 g_signal_connect((gpointer)DruidButtonSkip,"clicked",G_CALLBACK(on_DruidButtonSkip_clicked),NULL);
570 DruidButtonOK=GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_OK));
571 gtk_box_pack_end(druid_button_box,GTK_WIDGET(DruidButtonOK),FALSE,TRUE,0);
572 gtk_widget_show(GTK_WIDGET(DruidButtonOK));
573 g_signal_connect((gpointer)DruidButtonOK,"clicked",G_CALLBACK(on_DruidButtonOK_clicked),NULL);
578 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
582 /* Ignore arrors by cabextract during its abortion. */
583 if (in_progress && aborted)
586 /**/ if (log_level & G_LOG_LEVEL_ERROR)
587 dialog=gnome_app_error(App,message);
588 else if (log_level & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING))
589 dialog=gnome_app_warning(App,message);
591 dialog=gnome_app_message(App,message);
593 gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
594 g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
596 /* 'dialog' gets destroyed automatically */
599 static void ui_gnome_interactive(void)
601 gtk_widget_show_all(GTK_WIDGET(App));
606 gboolean ui_gnome_init(void)
608 acquire_module_available_notify=ui_gnome_module_available_notify;
609 acquire_module_all_modules_found_notify=ui_gnome_all_modules_found_notify;
610 ui_progress=ui_gnome_progress;
611 ui_progress_bar=ui_gnome_progress_bar;
612 ui_interactive=ui_gnome_interactive;
613 captivemodid_module_best_priority_notify=ui_gnome_module_best_priority_notify;
615 /* Graphic widgets will all be hidden yet. */
617 /* ui_gnome_g_log_handler() needs 'App'. */
619 G_LOG_DOMAIN, /* log_domain; "Captive" */
620 G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL, /* log_levels */
621 ui_gnome_g_log_handler, /* log_func */
622 NULL); /* user_data */