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

« back to all changes in this revision

Viewing changes to src/cdroms.c

  • Committer: dann frazier
  • Date: 2020-02-04 02:27:07 UTC
  • Revision ID: dannf@ubuntu.com-20200204022707-37vu1ltkpx2m2t5x
tests/test_motd.py: Fix cut & paste error in comment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#ifdef HAVE_CONFIG_H
3
3
#include "config.h"
4
4
#endif
5
 
#ifdef HAVE_GDU
6
5
#include <glib.h>
7
6
#include <glib-object.h>
 
7
#include <gtk/gtk.h>
8
8
 
9
9
#include <sys/types.h>
10
10
#include <sys/wait.h>
11
11
 
12
12
#include "update-notifier.h"
13
 
#define GDU_API_IS_SUBJECT_TO_CHANGE
14
 
#include <gdu/gdu.h>
15
 
#include "gdu.h"
 
13
#include "cdroms.h"
16
14
 
17
15
#define CDROM_CHECKER PACKAGE_LIB_DIR"/update-notifier/apt-cdrom-check"
18
16
 
19
 
/* reposonses for the dialog */
 
17
/* responses for the dialog */
20
18
enum {
21
19
   RES_START_PM=1,
22
20
   RES_DIST_UPGRADER=2,
23
 
   RES_ADDON_CD=3,
24
 
   RES_APTONCD=4
 
21
   RES_APTONCD=3
25
22
};
26
23
 
27
24
/* Returnvalues from apt-cdrom-check:
28
25
    # 0 - no ubuntu CD
29
 
    # 1 - CD with packages 
 
26
    # 1 - CD with packages
30
27
    # 2 - dist-upgrader CD
31
 
    # 3 - addon CD
32
 
    # 4 - aptoncd media 
 
28
    # 3 - aptoncd media
33
29
*/
34
30
enum {
35
 
   NO_CD, 
36
 
   CD_WITH_PACKAGES, 
 
31
   NO_CD,
 
32
   CD_WITH_PACKAGES,
37
33
   CD_WITH_DISTUPGRADER,
38
 
   CD_WITH_ADDONS,
39
 
   CD_WITH_APTONCD   
 
34
   CD_WITH_APTONCD
40
35
};
41
36
 
42
 
void distro_cd_detected(UpgradeNotifier *un, 
43
 
                        int cdtype, 
44
 
                        const char *mount_point)
 
37
static void
 
38
distro_cd_detected(int cdtype,
 
39
                   const char *mount_point)
45
40
{
46
41
   GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
47
42
                                              GTK_MESSAGE_QUESTION, 
73
68
      gtk_dialog_add_buttons(GTK_DIALOG(dialog), 
74
69
                             GTK_STOCK_CANCEL,
75
70
                             GTK_RESPONSE_REJECT,
76
 
                             _("Start package manager"), 
77
 
                             RES_START_PM,
78
71
                             _("Run upgrade"), 
79
72
                             RES_DIST_UPGRADER,
80
73
                             NULL);
81
74
      gtk_dialog_set_default_response (GTK_DIALOG(dialog), RES_DIST_UPGRADER);
82
75
      break;
83
 
   case CD_WITH_ADDONS:
84
 
      title = _("Addon volume detected");
85
 
      markup = _("<span weight=\"bold\" size=\"larger\">"
86
 
            "An addon volume with software applications has "
87
 
            "been detected.</span>\n\n"
88
 
            "Would you like to view/install the content? ");
89
 
      gtk_dialog_add_buttons(GTK_DIALOG(dialog), 
90
 
                             GTK_STOCK_CANCEL,
91
 
                             GTK_RESPONSE_REJECT,
92
 
                             _("Start package manager"), 
93
 
                             RES_START_PM,
94
 
                             _("Start addon installer"), 
95
 
                             RES_ADDON_CD,
96
 
                             NULL);
97
 
      gtk_dialog_set_default_response (GTK_DIALOG(dialog), RES_ADDON_CD);
98
 
      break;
99
 
      
 
76
#if 0  //  we don't have aptoncd support currently, g-a-i is not
 
77
       // in the archive anymore
100
78
   case CD_WITH_APTONCD:
101
79
      title = _("APTonCD volume detected");
102
80
      markup = _("<span weight=\"bold\" size=\"larger\">"
112
90
                             NULL);
113
91
      gtk_dialog_set_default_response (GTK_DIALOG(dialog), RES_START_PM);
114
92
      break;      
 
93
#endif
115
94
   default:
116
95
      g_assert_not_reached();
117
96
   }
121
100
   gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), markup);
122
101
 
123
102
   int res = gtk_dialog_run (GTK_DIALOG (dialog));
124
 
   char *cmd = NULL;
125
103
   switch(res) {
126
 
   gchar *argv[3];
 
104
   gchar *argv[5];
127
105
   case RES_START_PM:
128
 
      cmd = g_strdup_printf("synaptic --add-cdrom '%s'",mount_point);
129
 
      invoke_with_gksu(cmd, "/usr/share/applications/synaptic.desktop", FALSE);
 
106
      argv[0] = "/usr/lib/update-notifier/backend_helper.py";
 
107
      argv[1] = "add_cdrom";
 
108
      argv[2] = (gchar *)mount_point;
 
109
      argv[3] = NULL;
 
110
      g_spawn_async (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL);
130
111
      break;
131
112
   case RES_DIST_UPGRADER:
132
 
      argv[0] = "/usr/lib/update-notifier/cddistupgrader";
133
 
      argv[1] = (gchar *)mount_point;
134
 
      argv[2] = NULL;
 
113
      argv[0] = "/usr/bin/pkexec";
 
114
      argv[1] = "/usr/lib/update-notifier/cddistupgrader";
 
115
      argv[2] = (gchar *)mount_point;
 
116
      argv[3] = NULL;
135
117
      g_spawn_async (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL);
136
118
      break;
137
 
   case RES_ADDON_CD:
138
 
      cmd = g_strdup_printf("gnome-app-install --addon-cd='%s'", mount_point);
139
 
      invoke_with_gksu(cmd, "/usr/share/applications/gnome-app-install.desktop", FALSE);
140
 
      break;
141
119
   default:
142
120
      /* do nothing */
143
121
      break;
144
122
   }
145
 
   g_free(cmd);
146
123
   gtk_widget_destroy (dialog);
147
124
}
148
125
 
149
 
void 
150
 
up_check_mount_point_for_packages (const char *mount_point, gpointer data)
151
 
{
152
 
   if (!mount_point)
153
 
      return;
154
 
 
155
 
   char *ubuntu_dir = g_strdup_printf("%s/ubuntu",mount_point);
156
 
   char *aptoncd_file = g_strdup_printf("%s/aptoncd.info",mount_point);
157
 
   if(! (g_file_test (ubuntu_dir, G_FILE_TEST_IS_SYMLINK) ||
158
 
         g_file_test (aptoncd_file, G_FILE_TEST_IS_REGULAR) )) {
159
 
      g_free(ubuntu_dir);
160
 
      g_free(aptoncd_file);
161
 
      return;
162
 
   }
163
 
   g_free(ubuntu_dir);
164
 
   g_free(aptoncd_file);
165
 
 
166
 
   /* this looks like a ubuntu CD, run the checker script to verify
167
 
    * this. We expect the following return codes:
168
 
    # 0 - no ubuntu CD
169
 
    # 1 - CD with packages 
170
 
    # 2 - dist-upgrader CD
171
 
    # 3 - addon CD
172
 
    # 4 - aptoncd media
173
 
    * (see data/apt-cdrom-check)
174
 
    */
175
 
   //g_print("this looks like a ubuntu-cdrom\n");
176
 
   char *cmd = g_strdup_printf(CDROM_CHECKER" '%s'",mount_point);
177
 
   int retval=-1;
178
 
   g_spawn_command_line_sync(cmd, NULL, NULL,  &retval, NULL);
179
 
   
180
 
   //g_print("retval: %i \n", WEXITSTATUS(retval));
181
 
   int cdtype = WEXITSTATUS(retval);
182
 
   if(cdtype > 0) {
183
 
      distro_cd_detected(data, cdtype, mount_point);
184
 
   }
185
 
 
186
 
   g_free(cmd);
187
 
}
188
 
 
189
 
void 
190
 
up_device_changed (GduPool *pool, GduDevice *device, gpointer data)
191
 
{
192
 
   //g_print("up_device_changed %s\n", gdu_device_get_device_file (device));
193
 
 
194
 
   // check if that is a removable device
195
 
   if (!gdu_device_is_removable(device))
196
 
      return;
197
 
 
198
 
   // we only care about the first mount point
199
 
   const gchar *p = gdu_device_get_mount_path (device);
200
 
   //g_print("checking mount point %s\n", p);
201
 
   up_check_mount_point_for_packages (p, data);
202
 
}
203
 
 
204
 
 
205
 
void
206
 
up_check_mounted_devices (GduPool *pool, gpointer data)
207
 
{
208
 
   GList *devices = gdu_pool_get_devices (pool);
209
 
   
210
 
   while(devices != NULL) {
211
 
      up_device_changed (pool, devices->data, data);
212
 
      devices = g_list_next(devices);
213
 
   }
214
 
   g_list_free(devices);
215
 
}
216
 
 
217
 
gboolean
218
 
up_do_hal_init (UpgradeNotifier *un)
219
 
{
220
 
   GduPool *pool = gdu_pool_new ();
221
 
   if (pool == NULL)
222
 
      return FALSE;
223
 
 
224
 
   g_signal_connect (pool, "device_changed", (GCallback)up_device_changed, un);
225
 
   // now check what devices we have
226
 
   up_check_mounted_devices(pool, un);
227
 
 
228
 
   return TRUE;
229
 
}
230
 
 
231
 
 
232
 
#else
233
 
#include <glib.h>
234
 
 
235
 
#include "update-notifier.h"
236
 
 
237
 
gboolean
238
 
up_do_hal_init (UpgradeNotifier *un)
239
 
{
240
 
    g_warning("Detection and monitoring of CD-ROMs disabled.");
241
 
    return FALSE;
242
 
}
243
 
#endif // HAVE_GUDEV
 
126
int
 
127
main (int argc, char **argv)
 
128
{
 
129
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
 
130
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
 
131
    textdomain(GETTEXT_PACKAGE);
 
132
 
 
133
    if ( argc != 3 )
 
134
    {
 
135
        printf("usage: %s cdtype mountpoint", argv[0]);
 
136
        return 1;
 
137
    }
 
138
    else
 
139
    {
 
140
        int cdtype = atoi(argv[1]);
 
141
        char *mount_point = argv[2];
 
142
        if(cdtype > 0) {
 
143
            gtk_init (&argc, &argv);
 
144
            distro_cd_detected(cdtype, mount_point);
 
145
            return 0;
 
146
        }
 
147
    }
 
148
}