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
|
#!/usr/bin/env python
## Copyright (C) 2007, 2008, 2009, 2010 Red Hat, Inc.
## Author: 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., 675 Mass Ave, Cambridge, MA 02139, USA.
import cups
cups.require ("1.9.42")
import sys
sys.path.append("/usr/share/system-config-printer")
from debug import *
import dbus
import dbus.glib
import dbus.service
import gobject
import pynotify
import time
import locale
import gettext
import installdriver
from gettext import gettext as _
DOMAIN="system-config-printer"
gettext.textdomain (DOMAIN)
try:
locale.setlocale (locale.LC_ALL, "")
except locale.Error, e:
import os
os.environ['LC_ALL'] = 'C'
locale.setlocale (locale.LC_ALL, "")
APPDIR="/usr/share/system-config-printer"
DOMAIN="system-config-printer"
ICON="printer"
SEARCHING_ICON="document-print-preview"
# Let gobject know we'll be using threads.
gobject.threads_init ()
# We need to call pynotify.init before we can check the server for caps
pynotify.init('System Config Printer Notification')
####
#### NewPrinterNotification DBus server (the 'new' way).
####
PDS_PATH="/com/redhat/NewPrinterNotification"
PDS_IFACE="com.redhat.NewPrinterNotification"
PDS_OBJ="com.redhat.NewPrinterNotification"
class NewPrinterNotification(dbus.service.Object):
STATUS_SUCCESS = 0
STATUS_MODEL_MISMATCH = 1
STATUS_GENERIC_DRIVER = 2
STATUS_NO_DRIVER = 3
def __init__ (self, bus):
self.bus = bus
self.getting_ready = 0
bus_name = dbus.service.BusName (PDS_OBJ, bus=bus)
dbus.service.Object.__init__ (self, bus_name, PDS_PATH)
def wake_up (self):
global waitloop, runloop, viewer
import jobviewer
if viewer == None:
try:
waitloop.quit ()
except:
pass
runloop = gobject.MainLoop ()
viewer = jobviewer.JobViewer(bus=bus, loop=runloop,
trayicon=trayicon,
suppress_icon_hide=True)
@dbus.service.method(PDS_IFACE, in_signature='', out_signature='')
def GetReady (self):
self.wake_up ()
if self.getting_ready == 0:
viewer.set_special_statusicon (SEARCHING_ICON,
tooltip=_("Configuring new printer"))
self.getting_ready += 1
gobject.timeout_add_seconds (1200, self.timeout_ready)
def timeout_ready (self):
global viewer
if self.getting_ready > 0:
self.getting_ready -= 1
if self.getting_ready == 0:
viewer.unset_special_statusicon ()
return False
@dbus.service.method(PDS_IFACE, in_signature='isssss', out_signature='')
def NewPrinter (self, status, name, mfg, mdl, des, cmd):
global viewer
self.wake_up ()
if name.find("/") >= 0:
# name is a URI, no queue was generated, because no suitable
# driver was found
title = _("Missing printer driver")
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd)
if (mfg and mdl) or des:
if (mfg and mdl):
device = "%s %s" % (mfg, mdl)
else:
device = des
text = _("No printer driver for %s.") % device
else:
text = _("No driver for this printer.")
n = pynotify.Notification (title, text, 'printer')
if "actions" in pynotify.get_server_caps():
n.set_urgency (pynotify.URGENCY_CRITICAL)
n.set_timeout (pynotify.EXPIRES_NEVER)
n.add_action ("setup-printer", _("Search"),
lambda x, y:
self.setup_printer (x, y, name, devid))
else:
args = ["--setup-printer", name, "--devid", devid]
self.run_config_tool (args)
else:
# name is the name of the queue which hal_lpadmin has set up
# automatically.
c = cups.Connection ()
try:
printer = c.getPrinters ()[name]
except KeyError:
return
try:
filename = c.getPPD (name)
except cups.IPPError:
return
del c
# Check for missing packages
cups.ppdSetConformance (cups.PPD_CONFORM_RELAXED)
ppd = cups.PPD (filename)
import os
os.unlink (filename)
import sys
sys.path.append (APPDIR)
import cupshelpers
(missing_pkgs,
missing_exes) = cupshelpers.missingPackagesAndExecutables (ppd)
from cupshelpers.ppds import ppdMakeModelSplit
(make, model) = ppdMakeModelSplit (printer['printer-make-and-model'])
driver = make + " " + model
if status < self.STATUS_GENERIC_DRIVER:
title = _("Printer added")
else:
title = _("Missing printer driver")
if len (missing_pkgs) > 0:
pkgs = reduce (lambda x,y: x + ", " + y, missing_pkgs)
title = _("Install printer driver")
text = _("`%s' requires driver installation: %s.") % (name, pkgs)
n = pynotify.Notification (title, text)
import installpackage
if "actions" in pynotify.get_server_caps():
try:
self.packagekit = installpackage.PackageKit ()
n.set_timeout (pynotify.EXPIRES_NEVER)
n.add_action ("install-driver", _("Install"),
lambda x, y:
self.install_driver (x, y,
missing_pkgs))
except:
pass
else:
try:
self.packagekit = installpackage.PackageKit ()
self.packagekit.InstallPackageName (0, 0,
missing_pkgs[0])
except:
pass
elif status == self.STATUS_SUCCESS:
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd)
text = _("`%s' is ready for printing.") % name
n = pynotify.Notification (title, text)
if "actions" in pynotify.get_server_caps():
n.set_urgency (pynotify.URGENCY_NORMAL)
n.add_action ("test-page", _("Print test page"),
lambda x, y:
self.print_test_page (x, y, name, devid))
n.add_action ("configure", _("Configure"),
lambda x, y: self.configure (x, y, name))
else: # Model mismatch
devid = "MFG:%s;MDL:%s;DES:%s;CMD:%s;" % (mfg, mdl, des, cmd)
text = (_("`%s' has been added, using the `%s' driver.") %
(name, driver))
n = pynotify.Notification (title, text, 'printer')
if "actions" in pynotify.get_server_caps():
n.set_urgency (pynotify.URGENCY_CRITICAL)
n.add_action ("test-page", _("Print test page"),
lambda x, y:
self.print_test_page (x, y, name, devid))
n.add_action ("find-driver", _("Find driver"),
lambda x, y:
self.find_driver (x, y, name, devid))
n.set_timeout (pynotify.EXPIRES_NEVER)
else:
self.run_config_tool (["--configure-printer",
name, "--no-focus-on-map"])
viewer.notify_new_printer (name, n)
# Set the icon back how it was.
self.timeout_ready ()
def run_config_tool (self, argv):
import os
pid = os.fork ()
if pid == 0:
# Child.
cmd = "/usr/bin/system-config-printer"
argv.insert (0, cmd)
os.execvp (cmd, argv)
sys.exit (1)
elif pid == -1:
print "Error forking process"
else:
gobject.timeout_add_seconds (60, self.collect_exit_code, pid)
def print_test_page (self, notification, action, name, devid = ""):
args = ["--print-test-page", name]
if devid != "":
args.extend (["--devid", devid])
self.run_config_tool (args)
def configure (self, notification, action, name):
self.run_config_tool (["--configure-printer", name])
def find_driver (self, notification, action, name, devid = ""):
args = ["--choose-driver", name]
if devid != "": args = args + ["--devid", devid]
self.run_config_tool (args)
def setup_printer (self, notification, action, uri, devid = ""):
args = ["--setup-printer", uri]
if devid != "": args = args + ["--devid", devid]
self.run_config_tool (args)
def install_driver (self, notification, action, missing_pkgs):
try:
self.packagekit.InstallPackageName (0, 0, missing_pkgs[0])
except:
pass
def collect_exit_code (self, pid):
# We do this with timers instead of signals because we already
# have gobject imported, but don't (yet) import signal;
# let's try not to inflate the process size.
import os
try:
print "Waiting for child %d" % pid
(pid, status) = os.waitpid (pid, os.WNOHANG)
if pid == 0:
# Run this timer again.
return True
except OSError:
pass
return False
PROGRAM_NAME="system-config-printer-applet"
def show_help ():
print "usage: %s [--no-tray-icon]" % PROGRAM_NAME
def show_version ():
import config
print "%s %s" % (PROGRAM_NAME, config.VERSION)
####
#### Main program entry
####
global waitloop, runloop, viewer
trayicon = True
waitloop = runloop = None
viewer = None
if __name__ == '__main__':
import sys, getopt
try:
opts, args = getopt.gnu_getopt (sys.argv[1:], '',
['no-tray-icon',
'debug',
'help',
'version'])
except getopt.GetoptError:
show_help ()
sys.exit (1)
for opt, optarg in opts:
if opt == "--help":
show_help ()
sys.exit (0)
if opt == "--version":
show_version ()
sys.exit (0)
if opt == "--no-tray-icon":
trayicon = False
elif opt == "--debug":
set_debugging (True)
# Must be done before connecting to D-Bus (for some reason).
if not pynotify.init (PROGRAM_NAME):
try:
print >> sys.stderr, ("%s: unable to initialize pynotify" %
PROGRAM_NAME)
except:
pass
try:
bus = dbus.SystemBus()
except:
try:
print >> sys.stderr, ("%s: failed to connect to system D-Bus" %
PROGRAM_NAME)
finally:
sys.exit (1)
if trayicon:
try:
NewPrinterNotification(bus)
except:
try:
print >> sys.stderr, ("%s: failed to start "
"NewPrinterNotification service" %
PROGRAM_NAME)
except:
pass
try:
installdriver.PrinterDriversInstaller(bus)
except Exception, e:
try:
print >> sys.stderr, ("%s: failed to start "
"PrinterDriversInstaller service: %s" %
(PROGRAM_NAME, e))
except:
pass
if trayicon and get_debugging () == False:
# Start off just waiting for print jobs.
def any_jobs ():
try:
c = cups.Connection ()
jobs = c.getJobs (my_jobs=True, limit=1)
if len (jobs):
return True
except:
pass
return False
if not any_jobs ():
###
class WaitForJobs:
DBUS_PATH="/com/redhat/PrinterSpooler"
DBUS_IFACE="com.redhat.PrinterSpooler"
def __init__ (self, bus, waitloop):
self.bus = bus
self.waitloop = waitloop
self.timer = None
bus.add_signal_receiver (self.handle_dbus_signal,
path=self.DBUS_PATH,
dbus_interface=self.DBUS_IFACE)
def __del__ (self):
bus = self.bus
bus.remove_signal_receiver (self.handle_dbus_signal,
path=self.DBUS_PATH,
dbus_interface=self.DBUS_IFACE)
if self.timer:
gobject.source_remove (self.timer)
def handle_dbus_signal (self, *args):
if self.timer:
gobject.source_remove (self.timer)
self.timer = gobject.timeout_add (200, self.check_for_jobs)
def check_for_jobs (self, *args):
debugprint ("checking for jobs")
if any_jobs ():
gobject.source_remove (self.timer)
self.waitloop.quit ()
# Don't run this timer again.
return False
###
waitloop = gobject.MainLoop ()
jobwaiter = WaitForJobs(bus, waitloop)
waitloop.run()
del jobwaiter
waitloop = None
if viewer == None:
import jobviewer
import gtk
runloop = gobject.MainLoop ()
gtk.window_set_default_icon_name ('printer')
viewer = jobviewer.JobViewer(bus=bus, loop=runloop,
trayicon=trayicon)
try:
runloop.run()
except KeyboardInterrupt:
pass
viewer.cleanup ()
|