~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to src/hal.c

  • Committer: mvo
  • Date: 2005-01-15 00:23:58 UTC
  • Revision ID: gustavo@niemeyer.net-20050115002358-469848491f936cd5
* big rename: upgrade-notifier -> update-notifier

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifdef HAVE_CONFIG_H
2
 
#include "config.h"
3
 
#endif
4
 
 
5
1
#include <sys/types.h>
6
2
#include <sys/wait.h>
7
3
 
9
5
#include "update-notifier.h"
10
6
#include "hal.h"
11
7
 
12
 
#define CDROM_CHECKER PACKAGE_LIB_DIR"/update-notifier/apt-cdrom-check"
 
8
#define CDROM_CHECKER PACKAGE_LIB_DIR"/upgrade-notifier/apt-cdrom-check"
13
9
 
14
10
/* reposonses for the dialog */
15
11
#define RES_START_PM 1
16
12
#define RES_UPGRADE_AUTOMATIC 2
17
13
 
18
 
static LibHalContext *hal_ctx;
19
 
static DBusConnection *dbus_connection;
20
14
 
21
15
void distro_cd_detected(UpgradeNotifier *un, char *mount_point)
22
16
{
25
19
                                              GTK_BUTTONS_NONE,
26
20
                                              NULL );
27
21
   gtk_window_set_title(GTK_WINDOW(dialog),_("Ubuntu CD detected"));
28
 
   gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
29
22
   gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog),
30
23
                                 _("<span weight=\"bold\" size=\"larger\">"
31
24
                                   "Ubuntu CD detected</span> \n\n"
32
 
                                   "You can start the package manager "
33
 
                                   "application with it now."));
 
25
                                   "You can automatically upgrade from it, "
 
26
                                   "start the package manager application "
 
27
                                   "or cancel"));
34
28
   gtk_dialog_add_buttons(GTK_DIALOG(dialog),
35
29
                          GTK_STOCK_CANCEL,
36
30
                          GTK_RESPONSE_REJECT,
37
31
                          _("Start package manager"), 
38
32
                          RES_START_PM,
 
33
                          _("Automatically upgrade"), 
 
34
                          RES_UPGRADE_AUTOMATIC,
39
35
                          NULL);
40
36
 
41
37
 
42
38
   int res = gtk_dialog_run (GTK_DIALOG (dialog));
43
 
   char *cmd;
44
 
   switch(res) {
45
 
   case RES_START_PM:
46
 
      cmd = g_strdup_printf("synaptic --add-cdrom %s",mount_point);
47
 
      invoke_with_gksu(cmd, _("Package manager"));
48
 
      break;
 
39
   if(res == RES_START_PM) {
 
40
      //g_print("trying to start pm\n");
 
41
      char *synaptic = g_strdup_printf("synaptic --add-cdrom %s",mount_point);
 
42
      const char *cmd[] = {"/usr/bin/gksudo", synaptic,
 
43
                           NULL};
 
44
      g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
 
45
      g_free(synaptic);
 
46
   } else if (res == RES_UPGRADE_AUTOMATIC) {
 
47
      char *synaptic = g_strdup_printf("synaptic --add-cdrom %s --dist-upgrade-mode --non-interactive",mount_point);
 
48
      const char *cmd[] = {"/usr/bin/gksudo", synaptic,
 
49
                           NULL};
 
50
      g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
 
51
      g_free(synaptic);
49
52
   }
50
53
   gtk_widget_destroy (dialog);
51
54
}
52
55
 
53
 
#if 0
54
 
LibHalFunctions *
55
 
up_return_hal_functions ()
56
 
{
57
 
       static const LibHalFunctions hal_functions = { 
58
 
          hal_mainloop_integration,
59
 
          NULL /*hal_device_added*/,
60
 
          NULL /*hal_device_removed*/,
61
 
          NULL /*hal_device_new_capability*/,
62
 
          NULL /*hal_device_lost_capability*/,
63
 
          hal_property_modified,
64
 
          NULL /*hal_device_condition*/
65
 
       };
66
 
       return &hal_functions;
67
 
}
68
 
#endif
69
 
 
70
 
/** Internal UP initialization function
 
56
/** Internal HAL initialization function
71
57
 *
72
58
 * @functions                   The LibHalFunctions to register as callbacks.
73
59
 * @return                      The LibHalContext of the HAL connection or
74
60
 *                              NULL on error.
75
61
 */
76
62
LibHalContext *
77
 
up_do_hal_init ()
 
63
up_do_hal_init (LibHalFunctions *functions)
78
64
{
79
 
        DBusError error;
 
65
        LibHalContext *ctx;
80
66
        char **devices;
81
67
        int nr;
82
68
 
83
 
        hal_ctx = libhal_ctx_new ();
84
 
        if (!hal_ctx) {
85
 
                g_warning ("failed to initialize HAL!\n");
86
 
                return NULL;
87
 
        }
88
 
 
89
 
        dbus_error_init (&error);
90
 
        if (!hal_mainloop_integration (hal_ctx, &error)) {
91
 
                g_warning ("hal_initialize failed: %s\n", error.message);
92
 
                dbus_error_free (&error);
93
 
                return NULL;
94
 
        }
95
 
 
96
 
        libhal_ctx_set_device_property_modified (hal_ctx,
97
 
                                                 hal_property_modified);
98
 
 
99
 
        if (!libhal_device_property_watch_all (hal_ctx, &error)) {
100
 
                g_warning ("failed to watch all HAL properties!: %s\n", error.message);
101
 
                libhal_ctx_shutdown (hal_ctx, &error);
102
 
                return NULL;
103
 
        }
104
 
 
105
 
        if (!libhal_ctx_init (hal_ctx, &error)) {
106
 
                warn ("hal_initialize failed: %s\n", error.message);
107
 
                dbus_error_free (&error);
108
 
                libhal_ctx_free (hal_ctx);
 
69
        ctx = hal_initialize (functions, FALSE);
 
70
        if (!ctx) {
 
71
                warn ("failed to initialize HAL!\n");
 
72
                return NULL;
 
73
        }
 
74
 
 
75
        if (hal_device_property_watch_all (ctx)) {
 
76
                warn ("failed to watch all HAL properties!\n");
 
77
                hal_shutdown (ctx);
109
78
                return NULL;
110
79
        }
111
80
 
115
84
         * want to exit silently if hald is not running, to behave on
116
85
         * pre-2.6 systems.
117
86
         */
118
 
        devices = libhal_get_all_devices (hal_ctx, &nr, &error);
 
87
        devices = hal_get_all_devices (ctx, &nr);
119
88
        if (!devices) {
120
 
                g_warning ("seems that HAL is not running\n");
121
 
                libhal_ctx_shutdown (hal_ctx, &error);
122
 
                if (dbus_error_is_set (&error))
123
 
                        dbus_error_free (&error);
124
 
                libhal_ctx_free (hal_ctx);
 
89
                warn ("seems that HAL is not running\n");
 
90
                hal_shutdown (ctx);
125
91
                return NULL;
126
92
        }
127
 
        libhal_free_string_array (devices);
 
93
        hal_free_string_array (devices);
128
94
 
129
 
        return hal_ctx;
 
95
        return ctx;
130
96
}
131
97
 
132
 
/** Invoked by GLib in response to a D-BUS disconnect event.
 
98
/** Invoked by libhal for integration with our mainloop. 
133
99
 *
134
 
 * @param  data                 Context pointer
 
100
 *  @param  ctx                 LibHal context
 
101
 *  @param  dbus_connection     D-BUS connection to integrate
135
102
 */
136
 
static gboolean
137
 
up_reconnect_to_hal (gpointer data __attribute__((__unused__)))
138
 
{
139
 
        static unsigned int retries = 0;
140
 
 
141
 
        g_message ("Trying a reconnect ...");
142
 
        hal_ctx = up_do_hal_init ();
143
 
        if (hal_ctx != NULL) {
144
 
                g_message ("Reconnected OK.");
145
 
                retries = 0;
146
 
                return FALSE;
147
 
        } else if (dbus_connection) { 
148
 
           dbus_connection_unref (dbus_connection);
149
 
           dbus_connection = NULL;
150
 
        }
151
 
 
152
 
        /* Retry later if it failed. */
153
 
        if (retries++ < 6000)
154
 
                return TRUE;
155
 
 
156
 
        /* Too many retries; clean up and bail. */
157
 
        warn("gvm_reconnect_to_hal: no reconnection after 6000 retries, "
158
 
             "exiting\n");
159
 
 
160
 
        /* Too many retries; clean up and bail. */
161
 
        gtk_main_quit ();
162
 
        return FALSE;
163
 
}
164
 
 
165
 
static DBusHandlerResult
166
 
up_do_filter_dbus_msg (DBusConnection *connection __attribute__((__unused__)),
167
 
                        DBusMessage *message,
168
 
                        void *user_data __attribute__((__unused__)))
169
 
{
170
 
        DBusError error;
171
 
 
172
 
        if (dbus_message_is_signal (message,
173
 
                                    DBUS_INTERFACE_LOCAL,
174
 
                                    "Disconnected")) {
175
 
                g_timeout_add(1000, up_reconnect_to_hal, NULL);
176
 
                libhal_ctx_shutdown (hal_ctx, &error);
177
 
                libhal_ctx_free (hal_ctx);
178
 
                hal_ctx = NULL;
179
 
                dbus_connection_unref (dbus_connection);
180
 
                dbus_connection = NULL;
181
 
                return DBUS_HANDLER_RESULT_HANDLED;
182
 
        }
183
 
        else
184
 
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
185
 
}
186
 
 
187
 
static dbus_bool_t
188
 
hal_mainloop_integration (LibHalContext *ctx, 
189
 
                           DBusError *error)
190
 
                          
191
 
{
192
 
 
193
 
        dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, error);
194
 
 
195
 
        if (dbus_error_is_set (error))
196
 
                return FALSE;
197
 
 
198
 
        dbus_connection_set_exit_on_disconnect (dbus_connection, FALSE);
199
 
 
200
 
        dbus_connection_setup_with_g_main (dbus_connection, NULL);
201
 
        dbus_connection_add_filter (dbus_connection, up_do_filter_dbus_msg, 
202
 
                                    NULL, NULL);
203
 
        
204
 
        libhal_ctx_set_dbus_connection (ctx, dbus_connection);
205
 
        
206
 
        return TRUE;
 
103
void
 
104
hal_mainloop_integration (LibHalContext *ctx __attribute__((__unused__)),
 
105
                          DBusConnection * dbus_connection)
 
106
{
 
107
        dbus_connection_setup_with_g_main (dbus_connection, NULL);
207
108
}
208
109
 
209
110
/** Type for callback when a property of a device changes. 
217
118
hal_property_modified(LibHalContext *ctx, const char *udi, const char *key,
218
119
                      dbus_bool_t is_removed, dbus_bool_t is_added)
219
120
{
 
121
   dbus_bool_t val;
220
122
   char *mount_point;
221
 
   //g_print("hal_device_changed(): %s\n",key);
 
123
   //g_print("hal_device_changed()\n");
222
124
 
223
125
   /* we are only interessted in mount events from the cdrom*/
224
126
   if (g_strcasecmp (key, "volume.is_mounted") != 0)
225
127
      return;
226
 
   if(!libhal_device_get_property_bool(ctx, udi, key, NULL))
 
128
   if(!hal_device_get_property_bool(ctx, udi, key))
227
129
      return;
228
130
 
229
 
   char* storage_device = libhal_device_get_property_string (ctx, udi,
230
 
                                                    "block.storage_device",
231
 
                                                             NULL);
 
131
   char* storage_device = hal_device_get_property_string (ctx, udi,
 
132
                                                    "block.storage_device");
232
133
   if (!storage_device) {
233
 
      g_warning("cannot get storage_device\n");
 
134
      warn("cannot get storage_device\n");
234
135
      return;
235
136
   }
236
 
   char* media_type = libhal_device_get_property_string (ctx, storage_device, 
237
 
                                                      "storage.drive_type",
238
 
                                                      NULL);
 
137
   char* media_type = hal_device_get_property_string (ctx, storage_device, 
 
138
                                                      "storage.drive_type");
239
139
   if(!media_type) {
240
 
      g_warning("cannot get storage.drive_type\n");
 
140
      warn("cannot get storage.drive_type\n");
241
141
      return;
242
142
   }
243
143
   if (g_ascii_strcasecmp(media_type, "cdrom") != 0) {
244
 
      g_warning("no cdrom: %s\n",media_type);
 
144
      warn("no cdrom: %s\n",media_type);
245
145
      return;
246
146
   }
247
147
      
248
 
   mount_point = libhal_device_get_property_string (ctx, udi, 
249
 
                                                    "volume.mount_point",
250
 
                                                    NULL);
 
148
   mount_point = hal_device_get_property_string (ctx, udi, 
 
149
                                                 "volume.mount_point");
251
150
   //g_print("mounted: %s on %s\n", udi, mount_point);
252
151
 
253
152
   char *ubuntu_dir = g_strdup_printf("%s/ubuntu",mount_point);
261
160
   int retval=-1;
262
161
   g_spawn_command_line_sync(cmd, NULL, NULL,  &retval, NULL);
263
162
   
264
 
   //g_print("retval: %i \n", WEXITSTATUS(retval));
 
163
   //g_print("retval: %i \n", retval);
265
164
   if(WEXITSTATUS(retval) == 1) {
266
 
      distro_cd_detected(libhal_ctx_get_user_data(ctx), mount_point);
 
165
      distro_cd_detected(hal_ctx_get_user_data(ctx), mount_point);
267
166
   }
268
167
 
269
168
   g_free(cmd);