| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Red Hat, Inc. |
| 4 |
## Author: Tim Waugh <[email protected]> |
| 5 |
|
| 6 |
## This program is free software; you can redistribute it and/or modify |
| 7 |
## it under the terms of the GNU General Public License as published by |
| 8 |
## the Free Software Foundation; either version 2 of the License, or |
| 9 |
## (at your option) any later version. |
| 10 |
|
| 11 |
## This program is distributed in the hope that it will be useful, |
| 12 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
## GNU General Public License for more details. |
| 15 |
|
| 16 |
## You should have received a copy of the GNU General Public License |
| 17 |
## along with this program; if not, write to the Free Software |
| 18 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
|
| 20 |
import cups |
| 21 |
from functools import reduce |
| 22 |
cups.require ("1.9.42") |
| 23 |
import sys |
| 24 |
import socket |
| 25 |
import getpass |
| 26 |
from debug import * |
| 27 |
|
| 28 |
import dbus |
| 29 |
|
| 30 |
# set up global default main loop |
| 31 |
from dbus.mainloop.glib import DBusGMainLoop |
| 32 |
DBusGMainLoop(set_as_default=True) |
| 33 |
|
| 34 |
import dbus.service |
| 35 |
import gi |
| 36 |
from gi.repository import GObject |
| 37 |
from gi.repository import GLib |
| 38 |
import time |
| 39 |
import locale |
| 40 |
import cupshelpers.installdriver |
| 41 |
|
| 42 |
DOMAIN="system-config-printer" |
| 43 |
import gettext |
| 44 |
gettext.install(domain=DOMAIN) |
| 45 |
try: |
| 46 |
locale.setlocale (locale.LC_ALL, "") |
| 47 |
except locale.Error as e: |
| 48 |
import os |
| 49 |
os.environ['LC_ALL'] = 'C' |
| 50 |
locale.setlocale (locale.LC_ALL, "") |
| 51 |
|
| 52 |
gi.require_version('Notify', '0.7') |
| 53 |
from gi.repository import Notify |
| 54 |
|
| 55 |
APPDIR="/usr/share/system-config-printer" |
| 56 |
ICON="printer" |
| 57 |
|
| 58 |
# We need to call Notify.init before we can check the server for caps |
| 59 |
Notify.init('System Config Printer Notification') |
| 60 |
|
| 61 |
# D-Bus APIs of other objects we'll use. |
| 62 |
PRINTING_BUS="org.fedoraproject.Config.Printing" |
| 63 |
PRINTING_PATH="/org/fedoraproject/Config/Printing" |
| 64 |
PRINTING_IFACE="org.fedoraproject.Config.Printing" |
| 65 |
NEWPRINTERDIALOG_IFACE=PRINTING_IFACE + ".NewPrinterDialog" |
| 66 |
PRINTERPROPERTIESDIALOG_IFACE=PRINTING_IFACE + ".PrinterPropertiesDialog" |
| 67 |
|
| 68 |
#### |
| 69 |
#### NewPrinterNotification DBus server (the 'new' way). |
| 70 |
#### |
| 71 |
PDS_PATH="/com/redhat/NewPrinterNotification" |
| 72 |
PDS_IFACE="com.redhat.NewPrinterNotification" |
| 73 |
PDS_OBJ="com.redhat.NewPrinterNotification" |
| 74 |
class NewPrinterNotification(dbus.service.Object): |
| 75 |
STATUS_SUCCESS = 0 |
| 76 |
STATUS_MODEL_MISMATCH = 1 |
| 77 |
STATUS_GENERIC_DRIVER = 2 |
| 78 |
STATUS_NO_DRIVER = 3 |
| 79 |
|
| 80 |
def __init__ (self, system_bus, session_bus): |
| 81 |
self.system_bus = system_bus |
| 82 |
self.session_bus = session_bus |
| 83 |
self.getting_ready = 0 |
| 84 |
bus_name = dbus.service.BusName (PDS_OBJ, bus=system_bus) |
| 85 |
dbus.service.Object.__init__ (self, bus_name, PDS_PATH) |
| 86 |
self.notification = None |
| 87 |
|
| 88 |
@dbus.service.method(PDS_IFACE, in_signature='', out_signature='') |
| 89 |
def GetReady (self): |
| 90 |
TIMEOUT=1200000 |
| 91 |
if self.getting_ready == 0: |
| 92 |
n = Notify.Notification.new (_("Configuring new printer"), |
| 93 |
_("Please wait..."), |
| 94 |
'printer') |
| 95 |
n.set_timeout (TIMEOUT + 5000) |
| 96 |
n.closed = False |
| 97 |
n.connect ('closed', self.on_notification_closed) |
| 98 |
n.show () |
| 99 |
self.notification = n |
| 100 |
|
| 101 |
self.getting_ready += 1 |
| 102 |
GLib.timeout_add_seconds (TIMEOUT, self.timeout_ready) |
| 103 |
|
| 104 |
def on_notification_closed (self, notification): |
| 105 |
notification.closed = True |
| 106 |
|
| 107 |
def timeout_ready (self): |
| 108 |
if self.getting_ready > 0: |
| 109 |
self.getting_ready -= 1 |
| 110 |
if (self.getting_ready == 0 and |
| 111 |
self.notification and |
| 112 |
not getattr (self.notification, 'closed', None)): |
| 113 |
self.notification.close () |
| 114 |
|
| 115 |
return False |
| 116 |
|
| 117 |
@dbus.service.method(PDS_IFACE, in_signature='isssss', out_signature='') |
| 118 |
def NewPrinter (self, status, name, mfg, mdl, des, cmd): |
| 119 |
if name.find("/") >= 0: |
| 120 |
# name is a URI, no queue was generated, because no suitable |
| 121 |
# driver was found |
| 122 |
title = _("Missing printer driver") |
| 123 |
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd) |
| 124 |
if (mfg and mdl) or des: |
| 125 |
if (mfg and mdl): |
| 126 |
device = "%s %s" % (mfg, mdl) |
| 127 |
else: |
| 128 |
device = des |
| 129 |
text = _("No printer driver for %s.") % device |
| 130 |
else: |
| 131 |
text = _("No driver for this printer.") |
| 132 |
n = Notify.Notification.new (title, text, 'printer') |
| 133 |
if "actions" in Notify.get_server_caps(): |
| 134 |
n.set_urgency (Notify.Urgency.CRITICAL) |
| 135 |
n.set_timeout (Notify.EXPIRES_NEVER) |
| 136 |
n.add_action ("setup-printer", _("Search"), |
| 137 |
lambda x, y: |
| 138 |
self.setup_printer (x, y, name, devid)) |
| 139 |
else: |
| 140 |
self.setup_printer (None, None, name, devid) |
| 141 |
|
| 142 |
else: |
| 143 |
# name is the name of the queue which hal_lpadmin has set up |
| 144 |
# automatically. |
| 145 |
c = cups.Connection () |
| 146 |
try: |
| 147 |
printer = c.getPrinters ()[name] |
| 148 |
except KeyError: |
| 149 |
return |
| 150 |
|
| 151 |
try: |
| 152 |
filename = c.getPPD (name) |
| 153 |
except cups.IPPError: |
| 154 |
return |
| 155 |
|
| 156 |
del c |
| 157 |
|
| 158 |
# Check for missing packages |
| 159 |
cups.ppdSetConformance (cups.PPD_CONFORM_RELAXED) |
| 160 |
ppd = cups.PPD (filename) |
| 161 |
import os |
| 162 |
os.unlink (filename) |
| 163 |
import sys |
| 164 |
sys.path.append (APPDIR) |
| 165 |
import cupshelpers |
| 166 |
(missing_pkgs, |
| 167 |
missing_exes) = cupshelpers.missingPackagesAndExecutables (ppd) |
| 168 |
|
| 169 |
from cupshelpers.ppds import ppdMakeModelSplit |
| 170 |
(make, model) = ppdMakeModelSplit (printer['printer-make-and-model']) |
| 171 |
driver = make + " " + model |
| 172 |
if status < self.STATUS_GENERIC_DRIVER: |
| 173 |
title = _("Printer added") |
| 174 |
else: |
| 175 |
title = _("Missing printer driver") |
| 176 |
|
| 177 |
if len (missing_pkgs) > 0: |
| 178 |
pkgs = reduce (lambda x,y: x + ", " + y, missing_pkgs) |
| 179 |
title = _("Install printer driver") |
| 180 |
text = (_("`%s' requires driver installation: %s.") % |
| 181 |
(name, pkgs)) |
| 182 |
n = Notify.Notification.new (title, text, 'printer') |
| 183 |
import installpackage |
| 184 |
if "actions" in Notify.get_server_caps(): |
| 185 |
try: |
| 186 |
self.packagekit = installpackage.PackageKit () |
| 187 |
n.set_timeout (Notify.EXPIRES_NEVER) |
| 188 |
n.add_action ("install-driver", _("Install"), |
| 189 |
lambda x, y: |
| 190 |
self.install_driver (x, y, |
| 191 |
missing_pkgs)) |
| 192 |
except: |
| 193 |
pass |
| 194 |
else: |
| 195 |
try: |
| 196 |
self.packagekit = installpackage.PackageKit () |
| 197 |
self.packagekit.InstallPackageName (0, 0, |
| 198 |
missing_pkgs[0]) |
| 199 |
except: |
| 200 |
pass |
| 201 |
|
| 202 |
elif status == self.STATUS_SUCCESS: |
| 203 |
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd) |
| 204 |
text = _("`%s' is ready for printing.") % name |
| 205 |
n = Notify.Notification.new (title, text, 'printer') |
| 206 |
if "actions" in Notify.get_server_caps(): |
| 207 |
n.set_urgency (Notify.Urgency.NORMAL) |
| 208 |
n.add_action ("test-page", _("Print test page"), |
| 209 |
lambda x, y: |
| 210 |
self.print_test_page (x, y, name)) |
| 211 |
n.add_action ("configure", _("Configure"), |
| 212 |
lambda x, y: self.configure (x, y, name)) |
| 213 |
else: # Model mismatch |
| 214 |
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd) |
| 215 |
text = (_("`%s' has been added, using the `%s' driver.") % |
| 216 |
(name, driver)) |
| 217 |
n = Notify.Notification.new (title, text, 'printer') |
| 218 |
if "actions" in Notify.get_server_caps(): |
| 219 |
n.set_urgency (Notify.Urgency.CRITICAL) |
| 220 |
n.add_action ("test-page", _("Print test page"), |
| 221 |
lambda x, y: |
| 222 |
self.print_test_page (x, y, name, devid)) |
| 223 |
n.add_action ("find-driver", _("Find driver"), |
| 224 |
lambda x, y: |
| 225 |
self.find_driver (x, y, name, devid)) |
| 226 |
n.set_timeout (Notify.EXPIRES_NEVER) |
| 227 |
else: |
| 228 |
self.configure (None, None, name) |
| 229 |
|
| 230 |
self.timeout_ready () |
| 231 |
n.show () |
| 232 |
self.notification = n |
| 233 |
|
| 234 |
def print_test_page (self, notification, action, name): |
| 235 |
path = self.configure (None, None, name) |
| 236 |
obj = self.session_bus.get_object (PRINTING_BUS, path) |
| 237 |
iface = dbus.Interface (obj, PRINTERPROPERTIESDIALOG_IFACE) |
| 238 |
iface.PrintTestPage () |
| 239 |
|
| 240 |
def configure (self, notification, action, name): |
| 241 |
obj = self.session_bus.get_object (PRINTING_BUS, PRINTING_PATH) |
| 242 |
iface = dbus.Interface (obj, PRINTING_IFACE) |
| 243 |
return iface.PrinterPropertiesDialog (dbus.UInt32(0), name) |
| 244 |
|
| 245 |
def get_newprinterdialog_interface (self): |
| 246 |
obj = self.session_bus.get_object (PRINTING_BUS, PRINTING_PATH) |
| 247 |
iface = dbus.Interface (obj, PRINTING_IFACE) |
| 248 |
path = iface.NewPrinterDialog () |
| 249 |
obj = self.session_bus.get_object (PRINTING_BUS, path) |
| 250 |
iface = dbus.Interface (obj, NEWPRINTERDIALOG_IFACE) |
| 251 |
return iface |
| 252 |
|
| 253 |
def ignore_dbus_replies (self, *args): |
| 254 |
pass |
| 255 |
|
| 256 |
def find_driver (self, notification, action, name, devid = ""): |
| 257 |
try: |
| 258 |
iface = self.get_newprinterdialog_interface () |
| 259 |
iface.ChangePPD (dbus.UInt32(0), name, devid, |
| 260 |
reply_handler=self.ignore_dbus_replies, |
| 261 |
error_handler=self.ignore_dbus_replies) |
| 262 |
except dbus.DBusException: |
| 263 |
pass |
| 264 |
|
| 265 |
def setup_printer (self, notification, action, uri, devid = ""): |
| 266 |
try: |
| 267 |
iface = self.get_newprinterdialog_interface () |
| 268 |
iface.NewPrinterFromDevice (dbus.UInt32(0), uri, devid, |
| 269 |
reply_handler=self.ignore_dbus_replies, |
| 270 |
error_handler=self.ignore_dbus_replies) |
| 271 |
except dbus.DBusException: |
| 272 |
pass |
| 273 |
|
| 274 |
def install_driver (self, notification, action, missing_pkgs): |
| 275 |
try: |
| 276 |
self.packagekit.InstallPackageName (0, 0, missing_pkgs[0]) |
| 277 |
except: |
| 278 |
pass |
| 279 |
|
| 280 |
def collect_exit_code (self, pid): |
| 281 |
# We do this with timers instead of signals because we already |
| 282 |
# have GLib imported, but don't (yet) import signal; |
| 283 |
# let's try not to inflate the process size. |
| 284 |
import os |
| 285 |
try: |
| 286 |
print("Waiting for child %d" % pid) |
| 287 |
(pid, status) = os.waitpid (pid, os.WNOHANG) |
| 288 |
if pid == 0: |
| 289 |
# Run this timer again. |
| 290 |
return True |
| 291 |
except OSError: |
| 292 |
pass |
| 293 |
|
| 294 |
return False |
| 295 |
|
| 296 |
PROGRAM_NAME="system-config-printer-applet" |
| 297 |
def show_help (): |
| 298 |
print("usage: %s [--help|--version|--debug]" % PROGRAM_NAME) |
| 299 |
|
| 300 |
def show_version (): |
| 301 |
import config |
| 302 |
print("%s %s" % (PROGRAM_NAME, config.VERSION)) |
| 303 |
|
| 304 |
#### |
| 305 |
#### Main program entry |
| 306 |
#### |
| 307 |
|
| 308 |
def monitor_session (*args): |
| 309 |
pass |
| 310 |
|
| 311 |
def any_jobs (): |
| 312 |
try: |
| 313 |
c = cups.Connection () |
| 314 |
jobs = c.getJobs (my_jobs=True, limit=1) |
| 315 |
if len (jobs): |
| 316 |
return True |
| 317 |
except: |
| 318 |
pass |
| 319 |
|
| 320 |
return False |
| 321 |
|
| 322 |
class RunLoop: |
| 323 |
DBUS_PATH="/com/redhat/PrinterSpooler" |
| 324 |
DBUS_IFACE="com.redhat.PrinterSpooler" |
| 325 |
|
| 326 |
def __init__ (self, session_bus, system_bus, loop): |
| 327 |
self.system_bus = system_bus |
| 328 |
self.session_bus = session_bus |
| 329 |
self.loop = loop |
| 330 |
self.timer = None |
| 331 |
try: |
| 332 |
system_bus.add_signal_receiver (self.handle_dbus_signal, |
| 333 |
path=self.DBUS_PATH, |
| 334 |
dbus_interface=self.DBUS_IFACE) |
| 335 |
except dbus.DBusException as e: |
| 336 |
try: |
| 337 |
print ("%s: failed to add D-Bus signal receiver: %s" \ |
| 338 |
% (PROGRAM_NAME, e), file=sys.stderr) |
| 339 |
finally: |
| 340 |
sys.exit (1) |
| 341 |
|
| 342 |
self.check_for_jobs () |
| 343 |
|
| 344 |
def remove_signal_receiver (self): |
| 345 |
try: |
| 346 |
self.system_bus.remove_signal_receiver (self.handle_dbus_signal, |
| 347 |
path=self.DBUS_PATH, |
| 348 |
dbus_interface=self.DBUS_IFACE) |
| 349 |
except dbus.DBusException as e: |
| 350 |
try: |
| 351 |
print ("%s: failed to remove D-Bus signal receiver: %s" \ |
| 352 |
% (PROGRAM_NAME, e), file=sys.stderr) |
| 353 |
except: |
| 354 |
pass |
| 355 |
|
| 356 |
def run (self): |
| 357 |
self.loop.run () |
| 358 |
|
| 359 |
def __del__ (self): |
| 360 |
self.remove_signal_receiver () |
| 361 |
if self.timer: |
| 362 |
GLib.source_remove (self.timer) |
| 363 |
|
| 364 |
def handle_dbus_signal (self, *args): |
| 365 |
if self.timer: |
| 366 |
GLib.source_remove (self.timer) |
| 367 |
self.timer = GLib.timeout_add (200, self.check_for_jobs) |
| 368 |
|
| 369 |
def check_for_jobs (self, *args): |
| 370 |
debugprint ("checking for jobs") |
| 371 |
if any_jobs (): |
| 372 |
if self.timer is not None: |
| 373 |
GLib.source_remove (self.timer) |
| 374 |
|
| 375 |
self.remove_signal_receiver () |
| 376 |
|
| 377 |
# Start the job applet. |
| 378 |
debugprint ("Starting job applet") |
| 379 |
try: |
| 380 |
obj = self.session_bus.get_object (PRINTING_BUS, PRINTING_PATH) |
| 381 |
iface = dbus.Interface (obj, PRINTING_IFACE) |
| 382 |
path = iface.JobApplet () |
| 383 |
debugprint ("Job applet is %s" % path) |
| 384 |
except dbus.DBusException as e: |
| 385 |
try: |
| 386 |
print(e) |
| 387 |
except: |
| 388 |
pass |
| 389 |
|
| 390 |
# Don't run this timer again. |
| 391 |
return False |
| 392 |
|
| 393 |
if __name__ == '__main__': |
| 394 |
import sys, getopt |
| 395 |
try: |
| 396 |
opts, args = getopt.gnu_getopt (sys.argv[1:], '', |
| 397 |
['debug', |
| 398 |
'help', |
| 399 |
'version']) |
| 400 |
except getopt.GetoptError: |
| 401 |
show_help () |
| 402 |
sys.exit (1) |
| 403 |
|
| 404 |
for opt, optarg in opts: |
| 405 |
if opt == "--help": |
| 406 |
show_help () |
| 407 |
sys.exit (0) |
| 408 |
if opt == "--version": |
| 409 |
show_version () |
| 410 |
sys.exit (0) |
| 411 |
elif opt == "--debug": |
| 412 |
set_debugging (True) |
| 413 |
|
| 414 |
# Must be done before connecting to D-Bus (for some reason). |
| 415 |
if not Notify.init (PROGRAM_NAME): |
| 416 |
try: |
| 417 |
print(("%s: unable to initialize pynotify" % |
| 418 |
PROGRAM_NAME), file=sys.stderr) |
| 419 |
except: |
| 420 |
pass |
| 421 |
|
| 422 |
system_bus = session_bus = None |
| 423 |
try: |
| 424 |
system_bus = dbus.SystemBus() |
| 425 |
except: |
| 426 |
try: |
| 427 |
print(("%s: failed to connect to system D-Bus" % |
| 428 |
PROGRAM_NAME), file=sys.stderr) |
| 429 |
finally: |
| 430 |
sys.exit (1) |
| 431 |
|
| 432 |
try: |
| 433 |
session_bus = dbus.SessionBus() |
| 434 |
# Stop running when the session ends. |
| 435 |
session_bus.add_signal_receiver (monitor_session) |
| 436 |
except: |
| 437 |
try: |
| 438 |
print(("%s: failed to connect to " |
| 439 |
"session D-Bus" % PROGRAM_NAME), file=sys.stderr) |
| 440 |
finally: |
| 441 |
sys.exit (1) |
| 442 |
|
| 443 |
try: |
| 444 |
NewPrinterNotification(system_bus, session_bus) |
| 445 |
except: |
| 446 |
try: |
| 447 |
print(("%s: failed to start " |
| 448 |
"NewPrinterNotification service" % |
| 449 |
PROGRAM_NAME), file=sys.stderr) |
| 450 |
except: |
| 451 |
pass |
| 452 |
|
| 453 |
try: |
| 454 |
cupshelpers.installdriver.set_debugprint_fn (debugprint) |
| 455 |
cupshelpers.installdriver.PrinterDriversInstaller(system_bus) |
| 456 |
except Exception as e: |
| 457 |
try: |
| 458 |
print(("%s: failed to start " |
| 459 |
"PrinterDriversInstaller service: " |
| 460 |
"%s" % (PROGRAM_NAME, e)), file=sys.stderr) |
| 461 |
except: |
| 462 |
pass |
| 463 |
|
| 464 |
try: |
| 465 |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 466 |
# Create an abstract socket, by prefixing it with null. |
| 467 |
sock.bind("\0printer-applet-lock-user-" + getpass.getuser()) |
| 468 |
except socket.error as err: |
| 469 |
error_code = err.args[0] |
| 470 |
error_string = err.args[1] |
| 471 |
print("Process already running ({0}:{1}). Exiting.".format(error_code, error_string)) |
| 472 |
sys.exit(0) |
| 473 |
|
| 474 |
loop = GLib.MainLoop () |
| 475 |
runloop = RunLoop (session_bus, system_bus, loop) |
| 476 |
try: |
| 477 |
runloop.run () |
| 478 |
except KeyboardInterrupt: |
| 479 |
pass |