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

« back to all changes in this revision

Viewing changes to src/hal.c

  • Committer: mvo
  • Date: 2005-02-03 12:45:41 UTC
  • Revision ID: gustavo@niemeyer.net-20050203124541-66b00f59dd8cee24
* src/rfc822.h: added a comment explaining about the memory use
* src/hooks.c: basic i18n support added, needs more love

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
 
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
      char *cmd[] = {"/usr/bin/gksudo", synaptic,NULL};
 
43
      g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
 
44
      g_free(synaptic);
 
45
   } else if (res == RES_UPGRADE_AUTOMATIC) {
 
46
      char *synaptic = g_strdup_printf("synaptic --add-cdrom %s --dist-upgrade-mode --non-interactive",mount_point);
 
47
      char *cmd[] = {"/usr/bin/gksudo", synaptic, NULL};
 
48
      g_spawn_async("/",cmd,NULL,0,NULL,NULL,NULL,NULL);
 
49
      g_free(synaptic);
49
50
   }
50
51
   gtk_widget_destroy (dialog);
51
52
}
52
53
 
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
 
54
/** Internal HAL initialization function
71
55
 *
72
56
 * @functions                   The LibHalFunctions to register as callbacks.
73
57
 * @return                      The LibHalContext of the HAL connection or
74
58
 *                              NULL on error.
75
59
 */
76
60
LibHalContext *
77
 
up_do_hal_init ()
 
61
up_do_hal_init (LibHalFunctions *functions)
78
62
{
79
 
        DBusError error;
 
63
        LibHalContext *ctx;
80
64
        char **devices;
81
65
        int nr;
82
66
 
83
 
        hal_ctx = libhal_ctx_new ();
84
 
        if (!hal_ctx) {
 
67
        ctx = hal_initialize (functions, FALSE);
 
68
        if (!ctx) {
85
69
                g_warning ("failed to initialize HAL!\n");
86
70
                return NULL;
87
71
        }
88
72
 
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);
 
73
        if (hal_device_property_watch_all (ctx)) {
 
74
                g_warning ("failed to watch all HAL properties!\n");
 
75
                hal_shutdown (ctx);
109
76
                return NULL;
110
77
        }
111
78
 
115
82
         * want to exit silently if hald is not running, to behave on
116
83
         * pre-2.6 systems.
117
84
         */
118
 
        devices = libhal_get_all_devices (hal_ctx, &nr, &error);
 
85
        devices = hal_get_all_devices (ctx, &nr);
119
86
        if (!devices) {
120
87
                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);
 
88
                hal_shutdown (ctx);
125
89
                return NULL;
126
90
        }
127
 
        libhal_free_string_array (devices);
 
91
        hal_free_string_array (devices);
128
92
 
129
 
        return hal_ctx;
 
93
        return ctx;
130
94
}
131
95
 
132
 
/** Invoked by GLib in response to a D-BUS disconnect event.
 
96
/** Invoked by libhal for integration with our mainloop. 
133
97
 *
134
 
 * @param  data                 Context pointer
 
98
 *  @param  ctx                 LibHal context
 
99
 *  @param  dbus_connection     D-BUS connection to integrate
135
100
 */
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;
 
101
void
 
102
hal_mainloop_integration (LibHalContext *ctx __attribute__((__unused__)),
 
103
                          DBusConnection * dbus_connection)
 
104
{
 
105
        dbus_connection_setup_with_g_main (dbus_connection, NULL);
207
106
}
208
107
 
209
108
/** Type for callback when a property of a device changes. 
218
117
                      dbus_bool_t is_removed, dbus_bool_t is_added)
219
118
{
220
119
   char *mount_point;
221
 
   //g_print("hal_device_changed(): %s\n",key);
 
120
   //g_print("hal_device_changed()\n");
222
121
 
223
122
   /* we are only interessted in mount events from the cdrom*/
224
123
   if (g_strcasecmp (key, "volume.is_mounted") != 0)
225
124
      return;
226
 
   if(!libhal_device_get_property_bool(ctx, udi, key, NULL))
 
125
   if(!hal_device_get_property_bool(ctx, udi, key))
227
126
      return;
228
127
 
229
 
   char* storage_device = libhal_device_get_property_string (ctx, udi,
230
 
                                                    "block.storage_device",
231
 
                                                             NULL);
 
128
   char* storage_device = hal_device_get_property_string (ctx, udi,
 
129
                                                    "block.storage_device");
232
130
   if (!storage_device) {
233
131
      g_warning("cannot get storage_device\n");
234
132
      return;
235
133
   }
236
 
   char* media_type = libhal_device_get_property_string (ctx, storage_device, 
237
 
                                                      "storage.drive_type",
238
 
                                                      NULL);
 
134
   char* media_type = hal_device_get_property_string (ctx, storage_device, 
 
135
                                                      "storage.drive_type");
239
136
   if(!media_type) {
240
137
      g_warning("cannot get storage.drive_type\n");
241
138
      return;
245
142
      return;
246
143
   }
247
144
      
248
 
   mount_point = libhal_device_get_property_string (ctx, udi, 
249
 
                                                    "volume.mount_point",
250
 
                                                    NULL);
 
145
   mount_point = hal_device_get_property_string (ctx, udi, 
 
146
                                                 "volume.mount_point");
251
147
   //g_print("mounted: %s on %s\n", udi, mount_point);
252
148
 
253
149
   char *ubuntu_dir = g_strdup_printf("%s/ubuntu",mount_point);
261
157
   int retval=-1;
262
158
   g_spawn_command_line_sync(cmd, NULL, NULL,  &retval, NULL);
263
159
   
264
 
   //g_print("retval: %i \n", WEXITSTATUS(retval));
 
160
   //g_print("retval: %i \n", retval);
265
161
   if(WEXITSTATUS(retval) == 1) {
266
 
      distro_cd_detected(libhal_ctx_get_user_data(ctx), mount_point);
 
162
      distro_cd_detected(hal_ctx_get_user_data(ctx), mount_point);
267
163
   }
268
164
 
269
165
   g_free(cmd);