| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## system-config-printer |
| 4 |
|
| 5 |
## Copyright (C) 2010, 2011, 2012, 2013, 2014 Red Hat, Inc. |
| 6 |
## Author: Tim Waugh <[email protected]> |
| 7 |
|
| 8 |
## This program is free software; you can redistribute it and/or modify |
| 9 |
## it under the terms of the GNU General Public License as published by |
| 10 |
## the Free Software Foundation; either version 2 of the License, or |
| 11 |
## (at your option) any later version. |
| 12 |
|
| 13 |
## This program is distributed in the hope that it will be useful, |
| 14 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
## GNU General Public License for more details. |
| 17 |
|
| 18 |
## You should have received a copy of the GNU General Public License |
| 19 |
## along with this program; if not, write to the Free Software |
| 20 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
|
| 22 |
import dbus |
| 23 |
from gi.repository import GObject |
| 24 |
from gi.repository import Gtk |
| 25 |
import cupshelpers |
| 26 |
|
| 27 |
import cups |
| 28 |
cups.require ("1.9.52") |
| 29 |
|
| 30 |
import asyncconn |
| 31 |
from debug import debugprint |
| 32 |
import config |
| 33 |
import gettext |
| 34 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 35 |
|
| 36 |
class PPDsLoader(GObject.GObject): |
| 37 |
""" |
| 38 |
1. If PackageKit support is available, and this is a local server, |
| 39 |
try to use PackageKit to install relevant drivers. We do this |
| 40 |
because we can only make the right choice about the "best" driver |
| 41 |
when the full complement of drivers is there to choose from. |
| 42 |
|
| 43 |
2. Fetch the list of available drivers from CUPS. |
| 44 |
|
| 45 |
3. If Jockey is available, and there is no appropriate driver |
| 46 |
available, try to use Jockey to install one. |
| 47 |
|
| 48 |
4. If Jockey was able to install one, fetch the list of available |
| 49 |
drivers again. |
| 50 |
""" |
| 51 |
|
| 52 |
__gsignals__ = { |
| 53 |
'finished': (GObject.SignalFlags.RUN_LAST, None, ()) |
| 54 |
} |
| 55 |
|
| 56 |
def __init__ (self, device_id=None, parent=None, device_uri=None, |
| 57 |
host=None, encryption=None, language=None, |
| 58 |
device_make_and_model=None): |
| 59 |
GObject.GObject.__init__ (self) |
| 60 |
debugprint ("+%s" % self) |
| 61 |
self._device_id = device_id |
| 62 |
self._device_uri = device_uri |
| 63 |
self._device_make_and_model = device_make_and_model |
| 64 |
self._parent = parent |
| 65 |
self._host = host |
| 66 |
self._encryption = encryption |
| 67 |
self._language = language |
| 68 |
|
| 69 |
self._installed_files = [] |
| 70 |
self._conn = None |
| 71 |
self._ppds = None |
| 72 |
self._exc = None |
| 73 |
|
| 74 |
self._ppdsmatch_result = None |
| 75 |
self._jockey_queried = False |
| 76 |
self._jockey_has_answered = False |
| 77 |
self._local_cups = (self._host is None or |
| 78 |
self._host == "localhost" or |
| 79 |
self._host[0] == '/') |
| 80 |
try: |
| 81 |
self._bus = dbus.SessionBus () |
| 82 |
except: |
| 83 |
debugprint ("Failed to get session bus") |
| 84 |
self._bus = None |
| 85 |
|
| 86 |
fmt = _("Searching") |
| 87 |
self._dialog = Gtk.MessageDialog (parent=parent, |
| 88 |
modal=True, destroy_with_parent=True, |
| 89 |
message_type=Gtk.MessageType.INFO, |
| 90 |
buttons=Gtk.ButtonsType.CANCEL, |
| 91 |
text=fmt) |
| 92 |
|
| 93 |
self._dialog.format_secondary_text (_("Searching for drivers")) |
| 94 |
|
| 95 |
self._dialog.connect ("response", self._dialog_response) |
| 96 |
|
| 97 |
def run (self): |
| 98 |
self._dialog.show_all () |
| 99 |
|
| 100 |
if self._device_id: |
| 101 |
self._devid_dict = cupshelpers.parseDeviceID (self._device_id) |
| 102 |
|
| 103 |
if (self._local_cups and |
| 104 |
self._device_id and |
| 105 |
self._devid_dict["MFG"] and |
| 106 |
self._devid_dict["MDL"] and |
| 107 |
self._bus): |
| 108 |
self._gpk_device_id = "MFG:%s;MDL:%s;" % (self._devid_dict["MFG"], |
| 109 |
self._devid_dict["MDL"]) |
| 110 |
self._query_packagekit () |
| 111 |
else: |
| 112 |
self._query_cups () |
| 113 |
|
| 114 |
def __del__ (self): |
| 115 |
debugprint ("-%s" % self) |
| 116 |
|
| 117 |
def destroy (self): |
| 118 |
debugprint ("DESTROY: %s" % self) |
| 119 |
if self._dialog: |
| 120 |
self._dialog.destroy () |
| 121 |
self._dialog = None |
| 122 |
|
| 123 |
self._parent = None |
| 124 |
|
| 125 |
if self._conn: |
| 126 |
self._conn.destroy () |
| 127 |
self._conn = None |
| 128 |
|
| 129 |
def get_installed_files (self): |
| 130 |
return self._installed_files |
| 131 |
|
| 132 |
def get_ppds (self): |
| 133 |
return self._ppds |
| 134 |
|
| 135 |
def get_ppdsmatch_result (self): |
| 136 |
return self._ppdsmatch_result |
| 137 |
|
| 138 |
def get_error (self): |
| 139 |
debugprint ("%s: stored error is %s" % (self, repr (self._exc))) |
| 140 |
return self._exc |
| 141 |
|
| 142 |
def _dialog_response (self, dialog, response): |
| 143 |
dialog.destroy () |
| 144 |
self._dialog = None |
| 145 |
self.emit ('finished') |
| 146 |
|
| 147 |
def _query_cups (self): |
| 148 |
debugprint ("Asking CUPS for PPDs") |
| 149 |
if (not self._conn): |
| 150 |
c = asyncconn.Connection (host=self._host, |
| 151 |
encryption=self._encryption, |
| 152 |
reply_handler=self._cups_connect_reply, |
| 153 |
error_handler=self._cups_error) |
| 154 |
self._conn = c |
| 155 |
else: |
| 156 |
self._cups_connect_reply(self._conn, None) |
| 157 |
|
| 158 |
def _cups_connect_reply (self, conn, UNUSED): |
| 159 |
conn._begin_operation (_("fetching PPDs")) |
| 160 |
conn.getPPDs2 (reply_handler=self._cups_reply, |
| 161 |
error_handler=self._cups_error) |
| 162 |
|
| 163 |
def _cups_reply (self, conn, result): |
| 164 |
ppds = cupshelpers.ppds.PPDs (result, language=self._language) |
| 165 |
self._ppds = ppds |
| 166 |
self._need_requery_cups = False |
| 167 |
if self._device_id: |
| 168 |
fit = ppds.\ |
| 169 |
getPPDNamesFromDeviceID (self._devid_dict["MFG"], |
| 170 |
self._devid_dict["MDL"], |
| 171 |
self._devid_dict["DES"], |
| 172 |
self._devid_dict["CMD"], |
| 173 |
self._device_uri, |
| 174 |
self._device_make_and_model) |
| 175 |
|
| 176 |
ppdnamelist = ppds.\ |
| 177 |
orderPPDNamesByPreference (list(fit.keys ()), |
| 178 |
self._installed_files, |
| 179 |
devid=self._devid_dict, |
| 180 |
fit=fit) |
| 181 |
self._ppdsmatch_result = (fit, ppdnamelist) |
| 182 |
|
| 183 |
ppdname = ppdnamelist[0] |
| 184 |
if (self._bus and |
| 185 |
not fit[ppdname].startswith ("exact") and |
| 186 |
not self._jockey_queried and |
| 187 |
self._devid_dict["MFG"] and |
| 188 |
self._devid_dict["MDL"] and |
| 189 |
self._local_cups): |
| 190 |
# Try to install packages using jockey if |
| 191 |
# - there's no appropriate driver (PPD) locally available |
| 192 |
# - we are configuring local CUPS server |
| 193 |
self._jockey_queried = True |
| 194 |
self._query_jockey () |
| 195 |
return |
| 196 |
|
| 197 |
conn.destroy () |
| 198 |
self._conn = None |
| 199 |
if self._dialog is not None: |
| 200 |
self._dialog.destroy () |
| 201 |
self._dialog = None |
| 202 |
|
| 203 |
self.emit ('finished') |
| 204 |
|
| 205 |
def _cups_error (self, conn, exc): |
| 206 |
conn.destroy () |
| 207 |
self._conn = None |
| 208 |
self._ppds = None |
| 209 |
self._exc = exc |
| 210 |
if self._dialog is not None: |
| 211 |
self._dialog.destroy () |
| 212 |
self._dialog = None |
| 213 |
|
| 214 |
self.emit ('finished') |
| 215 |
|
| 216 |
def _query_packagekit (self): |
| 217 |
debugprint ("Asking PackageKit to install drivers") |
| 218 |
try: |
| 219 |
obj = self._bus.get_object ("org.freedesktop.PackageKit", |
| 220 |
"/org/freedesktop/PackageKit") |
| 221 |
proxy = dbus.Interface (obj, "org.freedesktop.PackageKit.Modify") |
| 222 |
resources = [self._gpk_device_id] |
| 223 |
interaction = "hide-finished" |
| 224 |
debugprint ("Calling InstallPrinterDrivers (%s, %s, %s)" % |
| 225 |
(repr (0), repr (resources), repr (interaction))) |
| 226 |
proxy.InstallPrinterDrivers (dbus.UInt32 (0), |
| 227 |
resources, interaction, |
| 228 |
reply_handler=self._packagekit_reply, |
| 229 |
error_handler=self._packagekit_error, |
| 230 |
timeout=3600) |
| 231 |
except Exception as e: |
| 232 |
debugprint ("Failed to talk to PackageKit: %s" % repr (e)) |
| 233 |
if self._dialog: |
| 234 |
self._dialog.show_all () |
| 235 |
self._query_cups () |
| 236 |
|
| 237 |
def _packagekit_reply (self): |
| 238 |
debugprint ("Got PackageKit reply") |
| 239 |
self._need_requery_cups = True |
| 240 |
if self._dialog: |
| 241 |
self._dialog.show_all () |
| 242 |
self._query_cups () |
| 243 |
|
| 244 |
def _packagekit_error (self, exc): |
| 245 |
debugprint ("Got PackageKit error: %s" % repr (exc)) |
| 246 |
if self._dialog: |
| 247 |
self._dialog.show_all () |
| 248 |
self._query_cups () |
| 249 |
|
| 250 |
def _query_jockey (self): |
| 251 |
debugprint ("Asking Jockey to install drivers") |
| 252 |
try: |
| 253 |
obj = self._bus.get_object ("com.ubuntu.DeviceDriver", "/GUI") |
| 254 |
jockey = dbus.Interface (obj, "com.ubuntu.DeviceDriver") |
| 255 |
r = jockey.search_driver ("printer_deviceid:%s" % self._device_id, |
| 256 |
reply_handler=self._jockey_reply, |
| 257 |
error_handler=self._jockey_error, |
| 258 |
timeout=3600) |
| 259 |
except Exception as e: |
| 260 |
self._jockey_error (e) |
| 261 |
|
| 262 |
def _jockey_reply (self, conn, result): |
| 263 |
debugprint ("Got Jockey result: %s" % repr (result)) |
| 264 |
self._jockey_has_answered = True |
| 265 |
try: |
| 266 |
self._installed_files = result[1] |
| 267 |
except: |
| 268 |
self._installed_files = [] |
| 269 |
self._query_cups () |
| 270 |
|
| 271 |
def _jockey_error (self, exc): |
| 272 |
debugprint ("Got Jockey error: %s" % repr (exc)) |
| 273 |
if self._need_requery_cups: |
| 274 |
self._query_cups () |
| 275 |
else: |
| 276 |
if self._conn is not None: |
| 277 |
self._conn.destroy () |
| 278 |
self._conn = None |
| 279 |
|
| 280 |
if self._dialog is not None: |
| 281 |
self._dialog.destroy () |
| 282 |
self._dialog = None |
| 283 |
|
| 284 |
self.emit ('finished') |
| 285 |
|
| 286 |
if __name__ == "__main__": |
| 287 |
class Foo: |
| 288 |
def __init__ (self): |
| 289 |
w = Gtk.Window () |
| 290 |
b = Gtk.Button.new_with_label ("Go") |
| 291 |
w.add (b) |
| 292 |
b.connect ('clicked', self.go) |
| 293 |
w.connect ('delete-event', Gtk.main_quit) |
| 294 |
w.show_all () |
| 295 |
self._window = w |
| 296 |
|
| 297 |
def go (self, button): |
| 298 |
loader = PPDsLoader (device_id="MFG:MFG;MDL:MDL;", |
| 299 |
parent=self._window) |
| 300 |
loader.connect ('finished', self.ppds_loaded) |
| 301 |
loader.run () |
| 302 |
|
| 303 |
def ppds_loaded (self, ppdsloader): |
| 304 |
self._window.destroy () |
| 305 |
Gtk.main_quit () |
| 306 |
exc = ppdsloader.get_error () |
| 307 |
print(exc) |
| 308 |
ppds = ppdsloader.get_ppds () |
| 309 |
if ppds is not None: |
| 310 |
print(len (ppds)) |
| 311 |
|
| 312 |
ppdsloader.destroy () |
| 313 |
|
| 314 |
from debug import set_debugging |
| 315 |
set_debugging (True) |
| 316 |
Foo () |
| 317 |
Gtk.main () |