summaryrefslogtreecommitdiff
path: root/nautilus-installer/src/installer.c
blob: d3ffc6d5cc000ae405d8763572d93e788f2e07f2 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * Copyright (C) 2000 Eazel, Inc
 *
 * 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.
 *
 * Authors: Eskil Heyn Olsen  <eskil@eazel.com>
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>
#include "installer.h"
#include <eazel-install-public.h>
#include <libtrilobite/helixcode-utils.h>

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

#include <sys/utsname.h>

#define EAZEL_SERVICES_DIR_HOME "/var/eazel"
#define EAZEL_SERVICES_DIR EAZEL_SERVICES_DIR_HOME "/services"

#define HOSTNAME "testmachine.eazel.com"
#define PORT_NUMBER 80
#define TMP_DIR "/tmp/eazel-install"
#define RPMRC "/usr/lib/rpm/rpmrc"
#define REMOTE_RPM_DIR "/RPMS"

static char *package_list[LAST] = {
	"/package-list.xml",
	"/package-list-stable.xml",
	"/services-only-list.xml",
	"/package-list.xml",
	"/package-uninstall-list.xml",
};

static char *failure_info = NULL;
int installer_debug = 0;
int installer_test = 0;
int installer_local = 0;

static void 
eazel_install_progress (EazelInstall *service, 
			const PackageData *package,
			int package_num, int num_packages, 
			int amount, int total,
			int total_size_completed, int total_size, 
			GtkWidget *widget) 
{
	GtkProgressBar *progressbar, *progress_overall;
	GtkWidget *summary;
	GtkLabel *package_label;

	package_label = gtk_object_get_data (GTK_OBJECT (widget), "package_label");
	summary = gtk_object_get_data (GTK_OBJECT (widget), "summary");
	progressbar = gtk_object_get_data (GTK_OBJECT (widget), "progressbar_single");
	progress_overall = gtk_object_get_data (GTK_OBJECT (widget), "progressbar_overall");

	if (amount == 0) {
		char *tmp;
		tmp = g_strdup_printf ("Installing %s", package->name);
		gtk_label_set_text (package_label, tmp);
		g_free (tmp);

		gtk_progress_set_format_string (GTK_PROGRESS (progressbar), "%p%% (%v of %u kb)");
		gtk_progress_configure (GTK_PROGRESS (progressbar), 0, 0, (float)(total/1024));		
#ifdef NO_TEXT_BOX
		gtk_label_set_text (GTK_LABEL (summary), package->description);
#else
		gtk_text_backward_delete (GTK_TEXT (summary), 
					  gtk_text_get_length (GTK_TEXT (summary)));
		gtk_text_insert (GTK_TEXT (summary), 
				 NULL, NULL, NULL,
				 package->description, strlen (package->summary));
#endif
			
	}

	if (installer_debug) {
		float pct;
		pct = ( (total > 0) ? ((float) ((((float) amount) / total) * 100)): 100.0);
		fprintf (stdout, "Install Progress - %s - %d %d (%d %d) %% %f\r", 
			 package->name?package->name:"(null)", 
			 amount, total, 
			 total_size_completed, total_size, 
			 pct);
	}

	gtk_progress_set_value (GTK_PROGRESS (progressbar), 
				(float)(amount/1024 > total/1024 ? total/1024 : amount/1024));
	gtk_progress_set_value (GTK_PROGRESS (progress_overall), 
				(float)total_size_completed>total_size ? total_size : total_size_completed);

	fflush (stdout);
	if (amount == total && installer_debug) {
		fprintf (stdout, "\n");
	}
}


static void 
eazel_download_progress (EazelInstall *service, 
			 const char *name,
			 int amount, 
			 int total,
			 GtkWidget *widget) 
{
	GtkProgressBar *progressbar;
	GtkLabel *package_label;

	package_label = gtk_object_get_data (GTK_OBJECT (widget), "package_label");
	progressbar = gtk_object_get_data (GTK_OBJECT (widget), "progressbar_single");

	if (amount == 0) {
		char *tmp;
		tmp = g_strdup_printf ("Retrieving %s", name);
		gtk_label_set_text (package_label, tmp);
		g_free (tmp);

		gtk_progress_set_format_string (GTK_PROGRESS (progressbar), "%p%% (%v of %u kb)");
		gtk_progress_configure (GTK_PROGRESS (progressbar), 0, 0, (float)(total/1024));
	}

	if (installer_debug) {
		float pct;
		pct = ( (total > 0) ? ((float) ((((float) amount) / total) * 100)): 100.0);
		fprintf (stdout, "DOWNLOAD Progress - %s - %d %d %% %f\r", 
			 name?name:"(null)", amount, total, pct); 
		fflush (stdout);
	}
	
	gtk_progress_set_value (GTK_PROGRESS (progressbar), amount/1024);

	if (amount != total) {		
		/* g_main_iteration (FALSE); */
		/* gtk_main_iteration (); */
	} else if (amount == total) {		
		/*
		gtk_progress_set_format_string (GTK_OBJECT (progressbar), "done..."); 
		g_main_iteration (FALSE);
		*/
	}
}

static void
install_failed_helper (EazelInstall *service,
		       const PackageData *pd,
		       char *indent,
		       char **str)
{
	GList *iterator;

	if (pd->toplevel) {
		char *tmp;
		tmp = g_strdup_printf ("%s\n***The package %s failed. Here's the dep tree\n", *str, pd->name);
		g_free (*str);
		(*str) = tmp;
	}
	switch (pd->status) {
	case PACKAGE_DEPENDENCY_FAIL: {
		char *tmp;
		tmp = g_strdup_printf ("%s%s-%s failed\n", *str, indent, rpmfilename_from_packagedata (pd));
		g_free (*str);
		(*str) = tmp;
		break;
	}
	case PACKAGE_CANNOT_OPEN: {
		char *tmp;
		tmp = g_strdup_printf ("%s%s-%s NOT FOUND\n", *str, indent, rpmfilename_from_packagedata (pd));
		g_free (*str);
		(*str) = tmp;
		break;		
	}
	case PACKAGE_SOURCE_NOT_SUPPORTED: {
		char *tmp;
		tmp = g_strdup_printf ("%s%s-%s is a source\n", *str, indent, rpmfilename_from_packagedata (pd));
		g_free (*str);
		(*str) = tmp;
		break;
	}
	case PACKAGE_BREAKS_DEPENDENCY: {
		char *tmp;
		tmp = g_strdup_printf ("%s%s-%s breaks\n", *str, indent, rpmfilename_from_packagedata (pd));
		g_free (*str);
		(*str) = tmp;
		break;
	}
	default: {
		char *tmp;
		tmp = g_strdup_printf ("%s%s-%s\n", *str, indent, rpmfilename_from_packagedata (pd));
		g_free (*str);
		(*str) = tmp;
		break;
	}
	}
	for (iterator = pd->soft_depends; iterator; iterator = iterator->next) {			
		PackageData *pack;
		char *indent2;
		indent2 = g_strconcat (indent, iterator->next ? " |" : "  " , NULL);
		pack = (PackageData*)iterator->data;
		install_failed_helper (service, pack, indent2, str);
		g_free (indent2);
	}
	for (iterator = pd->breaks; iterator; iterator = iterator->next) {			
		PackageData *pack;
		char *indent2;
		indent2 = g_strconcat (indent, iterator->next ? " |" : "  " , NULL);
		pack = (PackageData*)iterator->data;
		install_failed_helper (service, pack, indent2, str);
		g_free (indent2);
	}
}


static void
install_failed (EazelInstall *service,
		const PackageData *pd,
		char **output)
{
	if (pd->toplevel == TRUE) {
		install_failed_helper (service, pd, g_strdup (""), output);
	}
}

static void
download_failed (EazelInstall *service,
		 const char *name,
		 char **output)
{
	if (*output) {		
		(*output) = g_strdup_printf ("%s\nDownload of %s failed", *output, name);
	} else {
		(*output) = g_strdup_printf ("Download of %s failed", name);
	}
}

static void
eazel_install_preflight (EazelInstall *service,
			 int total_size,
			 int num_packages,
			 GtkWidget *widget)
{
	GtkProgressBar *progress_overall;
	GtkLabel *package_label;
	GtkWidget *summary;
	char *summary_string;
	char *tmp;

	summary = gtk_object_get_data (GTK_OBJECT (widget), "summary");
	package_label = gtk_object_get_data (GTK_OBJECT (widget), "package_label");
	progress_overall = gtk_object_get_data (GTK_OBJECT (widget), "progressbar_overall");

	gtk_progress_set_format_string (GTK_PROGRESS (progress_overall), "Total completion %p%%");
	gtk_progress_configure (GTK_PROGRESS (progress_overall), 0, 0, (float)total_size);
	gtk_widget_show (GTK_WIDGET (progress_overall));

	summary_string = g_strdup_printf (_("Now starting the install process.\n"
					    "Starting the process takes some time, please be patient.\n"
					    "In total, %d mb of software will be installed"), 
					  total_size/(1024*1024));
	tmp = g_strdup_printf ("Preparing RPM, %d packages (%d mb)", num_packages, total_size/(1024*1024));

	if (installer_debug) {
		fprintf (stdout, "PREFLIGHT: %s\n", tmp);
	}

	gtk_label_set_text (package_label, tmp);
#ifdef NO_TEXT_BOX
	gtk_label_set_text (GTK_LABEL (summary), summary_string);
#else
	gtk_text_backward_delete (GTK_TEXT (summary), 
				  gtk_text_get_length (GTK_TEXT (summary)));
	gtk_text_insert (GTK_TEXT (summary), 
			 NULL, NULL, NULL,
			 summary_string, strlen (summary_string));
#endif
	g_main_iteration (FALSE);
}

static void
eazel_install_dep_check (EazelInstall *service,
			 const PackageData *pack,
			 const PackageData *needs,
			 GtkWidget *widget)
{
	GtkLabel *action_label;
	GtkLabel *package_label;
	char *tmp;

	action_label = gtk_object_get_data (GTK_OBJECT (widget), "action_label");
	package_label = gtk_object_get_data (GTK_OBJECT (widget), "package_label");

	tmp = g_strdup_printf ("%s needs %d", pack->name, needs->name);

	if (installer_debug) {
		fprintf (stdout, "DEP CHECK : %s\n", tmp);
	}

	gtk_label_set_text (action_label, "Dep check :");
	gtk_label_set_text (package_label, tmp);

	g_main_iteration (FALSE);
}

static gboolean
eazel_install_delete_files (EazelInstall *service,
			    GtkWidget *widget) 
{
	if (installer_debug) {
		fprintf (stdout, "Deleting rpm's\n");
	}
	return TRUE ;
}

static void
make_dirs ()
{
	int retval;
	if (! g_file_test (EAZEL_SERVICES_DIR, G_FILE_TEST_ISDIR)) {
		if (! g_file_test (EAZEL_SERVICES_DIR_HOME, G_FILE_TEST_ISDIR)) {
			retval = mkdir (EAZEL_SERVICES_DIR_HOME, 0755);		       
			if (retval < 0) {
				if (errno != EEXIST) {
					g_error (_("*** Could not create services directory (%s)! ***\n"), 
						 EAZEL_SERVICES_DIR_HOME);
				}
			}
		}

		retval = mkdir (EAZEL_SERVICES_DIR, 0755);
		if (retval < 0) {
			if (errno != EEXIST) {
				g_error (_("*** Could not create services directory (%s)! ***\n"), 
					 EAZEL_SERVICES_DIR);
			}
		}
	}
}

void
check_system (GtkWidget *window)
{
	DistributionInfo dist;

	dist = trilobite_get_distribution ();

#ifndef NAUTILUS_INSTALLER_RELEASE
	if (!installer_test) {
		GnomeDialog *d;
		d = GNOME_DIALOG (gnome_warning_dialog_parented (_("This is a warning, you're running\n"
								   "the installer for real, without \n"
								   "the --test flag... Beware!"),
								 GTK_WINDOW (window)));
		gnome_dialog_run_and_close (d);
	} 
#endif

	g_message ("checking for rehat");
	if (dist.name != DISTRO_REDHAT) {
		GnomeDialog *d;
		d = GNOME_DIALOG (gnome_warning_dialog_parented (_("This preview installer only works\n"
								   "for RPM based systems. You will have\n"
								   "to download the source yourself."),
								 GTK_WINDOW (window)));
		gnome_dialog_run_and_close (d);
		exit (1);
	}

	if (!g_file_test ("/etc/pam.d/helix-update", G_FILE_TEST_ISFILE)) {
		GnomeDialog *d;
		d = GNOME_DIALOG (gnome_warning_dialog_parented (_("You do not have HelixCode gnome installed.\n"
								   "This means I will install the required parts\n"
								   "for you, but you might want to abort the\n"
								   "installer and go to http://www.helixcode.com\n"
								   "and download the full HelixCode Gnome\n"
								   "installation"),
								 GTK_WINDOW (window)));
		gnome_dialog_run_and_close (d);
	}
}

void
revert_nautilus_install (EazelInstall *service)
{
	DIR *dirent;
	struct dirent *de;

	dirent = opendir (EAZEL_SERVICES_DIR);
	
	while (de = readdir (dirent)) {
		if (strncmp (de->d_name, "transaction.", 12)==0) {
			eazel_install_revert_transaction_from_file (service, de->d_name);
			unlink (de->d_name);
		}
	}
}

void installer (GtkWidget *window,
		gint method) 
{
	EazelInstall *service;
	GtkWidget *druid;
/*
	GtkProgressBar *progressbar;
	GtkLabel *package_label;
	GtkLabel *action_label;
*/
	if (method==UPGRADE) {
		gnome_warning_dialog ("We don't do UPGRADE yet");
		return;
	}
	
	/* We set force, update and downgrade to true. */
	service = EAZEL_INSTALL (gtk_object_new (TYPE_EAZEL_INSTALL,
						 "verbose", TRUE,
						 "silent", FALSE,
						 "debug", TRUE,
						 "test", installer_test ? TRUE : FALSE, 
						 "force", FALSE,
						 "depend", FALSE,
						 "update", TRUE,
						 "uninstall", method==UNINSTALL ? TRUE : FALSE,
						 "downgrade", TRUE,
						 "protocol", installer_local ? PROTOCOL_LOCAL: PROTOCOL_HTTP,
						 "tmp_dir", TMP_DIR,
						 "rpmrc_file", RPMRC,
						 "server", HOSTNAME,
						 "package_list", installer_local, 
						 "package_list_storage_path", package_list [ method ],
						 "server_port", PORT_NUMBER,
						 "transaction_dir", EAZEL_SERVICES_DIR,
						 NULL));
	g_assert (service != NULL);

	if (!installer_debug) {
		eazel_install_open_log (service, "/tmp/nautilus-install.log");
	}

	g_assert (service != NULL);

	gtk_signal_connect (GTK_OBJECT (service), "download_progress", 
			    GTK_SIGNAL_FUNC (eazel_download_progress), window);
	gtk_signal_connect (GTK_OBJECT (service), "install_progress", 
			    GTK_SIGNAL_FUNC (eazel_install_progress), window);
	gtk_signal_connect (GTK_OBJECT (service), "preflight_check", 
			    GTK_SIGNAL_FUNC (eazel_install_preflight), window);
	gtk_signal_connect (GTK_OBJECT (service), "dependency_check", 
			    GTK_SIGNAL_FUNC (eazel_install_dep_check), window);
	gtk_signal_connect (GTK_OBJECT (service), "delete_files", 
			    GTK_SIGNAL_FUNC (eazel_install_delete_files), window);
	gtk_signal_connect (GTK_OBJECT (service), "download_failed", 
			    download_failed, &failure_info); 
	gtk_signal_connect (GTK_OBJECT (service), "install_failed", 
			    GTK_SIGNAL_FUNC (install_failed), &failure_info);

	switch (method) {
	case FULL_INST:
	case NAUTILUS_ONLY:
	case SERVICES_ONLY:
		eazel_install_install_packages (service, NULL);
		break;
	case UPGRADE:	
	{
		GList *categories;
		CategoryData *cat;
		PackageData *pack;

		cat = categorydata_new ();
		pack = packagedata_new ();
		pack->name = g_strdup ("nautilus");
		pack->distribution = trilobite_get_distribution ();
		cat->packages = g_list_prepend (NULL, pack);
		categories = g_list_prepend (NULL, cat);
		eazel_install_install_packages (service, categories);
	}
	break;
	case UNINSTALL:
		revert_nautilus_install (service);
		break;
	};

	gtk_object_destroy (GTK_OBJECT (service)); 

/*
	progressbar = gtk_object_get_data (GTK_OBJECT (window), "progressbar_single");
	gtk_progress_set_format_string (GTK_PROGRESS (progressbar), "done");
	progressbar = gtk_object_get_data (GTK_OBJECT (window), "progressbar_overall");
	gtk_progress_set_format_string (GTK_PROGRESS (progressbar), "done");
*/

	if (failure_info && strlen (failure_info)>1) {
		if (installer_debug) {
			fprintf (stdout, "ERROR :\n%s", failure_info);
		}
		gnome_error_dialog_parented (failure_info, GTK_WINDOW (window));
	} else {
	}
}


/* Dummy functions to make linking work */

const gpointer oaf_popt_options = NULL;
gpointer oaf_init (int argc, char *argv[]) {}
int bonobo_init (gpointer a, gpointer b, gpointer c) {};