a8a2eba0ad23ca96411120ce52ea2e9eb9964105
[captive.git] / src / install / acquire / main.c
1 /* $Id$
2  * Drivers acquiring installation utility
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <popt.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <mntent.h>
28 #include <glib/ghash.h>
29 #include <glib/gstrfuncs.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <libgnomevfs/gnome-vfs-init.h>
34 #include "ui-line.h"
35 #include "microsoftcom.h"
36 #include <libgnomevfs/gnome-vfs-uri.h>
37 #include "diskscan.h"
38 #include "moduriload.h"
39 #include <libgnome/gnome-program.h>
40 #include <libgnomeui/gnome-ui-init.h>
41 #include <setjmp.h>
42 #include "ui-gnome.h"
43
44 #include <captive/macros.h>
45 #include <captive/client.h>
46
47
48 int optarg_verbose;
49 int optarg_dry;
50 static int optarg_microsoft_com;
51 static int optarg_scan_disks;
52 static int optarg_scan_disks_quick;
53 static int optarg_text;
54 static char *optarg_modid_path=G_STRINGIFY(SYSCONFDIR) "/w32-mod-id.captivemodid.xml";
55 static GList *optarg_scan_path_list;    /* of (char *) */
56
57 static void acquire_popt_callback
58                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
59
60 static const struct poptOption popt_table[]={
61                 { argInfo:POPT_ARG_CALLBACK,arg:(void *)acquire_popt_callback },
62
63 #define BUG_ACQUIRE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
64                 { \
65                         longName: (longname), \
66                         shortName: (shortname), \
67                         argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
68                         arg: (void *)argP, \
69                         val: (valP), \
70                         descrip: (descripP), \
71                         argDescrip: (argDescripP), \
72                 }
73
74                 BUG_ACQUIRE_POPT(0  ,"text"            ,POPT_ARG_NONE  ,&optarg_text   ,0,
75                                 N_("Disable Gnome UI; --text must be first argument"),NULL),
76                 BUG_ACQUIRE_POPT('v',"verbose"         ,POPT_ARG_NONE  ,&optarg_verbose,0,N_("Display additional debug information"),NULL),
77                 BUG_ACQUIRE_POPT('n',"dry"             ,POPT_ARG_NONE  ,&optarg_dry    ,0,N_("No modifications, no files written"),NULL),
78                 BUG_ACQUIRE_POPT(0  ,"modid-path"      ,POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT,&optarg_modid_path,0,
79                                 N_("Path to .captivemodid.xml database"),N_("path")),
80                 BUG_ACQUIRE_POPT(0  ,"scan-disks"      ,POPT_ARG_NONE  ,&optarg_scan_disks,0,N_("Scan all files on local disks"),NULL),
81                 BUG_ACQUIRE_POPT(0  ,"scan-disks-quick",POPT_ARG_NONE  ,&optarg_scan_disks_quick,0,
82                                 N_("Scan MS-Windows directories on local disks"),NULL),
83                 BUG_ACQUIRE_POPT(0  ,"scan-path" ,POPT_ARG_STRING,NULL              ,0,
84                                 N_("Scan specified disk path or web URL"),N_("path/URI")),
85                 BUG_ACQUIRE_POPT(0  ,"microsoft-com"   ,POPT_ARG_NONE  ,&optarg_microsoft_com,0,
86                                 N_("Download from microsoft.com; Legal: You may need to have valid Microsoft Windows XP license."),NULL),
87
88 #undef BUG_ACQUIRE_POPT
89                 POPT_TABLEEND
90                 };
91
92 static const struct poptOption popt_table_autohelp[]={
93                 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
94                 POPT_AUTOHELP
95                 POPT_TABLEEND
96                 };
97
98 /* poptCallbackType captive_popt_callback */
99 static void acquire_popt_callback
100                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
101 {
102         g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
103
104         if (opt->longName && !strcmp(opt->longName,"scan-path")) {
105                 optarg_scan_path_list=g_list_append(optarg_scan_path_list,gnome_vfs_uri_new(arg));
106                 }
107 }
108
109
110 void (*ui_interactive)(void);
111
112 static gboolean ui_progress_dummy(GnomeVFSURI *uri)
113 {
114         /* 'uri' may be NULL */
115
116         return FALSE;   /* not aborted */
117 }
118
119 gboolean (*ui_progress)(GnomeVFSURI *uri)=ui_progress_dummy;
120 void (*ui_progress_bar)(gint done,gint length);
121
122 static GList *mod_uri_list_local;
123 static void mod_uri_list_local_init(void)
124 {
125         if (mod_uri_list_local)
126                 return;
127         mod_uri_list_local=mod_uri_list();
128 }
129
130 void scan_disks_quick(void)
131 {
132 GList *mod_uri_list_quick_local,*mod_uri_l;
133
134         mod_uri_list_local_init();
135         mod_uri_list_quick_local=NULL;
136         for (mod_uri_l=mod_uri_list_local;mod_uri_l;mod_uri_l=mod_uri_l->next) {
137                 mod_uri_list_quick_local=g_list_prepend(mod_uri_list_quick_local,
138                                 gnome_vfs_uri_append_path(mod_uri_l->data,"windows/system32"));
139                 }
140         mod_uri_list_quick_local=g_list_reverse(mod_uri_list_quick_local);
141         g_list_foreach(mod_uri_list_quick_local,(GFunc)mod_uri_load,NULL);
142         gnome_vfs_uri_list_free(mod_uri_list_quick_local);
143 }
144
145 void scan_disks(void)
146 {
147         mod_uri_list_local_init();
148         g_list_foreach(mod_uri_list_local,(GFunc)mod_uri_load,NULL);
149 }
150
151 void microsoft_com(void)
152 {
153         g_list_foreach(mod_uri_microsoftcom_list(),(GFunc)mod_uri_load_base_reporting,NULL);
154 }
155
156 static void scan_batch(void)
157 {
158         g_list_foreach(optarg_scan_path_list,(GFunc)mod_uri_load_base_reporting,NULL);
159
160         if (optarg_scan_disks_quick)
161                 scan_disks_quick();
162         if (optarg_scan_disks)
163                 scan_disks();
164         if (optarg_microsoft_com)
165                 microsoft_com();
166 }
167
168 static jmp_buf gnome_init_atexit_jmpbuf;
169 static gboolean gnome_init_atexit_disable;
170
171 static void gnome_init_atexit_handler(void)
172 {
173         if (gnome_init_atexit_disable)
174                 return;
175
176         longjmp(gnome_init_atexit_jmpbuf,1);
177         g_assert_not_reached();
178         _exit(EXIT_FAILURE);
179 }
180
181 gboolean gnome_init_g_log_handler_hit;
182 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
183 {
184         gnome_init_g_log_handler_hit=TRUE;
185         g_log_default_handler(log_domain,log_level,message,user_data);
186 }
187
188 int main(int argc,char **argv)
189 {
190 poptContext context;
191 int errint;
192 gboolean is_interactive;
193 gboolean no_gnome;
194
195 #if 0
196         g_log_set_always_fatal(~(0
197                         |G_LOG_LEVEL_MESSAGE
198                         |G_LOG_LEVEL_INFO
199                         |G_LOG_LEVEL_DEBUG
200                         ));
201 #endif
202
203         captive_standalone_init();
204
205         if (!gnome_vfs_init())
206                 g_error(_("GnomeVFS failed to initialize"));
207
208         if (argv[1] && !strcmp(argv[1],"--text"))
209                 optarg_text=1;
210
211         no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
212
213         if (no_gnome) {
214                 context=poptGetContext(
215                                 PACKAGE,        /* name */
216                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
217                                 popt_table_autohelp,    /* options */
218                                 POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
219                 if (context==NULL) {
220                         g_assert_not_reached(); /* argument recognization args_error */
221                         return EXIT_FAILURE;
222                         }
223                 errint=poptReadDefaultConfig(context,
224                                 TRUE);  /* useEnv */
225                 if (errint!=0) {
226                         g_assert_not_reached(); /* argument recognization args_error */
227                         return EXIT_FAILURE;
228                         }
229                 errint=poptGetNextOpt(context);
230                 if (errint!=-1) {
231                         g_assert_not_reached(); /* some non-callbacked argument reached */
232                         return EXIT_FAILURE;
233                         }
234                 if (poptPeekArg(context)) {
235                         g_error(_("No arguments expected"));
236                         return EXIT_FAILURE;
237                         }
238                 }
239         else {
240 GnomeProgram *gnome_program;
241 guint handler_id;
242
243                 captive_standalone_gnome_init();
244                 gnome_init_atexit_disable=FALSE;
245                 g_atexit(gnome_init_atexit_handler);
246                 gnome_init_g_log_handler_hit=FALSE;
247                 handler_id=g_log_set_handler(
248                                 "Gtk",  /* log_domain */
249                                 G_LOG_LEVEL_WARNING,    /* log_levels */
250                                 gnome_init_g_log_handler,       /* log_func */
251                                 NULL);  /* user_data */
252                 if (!setjmp(gnome_init_atexit_jmpbuf))
253                         gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
254                                         GNOME_PARAM_POPT_TABLE,popt_table,
255                                         GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
256                                         NULL);
257                 else {
258                         no_gnome=TRUE;
259                         /* No message: (captive-install-acquire:3693): Gtk-WARNING **: cannot open display:
260                          * was reported, probably only '--help' message was shown.
261                          */
262                         if (!gnome_init_g_log_handler_hit)
263                                 exit(EXIT_SUCCESS);
264                         }
265                 gnome_init_atexit_disable=TRUE;
266                 g_log_remove_handler(
267                                 "Gtk",  /* log_domain */
268                                 handler_id);    /* handler_id */
269                 }
270
271         is_interactive=(1
272                         && ! optarg_scan_path_list
273                         && ! optarg_scan_disks_quick
274                         && ! optarg_scan_disks 
275                         && ! optarg_microsoft_com);
276
277         /* Initialize UI here to catch all GLog errors below. */
278         if (is_interactive
279                         && (no_gnome || !ui_gnome_init())
280                         && !ui_line_init())
281                 g_error(_("No UI interface could be initialized"));
282
283         if (!captive_captivemodid_load(optarg_modid_path) && !captive_captivemodid_load("./w32-mod-id.captivemodid.xml"))
284                 g_error(_("Unable to load modid database: %s"),optarg_modid_path);
285
286         mod_uri_load_base_reporting(gnome_vfs_uri_new("file://" G_STRINGIFY(VARLIBCAPTIVEDIR)));
287
288         if (!is_interactive)
289                 scan_batch();
290         else
291                 (*ui_interactive)();
292
293         gnome_vfs_shutdown();
294
295         return EXIT_SUCCESS;
296 }