~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/printers/pp-new-printer.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright 2012  Red Hat, Inc,
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Author: Marek Kasik <mkasik@redhat.com>
 
20
 */
 
21
 
 
22
#include "pp-new-printer.h"
 
23
 
 
24
#include <glib/gstdio.h>
 
25
#include <glib/gi18n.h>
 
26
 
 
27
#include "pp-utils.h"
 
28
#include "pp-maintenance-command.h"
 
29
 
 
30
#define PACKAGE_KIT_BUS "org.freedesktop.PackageKit"
 
31
#define PACKAGE_KIT_PATH "/org/freedesktop/PackageKit"
 
32
#define PACKAGE_KIT_MODIFY_IFACE "org.freedesktop.PackageKit.Modify"
 
33
#define PACKAGE_KIT_QUERY_IFACE  "org.freedesktop.PackageKit.Query"
 
34
 
 
35
#define DBUS_TIMEOUT      120000
 
36
#define DBUS_TIMEOUT_LONG 600000
 
37
 
 
38
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
 
39
#define HAVE_CUPS_1_6 1
 
40
#endif
 
41
 
 
42
#ifndef HAVE_CUPS_1_6
 
43
#define ippGetState(ipp)      ipp->state
 
44
#endif
 
45
 
 
46
struct _PpNewPrinterPrivate
 
47
{
 
48
  gchar    *name;
 
49
  gchar    *original_name;
 
50
  gchar    *device_uri;
 
51
  gchar    *device_id;
 
52
  gchar    *ppd_name;
 
53
  gchar    *ppd_file_name;
 
54
  gchar    *info;
 
55
  gchar    *location;
 
56
  gchar    *make_and_model;
 
57
  gchar    *host_name;
 
58
  gint      host_port;
 
59
  gboolean  is_network_device;
 
60
  guint     window_id;
 
61
  gboolean  unlink_ppd_file;
 
62
 
 
63
  GSimpleAsyncResult *res;
 
64
  GCancellable *cancellable;
 
65
};
 
66
 
 
67
G_DEFINE_TYPE (PpNewPrinter, pp_new_printer, G_TYPE_OBJECT);
 
68
 
 
69
enum {
 
70
  PROP_0 = 0,
 
71
  PROP_NAME,
 
72
  PROP_ORIGINAL_NAME,
 
73
  PROP_DEVICE_URI,
 
74
  PROP_DEVICE_ID,
 
75
  PROP_PPD_NAME,
 
76
  PROP_PPD_FILE_NAME,
 
77
  PROP_INFO,
 
78
  PROP_LOCATION,
 
79
  PROP_MAKE_AND_MODEL,
 
80
  PROP_HOST_NAME,
 
81
  PROP_HOST_PORT,
 
82
  PROP_IS_NETWORK_DEVICE,
 
83
  PROP_WINDOW_ID
 
84
};
 
85
 
 
86
static void
 
87
pp_new_printer_finalize (GObject *object)
 
88
{
 
89
  PpNewPrinterPrivate *priv;
 
90
 
 
91
  priv = PP_NEW_PRINTER (object)->priv;
 
92
 
 
93
  if (priv->unlink_ppd_file && priv->ppd_file_name)
 
94
    g_unlink (priv->ppd_file_name);
 
95
 
 
96
  g_clear_pointer (&priv->name, g_free);
 
97
  g_clear_pointer (&priv->original_name, g_free);
 
98
  g_clear_pointer (&priv->device_uri, g_free);
 
99
  g_clear_pointer (&priv->device_id, g_free);
 
100
  g_clear_pointer (&priv->ppd_name, g_free);
 
101
  g_clear_pointer (&priv->ppd_file_name, g_free);
 
102
  g_clear_pointer (&priv->info, g_free);
 
103
  g_clear_pointer (&priv->location, g_free);
 
104
  g_clear_pointer (&priv->make_and_model, g_free);
 
105
  g_clear_pointer (&priv->host_name, g_free);
 
106
 
 
107
  if (priv->res)
 
108
    g_object_unref (priv->res);
 
109
 
 
110
  if (priv->cancellable)
 
111
    g_object_unref (priv->cancellable);
 
112
 
 
113
  G_OBJECT_CLASS (pp_new_printer_parent_class)->finalize (object);
 
114
}
 
115
 
 
116
static void
 
117
pp_new_printer_get_property (GObject    *object,
 
118
                             guint       prop_id,
 
119
                             GValue     *value,
 
120
                             GParamSpec *param_spec)
 
121
{
 
122
  PpNewPrinter *self;
 
123
 
 
124
  self = PP_NEW_PRINTER (object);
 
125
 
 
126
  switch (prop_id)
 
127
    {
 
128
      case PROP_NAME:
 
129
        g_value_set_string (value, self->priv->name);
 
130
        break;
 
131
      case PROP_ORIGINAL_NAME:
 
132
        g_value_set_string (value, self->priv->original_name);
 
133
        break;
 
134
      case PROP_DEVICE_URI:
 
135
        g_value_set_string (value, self->priv->device_uri);
 
136
        break;
 
137
      case PROP_DEVICE_ID:
 
138
        g_value_set_string (value, self->priv->device_id);
 
139
        break;
 
140
      case PROP_PPD_NAME:
 
141
        g_value_set_string (value, self->priv->ppd_name);
 
142
        break;
 
143
      case PROP_PPD_FILE_NAME:
 
144
        g_value_set_string (value, self->priv->ppd_file_name);
 
145
        break;
 
146
      case PROP_INFO:
 
147
        g_value_set_string (value, self->priv->info);
 
148
        break;
 
149
      case PROP_LOCATION:
 
150
        g_value_set_string (value, self->priv->location);
 
151
        break;
 
152
      case PROP_MAKE_AND_MODEL:
 
153
        g_value_set_string (value, self->priv->make_and_model);
 
154
        break;
 
155
      case PROP_HOST_NAME:
 
156
        g_value_set_string (value, self->priv->host_name);
 
157
        break;
 
158
      case PROP_HOST_PORT:
 
159
        g_value_set_int (value, self->priv->host_port);
 
160
        break;
 
161
      case PROP_IS_NETWORK_DEVICE:
 
162
        g_value_set_boolean (value, self->priv->is_network_device);
 
163
        break;
 
164
      case PROP_WINDOW_ID:
 
165
        g_value_set_int (value, self->priv->window_id);
 
166
        break;
 
167
      default:
 
168
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
 
169
                                           prop_id,
 
170
                                           param_spec);
 
171
      break;
 
172
    }
 
173
}
 
174
 
 
175
static void
 
176
pp_new_printer_set_property (GObject      *object,
 
177
                             guint         prop_id,
 
178
                             const GValue *value,
 
179
                             GParamSpec   *param_spec)
 
180
{
 
181
  PpNewPrinter *self = PP_NEW_PRINTER (object);
 
182
 
 
183
  switch (prop_id)
 
184
    {
 
185
      case PROP_NAME:
 
186
        g_free (self->priv->name);
 
187
        self->priv->name = g_value_dup_string (value);
 
188
        break;
 
189
      case PROP_ORIGINAL_NAME:
 
190
        g_free (self->priv->original_name);
 
191
        self->priv->original_name = g_value_dup_string (value);
 
192
        break;
 
193
      case PROP_DEVICE_URI:
 
194
        g_free (self->priv->device_uri);
 
195
        self->priv->device_uri = g_value_dup_string (value);
 
196
        break;
 
197
      case PROP_DEVICE_ID:
 
198
        g_free (self->priv->device_id);
 
199
        self->priv->device_id = g_value_dup_string (value);
 
200
        break;
 
201
      case PROP_PPD_NAME:
 
202
        g_free (self->priv->ppd_name);
 
203
        self->priv->ppd_name = g_value_dup_string (value);
 
204
        break;
 
205
      case PROP_PPD_FILE_NAME:
 
206
        g_free (self->priv->ppd_file_name);
 
207
        self->priv->ppd_file_name = g_value_dup_string (value);
 
208
        break;
 
209
      case PROP_INFO:
 
210
        g_free (self->priv->info);
 
211
        self->priv->info = g_value_dup_string (value);
 
212
        break;
 
213
      case PROP_LOCATION:
 
214
        g_free (self->priv->location);
 
215
        self->priv->location = g_value_dup_string (value);
 
216
        break;
 
217
      case PROP_MAKE_AND_MODEL:
 
218
        g_free (self->priv->make_and_model);
 
219
        self->priv->make_and_model = g_value_dup_string (value);
 
220
        break;
 
221
      case PROP_HOST_NAME:
 
222
        g_free (self->priv->host_name);
 
223
        self->priv->host_name = g_value_dup_string (value);
 
224
        break;
 
225
      case PROP_HOST_PORT:
 
226
        self->priv->host_port = g_value_get_int (value);
 
227
        break;
 
228
      case PROP_IS_NETWORK_DEVICE:
 
229
        self->priv->is_network_device = g_value_get_boolean (value);
 
230
        break;
 
231
      case PROP_WINDOW_ID:
 
232
        self->priv->window_id = g_value_get_int (value);
 
233
        break;
 
234
      default:
 
235
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
 
236
                                           prop_id,
 
237
                                           param_spec);
 
238
        break;
 
239
    }
 
240
}
 
241
 
 
242
static void
 
243
pp_new_printer_class_init (PpNewPrinterClass *klass)
 
244
{
 
245
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
246
 
 
247
  g_type_class_add_private (klass, sizeof (PpNewPrinterPrivate));
 
248
 
 
249
  gobject_class->set_property = pp_new_printer_set_property;
 
250
  gobject_class->get_property = pp_new_printer_get_property;
 
251
 
 
252
  gobject_class->finalize = pp_new_printer_finalize;
 
253
 
 
254
  g_object_class_install_property (gobject_class, PROP_NAME,
 
255
    g_param_spec_string ("name",
 
256
                         "Name",
 
257
                         "The new printer's name",
 
258
                         NULL,
 
259
                         G_PARAM_READWRITE));
 
260
 
 
261
  g_object_class_install_property (gobject_class, PROP_ORIGINAL_NAME,
 
262
    g_param_spec_string ("original-name",
 
263
                         "Original name",
 
264
                         "Original name of the new printer",
 
265
                         NULL,
 
266
                         G_PARAM_READWRITE));
 
267
 
 
268
  g_object_class_install_property (gobject_class, PROP_DEVICE_URI,
 
269
    g_param_spec_string ("device-uri",
 
270
                         "Device URI",
 
271
                         "The new printer's device URI",
 
272
                         NULL,
 
273
                         G_PARAM_READWRITE));
 
274
 
 
275
  g_object_class_install_property (gobject_class, PROP_DEVICE_ID,
 
276
    g_param_spec_string ("device-id",
 
277
                         "DeviceID",
 
278
                         "The new printer's DeviceID",
 
279
                         NULL,
 
280
                         G_PARAM_READWRITE));
 
281
 
 
282
  g_object_class_install_property (gobject_class, PROP_PPD_NAME,
 
283
    g_param_spec_string ("ppd-name",
 
284
                         "PPD name",
 
285
                         "Name of PPD for the new printer",
 
286
                         NULL,
 
287
                         G_PARAM_READWRITE));
 
288
 
 
289
  g_object_class_install_property (gobject_class, PROP_PPD_FILE_NAME,
 
290
    g_param_spec_string ("ppd-file-name",
 
291
                         "PPD file name",
 
292
                         "PPD file for the new printer",
 
293
                         NULL,
 
294
                         G_PARAM_READWRITE));
 
295
 
 
296
  g_object_class_install_property (gobject_class, PROP_INFO,
 
297
    g_param_spec_string ("info",
 
298
                         "Printer info",
 
299
                         "The new printer's info",
 
300
                         NULL,
 
301
                         G_PARAM_READWRITE));
 
302
 
 
303
  g_object_class_install_property (gobject_class, PROP_LOCATION,
 
304
    g_param_spec_string ("location",
 
305
                         "Printer location",
 
306
                         "The new printer's location",
 
307
                         NULL,
 
308
                         G_PARAM_READWRITE));
 
309
 
 
310
  g_object_class_install_property (gobject_class, PROP_MAKE_AND_MODEL,
 
311
    g_param_spec_string ("make-and-model",
 
312
                         "Printer make and model",
 
313
                         "The new printer's make and model",
 
314
                         NULL,
 
315
                         G_PARAM_READWRITE));
 
316
 
 
317
  g_object_class_install_property (gobject_class, PROP_HOST_NAME,
 
318
    g_param_spec_string ("host-name",
 
319
                         "Hostname",
 
320
                         "The new printer's hostname",
 
321
                         NULL,
 
322
                         G_PARAM_READWRITE));
 
323
 
 
324
  g_object_class_install_property (gobject_class, PROP_HOST_PORT,
 
325
    g_param_spec_int ("host-port",
 
326
                      "Host port",
 
327
                      "The port of the host",
 
328
                      0, G_MAXINT32, 631,
 
329
                      G_PARAM_READWRITE));
 
330
 
 
331
  g_object_class_install_property (gobject_class, PROP_IS_NETWORK_DEVICE,
 
332
    g_param_spec_boolean ("is-network-device",
 
333
                          "Network device",
 
334
                          "Whether the new printer is a network device",
 
335
                          FALSE,
 
336
                          G_PARAM_READWRITE));
 
337
 
 
338
  g_object_class_install_property (gobject_class, PROP_WINDOW_ID,
 
339
    g_param_spec_int ("window-id",
 
340
                      "WindowID",
 
341
                      "Window ID of parent window",
 
342
                      0, G_MAXINT32, 631,
 
343
                      G_PARAM_READWRITE));
 
344
}
 
345
 
 
346
static void
 
347
pp_new_printer_init (PpNewPrinter *printer)
 
348
{
 
349
  printer->priv = G_TYPE_INSTANCE_GET_PRIVATE (printer,
 
350
                                               PP_TYPE_NEW_PRINTER,
 
351
                                               PpNewPrinterPrivate);
 
352
 
 
353
  printer->priv->unlink_ppd_file = FALSE;
 
354
  printer->priv->cancellable = NULL;
 
355
  printer->priv->res = NULL;
 
356
}
 
357
 
 
358
PpNewPrinter *
 
359
pp_new_printer_new ()
 
360
{
 
361
  return g_object_new (PP_TYPE_NEW_PRINTER, NULL);
 
362
}
 
363
 
 
364
static void printer_configure_async (PpNewPrinter *new_printer);
 
365
 
 
366
static void
 
367
_pp_new_printer_add_async_cb (gboolean      success,
 
368
                              PpNewPrinter *printer)
 
369
{
 
370
  PpNewPrinterPrivate *priv = printer->priv;
 
371
 
 
372
  if (!success)
 
373
    {
 
374
      g_simple_async_result_set_error (priv->res,
 
375
                                       G_IO_ERROR,
 
376
                                       G_IO_ERROR_FAILED,
 
377
                                       "Installation of the new printer failed.");
 
378
    }
 
379
 
 
380
  g_simple_async_result_set_op_res_gboolean (priv->res, success);
 
381
  g_simple_async_result_complete_in_idle (priv->res);
 
382
}
 
383
 
 
384
static void
 
385
printer_add_real_async_cb (cups_dest_t *destination,
 
386
                           gpointer     user_data)
 
387
{
 
388
  PpNewPrinter        *printer = (PpNewPrinter *) user_data;
 
389
  gboolean             success = FALSE;
 
390
 
 
391
  if (destination)
 
392
    {
 
393
      success = TRUE;
 
394
      cupsFreeDests (1, destination);
 
395
    }
 
396
 
 
397
  if (success)
 
398
    {
 
399
      printer_configure_async (printer);
 
400
    }
 
401
  else
 
402
    {
 
403
      _pp_new_printer_add_async_cb (FALSE, printer);
 
404
    }
 
405
}
 
406
 
 
407
static void
 
408
printer_add_real_async_dbus_cb (GObject      *source_object,
 
409
                                GAsyncResult *res,
 
410
                                gpointer      user_data)
 
411
{
 
412
  PpNewPrinter        *printer = (PpNewPrinter *) user_data;
 
413
  PpNewPrinterPrivate *priv = printer->priv;
 
414
  GVariant            *output;
 
415
  GError              *error = NULL;
 
416
 
 
417
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
418
                                          res,
 
419
                                          &error);
 
420
  g_object_unref (source_object);
 
421
 
 
422
  if (output)
 
423
    {
 
424
      const gchar *ret_error;
 
425
 
 
426
      g_variant_get (output, "(&s)", &ret_error);
 
427
      if (ret_error[0] != '\0')
 
428
        {
 
429
          g_warning ("cups-pk-helper: addition of printer %s failed: %s", priv->name, ret_error);
 
430
        }
 
431
 
 
432
      g_variant_unref (output);
 
433
    }
 
434
  else
 
435
    {
 
436
      if (error->domain != G_IO_ERROR ||
 
437
          error->code != G_IO_ERROR_CANCELLED)
 
438
        g_warning ("%s", error->message);
 
439
    }
 
440
 
 
441
  if (!error ||
 
442
      error->domain != G_IO_ERROR ||
 
443
      error->code != G_IO_ERROR_CANCELLED)
 
444
    {
 
445
      get_named_dest_async (priv->name,
 
446
                            printer_add_real_async_cb,
 
447
                            printer);
 
448
    }
 
449
 
 
450
  if (error)
 
451
      g_error_free (error);
 
452
}
 
453
 
 
454
static void
 
455
printer_add_real_async (PpNewPrinter *printer)
 
456
{
 
457
  PpNewPrinterPrivate *priv = printer->priv;
 
458
  GDBusConnection     *bus;
 
459
  GError              *error = NULL;
 
460
 
 
461
  if (!priv->ppd_name && !priv->ppd_file_name)
 
462
    {
 
463
      _pp_new_printer_add_async_cb (FALSE, printer);
 
464
      return;
 
465
    }
 
466
 
 
467
  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
 
468
  if (!bus)
 
469
    {
 
470
      g_warning ("Failed to get system bus: %s", error->message);
 
471
      g_error_free (error);
 
472
      _pp_new_printer_add_async_cb (FALSE, printer);
 
473
      return;
 
474
    }
 
475
 
 
476
  g_dbus_connection_call (bus,
 
477
                          MECHANISM_BUS,
 
478
                          "/",
 
479
                          MECHANISM_BUS,
 
480
                          priv->ppd_name ? "PrinterAdd" : "PrinterAddWithPpdFile",
 
481
                          g_variant_new ("(sssss)",
 
482
                                         priv->name,
 
483
                                         priv->device_uri,
 
484
                                         priv->ppd_name ? priv->ppd_name : priv->ppd_file_name,
 
485
                                         priv->info ? priv->info : "",
 
486
                                         priv->location ? priv->location : ""),
 
487
                          G_VARIANT_TYPE ("(s)"),
 
488
                          G_DBUS_CALL_FLAGS_NONE,
 
489
                          DBUS_TIMEOUT,
 
490
                          NULL,
 
491
                          printer_add_real_async_dbus_cb,
 
492
                          printer);
 
493
}
 
494
 
 
495
static PPDName *
 
496
get_ppd_item_from_output (GVariant *output)
 
497
{
 
498
  GVariant *array;
 
499
  PPDName  *ppd_item = NULL;
 
500
  gint      j;
 
501
  static const char * const match_levels[] = {
 
502
             "exact-cmd",
 
503
             "exact",
 
504
             "close",
 
505
             "generic",
 
506
             "none"};
 
507
 
 
508
  if (output)
 
509
    {
 
510
      g_variant_get (output, "(@a(ss))", &array);
 
511
      if (array)
 
512
        {
 
513
          GVariantIter *iter;
 
514
          GVariant     *item;
 
515
          gchar        *driver;
 
516
          gchar        *match;
 
517
 
 
518
          for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++)
 
519
            {
 
520
              g_variant_get (array, "a(ss)", &iter);
 
521
              while ((item = g_variant_iter_next_value (iter)) && !ppd_item)
 
522
                {
 
523
                  g_variant_get (item, "(ss)", &driver, &match);
 
524
                  if (g_str_equal (match, match_levels[j]))
 
525
                    {
 
526
                      ppd_item = g_new0 (PPDName, 1);
 
527
                      ppd_item->ppd_name = g_strdup (driver);
 
528
 
 
529
                      if (g_strcmp0 (match, "exact-cmd") == 0)
 
530
                        ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH;
 
531
                      else if (g_strcmp0 (match, "exact") == 0)
 
532
                        ppd_item->ppd_match_level = PPD_EXACT_MATCH;
 
533
                      else if (g_strcmp0 (match, "close") == 0)
 
534
                        ppd_item->ppd_match_level = PPD_CLOSE_MATCH;
 
535
                      else if (g_strcmp0 (match, "generic") == 0)
 
536
                        ppd_item->ppd_match_level = PPD_GENERIC_MATCH;
 
537
                      else if (g_strcmp0 (match, "none") == 0)
 
538
                        ppd_item->ppd_match_level = PPD_NO_MATCH;
 
539
                    }
 
540
 
 
541
                  g_free (driver);
 
542
                  g_free (match);
 
543
                  g_variant_unref (item);
 
544
                }
 
545
            }
 
546
 
 
547
          g_variant_unref (array);
 
548
        }
 
549
    }
 
550
 
 
551
  return ppd_item;
 
552
}
 
553
 
 
554
 
 
555
static void
 
556
printer_add_async_scb3 (GObject      *source_object,
 
557
                        GAsyncResult *res,
 
558
                        gpointer      user_data)
 
559
{
 
560
  PpNewPrinter        *printer = (PpNewPrinter *) user_data;
 
561
  PpNewPrinterPrivate *priv = printer->priv;
 
562
  GVariant            *output;
 
563
  PPDName             *ppd_item = NULL;
 
564
  GError              *error = NULL;
 
565
 
 
566
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
567
                                          res,
 
568
                                          &error);
 
569
  g_object_unref (source_object);
 
570
 
 
571
  if (output)
 
572
    {
 
573
      ppd_item = get_ppd_item_from_output (output);
 
574
      g_variant_unref (output);
 
575
    }
 
576
  else
 
577
    {
 
578
      if (error->domain != G_IO_ERROR ||
 
579
          error->code != G_IO_ERROR_CANCELLED)
 
580
        g_warning ("%s", error->message);
 
581
    }
 
582
 
 
583
  if ((!error ||
 
584
      error->domain != G_IO_ERROR ||
 
585
      error->code != G_IO_ERROR_CANCELLED) &&
 
586
      ppd_item && ppd_item->ppd_name)
 
587
    {
 
588
      priv->ppd_name = g_strdup (ppd_item->ppd_name);
 
589
      printer_add_real_async (printer);
 
590
    }
 
591
  else
 
592
    {
 
593
      _pp_new_printer_add_async_cb (FALSE, printer);
 
594
    }
 
595
 
 
596
  if (error)
 
597
    {
 
598
      g_error_free (error);
 
599
    }
 
600
 
 
601
  if (ppd_item)
 
602
    {
 
603
      g_free (ppd_item->ppd_name);
 
604
      g_free (ppd_item);
 
605
    }
 
606
}
 
607
 
 
608
static void
 
609
install_printer_drivers_cb (GObject      *source_object,
 
610
                            GAsyncResult *res,
 
611
                            gpointer      user_data)
 
612
{
 
613
  PpNewPrinterPrivate *priv;
 
614
  PpNewPrinter        *printer;
 
615
  GVariant            *output;
 
616
  GError              *error = NULL;
 
617
 
 
618
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
619
                                          res,
 
620
                                          &error);
 
621
  g_object_unref (source_object);
 
622
 
 
623
  if (output)
 
624
    {
 
625
      g_variant_unref (output);
 
626
    }
 
627
  else
 
628
    {
 
629
      if (error->domain != G_IO_ERROR ||
 
630
          error->code != G_IO_ERROR_CANCELLED)
 
631
        g_warning ("%s", error->message);
 
632
    }
 
633
 
 
634
  if (!error ||
 
635
      error->domain != G_IO_ERROR ||
 
636
      error->code != G_IO_ERROR_CANCELLED)
 
637
    {
 
638
      GDBusConnection *bus;
 
639
      GError          *error = NULL;
 
640
 
 
641
      printer = (PpNewPrinter *) user_data;
 
642
      priv = printer->priv;
 
643
 
 
644
      /* Try whether CUPS has a driver for the new printer */
 
645
      bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
646
      if (bus)
 
647
        {
 
648
          g_dbus_connection_call (bus,
 
649
                                  SCP_BUS,
 
650
                                  SCP_PATH,
 
651
                                  SCP_IFACE,
 
652
                                  "GetBestDrivers",
 
653
                                  g_variant_new ("(sss)",
 
654
                                                 priv->device_id,
 
655
                                                 priv->make_and_model ? priv->make_and_model : "",
 
656
                                                 priv->device_uri ? priv->device_uri : ""),
 
657
                                  G_VARIANT_TYPE ("(a(ss))"),
 
658
                                  G_DBUS_CALL_FLAGS_NONE,
 
659
                                  DBUS_TIMEOUT_LONG,
 
660
                                  priv->cancellable,
 
661
                                  printer_add_async_scb3,
 
662
                                  printer);
 
663
        }
 
664
      else
 
665
        {
 
666
          g_warning ("Failed to get system bus: %s", error->message);
 
667
          g_error_free (error);
 
668
          _pp_new_printer_add_async_cb (FALSE, printer);
 
669
        }
 
670
    }
 
671
 
 
672
  if (error)
 
673
    g_error_free (error);
 
674
}
 
675
 
 
676
static void
 
677
printer_add_async_scb (GObject      *source_object,
 
678
                       GAsyncResult *res,
 
679
                       gpointer      user_data)
 
680
{
 
681
  PpNewPrinter        *printer = (PpNewPrinter *) user_data;
 
682
  PpNewPrinterPrivate *priv = printer->priv;
 
683
  GDBusConnection     *bus;
 
684
  GVariantBuilder      array_builder;
 
685
  GVariant            *output;
 
686
  gboolean             cancelled = FALSE;
 
687
  PPDName             *ppd_item = NULL;
 
688
  GError              *error = NULL;
 
689
 
 
690
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
691
                                          res,
 
692
                                          &error);
 
693
  g_object_unref (source_object);
 
694
 
 
695
  if (output)
 
696
    {
 
697
      ppd_item = get_ppd_item_from_output (output);
 
698
      g_variant_unref (output);
 
699
    }
 
700
  else
 
701
    {
 
702
      cancelled = g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
 
703
 
 
704
      if (!cancelled)
 
705
        g_warning ("%s", error->message);
 
706
 
 
707
      g_clear_error (&error);
 
708
    }
 
709
 
 
710
  if (!cancelled)
 
711
    {
 
712
      if (ppd_item == NULL || ppd_item->ppd_match_level < PPD_EXACT_MATCH)
 
713
        {
 
714
          bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
715
          if (bus)
 
716
            {
 
717
              g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("as"));
 
718
              g_variant_builder_add (&array_builder, "s", priv->device_id);
 
719
 
 
720
              g_dbus_connection_call (bus,
 
721
                                      PACKAGE_KIT_BUS,
 
722
                                      PACKAGE_KIT_PATH,
 
723
                                      PACKAGE_KIT_MODIFY_IFACE,
 
724
                                      "InstallPrinterDrivers",
 
725
                                      g_variant_new ("(uass)",
 
726
                                                     priv->window_id,
 
727
                                                     &array_builder,
 
728
                                                     "hide-finished"),
 
729
                                      G_VARIANT_TYPE ("()"),
 
730
                                      G_DBUS_CALL_FLAGS_NONE,
 
731
                                      DBUS_TIMEOUT_LONG,
 
732
                                      NULL,
 
733
                                      install_printer_drivers_cb,
 
734
                                      printer);
 
735
            }
 
736
          else
 
737
            {
 
738
              g_warning ("Failed to get session bus: %s", error->message);
 
739
              g_error_free (error);
 
740
              _pp_new_printer_add_async_cb (FALSE, printer);
 
741
            }
 
742
        }
 
743
      else if (ppd_item && ppd_item->ppd_name)
 
744
        {
 
745
          priv->ppd_name = g_strdup (ppd_item->ppd_name);
 
746
          printer_add_real_async (printer);
 
747
        }
 
748
      else
 
749
        {
 
750
          _pp_new_printer_add_async_cb (FALSE, printer);
 
751
        }
 
752
    }
 
753
 
 
754
  if (ppd_item)
 
755
    {
 
756
      g_free (ppd_item->ppd_name);
 
757
      g_free (ppd_item);
 
758
    }
 
759
}
 
760
 
 
761
static void
 
762
printer_add_async_scb4 (const gchar *ppd_filename,
 
763
                        gpointer     user_data)
 
764
{
 
765
  PpNewPrinter        *printer = (PpNewPrinter *) user_data;
 
766
  PpNewPrinterPrivate *priv = printer->priv;
 
767
 
 
768
  priv->ppd_file_name = g_strdup (ppd_filename);
 
769
  if (priv->ppd_file_name)
 
770
    {
 
771
      priv->unlink_ppd_file = TRUE;
 
772
      printer_add_real_async (printer);
 
773
    }
 
774
  else
 
775
    {
 
776
      _pp_new_printer_add_async_cb (FALSE, printer);
 
777
    }
 
778
}
 
779
 
 
780
static GList *
 
781
glist_uniq (GList *list)
 
782
{
 
783
  GList *result = NULL;
 
784
  GList *iter = NULL;
 
785
  GList *tmp = NULL;
 
786
 
 
787
  for (iter = list; iter; iter = iter->next)
 
788
    {
 
789
      if (tmp == NULL ||
 
790
          g_strcmp0 ((gchar *) tmp->data, (gchar *) iter->data) != 0)
 
791
        {
 
792
          tmp = iter;
 
793
          result = g_list_append (result, g_strdup (iter->data));
 
794
        }
 
795
    }
 
796
 
 
797
  g_list_free_full (list, g_free);
 
798
 
 
799
  return result;
 
800
}
 
801
 
 
802
typedef struct
 
803
{
 
804
  PpNewPrinter *new_printer;
 
805
  GCancellable *cancellable;
 
806
  gboolean      set_accept_jobs_finished;
 
807
  gboolean      set_enabled_finished;
 
808
  gboolean      autoconfigure_finished;
 
809
  gboolean      set_media_size_finished;
 
810
  gboolean      install_missing_executables_finished;
 
811
} PCData;
 
812
 
 
813
static void
 
814
printer_configure_async_finish (PCData *data)
 
815
{
 
816
  PpNewPrinterPrivate *priv = data->new_printer->priv;
 
817
 
 
818
  if (data->set_accept_jobs_finished &&
 
819
      data->set_enabled_finished &&
 
820
      (data->autoconfigure_finished || priv->is_network_device) &&
 
821
      data->set_media_size_finished &&
 
822
      data->install_missing_executables_finished)
 
823
    {
 
824
      _pp_new_printer_add_async_cb (TRUE, data->new_printer);
 
825
 
 
826
      if (data->cancellable)
 
827
        g_object_unref (data->cancellable);
 
828
      g_free (data);
 
829
    }
 
830
}
 
831
 
 
832
static void
 
833
pao_cb (gboolean success,
 
834
        gpointer user_data)
 
835
{
 
836
  PCData *data = (PCData *) user_data;
 
837
 
 
838
  data->set_media_size_finished = TRUE;
 
839
  printer_configure_async_finish (data);
 
840
}
 
841
 
 
842
static void
 
843
printer_set_accepting_jobs_cb (GObject      *source_object,
 
844
                               GAsyncResult *res,
 
845
                               gpointer      user_data)
 
846
{
 
847
  GVariant *output;
 
848
  PCData   *data = (PCData *) user_data;
 
849
  GError   *error = NULL;
 
850
 
 
851
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
852
                                          res,
 
853
                                          &error);
 
854
  g_object_unref (source_object);
 
855
 
 
856
  if (output)
 
857
    {
 
858
      g_variant_unref (output);
 
859
    }
 
860
  else
 
861
    {
 
862
      if (error->domain != G_IO_ERROR ||
 
863
          error->code != G_IO_ERROR_CANCELLED)
 
864
        g_warning ("%s", error->message);
 
865
      g_error_free (error);
 
866
    }
 
867
 
 
868
  data->set_accept_jobs_finished = TRUE;
 
869
  printer_configure_async_finish (data);
 
870
}
 
871
 
 
872
static void
 
873
printer_set_enabled_cb (GObject      *source_object,
 
874
                        GAsyncResult *res,
 
875
                        gpointer      user_data)
 
876
{
 
877
  GVariant *output;
 
878
  PCData   *data = (PCData *) user_data;
 
879
  GError   *error = NULL;
 
880
 
 
881
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
882
                                          res,
 
883
                                          &error);
 
884
  g_object_unref (source_object);
 
885
 
 
886
  if (output)
 
887
    {
 
888
      g_variant_unref (output);
 
889
    }
 
890
  else
 
891
    {
 
892
      if (error->domain != G_IO_ERROR ||
 
893
          error->code != G_IO_ERROR_CANCELLED)
 
894
        g_warning ("%s", error->message);
 
895
      g_error_free (error);
 
896
    }
 
897
 
 
898
  data->set_enabled_finished = TRUE;
 
899
  printer_configure_async_finish (data);
 
900
}
 
901
 
 
902
typedef struct
 
903
{
 
904
  GList        *executables;
 
905
  GList        *packages;
 
906
  guint         window_id;
 
907
  gchar        *ppd_file_name;
 
908
  GCancellable *cancellable;
 
909
  gpointer      user_data;
 
910
} IMEData;
 
911
 
 
912
static void
 
913
install_missing_executables_cb (IMEData *data)
 
914
{
 
915
  PCData *pc_data = (PCData *) data->user_data;
 
916
 
 
917
  pc_data->install_missing_executables_finished = TRUE;
 
918
  printer_configure_async_finish (pc_data);
 
919
 
 
920
  if (data->ppd_file_name)
 
921
    {
 
922
      g_unlink (data->ppd_file_name);
 
923
      g_clear_pointer (&data->ppd_file_name, g_free);
 
924
    }
 
925
 
 
926
  if (data->executables)
 
927
    {
 
928
      g_list_free_full (data->executables, g_free);
 
929
      data->executables = NULL;
 
930
    }
 
931
 
 
932
  if (data->packages)
 
933
    {
 
934
      g_list_free_full (data->packages, g_free);
 
935
      data->packages = NULL;
 
936
    }
 
937
 
 
938
  if (data->cancellable)
 
939
    g_clear_object (&data->cancellable);
 
940
 
 
941
  g_free (data);
 
942
}
 
943
 
 
944
static void
 
945
install_package_names_cb (GObject      *source_object,
 
946
                          GAsyncResult *res,
 
947
                          gpointer      user_data)
 
948
{
 
949
  GVariant *output;
 
950
  IMEData  *data = (IMEData *) user_data;
 
951
  GError   *error = NULL;
 
952
 
 
953
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
954
                                          res,
 
955
                                          &error);
 
956
  g_object_unref (source_object);
 
957
 
 
958
  if (output)
 
959
    {
 
960
      g_variant_unref (output);
 
961
    }
 
962
  else
 
963
    {
 
964
      if (error->domain != G_IO_ERROR ||
 
965
          error->code != G_IO_ERROR_CANCELLED)
 
966
        g_warning ("%s", error->message);
 
967
      g_error_free (error);
 
968
    }
 
969
 
 
970
  install_missing_executables_cb (data);
 
971
}
 
972
 
 
973
 
 
974
static void
 
975
search_files_cb (GObject      *source_object,
 
976
                 GAsyncResult *res,
 
977
                 gpointer      user_data)
 
978
{
 
979
  GVariant *output;
 
980
  IMEData  *data = (IMEData *) user_data;
 
981
  GError   *error = NULL;
 
982
  GList    *item;
 
983
 
 
984
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
985
                                          res,
 
986
                                          &error);
 
987
  if (output)
 
988
    {
 
989
      gboolean  installed;
 
990
      gchar    *package;
 
991
 
 
992
      g_variant_get (output,
 
993
                     "(bs)",
 
994
                     &installed,
 
995
                     &package);
 
996
 
 
997
      if (!installed)
 
998
        data->packages = g_list_append (data->packages, g_strdup (package));
 
999
      g_variant_unref (output);
 
1000
    }
 
1001
  else
 
1002
    {
 
1003
      if (error->domain != G_IO_ERROR ||
 
1004
          error->code != G_IO_ERROR_CANCELLED)
 
1005
        g_warning ("%s", error->message);
 
1006
      g_error_free (error);
 
1007
    }
 
1008
 
 
1009
  if (data->executables)
 
1010
    {
 
1011
      item = data->executables;
 
1012
      g_dbus_connection_call (G_DBUS_CONNECTION (source_object),
 
1013
                              PACKAGE_KIT_BUS,
 
1014
                              PACKAGE_KIT_PATH,
 
1015
                              PACKAGE_KIT_QUERY_IFACE,
 
1016
                              "SearchFile",
 
1017
                              g_variant_new ("(ss)",
 
1018
                                             (gchar *) item->data,
 
1019
                                             ""),
 
1020
                              G_VARIANT_TYPE ("(bs)"),
 
1021
                              G_DBUS_CALL_FLAGS_NONE,
 
1022
                              DBUS_TIMEOUT_LONG,
 
1023
                              data->cancellable,
 
1024
                              search_files_cb,
 
1025
                              data);
 
1026
 
 
1027
      data->executables = g_list_remove_link (data->executables, item);
 
1028
      g_list_free_full (item, g_free);
 
1029
    }
 
1030
  else
 
1031
    {
 
1032
      GVariantBuilder  array_builder;
 
1033
      GList           *pkg_iter;
 
1034
 
 
1035
      data->packages = g_list_sort (data->packages, (GCompareFunc) g_strcmp0);
 
1036
      data->packages = glist_uniq (data->packages);
 
1037
 
 
1038
      if (data->packages)
 
1039
        {
 
1040
          g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("as"));
 
1041
 
 
1042
          for (pkg_iter = data->packages; pkg_iter; pkg_iter = pkg_iter->next)
 
1043
            g_variant_builder_add (&array_builder,
 
1044
                                   "s",
 
1045
                                   (gchar *) pkg_iter->data);
 
1046
 
 
1047
          g_dbus_connection_call (G_DBUS_CONNECTION (source_object),
 
1048
                                  PACKAGE_KIT_BUS,
 
1049
                                  PACKAGE_KIT_PATH,
 
1050
                                  PACKAGE_KIT_MODIFY_IFACE,
 
1051
                                  "InstallPackageNames",
 
1052
                                  g_variant_new ("(uass)",
 
1053
                                                 data->window_id,
 
1054
                                                 &array_builder,
 
1055
                                                 "hide-finished"),
 
1056
                                  NULL,
 
1057
                                  G_DBUS_CALL_FLAGS_NONE,
 
1058
                                  DBUS_TIMEOUT_LONG,
 
1059
                                  data->cancellable,
 
1060
                                  install_package_names_cb,
 
1061
                                  data);
 
1062
 
 
1063
          g_list_free_full (data->packages, g_free);
 
1064
          data->packages = NULL;
 
1065
        }
 
1066
      else
 
1067
        {
 
1068
          g_object_unref (source_object);
 
1069
          install_missing_executables_cb (data);
 
1070
        }
 
1071
    }
 
1072
}
 
1073
 
 
1074
static void
 
1075
get_missing_executables_cb (GObject      *source_object,
 
1076
                            GAsyncResult *res,
 
1077
                            gpointer      user_data)
 
1078
{
 
1079
  GVariant *output;
 
1080
  IMEData  *data = (IMEData *) user_data;
 
1081
  GError   *error = NULL;
 
1082
  GList    *executables = NULL;
 
1083
  GList    *item;
 
1084
 
 
1085
  if (data->ppd_file_name)
 
1086
    {
 
1087
      g_unlink (data->ppd_file_name);
 
1088
      g_clear_pointer (&data->ppd_file_name, g_free);
 
1089
    }
 
1090
 
 
1091
  output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
 
1092
                                          res,
 
1093
                                          &error);
 
1094
 
 
1095
  if (output)
 
1096
    {
 
1097
      GVariant *array;
 
1098
 
 
1099
      g_variant_get (output, "(@as)", &array);
 
1100
 
 
1101
      if (array)
 
1102
        {
 
1103
          GVariantIter *iter;
 
1104
          GVariant     *item;
 
1105
          gchar        *executable;
 
1106
 
 
1107
          g_variant_get (array, "as", &iter);
 
1108
          while ((item = g_variant_iter_next_value (iter)))
 
1109
            {
 
1110
              g_variant_get (item, "s", &executable);
 
1111
              executables = g_list_append (executables, executable);
 
1112
              g_variant_unref (item);
 
1113
            }
 
1114
 
 
1115
          g_variant_unref (array);
 
1116
        }
 
1117
 
 
1118
      g_variant_unref (output);
 
1119
    }
 
1120
  else if (error->domain == G_DBUS_ERROR &&
 
1121
           (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN ||
 
1122
            error->code == G_DBUS_ERROR_UNKNOWN_METHOD))
 
1123
    {
 
1124
      g_warning ("Install system-config-printer which provides \
 
1125
DBus method \"MissingExecutables\" to find missing executables and filters.");
 
1126
      g_error_free (error);
 
1127
    }
 
1128
  else
 
1129
    {
 
1130
      if (error->domain != G_IO_ERROR ||
 
1131
          error->code != G_IO_ERROR_CANCELLED)
 
1132
        g_warning ("%s", error->message);
 
1133
      g_error_free (error);
 
1134
    }
 
1135
 
 
1136
  executables = g_list_sort (executables, (GCompareFunc) g_strcmp0);
 
1137
  executables = glist_uniq (executables);
 
1138
 
 
1139
  if (executables)
 
1140
    {
 
1141
      data->executables = executables;
 
1142
 
 
1143
      item = data->executables;
 
1144
      g_dbus_connection_call (g_object_ref (source_object),
 
1145
                              PACKAGE_KIT_BUS,
 
1146
                              PACKAGE_KIT_PATH,
 
1147
                              PACKAGE_KIT_QUERY_IFACE,
 
1148
                              "SearchFile",
 
1149
                              g_variant_new ("(ss)",
 
1150
                                             (gchar *) item->data,
 
1151
                                             ""),
 
1152
                              G_VARIANT_TYPE ("(bs)"),
 
1153
                              G_DBUS_CALL_FLAGS_NONE,
 
1154
                              DBUS_TIMEOUT_LONG,
 
1155
                              data->cancellable,
 
1156
                              search_files_cb,
 
1157
                              data);
 
1158
 
 
1159
      data->executables = g_list_remove_link (data->executables, item);
 
1160
      g_list_free_full (item, g_free);
 
1161
    }
 
1162
  else
 
1163
    {
 
1164
      g_object_unref (source_object);
 
1165
      install_missing_executables_cb (data);
 
1166
    }
 
1167
}
 
1168
 
 
1169
static void
 
1170
printer_get_ppd_cb (const gchar *ppd_filename,
 
1171
                    gpointer     user_data)
 
1172
{
 
1173
  GDBusConnection *bus;
 
1174
  IMEData         *data = (IMEData *) user_data;
 
1175
  GError          *error = NULL;
 
1176
 
 
1177
  data->ppd_file_name = g_strdup (ppd_filename);
 
1178
  if (data->ppd_file_name)
 
1179
    {
 
1180
      bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
1181
      if (!bus)
 
1182
        {
 
1183
          g_warning ("%s", error->message);
 
1184
          g_error_free (error);
 
1185
        }
 
1186
      else
 
1187
        {
 
1188
          g_dbus_connection_call (bus,
 
1189
                                  SCP_BUS,
 
1190
                                  SCP_PATH,
 
1191
                                  SCP_IFACE,
 
1192
                                  "MissingExecutables",
 
1193
                                  g_variant_new ("(s)", data->ppd_file_name),
 
1194
                                  G_VARIANT_TYPE ("(as)"),
 
1195
                                  G_DBUS_CALL_FLAGS_NONE,
 
1196
                                  DBUS_TIMEOUT,
 
1197
                                  data->cancellable,
 
1198
                                  get_missing_executables_cb,
 
1199
                                  data);
 
1200
          return;
 
1201
        }
 
1202
    }
 
1203
 
 
1204
  install_missing_executables_cb (data);
 
1205
}
 
1206
 
 
1207
static void
 
1208
pp_maintenance_command_execute_cb (GObject      *source_object,
 
1209
                                   GAsyncResult *res,
 
1210
                                   gpointer      user_data)
 
1211
{
 
1212
  PpMaintenanceCommand *command = (PpMaintenanceCommand *) source_object;
 
1213
  GError               *error = NULL;
 
1214
  PCData               *data;
 
1215
  gboolean              result;
 
1216
 
 
1217
  result = pp_maintenance_command_execute_finish (command, res, &error);
 
1218
  g_object_unref (source_object);
 
1219
 
 
1220
  if (result)
 
1221
    {
 
1222
      data = (PCData *) user_data;
 
1223
 
 
1224
      data->autoconfigure_finished = TRUE;
 
1225
      printer_configure_async_finish (data);
 
1226
    }
 
1227
  else
 
1228
    {
 
1229
      if (error->domain != G_IO_ERROR ||
 
1230
          error->code != G_IO_ERROR_CANCELLED)
 
1231
        {
 
1232
          data = (PCData *) user_data;
 
1233
 
 
1234
          g_warning ("%s", error->message);
 
1235
 
 
1236
          data->autoconfigure_finished = TRUE;
 
1237
          printer_configure_async_finish (data);
 
1238
        }
 
1239
 
 
1240
      g_error_free (error);
 
1241
    }
 
1242
}
 
1243
 
 
1244
static void
 
1245
printer_configure_async (PpNewPrinter *new_printer)
 
1246
{
 
1247
  PpNewPrinterPrivate  *priv = new_printer->priv;
 
1248
  GDBusConnection      *bus;
 
1249
  PCData               *data;
 
1250
  IMEData              *ime_data;
 
1251
  gchar               **values;
 
1252
  GError               *error = NULL;
 
1253
 
 
1254
  data = g_new0 (PCData, 1);
 
1255
  data->new_printer = new_printer;
 
1256
  data->set_accept_jobs_finished = FALSE;
 
1257
  data->set_enabled_finished = FALSE;
 
1258
  data->autoconfigure_finished = FALSE;
 
1259
  data->set_media_size_finished = FALSE;
 
1260
  data->install_missing_executables_finished = FALSE;
 
1261
 
 
1262
  /* Enable printer and make it accept jobs */
 
1263
  if (priv->name)
 
1264
    {
 
1265
      bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
 
1266
      if (bus)
 
1267
        {
 
1268
          g_dbus_connection_call (bus,
 
1269
                                  MECHANISM_BUS,
 
1270
                                  "/",
 
1271
                                  MECHANISM_BUS,
 
1272
                                  "PrinterSetAcceptJobs",
 
1273
                                  g_variant_new ("(sbs)",
 
1274
                                                 priv->name,
 
1275
                                                 TRUE,
 
1276
                                                 ""),
 
1277
                                  G_VARIANT_TYPE ("(s)"),
 
1278
                                  G_DBUS_CALL_FLAGS_NONE,
 
1279
                                  -1,
 
1280
                                  NULL,
 
1281
                                  printer_set_accepting_jobs_cb,
 
1282
                                  data);
 
1283
 
 
1284
          g_dbus_connection_call (g_object_ref (bus),
 
1285
                                  MECHANISM_BUS,
 
1286
                                  "/",
 
1287
                                  MECHANISM_BUS,
 
1288
                                  "PrinterSetEnabled",
 
1289
                                  g_variant_new ("(sb)",
 
1290
                                                 priv->name,
 
1291
                                                 TRUE),
 
1292
                                  G_VARIANT_TYPE ("(s)"),
 
1293
                                  G_DBUS_CALL_FLAGS_NONE,
 
1294
                                  -1,
 
1295
                                  NULL,
 
1296
                                  printer_set_enabled_cb,
 
1297
                                  data);
 
1298
        }
 
1299
      else
 
1300
        {
 
1301
          g_warning ("Failed to get system bus: %s", error->message);
 
1302
          g_error_free (error);
 
1303
          data->set_accept_jobs_finished = TRUE;
 
1304
          data->set_enabled_finished = TRUE;
 
1305
        }
 
1306
    }
 
1307
  else
 
1308
    {
 
1309
      data->set_accept_jobs_finished = TRUE;
 
1310
      data->set_enabled_finished = TRUE;
 
1311
    }
 
1312
 
 
1313
  /* Run autoconfiguration of printer */
 
1314
  if (!priv->is_network_device)
 
1315
    {
 
1316
      PpMaintenanceCommand *command;
 
1317
      command = pp_maintenance_command_new (priv->name,
 
1318
                                            "autoconfigure",
 
1319
      /* Translators: Name of job which makes printer to autoconfigure itself */
 
1320
                                            _("Automatic configuration"));
 
1321
 
 
1322
      pp_maintenance_command_execute_async (command,
 
1323
                                            NULL,
 
1324
                                            pp_maintenance_command_execute_cb,
 
1325
                                            data);
 
1326
    }
 
1327
 
 
1328
  /* Set media size for printer */
 
1329
  values = g_new0 (gchar *, 2);
 
1330
  values[0] = g_strdup (get_paper_size_from_locale ());
 
1331
 
 
1332
  printer_add_option_async (priv->name, "media", values, TRUE, NULL, pao_cb, data);
 
1333
 
 
1334
  g_strfreev (values);
 
1335
 
 
1336
  /* Install missing executables for printer */
 
1337
  ime_data = g_new0 (IMEData, 1);
 
1338
  ime_data->window_id = priv->window_id;
 
1339
  if (data->cancellable)
 
1340
    ime_data->cancellable = g_object_ref (data->cancellable);
 
1341
  ime_data->user_data = data;
 
1342
 
 
1343
  printer_get_ppd_async (priv->name,
 
1344
                         NULL,
 
1345
                         0,
 
1346
                         printer_get_ppd_cb,
 
1347
                         ime_data);
 
1348
}
 
1349
 
 
1350
static void
 
1351
_pp_new_printer_add_async (GSimpleAsyncResult *res,
 
1352
                           GObject            *object,
 
1353
                           GCancellable       *cancellable)
 
1354
{
 
1355
  PpNewPrinter        *printer = PP_NEW_PRINTER (object);
 
1356
  PpNewPrinterPrivate *priv = printer->priv;
 
1357
 
 
1358
  priv->res = g_object_ref (res);
 
1359
  priv->cancellable = g_object_ref (cancellable);
 
1360
 
 
1361
  if (priv->ppd_name || priv->ppd_file_name)
 
1362
    {
 
1363
      /* We have everything we need */
 
1364
      printer_add_real_async (printer);
 
1365
    }
 
1366
  else if (priv->device_id)
 
1367
    {
 
1368
      GDBusConnection *bus;
 
1369
      GError          *error = NULL;
 
1370
 
 
1371
      /* Try whether CUPS has a driver for the new printer */
 
1372
      bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
1373
      if (bus)
 
1374
        {
 
1375
          g_dbus_connection_call (bus,
 
1376
                                  SCP_BUS,
 
1377
                                  SCP_PATH,
 
1378
                                  SCP_IFACE,
 
1379
                                  "GetBestDrivers",
 
1380
                                  g_variant_new ("(sss)",
 
1381
                                                 priv->device_id,
 
1382
                                                 priv->make_and_model ? priv->make_and_model : "",
 
1383
                                                 priv->device_uri ? priv->device_uri : ""),
 
1384
                                  G_VARIANT_TYPE ("(a(ss))"),
 
1385
                                  G_DBUS_CALL_FLAGS_NONE,
 
1386
                                  DBUS_TIMEOUT_LONG,
 
1387
                                  cancellable,
 
1388
                                  printer_add_async_scb,
 
1389
                                  printer);
 
1390
        }
 
1391
      else
 
1392
        {
 
1393
          g_warning ("Failed to get system bus: %s", error->message);
 
1394
          g_error_free (error);
 
1395
          _pp_new_printer_add_async_cb (FALSE, printer);
 
1396
        }
 
1397
    }
 
1398
  else if (priv->original_name && priv->host_name)
 
1399
    {
 
1400
      /* Try to get PPD from remote CUPS */
 
1401
      printer_get_ppd_async (priv->original_name,
 
1402
                             priv->host_name,
 
1403
                             priv->host_port,
 
1404
                             printer_add_async_scb4,
 
1405
                             printer);
 
1406
    }
 
1407
  else
 
1408
    {
 
1409
      _pp_new_printer_add_async_cb (FALSE, printer);
 
1410
    }
 
1411
}
 
1412
 
 
1413
void
 
1414
pp_new_printer_add_async (PpNewPrinter        *printer,
 
1415
                          GCancellable        *cancellable,
 
1416
                          GAsyncReadyCallback  callback,
 
1417
                          gpointer             user_data)
 
1418
{
 
1419
  GSimpleAsyncResult *res;
 
1420
 
 
1421
  res = g_simple_async_result_new (G_OBJECT (printer), callback, user_data, pp_new_printer_add_async);
 
1422
 
 
1423
  g_simple_async_result_set_check_cancellable (res, cancellable);
 
1424
  _pp_new_printer_add_async (res, G_OBJECT (printer), cancellable);
 
1425
 
 
1426
  g_object_unref (res);
 
1427
}
 
1428
 
 
1429
gboolean
 
1430
pp_new_printer_add_finish (PpNewPrinter  *printer,
 
1431
                           GAsyncResult  *res,
 
1432
                           GError       **error)
 
1433
{
 
1434
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
 
1435
 
 
1436
  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == pp_new_printer_add_async);
 
1437
 
 
1438
  if (g_simple_async_result_propagate_error (simple, error))
 
1439
    return FALSE;
 
1440
 
 
1441
  return g_simple_async_result_get_op_res_gboolean (simple);
 
1442
}