Debian Codegraph @

lib/packagekit-glib2/pk-task-text.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2009 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #include <glib/gi18n.h>
25 #include <packagekit-glib2/pk-eula-required.h>
26 #include <packagekit-glib2/pk-media-change-required.h>
27 #include <packagekit-glib2/pk-package.h>
28 #include <packagekit-glib2/pk-package-id.h>
29 #include <packagekit-glib2/pk-repo-signature-required.h>
30 #include <packagekit-glib2/pk-task.h>
31
32 #include "pk-task-text.h"
33 #include "pk-console-private.h"
34
35 struct _PkTaskText
36 {
37 PkTask parent;
38 };
39
40 G_DEFINE_TYPE (PkTaskText, pk_task_text, PK_TYPE_TASK)
41
42 /*
43 * pk_task_text_untrusted_question:
44 **/
45 static void
46 pk_task_text_untrusted_question (PkTask *task, guint request, PkResults *results)
47 {
48 gboolean ret;
49
50 /* clear new line */
51 g_print ("\n");
52
53 /* TRANSLATORS: ask the user if they are comfortable installing insecure packages */
54 ret = pk_console_get_prompt (_("Do you want to allow installing of unsigned software?"), FALSE);
55 if (ret) {
56 pk_task_user_accepted (task, request);
57 } else {
58 /* TRANSLATORS: tell the user we've not done anything */
59 g_print ("%s\n", _("The unsigned software will not be installed."));
60 pk_task_user_declined (task, request);
61 }
62 }
63
64 /*
65 * pk_task_text_key_question:
66 **/
67 static void
68 pk_task_text_key_question (PkTask *task, guint request, PkResults *results)
69 {
70 guint i;
71 gboolean ret;
72 GPtrArray *array;
73 gchar *printable = NULL;
74 gchar *package_id;
75 gchar *repository_name;
76 gchar *key_url;
77 gchar *key_userid;
78 gchar *key_id;
79 gchar *key_fingerprint;
80 gchar *key_timestamp;
81 PkRepoSignatureRequired *item;
82
83 /* clear new line */
84 g_print ("\n");
85
86 /* get data */
87 array = pk_results_get_repo_signature_required_array (results);
88 for (i = 0; i < array->len; i++) {
89 item = g_ptr_array_index (array, i);
90 g_object_get (item,
91 "package-id", &package_id,
92 "repository-name", &repository_name,
93 "key-url", &key_url,
94 "key-userid", &key_userid,
95 "key-id", &key_id,
96 "key-fingerprint", &key_fingerprint,
97 "key-timestamp", &key_timestamp,
98 NULL);
99
100 /* create printable */
101 printable = pk_package_id_to_printable (package_id);
102
103 /* TRANSLATORS: the package repository is signed by a key that is not recognised */
104 g_print ("%s\n", _("Software source signature required"));
105
106 /* TRANSLATORS: the package that is not signed by a known key */
107 g_print (" %s: %s\n", _("Package"), printable);
108
109 /* TRANSLATORS: the package repository name */
110 g_print (" %s: %s\n", _("Software source name"), repository_name);
111
112 /* TRANSLATORS: the key URL */
113 g_print (" %s: %s\n", _("Key URL"), key_url);
114
115 /* TRANSLATORS: the username of the key */
116 g_print (" %s: %s\n", _("Key user"), key_userid);
117
118 /* TRANSLATORS: the key ID, usually a few hex digits */
119 g_print (" %s: %s\n", _("Key ID"), key_id);
120
121 /* TRANSLATORS: the key fingerprint, again, yet more hex */
122 g_print (" %s: %s\n", _("Key fingerprint"), key_fingerprint);
123
124 /* TRANSLATORS: the timestamp (a bit like a machine readable time) */
125 g_print (" %s: %s\n", _("Key Timestamp"), key_timestamp);
126
127 g_free (printable);
128 g_free (package_id);
129 g_free (repository_name);
130 g_free (key_url);
131 g_free (key_userid);
132 g_free (key_id);
133 g_free (key_fingerprint);
134 g_free (key_timestamp);
135 }
136
137 /* TRANSLATORS: ask the user if they want to import */
138 ret = pk_console_get_prompt (_("Do you accept this signature?"), FALSE);
139 if (ret) {
140 pk_task_user_accepted (task, request);
141 } else {
142 /* TRANSLATORS: tell the user we've not done anything */
143 g_print ("%s\n", _("The signature was not accepted."));
144 pk_task_user_declined (task, request);
145 }
146
147 g_ptr_array_unref (array);
148 }
149
150 /*
151 * pk_task_text_eula_question:
152 **/
153 static void
154 pk_task_text_eula_question (PkTask *task, guint request, PkResults *results)
155 {
156 guint i;
157 gboolean ret;
158 GPtrArray *array;
159
160 /* clear new line */
161 g_print ("\n");
162
163 /* get data */
164 array = pk_results_get_eula_required_array (results);
165 for (i = 0; i < array->len; i++) {
166 PkEulaRequired *item;
167 g_autofree gchar *printable = NULL;
168
169 item = g_ptr_array_index (array, i);
170
171 /* create printable */
172 printable = pk_package_id_to_printable (pk_eula_required_get_package_id (item));
173
174 /* TRANSLATORS: this is another name for a software licence that has to be read before installing */
175 g_print ("%s\n", _("End user licence agreement required"));
176
177 /* TRANSLATORS: the package name that was trying to be installed */
178 g_print (" %s: %s\n", _("Package"), printable);
179
180 /* TRANSLATORS: the vendor (e.g. vmware) that is providing the EULA */
181 g_print (" %s: %s\n", _("Vendor"), pk_eula_required_get_vendor_name (item));
182
183 /* TRANSLATORS: the EULA text itself (long and boring) */
184 g_print (" %s: %s\n", _("Agreement"), pk_eula_required_get_license_agreement (item));
185 }
186
187 /* TRANSLATORS: ask the user if they've read and accepted the EULA */
188 ret = pk_console_get_prompt (_("Do you accept this agreement?"), FALSE);
189 if (ret) {
190 pk_task_user_accepted (task, request);
191 } else {
192 /* TRANSLATORS: tell the user we've not done anything */
193 g_print ("%s\n", _("The agreement was not accepted."));
194 pk_task_user_declined (task, request);
195 }
196
197 g_ptr_array_unref (array);
198 }
199
200 /*
201 * pk_task_text_media_change_question:
202 **/
203 static void
204 pk_task_text_media_change_question (PkTask *task, guint request, PkResults *results)
205 {
206 guint i;
207 gboolean ret;
208 GPtrArray *array;
209 PkMediaChangeRequired *item;
210 gchar *media_id;
211 PkMediaTypeEnum media_type;
212 gchar *media_text;
213
214 /* clear new line */
215 g_print ("\n");
216
217 /* get data */
218 array = pk_results_get_media_change_required_array (results);
219 for (i = 0; i < array->len; i++) {
220 item = g_ptr_array_index (array, i);
221 g_object_get (item,
222 "media-id", &media_id,
223 "media-type", &media_type,
224 "media-text", &media_text,
225 NULL);
226
227 /* TRANSLATORS: the user needs to change media inserted into the computer */
228 g_print ("%s\n", _("Media change required"));
229
230 /* TRANSLATORS: the type, e.g. DVD, CD, etc */
231 g_print (" %s: %s\n", _("Media type"), pk_media_type_enum_to_string (media_type));
232
233 /* TRANSLATORS: the media label, usually like 'disk-1of3' */
234 g_print (" %s: %s\n", _("Media label"), media_id);
235
236 /* TRANSLATORS: the media description, usually like 'Fedora 12 disk 5' */
237 g_print (" %s: %s\n", _("Text"), media_text);
238 g_free (media_id);
239 g_free (media_text);
240 }
241
242 /* TRANSLATORS: ask the user to insert the media */
243 ret = pk_console_get_prompt (_("Please insert the correct media"), FALSE);
244 if (ret) {
245 pk_task_user_accepted (task, request);
246 } else {
247 /* TRANSLATORS: tell the user we've not done anything as they are lazy */
248 g_print ("%s\n", _("The correct media was not inserted."));
249 pk_task_user_declined (task, request);
250 }
251
252 g_ptr_array_unref (array);
253 }
254
255 /*
256 * pk_task_text_simulate_question_type_to_string:
257 **/
258 static const gchar *
259 pk_task_text_simulate_question_type_to_string (PkInfoEnum info)
260 {
261 if (info == PK_INFO_ENUM_REMOVING) {
262 /* TRANSLATORS: When processing, we might have to remove other dependencies */
263 return _("The following packages have to be removed:");
264 }
265
266 if (info == PK_INFO_ENUM_INSTALLING) {
267 /* TRANSLATORS: When processing, we might have to install other dependencies */
268 return _("The following packages have to be installed:");
269 }
270
271 if (info == PK_INFO_ENUM_UPDATING) {
272 /* TRANSLATORS: When processing, we might have to update other dependencies */
273 return _("The following packages have to be updated:");
274 }
275
276 if (info == PK_INFO_ENUM_REINSTALLING) {
277 /* TRANSLATORS: When processing, we might have to reinstall other dependencies */
278 return _("The following packages have to be reinstalled:");
279 }
280
281 if (info == PK_INFO_ENUM_DOWNGRADING) {
282 /* TRANSLATORS: When processing, we might have to downgrade other dependencies */
283 return _("The following packages have to be downgraded:");
284 }
285
286 if (info == PK_INFO_ENUM_OBSOLETING) {
287 /* TRANSLATORS: When processing, we might have to obsolete other dependencies */
288 return _("The following packages have to be obsoleted:");
289 }
290
291 if (info == PK_INFO_ENUM_UNTRUSTED) {
292 /* TRANSLATORS: When processing, untrusted and non-verified packages may be encountered */
293 return _("The following packages are untrusted:");
294 }
295
296 /* do not show */
297 return NULL;
298 }
299
300 static gint
301 package_sort_func (gconstpointer a, gconstpointer b)
302 {
303 const gchar *package_id1;
304 const gchar *package_id2;
305 g_auto(GStrv) split1 = NULL;
306 g_auto(GStrv) split2 = NULL;
307
308 package_id1 = pk_package_get_id (*(PkPackage **)a);
309 package_id2 = pk_package_get_id (*(PkPackage **)b);
310 split1 = pk_package_id_split (package_id1);
311 split2 = pk_package_id_split (package_id2);
312 return g_strcmp0 (split1[PK_PACKAGE_ID_NAME], split2[PK_PACKAGE_ID_NAME]);
313 }
314
315 /**
316 * pk_task_text_simulate_question:
317 */
318 static void
319 pk_task_text_simulate_question (PkTask *task, guint request, PkResults *results)
320 {
321 guint max_width = 0;
322 gboolean ret;
323 PkPackage *package;
324 g_autoptr(GPtrArray) array = NULL;
325 g_autoptr(GHashTable) table = NULL;
326 g_autoptr(GList) list = NULL;
327
328 /* clear new line */
329 g_print ("\n");
330
331 /* get data */
332 array = pk_results_get_package_array (results);
333
334 /* put data in a hash table for easier sorting */
335 table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
336 NULL, (GDestroyNotify) g_ptr_array_unref);
337 for (guint i = 0; i < array->len; i++) {
338 PkInfoEnum info;
339 g_autoptr(GPtrArray) package_array = NULL;
340
341 package = g_ptr_array_index (array, i);
342 g_object_get (package,
343 "info", &info,
344 NULL);
345
346 if (g_hash_table_contains (table, GINT_TO_POINTER (info)))
347 package_array = g_ptr_array_ref (g_hash_table_lookup (table, GINT_TO_POINTER (info)));
348 else
349 package_array = g_ptr_array_new_with_free_func (g_object_unref);
350 g_ptr_array_add (package_array, g_object_ref (package));
351 g_hash_table_insert (table,
352 GINT_TO_POINTER (info),
353 g_ptr_array_ref (package_array));
354 }
355
356 /* calculate the maximum package name width for proper alignment */
357 max_width = 0;
358 list = g_hash_table_get_keys (table);
359 for (GList *l = list; l != NULL; l = l->next) {
360 GPtrArray *package_array;
361
362 package_array = g_hash_table_lookup (table, l->data);
363 for (guint i = 0; i < package_array->len; i++) {
364 guint width;
365 g_autofree gchar *package_id = NULL;
366 g_autofree gchar *printable = NULL;
367
368 package = g_ptr_array_index (package_array, i);
369 g_object_get (package, "package-id", &package_id, NULL);
370 /* NOTE: We could put this into an array to not invoke pk_package_id_to_printable()
371 * multiple times and save allocations, but this is simpler code.
372 * If this becomes a problem in future, PkPackage should get a function to
373 * generate & cache a printable name, */
374 printable = pk_package_id_to_printable (package_id);
375
376 width = pk_console_str_width (printable);
377 if (width > max_width)
378 max_width = width;
379 }
380 }
381
382 /* add a minimum spacing of 2 characters between columns */
383 max_width += 2;
384
385 /* go over the data we now have in a hash table and print */
386 for (GList *l = list; l != NULL; l = l->next) {
387 PkInfoEnum info;
388 GPtrArray *package_array;
389 const gchar *title;
390
391 /* new header */
392 info = GPOINTER_TO_INT (l->data);
393 title = pk_task_text_simulate_question_type_to_string (info);
394 if (title == NULL) {
395 title = pk_info_enum_to_string (info);
396 g_warning ("cannot translate '%s', please report!", title);
397 }
398 g_print ("%s\n", title);
399
400 /* sort the packages and print */
401 package_array = g_hash_table_lookup (table, l->data);
402 g_ptr_array_sort (package_array, package_sort_func);
403 for (guint i = 0; i < package_array->len; i++) {
404 g_autofree gchar *package_id = NULL;
405 g_autofree gchar *summary = NULL;
406 g_autofree gchar *printable = NULL;
407 g_autofree gchar *printable_pad = NULL;
408
409 package = g_ptr_array_index (package_array, i);
410 g_object_get (package,
411 "package-id", &package_id,
412 "summary", &summary,
413 NULL);
414 printable = pk_package_id_to_printable (package_id);
415
416 /* pad the package name to align descriptions */
417 printable_pad = pk_console_strpad (printable, max_width);
418 g_print (" %s%s\n", printable_pad, summary);
419 }
420 }
421
422 /* TRANSLATORS: ask the user if the proposed changes are okay */
423 ret = pk_console_get_prompt (_("Proceed with changes?"), FALSE);
424 if (ret) {
425 pk_task_user_accepted (task, request);
426 } else {
427 /* TRANSLATORS: tell the user we didn't do anything */
428 g_print ("%s\n", _("The transaction did not proceed."));
429 pk_task_user_declined (task, request);
430 }
431 }
432
433 /*
434 * pk_task_text_class_init:
435 **/
436 static void
437 pk_task_text_class_init (PkTaskTextClass *klass)
438 {
439 PkTaskClass *task_class = PK_TASK_CLASS (klass);
440
441 task_class->untrusted_question = pk_task_text_untrusted_question;
442 task_class->key_question = pk_task_text_key_question;
443 task_class->eula_question = pk_task_text_eula_question;
444 task_class->media_change_question = pk_task_text_media_change_question;
445 task_class->simulate_question = pk_task_text_simulate_question;
446 }
447
448 /*
449 * pk_task_text_init:
450 * @task_text: This class instance
451 **/
452 static void
453 pk_task_text_init (PkTaskText *task)
454 {
455 }
456
457 /**
458 * pk_task_text_new:
459 *
460 * Return value: a new #PkTaskText object.
461 **/
462 PkTaskText *
463 pk_task_text_new (void)
464 {
465 PkTaskText *task;
466 task = g_object_new (PK_TYPE_TASK_TEXT, NULL);
467 return PK_TASK_TEXT (task);
468 }