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
|
#include <sys/types.h>
#include <sys/wait.h>
#include "update-notifier.h"
#include "hal.h"
#define CDROM_CHECKER PACKAGE_LIB_DIR"/update-notifier/apt-cdrom-check"
/* reposonses for the dialog */
#define RES_START_PM 1
#define RES_UPGRADE_AUTOMATIC 2
void distro_cd_detected(UpgradeNotifier *un, char *mount_point)
{
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
NULL );
gtk_window_set_title(GTK_WINDOW(dialog),_("Ubuntu CD detected"));
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog),
_("<span weight=\"bold\" size=\"larger\">"
"Ubuntu CD detected</span> \n\n"
"You can automatically upgrade from it, "
"start the package manager application "
"or cancel"));
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
GTK_STOCK_CANCEL,
GTK_RESPONSE_REJECT,
_("Start package manager"),
RES_START_PM,
_("Automatically upgrade"),
RES_UPGRADE_AUTOMATIC,
NULL);
int res = gtk_dialog_run (GTK_DIALOG (dialog));
if(res == RES_START_PM) {
//g_print("trying to start pm\n");
char *synaptic = g_strdup_printf("synaptic --add-cdrom %s",mount_point);
char *cmd[] = {"/usr/bin/gksudo", synaptic,NULL};
g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
g_free(synaptic);
} else if (res == RES_UPGRADE_AUTOMATIC) {
char *synaptic = g_strdup_printf("synaptic --add-cdrom %s --dist-upgrade-mode --non-interactive",mount_point);
char *cmd[] = {"/usr/bin/gksudo", synaptic, NULL};
g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
g_free(synaptic);
}
gtk_widget_destroy (dialog);
}
/** Internal HAL initialization function
*
* @functions The LibHalFunctions to register as callbacks.
* @return The LibHalContext of the HAL connection or
* NULL on error.
*/
LibHalContext *
up_do_hal_init (LibHalFunctions *functions)
{
LibHalContext *ctx;
char **devices;
int nr;
ctx = hal_initialize (functions, FALSE);
if (!ctx) {
g_warning ("failed to initialize HAL!\n");
return NULL;
}
if (hal_device_property_watch_all (ctx)) {
g_warning ("failed to watch all HAL properties!\n");
hal_shutdown (ctx);
return NULL;
}
/*
* Do something to ping the HAL daemon - the above functions will
* succeed even if hald is not running, so long as DBUS is. But we
* want to exit silently if hald is not running, to behave on
* pre-2.6 systems.
*/
devices = hal_get_all_devices (ctx, &nr);
if (!devices) {
g_warning ("seems that HAL is not running\n");
hal_shutdown (ctx);
return NULL;
}
hal_free_string_array (devices);
return ctx;
}
/** Invoked by libhal for integration with our mainloop.
*
* @param ctx LibHal context
* @param dbus_connection D-BUS connection to integrate
*/
void
hal_mainloop_integration (LibHalContext *ctx __attribute__((__unused__)),
DBusConnection * dbus_connection)
{
dbus_connection_setup_with_g_main (dbus_connection, NULL);
}
/** Type for callback when a property of a device changes.
*
* @param udi Unique Device Id
* @param key Name of the property that has changed
* @param is_removed Property removed
* @param is_added Property added
*/
void
hal_property_modified(LibHalContext *ctx, const char *udi, const char *key,
dbus_bool_t is_removed, dbus_bool_t is_added)
{
char *mount_point;
//g_print("hal_device_changed()\n");
/* we are only interessted in mount events from the cdrom*/
if (g_strcasecmp (key, "volume.is_mounted") != 0)
return;
if(!hal_device_get_property_bool(ctx, udi, key))
return;
char* storage_device = hal_device_get_property_string (ctx, udi,
"block.storage_device");
if (!storage_device) {
g_warning("cannot get storage_device\n");
return;
}
char* media_type = hal_device_get_property_string (ctx, storage_device,
"storage.drive_type");
if(!media_type) {
g_warning("cannot get storage.drive_type\n");
return;
}
if (g_ascii_strcasecmp(media_type, "cdrom") != 0) {
g_warning("no cdrom: %s\n",media_type);
return;
}
mount_point = hal_device_get_property_string (ctx, udi,
"volume.mount_point");
//g_print("mounted: %s on %s\n", udi, mount_point);
char *ubuntu_dir = g_strdup_printf("%s/ubuntu",mount_point);
if(!g_file_test (ubuntu_dir, G_FILE_TEST_IS_SYMLINK)) {
g_free(ubuntu_dir);
return;
}
//g_print("this looks like a ubuntu-cdrom\n");
char *cmd = g_strdup_printf(CDROM_CHECKER" %s",mount_point);
int retval=-1;
g_spawn_command_line_sync(cmd, NULL, NULL, &retval, NULL);
//g_print("retval: %i \n", retval);
if(WEXITSTATUS(retval) == 1) {
distro_cd_detected(hal_ctx_get_user_data(ctx), mount_point);
}
g_free(cmd);
g_free(ubuntu_dir);
}
|