File: scp-dbus-service.py

package info (click to toggle)
system-config-printer 1.5.11-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 10,328 kB
  • sloc: python: 22,047; sh: 4,780; ansic: 1,472; xml: 798; makefile: 370
file content (603 lines) | stat: -rw-r--r-- 22,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/python3

## system-config-printer

## Copyright (C) 2010, 2011, 2012, 2013, 2014 Red Hat, Inc.
## Authors:
##  Tim Waugh <twaugh@redhat.com>

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import gi
import dbus.service
from gi.repository import GObject
from gi.repository import GLib
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import sys

from debug import *
import asyncconn
import config
import cups
import cupshelpers
import dnssdresolve
import jobviewer
import killtimer
import newprinter
import PhysicalDevice
import ppdcache
import printerproperties

cups.require ("1.9.52")

CONFIG_BUS='org.fedoraproject.Config.Printing'
CONFIG_PATH='/org/fedoraproject/Config/Printing'
CONFIG_IFACE='org.fedoraproject.Config.Printing'
CONFIG_NEWPRINTERDIALOG_IFACE=CONFIG_IFACE + ".NewPrinterDialog"
CONFIG_PRINTERPROPERTIESDIALOG_IFACE=CONFIG_IFACE + ".PrinterPropertiesDialog"
CONFIG_JOBVIEWER_IFACE=CONFIG_IFACE + ".JobViewer"

g_ppds = None
g_killtimer = None

#set program name
GLib.set_prgname("system-config-printer")

class FetchedPPDs(GObject.GObject):
    __gsignals__ = {
        'ready': (GObject.SIGNAL_RUN_LAST, None, ()),
        'error': (GObject.SIGNAL_RUN_LAST, None,
                  (GObject.TYPE_PYOBJECT,))
        }

    def __init__ (self, cupsconn, language):
        GObject.GObject.__init__ (self)
        self._cupsconn = cupsconn
        self._language = language
        self._ppds = None

    def is_ready (self):
        return self._ppds is not None

    def get_ppds (self):
        return self._ppds

    def run (self):
        debugprint ("FetchPPDs: running")
        self._ppds = None
        self._cupsconn.getPPDs2 (reply_handler=self._cups_getppds_reply,
                                 error_handler=self._cups_error)

    def _cups_error (self, conn, exc):
        debugprint ("FetchPPDs: error: %s" % repr (exc))
        self.emit ('error', exc)

    def _cups_getppds_reply (self, conn, result):
        debugprint ("FetchPPDs: success")
        self._ppds = cupshelpers.ppds.PPDs (result, language=self._language)
        self.emit ('ready')

class GetBestDriversRequest:
    def __init__ (self, device_id, device_make_and_model, device_uri,
                  cupsconn, language, reply_handler, error_handler):
        self.device_id = device_id
        self.device_make_and_model = device_make_and_model
        self.device_uri = device_uri
        self.cupsconn = cupsconn
        self.language = language
        self.reply_handler = reply_handler
        self.error_handler = error_handler
        self._signals = []
        self.installed_files = []
        self.download_tried = False
        debugprint ("+%s" % self)

        g_killtimer.add_hold ()
        global g_ppds
        if g_ppds is None:
            debugprint ("GetBestDrivers request: need to fetch PPDs")
            g_ppds = FetchedPPDs (self.cupsconn, self.language)
            self._signals.append (g_ppds.connect ('ready', self._ppds_ready))
            self._signals.append (g_ppds.connect ('error', self._ppds_error))
            g_ppds.run ()
        else:
            if g_ppds.is_ready ():
                debugprint ("GetBestDrivers request: PPDs already fetched")
                self._ppds_ready (g_ppds)
            else:
                debugprint ("GetBestDrivers request: waiting for PPDs")
                self._signals.append (g_ppds.connect ('ready',
                                                      self._ppds_ready))
                self._signals.append (g_ppds.connect ('error',
                                                      self._ppds_error))

    def __del__ (self):
        debugprint ("-%s" % self)

    def _disconnect_signals (self):
        for s in self._signals:
            g_ppds.disconnect (s)

    def _ppds_error (self, fetchedppds, exc):
        self._disconnect_signals ()
        self.error_handler (exc)

    def _ppds_ready (self, fetchedppds):
        if not fetchedppds.is_ready ():
            # PPDs being reloaded. Wait for next 'ready' signal.
            return

        self._disconnect_signals ()
        ppds = fetchedppds.get_ppds ()

        try:
            if self.device_id:
                id_dict = cupshelpers.parseDeviceID (self.device_id)
            else:
                id_dict = {}
                (mfg,
                 mdl) = cupshelpers.ppds.ppdMakeModelSplit (self.device_make_and_model)
                id_dict["MFG"] = mfg
                id_dict["MDL"] = mdl
                id_dict["DES"] = ""
                id_dict["CMD"] = []
                self.device_id = "MFG:%s;MDL:%s;" % (mfg, mdl)

            fit = ppds.getPPDNamesFromDeviceID (id_dict["MFG"],
                                                id_dict["MDL"],
                                                id_dict["DES"],
                                                id_dict["CMD"],
                                                self.device_uri,
                                                self.device_make_and_model)

            ppdnamelist = ppds.orderPPDNamesByPreference (fit.keys (),
                                                          self.installed_files,
                                                          devid=id_dict,
                                                          fit=fit)
            ppdname = ppdnamelist[0]
            status = fit[ppdname]

            try:
                if status != "exact" and not self.download_tried:
                    self.download_tried = True
                    self.dialog = newprinter.NewPrinterGUI()
                    self.dialog.NewPrinterWindow.set_modal (False)
                    self.handles = \
                                   [self.dialog.connect ('dialog-canceled',
                                                         self.on_dialog_canceled),
                                    self.dialog.connect ('driver-download-checked',
                                                         self.on_driver_download_checked)]

                    self.reply_if_fail = [(x, fit[x]) for x in ppdnamelist]
                    if not self.dialog.init ('download_driver',
                                             devid=self.device_id):
                        try:
                            g_killtimer.remove_hold ()
                        finally:
                            e = RuntimeError ("Failed to launch dialog")
                            self.error_handler (e)

                    return
            except:
                # Ignore driver download if packages needed for the GUI are not
                # installed or if no windows can be opened
                pass

            g_killtimer.remove_hold ()
            self.reply_handler ([(x, fit[x]) for x in ppdnamelist])
        except Exception as e:
            try:
                g_killtimer.remove_hold ()
            except:
                pass

            self.error_handler (e)

    def _destroy_dialog (self):
        for handle in self.handles:
            self.dialog.disconnect (handle)

        self.dialog.destroy ()
        del self.dialog

    def on_driver_download_checked(self, obj, installed_files):
        if len (installed_files) > 0:
            debugprint ("GetBestDrivers request: Re-fetch PPDs after driver download")
            self._signals.append (g_ppds.connect ('ready', self._ppds_ready))
            self._signals.append (g_ppds.connect ('error', self._ppds_error))
            g_ppds.run ()
            return

        g_killtimer.remove_hold ()
        self._destroy_dialog ()
        self.reply_handler (self.reply_if_fail)

    def on_dialog_canceled(self, obj):
        g_killtimer.remove_hold ()
        self._destroy_dialog ()
        self.reply_handler (self.reply_if_fail)

class GroupPhysicalDevicesRequest:
    def __init__ (self, devices, reply_handler, error_handler):
        self.devices = devices
        self.reply_handler = reply_handler
        self.error_handler = error_handler
        debugprint ("+%s" % self)

        try:
            g_killtimer.add_hold ()
            need_resolving = {}
            self.deviceobjs = {}
            for device_uri, device_dict in self.devices.items ():
                deviceobj = cupshelpers.Device (device_uri, **device_dict)
                self.deviceobjs[device_uri] = deviceobj
                if device_uri.startswith ("dnssd://"):
                    need_resolving[device_uri] = deviceobj

            if len (need_resolving) > 0:
                resolver = dnssdresolve.DNSSDHostNamesResolver (need_resolving)
                resolver.resolve (reply_handler=self._group)
            else:
                self._group ()
        except Exception as e:
            g_killtimer.remove_hold ()
            self.error_handler (e)

    def __del__ (self):
        debugprint ("-%s" % self)

    def _group (self, resolved_devices=None):
        # We can ignore resolved_devices because the actual objects
        # (in self.devices) have been modified.
        try:
            self.physdevs = []
            for device_uri, deviceobj in self.deviceobjs.items ():
                newphysicaldevice = PhysicalDevice.PhysicalDevice (deviceobj)
                matched = False
                try:
                    i = self.physdevs.index (newphysicaldevice)
                    self.physdevs[i].add_device (deviceobj)
                except ValueError:
                    self.physdevs.append (newphysicaldevice)

            uris_by_phys = []
            for physdev in self.physdevs:
                uris_by_phys.append ([x.uri for x in physdev.get_devices ()])

            g_killtimer.remove_hold ()
            self.reply_handler (uris_by_phys)
        except Exception as e:
            g_killtimer.remove_hold ()
            self.error_handler (e)

class ConfigPrintingNewPrinterDialog(dbus.service.Object):
    def __init__ (self, bus, path, cupsconn):
        bus_name = dbus.service.BusName (CONFIG_BUS, bus=bus)
        dbus.service.Object.__init__ (self, bus_name, path)
        self.dialog = newprinter.NewPrinterGUI()
        self.dialog.NewPrinterWindow.set_modal (False)
        self.handles = [self.dialog.connect ('dialog-canceled',
                                             self.on_dialog_canceled),
                        self.dialog.connect ('printer-added',
                                             self.on_printer_added),
                        self.dialog.connect ('printer-modified',
                                             self.on_printer_modified),
                        self.dialog.connect ('driver-download-checked',
                                             self.on_driver_download_checked)]
        self._ppdcache = ppdcache.PPDCache ()
        self._cupsconn = cupsconn
        debugprint ("+%s" % self)

    def __del__ (self):
        self.dialog.destroy ()
        debugprint ("-%s" % self)

    @dbus.service.method(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         in_signature='uss', out_signature='')
    def NewPrinterFromDevice(self, xid, device_uri, device_id):
        g_killtimer.add_hold ()
        self.dialog.init ('printer_with_uri', device_uri=device_uri,
                          devid=device_id, xid=xid)

    @dbus.service.method(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         in_signature='us', out_signature='')
    def DownloadDriverForDeviceID(self, xid, device_id):
        g_killtimer.add_hold ()
        self.dialog.init ('download_driver', devid=device_id, xid=xid)

    @dbus.service.method(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         in_signature='uss', out_signature='')
    def ChangePPD(self, xid, name, device_id):
        g_killtimer.add_hold ()
        self.xid = xid
        self.name = name
        self.device_id = device_id
        self._ppdcache.fetch_ppd (name, self._change_ppd_got_ppd)

    def _change_ppd_got_ppd(self, name, ppd, exc):
        # Got PPD; now find device URI.
        self.ppd = ppd
        self._cupsconn.getPrinters (reply_handler=self._change_ppd_with_dev,
                                    error_handler=self._do_change_ppd)

    def _change_ppd_with_dev (self, conn, result):
        self.device_uri = result.get (self.name, {}).get ('device-uri', None)
        self._do_change_ppd (conn)

    def _do_change_ppd(self, conn, exc=None):
        self.dialog.init ('ppd', device_uri=self.device_uri, name=self.name,
                          ppd=self.ppd, devid=self.device_id, xid=self.xid)

    @dbus.service.signal(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         signature='')
    def DialogCanceled(self):
        pass

    @dbus.service.signal(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         signature='s')
    def PrinterAdded(self, name):
        pass

    @dbus.service.signal(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         signature='sb')
    def PrinterModified(self, name, ppd_has_changed):
        pass

    @dbus.service.signal(dbus_interface=CONFIG_NEWPRINTERDIALOG_IFACE,
                         signature='a(s)')
    def DriverDownloadChecked(self, installed_files):
        pass

    def on_dialog_canceled(self, obj):
        debugprint ("%s: dialog canceled" % self)
        g_killtimer.remove_hold ()
        self.DialogCanceled ()
        self.remove_handles ()
        self.remove_from_connection ()

    def on_printer_added(self, obj, name):
        debugprint ("%s: printer added" % self)
        g_killtimer.remove_hold ()
        self.PrinterAdded (name)
        self.remove_handles ()
        self.remove_from_connection ()

    def on_printer_modified(self, obj, name, ppd_has_changed):
        debugprint ("%s: printer modified" % self)
        g_killtimer.remove_hold ()
        self.PrinterModifed (name, ppd_has_changed)
        self.remove_handles ()
        self.remove_from_connection ()

    def on_driver_download_checked(self, obj, installed_files):
        debugprint ("%s: driver download checked" % self)
        g_killtimer.remove_hold ()
        self.DriverDownloadChecked (installed_files)
        self.remove_handles ()
        self.remove_from_connection ()

    def remove_handles (self):
        for handle in self.handles:
            self.dialog.disconnect (handle)

class ConfigPrintingPrinterPropertiesDialog(dbus.service.Object):
    def __init__ (self, bus, path, xid, name):
        bus_name = dbus.service.BusName (CONFIG_BUS, bus=bus)
        dbus.service.Object.__init__ (self, bus_name=bus_name, object_path=path)
        self.dialog = printerproperties.PrinterPropertiesDialog ()
        self.dialog.dialog.set_modal (False)
        handle = self.dialog.connect ('dialog-closed', self.on_dialog_closed)
        self.closed_handle = handle
        self.dialog.show (name)
        self.dialog.dialog.set_modal (False)
        g_killtimer.add_hold ()

    @dbus.service.method(dbus_interface=CONFIG_PRINTERPROPERTIESDIALOG_IFACE,
                         in_signature='', out_signature='')
    def PrintTestPage (self):
        debugprint ("Printing test page")
        return self.dialog.printTestPage ()

    @dbus.service.signal(dbus_interface=CONFIG_PRINTERPROPERTIESDIALOG_IFACE,
                         signature='')
    def Finished (self):
        pass

    def on_dialog_closed (self, dialog):
        dialog.destroy ()
        g_killtimer.remove_hold ()
        self.Finished ()
        self.dialog.disconnect (self.closed_handle)
        self.remove_from_connection ()

class ConfigPrintingJobApplet(dbus.service.Object):
    def __init__ (self, bus, path):
        bus_name = dbus.service.BusName (CONFIG_BUS, bus=bus)
        dbus.service.Object.__init__ (self, bus_name=bus_name, object_path=path)
        Gdk.threads_enter ()
        self.jobapplet = jobviewer.JobViewer(bus=dbus.SystemBus (),
                                             applet=True, my_jobs=True)
        self.jobapplet.set_process_pending (False)
        Gdk.threads_leave ()
        handle = self.jobapplet.connect ('finished', self.on_jobapplet_finished)
        self.finished_handle = handle
        self.has_finished = False
        g_killtimer.add_hold ()
        debugprint ("+%s" % self)

    def __del__ (self):
        debugprint ("-%s" % self)

    @dbus.service.method(dbus_interface=CONFIG_JOBVIEWER_IFACE,
                         in_signature='', out_signature='')
    def Quit(self):
        if not self.has_finished:
            self.jobapplet.cleanup ()

    @dbus.service.signal(dbus_interface=CONFIG_JOBVIEWER_IFACE, signature='')
    def Finished(self):
        pass

    def on_jobapplet_finished (self, jobapplet):
        self.Finished ()
        g_killtimer.remove_hold ()
        self.has_finished = True
        self.jobapplet.disconnect (self.finished_handle)
        self.remove_from_connection ()

class ConfigPrinting(dbus.service.Object):
    def __init__ (self):
        self.bus = dbus.SessionBus ()
        bus_name = dbus.service.BusName (CONFIG_BUS, bus=self.bus)
        dbus.service.Object.__init__ (self, bus_name, CONFIG_PATH)
        self._cupsconn = asyncconn.Connection ()
        self._pathn = 0
        self._jobapplet = None
        self._jobappletpath = None
        self._ppds = None
        self._language = locale.getlocale (locale.LC_MESSAGES)[0]

    def destroy (self):
        self._cupsconn.destroy ()

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='', out_signature='s')
    def NewPrinterDialog(self):
        self._pathn += 1
        path = "%s/NewPrinterDialog/%s" % (CONFIG_PATH, self._pathn)
        ConfigPrintingNewPrinterDialog (self.bus, path,
                                        self._cupsconn)
        g_killtimer.alive ()
        return path

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='us', out_signature='s')
    def PrinterPropertiesDialog(self, xid, name):
        self._pathn += 1
        path = "%s/PrinterPropertiesDialog/%s" % (CONFIG_PATH, self._pathn)
        ConfigPrintingPrinterPropertiesDialog (self.bus, path, xid, name)
        g_killtimer.alive ()
        return path

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='', out_signature='s')
    def JobApplet(self):
       if self._jobapplet is None or self._jobapplet.has_finished:
            self._pathn += 1
            path = "%s/JobApplet/%s" % (CONFIG_PATH, self._pathn)
            self._jobapplet = ConfigPrintingJobApplet (self.bus, path)
            self._jobappletpath = path

       return self._jobappletpath

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='sss', out_signature='a(ss)',
                         async_callbacks=('reply_handler', 'error_handler'))
    def GetBestDrivers(self, device_id, device_make_and_model, device_uri,
                   reply_handler, error_handler):
        GetBestDriversRequest (device_id, device_make_and_model, device_uri,
                               self._cupsconn, self._language[0],
                               reply_handler, error_handler)

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='s', out_signature='as')
    def MissingExecutables(self, ppd_filename):
        ppd = cups.PPD (ppd_filename)
        return cupshelpers.missingExecutables (ppd)

    @dbus.service.method(dbus_interface=CONFIG_IFACE,
                         in_signature='a{sa{ss}}', out_signature='aas',
                         async_callbacks=('reply_handler', 'error_handler'))
    def GroupPhysicalDevices(self, devices, reply_handler, error_handler):
        GroupPhysicalDevicesRequest (devices, reply_handler, error_handler)

def _client_demo ():
    # Client demo
    if len (sys.argv) > 2:
        device_uri = sys.argv[2]
        device_id = ''
        if (len (sys.argv) > 4 and
            sys.argv[3] == '--devid'):
            device_id = sys.argv[4]
    else:
        print ("Device URI required")
        return

    from gi.repository import Gtk
    bus = dbus.SessionBus ()
    obj = bus.get_object (CONFIG_BUS, CONFIG_PATH)
    iface = dbus.Interface (obj, CONFIG_IFACE)
    path = iface.NewPrinterDialog ()
    debugprint (path)

    obj = bus.get_object (CONFIG_BUS, path)
    iface = dbus.Interface (obj, CONFIG_NEWPRINTERDIALOG_IFACE)
    loop = GObject.MainLoop ()
    def on_canceled(path=None):
        print ("%s: Dialog canceled" % path)
        loop.quit ()

    def on_added(name, path=None):
        print ("%s: Printer '%s' added" % (path, name))
        loop.quit ()

    iface.connect_to_signal ("DialogCanceled", on_canceled,
                             path_keyword="path")
    iface.connect_to_signal ("PrinterAdded", on_added,
                             path_keyword="path")

    iface.NewPrinterFromDevice (0, device_uri, device_id)
    loop.run ()

if __name__ == '__main__':
    import ppdippstr
    import config
    import gettext
    gettext.install(domain=config.PACKAGE, localedir=config.localedir)

    import locale
    try:
        locale.setlocale (locale.LC_ALL, "")
    except:
        pass

    ppdippstr.init ()
    Gdk.threads_init ()
    from dbus.glib import DBusGMainLoop
    DBusGMainLoop (set_as_default=True)

    client_demo = False
    if len (sys.argv) > 1:
        for opt in sys.argv[1:]:
            if opt == "--debug":
                set_debugging (True)
                cupshelpers.set_debugprint_fn (debugprint)
            elif opt == "--client":
                client_demo = True

    if client_demo:
        _client_demo ()
        sys.exit (0)

    debugprint ("Service running...")
    g_killtimer = killtimer.KillTimer (killfunc=Gtk.main_quit)
    cp = ConfigPrinting ()
    Gdk.threads_enter ()
    Gtk.main ()
    Gdk.threads_leave ()
    cp.destroy ()