| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## system-config-printer |
| 4 |
|
| 5 |
## Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
## Florian Festi <[email protected]> |
| 9 |
|
| 10 |
## This program is free software; you can redistribute it and/or modify |
| 11 |
## it under the terms of the GNU General Public License as published by |
| 12 |
## the Free Software Foundation; either version 2 of the License, or |
| 13 |
## (at your option) any later version. |
| 14 |
|
| 15 |
## This program is distributed in the hope that it will be useful, |
| 16 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
## GNU General Public License for more details. |
| 19 |
|
| 20 |
## You should have received a copy of the GNU General Public License |
| 21 |
## along with this program; if not, write to the Free Software |
| 22 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 |
|
| 24 |
# config is generated from config.py.in by configure |
| 25 |
import config |
| 26 |
|
| 27 |
import authconn |
| 28 |
import cupshelpers |
| 29 |
from OpenPrintingRequest import OpenPrintingRequest |
| 30 |
|
| 31 |
import errno |
| 32 |
import sys, os, tempfile, time, re, http.client |
| 33 |
import locale |
| 34 |
import string |
| 35 |
import subprocess |
| 36 |
from timedops import * |
| 37 |
import dbus |
| 38 |
from gi.repository import Gdk |
| 39 |
from gi.repository import Gtk |
| 40 |
import functools |
| 41 |
|
| 42 |
import cups |
| 43 |
|
| 44 |
try: |
| 45 |
import pysmb |
| 46 |
PYSMB_AVAILABLE=True |
| 47 |
except: |
| 48 |
PYSMB_AVAILABLE=False |
| 49 |
|
| 50 |
import options |
| 51 |
from gi.repository import GObject |
| 52 |
from gi.repository import GLib |
| 53 |
from gui import GtkGUI |
| 54 |
from optionwidgets import OptionWidget |
| 55 |
from debug import * |
| 56 |
import probe_printer |
| 57 |
import urllib.request, urllib.parse |
| 58 |
from smburi import SMBURI |
| 59 |
from errordialogs import * |
| 60 |
from PhysicalDevice import PhysicalDevice |
| 61 |
import firewallsettings |
| 62 |
import asyncconn |
| 63 |
import ppdsloader |
| 64 |
import dnssdresolve |
| 65 |
import installpackage |
| 66 |
|
| 67 |
import gettext |
| 68 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 69 |
|
| 70 |
HTTPS_TIMEOUT = 15.0 |
| 71 |
|
| 72 |
TEXT_adjust_firewall = _("The firewall may need adjusting in order to " |
| 73 |
"detect network printers. Adjust the " |
| 74 |
"firewall now?") |
| 75 |
|
| 76 |
def validDeviceURI (uri): |
| 77 |
"""Returns True is the provided URI is valid.""" |
| 78 |
(scheme, rest) = urllib.parse.splittype (uri) |
| 79 |
if scheme is None or scheme == '': |
| 80 |
return False |
| 81 |
return True |
| 82 |
|
| 83 |
# Both the printer properties window and the new printer window |
| 84 |
# need to be able to drive 'class members' selections. |
| 85 |
def moveClassMembers(treeview_from, treeview_to): |
| 86 |
selection = treeview_from.get_selection() |
| 87 |
model_from, rows = selection.get_selected_rows() |
| 88 |
rows = [Gtk.TreeRowReference.new(model_from, row) for row in rows] |
| 89 |
|
| 90 |
model_to = treeview_to.get_model() |
| 91 |
|
| 92 |
for row in rows: |
| 93 |
path = row.get_path() |
| 94 |
iter = model_from.get_iter(path) |
| 95 |
row_data = model_from.get(iter, 0) |
| 96 |
model_to.append(row_data) |
| 97 |
model_from.remove(iter) |
| 98 |
|
| 99 |
def getCurrentClassMembers(treeview): |
| 100 |
model = treeview.get_model() |
| 101 |
iter = model.get_iter_first() |
| 102 |
result = [] |
| 103 |
while iter: |
| 104 |
result.append(model.get(iter, 0)[0]) |
| 105 |
iter = model.iter_next(iter) |
| 106 |
result.sort() |
| 107 |
return result |
| 108 |
|
| 109 |
def checkNPName(printers, name): |
| 110 |
if not name: return False |
| 111 |
name = name.lower() |
| 112 |
for printer in printers.values(): |
| 113 |
if not printer.discovered and printer.name.lower()==name: |
| 114 |
return False |
| 115 |
return True |
| 116 |
|
| 117 |
def ready (win, cursor=None): |
| 118 |
try: |
| 119 |
gdkwin = win.get_window() |
| 120 |
if gdkwin: |
| 121 |
gdkwin.set_cursor (cursor) |
| 122 |
while Gtk.events_pending (): |
| 123 |
Gtk.main_iteration () |
| 124 |
except: |
| 125 |
nonfatalException () |
| 126 |
|
| 127 |
def busy (win): |
| 128 |
ready (win, Gdk.Cursor.new(Gdk.CursorType.WATCH)) |
| 129 |
|
| 130 |
def on_delete_just_hide (widget, event): |
| 131 |
widget.hide () |
| 132 |
return True # stop other handlers |
| 133 |
|
| 134 |
def _singleton (x): |
| 135 |
"""If we don't know whether getPPDs() or getPPDs2() was used, this |
| 136 |
function can unwrap an item from a list in either case.""" |
| 137 |
if isinstance (x, list): |
| 138 |
return x[0] |
| 139 |
return x |
| 140 |
|
| 141 |
def download_gpg_fingerprint(url): |
| 142 |
"""Get GPG fingerprint from URL. |
| 143 |
|
| 144 |
Check that the URL is HTTPS with a valid and trusted server |
| 145 |
certificate, read it, extract the GPG fingerprint from it, and return |
| 146 |
it. Return None if the URL is invalid, not trusted, or the fingerprint |
| 147 |
can't be found. |
| 148 |
""" |
| 149 |
if not url.startswith('https://'): |
| 150 |
debugprint('Not a https fingerprint URL: %s, ignoring driver' % url) |
| 151 |
return None |
| 152 |
|
| 153 |
content = None |
| 154 |
try: |
| 155 |
with urllib.request.urlopen(url, timeout=HTTPS_TIMEOUT) as resp: |
| 156 |
if resp.status == 200: |
| 157 |
content = resp.read().decode('utf-8') |
| 158 |
except Exception as e: |
| 159 |
debugprint('Cannot retrieve %s: %s' % (url, e)) |
| 160 |
|
| 161 |
if content is None: |
| 162 |
debugprint('Cannot retrieve %s' % url) |
| 163 |
return None |
| 164 |
|
| 165 |
keyid_re = re.compile(r' ((?:(?:[0-9A-F]{4})(?:\s+|$)){10})$', re.M) |
| 166 |
|
| 167 |
m = keyid_re.search(content) |
| 168 |
if m: |
| 169 |
return m.group(1).strip().replace(' ','') |
| 170 |
|
| 171 |
return None |
| 172 |
|
| 173 |
class NewPrinterGUI(GtkGUI): |
| 174 |
|
| 175 |
__gsignals__ = { |
| 176 |
'destroy': (GObject.SignalFlags.RUN_LAST, None, ()), |
| 177 |
'printer-added' : (GObject.SignalFlags.RUN_LAST, None, (str,)), |
| 178 |
'printer-modified': (GObject.SignalFlags.RUN_LAST, None, |
| 179 |
(str, # printer name |
| 180 |
bool,)), # PPD modified? |
| 181 |
'driver-download-checked': (GObject.SignalFlags.RUN_LAST, None, (str,)), |
| 182 |
'dialog-canceled': (GObject.SignalFlags.RUN_LAST, None, ()), |
| 183 |
} |
| 184 |
|
| 185 |
# Page numbers used to name the different stages |
| 186 |
PAGE_DESCRIBE_PRINTER = 0 |
| 187 |
PAGE_SELECT_DEVICE = 1 |
| 188 |
PAGE_SELECT_INSTALL_METHOD = 2 |
| 189 |
PAGE_CHOOSE_DRIVER_FROM_DB = 3 |
| 190 |
PAGE_CHOOSE_CLASS_MEMBERS = 4 |
| 191 |
PAGE_APPLY = 5 |
| 192 |
PAGE_INSTALLABLE_OPTIONS = 6 |
| 193 |
PAGE_DOWNLOAD_DRIVER = 7 |
| 194 |
|
| 195 |
# Values returned by _handlePrinterInstallationMode() and |
| 196 |
# related sub-functions, to know whether to continue the |
| 197 |
# execution of the remaining logic from calling functions |
| 198 |
INSTALL_RESULT_DONE = True |
| 199 |
INSTALL_RESULT_OPS_PENDING = False |
| 200 |
|
| 201 |
new_printer_device_tabs = { |
| 202 |
"parallel" : 0, # empty tab |
| 203 |
"usb" : 0, |
| 204 |
"bluetooth" : 0, |
| 205 |
"hal" : 0, |
| 206 |
"beh" : 0, |
| 207 |
"hp" : 0, |
| 208 |
"hpfax" : 0, |
| 209 |
"dnssd" : 0, |
| 210 |
"socket": 2, |
| 211 |
"lpd" : 3, |
| 212 |
"scsi" : 4, |
| 213 |
"serial" : 5, |
| 214 |
"smb" : 6, |
| 215 |
"network": 7, |
| 216 |
} |
| 217 |
|
| 218 |
def __init__(self): |
| 219 |
GObject.GObject.__init__ (self) |
| 220 |
self.language = locale.getlocale (locale.LC_MESSAGES) |
| 221 |
|
| 222 |
self.options = {} # keyword -> Option object |
| 223 |
self.changed = set() |
| 224 |
self.conflicts = set() |
| 225 |
self.device = None |
| 226 |
self.ppd = None |
| 227 |
self.remotecupsqueue = False |
| 228 |
self.exactdrivermatch = False |
| 229 |
self.installable_options = False |
| 230 |
self.ppdsloader = None |
| 231 |
self.installed_driver_files = [] |
| 232 |
self.searchedfordriverpackages = False |
| 233 |
self.founddownloadabledrivers = False |
| 234 |
self.founddownloadableppd = False |
| 235 |
self.downloadable_driver_for_printer = None |
| 236 |
self.downloadable_printers = [] |
| 237 |
self.nextnptab_rerun = False |
| 238 |
self.printers = {} # set in init() |
| 239 |
self.recommended_model_selected = False |
| 240 |
self._searchdialog = None |
| 241 |
self._installdialog = None |
| 242 |
|
| 243 |
self.getWidgets({"NewPrinterWindow": |
| 244 |
["NewPrinterWindow", |
| 245 |
"ntbkNewPrinter", |
| 246 |
"btnNPBack", |
| 247 |
"btnNPForward", |
| 248 |
"btnNPApply", |
| 249 |
"spinner", |
| 250 |
"entNPName", |
| 251 |
"entNPDescription", |
| 252 |
"entNPLocation", |
| 253 |
"tvNPDevices", |
| 254 |
"ntbkNPType", |
| 255 |
"lblNPDeviceDescription", |
| 256 |
"expNPDeviceURIs", |
| 257 |
"tvNPDeviceURIs", |
| 258 |
"cmbNPTSerialBaud", |
| 259 |
"cmbNPTSerialParity", |
| 260 |
"cmbNPTSerialBits", |
| 261 |
"cmbNPTSerialFlow", |
| 262 |
"btnNPTLpdProbe", |
| 263 |
"entNPTLpdHost", |
| 264 |
"entNPTLpdQueue", |
| 265 |
"entNPTJetDirectHostname", |
| 266 |
"entNPTJetDirectPort", |
| 267 |
"entSMBURI", |
| 268 |
"btnSMBBrowse", |
| 269 |
"tblSMBAuth", |
| 270 |
"rbtnSMBAuthPrompt", |
| 271 |
"rbtnSMBAuthSet", |
| 272 |
"entSMBUsername", |
| 273 |
"entSMBPassword", |
| 274 |
"btnSMBVerify", |
| 275 |
"entNPTNetworkHostname", |
| 276 |
"btnNetworkFind", |
| 277 |
"lblNetworkFindSearching", |
| 278 |
"lblNetworkFindNotFound", |
| 279 |
"entNPTDevice", |
| 280 |
"tvNCMembers", |
| 281 |
"tvNCNotMembers", |
| 282 |
"btnNCAddMember", |
| 283 |
"btnNCDelMember", |
| 284 |
"ntbkPPDSource", |
| 285 |
"rbtnNPPPD", |
| 286 |
"tvNPMakes", |
| 287 |
"rbtnNPFoomatic", |
| 288 |
"filechooserPPD", |
| 289 |
"rbtnNPDownloadableDriverSearch", |
| 290 |
"entNPDownloadableDriverSearch", |
| 291 |
"btnNPDownloadableDriverSearch", |
| 292 |
"btnNPDownloadableDriverSearch_label", |
| 293 |
"cmbNPDownloadableDriverFoundPrinters", |
| 294 |
"tvNPModels", |
| 295 |
"tvNPDrivers", |
| 296 |
"rbtnChangePPDasIs", |
| 297 |
"rbtnChangePPDKeepSettings", |
| 298 |
"scrNPInstallableOptions", |
| 299 |
"vbNPInstallOptions", |
| 300 |
"tvNPDownloadableDrivers", |
| 301 |
"ntbkNPDownloadableDriverProperties", |
| 302 |
"lblNPDownloadableDriverSupplier", |
| 303 |
"cbNPDownloadableDriverSupplierVendor", |
| 304 |
"lblNPDownloadableDriverLicense", |
| 305 |
"cbNPDownloadableDriverLicensePatents", |
| 306 |
"cbNPDownloadableDriverLicenseFree", |
| 307 |
"lblNPDownloadableDriverDescription", |
| 308 |
"lblNPDownloadableDriverSupportContacts", |
| 309 |
"hsDownloadableDriverPerfText", |
| 310 |
"hsDownloadableDriverPerfLineArt", |
| 311 |
"hsDownloadableDriverPerfGraphics", |
| 312 |
"hsDownloadableDriverPerfPhoto", |
| 313 |
"lblDownloadableDriverPerfTextUnknown", |
| 314 |
"lblDownloadableDriverPerfLineArtUnknown", |
| 315 |
"lblDownloadableDriverPerfGraphicsUnknown", |
| 316 |
"lblDownloadableDriverPerfPhotoUnknown", |
| 317 |
"frmNPDownloadableDriverLicenseTerms", |
| 318 |
"tvNPDownloadableDriverLicense", |
| 319 |
"rbtnNPDownloadLicenseYes", |
| 320 |
"rbtnNPDownloadLicenseNo"], |
| 321 |
"WaitWindow": |
| 322 |
["WaitWindow", |
| 323 |
"lblWait"], |
| 324 |
"SMBBrowseDialog": |
| 325 |
["SMBBrowseDialog", |
| 326 |
"tvSMBBrowser", |
| 327 |
"btnSMBBrowseOk"]}, |
| 328 |
|
| 329 |
domain=config.PACKAGE) |
| 330 |
|
| 331 |
# Fill in liststores for combo-box widgets |
| 332 |
for (widget, |
| 333 |
opts) in [(self.cmbNPTSerialBaud, |
| 334 |
[[_("Default"), ""], |
| 335 |
["1200", "1200"], |
| 336 |
["2400", "2400"], |
| 337 |
["4800", "4800"], |
| 338 |
["9600", "9600"], |
| 339 |
["19200", "19200"], |
| 340 |
["38400", "38400"], |
| 341 |
["57600", "57600"], |
| 342 |
["115200", "115200"]]), |
| 343 |
|
| 344 |
(self.cmbNPTSerialParity, |
| 345 |
[[_("Default"), ""], |
| 346 |
[_("None"), "none"], |
| 347 |
[_("Odd"), "odd"], |
| 348 |
[_("Even"), "even"]]), |
| 349 |
|
| 350 |
(self.cmbNPTSerialBits, |
| 351 |
[[_("Default"), ""], |
| 352 |
["8", "8"], |
| 353 |
["7", "7"]]), |
| 354 |
|
| 355 |
(self.cmbNPTSerialFlow, |
| 356 |
[[_("Default"), ""], |
| 357 |
[_("None"), "none"], |
| 358 |
[_("XON/XOFF (Software)"), "soft"], |
| 359 |
[_("RTS/CTS (Hardware)"), "hard"], |
| 360 |
[_("DTR/DSR (Hardware)"), "hard"]]), |
| 361 |
|
| 362 |
]: |
| 363 |
store = Gtk.ListStore (str, str) |
| 364 |
for row in opts: |
| 365 |
store.append (row) |
| 366 |
|
| 367 |
widget.set_model (store) |
| 368 |
cell = Gtk.CellRendererText () |
| 369 |
widget.clear () |
| 370 |
widget.pack_start (cell, True) |
| 371 |
widget.add_attribute (cell, 'text', 0) |
| 372 |
|
| 373 |
# Set up some lists |
| 374 |
m = Gtk.SelectionMode.MULTIPLE |
| 375 |
s = Gtk.SelectionMode.SINGLE |
| 376 |
b = Gtk.SelectionMode.BROWSE |
| 377 |
for name, model, treeview, selection_mode in ( |
| 378 |
(_("Members of this class"), Gtk.ListStore(str), |
| 379 |
self.tvNCMembers, m), |
| 380 |
(_("Others"), Gtk.ListStore(str), self.tvNCNotMembers, m), |
| 381 |
(_("Devices"), Gtk.ListStore(str), self.tvNPDevices, s), |
| 382 |
(_("Connections"), Gtk.ListStore(str), self.tvNPDeviceURIs, s), |
| 383 |
(_("Makes"), Gtk.ListStore(str, str), self.tvNPMakes,s), |
| 384 |
(_("Models"), Gtk.ListStore(str, str), self.tvNPModels,s), |
| 385 |
(_("Drivers"), Gtk.ListStore(str), self.tvNPDrivers,s), |
| 386 |
(_("Downloadable Drivers"), Gtk.ListStore(str), |
| 387 |
self.tvNPDownloadableDrivers, b), |
| 388 |
): |
| 389 |
|
| 390 |
cell = Gtk.CellRendererText() |
| 391 |
column = Gtk.TreeViewColumn(name, cell, text=0) |
| 392 |
treeview.set_model(model) |
| 393 |
treeview.append_column(column) |
| 394 |
treeview.get_selection().set_mode(selection_mode) |
| 395 |
|
| 396 |
# Since some dialogs are reused we can't let the delete-event's |
| 397 |
# default handler destroy them |
| 398 |
self.SMBBrowseDialog.connect ("delete-event", on_delete_just_hide) |
| 399 |
self.WaitWindow_handler = self.WaitWindow.connect ("delete-event", |
| 400 |
on_delete_just_hide) |
| 401 |
|
| 402 |
self.ntbkNewPrinter.set_show_tabs(False) |
| 403 |
self.ntbkPPDSource.set_show_tabs(False) |
| 404 |
self.ntbkNPType.set_show_tabs(False) |
| 405 |
self.ntbkNPDownloadableDriverProperties.set_show_tabs(False) |
| 406 |
|
| 407 |
self.spinner_count = 0 |
| 408 |
|
| 409 |
# Set up OpenPrinting widgets. |
| 410 |
self.opreq = None |
| 411 |
self.opreq_handlers = None |
| 412 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 413 |
cell = Gtk.CellRendererText() |
| 414 |
combobox.pack_start (cell, True) |
| 415 |
combobox.add_attribute(cell, 'text', 0) |
| 416 |
if config.DOWNLOADABLE_ONLYFREE: |
| 417 |
for widget in [self.cbNPDownloadableDriverLicenseFree, |
| 418 |
self.cbNPDownloadableDriverLicensePatents]: |
| 419 |
widget.hide () |
| 420 |
if os.path.exists('/etc/apt/sources.list') or os.path.exists( |
| 421 |
'/etc/apt/sources.list.d'): |
| 422 |
config.packagesystem = 'deb' |
| 423 |
self.packageinstaller = 'apt' |
| 424 |
elif os.path.exists('/etc/yum.conf'): |
| 425 |
config.packagesystem = 'rpm' |
| 426 |
self.packageinstaller = 'yum' |
| 427 |
else: |
| 428 |
# No known package system, so we only load single PPDs via |
| 429 |
# OpenPrinting |
| 430 |
config.DOWNLOADABLE_ONLYPPD = True |
| 431 |
|
| 432 |
def protect_toggle (toggle_widget): |
| 433 |
active = getattr (toggle_widget, 'protect_active', None) |
| 434 |
if active is not None: |
| 435 |
toggle_widget.set_active (active) |
| 436 |
|
| 437 |
for widget in [self.cbNPDownloadableDriverSupplierVendor, |
| 438 |
self.cbNPDownloadableDriverLicenseFree, |
| 439 |
self.cbNPDownloadableDriverLicensePatents]: |
| 440 |
widget.connect ('clicked', protect_toggle) |
| 441 |
|
| 442 |
for widget in [self.hsDownloadableDriverPerfText, |
| 443 |
self.hsDownloadableDriverPerfLineArt, |
| 444 |
self.hsDownloadableDriverPerfGraphics, |
| 445 |
self.hsDownloadableDriverPerfPhoto]: |
| 446 |
widget.connect ('change-value', |
| 447 |
lambda x, y, z: True) |
| 448 |
|
| 449 |
# Device list |
| 450 |
slct = self.tvNPDevices.get_selection () |
| 451 |
slct.set_select_function (self.device_select_function, None) |
| 452 |
self.tvNPDevices.set_row_separator_func (self.device_row_separator_fn, None) |
| 453 |
self.tvNPDevices.connect ("row-activated", self.device_row_activated) |
| 454 |
self.tvNPDevices.connect ("row-expanded", self.device_row_expanded) |
| 455 |
|
| 456 |
# Devices expander |
| 457 |
self.expNPDeviceURIs.connect ("notify::expanded", |
| 458 |
self.on_expNPDeviceURIs_expanded) |
| 459 |
self.expNPDeviceURIs.set_expanded(1) |
| 460 |
|
| 461 |
# SMB browser |
| 462 |
self.smb_store = Gtk.TreeStore (GObject.TYPE_PYOBJECT) |
| 463 |
self.btnSMBBrowse.set_sensitive (True) |
| 464 |
if not PYSMB_AVAILABLE: |
| 465 |
self.btnSMBBrowse.set_tooltip_text (_("Browsing requires pysmbc " |
| 466 |
"module")) |
| 467 |
self.tvSMBBrowser.set_model (self.smb_store) |
| 468 |
|
| 469 |
# SMB list columns |
| 470 |
col = Gtk.TreeViewColumn (_("Share")) |
| 471 |
cell = Gtk.CellRendererText () |
| 472 |
col.pack_start (cell, False) |
| 473 |
col.set_cell_data_func (cell, self.smbbrowser_cell_share, None) |
| 474 |
self.tvSMBBrowser.append_column (col) |
| 475 |
|
| 476 |
col = Gtk.TreeViewColumn (_("Comment")) |
| 477 |
cell = Gtk.CellRendererText () |
| 478 |
col.pack_start (cell, False) |
| 479 |
col.set_cell_data_func (cell, self.smbbrowser_cell_comment, None) |
| 480 |
self.tvSMBBrowser.append_column (col) |
| 481 |
|
| 482 |
slct = self.tvSMBBrowser.get_selection () |
| 483 |
slct.set_select_function (self.smb_select_function, None) |
| 484 |
|
| 485 |
self.SMBBrowseDialog.set_transient_for(self.NewPrinterWindow) |
| 486 |
|
| 487 |
self.tvNPDrivers.set_has_tooltip(True) |
| 488 |
self.tvNPDrivers.connect("query-tooltip", self.on_NPDrivers_query_tooltip) |
| 489 |
|
| 490 |
ppd_filter = Gtk.FileFilter() |
| 491 |
ppd_filter.set_name(_("PostScript Printer Description files (*.ppd, *.PPD, *.ppd.gz, *.PPD.gz, *.PPD.GZ)")) |
| 492 |
ppd_filter.add_pattern("*.ppd") |
| 493 |
ppd_filter.add_pattern("*.PPD") |
| 494 |
ppd_filter.add_pattern("*.ppd.gz") |
| 495 |
ppd_filter.add_pattern("*.PPD.gz") |
| 496 |
ppd_filter.add_pattern("*.PPD.GZ") |
| 497 |
self.filechooserPPD.add_filter(ppd_filter) |
| 498 |
|
| 499 |
ppd_filter = Gtk.FileFilter() |
| 500 |
ppd_filter.set_name(_("All files (*)")) |
| 501 |
ppd_filter.add_pattern("*") |
| 502 |
self.filechooserPPD.add_filter(ppd_filter) |
| 503 |
|
| 504 |
self.device_selected = -1 |
| 505 |
self.dialog_mode = "printer" |
| 506 |
self.connect_signals () |
| 507 |
debugprint ("+%s" % self) |
| 508 |
|
| 509 |
def __del__ (self): |
| 510 |
debugprint ("-%s" % self) |
| 511 |
|
| 512 |
def do_destroy (self): |
| 513 |
debugprint ("DESTROY: %s" % self) |
| 514 |
if self.SMBBrowseDialog: |
| 515 |
self.SMBBrowseDialog.destroy () |
| 516 |
self.SMBBrowseDialog = None |
| 517 |
|
| 518 |
if self.NewPrinterWindow: |
| 519 |
self.NewPrinterWindow.destroy () |
| 520 |
self.NewPrinterWindow = None |
| 521 |
|
| 522 |
if self.WaitWindow: |
| 523 |
self.WaitWindow.destroy () |
| 524 |
self.WaitWindow = None |
| 525 |
|
| 526 |
def inc_spinner_task (self): |
| 527 |
if self.spinner_count == 0: |
| 528 |
self.spinner.show () |
| 529 |
self.spinner.start () |
| 530 |
|
| 531 |
self.spinner_count += 1 |
| 532 |
|
| 533 |
def dec_spinner_task (self): |
| 534 |
self.spinner_count -= 1 |
| 535 |
if self.spinner_count == 0: |
| 536 |
self.spinner.hide () |
| 537 |
self.spinner.stop () |
| 538 |
|
| 539 |
def show_IPP_Error (self, exception, message): |
| 540 |
debugprint ("%s: IPP error dialog (%s, %s)" % (self, repr (exception), |
| 541 |
message)) |
| 542 |
return show_IPP_Error (exception, message, parent=self.NewPrinterWindow) |
| 543 |
|
| 544 |
def option_changed(self, option): |
| 545 |
if option.is_changed(): |
| 546 |
self.changed.add(option) |
| 547 |
else: |
| 548 |
self.changed.discard(option) |
| 549 |
|
| 550 |
if option.conflicts: |
| 551 |
self.conflicts.add(option) |
| 552 |
else: |
| 553 |
self.conflicts.discard(option) |
| 554 |
self.setDataButtonState() |
| 555 |
|
| 556 |
return |
| 557 |
|
| 558 |
def setDataButtonState(self): |
| 559 |
self.btnNPForward.set_sensitive(not bool(self.conflicts)) |
| 560 |
|
| 561 |
def makeNameUnique(self, name): |
| 562 |
"""Make a suggested queue name valid and unique.""" |
| 563 |
name = name.replace (" ", "-") |
| 564 |
name = name.replace ("/", "-") |
| 565 |
name = name.replace ("#", "-") |
| 566 |
if not checkNPName (self.printers, name): |
| 567 |
suffix=2 |
| 568 |
while not checkNPName (self.printers, name + "-" + str (suffix)): |
| 569 |
suffix += 1 |
| 570 |
if suffix == 100: |
| 571 |
break |
| 572 |
name += "-" + str (suffix) |
| 573 |
return name |
| 574 |
|
| 575 |
def destroy (self): |
| 576 |
self.emit ('destroy') |
| 577 |
|
| 578 |
def init(self, dialog_mode, device_uri=None, name=None, ppd=None, |
| 579 |
devid="", host=None, encryption=None, parent=None, xid=0): |
| 580 |
self.parent = parent |
| 581 |
if not self.parent: |
| 582 |
self.NewPrinterWindow.set_focus_on_map (False) |
| 583 |
|
| 584 |
self.dialog_mode = dialog_mode |
| 585 |
self._device_uri = device_uri |
| 586 |
self.orig_ppd = ppd |
| 587 |
self.devid = devid |
| 588 |
self._host = host |
| 589 |
self._encryption = encryption |
| 590 |
self._name = name |
| 591 |
if not host: |
| 592 |
self._host = cups.getServer () |
| 593 |
if not encryption: |
| 594 |
self._encryption = cups.getEncryption () |
| 595 |
|
| 596 |
self.options = {} # keyword -> Option object |
| 597 |
self.changed = set() |
| 598 |
self.conflicts = set() |
| 599 |
self.fetchDevices_conn = None |
| 600 |
self.ppds = None |
| 601 |
self.ppdsmatch_result = None |
| 602 |
self.printer_finder = None |
| 603 |
|
| 604 |
if not self._validInitParameters (): |
| 605 |
raise RuntimeError |
| 606 |
|
| 607 |
# Get a current list of printers so that we can know whether |
| 608 |
# the chosen name is unique. |
| 609 |
try: |
| 610 |
self.cups = authconn.Connection (parent=self.NewPrinterWindow, |
| 611 |
host=self._host, |
| 612 |
encryption=self._encryption) |
| 613 |
except cups.HTTPError as e: |
| 614 |
(s,) = e.args |
| 615 |
show_HTTP_Error (s, self.parent) |
| 616 |
return False |
| 617 |
except RuntimeError: |
| 618 |
show_HTTP_Error (-1, self.parent) |
| 619 |
return False |
| 620 |
except Exception as e: |
| 621 |
nonfatalException (e) |
| 622 |
return False |
| 623 |
|
| 624 |
try: |
| 625 |
self.printers = cupshelpers.getPrinters (self.cups) |
| 626 |
except cups.IPPError as e: |
| 627 |
(e, m) = e.args |
| 628 |
show_IPP_Error (e, m, parent=self.parent) |
| 629 |
return False |
| 630 |
|
| 631 |
# Initialise widgets. |
| 632 |
self.lblNetworkFindSearching.hide () |
| 633 |
self.entNPTNetworkHostname.set_sensitive (True) |
| 634 |
self.entNPTNetworkHostname.set_text ('') |
| 635 |
self.btnNetworkFind.set_sensitive (True) |
| 636 |
self.lblNetworkFindNotFound.hide () |
| 637 |
|
| 638 |
# Clear out any previous list of makes. |
| 639 |
model = self.tvNPMakes.get_model() |
| 640 |
model.clear() |
| 641 |
|
| 642 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 643 |
combobox.set_model (Gtk.ListStore (str, str)) |
| 644 |
self.entNPDownloadableDriverSearch.set_text ('') |
| 645 |
label = self.btnNPDownloadableDriverSearch_label |
| 646 |
label.set_text (_("Search")) |
| 647 |
|
| 648 |
self.entNPTJetDirectPort.set_text('9100') |
| 649 |
self.rbtnSMBAuthPrompt.set_active(True) |
| 650 |
|
| 651 |
if xid != 0 and self.parent: |
| 652 |
self.NewPrinterWindow.show_now() |
| 653 |
self.NewPrinterWindow.set_transient_for (self.parent) |
| 654 |
|
| 655 |
if self.dialog_mode == "printer": |
| 656 |
self._initialisePrinterMode () |
| 657 |
elif self.dialog_mode == "class": |
| 658 |
self._initialiseClassMode () |
| 659 |
elif self.dialog_mode == "device": |
| 660 |
self._initialiseDeviceMode () |
| 661 |
elif self.dialog_mode == "printer_with_uri": |
| 662 |
self._initialisePrinterWithURIMode () |
| 663 |
elif self.dialog_mode == "ppd": |
| 664 |
self._initialisePPDMode () |
| 665 |
elif self.dialog_mode == "download_driver": |
| 666 |
self._initialiseDownloadDriverMode () |
| 667 |
return True |
| 668 |
|
| 669 |
if xid == 0 and self.parent: |
| 670 |
self.NewPrinterWindow.set_transient_for (parent) |
| 671 |
|
| 672 |
self.NewPrinterWindow.show() |
| 673 |
self.setNPButtons() |
| 674 |
return True |
| 675 |
|
| 676 |
def _validInitParameters (self): |
| 677 |
if self.dialog_mode in ['printer_with_uri', 'device', 'ppd']: |
| 678 |
return self._device_uri is not None |
| 679 |
elif self.dialog_mode == 'download_driver': |
| 680 |
return self.devid != "" |
| 681 |
return True |
| 682 |
|
| 683 |
def _initialiseClassMode (self): |
| 684 |
self._initialiseWidgetsForMode ("class") |
| 685 |
self.NewPrinterWindow.set_title (_("New Class")) |
| 686 |
|
| 687 |
self.fillNewClassMembers () |
| 688 |
|
| 689 |
# Start on name page |
| 690 |
self.ntbkNewPrinter.set_current_page (self.PAGE_DESCRIBE_PRINTER) |
| 691 |
|
| 692 |
def _initialisePrinterMode (self): |
| 693 |
self._initialiseWidgetsForMode ("printer") |
| 694 |
self.NewPrinterWindow.set_title (_("New Printer")) |
| 695 |
|
| 696 |
# Start on devices page (SELECT_DEVICE, not DESCRIBE_PRINTER) |
| 697 |
self.ntbkNewPrinter.set_current_page (self.PAGE_SELECT_DEVICE) |
| 698 |
self.fillDeviceTab () |
| 699 |
self.rbtnNPFoomatic.set_active (True) |
| 700 |
self.on_rbtnNPFoomatic_toggled (self.rbtnNPFoomatic) |
| 701 |
|
| 702 |
def _initialiseDeviceMode (self): |
| 703 |
self.NewPrinterWindow.set_title (_("Change Device URI")) |
| 704 |
self.ntbkNewPrinter.set_current_page (self.PAGE_SELECT_DEVICE) |
| 705 |
self.fillDeviceTab (self._device_uri) |
| 706 |
|
| 707 |
def _initialisePrinterWithURIMode (self): |
| 708 |
self._initialiseWidgetsForMode ("printer") |
| 709 |
self._initialiseDeviceFromURI () |
| 710 |
|
| 711 |
self.NewPrinterWindow.set_title (_("New Printer")) |
| 712 |
|
| 713 |
self.ntbkNewPrinter.set_current_page (self.PAGE_SELECT_INSTALL_METHOD) |
| 714 |
self.rbtnNPFoomatic.set_active (True) |
| 715 |
self.on_rbtnNPFoomatic_toggled (self.rbtnNPFoomatic) |
| 716 |
self.rbtnChangePPDKeepSettings.set_active (True) |
| 717 |
|
| 718 |
self._initialiseAutoVariables () |
| 719 |
self.nextNPTab (step = 0) |
| 720 |
|
| 721 |
def _initialiseDownloadDriverMode (self): |
| 722 |
self.NewPrinterWindow.set_title (_("Download Printer Driver")) |
| 723 |
|
| 724 |
self.ntbkNewPrinter.set_current_page (self.PAGE_DOWNLOAD_DRIVER) |
| 725 |
self.nextnptab_rerun = True |
| 726 |
self.nextNPTab (step = 0) |
| 727 |
|
| 728 |
def _initialisePPDMode (self): |
| 729 |
self._initialiseDeviceFromURI () |
| 730 |
|
| 731 |
self.NewPrinterWindow.set_title (_("Change Driver")) |
| 732 |
|
| 733 |
# We'll need to know the Device ID for this device. |
| 734 |
if not self.devid: |
| 735 |
scheme = str (self._device_uri.split (":", 1)[0]) |
| 736 |
schemes = [scheme] |
| 737 |
if scheme in ["socket", "lpd", "ipp"]: |
| 738 |
schemes.extend (["snmp", "dnssd"]) |
| 739 |
self.fetchDevices_conn = asyncconn.Connection () |
| 740 |
self.fetchDevices_conn._begin_operation (_("fetching device list")) |
| 741 |
self.inc_spinner_task () |
| 742 |
cupshelpers.getDevices (self.fetchDevices_conn, |
| 743 |
include_schemes=schemes, |
| 744 |
reply_handler=self.change_ppd_got_devs, |
| 745 |
error_handler=self.change_ppd_got_devs) |
| 746 |
|
| 747 |
self.ntbkNewPrinter.set_current_page (self.PAGE_SELECT_INSTALL_METHOD) |
| 748 |
self.rbtnNPFoomatic.set_active (True) |
| 749 |
self.on_rbtnNPFoomatic_toggled (self.rbtnNPFoomatic) |
| 750 |
self.rbtnChangePPDKeepSettings.set_active (True) |
| 751 |
|
| 752 |
self._initialiseAutoVariables () |
| 753 |
|
| 754 |
def _initialiseWidgetsForMode (self, mode_name): |
| 755 |
self.entNPName.set_text (self.makeNameUnique (mode_name)) |
| 756 |
self.entNPName.grab_focus () |
| 757 |
for widget in [self.entNPLocation, |
| 758 |
self.entNPDescription, |
| 759 |
self.entSMBURI, self.entSMBUsername, |
| 760 |
self.entSMBPassword]: |
| 761 |
widget.set_text ('') |
| 762 |
|
| 763 |
def _initialiseDeviceFromURI (self): |
| 764 |
device_dict = { } |
| 765 |
self.device = cupshelpers.Device (self._device_uri, **device_dict) |
| 766 |
|
| 767 |
def _initialiseAutoVariables (self): |
| 768 |
self.auto_make = "" |
| 769 |
self.auto_model = "" |
| 770 |
self.auto_driver = None |
| 771 |
|
| 772 |
def change_ppd_got_devs (self, conn, result): |
| 773 |
self.fetchDevices_conn._end_operation () |
| 774 |
self.fetchDevices_conn.destroy () |
| 775 |
self.fetchDevices_conn = None |
| 776 |
self.dec_spinner_task () |
| 777 |
if isinstance (result, Exception): |
| 778 |
current_devices = {} |
| 779 |
else: |
| 780 |
current_devices = result |
| 781 |
|
| 782 |
devid = None |
| 783 |
mm = None |
| 784 |
if self.devid != "": |
| 785 |
devid = self.devid |
| 786 |
else: |
| 787 |
device = current_devices.get (self.device.uri) |
| 788 |
if device: |
| 789 |
devid = device.id |
| 790 |
mm = device.make_and_model |
| 791 |
self.device = device |
| 792 |
|
| 793 |
# We'll also need the list of PPDs |
| 794 |
self.ntbkNewPrinter.set_current_page (self.PAGE_SELECT_INSTALL_METHOD) |
| 795 |
self.nextNPTab(step = 0) |
| 796 |
|
| 797 |
def on_ppdsloader_finished_next (self, ppdsloader): |
| 798 |
""" |
| 799 |
This method is called when the PPDs loader has finished |
| 800 |
loading PPDs in preparation for the next screen the user will |
| 801 |
see, having clicked 'Forward'. We are creating a new queue, |
| 802 |
and dialog_mode is either "printer" or "printer_with_uri". |
| 803 |
""" |
| 804 |
|
| 805 |
self._getPPDs_reply (ppdsloader) |
| 806 |
if not self.ppds: |
| 807 |
return |
| 808 |
|
| 809 |
if ppdsloader._jockey_has_answered: |
| 810 |
self.searchedfordriverpackages = True |
| 811 |
|
| 812 |
debugprint ("Loaded PPDs this time; try nextNPTab again...") |
| 813 |
self.nextnptab_rerun = True |
| 814 |
if self.ntbkNewPrinter.get_current_page () == self.PAGE_SELECT_INSTALL_METHOD: |
| 815 |
self.nextNPTab (step = 0) |
| 816 |
else: |
| 817 |
self.nextNPTab () |
| 818 |
|
| 819 |
# get PPDs |
| 820 |
|
| 821 |
def _getPPDs_reply (self, ppdsloader): |
| 822 |
exc = ppdsloader.get_error () |
| 823 |
if exc: |
| 824 |
ppdsloader.destroy () |
| 825 |
try: |
| 826 |
raise exc |
| 827 |
except cups.IPPError as e: |
| 828 |
(e, m) = e.args |
| 829 |
self.show_IPP_Error (e, m) |
| 830 |
return |
| 831 |
|
| 832 |
ppds = ppdsloader.get_ppds () |
| 833 |
if ppds: |
| 834 |
self.ppds = ppds |
| 835 |
self.ppdsmatch_result = ppdsloader.get_ppdsmatch_result () |
| 836 |
if ppdsloader._jockey_has_answered: |
| 837 |
self.installed_driver_files = ppdsloader.get_installed_files () |
| 838 |
else: |
| 839 |
self.ppds = None |
| 840 |
self.ppdsmatch_result = None |
| 841 |
|
| 842 |
ppdsloader.destroy () |
| 843 |
self.ppdsloader = None |
| 844 |
|
| 845 |
# Class members |
| 846 |
|
| 847 |
def fillNewClassMembers(self): |
| 848 |
model = self.tvNCMembers.get_model() |
| 849 |
model.clear() |
| 850 |
model = self.tvNCNotMembers.get_model() |
| 851 |
model.clear() |
| 852 |
try: |
| 853 |
self.printers = cupshelpers.getPrinters (self.cups) |
| 854 |
except cups.IPPError: |
| 855 |
self.printers = {} |
| 856 |
|
| 857 |
for printer in self.printers.keys(): |
| 858 |
if not self.printers[printer].type & cups.CUPS_PRINTER_CLASS: |
| 859 |
model.append((printer,)) |
| 860 |
|
| 861 |
def on_btnNCAddMember_clicked(self, button): |
| 862 |
moveClassMembers(self.tvNCNotMembers, self.tvNCMembers) |
| 863 |
self.btnNPApply.set_sensitive( |
| 864 |
bool(getCurrentClassMembers(self.tvNCMembers))) |
| 865 |
button.set_sensitive(False) |
| 866 |
|
| 867 |
def on_btnNCDelMember_clicked(self, button): |
| 868 |
moveClassMembers(self.tvNCMembers, self.tvNCNotMembers) |
| 869 |
self.btnNPApply.set_sensitive( |
| 870 |
bool(getCurrentClassMembers(self.tvNCMembers))) |
| 871 |
button.set_sensitive(False) |
| 872 |
|
| 873 |
def on_tvNCMembers_cursor_changed(self, widget): |
| 874 |
selection = widget.get_selection() |
| 875 |
if selection is None: |
| 876 |
return |
| 877 |
|
| 878 |
model_from, rows = selection.get_selected_rows() |
| 879 |
self.btnNCDelMember.set_sensitive(rows != []) |
| 880 |
|
| 881 |
def on_tvNCNotMembers_cursor_changed(self, widget): |
| 882 |
selection = widget.get_selection() |
| 883 |
if selection is None: |
| 884 |
return |
| 885 |
|
| 886 |
model_from, rows = selection.get_selected_rows() |
| 887 |
self.btnNCAddMember.set_sensitive(rows != []) |
| 888 |
|
| 889 |
# Navigation buttons |
| 890 |
|
| 891 |
def on_NPCancel(self, widget, event=None): |
| 892 |
if self.fetchDevices_conn: |
| 893 |
self.fetchDevices_conn.destroy () |
| 894 |
self.fetchDevices_conn = None |
| 895 |
self.dec_spinner_task () |
| 896 |
|
| 897 |
if self.ppdsloader: |
| 898 |
self.ppdsloader.destroy () |
| 899 |
self.ppdsloader = None |
| 900 |
|
| 901 |
if self.printer_finder: |
| 902 |
self.printer_finder.cancel () |
| 903 |
self.printer_finder = None |
| 904 |
self.dec_spinner_task () |
| 905 |
|
| 906 |
self.NewPrinterWindow.hide() |
| 907 |
if self.opreq is not None: |
| 908 |
for handler in self.opreq_handlers: |
| 909 |
self.opreq.disconnect (handler) |
| 910 |
|
| 911 |
self.opreq_handlers = None |
| 912 |
self.opreq.cancel () |
| 913 |
self.opreq = None |
| 914 |
|
| 915 |
self.device = None |
| 916 |
self.printers = {} |
| 917 |
self.emit ('dialog-canceled') |
| 918 |
return True |
| 919 |
|
| 920 |
def on_btnNPBack_clicked(self, widget): |
| 921 |
self.nextNPTab(-1) |
| 922 |
|
| 923 |
def on_btnNPForward_clicked(self, widget): |
| 924 |
self.nextNPTab() |
| 925 |
|
| 926 |
def installdriverpackage (self, driver): |
| 927 |
install_info = self._getDriverInstallationInfo (driver) |
| 928 |
if not install_info: |
| 929 |
return False |
| 930 |
|
| 931 |
name = install_info['name'] |
| 932 |
repo = install_info['repo'] |
| 933 |
keyid = install_info['keyid'] |
| 934 |
|
| 935 |
fmt = _("Installing driver %s") % name |
| 936 |
self._installdialog = Gtk.MessageDialog (parent=self.NewPrinterWindow, |
| 937 |
modal=True, destroy_with_parent=True, |
| 938 |
type=Gtk.MessageType.INFO, |
| 939 |
buttons=Gtk.ButtonsType.CANCEL, |
| 940 |
text=fmt) |
| 941 |
|
| 942 |
self._installdialog.format_secondary_text (_("Installing ...")) |
| 943 |
# Add a progress bar to the message box |
| 944 |
dialogarea = self._installdialog.get_message_area() |
| 945 |
pbar = Gtk.ProgressBar() |
| 946 |
dialogarea.add(pbar) |
| 947 |
pbar.show() |
| 948 |
|
| 949 |
# Save a reference to the progress bar in the dialog, so that |
| 950 |
# we can easily reference it from do_installdriverpackage() |
| 951 |
self._installdialog._progress_bar = pbar |
| 952 |
|
| 953 |
self._installdialog.connect ("response", self._installdialog_response) |
| 954 |
self._installdialog.show_all () |
| 955 |
|
| 956 |
# Perform the actual installation of the printer driver |
| 957 |
ret = self.do_installdriverpackage (name, repo, keyid) |
| 958 |
|
| 959 |
if self._installdialog: |
| 960 |
self._installdialog.hide () |
| 961 |
self._installdialog.destroy () |
| 962 |
self._installdialog = None |
| 963 |
|
| 964 |
return ret |
| 965 |
|
| 966 |
def do_installdriverpackage(self, name, repo, keyid): |
| 967 |
debugprint('Installing driver: %s; Repo: %s; Key ID: %s' % |
| 968 |
(repr (name), |
| 969 |
repr (repo), |
| 970 |
repr (keyid))) |
| 971 |
|
| 972 |
# Do the installation with a command line helper script |
| 973 |
new_environ = os.environ.copy() |
| 974 |
new_environ['LC_ALL'] = "C" |
| 975 |
if keyid: |
| 976 |
args = ["install-printerdriver", name, repo, keyid] |
| 977 |
else: |
| 978 |
args = ["install-printerdriver", name, repo] |
| 979 |
debugprint ("Running command: " + repr(args)) |
| 980 |
ret = True |
| 981 |
try: |
| 982 |
self.p = subprocess.Popen (args, env=new_environ, close_fds=True, |
| 983 |
stdin=subprocess.DEVNULL, |
| 984 |
stdout=subprocess.PIPE) |
| 985 |
# Keep the UI refreshed while we wait for |
| 986 |
# the drivers query to complete. |
| 987 |
(stdout, stderr) = (self.p.stdout, self.p.stderr) |
| 988 |
|
| 989 |
done = False |
| 990 |
pbar = self._installdialog._progress_bar |
| 991 |
while self.p.poll() is None: |
| 992 |
line = stdout.readline ().strip() |
| 993 |
if (len(line) > 0): |
| 994 |
if line == "done": |
| 995 |
done = True |
| 996 |
break |
| 997 |
elif line.startswith(b"P"): |
| 998 |
try: |
| 999 |
percentage = float(line[1:]) |
| 1000 |
if percentage >= 0: |
| 1001 |
pbar.set_fraction(percentage/100) |
| 1002 |
else: |
| 1003 |
pbar.set_pulse_step(-percentage/100) |
| 1004 |
pbar.pulse() |
| 1005 |
except: |
| 1006 |
pass |
| 1007 |
else: |
| 1008 |
self.installed_driver_files.append(line.decode("utf-8")); |
| 1009 |
while Gtk.events_pending (): |
| 1010 |
Gtk.main_iteration () |
| 1011 |
if not line: |
| 1012 |
time.sleep (0.1) |
| 1013 |
if self.p.returncode != 0 and not done: |
| 1014 |
ret = False |
| 1015 |
except: |
| 1016 |
# Problem executing command. |
| 1017 |
ret = False |
| 1018 |
|
| 1019 |
if not ret: |
| 1020 |
self.installed_driver_files = []; |
| 1021 |
|
| 1022 |
return ret |
| 1023 |
|
| 1024 |
def _getDriverInstallationInfo (self, driver): |
| 1025 |
pkgs = driver.get('packages', {}) |
| 1026 |
arches = list(pkgs.keys()) |
| 1027 |
if len(arches) == 0: |
| 1028 |
debugprint('No packages for driver') |
| 1029 |
return None |
| 1030 |
if len(arches) > 1: |
| 1031 |
debugprint('Returned more than one matching architecture, please report this as a bug: %s' % repr (arches)) |
| 1032 |
return None |
| 1033 |
|
| 1034 |
pkgs = pkgs[arches[0]] |
| 1035 |
|
| 1036 |
if len(pkgs) != 1: |
| 1037 |
debugprint('Returned more than one package, this is currently not handled') |
| 1038 |
return None |
| 1039 |
pkg = list(pkgs.keys())[0] |
| 1040 |
|
| 1041 |
name = '' |
| 1042 |
if pkg.endswith('.deb'): |
| 1043 |
name = pkg.split('_')[0] |
| 1044 |
elif pkg.endswith('.rpm'): |
| 1045 |
name = '-'.join(pkg.split('-')[0:-2]) |
| 1046 |
else: |
| 1047 |
raise ValueError('Unknown package type: ' + pkg) |
| 1048 |
|
| 1049 |
# require signature for binary packages; architecture |
| 1050 |
# independent packages are usually PPDs, which we trust enough |
| 1051 |
keyid = None |
| 1052 |
if 'fingerprint' not in pkgs[pkg]: |
| 1053 |
if config.DOWNLOADABLE_PKG_ONLYSIGNED and arches[0] not in ['all', 'noarch']: |
| 1054 |
debugprint('Not installing driver as it does not have a GPG fingerprint URL') |
| 1055 |
return None |
| 1056 |
else: |
| 1057 |
keyid = download_gpg_fingerprint(pkgs[pkg]['fingerprint']) |
| 1058 |
if config.DOWNLOADABLE_PKG_ONLYSIGNED and arches[0] not in ['all', 'noarch'] and not keyid: |
| 1059 |
debugprint('Not installing driver as it does not have a valid GPG fingerprint') |
| 1060 |
return None |
| 1061 |
|
| 1062 |
|
| 1063 |
repo = pkgs[pkg].get('repositories', {}).get(self.packageinstaller) |
| 1064 |
if not repo: |
| 1065 |
debugprint('Local package system %s not found in %s' % |
| 1066 |
(self.packageinstaller, |
| 1067 |
repr (pkgs[pkg].get('repositories', {})))) |
| 1068 |
return None |
| 1069 |
|
| 1070 |
# All good: return necessary information to install the driver |
| 1071 |
return { 'name': name, 'repo': repo, 'keyid': keyid } |
| 1072 |
|
| 1073 |
def _installdialog_response (self, dialog, response): |
| 1074 |
self.p.terminate () |
| 1075 |
|
| 1076 |
def nextNPTab(self, step=1): |
| 1077 |
page_nr = self.ntbkNewPrinter.get_current_page() |
| 1078 |
debugprint ("Next clicked on page %d" % page_nr) |
| 1079 |
|
| 1080 |
keep_going = True |
| 1081 |
if self.dialog_mode == "printer" or self.dialog_mode == "printer_with_uri" or \ |
| 1082 |
self.dialog_mode == "ppd" or self.dialog_mode == "download_driver": |
| 1083 |
install_result = self._handlePrinterInstallationMode (step) |
| 1084 |
if install_result == self.INSTALL_RESULT_OPS_PENDING: |
| 1085 |
# Do not continue if the installation process says so |
| 1086 |
# (e.g. waiting for a CUPS to return a list of PPDs) |
| 1087 |
keep_going = False |
| 1088 |
|
| 1089 |
if not keep_going: |
| 1090 |
debugprint ('Interrupting execution of nextNPTab(): Operations pending') |
| 1091 |
return |
| 1092 |
|
| 1093 |
order = self._getPagesOrderForDialogMode () |
| 1094 |
next_page_nr = order[order.index(page_nr)+step] |
| 1095 |
|
| 1096 |
# fill Installable Options tab |
| 1097 |
fetch_ppd = False |
| 1098 |
try: |
| 1099 |
if order.index (self.PAGE_APPLY) > -1: |
| 1100 |
# There is a copy settings page in this set |
| 1101 |
fetch_ppd = next_page_nr == self.PAGE_APPLY and step >= 0 |
| 1102 |
except ValueError: |
| 1103 |
fetch_ppd = next_page_nr == self.PAGE_INSTALLABLE_OPTIONS and step >= 0 |
| 1104 |
|
| 1105 |
debugprint ("Will fetch ppd? %d" % fetch_ppd) |
| 1106 |
if fetch_ppd: |
| 1107 |
self.ppd = self.getNPPPD() |
| 1108 |
self.installable_options = False |
| 1109 |
if self.ppd is None: |
| 1110 |
return |
| 1111 |
|
| 1112 |
# Prepare Installable Options screen. |
| 1113 |
if isinstance(self.ppd, cups.PPD): |
| 1114 |
self.fillNPInstallableOptions() |
| 1115 |
else: |
| 1116 |
# Put a label there explaining why the page is empty. |
| 1117 |
ppd = self.ppd |
| 1118 |
self.ppd = None |
| 1119 |
self.fillNPInstallableOptions() |
| 1120 |
self.ppd = ppd |
| 1121 |
|
| 1122 |
if not self.installable_options: |
| 1123 |
if next_page_nr == self.PAGE_INSTALLABLE_OPTIONS: |
| 1124 |
# step over if empty |
| 1125 |
next_page_nr = order[order.index(next_page_nr)+1] |
| 1126 |
|
| 1127 |
# Step over empty Installable Options tab when moving backwards. |
| 1128 |
if next_page_nr == self.PAGE_INSTALLABLE_OPTIONS and \ |
| 1129 |
not self.installable_options and step < 0: |
| 1130 |
next_page_nr = order[order.index(next_page_nr)-1] |
| 1131 |
|
| 1132 |
debugprint ("Will advance to page %d" % next_page_nr) |
| 1133 |
if step >= 0 and next_page_nr == self.PAGE_DOWNLOAD_DRIVER: # About to show downloadable drivers |
| 1134 |
self.fillDownloadableDrivers () |
| 1135 |
|
| 1136 |
if step >= 0 and next_page_nr == self.PAGE_DESCRIBE_PRINTER: # About to choose a name. |
| 1137 |
# Suggest an appropriate name. |
| 1138 |
name = None |
| 1139 |
descr = None |
| 1140 |
|
| 1141 |
try: |
| 1142 |
if (self.device.id and |
| 1143 |
not self.device.type in ("socket", "lpd", "ipp", |
| 1144 |
"http", "https", "bluetooth")): |
| 1145 |
name = "%s %s" % (self.device.id_dict["MFG"], |
| 1146 |
self.device.id_dict["MDL"]) |
| 1147 |
except: |
| 1148 |
nonfatalException () |
| 1149 |
|
| 1150 |
try: |
| 1151 |
if name is None and isinstance (self.ppd, cups.PPD): |
| 1152 |
mname = self.ppd.findAttr ("modelName").value |
| 1153 |
make, model = cupshelpers.ppds.ppdMakeModelSplit (mname) |
| 1154 |
if make and model: |
| 1155 |
name = "%s %s" % (make, model) |
| 1156 |
elif make or model: |
| 1157 |
name = "%s%s" % (make, model) |
| 1158 |
except: |
| 1159 |
nonfatalException () |
| 1160 |
|
| 1161 |
if name: |
| 1162 |
descr = name |
| 1163 |
else: |
| 1164 |
name = 'printer' |
| 1165 |
|
| 1166 |
name = self.makeNameUnique (name) |
| 1167 |
self.entNPName.set_text (name) |
| 1168 |
|
| 1169 |
if descr: |
| 1170 |
self.entNPDescription.set_text (descr) |
| 1171 |
|
| 1172 |
self.ntbkNewPrinter.set_current_page(next_page_nr) |
| 1173 |
|
| 1174 |
self.setNPButtons() |
| 1175 |
|
| 1176 |
|
| 1177 |
def _getPagesOrderForDialogMode (self): |
| 1178 |
order = [] |
| 1179 |
if self.dialog_mode == "class": |
| 1180 |
order = [ |
| 1181 |
self.PAGE_DESCRIBE_PRINTER, |
| 1182 |
self.PAGE_CHOOSE_CLASS_MEMBERS, |
| 1183 |
self.PAGE_APPLY, |
| 1184 |
] |
| 1185 |
elif self.dialog_mode == "download_driver": |
| 1186 |
order = [ |
| 1187 |
self.PAGE_DOWNLOAD_DRIVER |
| 1188 |
] |
| 1189 |
elif self.dialog_mode == "printer": |
| 1190 |
if self.remotecupsqueue: |
| 1191 |
order = [ |
| 1192 |
self.PAGE_SELECT_DEVICE, |
| 1193 |
self.PAGE_DESCRIBE_PRINTER |
| 1194 |
] |
| 1195 |
elif (self.founddownloadabledrivers and |
| 1196 |
not self.rbtnNPDownloadableDriverSearch.get_active()): |
| 1197 |
if self.exactdrivermatch: |
| 1198 |
order = [ |
| 1199 |
self.PAGE_SELECT_DEVICE, |
| 1200 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1201 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1202 |
self.PAGE_DESCRIBE_PRINTER |
| 1203 |
] |
| 1204 |
else: |
| 1205 |
order = [ |
| 1206 |
self.PAGE_SELECT_DEVICE, |
| 1207 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1208 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1209 |
self.PAGE_CHOOSE_DRIVER_FROM_DB, |
| 1210 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1211 |
self.PAGE_DESCRIBE_PRINTER |
| 1212 |
] |
| 1213 |
elif (self.exactdrivermatch and |
| 1214 |
not self.rbtnNPDownloadableDriverSearch.get_active()): |
| 1215 |
order = [ |
| 1216 |
self.PAGE_SELECT_DEVICE, |
| 1217 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1218 |
self.PAGE_DESCRIBE_PRINTER |
| 1219 |
] |
| 1220 |
elif self.rbtnNPFoomatic.get_active(): |
| 1221 |
order = [ |
| 1222 |
self.PAGE_SELECT_DEVICE, |
| 1223 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1224 |
self.PAGE_CHOOSE_DRIVER_FROM_DB, |
| 1225 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1226 |
self.PAGE_DESCRIBE_PRINTER |
| 1227 |
] |
| 1228 |
elif self.rbtnNPPPD.get_active(): |
| 1229 |
order = [ |
| 1230 |
self.PAGE_SELECT_DEVICE, |
| 1231 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1232 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1233 |
self.PAGE_DESCRIBE_PRINTER |
| 1234 |
] |
| 1235 |
else: |
| 1236 |
# Downloadable driver |
| 1237 |
order = [ |
| 1238 |
self.PAGE_SELECT_DEVICE, |
| 1239 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1240 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1241 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1242 |
self.PAGE_DESCRIBE_PRINTER |
| 1243 |
] |
| 1244 |
elif self.dialog_mode == "ppd": |
| 1245 |
if self.rbtnNPFoomatic.get_active(): |
| 1246 |
order = [ |
| 1247 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1248 |
self.PAGE_CHOOSE_DRIVER_FROM_DB, |
| 1249 |
self.PAGE_APPLY, |
| 1250 |
self.PAGE_INSTALLABLE_OPTIONS |
| 1251 |
] |
| 1252 |
elif self.rbtnNPPPD.get_active(): |
| 1253 |
order = [ |
| 1254 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1255 |
self.PAGE_APPLY, |
| 1256 |
self.PAGE_INSTALLABLE_OPTIONS |
| 1257 |
] |
| 1258 |
else: |
| 1259 |
# Downloadable driver |
| 1260 |
order = [ |
| 1261 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1262 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1263 |
self.PAGE_APPLY, |
| 1264 |
self.PAGE_INSTALLABLE_OPTIONS |
| 1265 |
] |
| 1266 |
elif self.dialog_mode == "device": |
| 1267 |
order = [ |
| 1268 |
self.PAGE_SELECT_DEVICE, |
| 1269 |
] |
| 1270 |
else: # dialog_mode == "printer-from-uri" |
| 1271 |
if self.remotecupsqueue: |
| 1272 |
order = [ |
| 1273 |
self.PAGE_DESCRIBE_PRINTER, |
| 1274 |
] |
| 1275 |
elif (self.founddownloadabledrivers and |
| 1276 |
not self.rbtnNPDownloadableDriverSearch.get_active()): |
| 1277 |
if self.exactdrivermatch: |
| 1278 |
order = [ |
| 1279 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1280 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1281 |
self.PAGE_DESCRIBE_PRINTER |
| 1282 |
] |
| 1283 |
else: |
| 1284 |
order = [ |
| 1285 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1286 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1287 |
self.PAGE_CHOOSE_DRIVER_FROM_DB, |
| 1288 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1289 |
self.PAGE_DESCRIBE_PRINTER |
| 1290 |
] |
| 1291 |
elif (self.exactdrivermatch and |
| 1292 |
not self.rbtnNPDownloadableDriverSearch.get_active()): |
| 1293 |
order = [ |
| 1294 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1295 |
self.PAGE_DESCRIBE_PRINTER |
| 1296 |
] |
| 1297 |
elif self.rbtnNPFoomatic.get_active(): |
| 1298 |
order = [ |
| 1299 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1300 |
self.PAGE_CHOOSE_DRIVER_FROM_DB, |
| 1301 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1302 |
self.PAGE_DESCRIBE_PRINTER |
| 1303 |
] |
| 1304 |
elif self.rbtnNPPPD.get_active(): |
| 1305 |
order = [ |
| 1306 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1307 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1308 |
self.PAGE_DESCRIBE_PRINTER |
| 1309 |
] |
| 1310 |
else: |
| 1311 |
# Downloadable driver |
| 1312 |
order = [ |
| 1313 |
self.PAGE_SELECT_INSTALL_METHOD, |
| 1314 |
self.PAGE_DOWNLOAD_DRIVER, |
| 1315 |
self.PAGE_INSTALLABLE_OPTIONS, |
| 1316 |
self.PAGE_DESCRIBE_PRINTER |
| 1317 |
] |
| 1318 |
return order |
| 1319 |
|
| 1320 |
def _handlePrinterInstallationMode (self, step): |
| 1321 |
busy (self.NewPrinterWindow) |
| 1322 |
|
| 1323 |
page_nr = self.ntbkNewPrinter.get_current_page () |
| 1324 |
page_nr = self.ntbkNewPrinter.get_current_page () |
| 1325 |
|
| 1326 |
# Let's assume everything will be completed by default |
| 1327 |
result = self.INSTALL_RESULT_DONE |
| 1328 |
|
| 1329 |
if (((page_nr == self.PAGE_SELECT_DEVICE or |
| 1330 |
page_nr == self.PAGE_DOWNLOAD_DRIVER) and step > 0) or |
| 1331 |
((page_nr == self.PAGE_SELECT_INSTALL_METHOD or |
| 1332 |
page_nr == self.PAGE_DOWNLOAD_DRIVER) and step == 0)): |
| 1333 |
# The function below will return INSTALL_RESULT_OPS_PENDING |
| 1334 |
# if, for some reason, it spawns an asynchronous operation |
| 1335 |
# and needs to wait for its results before continuing. |
| 1336 |
result = self._handlePrinterInstallationStage (page_nr, step) |
| 1337 |
|
| 1338 |
if page_nr == self.PAGE_CHOOSE_DRIVER_FROM_DB: |
| 1339 |
if not self.device.id: |
| 1340 |
# Choose an appropriate name when no Device ID |
| 1341 |
# is available, based on the model the user has |
| 1342 |
# selected. |
| 1343 |
try: |
| 1344 |
model, iter = self.tvNPModels.get_selection ().\ |
| 1345 |
get_selected () |
| 1346 |
name = model.get(iter, 0)[0] |
| 1347 |
name = self.makeNameUnique (name) |
| 1348 |
self.entNPName.set_text (name) |
| 1349 |
except: |
| 1350 |
nonfatalException () |
| 1351 |
|
| 1352 |
ready (self.NewPrinterWindow) |
| 1353 |
return result |
| 1354 |
|
| 1355 |
def _handlePrinterInstallationStage (self, page_nr, step): |
| 1356 |
if self.dialog_mode != "download_driver": |
| 1357 |
uri = self.device.uri |
| 1358 |
if uri and uri.startswith ("smb"): |
| 1359 |
# User has selected an smb device |
| 1360 |
uri = SMBURI (uri=uri[6:]).sanitize_uri () |
| 1361 |
self._installSMBBackendIfNeeded () |
| 1362 |
|
| 1363 |
if page_nr == self.PAGE_SELECT_DEVICE or page_nr == self.PAGE_SELECT_INSTALL_METHOD: |
| 1364 |
self._selectDeviceForInstallation (uri) |
| 1365 |
elif page_nr == self.PAGE_DOWNLOAD_DRIVER and self.nextnptab_rerun == False: |
| 1366 |
self._handleDriverInstallation () |
| 1367 |
|
| 1368 |
devid = None |
| 1369 |
if not self.remotecupsqueue or self.dialog_mode == "ppd": |
| 1370 |
if self.dialog_mode != "download_driver": |
| 1371 |
devid = self.device.id # ID of selected device |
| 1372 |
if not devid: |
| 1373 |
devid = self.devid # ID supplied at init() |
| 1374 |
if not devid: |
| 1375 |
devid = None |
| 1376 |
|
| 1377 |
if self.ppds is None and self.dialog_mode != "download_driver": |
| 1378 |
self._loadPPDsForDevice (devid, uri) |
| 1379 |
# _loadPPDsForDevice () is an asynchronous operation, so |
| 1380 |
# let the caller know that it can't continue for now. |
| 1381 |
return self.INSTALL_RESULT_OPS_PENDING |
| 1382 |
|
| 1383 |
self.nextnptab_rerun = False |
| 1384 |
if page_nr == self.PAGE_SELECT_DEVICE or page_nr == self.PAGE_SELECT_INSTALL_METHOD: |
| 1385 |
self._installHPScannerFilesIfNeeded () |
| 1386 |
|
| 1387 |
return self._installPrinterFromDeviceID (devid, page_nr, step) |
| 1388 |
|
| 1389 |
def _installSMBBackendIfNeeded (self): |
| 1390 |
# Does the backend need to be installed? |
| 1391 |
if (self.nextnptab_rerun == False and not self.searchedfordriverpackages and |
| 1392 |
(self._host == 'localhost' or self._host[0] == '/') and |
| 1393 |
not os.access ("/usr/lib/cups/backend/smb", os.F_OK)): |
| 1394 |
debugprint ("No smb backend so attempting install") |
| 1395 |
try: |
| 1396 |
pk = installpackage.PackageKit () |
| 1397 |
# The following call means a blocking, synchronous, D-Bus call |
| 1398 |
pk.InstallPackageName (0, 0, "samba-client") |
| 1399 |
except: |
| 1400 |
nonfatalException () |
| 1401 |
|
| 1402 |
def _selectDeviceForInstallation (self, uri): |
| 1403 |
self._initialiseAutoVariables () |
| 1404 |
self.device.uri = self.getDeviceURI () |
| 1405 |
|
| 1406 |
# Cancel the printer finder now as the user has |
| 1407 |
# already selected their device. |
| 1408 |
if self.fetchDevices_conn: |
| 1409 |
self.fetchDevices_conn.destroy () |
| 1410 |
self.fetchDevices_conn = None |
| 1411 |
self.dec_spinner_task () |
| 1412 |
if self.printer_finder: |
| 1413 |
self.printer_finder.cancel () |
| 1414 |
self.printer_finder = None |
| 1415 |
self.dec_spinner_task () |
| 1416 |
|
| 1417 |
if (not self.device.id and |
| 1418 |
self.device.type in ["socket", "lpd", "ipp"]): |
| 1419 |
# This is a network printer whose model we don't yet know. |
| 1420 |
# Try to discover it. |
| 1421 |
self.getNetworkPrinterMakeModel () |
| 1422 |
|
| 1423 |
# Try to access the PPD, in this case our detected IPP |
| 1424 |
# printer is a queue on a remote CUPS server which is |
| 1425 |
# not automatically set up on our local CUPS server |
| 1426 |
# (for example DNS-SD broadcasted queue from Mac OS X) |
| 1427 |
self.remotecupsqueue = None |
| 1428 |
res = re.search (r"ipp://(\S+?)(:\d+|)/printers/(\S+)", uri) |
| 1429 |
if res: |
| 1430 |
resg = res.groups() |
| 1431 |
if len (resg[1]) > 0: |
| 1432 |
port = int (resg[1][1:]) |
| 1433 |
else: |
| 1434 |
port = 631 |
| 1435 |
try: |
| 1436 |
debugprint('Download ppd file from remote server') |
| 1437 |
conn = http.client.HTTPConnection(resg[0], port) |
| 1438 |
conn.request("GET", "/printers/%s.ppd" % resg[2]) |
| 1439 |
resp = conn.getresponse() |
| 1440 |
if resp.status == 200: |
| 1441 |
self.remotecupsqueue = resg[2] |
| 1442 |
|
| 1443 |
ppdcontent = resp.read() |
| 1444 |
|
| 1445 |
with tempfile.NamedTemporaryFile () as tmpf: |
| 1446 |
tmpf.write(ppdcontent) |
| 1447 |
tmpf.flush() |
| 1448 |
try: |
| 1449 |
ppd = cups.PPD(tmpf.name) |
| 1450 |
except (cups.IPPError, RuntimeError): |
| 1451 |
raise IOError("Server's ppd file is corrupted.") |
| 1452 |
except: |
| 1453 |
pass |
| 1454 |
|
| 1455 |
# We also want to fetch the printer-info and |
| 1456 |
# printer-location attributes, to pre-fill those |
| 1457 |
# fields for this new queue. |
| 1458 |
try: |
| 1459 |
encryption = cups.HTTP_ENCRYPT_IF_REQUESTED |
| 1460 |
c = cups.Connection (host=resg[0], |
| 1461 |
port=port, |
| 1462 |
encryption=encryption) |
| 1463 |
|
| 1464 |
r = ['printer-info', 'printer-location'] |
| 1465 |
attrs = c.getPrinterAttributes (uri=uri, |
| 1466 |
requested_attributes=r) |
| 1467 |
info = attrs.get ('printer-info', '') |
| 1468 |
location = attrs.get ('printer-location', '') |
| 1469 |
if len (info) > 0: |
| 1470 |
self.entNPDescription.set_text (info) |
| 1471 |
if len (location) > 0: |
| 1472 |
self.device.location = location |
| 1473 |
except RuntimeError: |
| 1474 |
pass |
| 1475 |
except: |
| 1476 |
nonfatalException () |
| 1477 |
elif ((uri.startswith ("dnssd:") or uri.startswith("mdns:")) and |
| 1478 |
uri.find ("/cups") != -1 and self.device.info): |
| 1479 |
# Remote CUPS queue discovered by "dnssd" CUPS backend |
| 1480 |
self.remotecupsqueue = self.device.info |
| 1481 |
|
| 1482 |
def _handleDriverInstallation (self): |
| 1483 |
# Install package of the driver found on OpenPrinting |
| 1484 |
treeview = self.tvNPDownloadableDrivers |
| 1485 |
model, iter = treeview.get_selection ().get_selected () |
| 1486 |
driver = None |
| 1487 |
if iter is not None: |
| 1488 |
driver = model.get_value (iter, 1) |
| 1489 |
if driver is None or driver == 0 or 'packages' not in driver: |
| 1490 |
return |
| 1491 |
|
| 1492 |
# Find the package name, repository, and fingerprint |
| 1493 |
# and install the package |
| 1494 |
result = self.installdriverpackage (driver) |
| 1495 |
if not result or len(self.installed_driver_files) == 0: |
| 1496 |
return |
| 1497 |
|
| 1498 |
# We actually installed a package, delete the |
| 1499 |
# PPD list to get it regenerated |
| 1500 |
self.ppds = None |
| 1501 |
|
| 1502 |
if (self.dialog_mode != "download_driver" and |
| 1503 |
(not self.device.id and |
| 1504 |
(not self.device.make_and_model or self.device.make_and_model == "Unknown") and |
| 1505 |
self.downloadable_driver_for_printer)): |
| 1506 |
self.device.make_and_model = self.downloadable_driver_for_printer |
| 1507 |
|
| 1508 |
def _installHPScannerFilesIfNeeded (self): |
| 1509 |
if (hasattr (self.device, 'hp_scannable') and self.device.hp_scannable and |
| 1510 |
not os.access ("/etc/sane.d/dll.d/hpaio", os.R_OK) and |
| 1511 |
not os.access ("/etc/sane.d/dll.d/hplip", os.R_OK)): |
| 1512 |
debugprint ("No HPLIP sane backend so " |
| 1513 |
"attempting install") |
| 1514 |
try: |
| 1515 |
pk = installpackage.PackageKit () |
| 1516 |
# The following call means a blocking, synchronous, D-Bus call |
| 1517 |
pk.InstallPackageName (0, 0, "libsane-hpaio") |
| 1518 |
except: |
| 1519 |
pass |
| 1520 |
|
| 1521 |
def _loadPPDsForDevice (self, devid, uri): |
| 1522 |
debugprint ("nextNPTab: need PPDs loaded") |
| 1523 |
p = ppdsloader.PPDsLoader (device_id=devid, |
| 1524 |
device_uri=uri, |
| 1525 |
parent=self.NewPrinterWindow, |
| 1526 |
host=self._host, |
| 1527 |
encryption=self._encryption) |
| 1528 |
self.ppdsloader = p |
| 1529 |
p.connect ('finished',self.on_ppdsloader_finished_next) |
| 1530 |
p.run () |
| 1531 |
|
| 1532 |
def _installPrinterFromDeviceID (self, devid, page_nr, step): |
| 1533 |
ppdname = None |
| 1534 |
self.id_matched_ppdnames = [] |
| 1535 |
try: |
| 1536 |
if self.dialog_mode == "download_driver": |
| 1537 |
ppdname = "download" |
| 1538 |
status = "generic" |
| 1539 |
elif self.remotecupsqueue: |
| 1540 |
# We have a remote CUPS queue, let the client queue |
| 1541 |
# stay raw so that the driver on the server gets used |
| 1542 |
if self.ppd is None: |
| 1543 |
ppdname = 'raw' |
| 1544 |
self.ppd = ppdname |
| 1545 |
name = self.remotecupsqueue |
| 1546 |
name = self.makeNameUnique (name) |
| 1547 |
self.entNPName.set_text (name) |
| 1548 |
status = "exact" |
| 1549 |
elif (self.device.id or |
| 1550 |
(self.device.make_and_model and |
| 1551 |
self.device.make_and_model != "Unknown") or |
| 1552 |
devid): |
| 1553 |
if self.device.id: |
| 1554 |
id_dict = self.device.id_dict |
| 1555 |
elif devid: |
| 1556 |
id_dict = cupshelpers.parseDeviceID (devid) |
| 1557 |
else: |
| 1558 |
id_dict = {} |
| 1559 |
(id_dict["MFG"], |
| 1560 |
id_dict["MDL"]) = cupshelpers.ppds.\ |
| 1561 |
ppdMakeModelSplit (self.device.make_and_model) |
| 1562 |
id_dict["DES"] = "" |
| 1563 |
id_dict["CMD"] = [] |
| 1564 |
devid = "MFG:%s;MDL:%s;" % (id_dict["MFG"], |
| 1565 |
id_dict["MDL"]) |
| 1566 |
|
| 1567 |
fit = self.ppds.\ |
| 1568 |
getPPDNamesFromDeviceID (id_dict["MFG"], |
| 1569 |
id_dict["MDL"], |
| 1570 |
id_dict["DES"], |
| 1571 |
id_dict["CMD"], |
| 1572 |
self.device.uri, |
| 1573 |
self.device.make_and_model) |
| 1574 |
debugprint ("Suitable PPDs found: %s" % repr(fit)) |
| 1575 |
ppdnamelist = self.ppds.\ |
| 1576 |
orderPPDNamesByPreference (list(fit.keys ()), |
| 1577 |
self.installed_driver_files, |
| 1578 |
devid=id_dict, fit=fit) |
| 1579 |
debugprint ("PPDs in priority order: %s" % repr(ppdnamelist)) |
| 1580 |
self.id_matched_ppdnames = ppdnamelist |
| 1581 |
ppdname = ppdnamelist[0] |
| 1582 |
status = fit[ppdname] |
| 1583 |
elif (self.dialog_mode == "ppd" and self.orig_ppd): |
| 1584 |
attr = self.orig_ppd.findAttr("NickName") |
| 1585 |
if not attr: |
| 1586 |
attr = self.orig_ppd.findAttr("ModelName") |
| 1587 |
|
| 1588 |
if attr and attr.value: |
| 1589 |
value = attr.value |
| 1590 |
if value.endswith (" (recommended)"): |
| 1591 |
value = value[:-14] |
| 1592 |
|
| 1593 |
mfgmdl = cupshelpers.ppds.ppdMakeModelSplit (value) |
| 1594 |
(make, model) = mfgmdl |
| 1595 |
|
| 1596 |
# Search for ppdname with that make-and-model |
| 1597 |
ppds = self.ppds.getInfoFromModel (make, model) |
| 1598 |
for ppd, info in ppds.items (): |
| 1599 |
if (_singleton (info. |
| 1600 |
get ("ppd-make-and-model")) == |
| 1601 |
value): |
| 1602 |
ppdname = ppd |
| 1603 |
break |
| 1604 |
if ppdname: |
| 1605 |
status = "exact" |
| 1606 |
else: |
| 1607 |
ppdname = 'raw' |
| 1608 |
self.ppd = ppdname |
| 1609 |
status = "generic" |
| 1610 |
elif self.dialog_mode == "ppd": |
| 1611 |
# Special CUPS names for a raw queue. |
| 1612 |
ppdname = 'raw' |
| 1613 |
self.ppd = ppdname |
| 1614 |
status = "exact" |
| 1615 |
else: |
| 1616 |
(status, ppdname) = self.ppds.\ |
| 1617 |
getPPDNameFromDeviceID ("Generic", |
| 1618 |
"Printer", |
| 1619 |
"Generic Printer", |
| 1620 |
[], |
| 1621 |
self.device.uri) |
| 1622 |
status = "generic" |
| 1623 |
except: |
| 1624 |
nonfatalException () |
| 1625 |
|
| 1626 |
if (ppdname and |
| 1627 |
(not self.remotecupsqueue or self.dialog_mode == "ppd")): |
| 1628 |
return self._installPrinterOrSearchForDriver (devid, ppdname, status, page_nr, step) |
| 1629 |
|
| 1630 |
# No operations are pending if reached. |
| 1631 |
return self.INSTALL_RESULT_DONE |
| 1632 |
|
| 1633 |
def _installPrinterOrSearchForDriver (self, devid, ppdname, status, page_nr, step): |
| 1634 |
try: |
| 1635 |
if ppdname != "download": |
| 1636 |
ppddict = self.ppds.getInfoFromPPDName (ppdname) |
| 1637 |
make_model = _singleton (ppddict['ppd-make-and-model']) |
| 1638 |
(make, model) = \ |
| 1639 |
cupshelpers.ppds.ppdMakeModelSplit (make_model) |
| 1640 |
self.auto_make = make |
| 1641 |
self.auto_model = model |
| 1642 |
self.auto_driver = ppdname |
| 1643 |
self.fillDriverList(make, model) |
| 1644 |
if ((status == "exact" or status == "exact-cmd") and \ |
| 1645 |
self.dialog_mode != "ppd"): |
| 1646 |
self.exactdrivermatch = True |
| 1647 |
if step == 0: |
| 1648 |
page_nr = self.PAGE_INSTALLABLE_OPTIONS; |
| 1649 |
else: |
| 1650 |
self.exactdrivermatch = False |
| 1651 |
if (self.dialog_mode != "ppd" and |
| 1652 |
self.searchedfordriverpackages == False and |
| 1653 |
devid and len(devid) > 0 and |
| 1654 |
not (devid.find("MFG:generic;") >= 0 or |
| 1655 |
devid.find("MFG:Generic;") >= 0 or |
| 1656 |
devid.find("MFG:unknown") >= 0 or |
| 1657 |
devid.find("MFG:Unknown") >= 0 or |
| 1658 |
devid.find("MDL:unknown") >= 0 or |
| 1659 |
devid.find("MDL:Unknown") >= 0 or |
| 1660 |
devid.find("MFG:;") >= 0 or |
| 1661 |
devid.find("MDL:;") >= 0)): |
| 1662 |
# Query driver packages and PPD files on |
| 1663 |
# OpenPrinting |
| 1664 |
debugprint ("nextNPTab: No exact driver match, querying OpenPrinting") |
| 1665 |
debugprint ('nextNPTab: Searching for "%s"' % devid) |
| 1666 |
self.searchedfordriverpackages = True |
| 1667 |
|
| 1668 |
self._searchdialog_canceled = False |
| 1669 |
fmt = _("Searching") |
| 1670 |
self._searchdialog = Gtk.MessageDialog ( |
| 1671 |
parent=self.NewPrinterWindow, |
| 1672 |
modal=True, |
| 1673 |
destroy_with_parent=True, |
| 1674 |
message_type=Gtk.MessageType.INFO, |
| 1675 |
buttons=Gtk.ButtonsType.CANCEL, |
| 1676 |
text=fmt) |
| 1677 |
|
| 1678 |
self._searchdialog.format_secondary_text ( |
| 1679 |
_("Searching for drivers")) |
| 1680 |
|
| 1681 |
self.opreq = OpenPrintingRequest () |
| 1682 |
self._searchdialog.connect ( |
| 1683 |
"response", self._searchdialog_response) |
| 1684 |
self._searchdialog.show_all () |
| 1685 |
|
| 1686 |
self.opreq_handlers = [] |
| 1687 |
self.opreq_handlers.append ( |
| 1688 |
self.opreq.connect ( |
| 1689 |
'finished', |
| 1690 |
self.opreq_id_search_done)) |
| 1691 |
self.opreq_handlers.append ( |
| 1692 |
self.opreq.connect ( |
| 1693 |
'error', |
| 1694 |
self.opreq_id_search_error)) |
| 1695 |
self.opreq_user_search = False |
| 1696 |
self.opreq.searchPrinters (devid) |
| 1697 |
|
| 1698 |
# Searching for drivers in OpenPrinting takes times, so |
| 1699 |
# let the caller know that it can't continue for now. |
| 1700 |
return self.INSTALL_RESULT_OPS_PENDING |
| 1701 |
except: |
| 1702 |
nonfatalException () |
| 1703 |
|
| 1704 |
if (self.dialog_mode == "ppd" or |
| 1705 |
(self.dialog_mode != "download_driver" and |
| 1706 |
not self.remotecupsqueue and page_nr != self.PAGE_DOWNLOAD_DRIVER)): |
| 1707 |
self.fillMakeList() |
| 1708 |
|
| 1709 |
# No operations are pending if reached. |
| 1710 |
return self.INSTALL_RESULT_DONE |
| 1711 |
|
| 1712 |
def _searchdialog_response (self, dialog, response): |
| 1713 |
# Cancel clicked while performing openprinting search |
| 1714 |
|
| 1715 |
self.btnNPDownloadableDriverSearch.set_sensitive (True) |
| 1716 |
self.btnNPDownloadableDriverSearch_label.set_text (_("Search")) |
| 1717 |
|
| 1718 |
self.installed_driver_files = [] |
| 1719 |
self.searchedfordriverpackages = True |
| 1720 |
self.founddownloadabledrivers = False |
| 1721 |
self.founddownloadableppd = False |
| 1722 |
|
| 1723 |
ready (self.NewPrinterWindow) |
| 1724 |
|
| 1725 |
# Cancel the openprinting request. |
| 1726 |
GLib.idle_add (self.opreq.cancel) |
| 1727 |
|
| 1728 |
def opreq_id_search_done (self, opreq, printers, drivers): |
| 1729 |
for handler in self.opreq_handlers: |
| 1730 |
opreq.disconnect (handler) |
| 1731 |
|
| 1732 |
Gdk.threads_enter () |
| 1733 |
|
| 1734 |
try: |
| 1735 |
self.opreq_user_search = False |
| 1736 |
self.opreq_handlers = None |
| 1737 |
self.opreq = None |
| 1738 |
self._searchdialog.hide () |
| 1739 |
self._searchdialog.destroy () |
| 1740 |
self._searchdialog = None |
| 1741 |
|
| 1742 |
# Check whether we have found something |
| 1743 |
if len (printers) < 1: |
| 1744 |
# No. |
| 1745 |
ready (self.NewPrinterWindow) |
| 1746 |
|
| 1747 |
self.founddownloadabledrivers = False |
| 1748 |
if self.dialog_mode == "download_driver": |
| 1749 |
self.on_NPCancel(None) |
| 1750 |
else: |
| 1751 |
self.nextNPTab () |
| 1752 |
else: |
| 1753 |
self.downloadable_printers = printers |
| 1754 |
self.downloadable_drivers = drivers |
| 1755 |
self.founddownloadabledrivers = True |
| 1756 |
|
| 1757 |
try: |
| 1758 |
self.NewPrinterWindow.show() |
| 1759 |
self.setNPButtons() |
| 1760 |
if not self.fillDownloadableDrivers(): |
| 1761 |
ready (self.NewPrinterWindow) |
| 1762 |
|
| 1763 |
self.founddownloadabledrivers = False |
| 1764 |
if self.dialog_mode == "download_driver": |
| 1765 |
self.on_NPCancel(None) |
| 1766 |
else: |
| 1767 |
self.nextNPTab () |
| 1768 |
else: |
| 1769 |
if self.dialog_mode == "download_driver": |
| 1770 |
self.nextNPTab (step = 0) |
| 1771 |
else: |
| 1772 |
self.nextNPTab () |
| 1773 |
except: |
| 1774 |
nonfatalException () |
| 1775 |
self.nextNPTab () |
| 1776 |
|
| 1777 |
finally: |
| 1778 |
Gdk.threads_leave () |
| 1779 |
|
| 1780 |
def opreq_id_search_error (self, opreq, status, err): |
| 1781 |
debugprint ("OpenPrinting request failed (%d): %s" % (status, |
| 1782 |
repr (err))) |
| 1783 |
self.opreq_id_search_done (opreq, list(), dict()) |
| 1784 |
|
| 1785 |
|
| 1786 |
def _installSelectedDriverFromOpenPrinting(self): |
| 1787 |
# Install package of the driver found on OpenPrinting |
| 1788 |
treeview = self.tvNPDownloadableDrivers |
| 1789 |
model, iter = treeview.get_selection ().get_selected () |
| 1790 |
driver = None |
| 1791 |
if iter is not None: |
| 1792 |
driver = model.get_value (iter, 1) |
| 1793 |
if (driver is None or driver == 0 or 'packages' not in driver): |
| 1794 |
return |
| 1795 |
|
| 1796 |
# Find the package name, repository, and fingerprint |
| 1797 |
# and install the package |
| 1798 |
if not self.installdriverpackage (driver) or \ |
| 1799 |
len(self.installed_driver_files) <= 0: |
| 1800 |
return |
| 1801 |
|
| 1802 |
# We actually installed a package, delete the |
| 1803 |
# PPD list to get it regenerated |
| 1804 |
self.ppds = None |
| 1805 |
if self.dialog_mode != "download_driver": |
| 1806 |
if (not self.device.id and |
| 1807 |
(not self.device.make_and_model or |
| 1808 |
self.device.make_and_model == |
| 1809 |
"Unknown") and |
| 1810 |
self.downloadable_driver_for_printer): |
| 1811 |
self.device.make_and_model = \ |
| 1812 |
self.downloadable_driver_for_printer |
| 1813 |
|
| 1814 |
def setNPButtons(self): |
| 1815 |
nr = self.ntbkNewPrinter.get_current_page() |
| 1816 |
|
| 1817 |
if self.dialog_mode == "device": |
| 1818 |
self.btnNPBack.hide() |
| 1819 |
self.btnNPForward.hide() |
| 1820 |
self.btnNPApply.show() |
| 1821 |
try: |
| 1822 |
uri = self.getDeviceURI () |
| 1823 |
valid = validDeviceURI (uri) |
| 1824 |
except AttributeError: |
| 1825 |
# No device selected yet. |
| 1826 |
valid = False |
| 1827 |
self.btnNPApply.set_sensitive (valid) |
| 1828 |
return |
| 1829 |
|
| 1830 |
if self.dialog_mode == "ppd": |
| 1831 |
if nr == self.PAGE_APPLY: |
| 1832 |
if not self.installable_options: |
| 1833 |
# There are no installable options, so this is the |
| 1834 |
# last page. |
| 1835 |
debugprint ("No installable options") |
| 1836 |
self.btnNPForward.hide () |
| 1837 |
self.btnNPApply.show () |
| 1838 |
else: |
| 1839 |
self.btnNPForward.show () |
| 1840 |
self.btnNPApply.hide () |
| 1841 |
return |
| 1842 |
elif nr == self.PAGE_INSTALLABLE_OPTIONS: |
| 1843 |
self.btnNPForward.hide() |
| 1844 |
self.btnNPApply.show() |
| 1845 |
return |
| 1846 |
else: |
| 1847 |
self.btnNPForward.show() |
| 1848 |
self.btnNPApply.hide() |
| 1849 |
if nr == self.PAGE_SELECT_INSTALL_METHOD: |
| 1850 |
self.btnNPBack.hide() |
| 1851 |
self.btnNPForward.show() |
| 1852 |
downloadable_selected = False |
| 1853 |
if self.rbtnNPDownloadableDriverSearch.get_active (): |
| 1854 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 1855 |
iter = combobox.get_active_iter () |
| 1856 |
if iter and combobox.get_model ().get_value (iter, 1): |
| 1857 |
downloadable_selected = True |
| 1858 |
|
| 1859 |
self.btnNPForward.set_sensitive(bool( |
| 1860 |
(self.rbtnNPFoomatic.get_active() and |
| 1861 |
self.tvNPMakes.get_cursor()[0] is not None) or |
| 1862 |
self.filechooserPPD.get_filename() or |
| 1863 |
downloadable_selected)) |
| 1864 |
return |
| 1865 |
else: |
| 1866 |
self.btnNPBack.show() |
| 1867 |
|
| 1868 |
if self.dialog_mode == "download_driver": |
| 1869 |
self.btnNPBack.hide() |
| 1870 |
self.btnNPForward.hide() |
| 1871 |
self.btnNPApply.show() |
| 1872 |
|
| 1873 |
accepted = self._is_driver_license_accepted() |
| 1874 |
self.btnNPApply.set_sensitive(accepted) |
| 1875 |
return |
| 1876 |
|
| 1877 |
# class/printer |
| 1878 |
|
| 1879 |
if nr == self.PAGE_SELECT_DEVICE: |
| 1880 |
valid = False |
| 1881 |
try: |
| 1882 |
uri = self.getDeviceURI () |
| 1883 |
valid = validDeviceURI (uri) |
| 1884 |
except: |
| 1885 |
nonfatalException () |
| 1886 |
self.btnNPForward.set_sensitive(valid) |
| 1887 |
self.btnNPBack.hide () |
| 1888 |
else: |
| 1889 |
self.btnNPBack.show() |
| 1890 |
|
| 1891 |
self.btnNPForward.show() |
| 1892 |
self.btnNPApply.hide() |
| 1893 |
|
| 1894 |
if nr == self.PAGE_DESCRIBE_PRINTER: |
| 1895 |
self.btnNPBack.show() |
| 1896 |
if self.dialog_mode == "printer" or \ |
| 1897 |
self.dialog_mode == "printer_with_uri": |
| 1898 |
self.btnNPForward.hide() |
| 1899 |
self.btnNPApply.show() |
| 1900 |
self.btnNPApply.set_sensitive( |
| 1901 |
checkNPName(self.printers, self.entNPName.get_text())) |
| 1902 |
if self.dialog_mode == "class": |
| 1903 |
# This is the first page for the New Class dialog, so |
| 1904 |
# hide the Back button. |
| 1905 |
self.btnNPBack.hide () |
| 1906 |
if self.dialog_mode == "printer_with_uri" and \ |
| 1907 |
(self.remotecupsqueue or \ |
| 1908 |
(self.exactdrivermatch and \ |
| 1909 |
not self.installable_options)): |
| 1910 |
self.btnNPBack.hide () |
| 1911 |
if nr == self.PAGE_SELECT_INSTALL_METHOD: |
| 1912 |
downloadable_selected = False |
| 1913 |
if self.rbtnNPDownloadableDriverSearch.get_active (): |
| 1914 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 1915 |
iter = combobox.get_active_iter () |
| 1916 |
if iter and combobox.get_model ().get_value (iter, 1): |
| 1917 |
downloadable_selected = True |
| 1918 |
|
| 1919 |
self.btnNPForward.set_sensitive(bool( |
| 1920 |
self.rbtnNPFoomatic.get_active() or |
| 1921 |
self.filechooserPPD.get_filename() or |
| 1922 |
downloadable_selected)) |
| 1923 |
# If we have an auto-detected printer for which there was no |
| 1924 |
# driver found, we have already the URI and so this step is |
| 1925 |
# not needed in the wizard. This makes manufacturer?PPD selection |
| 1926 |
# the first step |
| 1927 |
if self.dialog_mode == "printer_with_uri": |
| 1928 |
self.btnNPBack.hide() |
| 1929 |
if nr == self.PAGE_CHOOSE_DRIVER_FROM_DB: |
| 1930 |
model, iter = self.tvNPDrivers.get_selection().get_selected() |
| 1931 |
self.btnNPForward.set_sensitive(bool(iter)) |
| 1932 |
if nr == self.PAGE_CHOOSE_CLASS_MEMBERS: |
| 1933 |
self.btnNPForward.hide() |
| 1934 |
self.btnNPApply.show() |
| 1935 |
self.btnNPApply.set_sensitive( |
| 1936 |
bool(getCurrentClassMembers(self.tvNCMembers))) |
| 1937 |
if nr == self.PAGE_INSTALLABLE_OPTIONS: |
| 1938 |
if self.dialog_mode == "printer_with_uri" and \ |
| 1939 |
self.exactdrivermatch: |
| 1940 |
self.btnNPBack.hide () |
| 1941 |
if nr == self.PAGE_DOWNLOAD_DRIVER: |
| 1942 |
accepted = self._is_driver_license_accepted() |
| 1943 |
self.btnNPForward.set_sensitive(accepted) |
| 1944 |
|
| 1945 |
def _is_driver_license_accepted(self): |
| 1946 |
current_page = self.ntbkNPDownloadableDriverProperties.get_current_page() |
| 1947 |
if current_page == self.PAGE_SELECT_DEVICE: |
| 1948 |
return self.rbtnNPDownloadLicenseYes.get_active () |
| 1949 |
|
| 1950 |
treeview = self.tvNPDownloadableDrivers |
| 1951 |
model, iter = treeview.get_selection ().get_selected () |
| 1952 |
if not iter: |
| 1953 |
path, column = treeview.get_cursor() |
| 1954 |
if path: |
| 1955 |
iter = model.get_iter (path) |
| 1956 |
return iter is not None |
| 1957 |
|
| 1958 |
def on_entNPName_changed(self, widget): |
| 1959 |
# restrict |
| 1960 |
text = widget.get_text() |
| 1961 |
new_text = text |
| 1962 |
new_text = new_text.replace("/", "") |
| 1963 |
new_text = new_text.replace("#", "") |
| 1964 |
new_text = new_text.replace(" ", "") |
| 1965 |
if text!=new_text: |
| 1966 |
widget.set_text(new_text) |
| 1967 |
if self.dialog_mode == "printer": |
| 1968 |
self.btnNPApply.set_sensitive( |
| 1969 |
checkNPName(self.printers, new_text)) |
| 1970 |
else: |
| 1971 |
self.btnNPForward.set_sensitive( |
| 1972 |
checkNPName(self.printers, new_text)) |
| 1973 |
|
| 1974 |
def fetchDevices(self, network=False, current_uri=None): |
| 1975 |
debugprint ("fetchDevices") |
| 1976 |
self.inc_spinner_task () |
| 1977 |
|
| 1978 |
# Search for Bluetooth printers together with the network printers |
| 1979 |
# as the Bluetooth search takes rather long time |
| 1980 |
network_schemes = ["dnssd", "snmp", "driverless", "bjnp", "bluetooth"] |
| 1981 |
error_handler = self.error_getting_devices |
| 1982 |
if network == False: |
| 1983 |
reply_handler = (lambda x, y: |
| 1984 |
self.local_devices_reply (x, y, |
| 1985 |
current_uri)) |
| 1986 |
cupshelpers.getDevices (self.fetchDevices_conn, |
| 1987 |
exclude_schemes=network_schemes, |
| 1988 |
reply_handler=reply_handler, |
| 1989 |
error_handler=error_handler) |
| 1990 |
else: |
| 1991 |
reply_handler = (lambda x, y: |
| 1992 |
self.network_devices_reply (x, y, |
| 1993 |
current_uri)) |
| 1994 |
cupshelpers.getDevices (self.fetchDevices_conn, |
| 1995 |
include_schemes=network_schemes, |
| 1996 |
reply_handler=reply_handler, |
| 1997 |
error_handler=error_handler) |
| 1998 |
|
| 1999 |
def error_getting_devices (self, conn, exc): |
| 2000 |
# Just ignore the error. |
| 2001 |
debugprint ("Error fetching devices: %s" % repr (exc)) |
| 2002 |
self.dec_spinner_task () |
| 2003 |
self.fetchDevices_conn._end_operation () |
| 2004 |
self.fetchDevices_conn.destroy () |
| 2005 |
self.fetchDevices_conn = None |
| 2006 |
|
| 2007 |
def local_devices_reply (self, conn, result, current_uri): |
| 2008 |
self.dec_spinner_task () |
| 2009 |
|
| 2010 |
# Now we've got the local devices, start a request for the |
| 2011 |
# network devices. |
| 2012 |
self.fetchDevices (network=True, current_uri=current_uri) |
| 2013 |
|
| 2014 |
# Add the local devices to the list. |
| 2015 |
self.add_devices (result, current_uri) |
| 2016 |
|
| 2017 |
def network_devices_reply (self, conn, result, current_uri): |
| 2018 |
self.fetchDevices_conn._end_operation () |
| 2019 |
self.fetchDevices_conn.destroy () |
| 2020 |
self.fetchDevices_conn = None |
| 2021 |
|
| 2022 |
# Add the network devices to the list. |
| 2023 |
no_more = True |
| 2024 |
need_resolving = {} |
| 2025 |
for uri, device in result.items (): |
| 2026 |
if uri.startswith ("dnssd://"): |
| 2027 |
need_resolving[uri] = device |
| 2028 |
no_more = False |
| 2029 |
|
| 2030 |
for uri in need_resolving.keys (): |
| 2031 |
del result[uri] |
| 2032 |
|
| 2033 |
self.add_devices (result, current_uri, no_more=no_more) |
| 2034 |
|
| 2035 |
if len (need_resolving) > 0: |
| 2036 |
resolver = dnssdresolve.DNSSDHostNamesResolver (need_resolving) |
| 2037 |
self.inc_spinner_task () |
| 2038 |
resolver.resolve (reply_handler=lambda devices: |
| 2039 |
self.dnssd_resolve_reply (current_uri, |
| 2040 |
devices)) |
| 2041 |
|
| 2042 |
self.dec_spinner_task () |
| 2043 |
self.check_firewall () |
| 2044 |
|
| 2045 |
def dnssd_resolve_reply (self, current_uri, devices): |
| 2046 |
self.add_devices (devices, current_uri, no_more=True) |
| 2047 |
self.dec_spinner_task () |
| 2048 |
self.check_firewall () |
| 2049 |
|
| 2050 |
def get_hpfax_device_id(self, faxuri): |
| 2051 |
new_environ = os.environ.copy() |
| 2052 |
new_environ['LC_ALL'] = "C" |
| 2053 |
new_environ['DISPLAY'] = "" |
| 2054 |
args = ["hp-info", "-x", "-i", "-d" + faxuri] |
| 2055 |
debugprint (faxuri + ": " + repr(args)) |
| 2056 |
try: |
| 2057 |
p = subprocess.Popen (args, env=new_environ, close_fds=True, |
| 2058 |
stdin=subprocess.DEVNULL, |
| 2059 |
stdout=subprocess.PIPE, |
| 2060 |
stderr=subprocess.PIPE) |
| 2061 |
(stdout, stderr) = p.communicate () |
| 2062 |
except: |
| 2063 |
# Problem executing command. |
| 2064 |
return None |
| 2065 |
|
| 2066 |
faxtype = -1 |
| 2067 |
for line in stdout.decode ().split ("\n"): |
| 2068 |
if line.find ("fax-type") == -1: |
| 2069 |
continue |
| 2070 |
res = re.search (r"(\d+)", line) |
| 2071 |
if res: |
| 2072 |
resg = res.groups() |
| 2073 |
try: |
| 2074 |
faxtype = int(resg[0]) |
| 2075 |
except: |
| 2076 |
faxtype = -1 |
| 2077 |
if faxtype >= 0: |
| 2078 |
break |
| 2079 |
if faxtype <= 0: |
| 2080 |
return None |
| 2081 |
elif faxtype == 4: |
| 2082 |
return 'MFG:HP;MDL:Fax 2;DES:HP Fax 2;' |
| 2083 |
else: |
| 2084 |
return 'MFG:HP;MDL:Fax;DES:HP Fax;' |
| 2085 |
|
| 2086 |
def get_hplip_scan_type_for_uri(self, uri): |
| 2087 |
args = ["hp-query", "-k", "scan-type", "-d", uri] |
| 2088 |
debugprint (uri + ": " + repr(args)) |
| 2089 |
try: |
| 2090 |
p = subprocess.Popen (args, close_fds=True, |
| 2091 |
stdin=subprocess.DEVNULL, |
| 2092 |
stdout=subprocess.PIPE, |
| 2093 |
stderr=subprocess.PIPE) |
| 2094 |
(stdout, stderr) = p.communicate () |
| 2095 |
if p.returncode != 0: |
| 2096 |
return None |
| 2097 |
except: |
| 2098 |
# Problem executing command. |
| 2099 |
return None |
| 2100 |
|
| 2101 |
scan_type = stdout.decode ().strip () |
| 2102 |
fields = scan_type.split ("=", 1) |
| 2103 |
if len (fields) < 2: |
| 2104 |
return None |
| 2105 |
|
| 2106 |
value = fields[1] |
| 2107 |
if value == '0': |
| 2108 |
return None |
| 2109 |
|
| 2110 |
return value |
| 2111 |
|
| 2112 |
def get_hplip_uri_for_network_printer(self, host, mode): |
| 2113 |
if mode == "print": mod = "-c" |
| 2114 |
elif mode == "fax": mod = "-f" |
| 2115 |
else: mod = "-c" |
| 2116 |
args = ["hp-makeuri", mod, host] |
| 2117 |
debugprint (host + ": " + repr(args)) |
| 2118 |
uri = None |
| 2119 |
try: |
| 2120 |
p = subprocess.Popen (args, close_fds=True, |
| 2121 |
stdin=subprocess.DEVNULL, |
| 2122 |
stdout=subprocess.PIPE, |
| 2123 |
stderr=subprocess.PIPE) |
| 2124 |
(stdout, stderr) = p.communicate () |
| 2125 |
if p.returncode != 0: |
| 2126 |
return None |
| 2127 |
except: |
| 2128 |
# Problem executing command. |
| 2129 |
return None |
| 2130 |
|
| 2131 |
uri = stdout.decode ().strip () |
| 2132 |
return uri |
| 2133 |
|
| 2134 |
def getNetworkPrinterMakeModel(self, host=None, device=None): |
| 2135 |
""" |
| 2136 |
Try to determine the make and model for the currently selected |
| 2137 |
network printer, and store this in the data structure for the |
| 2138 |
printer. |
| 2139 |
Returns (hostname or None, uri or None). |
| 2140 |
""" |
| 2141 |
uri = None |
| 2142 |
if device is None: |
| 2143 |
device = self.device |
| 2144 |
# Determine host name/IP |
| 2145 |
if host is None: |
| 2146 |
s = device.uri.find ("://") |
| 2147 |
if s != -1: |
| 2148 |
s += 3 |
| 2149 |
e = device.uri[s:].find (":") |
| 2150 |
if e == -1: e = device.uri[s:].find ("/") |
| 2151 |
if e == -1: e = device.uri[s:].find ("?") |
| 2152 |
if e == -1: e = len (device.uri) |
| 2153 |
host = device.uri[s:s+e] |
| 2154 |
# Try to get make and model via SNMP |
| 2155 |
if host: |
| 2156 |
args = ["/usr/lib/cups/backend/snmp", host] |
| 2157 |
debugprint (host + ": " + repr(args)) |
| 2158 |
stdout = None |
| 2159 |
try: |
| 2160 |
p = subprocess.Popen (args, close_fds=True, |
| 2161 |
stdin=subprocess.DEVNULL, |
| 2162 |
stdout=subprocess.PIPE, |
| 2163 |
stderr=subprocess.PIPE) |
| 2164 |
(stdout, stderr) = p.communicate () |
| 2165 |
if p.returncode != 0: |
| 2166 |
stdout = None |
| 2167 |
except: |
| 2168 |
# Problem executing command. |
| 2169 |
pass |
| 2170 |
|
| 2171 |
if stdout is not None: |
| 2172 |
try: |
| 2173 |
line = stdout.decode ('utf-8').strip () |
| 2174 |
except UnicodeDecodeError: |
| 2175 |
# Work-around snmp backend output encoded as iso-8859-1 (despire RFC 2571). |
| 2176 |
# If it's neither iso-8859-1, make a best guess by ignoring problematic bytes. |
| 2177 |
line = stdout.decode (encoding='iso-8859-1', errors='ignore').strip () |
| 2178 |
words = probe_printer.wordsep (line) |
| 2179 |
n = len (words) |
| 2180 |
if n < 4: |
| 2181 |
words.extend (['','','','']) |
| 2182 |
words = words[:4] |
| 2183 |
n = 4 |
| 2184 |
elif n > 6: |
| 2185 |
words = words[:6] |
| 2186 |
n = 6 |
| 2187 |
|
| 2188 |
if n == 6: |
| 2189 |
(device_class, uri, make_and_model, |
| 2190 |
info, device_id, device_location) = words |
| 2191 |
elif n == 5: |
| 2192 |
(device_class, uri, make_and_model, |
| 2193 |
info, device_id) = words |
| 2194 |
elif n == 4: |
| 2195 |
(device_class, uri, make_and_model, info) = words |
| 2196 |
|
| 2197 |
if n == 4: |
| 2198 |
# No Device ID given so we'll have to make one |
| 2199 |
# up. |
| 2200 |
debugprint ("No Device ID from snmp backend") |
| 2201 |
(mk, md) = cupshelpers.ppds.\ |
| 2202 |
ppdMakeModelSplit (make_and_model) |
| 2203 |
device.id = "MFG:%s;MDL:%s;DES:%s %s;" % (mk, md, |
| 2204 |
mk, md) |
| 2205 |
else: |
| 2206 |
debugprint ("Got Device ID: %s" % device_id) |
| 2207 |
device.id = device_id |
| 2208 |
|
| 2209 |
device.id_dict = cupshelpers.parseDeviceID (device.id) |
| 2210 |
device.make_and_model = make_and_model |
| 2211 |
device.info = info |
| 2212 |
if n == 6: |
| 2213 |
device.location = device_location |
| 2214 |
|
| 2215 |
return (host, uri) |
| 2216 |
|
| 2217 |
def fillDeviceTab(self, current_uri=None): |
| 2218 |
self.device_selected = -1 |
| 2219 |
model = Gtk.TreeStore (str, # device-info |
| 2220 |
GObject.TYPE_PYOBJECT, # PhysicalDevice obj |
| 2221 |
bool) # Separator? |
| 2222 |
other = cupshelpers.Device('', **{'device-info' :_("Enter URI")}) |
| 2223 |
physother = PhysicalDevice (other) |
| 2224 |
self.devices = [physother] |
| 2225 |
uri_iter = model.append (None, row=[physother.get_info (), |
| 2226 |
physother, False]) |
| 2227 |
network_iter = model.append (None, row=[_("Network Printer"), |
| 2228 |
None, |
| 2229 |
False]) |
| 2230 |
network_dict = { 'device-class': 'network', |
| 2231 |
'device-info': _("Find Network Printer") } |
| 2232 |
network = cupshelpers.Device ('network', **network_dict) |
| 2233 |
find_nw_iter = model.append (network_iter, |
| 2234 |
row=[network_dict['device-info'], |
| 2235 |
PhysicalDevice (network), False]) |
| 2236 |
model.insert_after (network_iter, find_nw_iter, row=['', None, True]) |
| 2237 |
smbdev_dict = { 'device-class': 'network', |
| 2238 |
'device-info': _("Windows Printer via SAMBA") } |
| 2239 |
smbdev = cupshelpers.Device ('smb', **smbdev_dict) |
| 2240 |
find_smb_iter = model.append (network_iter, |
| 2241 |
row=[smbdev_dict['device-info'], |
| 2242 |
PhysicalDevice (smbdev), False]) |
| 2243 |
model.insert_after (find_nw_iter, find_smb_iter, row=['', None, True]) |
| 2244 |
self.devices_uri_iter = uri_iter |
| 2245 |
self.devices_find_nw_iter = find_nw_iter |
| 2246 |
self.devices_network_iter = network_iter |
| 2247 |
self.devices_network_fetched = False |
| 2248 |
self.tvNPDevices.set_model (model) |
| 2249 |
self.entNPTDevice.set_text ('') |
| 2250 |
self.expNPDeviceURIs.hide () |
| 2251 |
column = self.tvNPDevices.get_column (0) |
| 2252 |
self.tvNPDevices.set_cursor (Gtk.TreePath(), column, False) |
| 2253 |
|
| 2254 |
self.current_uri = current_uri |
| 2255 |
self.firewall = None |
| 2256 |
debugprint ("Fetching devices") |
| 2257 |
self.start_fetching_devices () |
| 2258 |
|
| 2259 |
def on_firewall_read (self, data): |
| 2260 |
f = self.firewall |
| 2261 |
allowed = True |
| 2262 |
try: |
| 2263 |
ipp_allowed = f.check_ipp_client_allowed () |
| 2264 |
mdns_allowed = f.check_mdns_allowed () |
| 2265 |
allowed = (ipp_allowed and mdns_allowed) |
| 2266 |
|
| 2267 |
secondary_text = TEXT_adjust_firewall + "\n\n" |
| 2268 |
if not ipp_allowed: |
| 2269 |
secondary_text += ("- " + |
| 2270 |
_("Allow all incoming IPP Browse packets") + |
| 2271 |
"\n") |
| 2272 |
f.add_service (firewallsettings.IPP_CLIENT_SERVICE) |
| 2273 |
if not mdns_allowed: |
| 2274 |
secondary_text += ("- " + |
| 2275 |
_("Allow all incoming mDNS traffic") + "\n") |
| 2276 |
f.add_service (firewallsettings.MDNS_SERVICE) |
| 2277 |
|
| 2278 |
if not allowed: |
| 2279 |
debugprint ("Asking for permission to adjust firewall:\n%s" % |
| 2280 |
secondary_text) |
| 2281 |
dialog = Gtk.MessageDialog (parent=self.NewPrinterWindow, |
| 2282 |
modal=True, destroy_with_parent=True, |
| 2283 |
message_type=Gtk.MessageType.QUESTION, |
| 2284 |
buttons=Gtk.ButtonsType.NONE, |
| 2285 |
text= _("Adjust Firewall")) |
| 2286 |
dialog.format_secondary_markup (secondary_text) |
| 2287 |
dialog.add_buttons (_("Do It Later"), Gtk.ResponseType.NO, |
| 2288 |
_("Adjust Firewall"), Gtk.ResponseType.YES) |
| 2289 |
dialog.connect ('response', self.adjust_firewall_response) |
| 2290 |
dialog.show () |
| 2291 |
except (dbus.DBusException, Exception): |
| 2292 |
nonfatalException () |
| 2293 |
|
| 2294 |
if allowed: |
| 2295 |
debugprint ("Firewall all OK, no changes needed") |
| 2296 |
|
| 2297 |
def adjust_firewall_response (self, dialog, response): |
| 2298 |
dialog.destroy () |
| 2299 |
if response == Gtk.ResponseType.YES: |
| 2300 |
ipp_server_allowed = self.firewall.check_ipp_server_allowed () |
| 2301 |
if not ipp_server_allowed: |
| 2302 |
self.firewall.add_service (firewallsettings.IPP_SERVER_SERVICE) |
| 2303 |
self.firewall.write () |
| 2304 |
|
| 2305 |
debugprint ("Fetching network devices after firewall dialog response") |
| 2306 |
self.fetchDevices_conn = asyncconn.Connection () |
| 2307 |
self.fetchDevices_conn._begin_operation (_("fetching device list")) |
| 2308 |
self.fetchDevices (network=True) |
| 2309 |
|
| 2310 |
def start_fetching_devices (self): |
| 2311 |
self.fetchDevices_conn = asyncconn.Connection () |
| 2312 |
self.fetchDevices_conn._begin_operation (_("fetching device list")) |
| 2313 |
self.fetchDevices (network=False, current_uri=self.current_uri) |
| 2314 |
del self.current_uri |
| 2315 |
|
| 2316 |
def add_devices (self, devices, current_uri, no_more=False): |
| 2317 |
if current_uri: |
| 2318 |
if current_uri in devices: |
| 2319 |
current = devices.pop(current_uri) |
| 2320 |
elif current_uri.replace (":9100", "") in devices: |
| 2321 |
current_uri = current_uri.replace (":9100", "") |
| 2322 |
current = devices.pop(current_uri) |
| 2323 |
elif no_more: |
| 2324 |
current = cupshelpers.Device (current_uri) |
| 2325 |
current.info = "Current device" |
| 2326 |
else: |
| 2327 |
current_uri = None |
| 2328 |
|
| 2329 |
devices = list(devices.values()) |
| 2330 |
|
| 2331 |
for device in devices: |
| 2332 |
if device.type == "socket": |
| 2333 |
# Remove default port to more easily find duplicate URIs |
| 2334 |
device.uri = device.uri.replace (":9100", "") |
| 2335 |
|
| 2336 |
# Map generic URIs to something canonical |
| 2337 |
def replace_generic (device): |
| 2338 |
if device.uri == "hp:/no_device_found": |
| 2339 |
device.uri = "hp" |
| 2340 |
elif device.uri == "hpfax:/no_device_found": |
| 2341 |
device.uri = "hpfax" |
| 2342 |
return device |
| 2343 |
|
| 2344 |
devices = list(map (replace_generic, devices)) |
| 2345 |
|
| 2346 |
# Mark duplicate URIs for deletion |
| 2347 |
for i in range (len (devices) - 1): |
| 2348 |
for j in range (i + 1, len (devices)): |
| 2349 |
device1 = devices[i] |
| 2350 |
device2 = devices[j] |
| 2351 |
if device1.uri == "delete" or device2.uri == "delete": |
| 2352 |
continue |
| 2353 |
if device1.uri == device2.uri: |
| 2354 |
# Keep the one with the longer (better) device ID |
| 2355 |
if (not device1.id): |
| 2356 |
device1.uri = "delete" |
| 2357 |
elif (not device2.id): |
| 2358 |
device2.uri = "delete" |
| 2359 |
elif (len (device1.id) < len (device2.id)): |
| 2360 |
device1.uri = "delete" |
| 2361 |
else: |
| 2362 |
device2.uri = "delete" |
| 2363 |
devices = [x for x in devices if x.uri not in ("hp", "hpfax", |
| 2364 |
"hal", "beh", "smb", |
| 2365 |
"scsi", "http", "bjnp", |
| 2366 |
"delete")] |
| 2367 |
newdevices = [] |
| 2368 |
for device in devices: |
| 2369 |
debugprint("Adding device with URI %s" % device.uri) |
| 2370 |
if (hasattr (device, 'address')): |
| 2371 |
debugprint(" Device address %s" % device.address) |
| 2372 |
if (hasattr (device, 'hostname')): |
| 2373 |
debugprint(" Device host name %s" % device.hostname) |
| 2374 |
physicaldevice = PhysicalDevice (device) |
| 2375 |
debugprint (" Created physical device %s" % repr(physicaldevice)) |
| 2376 |
try: |
| 2377 |
i = self.devices.index (physicaldevice) |
| 2378 |
debugprint (" Physical device %d is the same printer" % i) |
| 2379 |
self.devices[i].add_device (device) |
| 2380 |
debugprint (" New physical device %s is same as physical device %d: %s" % |
| 2381 |
(repr(physicaldevice), i, repr(self.devices[i]))) |
| 2382 |
debugprint (" Joining devices") |
| 2383 |
except ValueError: |
| 2384 |
self.devices.append (physicaldevice) |
| 2385 |
newdevices.append (physicaldevice) |
| 2386 |
debugprint (" Physical device %s is a completely new device" % repr(physicaldevice)) |
| 2387 |
|
| 2388 |
self.devices.sort() |
| 2389 |
if current_uri: |
| 2390 |
current_device = PhysicalDevice (current) |
| 2391 |
try: |
| 2392 |
i = self.devices.index (current_device) |
| 2393 |
self.devices[i].add_device (current) |
| 2394 |
current_device = self.devices[i] |
| 2395 |
except ValueError: |
| 2396 |
self.devices.append (current_device) |
| 2397 |
newdevices.append (current_device) |
| 2398 |
else: |
| 2399 |
current_device = None |
| 2400 |
|
| 2401 |
model = self.tvNPDevices.get_model () |
| 2402 |
|
| 2403 |
network_iter = self.devices_network_iter |
| 2404 |
find_nw_iter = self.devices_find_nw_iter |
| 2405 |
for newdevice in newdevices: |
| 2406 |
device = None |
| 2407 |
try: |
| 2408 |
i = self.devices.index (newdevice) |
| 2409 |
device = self.devices[i] |
| 2410 |
except ValueError: |
| 2411 |
debugprint("ERROR: Cannot identify new physical device with its entry in the device list (%s)" % |
| 2412 |
repr(newdevice)) |
| 2413 |
continue |
| 2414 |
devs = device.get_devices () |
| 2415 |
network = devs[0].device_class == 'network' |
| 2416 |
info = device.get_info () |
| 2417 |
if device == current_device: |
| 2418 |
info += _(" (Current)") |
| 2419 |
row=[info, device, False] |
| 2420 |
if network: |
| 2421 |
if devs[0].uri != devs[0].type: |
| 2422 |
# An actual network printer device. Put this at the top. |
| 2423 |
iter = model.insert_before (network_iter, find_nw_iter, |
| 2424 |
row=row) |
| 2425 |
|
| 2426 |
# If this is the currently selected device we need |
| 2427 |
# to expand the "Network Printer" row so that it |
| 2428 |
# is visible. |
| 2429 |
if device == current_device: |
| 2430 |
network_path = model.get_path (network_iter) |
| 2431 |
self.tvNPDevices.expand_row (network_path, False) |
| 2432 |
else: |
| 2433 |
# Just a method of finding one. |
| 2434 |
iter = model.append (network_iter, row=row) |
| 2435 |
else: |
| 2436 |
# Insert this local device in order. |
| 2437 |
network_path = model.get_path (network_iter) |
| 2438 |
iter = model.get_iter_first () |
| 2439 |
while model.get_path (iter) != network_path: |
| 2440 |
physdev = model.get_value (iter, 1) |
| 2441 |
if physdev > device: |
| 2442 |
break |
| 2443 |
|
| 2444 |
iter = model.iter_next (iter) |
| 2445 |
|
| 2446 |
iter = model.insert_before (None, iter, row=row) |
| 2447 |
|
| 2448 |
if device == current_device: |
| 2449 |
device_select_path = model.get_path (iter) |
| 2450 |
self.tvNPDevices.scroll_to_cell (device_select_path, |
| 2451 |
row_align=0.5) |
| 2452 |
column = self.tvNPDevices.get_column (0) |
| 2453 |
self.tvNPDevices.set_cursor (device_select_path, column, False) |
| 2454 |
|
| 2455 |
connection_select_path = 0 |
| 2456 |
if current_uri: |
| 2457 |
model = self.tvNPDeviceURIs.get_model () |
| 2458 |
iter = model.get_iter_first () |
| 2459 |
i = 0 |
| 2460 |
while iter: |
| 2461 |
dev = model.get_value (iter, 1) |
| 2462 |
if current_uri == dev.uri: |
| 2463 |
connection_select_path = i |
| 2464 |
break |
| 2465 |
|
| 2466 |
iter = model.iter_next (iter) |
| 2467 |
i += 1 |
| 2468 |
elif not self.device_selected: |
| 2469 |
# Select the device. |
| 2470 |
column = self.tvNPDevices.get_column (0) |
| 2471 |
self.tvNPDevices.set_cursor (Gtk.TreePath(), column, False) |
| 2472 |
|
| 2473 |
# Select the connection. |
| 2474 |
column = self.tvNPDeviceURIs.get_column (0) |
| 2475 |
self.tvNPDeviceURIs.set_cursor (connection_select_path, column, False) |
| 2476 |
|
| 2477 |
## SMB browsing |
| 2478 |
|
| 2479 |
def install_python3_smbc_if_needed (self): |
| 2480 |
global PYSMB_AVAILABLE |
| 2481 |
global pysmb # Make the import of pysmb globally available |
| 2482 |
# Does the SMB client library need to be installed? |
| 2483 |
if PYSMB_AVAILABLE: |
| 2484 |
return True |
| 2485 |
|
| 2486 |
debugprint ("No SMB client library present so attempting install") |
| 2487 |
|
| 2488 |
try: |
| 2489 |
pk = installpackage.PackageKit () |
| 2490 |
# The following call means a blocking, synchronous, D-Bus call |
| 2491 |
pk.InstallPackageName (0, 0, "python3-smbc") |
| 2492 |
except DBusException as e: |
| 2493 |
debugprint ("Error during installation/setup of SMB client.") |
| 2494 |
debugprint("{}".format(e)) |
| 2495 |
return False |
| 2496 |
|
| 2497 |
try: |
| 2498 |
import pysmb |
| 2499 |
PYSMB_AVAILABLE=True |
| 2500 |
except ModuleNotFoundError as e: |
| 2501 |
debugprint ("SMB client setup failed.") |
| 2502 |
debugprint("{}".format(e)) |
| 2503 |
return False |
| 2504 |
|
| 2505 |
debugprint ("SMB client successfully installed and set up.") |
| 2506 |
|
| 2507 |
return True |
| 2508 |
|
| 2509 |
def browse_smb_hosts(self): |
| 2510 |
"""Initialise the SMB tree store.""" |
| 2511 |
store = self.smb_store |
| 2512 |
store.clear () |
| 2513 |
busy(self.SMBBrowseDialog) |
| 2514 |
class X: |
| 2515 |
pass |
| 2516 |
dummy = X() |
| 2517 |
dummy.smbc_type = pysmb.smbc.PRINTER_SHARE |
| 2518 |
dummy.name = _('Scanning...') |
| 2519 |
dummy.comment = '' |
| 2520 |
store.append(None, [dummy]) |
| 2521 |
while Gtk.events_pending (): |
| 2522 |
Gtk.main_iteration () |
| 2523 |
|
| 2524 |
debug = 0 |
| 2525 |
if get_debugging (): |
| 2526 |
debug = 10 |
| 2527 |
smbc_auth = pysmb.AuthContext (self.SMBBrowseDialog) |
| 2528 |
ctx = pysmb.smbc.Context (debug=debug, |
| 2529 |
auth_fn=smbc_auth.callback) |
| 2530 |
entries = None |
| 2531 |
try: |
| 2532 |
while smbc_auth.perform_authentication () > 0: |
| 2533 |
try: |
| 2534 |
entries = ctx.opendir ("smb://").getdents () |
| 2535 |
except Exception as e: |
| 2536 |
smbc_auth.failed (e) |
| 2537 |
except RuntimeError as e: |
| 2538 |
(e, s) = e.args |
| 2539 |
if e != errno.ENOENT: |
| 2540 |
debugprint ("Runtime error: %s" % repr ((e, s))) |
| 2541 |
except: |
| 2542 |
nonfatalException () |
| 2543 |
|
| 2544 |
store.clear () |
| 2545 |
if entries: |
| 2546 |
for entry in entries: |
| 2547 |
if entry.smbc_type in [pysmb.smbc.WORKGROUP, |
| 2548 |
pysmb.smbc.SERVER]: |
| 2549 |
iter = store.append (None, [entry]) |
| 2550 |
i = store.append (iter) |
| 2551 |
|
| 2552 |
specified_uri = SMBURI (uri=self.entSMBURI.get_text ()) |
| 2553 |
(group, host, share, user, password) = specified_uri.separate () |
| 2554 |
if len (host) > 0: |
| 2555 |
# The user has specified a server before clicking Browse. |
| 2556 |
# Append the server as a top-level entry. |
| 2557 |
class FakeEntry: |
| 2558 |
pass |
| 2559 |
toplevel = FakeEntry () |
| 2560 |
toplevel.smbc_type = pysmb.smbc.SERVER |
| 2561 |
toplevel.name = host |
| 2562 |
toplevel.comment = '' |
| 2563 |
iter = store.append (None, [toplevel]) |
| 2564 |
i = store.append (iter) |
| 2565 |
|
| 2566 |
# Now expand it. |
| 2567 |
path = store.get_path (iter) |
| 2568 |
self.tvSMBBrowser.expand_row (path, 0) |
| 2569 |
|
| 2570 |
ready(self.SMBBrowseDialog) |
| 2571 |
|
| 2572 |
if store.get_iter_first () is None: |
| 2573 |
self.SMBBrowseDialog.hide () |
| 2574 |
show_info_dialog (_("No Print Shares"), |
| 2575 |
_("There were no print shares found. " |
| 2576 |
"Please check that the Samba service is " |
| 2577 |
"marked as trusted in your firewall " |
| 2578 |
"configuration."), |
| 2579 |
parent=self.NewPrinterWindow) |
| 2580 |
|
| 2581 |
def smb_select_function (self, selection, model, path, path_selected, data): |
| 2582 |
"""Don't allow this path to be selected unless it is a leaf.""" |
| 2583 |
iter = self.smb_store.get_iter (path) |
| 2584 |
return not self.smb_store.iter_has_child (iter) |
| 2585 |
|
| 2586 |
def smbbrowser_cell_share (self, column, cell, model, iter, data): |
| 2587 |
entry = model.get_value (iter, 0) |
| 2588 |
share = '' |
| 2589 |
if entry is not None: |
| 2590 |
share = entry.name |
| 2591 |
cell.set_property ('text', share) |
| 2592 |
|
| 2593 |
def smbbrowser_cell_comment (self, column, cell, model, iter, data): |
| 2594 |
entry = model.get_value (iter, 0) |
| 2595 |
comment = '' |
| 2596 |
if entry is not None: |
| 2597 |
comment = entry.comment |
| 2598 |
cell.set_property ('text', comment) |
| 2599 |
|
| 2600 |
def on_tvSMBBrowser_row_activated (self, view, path, column): |
| 2601 |
"""Handle double-clicks in the SMB tree view.""" |
| 2602 |
store = self.smb_store |
| 2603 |
iter = store.get_iter (path) |
| 2604 |
entry = store.get_value (iter, 0) |
| 2605 |
if entry and entry.smbc_type == pysmb.smbc.PRINTER_SHARE: |
| 2606 |
# This is a share, not a host. |
| 2607 |
self.btnSMBBrowseOk.clicked () |
| 2608 |
return |
| 2609 |
|
| 2610 |
if view.row_expanded (path): |
| 2611 |
view.collapse_row (path) |
| 2612 |
else: |
| 2613 |
self.on_tvSMBBrowser_row_expanded (view, iter, path) |
| 2614 |
|
| 2615 |
def on_tvSMBBrowser_row_expanded (self, view, iter, path): |
| 2616 |
"""Handler for expanding a row in the SMB tree view.""" |
| 2617 |
model = view.get_model () |
| 2618 |
entry = model.get_value (iter, 0) |
| 2619 |
if entry is None: |
| 2620 |
return |
| 2621 |
|
| 2622 |
if entry.smbc_type == pysmb.smbc.WORKGROUP: |
| 2623 |
# Workgroup |
| 2624 |
# Be careful though: if there is a server with the |
| 2625 |
# same name as the workgroup we will get a list of its |
| 2626 |
# shares, not the workgroup's servers. |
| 2627 |
try: |
| 2628 |
if self.expanding_row: |
| 2629 |
return |
| 2630 |
except: |
| 2631 |
self.expanding_row = 1 |
| 2632 |
|
| 2633 |
busy (self.SMBBrowseDialog) |
| 2634 |
uri = "smb://%s/" % entry.name |
| 2635 |
debug = 0 |
| 2636 |
if get_debugging (): |
| 2637 |
debug = 10 |
| 2638 |
smbc_auth = pysmb.AuthContext (self.SMBBrowseDialog) |
| 2639 |
ctx = pysmb.smbc.Context (debug=debug, |
| 2640 |
auth_fn=smbc_auth.callback) |
| 2641 |
entries = [] |
| 2642 |
try: |
| 2643 |
while smbc_auth.perform_authentication () > 0: |
| 2644 |
try: |
| 2645 |
entries = ctx.opendir (uri).getdents () |
| 2646 |
except Exception as e: |
| 2647 |
smbc_auth.failed (e) |
| 2648 |
except RuntimeError as e: |
| 2649 |
(e, s) = e.args |
| 2650 |
if e != errno.ENOENT: |
| 2651 |
debugprint ("Runtime error: %s" % repr ((e, s))) |
| 2652 |
except: |
| 2653 |
nonfatalException() |
| 2654 |
|
| 2655 |
while model.iter_has_child (iter): |
| 2656 |
i = model.iter_nth_child (iter, 0) |
| 2657 |
model.remove (i) |
| 2658 |
|
| 2659 |
for entry in entries: |
| 2660 |
if entry.smbc_type in [pysmb.smbc.SERVER, |
| 2661 |
pysmb.smbc.PRINTER_SHARE]: |
| 2662 |
i = model.append (iter, [entry]) |
| 2663 |
if entry.smbc_type == pysmb.smbc.SERVER: |
| 2664 |
n = model.append (i) |
| 2665 |
|
| 2666 |
view.expand_row (path, 0) |
| 2667 |
del self.expanding_row |
| 2668 |
ready (self.SMBBrowseDialog) |
| 2669 |
|
| 2670 |
elif entry.smbc_type == pysmb.smbc.SERVER: |
| 2671 |
# Server |
| 2672 |
try: |
| 2673 |
if self.expanding_row: |
| 2674 |
return |
| 2675 |
except: |
| 2676 |
self.expanding_row = 1 |
| 2677 |
|
| 2678 |
busy (self.SMBBrowseDialog) |
| 2679 |
uri = "smb://%s/" % entry.name |
| 2680 |
debug = 0 |
| 2681 |
if get_debugging (): |
| 2682 |
debug = 10 |
| 2683 |
smbc_auth = pysmb.AuthContext (self.SMBBrowseDialog) |
| 2684 |
ctx = pysmb.smbc.Context (debug=debug, |
| 2685 |
auth_fn=smbc_auth.callback) |
| 2686 |
shares = [] |
| 2687 |
try: |
| 2688 |
while smbc_auth.perform_authentication () > 0: |
| 2689 |
try: |
| 2690 |
shares = ctx.opendir (uri).getdents () |
| 2691 |
except Exception as e: |
| 2692 |
smbc_auth.failed (e) |
| 2693 |
except RuntimeError as e: |
| 2694 |
(e, s) = e.args |
| 2695 |
if e != errno.EACCES and e != errno.EPERM: |
| 2696 |
debugprint ("Runtime error: %s" % repr ((e, s))) |
| 2697 |
except: |
| 2698 |
nonfatalException() |
| 2699 |
|
| 2700 |
while model.iter_has_child (iter): |
| 2701 |
i = model.iter_nth_child (iter, 0) |
| 2702 |
model.remove (i) |
| 2703 |
|
| 2704 |
for share in shares: |
| 2705 |
if share.smbc_type == pysmb.smbc.PRINTER_SHARE: |
| 2706 |
i = model.append (iter, [share]) |
| 2707 |
debugprint (repr (share)) |
| 2708 |
|
| 2709 |
view.expand_row (path, 0) |
| 2710 |
del self.expanding_row |
| 2711 |
ready (self.SMBBrowseDialog) |
| 2712 |
|
| 2713 |
def set_btnSMBVerify_sensitivity (self, on): |
| 2714 |
self.btnSMBVerify.set_sensitive (on) |
| 2715 |
if not PYSMB_AVAILABLE or not on: |
| 2716 |
self.btnSMBVerify.set_tooltip_text (_("Verification requires the " |
| 2717 |
"%s module") % "pysmbc") |
| 2718 |
|
| 2719 |
def on_entSMBURI_changed (self, ent): |
| 2720 |
allowed_chars = string.ascii_letters+string.digits+'_-./:%[]@' |
| 2721 |
self.entry_changed(ent, allowed_chars) |
| 2722 |
uri = ent.get_text () |
| 2723 |
(group, host, share, user, password) = SMBURI (uri=uri).separate () |
| 2724 |
if user: |
| 2725 |
self.entSMBUsername.set_text (user) |
| 2726 |
if password: |
| 2727 |
self.entSMBPassword.set_text (password) |
| 2728 |
if user or password: |
| 2729 |
uri = SMBURI (group=group, host=host, share=share).get_uri () |
| 2730 |
ent.set_text(uri) |
| 2731 |
self.rbtnSMBAuthSet.set_active(True) |
| 2732 |
elif self.entSMBUsername.get_text () == '': |
| 2733 |
self.rbtnSMBAuthPrompt.set_active(True) |
| 2734 |
|
| 2735 |
self.set_btnSMBVerify_sensitivity (bool(uri)) |
| 2736 |
self.setNPButtons () |
| 2737 |
|
| 2738 |
def on_tvSMBBrowser_cursor_changed(self, widget): |
| 2739 |
selection = self.tvSMBBrowser.get_selection() |
| 2740 |
if selection is None: |
| 2741 |
return |
| 2742 |
|
| 2743 |
store, iter = selection.get_selected() |
| 2744 |
is_share = False |
| 2745 |
if iter: |
| 2746 |
entry = store.get_value (iter, 0) |
| 2747 |
if entry: |
| 2748 |
is_share = entry.smbc_type == pysmb.smbc.PRINTER_SHARE |
| 2749 |
|
| 2750 |
self.btnSMBBrowseOk.set_sensitive(iter is not None and is_share) |
| 2751 |
|
| 2752 |
def on_btnSMBBrowse_clicked(self, button): |
| 2753 |
"""Check whether the needed SMB client library is available and""" |
| 2754 |
"""install it if needed""" |
| 2755 |
if not self.install_python3_smbc_if_needed(): |
| 2756 |
return |
| 2757 |
|
| 2758 |
self.btnSMBBrowseOk.set_sensitive(False) |
| 2759 |
|
| 2760 |
try: |
| 2761 |
# Note: we do the browsing from *this* machine, regardless |
| 2762 |
# of which CUPS server we are connected to. |
| 2763 |
f = firewallsettings.FirewallD () |
| 2764 |
if not f.running: |
| 2765 |
f = firewallsettings.SystemConfigFirewall () |
| 2766 |
allowed = f.check_samba_client_allowed () |
| 2767 |
secondary_text = TEXT_adjust_firewall + "\n\n" |
| 2768 |
if not allowed: |
| 2769 |
dialog = Gtk.MessageDialog (parent=self.NewPrinterWindow, |
| 2770 |
modal=True, destroy_with_parent=True, |
| 2771 |
message_type=Gtk.MessageType.QUESTION, |
| 2772 |
buttons=Gtk.ButtonsType.NONE, |
| 2773 |
text=_("Adjust Firewall")) |
| 2774 |
secondary_text += ("- " + |
| 2775 |
_("Allow all incoming SMB/CIFS " |
| 2776 |
"browse packets")) |
| 2777 |
dialog.format_secondary_markup (secondary_text) |
| 2778 |
dialog.add_buttons (_("Do It Later"), Gtk.ResponseType.NO, |
| 2779 |
_("Adjust Firewall"), Gtk.ResponseType.YES) |
| 2780 |
response = dialog.run () |
| 2781 |
dialog.destroy () |
| 2782 |
|
| 2783 |
if response == Gtk.ResponseType.YES: |
| 2784 |
f.add_service (firewallsettings.SAMBA_CLIENT_SERVICE) |
| 2785 |
f.write () |
| 2786 |
except (dbus.DBusException, Exception): |
| 2787 |
nonfatalException () |
| 2788 |
|
| 2789 |
self.SMBBrowseDialog.show() |
| 2790 |
self.browse_smb_hosts() |
| 2791 |
|
| 2792 |
def on_btnSMBBrowseOk_clicked(self, button): |
| 2793 |
store, iter = self.tvSMBBrowser.get_selection().get_selected() |
| 2794 |
is_share = False |
| 2795 |
if iter: |
| 2796 |
entry = store.get_value (iter, 0) |
| 2797 |
if entry: |
| 2798 |
is_share = entry.smbc_type == pysmb.smbc.PRINTER_SHARE |
| 2799 |
|
| 2800 |
if not iter or not is_share: |
| 2801 |
self.SMBBrowseDialog.hide() |
| 2802 |
return |
| 2803 |
|
| 2804 |
parent_iter = store.iter_parent (iter) |
| 2805 |
domain_iter = store.iter_parent (parent_iter) |
| 2806 |
share = store.get_value (iter, 0) |
| 2807 |
host = store.get_value (parent_iter, 0) |
| 2808 |
if domain_iter: |
| 2809 |
group = store.get_value (domain_iter, 0).name |
| 2810 |
else: |
| 2811 |
group = '' |
| 2812 |
uri = SMBURI (group=group, |
| 2813 |
host=host.name, |
| 2814 |
share=share.name).get_uri () |
| 2815 |
|
| 2816 |
self.entSMBUsername.set_text ('') |
| 2817 |
self.entSMBPassword.set_text ('') |
| 2818 |
self.entSMBURI.set_text (uri) |
| 2819 |
|
| 2820 |
self.SMBBrowseDialog.hide() |
| 2821 |
|
| 2822 |
def on_btnSMBBrowseCancel_clicked(self, widget, *args): |
| 2823 |
self.SMBBrowseDialog.hide() |
| 2824 |
|
| 2825 |
def on_btnSMBBrowseRefresh_clicked(self, button): |
| 2826 |
self.browse_smb_hosts() |
| 2827 |
|
| 2828 |
def on_rbtnSMBAuthSet_toggled(self, widget): |
| 2829 |
self.tblSMBAuth.set_sensitive(widget.get_active()) |
| 2830 |
|
| 2831 |
def on_btnSMBVerify_clicked(self, button): |
| 2832 |
"""Check whether the needed SMB client library is available and""" |
| 2833 |
"""install it if needed""" |
| 2834 |
if not self.install_python3_smbc_if_needed(): |
| 2835 |
return |
| 2836 |
|
| 2837 |
uri = self.entSMBURI.get_text () |
| 2838 |
(group, host, share, u, p) = SMBURI (uri=uri).separate () |
| 2839 |
user = '' |
| 2840 |
passwd = '' |
| 2841 |
reason = None |
| 2842 |
auth_set = self.rbtnSMBAuthSet.get_active() |
| 2843 |
if auth_set: |
| 2844 |
user = self.entSMBUsername.get_text () |
| 2845 |
passwd = self.entSMBPassword.get_text () |
| 2846 |
|
| 2847 |
accessible = False |
| 2848 |
canceled = False |
| 2849 |
busy (self.NewPrinterWindow) |
| 2850 |
try: |
| 2851 |
debug = 0 |
| 2852 |
if get_debugging (): |
| 2853 |
debug = 10 |
| 2854 |
|
| 2855 |
if auth_set: |
| 2856 |
# No prompting. |
| 2857 |
def do_auth (svr, shr, wg, un, pw): |
| 2858 |
return (group, user, passwd) |
| 2859 |
ctx = pysmb.smbc.Context (debug=debug, auth_fn=do_auth) |
| 2860 |
try: |
| 2861 |
ctx.optionUseKerberos = True |
| 2862 |
except AttributeError: |
| 2863 |
# requires pysmbc >= 1.0.12 |
| 2864 |
pass |
| 2865 |
|
| 2866 |
f = ctx.open ("smb://%s/%s" % (host, share), |
| 2867 |
os.O_RDWR, 0o777) |
| 2868 |
accessible = True |
| 2869 |
else: |
| 2870 |
# May need to prompt. |
| 2871 |
smbc_auth = pysmb.AuthContext (self.NewPrinterWindow, |
| 2872 |
workgroup=group, |
| 2873 |
user=user, |
| 2874 |
passwd=passwd) |
| 2875 |
ctx = pysmb.smbc.Context (debug=debug, |
| 2876 |
auth_fn=smbc_auth.callback) |
| 2877 |
while smbc_auth.perform_authentication () > 0: |
| 2878 |
try: |
| 2879 |
f = ctx.open ("smb://%s/%s" % (host, share), |
| 2880 |
os.O_RDWR, 0o777) |
| 2881 |
accessible = True |
| 2882 |
except Exception as e: |
| 2883 |
smbc_auth.failed (e) |
| 2884 |
|
| 2885 |
if not accessible: |
| 2886 |
canceled = True |
| 2887 |
except RuntimeError as e: |
| 2888 |
(e, s) = e.args |
| 2889 |
debugprint ("Error accessing share: %s" % repr ((e, s))) |
| 2890 |
reason = s |
| 2891 |
except: |
| 2892 |
nonfatalException() |
| 2893 |
ready (self.NewPrinterWindow) |
| 2894 |
|
| 2895 |
if accessible: |
| 2896 |
show_info_dialog (_("Print Share Verified"), |
| 2897 |
_("This print share is accessible."), |
| 2898 |
parent=self.NewPrinterWindow) |
| 2899 |
return |
| 2900 |
|
| 2901 |
if not canceled: |
| 2902 |
text = _("This print share is not accessible.") |
| 2903 |
if reason: |
| 2904 |
text = reason |
| 2905 |
show_error_dialog (_("Print Share Inaccessible"), text, |
| 2906 |
parent=self.NewPrinterWindow) |
| 2907 |
|
| 2908 |
def entry_changed(self, entry, allowed_chars): |
| 2909 |
"Remove all chars from entry's text that are not in allowed_chars." |
| 2910 |
origtext = entry.get_text() |
| 2911 |
new_text = origtext |
| 2912 |
for char in origtext: |
| 2913 |
if char not in allowed_chars: |
| 2914 |
new_text = new_text.replace(char, "") |
| 2915 |
debugprint ("removed disallowed character %s" % repr (char)) |
| 2916 |
if origtext!=new_text: |
| 2917 |
entry.set_text(new_text) |
| 2918 |
|
| 2919 |
def on_entNPTDevice_changed(self, ent): |
| 2920 |
allowed_chars = string.ascii_letters+string.digits+'_-./:%[]()@?=&+' |
| 2921 |
self.entry_changed(ent, allowed_chars) |
| 2922 |
self.setNPButtons() |
| 2923 |
|
| 2924 |
def on_entNPTJetDirectHostname_changed(self, ent): |
| 2925 |
allowed_chars = string.ascii_letters+string.digits+'_-.:%[]' |
| 2926 |
self.entry_changed(ent, allowed_chars) |
| 2927 |
self.setNPButtons() |
| 2928 |
|
| 2929 |
def on_entNPTJetDirectPort_changed(self, ent): |
| 2930 |
self.entry_changed(ent, string.digits) |
| 2931 |
self.setNPButtons() |
| 2932 |
|
| 2933 |
def on_expNPDeviceURIs_expanded (self, widget, UNUSED): |
| 2934 |
# When the expanded is not expanded we want its packing to be |
| 2935 |
# 'expand = false' so that it aligns at the bottom (it packs |
| 2936 |
# to the end of its vbox). But when it is expanded we'd like |
| 2937 |
# it to expand with the window. |
| 2938 |
# |
| 2939 |
# Adjust its 'expand' packing state depending on whether the |
| 2940 |
# widget is expanded. |
| 2941 |
|
| 2942 |
parent = widget.get_parent () |
| 2943 |
(expand, fill, |
| 2944 |
padding, pack_type) = parent.query_child_packing (widget) |
| 2945 |
expand = widget.get_expanded () |
| 2946 |
parent.set_child_packing (widget, expand, fill, |
| 2947 |
padding, pack_type) |
| 2948 |
|
| 2949 |
def device_row_separator_fn (self, model, iter, data): |
| 2950 |
return model.get_value (iter, 2) |
| 2951 |
|
| 2952 |
def device_row_activated (self, view, path, column): |
| 2953 |
if view.row_expanded (path): |
| 2954 |
view.collapse_row (path) |
| 2955 |
else: |
| 2956 |
view.expand_row (path, False) |
| 2957 |
|
| 2958 |
def check_firewall (self): |
| 2959 |
view = self.tvNPDevices |
| 2960 |
model = view.get_model () |
| 2961 |
if not model: |
| 2962 |
return |
| 2963 |
|
| 2964 |
network_path = model.get_path (self.devices_network_iter) |
| 2965 |
if not view.row_expanded (network_path): |
| 2966 |
# 'Network' not expanded |
| 2967 |
return |
| 2968 |
|
| 2969 |
if self.firewall is not None: |
| 2970 |
# Already checked |
| 2971 |
return |
| 2972 |
|
| 2973 |
if self.spinner_count > 0: |
| 2974 |
# Still discovering devices? |
| 2975 |
debugprint ("Skipping firewall adjustment: " |
| 2976 |
"discovery in progress") |
| 2977 |
return |
| 2978 |
|
| 2979 |
# Any network printers found? |
| 2980 |
for physdev in self.devices: |
| 2981 |
for device in physdev.get_devices (): |
| 2982 |
if (device.device_class == 'network' and |
| 2983 |
device.uri != device.type): |
| 2984 |
debugprint ("Skipping firewall adjustment: " |
| 2985 |
"network printers found") |
| 2986 |
return |
| 2987 |
|
| 2988 |
# If not, ask about the firewall |
| 2989 |
try: |
| 2990 |
if (self._host == 'localhost' or |
| 2991 |
self._host[0] == '/'): |
| 2992 |
self.firewall = firewallsettings.FirewallD () |
| 2993 |
if not self.firewall.running: |
| 2994 |
self.firewall = firewallsettings.SystemConfigFirewall () |
| 2995 |
|
| 2996 |
debugprint ("Examining firewall") |
| 2997 |
self.firewall.read (reply_handler=self.on_firewall_read) |
| 2998 |
except (dbus.DBusException, Exception): |
| 2999 |
nonfatalException () |
| 3000 |
|
| 3001 |
def device_row_expanded (self, view, iter, path): |
| 3002 |
model = view.get_model () |
| 3003 |
if not model or not iter: |
| 3004 |
return |
| 3005 |
|
| 3006 |
network_path = model.get_path (self.devices_network_iter) |
| 3007 |
if path == network_path: |
| 3008 |
self.check_firewall () |
| 3009 |
|
| 3010 |
def device_select_function (self, selection, model, path, *UNUSED): |
| 3011 |
""" |
| 3012 |
Allow this path to be selected as long as there |
| 3013 |
is a device associated with it. Otherwise, expand or collapse it. |
| 3014 |
""" |
| 3015 |
model = self.tvNPDevices.get_model () |
| 3016 |
iter = model.get_iter (path) |
| 3017 |
if model.get_value (iter, 1) is not None: |
| 3018 |
return True |
| 3019 |
|
| 3020 |
self.device_row_activated (self.tvNPDevices, path, None) |
| 3021 |
return False |
| 3022 |
|
| 3023 |
def on_tvNPDevices_cursor_changed(self, widget): |
| 3024 |
# Reset previous driver search result |
| 3025 |
self.installed_driver_files = [] |
| 3026 |
self.searchedfordriverpackages = False |
| 3027 |
self.founddownloadabledrivers = False |
| 3028 |
self.founddownloadableppd = False |
| 3029 |
self.downloadable_printers = [] |
| 3030 |
|
| 3031 |
self.device_selected += 1 |
| 3032 |
path, column = widget.get_cursor () |
| 3033 |
if path is None: |
| 3034 |
return |
| 3035 |
|
| 3036 |
model = widget.get_model () |
| 3037 |
iter = model.get_iter (path) |
| 3038 |
physicaldevice = model.get_value (iter, 1) |
| 3039 |
if physicaldevice is None: |
| 3040 |
return |
| 3041 |
show_uris = True |
| 3042 |
for device in physicaldevice.get_devices (): |
| 3043 |
if device.type == "parallel": |
| 3044 |
device.menuentry = _("Parallel Port") |
| 3045 |
elif device.type == "serial": |
| 3046 |
device.menuentry = _("Serial Port") |
| 3047 |
elif device.type == "usb": |
| 3048 |
if (hasattr(device, "uri") and |
| 3049 |
device.uri.lower().find("fax") > -1): |
| 3050 |
device.menuentry = _("Fax") + " - " + _("USB") |
| 3051 |
else: |
| 3052 |
device.menuentry = _("USB") |
| 3053 |
elif device.type == "bluetooth": |
| 3054 |
device.menuentry = _("Bluetooth") |
| 3055 |
elif device.type == "hp": |
| 3056 |
device.menuentry = _("HP Linux Imaging and Printing (HPLIP)") |
| 3057 |
elif device.type == "hpfax": |
| 3058 |
device.menuentry = _("Fax") + " - " + \ |
| 3059 |
_("HP Linux Imaging and Printing (HPLIP)") |
| 3060 |
elif device.type == "hal": |
| 3061 |
device.menuentry = _("Hardware Abstraction Layer (HAL)") |
| 3062 |
elif device.type == "socket": |
| 3063 |
device.menuentry = _("AppSocket/HP JetDirect") |
| 3064 |
elif device.type == "lpd": |
| 3065 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3066 |
(hostport, rest) = urllib.parse.splithost (rest) |
| 3067 |
(queue, rest) = urllib.parse.splitquery (rest) |
| 3068 |
if queue != '': |
| 3069 |
if queue[0] == '/': |
| 3070 |
queue = queue[1:] |
| 3071 |
|
| 3072 |
device.menuentry = (_("LPD/LPR queue '%s'") |
| 3073 |
% queue) |
| 3074 |
else: |
| 3075 |
device.menuentry = _("LPD/LPR queue") |
| 3076 |
|
| 3077 |
elif device.type == "smb": |
| 3078 |
device.menuentry = _("Windows Printer via SAMBA") |
| 3079 |
elif device.type == "ipp": |
| 3080 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3081 |
(hostport, rest) = urllib.parse.splithost (rest) |
| 3082 |
(queue, rest) = urllib.parse.splitquery (rest) |
| 3083 |
if queue != '': |
| 3084 |
if queue[0] == '/': |
| 3085 |
queue = queue[1:] |
| 3086 |
if queue.startswith("printers/"): |
| 3087 |
queue = queue[9:] |
| 3088 |
if 'driverless' in device.info: |
| 3089 |
drvless = "Driverless " |
| 3090 |
device.driverless = True |
| 3091 |
else: |
| 3092 |
drvless = "" |
| 3093 |
if queue != '': |
| 3094 |
device.menuentry = (("%s" + _("IPP") + " (%s)") % |
| 3095 |
(drvless, queue)) |
| 3096 |
else: |
| 3097 |
device.menuentry = (("%s" + _("IPP")) % drvless) |
| 3098 |
elif device.type == "http" or device.type == "https": |
| 3099 |
device.menuentry = _("HTTP") |
| 3100 |
elif device.type == "dnssd" or device.type == "mdns": |
| 3101 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3102 |
(name, rest) = urllib.parse.splithost (rest) |
| 3103 |
(cupsqueue, rest) = urllib.parse.splitquery (rest) |
| 3104 |
if cupsqueue != '' and cupsqueue[0] == '/': |
| 3105 |
cupsqueue = cupsqueue[1:] |
| 3106 |
if cupsqueue == 'cups': |
| 3107 |
device.menuentry = _("Remote CUPS printer via DNS-SD") |
| 3108 |
if device.info != '': |
| 3109 |
device.menuentry += " (%s)" % device.info |
| 3110 |
else: |
| 3111 |
protocol = None |
| 3112 |
if name.find("._ipp") != -1: |
| 3113 |
protocol = "IPP" |
| 3114 |
elif name.find("._printer") != -1: |
| 3115 |
protocol = "LPD" |
| 3116 |
elif name.find("._pdl-datastream") != -1: |
| 3117 |
protocol = "AppSocket/JetDirect" |
| 3118 |
if protocol is not None: |
| 3119 |
device.menuentry = (_("%s network printer via DNS-SD") |
| 3120 |
% protocol) |
| 3121 |
else: |
| 3122 |
device.menuentry = \ |
| 3123 |
_("Network printer via DNS-SD") |
| 3124 |
else: |
| 3125 |
show_uris = False |
| 3126 |
device.menuentry = device.uri |
| 3127 |
|
| 3128 |
model = Gtk.ListStore (str, # URI description |
| 3129 |
GObject.TYPE_PYOBJECT) # cupshelpers.Device |
| 3130 |
self.tvNPDeviceURIs.set_model (model) |
| 3131 |
|
| 3132 |
# If this is a network device, check whether HPLIP can drive it. |
| 3133 |
if getattr (physicaldevice, 'checked_hplip', None) != True: |
| 3134 |
hp_drivable = False |
| 3135 |
hp_scannable = False |
| 3136 |
is_network = False |
| 3137 |
remotecups = False |
| 3138 |
host = None |
| 3139 |
device_dict = { 'device-class': 'network' } |
| 3140 |
if physicaldevice._network_host: |
| 3141 |
host = physicaldevice._network_host |
| 3142 |
for device in physicaldevice.get_devices (): |
| 3143 |
if device.type == "hp": |
| 3144 |
# We already know that HPLIP can drive this device. |
| 3145 |
hp_drivable = True |
| 3146 |
|
| 3147 |
# But can we scan using it? |
| 3148 |
if self.get_hplip_scan_type_for_uri (device.uri): |
| 3149 |
hp_scannable = True |
| 3150 |
|
| 3151 |
break |
| 3152 |
elif device.type in ["socket", "lpd", "ipp", "dnssd", "mdns"]: |
| 3153 |
# This is a network printer. |
| 3154 |
if host is None and device.type in ["socket", "lpd", "ipp"]: |
| 3155 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3156 |
(hostport, rest) = urllib.parse.splithost (rest) |
| 3157 |
if hostport is not None: |
| 3158 |
(host, port) = urllib.parse.splitport (hostport) |
| 3159 |
if host: |
| 3160 |
is_network = True |
| 3161 |
remotecups = ((device.uri.startswith('dnssd:') or \ |
| 3162 |
device.uri.startswith('mdns:')) and \ |
| 3163 |
device.uri.endswith('/cups')) |
| 3164 |
if (not device.make_and_model or \ |
| 3165 |
device.make_and_model == "Unknown") and not \ |
| 3166 |
remotecups: |
| 3167 |
self.getNetworkPrinterMakeModel(host=host, |
| 3168 |
device=device) |
| 3169 |
device_dict['device-info'] = device.info |
| 3170 |
device_dict['device-make-and-model'] = (device. |
| 3171 |
make_and_model) |
| 3172 |
device_dict['device-id'] = device.id |
| 3173 |
device_dict['device-location'] = device.location |
| 3174 |
|
| 3175 |
if not hp_drivable and is_network and not remotecups and \ |
| 3176 |
(not device.make_and_model or \ |
| 3177 |
device.make_and_model == "Unknown" or \ |
| 3178 |
device.make_and_model.lower ().startswith ("hp") or \ |
| 3179 |
device.make_and_model.lower ().startswith ("hewlett")): |
| 3180 |
if (hasattr (physicaldevice, "dnssd_hostname") and \ |
| 3181 |
physicaldevice.dnssd_hostname): |
| 3182 |
hpliphost = physicaldevice.dnssd_hostname |
| 3183 |
else: |
| 3184 |
hpliphost = host |
| 3185 |
hplipuri = self.get_hplip_uri_for_network_printer (hpliphost, |
| 3186 |
"print") |
| 3187 |
if hplipuri: |
| 3188 |
dev = cupshelpers.Device (hplipuri, **device_dict) |
| 3189 |
dev.menuentry = "HP Linux Imaging and Printing (HPLIP)" |
| 3190 |
physicaldevice.add_device (dev) |
| 3191 |
|
| 3192 |
# Can we scan using this device? |
| 3193 |
if self.get_hplip_scan_type_for_uri (device.uri): |
| 3194 |
hp_scannable = True |
| 3195 |
|
| 3196 |
# Now check to see if we can also send faxes using |
| 3197 |
# this device. |
| 3198 |
faxuri = self.get_hplip_uri_for_network_printer (hpliphost, |
| 3199 |
"fax") |
| 3200 |
if faxuri: |
| 3201 |
faxdevid = self.get_hpfax_device_id (faxuri) |
| 3202 |
device_dict['device-id'] = faxdevid |
| 3203 |
device_dict['device-info'] = _("Fax") |
| 3204 |
faxdev = cupshelpers.Device (faxuri, **device_dict) |
| 3205 |
faxdev.menuentry = _("Fax") + " - " + \ |
| 3206 |
"HP Linux Imaging and Printing (HPLIP)" |
| 3207 |
physicaldevice.add_device (faxdev) |
| 3208 |
|
| 3209 |
if hp_scannable: |
| 3210 |
physicaldevice.hp_scannable = True |
| 3211 |
physicaldevice.checked_hplip = True |
| 3212 |
|
| 3213 |
device.hp_scannable = getattr (physicaldevice, 'hp_scannable', None) |
| 3214 |
|
| 3215 |
# Fill the list of connections for this device. |
| 3216 |
n = 0 |
| 3217 |
for device in physicaldevice.get_devices (): |
| 3218 |
model.append ((device.menuentry, device)) |
| 3219 |
n += 1 |
| 3220 |
column = self.tvNPDeviceURIs.get_column (0) |
| 3221 |
self.tvNPDeviceURIs.set_cursor (Gtk.TreePath(), column, False) |
| 3222 |
if show_uris: |
| 3223 |
self.expNPDeviceURIs.show_all () |
| 3224 |
else: |
| 3225 |
self.expNPDeviceURIs.hide () |
| 3226 |
|
| 3227 |
def on_tvNPDeviceURIs_cursor_changed(self, widget): |
| 3228 |
path, column = widget.get_cursor () |
| 3229 |
if path is None: |
| 3230 |
return |
| 3231 |
|
| 3232 |
model = widget.get_model () |
| 3233 |
iter = model.get_iter (path) |
| 3234 |
device = model.get_value(iter, 1) |
| 3235 |
self.device = device |
| 3236 |
self.lblNPDeviceDescription.set_text ('') |
| 3237 |
page = self.new_printer_device_tabs.get (device.type, self.PAGE_SELECT_DEVICE) |
| 3238 |
self.ntbkNPType.set_current_page(page) |
| 3239 |
|
| 3240 |
debugprint("Selected connection type. URI: %s" % device.uri) |
| 3241 |
location = '' |
| 3242 |
type = device.type |
| 3243 |
url = device.uri.split(":", 1)[-1] |
| 3244 |
if page == self.PAGE_DESCRIBE_PRINTER: |
| 3245 |
# This is the "no options" page, with just a label to describe |
| 3246 |
# the selected device. |
| 3247 |
if device.type == "parallel": |
| 3248 |
text = _("A printer connected to the parallel port.") |
| 3249 |
elif device.type == "usb": |
| 3250 |
if (hasattr(device, "uri") and |
| 3251 |
device.uri.lower().find("fax") > -1): |
| 3252 |
device.menuentry = _("Fax") + " - " + _("USB") |
| 3253 |
text = _("A fax machine or the fax function " |
| 3254 |
"of a multi-function device connected " |
| 3255 |
"to a USB port.") |
| 3256 |
else: |
| 3257 |
text = _("A printer connected to a USB port.") |
| 3258 |
elif device.type == "bluetooth": |
| 3259 |
text = _("A printer connected via Bluetooth.") |
| 3260 |
elif device.type == "hp": |
| 3261 |
text = _("HPLIP software driving a printer, " |
| 3262 |
"or the printer function of a multi-function device.") |
| 3263 |
elif device.type == "hpfax": |
| 3264 |
text = _("HPLIP software driving a fax machine, " |
| 3265 |
"or the fax function of a multi-function device.") |
| 3266 |
elif device.type == "hal": |
| 3267 |
text = _("Local printer detected by the " |
| 3268 |
"Hardware Abstraction Layer (HAL).") |
| 3269 |
elif device.type == "dnssd" or device.type == "mdns": |
| 3270 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3271 |
(name, rest) = urllib.parse.splithost (rest) |
| 3272 |
(cupsqueue, rest) = urllib.parse.splitquery (rest) |
| 3273 |
if cupsqueue != '' and cupsqueue[0] == '/': |
| 3274 |
cupsqueue = cupsqueue[1:] |
| 3275 |
if cupsqueue == 'cups': |
| 3276 |
text = _("Remote CUPS printer via DNS-SD") |
| 3277 |
else: |
| 3278 |
protocol = None |
| 3279 |
if name.find("._ipp") != -1: |
| 3280 |
protocol = "IPP" |
| 3281 |
elif name.find("._printer") != -1: |
| 3282 |
protocol = "LPD" |
| 3283 |
elif name.find("._pdl-datastream") != -1: |
| 3284 |
protocol = "AppSocket/JetDirect" |
| 3285 |
if protocol is not None: |
| 3286 |
text = _("%s network printer via DNS-SD") % protocol |
| 3287 |
else: |
| 3288 |
text = _("Network printer via DNS-SD") |
| 3289 |
else: |
| 3290 |
text = device.uri |
| 3291 |
|
| 3292 |
self.lblNPDeviceDescription.set_text (text) |
| 3293 |
elif device.type=="socket": |
| 3294 |
(scheme, rest) = urllib.parse.splittype (device.uri) |
| 3295 |
host = '' |
| 3296 |
port = 9100 |
| 3297 |
if scheme == "socket": |
| 3298 |
(hostport, rest) = urllib.parse.splithost (rest) |
| 3299 |
(host, port) = urllib.parse.splitnport (hostport, defport=port) |
| 3300 |
debugprint ("socket: host is %s, port is %s" % (host, |
| 3301 |
repr (port))) |
| 3302 |
if device.location != '': |
| 3303 |
location = device.location |
| 3304 |
else: |
| 3305 |
location = host |
| 3306 |
self.entNPTJetDirectHostname.set_text (host) |
| 3307 |
self.entNPTJetDirectPort.set_text (str (port)) |
| 3308 |
elif device.type=="serial": |
| 3309 |
if not device.is_class: |
| 3310 |
parts = device.uri.split("?", 1) |
| 3311 |
if len (parts) > 1: |
| 3312 |
options = parts[1] |
| 3313 |
else: |
| 3314 |
options = "" |
| 3315 |
|
| 3316 |
options = options.split("+") |
| 3317 |
option_dict = {} |
| 3318 |
for option in options: |
| 3319 |
name, value = option.split("=") |
| 3320 |
option_dict[name] = value |
| 3321 |
|
| 3322 |
for widget, name in ( |
| 3323 |
(self.cmbNPTSerialBaud, "baud"), |
| 3324 |
(self.cmbNPTSerialBits, "bits"), |
| 3325 |
(self.cmbNPTSerialParity, "parity"), |
| 3326 |
(self.cmbNPTSerialFlow, "flow")): |
| 3327 |
if name in option_dict: # option given in URI? |
| 3328 |
model = widget.get_model() |
| 3329 |
iter = model.get_iter_first() |
| 3330 |
nr = 0 |
| 3331 |
while iter: |
| 3332 |
value = model.get(iter,1)[0] |
| 3333 |
if str (value) == str (option_dict[name]): |
| 3334 |
break |
| 3335 |
iter = model.iter_next(iter) |
| 3336 |
nr += 1 |
| 3337 |
|
| 3338 |
if iter: |
| 3339 |
widget.set_active(nr) |
| 3340 |
else: |
| 3341 |
widget.set_active (0) |
| 3342 |
else: |
| 3343 |
widget.set_active(0) |
| 3344 |
|
| 3345 |
# XXX FILL TABS FOR VALID DEVICE URIs |
| 3346 |
elif device.type=="lpd": |
| 3347 |
self.entNPTLpdHost.set_text ('') |
| 3348 |
self.entNPTLpdQueue.set_text ('') |
| 3349 |
self.entNPTLpdQueue.set_completion (None) |
| 3350 |
self.btnNPTLpdProbe.set_sensitive (False) |
| 3351 |
if len (device.uri) > 6: |
| 3352 |
host = device.uri[6:] |
| 3353 |
i = host.find ("/") |
| 3354 |
if i != -1: |
| 3355 |
printer = host[i + 1:] |
| 3356 |
host = host[:i] |
| 3357 |
else: |
| 3358 |
printer = "" |
| 3359 |
self.entNPTLpdHost.set_text (host) |
| 3360 |
self.entNPTLpdQueue.set_text (printer) |
| 3361 |
location = host |
| 3362 |
self.btnNPTLpdProbe.set_sensitive (True) |
| 3363 |
elif device.uri == "smb": |
| 3364 |
self.entSMBURI.set_text('') |
| 3365 |
self.btnSMBVerify.set_sensitive(False) |
| 3366 |
elif device.type == "smb": |
| 3367 |
self.entSMBUsername.set_text ('') |
| 3368 |
self.entSMBPassword.set_text ('') |
| 3369 |
self.entSMBURI.set_text(device.uri[6:]) |
| 3370 |
self.set_btnSMBVerify_sensitivity (True) |
| 3371 |
else: |
| 3372 |
if device.uri: |
| 3373 |
self.entNPTDevice.set_text(device.uri) |
| 3374 |
|
| 3375 |
try: |
| 3376 |
if len (location) == 0 and self.device.device_class == "direct": |
| 3377 |
# Set location to the name of this host. |
| 3378 |
if (self._host == 'localhost' or |
| 3379 |
self._host[0] == '/'): |
| 3380 |
u = os.uname () |
| 3381 |
location = u[1] |
| 3382 |
else: |
| 3383 |
location = self._host |
| 3384 |
|
| 3385 |
# Pre-fill location field. |
| 3386 |
self.entNPLocation.set_text (location) |
| 3387 |
except: |
| 3388 |
nonfatalException () |
| 3389 |
|
| 3390 |
self.setNPButtons() |
| 3391 |
|
| 3392 |
def on_entNPTLpdHost_changed(self, ent): |
| 3393 |
hostname = ent.get_text() |
| 3394 |
self.btnNPTLpdProbe.set_sensitive (len (hostname) > 0) |
| 3395 |
self.setNPButtons() |
| 3396 |
|
| 3397 |
def on_entNPTLpdQueue_changed(self, ent): |
| 3398 |
self.setNPButtons() |
| 3399 |
|
| 3400 |
def on_btnNPTLpdProbe_clicked(self, button): |
| 3401 |
# read hostname, probe, fill printer names |
| 3402 |
hostname = self.entNPTLpdHost.get_text() |
| 3403 |
server = probe_printer.LpdServer(hostname) |
| 3404 |
|
| 3405 |
self.lblWait.set_markup ('<span weight="bold" size="larger">' + |
| 3406 |
_('Searching') + '</span>\n\n' + |
| 3407 |
_('Searching for printers')) |
| 3408 |
self.WaitWindow.set_transient_for (self.NewPrinterWindow) |
| 3409 |
self.WaitWindow.show_now () |
| 3410 |
busy (self.WaitWindow) |
| 3411 |
def stop (widget, event): |
| 3412 |
server.destroy () |
| 3413 |
return True |
| 3414 |
|
| 3415 |
self.WaitWindow.disconnect (self.WaitWindow_handler) |
| 3416 |
signal = self.WaitWindow.connect ("delete-event", stop) |
| 3417 |
printers = server.probe() |
| 3418 |
self.WaitWindow.disconnect (signal) |
| 3419 |
self.WaitWindow_handler = self.WaitWindow.connect ("delete-event", |
| 3420 |
on_delete_just_hide) |
| 3421 |
self.WaitWindow.hide () |
| 3422 |
|
| 3423 |
model = Gtk.ListStore (str) |
| 3424 |
for printer in printers: |
| 3425 |
model.append ([printer]) |
| 3426 |
|
| 3427 |
completion = Gtk.EntryCompletion () |
| 3428 |
completion.set_model (model) |
| 3429 |
completion.set_text_column (0) |
| 3430 |
completion.set_minimum_key_length (0) |
| 3431 |
self.entNPTLpdQueue.set_completion (completion) |
| 3432 |
|
| 3433 |
### Find Network Printer |
| 3434 |
def on_entNPTNetworkHostname_changed(self, ent): |
| 3435 |
text = ent.get_text () |
| 3436 |
if text.find (":") != -1: |
| 3437 |
# The user is typing in a URI. In that case, switch to URI entry. |
| 3438 |
ent.set_text ('') |
| 3439 |
debugprint ("URI detected (%s) -> Enter URI" % text) |
| 3440 |
self.entNPTDevice.set_text (text) |
| 3441 |
model = self.tvNPDevices.get_model () |
| 3442 |
path = model.get_path (self.devices_uri_iter) |
| 3443 |
self.tvNPDevices.set_cursor (path=path, |
| 3444 |
start_editing=False) |
| 3445 |
self.entNPTDevice.select_region (0, 0) |
| 3446 |
self.entNPTDevice.set_position (-1) |
| 3447 |
return |
| 3448 |
|
| 3449 |
allowed_chars = string.ascii_letters+string.digits+'_-.:%[]' |
| 3450 |
self.entry_changed(ent, allowed_chars) |
| 3451 |
s = ent.get_text () |
| 3452 |
self.btnNetworkFind.set_sensitive (len (s) > 0) |
| 3453 |
self.lblNetworkFindNotFound.hide () |
| 3454 |
self.setNPButtons () |
| 3455 |
|
| 3456 |
def on_btnNetworkFind_clicked(self, button): |
| 3457 |
host = self.entNPTNetworkHostname.get_text () |
| 3458 |
|
| 3459 |
def found_callback (new_device): |
| 3460 |
if self.printer_finder is None: |
| 3461 |
return |
| 3462 |
|
| 3463 |
GLib.idle_add (self.found_network_printer_callback, new_device) |
| 3464 |
|
| 3465 |
self.btnNetworkFind.set_sensitive (False) |
| 3466 |
self.entNPTNetworkHostname.set_sensitive (False) |
| 3467 |
self.network_found = 0 |
| 3468 |
self.lblNetworkFindNotFound.hide () |
| 3469 |
self.lblNetworkFindSearching.show_all () |
| 3470 |
finder = probe_printer.PrinterFinder () |
| 3471 |
self.inc_spinner_task () |
| 3472 |
finder.find (host, found_callback) |
| 3473 |
self.printer_finder = finder |
| 3474 |
|
| 3475 |
def found_network_printer_callback (self, new_device): |
| 3476 |
Gdk.threads_enter () |
| 3477 |
if new_device: |
| 3478 |
self.network_found += 1 |
| 3479 |
dev = PhysicalDevice (new_device) |
| 3480 |
try: |
| 3481 |
i = self.devices.index (dev) |
| 3482 |
|
| 3483 |
# Adding a new URI to an existing physical device. |
| 3484 |
self.devices[i].add_device (new_device) |
| 3485 |
|
| 3486 |
(path, column) = self.tvNPDevices.get_cursor () |
| 3487 |
if path: |
| 3488 |
model = self.tvNPDevices.get_model () |
| 3489 |
iter = model.get_iter (path) |
| 3490 |
if model.get_value (iter, 1) == self.devices[i]: |
| 3491 |
self.on_tvNPDevices_cursor_changed (self.tvNPDevices) |
| 3492 |
except ValueError: |
| 3493 |
# New physical device. |
| 3494 |
dev.checked_hplip = True |
| 3495 |
self.devices.append (dev) |
| 3496 |
self.devices.sort () |
| 3497 |
model = self.tvNPDevices.get_model () |
| 3498 |
iter = model.insert_before (None, self.devices_find_nw_iter, |
| 3499 |
row=[dev.get_info (), dev, False]) |
| 3500 |
|
| 3501 |
# If this is the first one we've found, select it. |
| 3502 |
if self.network_found == 1: |
| 3503 |
path = model.get_path (iter) |
| 3504 |
self.tvNPDevices.set_cursor (path, None, False) |
| 3505 |
else: |
| 3506 |
self.printer_finder = None |
| 3507 |
self.dec_spinner_task () |
| 3508 |
self.lblNetworkFindSearching.hide () |
| 3509 |
self.entNPTNetworkHostname.set_sensitive (True) |
| 3510 |
self.btnNetworkFind.set_sensitive (True) |
| 3511 |
if self.network_found == 0: |
| 3512 |
self.lblNetworkFindNotFound.set_markup ('<i>' + |
| 3513 |
_("No printer was " |
| 3514 |
"found at that " |
| 3515 |
"address.") + '</i>') |
| 3516 |
self.lblNetworkFindNotFound.show () |
| 3517 |
|
| 3518 |
Gdk.threads_leave () |
| 3519 |
### |
| 3520 |
|
| 3521 |
def getDeviceURI(self): |
| 3522 |
if self.dialog_mode in ['printer_with_uri', 'ppd']: |
| 3523 |
return self.device.uri |
| 3524 |
|
| 3525 |
type = self.device.type |
| 3526 |
page = self.new_printer_device_tabs.get (type, self.PAGE_SELECT_DEVICE) |
| 3527 |
device = type |
| 3528 |
if page == self.PAGE_DESCRIBE_PRINTER: |
| 3529 |
# The "no options page". We already have the URI. |
| 3530 |
device = self.device.uri |
| 3531 |
elif type == "socket": # JetDirect |
| 3532 |
host = self.entNPTJetDirectHostname.get_text() |
| 3533 |
port = self.entNPTJetDirectPort.get_text() |
| 3534 |
if host: |
| 3535 |
device += "://" + host |
| 3536 |
if port: |
| 3537 |
device += ":" + port |
| 3538 |
elif type == "lpd": # LPD |
| 3539 |
host = self.entNPTLpdHost.get_text() |
| 3540 |
printer = self.entNPTLpdQueue.get_text() |
| 3541 |
if host: |
| 3542 |
device += "://" + host |
| 3543 |
if printer: |
| 3544 |
device += "/" + printer |
| 3545 |
elif type == "serial": # Serial |
| 3546 |
options = [] |
| 3547 |
for widget, name in ( |
| 3548 |
(self.cmbNPTSerialBaud, "baud"), |
| 3549 |
(self.cmbNPTSerialBits, "bits"), |
| 3550 |
(self.cmbNPTSerialParity, "parity"), |
| 3551 |
(self.cmbNPTSerialFlow, "flow")): |
| 3552 |
model = widget.get_model () |
| 3553 |
iter = widget.get_active_iter() |
| 3554 |
option = model.get_value (iter, 1) |
| 3555 |
if option != "": |
| 3556 |
options.append(name + "=" + option) |
| 3557 |
options = "+".join(options) |
| 3558 |
device = self.device.uri.split("?")[0] #"serial:/dev/ttyS%s" |
| 3559 |
if options: |
| 3560 |
device = device + "?" + options |
| 3561 |
elif type == "smb": |
| 3562 |
uri = self.entSMBURI.get_text () |
| 3563 |
(group, host, share, u, p) = SMBURI (uri=uri).separate () |
| 3564 |
user = '' |
| 3565 |
password = '' |
| 3566 |
if self.rbtnSMBAuthSet.get_active (): |
| 3567 |
user = self.entSMBUsername.get_text () |
| 3568 |
password = self.entSMBPassword.get_text () |
| 3569 |
uri = SMBURI (group=group, host=host, share=share, |
| 3570 |
user=user, password=password).get_uri () |
| 3571 |
if uri: |
| 3572 |
device += "://" + uri |
| 3573 |
else: |
| 3574 |
device = self.entNPTDevice.get_text() |
| 3575 |
return device |
| 3576 |
|
| 3577 |
# PPD |
| 3578 |
|
| 3579 |
def on_rbtnNPFoomatic_toggled(self, widget): |
| 3580 |
rbtn1 = self.rbtnNPFoomatic.get_active() |
| 3581 |
rbtn2 = self.rbtnNPPPD.get_active() |
| 3582 |
rbtn3 = self.rbtnNPDownloadableDriverSearch.get_active() |
| 3583 |
self.tvNPMakes.set_sensitive(rbtn1) |
| 3584 |
self.filechooserPPD.set_sensitive(rbtn2) |
| 3585 |
|
| 3586 |
if rbtn1: |
| 3587 |
page = self.PAGE_DESCRIBE_PRINTER |
| 3588 |
if rbtn2: |
| 3589 |
page = self.PAGE_SELECT_DEVICE |
| 3590 |
if rbtn3: |
| 3591 |
page = self.PAGE_SELECT_INSTALL_METHOD |
| 3592 |
self.ntbkPPDSource.set_current_page (page) |
| 3593 |
|
| 3594 |
if not rbtn3 and self.opreq: |
| 3595 |
# Need to cancel a search in progress. |
| 3596 |
for handler in self.opreq_handlers: |
| 3597 |
self.opreq.disconnect (handler) |
| 3598 |
|
| 3599 |
self.opreq_handlers = None |
| 3600 |
self.opreq.cancel () |
| 3601 |
self.opreq = None |
| 3602 |
|
| 3603 |
self.btnNPDownloadableDriverSearch.set_sensitive (True) |
| 3604 |
self.btnNPDownloadableDriverSearch_label.set_text (_("Search")) |
| 3605 |
# Clear printer list. |
| 3606 |
model = Gtk.ListStore (str, str) |
| 3607 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 3608 |
combobox.set_model (model) |
| 3609 |
combobox.set_sensitive (False) |
| 3610 |
|
| 3611 |
for widget in [self.entNPDownloadableDriverSearch, |
| 3612 |
self.cmbNPDownloadableDriverFoundPrinters]: |
| 3613 |
widget.set_sensitive(rbtn3) |
| 3614 |
self.btnNPDownloadableDriverSearch.\ |
| 3615 |
set_sensitive (rbtn3 and (self.opreq is None)) |
| 3616 |
|
| 3617 |
self.setNPButtons() |
| 3618 |
|
| 3619 |
def on_filechooserPPD_selection_changed(self, widget): |
| 3620 |
self.setNPButtons() |
| 3621 |
|
| 3622 |
def on_btnNPDownloadableDriverSearch_clicked(self, widget): |
| 3623 |
self.searchedfordriverpackages = True |
| 3624 |
if self.opreq is not None: |
| 3625 |
for handler in self.opreq_handlers: |
| 3626 |
self.opreq.disconnect (handler) |
| 3627 |
|
| 3628 |
self.opreq_handlers = None |
| 3629 |
self.opreq.cancel () |
| 3630 |
self.opreq = None |
| 3631 |
|
| 3632 |
widget.set_sensitive (False) |
| 3633 |
label = self.btnNPDownloadableDriverSearch_label |
| 3634 |
label.set_text (_("Searching")) |
| 3635 |
searchterm = self.entNPDownloadableDriverSearch.get_text () |
| 3636 |
debugprint ('Searching for "%s"' % repr (searchterm)) |
| 3637 |
self.opreq = OpenPrintingRequest () |
| 3638 |
self.opreq_handlers = [] |
| 3639 |
self.opreq_handlers.append ( |
| 3640 |
self.opreq.connect ('finished', |
| 3641 |
self.opreq_user_search_done)) |
| 3642 |
self.opreq_handlers.append ( |
| 3643 |
self.opreq.connect ('error', |
| 3644 |
self.opreq_user_search_error)) |
| 3645 |
self.opreq.searchPrinters (searchterm) |
| 3646 |
self.cmbNPDownloadableDriverFoundPrinters.set_sensitive (False) |
| 3647 |
|
| 3648 |
def opreq_user_search_done (self, opreq, printers, drivers): |
| 3649 |
for handler in self.opreq_handlers: |
| 3650 |
opreq.disconnect (handler) |
| 3651 |
|
| 3652 |
self.opreq_user_search = True |
| 3653 |
self.opreq_handlers = None |
| 3654 |
self.opreq = None |
| 3655 |
|
| 3656 |
self.founddownloadabledrivers = True |
| 3657 |
self.downloadable_printers = printers |
| 3658 |
self.downloadable_drivers = drivers |
| 3659 |
|
| 3660 |
button = self.btnNPDownloadableDriverSearch |
| 3661 |
label = self.btnNPDownloadableDriverSearch_label |
| 3662 |
Gdk.threads_enter () |
| 3663 |
try: |
| 3664 |
label.set_text (_("Search")) |
| 3665 |
button.set_sensitive (True) |
| 3666 |
model = Gtk.ListStore (str, str) |
| 3667 |
if len (self.downloadable_printers) != 1: |
| 3668 |
if len (self.downloadable_printers) > 1: |
| 3669 |
first = _("-- Select from search results --") |
| 3670 |
else: |
| 3671 |
first = _("-- No matches found --") |
| 3672 |
|
| 3673 |
iter = model.append (None) |
| 3674 |
model.set_value (iter, 0, first) |
| 3675 |
model.set_value (iter, 1, '') |
| 3676 |
|
| 3677 |
sorted_list = [] |
| 3678 |
for printer_id, printer_name in self.downloadable_printers: |
| 3679 |
sorted_list.append ((printer_id, printer_name)) |
| 3680 |
|
| 3681 |
sorted_list.sort (key=functools.cmp_to_key(lambda x, y: cups.modelSort (x[1], y[1]))) |
| 3682 |
sought = self.entNPDownloadableDriverSearch.get_text ().lower () |
| 3683 |
select_index = 0 |
| 3684 |
for id, name in sorted_list: |
| 3685 |
iter = model.append (None) |
| 3686 |
model.set_value (iter, 0, name) |
| 3687 |
model.set_value (iter, 1, id) |
| 3688 |
if name.lower () == sought: |
| 3689 |
select_index = model.get_path (iter)[0] |
| 3690 |
combobox = self.cmbNPDownloadableDriverFoundPrinters |
| 3691 |
combobox.set_model (model) |
| 3692 |
combobox.set_active (select_index) |
| 3693 |
combobox.set_sensitive (True) |
| 3694 |
self.setNPButtons () |
| 3695 |
except: |
| 3696 |
nonfatalException() |
| 3697 |
|
| 3698 |
Gdk.threads_leave () |
| 3699 |
|
| 3700 |
def opreq_user_search_error (self, opreq, status, err): |
| 3701 |
debugprint ("OpenPrinting request failed (%d): %s" % (status, |
| 3702 |
repr (err))) |
| 3703 |
self.opreq_user_search_done (opreq, list(), dict()) |
| 3704 |
|
| 3705 |
def on_cmbNPDownloadableDriverFoundPrinters_changed(self, widget): |
| 3706 |
self.setNPButtons () |
| 3707 |
|
| 3708 |
if self.opreq is not None: |
| 3709 |
for handler in self.opreq_handlers: |
| 3710 |
self.opreq.disconnect (handler) |
| 3711 |
|
| 3712 |
self.opreq_handlers = None |
| 3713 |
self.opreq.cancel () |
| 3714 |
self.opreq = None |
| 3715 |
self.btnNPDownloadableDriverSearch.set_sensitive (True) |
| 3716 |
self.btnNPDownloadableDriverSearch_label.set_text (_("Search")) |
| 3717 |
|
| 3718 |
def fillDownloadableDrivers(self): |
| 3719 |
print ("filldownloadableDrivers") |
| 3720 |
self.downloadable_driver_for_printer = None |
| 3721 |
if self.opreq_user_search == True: |
| 3722 |
widget = self.cmbNPDownloadableDriverFoundPrinters |
| 3723 |
model = widget.get_model () |
| 3724 |
iter = widget.get_active_iter () |
| 3725 |
if iter: |
| 3726 |
printer_id = model.get_value (iter, 1) |
| 3727 |
printer_str = model.get_value (iter, 0) |
| 3728 |
if printer_id == '': |
| 3729 |
widget.set_active (1) |
| 3730 |
iter = widget.get_active_iter () |
| 3731 |
if iter: |
| 3732 |
printer_id = model.get_value (iter, 1) |
| 3733 |
printer_str = model.get_value (iter, 0) |
| 3734 |
else: |
| 3735 |
printer_id = None |
| 3736 |
printer_str = None |
| 3737 |
else: |
| 3738 |
printer_id = None |
| 3739 |
printer_str = None |
| 3740 |
else: |
| 3741 |
printer_id, printer_str = self.downloadable_printers[0] |
| 3742 |
|
| 3743 |
if printer_id is None: |
| 3744 |
# If none selected, show all. |
| 3745 |
# This also happens for ID-matching. |
| 3746 |
printer_ids = [x[0] for x in self.downloadable_printers] |
| 3747 |
else: |
| 3748 |
printer_ids = [printer_id] |
| 3749 |
|
| 3750 |
if printer_str: |
| 3751 |
self.downloadable_driver_for_printer = printer_str |
| 3752 |
|
| 3753 |
model = Gtk.ListStore (str, # driver name |
| 3754 |
GObject.TYPE_PYOBJECT) # driver data |
| 3755 |
recommended_iter = None |
| 3756 |
first_iter = None |
| 3757 |
for printer_id in printer_ids: |
| 3758 |
drivers = self.downloadable_drivers[printer_id] |
| 3759 |
for driver in drivers.values (): |
| 3760 |
if ((not 'ppds' in driver or |
| 3761 |
len(driver['ppds']) <= 0) and |
| 3762 |
(config.DOWNLOADABLE_ONLYPPD or |
| 3763 |
(not self._getDriverInstallationInfo (driver)))): |
| 3764 |
debugprint ("Removed invalid driver entry %s" % \ |
| 3765 |
driver['name']) |
| 3766 |
continue |
| 3767 |
iter = model.append (None) |
| 3768 |
if first_iter is None: |
| 3769 |
first_iter = iter |
| 3770 |
|
| 3771 |
model.set_value (iter, 0, driver['name']) |
| 3772 |
model.set_value (iter, 1, driver) |
| 3773 |
|
| 3774 |
if driver['recommended']: |
| 3775 |
recommended_iter = iter |
| 3776 |
|
| 3777 |
if first_iter is None: |
| 3778 |
return False |
| 3779 |
|
| 3780 |
if not self.rbtnNPDownloadableDriverSearch.get_active() and \ |
| 3781 |
self.dialog_mode != "download_driver": |
| 3782 |
iter = model.append (None) |
| 3783 |
model.set_value (iter, 0, _("Local Driver")) |
| 3784 |
model.set_value (iter, 1, 0) |
| 3785 |
|
| 3786 |
if recommended_iter is None: |
| 3787 |
recommended_iter = first_iter |
| 3788 |
|
| 3789 |
treeview = self.tvNPDownloadableDrivers |
| 3790 |
treeview.set_model (model) |
| 3791 |
if recommended_iter is not None: |
| 3792 |
treeview.get_selection ().select_iter (recommended_iter) |
| 3793 |
self.on_tvNPDownloadableDrivers_cursor_changed(treeview) |
| 3794 |
return True |
| 3795 |
|
| 3796 |
def on_rbtnNPDownloadLicense_toggled(self, widget): |
| 3797 |
self.setNPButtons () |
| 3798 |
|
| 3799 |
# PPD from foomatic |
| 3800 |
|
| 3801 |
def fillMakeList(self): |
| 3802 |
self.recommended_make_selected = False |
| 3803 |
makes = self.ppds.getMakes() |
| 3804 |
model = self.tvNPMakes.get_model() |
| 3805 |
model.clear() |
| 3806 |
found = False |
| 3807 |
if self.auto_make: |
| 3808 |
auto_make_norm = cupshelpers.ppds.normalize (self.auto_make) |
| 3809 |
else: |
| 3810 |
auto_make_norm = None |
| 3811 |
|
| 3812 |
for make in makes: |
| 3813 |
recommended = (auto_make_norm and |
| 3814 |
cupshelpers.ppds.normalize (make) == auto_make_norm) |
| 3815 |
if self.device and self.device.make_and_model and recommended: |
| 3816 |
text = make + _(" (recommended)") |
| 3817 |
else: |
| 3818 |
text = make |
| 3819 |
|
| 3820 |
iter = model.append((text, make,)) |
| 3821 |
if recommended: |
| 3822 |
path = model.get_path(iter) |
| 3823 |
self.tvNPMakes.set_cursor (path, None, False) |
| 3824 |
self.tvNPMakes.scroll_to_cell(path, None, |
| 3825 |
True, 0.5, 0.5) |
| 3826 |
found = True |
| 3827 |
|
| 3828 |
if not found: |
| 3829 |
self.tvNPMakes.set_cursor (Gtk.TreePath(), None, False) |
| 3830 |
self.tvNPMakes.scroll_to_cell(0, None, True, 0.0, 0.0) |
| 3831 |
|
| 3832 |
# Also pre-fill the OpenPrinting.org search box. |
| 3833 |
search = '' |
| 3834 |
if self.device and self.device.id_dict: |
| 3835 |
devid_dict = self.device.id_dict |
| 3836 |
if devid_dict["MFG"] and devid_dict["MDL"]: |
| 3837 |
search = devid_dict["MFG"] + " " + devid_dict["MDL"] |
| 3838 |
elif devid_dict["DES"]: |
| 3839 |
search = devid_dict["DES"] |
| 3840 |
elif devid_dict["MFG"]: |
| 3841 |
search = devid_dict["MFG"] |
| 3842 |
if search == '' and self.auto_make is not None: |
| 3843 |
search += self.auto_make |
| 3844 |
if self.auto_model is not None: |
| 3845 |
search += " " + self.auto_model |
| 3846 |
if (search.startswith("Generic") or |
| 3847 |
search.startswith("Unknown")): |
| 3848 |
search = '' |
| 3849 |
self.entNPDownloadableDriverSearch.set_text (search) |
| 3850 |
|
| 3851 |
def on_tvNPMakes_cursor_changed(self, tvNPMakes): |
| 3852 |
path, column = tvNPMakes.get_cursor() |
| 3853 |
if path is not None and self.ppds is not None: |
| 3854 |
model = tvNPMakes.get_model () |
| 3855 |
iter = model.get_iter (path) |
| 3856 |
self.NPMake = model.get(iter, 1)[0] |
| 3857 |
recommended_make = (self.auto_make and |
| 3858 |
cupshelpers.ppds.normalize (self.auto_make) == |
| 3859 |
cupshelpers.ppds.normalize (self.NPMake)) |
| 3860 |
self.recommended_make_selected = recommended_make |
| 3861 |
self.fillModelList() |
| 3862 |
|
| 3863 |
def fillModelList(self): |
| 3864 |
self.recommended_model_selected = False |
| 3865 |
models = self.ppds.getModels(self.NPMake) |
| 3866 |
model = self.tvNPModels.get_model() |
| 3867 |
model.clear() |
| 3868 |
selected = False |
| 3869 |
is_auto_make = (cupshelpers.ppds.normalize (self.NPMake) == |
| 3870 |
cupshelpers.ppds.normalize (self.auto_make)) |
| 3871 |
if is_auto_make: |
| 3872 |
auto_model_norm = cupshelpers.ppds.normalize (self.auto_model) |
| 3873 |
|
| 3874 |
for pmodel in models: |
| 3875 |
recommended = (is_auto_make and |
| 3876 |
cupshelpers.ppds.normalize (pmodel) == |
| 3877 |
auto_model_norm) |
| 3878 |
if self.device and self.device.make_and_model and recommended: |
| 3879 |
text = pmodel + _(" (recommended)") |
| 3880 |
else: |
| 3881 |
text = pmodel |
| 3882 |
|
| 3883 |
iter = model.append((text, pmodel,)) |
| 3884 |
if recommended: |
| 3885 |
path = model.get_path(iter) |
| 3886 |
self.tvNPModels.set_cursor (path, None, False) |
| 3887 |
self.tvNPModels.scroll_to_cell(path, None, |
| 3888 |
True, 0.5, 0.5) |
| 3889 |
selected = True |
| 3890 |
if not selected: |
| 3891 |
self.tvNPModels.set_cursor (Gtk.TreePath(), None, False) |
| 3892 |
self.tvNPModels.scroll_to_cell(0, None, True, 0.0, 0.0) |
| 3893 |
self.tvNPModels.columns_autosize() |
| 3894 |
|
| 3895 |
def fillDriverList(self, pmake, pmodel): |
| 3896 |
self.NPModel = pmodel |
| 3897 |
model = self.tvNPDrivers.get_model() |
| 3898 |
model.clear() |
| 3899 |
|
| 3900 |
if self.device: |
| 3901 |
devid = self.device.id_dict |
| 3902 |
else: |
| 3903 |
devid = None |
| 3904 |
|
| 3905 |
if (self.device and self.device.make_and_model and |
| 3906 |
self.recommended_model_selected and |
| 3907 |
self.id_matched_ppdnames): |
| 3908 |
# Use the actual device-make-and-model string. |
| 3909 |
make_and_model = self.device.make_and_model |
| 3910 |
|
| 3911 |
# and the ID-matched list of PPDs. |
| 3912 |
self.NPDrivers = self.id_matched_ppdnames |
| 3913 |
debugprint ("ID matched PPDs: %s" % repr (self.NPDrivers)) |
| 3914 |
elif self.ppds: |
| 3915 |
# Use a generic make and model string for generating the |
| 3916 |
# driver preference list. |
| 3917 |
make_and_model = pmake + " " + pmodel |
| 3918 |
ppds = self.ppds.getInfoFromModel(pmake, pmodel) |
| 3919 |
ppdnames = list(ppds.keys ()) |
| 3920 |
|
| 3921 |
files = self.installed_driver_files |
| 3922 |
try: |
| 3923 |
self.NPDrivers = self.ppds.orderPPDNamesByPreference(ppdnames, |
| 3924 |
files, |
| 3925 |
make_and_model, |
| 3926 |
devid) |
| 3927 |
except: |
| 3928 |
nonfatalException () |
| 3929 |
self.NPDrivers = ppdnames |
| 3930 |
|
| 3931 |
# Put the current driver first. |
| 3932 |
if self.auto_driver and self.device: |
| 3933 |
drivers = [] |
| 3934 |
for driver in self.NPDrivers: |
| 3935 |
if driver == self.auto_driver: |
| 3936 |
drivers.insert (0, driver) |
| 3937 |
else: |
| 3938 |
drivers.append (driver) |
| 3939 |
|
| 3940 |
self.NPDrivers = drivers |
| 3941 |
else: |
| 3942 |
# No available PPDs for some reason(!) |
| 3943 |
debugprint ("No PPDs available?") |
| 3944 |
self.NPDrivers = [] |
| 3945 |
|
| 3946 |
driverlist = [] |
| 3947 |
NPDrivers = [] |
| 3948 |
i = 0 |
| 3949 |
for ppdname in self.NPDrivers: |
| 3950 |
ppd = self.ppds.getInfoFromPPDName (ppdname) |
| 3951 |
driver = _singleton (ppd["ppd-make-and-model"]) |
| 3952 |
driver = driver.replace(" (recommended)", "") |
| 3953 |
|
| 3954 |
try: |
| 3955 |
lpostfix = " [%s]" % _singleton (ppd["ppd-natural-language"]) |
| 3956 |
driver += lpostfix |
| 3957 |
except KeyError: |
| 3958 |
pass |
| 3959 |
|
| 3960 |
duplicate = driver in driverlist |
| 3961 |
|
| 3962 |
if (not (self.device and self.device.make_and_model) and |
| 3963 |
self.auto_driver == ppdname): |
| 3964 |
driverlist.append (driver) |
| 3965 |
NPDrivers.append (ppdname) |
| 3966 |
i += 1 |
| 3967 |
iter = model.append ((driver + |
| 3968 |
_(" (Current)"),)) |
| 3969 |
path = model.get_path (iter) |
| 3970 |
self.tvNPDrivers.get_selection().select_path(path) |
| 3971 |
self.tvNPDrivers.scroll_to_cell(path, None, True, 0.5, 0.0) |
| 3972 |
elif self.device and i == 0: |
| 3973 |
driverlist.append (driver) |
| 3974 |
NPDrivers.append (ppdname) |
| 3975 |
i += 1 |
| 3976 |
iter = model.append ((driver + |
| 3977 |
_(" (recommended)"),)) |
| 3978 |
path = model.get_path (iter) |
| 3979 |
self.tvNPDrivers.get_selection().select_path(path) |
| 3980 |
self.tvNPDrivers.scroll_to_cell(path, None, True, 0.5, 0.0) |
| 3981 |
else: |
| 3982 |
if duplicate: |
| 3983 |
continue |
| 3984 |
driverlist.append (driver) |
| 3985 |
NPDrivers.append (ppdname) |
| 3986 |
i += 1 |
| 3987 |
model.append((driver, )) |
| 3988 |
|
| 3989 |
self.NPDrivers = NPDrivers |
| 3990 |
self.tvNPDrivers.columns_autosize() |
| 3991 |
|
| 3992 |
def on_NPDrivers_query_tooltip(self, tv, x, y, keyboard_mode, tooltip): |
| 3993 |
if keyboard_mode: |
| 3994 |
path = tv.get_cursor()[0] |
| 3995 |
if path is None: |
| 3996 |
return False |
| 3997 |
else: |
| 3998 |
bin_x, bin_y = tv.convert_widget_to_bin_window_coords(x, y) |
| 3999 |
ret = tv.get_path_at_pos (bin_x, bin_y) |
| 4000 |
if ret is None: |
| 4001 |
return False |
| 4002 |
path = ret[0] |
| 4003 |
|
| 4004 |
drivername = self.NPDrivers[path[0]] |
| 4005 |
ppddict = self.ppds.getInfoFromPPDName(drivername) |
| 4006 |
markup = _singleton (ppddict['ppd-make-and-model']) |
| 4007 |
if (drivername.startswith ("foomatic:")): |
| 4008 |
markup += " " |
| 4009 |
markup += _("This PPD is generated by foomatic.") |
| 4010 |
tooltip.set_markup(markup) |
| 4011 |
return True |
| 4012 |
|
| 4013 |
def on_tvNPModels_cursor_changed(self, widget): |
| 4014 |
path, column = widget.get_cursor() |
| 4015 |
if path is not None: |
| 4016 |
model = widget.get_model () |
| 4017 |
iter = model.get_iter (path) |
| 4018 |
pmodel = model.get(iter, 1)[0] |
| 4019 |
|
| 4020 |
# Find out if this is the auto-detected make and model |
| 4021 |
recommended_model = (self.recommended_make_selected and |
| 4022 |
self.auto_model and |
| 4023 |
self.auto_model.lower () == pmodel.lower ()) |
| 4024 |
self.recommended_model_selected = recommended_model |
| 4025 |
self.fillDriverList(self.NPMake, pmodel) |
| 4026 |
self.on_tvNPDrivers_cursor_changed(self.tvNPDrivers) |
| 4027 |
|
| 4028 |
def on_tvNPDrivers_cursor_changed(self, widget): |
| 4029 |
self.setNPButtons() |
| 4030 |
|
| 4031 |
def on_tvNPDownloadableDrivers_cursor_changed(self, widget): |
| 4032 |
# Clear out the properties. |
| 4033 |
self.lblNPDownloadableDriverSupplier.set_text ('') |
| 4034 |
self.lblNPDownloadableDriverLicense.set_text ('') |
| 4035 |
self.lblNPDownloadableDriverDescription.set_text ('') |
| 4036 |
self.lblNPDownloadableDriverSupportContacts.set_text ('') |
| 4037 |
self.rbtnNPDownloadLicenseNo.set_active (True) |
| 4038 |
self.frmNPDownloadableDriverLicenseTerms.hide () |
| 4039 |
|
| 4040 |
selection = widget.get_selection () |
| 4041 |
if selection is None: |
| 4042 |
return |
| 4043 |
|
| 4044 |
model, iter = selection.get_selected () |
| 4045 |
if not iter: |
| 4046 |
path, column = widget.get_cursor() |
| 4047 |
if iter: |
| 4048 |
iter = model.get_iter (path) |
| 4049 |
else: |
| 4050 |
return |
| 4051 |
driver = model.get_value (iter, 1) |
| 4052 |
if driver == 0: |
| 4053 |
self.ntbkNPDownloadableDriverProperties.set_current_page (self.PAGE_DESCRIBE_PRINTER) |
| 4054 |
self.setNPButtons() |
| 4055 |
return |
| 4056 |
self.ntbkNPDownloadableDriverProperties.set_current_page (self.PAGE_SELECT_DEVICE) |
| 4057 |
supplier = driver.get('supplier', _("OpenPrinting")) |
| 4058 |
vendor = self.cbNPDownloadableDriverSupplierVendor |
| 4059 |
active = driver['manufacturersupplied'] |
| 4060 |
|
| 4061 |
def set_protect_active (widget, active): |
| 4062 |
widget.protect_active = active |
| 4063 |
widget.set_active (active) |
| 4064 |
|
| 4065 |
set_protect_active (vendor, active) |
| 4066 |
self.lblNPDownloadableDriverSupplier.set_text (supplier) |
| 4067 |
|
| 4068 |
license = driver.get('license', _("Distributable")) |
| 4069 |
patents = self.cbNPDownloadableDriverLicensePatents |
| 4070 |
free = self.cbNPDownloadableDriverLicenseFree |
| 4071 |
set_protect_active (patents, driver['patents']) |
| 4072 |
set_protect_active (free, driver['freesoftware']) |
| 4073 |
self.lblNPDownloadableDriverLicense.set_text (license) |
| 4074 |
|
| 4075 |
description = driver.get('shortdescription', _("None")) |
| 4076 |
self.lblNPDownloadableDriverDescription.set_markup (description) |
| 4077 |
|
| 4078 |
if 'functionality' in driver: |
| 4079 |
functionality = driver['functionality'] |
| 4080 |
for field in ["Graphics", "LineArt", "Photo", "Text"]: |
| 4081 |
key = field.lower () |
| 4082 |
value = None |
| 4083 |
hs = self.__dict__.get ("hsDownloadableDriverPerf%s" % field) |
| 4084 |
unknown = self.__dict__.get ("lblDownloadableDriverPerf%sUnknown" |
| 4085 |
% field) |
| 4086 |
if key in functionality: |
| 4087 |
if hs: |
| 4088 |
try: |
| 4089 |
value = int (functionality[key]) |
| 4090 |
hs.set_range (0, 100) |
| 4091 |
hs.set_value (value) |
| 4092 |
hs.show_all () |
| 4093 |
unknown.hide () |
| 4094 |
except: |
| 4095 |
pass |
| 4096 |
|
| 4097 |
if value is None: |
| 4098 |
hs.hide () |
| 4099 |
unknown.show_all () |
| 4100 |
|
| 4101 |
supportcontacts = "" |
| 4102 |
if 'supportcontacts' in driver: |
| 4103 |
for supportentry in driver['supportcontacts']: |
| 4104 |
if supportentry['name']: |
| 4105 |
supportcontact = " - " + supportentry['name'] |
| 4106 |
supportcontact_extra = "" |
| 4107 |
if supportentry['url']: |
| 4108 |
supportcontact_extra = supportcontact_extra + \ |
| 4109 |
supportentry['url'] |
| 4110 |
if supportentry['level']: |
| 4111 |
if supportcontact_extra: |
| 4112 |
supportcontact_extra = supportcontact_extra + _(", ") |
| 4113 |
supportcontact_extra = supportcontact_extra + \ |
| 4114 |
supportentry['level'] |
| 4115 |
if supportcontact_extra: |
| 4116 |
supportcontact = supportcontact + \ |
| 4117 |
_("\n(%s)") % supportcontact_extra |
| 4118 |
if supportcontacts: |
| 4119 |
supportcontacts = supportcontacts + "\n" |
| 4120 |
supportcontacts = supportcontacts + supportcontact |
| 4121 |
if not supportcontacts: |
| 4122 |
supportcontacts = _("No support contacts known") |
| 4123 |
self.lblNPDownloadableDriverSupportContacts.set_text (supportcontacts) |
| 4124 |
if 'licensetext' in driver: |
| 4125 |
self.frmNPDownloadableDriverLicenseTerms.show () |
| 4126 |
terms = driver.get('licensetext', _("Not specified.")) |
| 4127 |
self.tvNPDownloadableDriverLicense.get_buffer ().set_text (terms) |
| 4128 |
else: |
| 4129 |
self.frmNPDownloadableDriverLicenseTerms.hide () |
| 4130 |
if not driver['nonfreesoftware'] and not driver['patents']: |
| 4131 |
self.rbtnNPDownloadLicenseYes.set_active (True) |
| 4132 |
self.rbtnNPDownloadLicenseYes.hide () |
| 4133 |
self.rbtnNPDownloadLicenseNo.hide () |
| 4134 |
else: |
| 4135 |
self.rbtnNPDownloadLicenseNo.set_active (True) |
| 4136 |
self.rbtnNPDownloadLicenseYes.show () |
| 4137 |
self.rbtnNPDownloadLicenseNo.show () |
| 4138 |
self.frmNPDownloadableDriverLicenseTerms.show () |
| 4139 |
terms = driver.get('licensetext', _("Not specified.")) |
| 4140 |
self.tvNPDownloadableDriverLicense.get_buffer ().set_text (terms) |
| 4141 |
|
| 4142 |
if 'ppds' in driver and len(driver["ppds"]) > 0: |
| 4143 |
self.founddownloadableppd = True |
| 4144 |
else: |
| 4145 |
self.founddownloadableppd = False |
| 4146 |
|
| 4147 |
self.setNPButtons() |
| 4148 |
|
| 4149 |
def getNPPPD(self): |
| 4150 |
ppd = None |
| 4151 |
try: |
| 4152 |
if ((self.rbtnNPFoomatic.get_active() or |
| 4153 |
len(self.installed_driver_files) > 0) and |
| 4154 |
self.founddownloadableppd == False): |
| 4155 |
model, iter = self.tvNPDrivers.get_selection().get_selected() |
| 4156 |
nr = model.get_path(iter)[0] |
| 4157 |
ppd = self.NPDrivers[nr] |
| 4158 |
elif self.rbtnNPPPD.get_active(): |
| 4159 |
ppd = cups.PPD(self.filechooserPPD.get_filename()) |
| 4160 |
else: |
| 4161 |
# PPD of the driver downloaded from OpenPrinting XXX |
| 4162 |
treeview = self.tvNPDownloadableDrivers |
| 4163 |
model, iter = treeview.get_selection ().get_selected () |
| 4164 |
driver = model.get_value (iter, 1) |
| 4165 |
if driver != 0 and 'ppds' in driver: |
| 4166 |
# Only need to download a PPD. |
| 4167 |
if (len(driver['ppds']) > 0): |
| 4168 |
file_to_download = driver['ppds'][0] |
| 4169 |
debugprint ("ppd file to download [" + file_to_download+ "]") |
| 4170 |
file_to_download = file_to_download.strip() |
| 4171 |
if (len(file_to_download) > 0): |
| 4172 |
ppdurlobj = urllib.request.urlopen(file_to_download) |
| 4173 |
ppdcontent = ppdurlobj.read() |
| 4174 |
ppdurlobj.close() |
| 4175 |
with tempfile.NamedTemporaryFile () as tmpf: |
| 4176 |
tmpf.write(ppdcontent) |
| 4177 |
tmpf.flush () |
| 4178 |
ppd = cups.PPD(tmpf.name) |
| 4179 |
|
| 4180 |
except (RuntimeError, urllib.error.HTTPError) as e: |
| 4181 |
debugprint ("RuntimeError: " + repr (e)) |
| 4182 |
if self.rbtnNPFoomatic.get_active(): |
| 4183 |
# Foomatic database problem of some sort. |
| 4184 |
err_title = _('Database error') |
| 4185 |
err_text = _("The '%s' driver cannot be " |
| 4186 |
"used with printer '%s %s'.") |
| 4187 |
model, iter = (self.tvNPDrivers.get_selection(). |
| 4188 |
get_selected()) |
| 4189 |
nr = model.get_path(iter)[0] |
| 4190 |
driver = self.NPDrivers[nr] |
| 4191 |
if driver.startswith ("gutenprint"): |
| 4192 |
# This printer references some XML that is not |
| 4193 |
# installed by default. Point the user at the |
| 4194 |
# package they need to install. |
| 4195 |
err = _("You will need to install the '%s' package " |
| 4196 |
"in order to use this driver.") % \ |
| 4197 |
"gutenprint-foomatic" |
| 4198 |
else: |
| 4199 |
err = err_text % (driver, self.NPMake, self.NPModel) |
| 4200 |
elif self.rbtnNPPPD.get_active(): |
| 4201 |
# This error came from trying to open the PPD file. |
| 4202 |
err_title = _('PPD error') |
| 4203 |
filename = self.filechooserPPD.get_filename() |
| 4204 |
err = _('Failed to read PPD file. Possible reason ' |
| 4205 |
'follows:') + '\n' |
| 4206 |
try: |
| 4207 |
# We want this to be in the current natural language, |
| 4208 |
# so we intentionally don't set LC_ALL=C here. |
| 4209 |
p = subprocess.Popen (['/usr/bin/cupstestppd', |
| 4210 |
'-rvv', filename], |
| 4211 |
close_fds=True, |
| 4212 |
stdin=subprocess.DEVNULL, |
| 4213 |
stdout=subprocess.PIPE, |
| 4214 |
stderr=subprocess.PIPE) |
| 4215 |
(stdout, stderr) = p.communicate () |
| 4216 |
err += stdout.decode () |
| 4217 |
except: |
| 4218 |
# Problem executing command. |
| 4219 |
raise |
| 4220 |
else: |
| 4221 |
# Failed to get PPD downloaded from OpenPrinting XXX |
| 4222 |
err_title = _('Downloadable drivers') |
| 4223 |
err = _("Failed to download PPD.") |
| 4224 |
|
| 4225 |
show_error_dialog (err_title, err, self.NewPrinterWindow) |
| 4226 |
return None |
| 4227 |
|
| 4228 |
debugprint("ppd: " + repr(ppd)) |
| 4229 |
|
| 4230 |
if isinstance(ppd, str): |
| 4231 |
self.cups._begin_operation (_("fetching PPD")) |
| 4232 |
try: |
| 4233 |
if ppd != "raw": |
| 4234 |
f = self.cups.getServerPPD(ppd) |
| 4235 |
ppd = cups.PPD(f) |
| 4236 |
os.unlink(f) |
| 4237 |
except RuntimeError: |
| 4238 |
nonfatalException() |
| 4239 |
debugprint ("libcups from CUPS 1.3 not available: never mind") |
| 4240 |
except cups.IPPError: |
| 4241 |
nonfatalException() |
| 4242 |
debugprint ("CUPS 1.3 server not available: never mind") |
| 4243 |
|
| 4244 |
self.cups._end_operation () |
| 4245 |
|
| 4246 |
return ppd |
| 4247 |
|
| 4248 |
# Installable Options |
| 4249 |
|
| 4250 |
def fillNPInstallableOptions(self): |
| 4251 |
debugprint ("Examining installable options") |
| 4252 |
self.installable_options = False |
| 4253 |
self.options = { } |
| 4254 |
|
| 4255 |
container = self.vbNPInstallOptions |
| 4256 |
for child in container.get_children(): |
| 4257 |
container.remove(child) |
| 4258 |
|
| 4259 |
if not self.ppd: |
| 4260 |
l = Gtk.Label(label=_("No Installable Options")) |
| 4261 |
container.add(l) |
| 4262 |
l.show() |
| 4263 |
debugprint ("No PPD so no installable options") |
| 4264 |
return |
| 4265 |
|
| 4266 |
# build option tabs |
| 4267 |
for group in self.ppd.optionGroups: |
| 4268 |
if group.name != "InstallableOptions": |
| 4269 |
continue |
| 4270 |
self.installable_options = True |
| 4271 |
grid = Gtk.Grid() |
| 4272 |
grid.set_column_spacing(6) |
| 4273 |
grid.set_row_spacing(6) |
| 4274 |
container.add(grid) |
| 4275 |
rows = 0 |
| 4276 |
|
| 4277 |
for nr, option in enumerate(group.options): |
| 4278 |
if option.keyword == "PageRegion": |
| 4279 |
continue |
| 4280 |
rows += 1 |
| 4281 |
o = OptionWidget(option, self.ppd, self) |
| 4282 |
grid.attach(o.conflictIcon, 0, nr, 1, 1) |
| 4283 |
|
| 4284 |
hbox = Gtk.Box() |
| 4285 |
if o.label: |
| 4286 |
a = Gtk.Alignment.new (0.5, 0.5, 1.0, 1.0) |
| 4287 |
a.set_padding (0, 0, 0, 6) |
| 4288 |
a.add (o.label) |
| 4289 |
grid.attach(a, 1, nr, 1, 1) |
| 4290 |
grid.attach(hbox, 2, nr, 1, 1) |
| 4291 |
else: |
| 4292 |
grid.attach(hbox, 1, nr, 2, 1) |
| 4293 |
hbox.pack_start(o.selector, False, False, 0) |
| 4294 |
self.options[option.keyword] = o |
| 4295 |
if not self.installable_options: |
| 4296 |
l = Gtk.Label(label=_("No Installable Options")) |
| 4297 |
container.add(l) |
| 4298 |
l.show() |
| 4299 |
self.scrNPInstallableOptions.hide() |
| 4300 |
self.scrNPInstallableOptions.show_all() |
| 4301 |
|
| 4302 |
|
| 4303 |
# Create new Printer |
| 4304 |
def on_btnNPApply_clicked(self, widget): |
| 4305 |
if self.fetchDevices_conn: |
| 4306 |
self.fetchDevices_conn.destroy () |
| 4307 |
self.fetchDevices_conn = None |
| 4308 |
self.dec_spinner_task () |
| 4309 |
|
| 4310 |
if self.ppdsloader: |
| 4311 |
self.ppdsloader.destroy () |
| 4312 |
self.ppdsloader = None |
| 4313 |
|
| 4314 |
if self.printer_finder: |
| 4315 |
self.printer_finder.cancel () |
| 4316 |
self.printer_finder = None |
| 4317 |
self.dec_spinner_task () |
| 4318 |
|
| 4319 |
if self.dialog_mode in ("class", "printer", "printer_with_uri"): |
| 4320 |
name = self.entNPName.get_text() |
| 4321 |
location = self.entNPLocation.get_text() |
| 4322 |
info = self.entNPDescription.get_text() |
| 4323 |
else: |
| 4324 |
name = self._name |
| 4325 |
|
| 4326 |
ppd = self.ppd |
| 4327 |
|
| 4328 |
if self.dialog_mode == "class": |
| 4329 |
members = getCurrentClassMembers(self.tvNCMembers) |
| 4330 |
try: |
| 4331 |
for member in members: |
| 4332 |
try: |
| 4333 |
self.cups.addPrinterToClass(member, name) |
| 4334 |
except RuntimeError: |
| 4335 |
# Printer already in class? |
| 4336 |
continue |
| 4337 |
except cups.IPPError as e: |
| 4338 |
(e, msg) = e.args |
| 4339 |
self.show_IPP_Error(e, msg) |
| 4340 |
return |
| 4341 |
elif self.dialog_mode == "printer" or \ |
| 4342 |
self.dialog_mode == "printer_with_uri": |
| 4343 |
uri = None |
| 4344 |
if self.device.uri: |
| 4345 |
uri = self.device.uri |
| 4346 |
else: |
| 4347 |
uri = self.getDeviceURI() |
| 4348 |
if not self.ppd: # XXX needed? |
| 4349 |
# Go back to previous page to re-select driver. |
| 4350 |
self.nextNPTab(-1) |
| 4351 |
return |
| 4352 |
|
| 4353 |
# write Installable Options to ppd |
| 4354 |
for option in self.options.values(): |
| 4355 |
option.writeback() |
| 4356 |
|
| 4357 |
busy (self.NewPrinterWindow) |
| 4358 |
while Gtk.events_pending (): |
| 4359 |
Gtk.main_iteration () |
| 4360 |
self.cups._begin_operation (_("adding printer %s") % name) |
| 4361 |
try: |
| 4362 |
if isinstance(ppd, str): |
| 4363 |
self.cups.addPrinter(name, ppdname=ppd, |
| 4364 |
device=uri, info=info, location=location) |
| 4365 |
elif ppd is None: # raw queue |
| 4366 |
self.cups.addPrinter(name, device=uri, |
| 4367 |
info=info, location=location) |
| 4368 |
else: |
| 4369 |
# Note: LC_MESSAGES is used here to determine the |
| 4370 |
# page size instead of LC_PAPER. This is not |
| 4371 |
# really correct, but it's what CUPS does so to |
| 4372 |
# avoid confusion we'll follow that method. |
| 4373 |
cupshelpers.setPPDPageSize(ppd, self.language[0]) |
| 4374 |
self.cups.addPrinter(name, ppd=ppd, device=uri, |
| 4375 |
info=info, location=location) |
| 4376 |
except cups.IPPError as e: |
| 4377 |
(e, msg) = e.args |
| 4378 |
ready (self.NewPrinterWindow) |
| 4379 |
self.show_IPP_Error(e, msg) |
| 4380 |
self.cups._end_operation() |
| 4381 |
return |
| 4382 |
except: |
| 4383 |
ready (self.NewPrinterWindow) |
| 4384 |
self.cups._end_operation() |
| 4385 |
fatalException (1) |
| 4386 |
self.cups._end_operation() |
| 4387 |
ready (self.NewPrinterWindow) |
| 4388 |
if self.dialog_mode in ("class", "printer", "printer_with_uri"): |
| 4389 |
self.cups._begin_operation (_("modifying printer %s") % name) |
| 4390 |
try: |
| 4391 |
cupshelpers.activateNewPrinter (self.cups, name) |
| 4392 |
self.cups.setPrinterLocation(name, location) |
| 4393 |
self.cups.setPrinterInfo(name, info) |
| 4394 |
except cups.IPPError as e: |
| 4395 |
(e, msg) = e.args |
| 4396 |
self.show_IPP_Error(e, msg) |
| 4397 |
self.cups._end_operation () |
| 4398 |
return |
| 4399 |
self.cups._end_operation () |
| 4400 |
elif self.dialog_mode == "device": |
| 4401 |
self.cups._begin_operation (_("modifying printer %s") % name) |
| 4402 |
try: |
| 4403 |
uri = self.getDeviceURI() |
| 4404 |
self.cups.addPrinter(name, device=uri) |
| 4405 |
except cups.IPPError as e: |
| 4406 |
(e, msg) = e.args |
| 4407 |
self.show_IPP_Error(e, msg) |
| 4408 |
self.cups._end_operation () |
| 4409 |
return |
| 4410 |
self.cups._end_operation () |
| 4411 |
elif self.dialog_mode == "ppd": |
| 4412 |
if not ppd: |
| 4413 |
ppd = self.ppd = self.getNPPPD() |
| 4414 |
if not ppd: |
| 4415 |
# Go back to previous page to re-select driver. |
| 4416 |
self.nextNPTab(-1) |
| 4417 |
return |
| 4418 |
|
| 4419 |
self.cups._begin_operation (_("modifying printer %s") % name) |
| 4420 |
# set ppd on server and retrieve it |
| 4421 |
# cups doesn't offer a way to just download a ppd ;(= |
| 4422 |
raw = False |
| 4423 |
if isinstance(ppd, str): |
| 4424 |
if self.rbtnChangePPDasIs.get_active(): |
| 4425 |
# To use the PPD as-is we need to prevent CUPS copying |
| 4426 |
# the old options over. Do this by setting it to a |
| 4427 |
# raw queue (no PPD) first. |
| 4428 |
try: |
| 4429 |
self.cups.addPrinter(name, ppdname='raw') |
| 4430 |
except cups.IPPError as e: |
| 4431 |
(e, msg) = e.args |
| 4432 |
self.show_IPP_Error(e, msg) |
| 4433 |
try: |
| 4434 |
self.cups.addPrinter(name, ppdname=ppd) |
| 4435 |
except cups.IPPError as e: |
| 4436 |
(e, msg) = e.args |
| 4437 |
self.show_IPP_Error(e, msg) |
| 4438 |
self.cups._end_operation () |
| 4439 |
return |
| 4440 |
|
| 4441 |
try: |
| 4442 |
filename = self.cups.getPPD(name) |
| 4443 |
ppd = cups.PPD(filename) |
| 4444 |
os.unlink(filename) |
| 4445 |
except cups.IPPError as e: |
| 4446 |
(e, msg) = e.args |
| 4447 |
if e == cups.IPP_NOT_FOUND: |
| 4448 |
raw = True |
| 4449 |
else: |
| 4450 |
self.show_IPP_Error(e, msg) |
| 4451 |
self.cups._end_operation () |
| 4452 |
return |
| 4453 |
else: |
| 4454 |
# We have an actual PPD to upload, not just a name. |
| 4455 |
if ((not self.rbtnChangePPDasIs.get_active()) and |
| 4456 |
isinstance (self.orig_ppd, cups.PPD)): |
| 4457 |
cupshelpers.copyPPDOptions(self.orig_ppd, ppd) |
| 4458 |
else: |
| 4459 |
cupshelpers.setPPDPageSize(ppd, self.language[0]) |
| 4460 |
|
| 4461 |
# write Installable Options to ppd |
| 4462 |
for option in self.options.values(): |
| 4463 |
option.writeback() |
| 4464 |
|
| 4465 |
try: |
| 4466 |
self.cups.addPrinter(name, ppd=ppd) |
| 4467 |
except cups.IPPError as e: |
| 4468 |
(e, msg) = e.args |
| 4469 |
self.show_IPP_Error(e, msg) |
| 4470 |
self.cups._end_operation () |
| 4471 |
return |
| 4472 |
|
| 4473 |
self.cups._end_operation () |
| 4474 |
|
| 4475 |
elif self.dialog_mode == "download_driver": |
| 4476 |
self.nextNPTab(0); |
| 4477 |
|
| 4478 |
self.NewPrinterWindow.hide() |
| 4479 |
if self.dialog_mode in ["printer", "printer_with_uri", "class"]: |
| 4480 |
self.emit ('printer-added', name) |
| 4481 |
elif self.dialog_mode == "download_driver": |
| 4482 |
self.emit ('driver-download-checked', self.installed_driver_files) |
| 4483 |
else: |
| 4484 |
self.emit ('printer-modified', name, self.orig_ppd != self.ppd) |
| 4485 |
|
| 4486 |
self.device = None |
| 4487 |
self.printers = {} |
| 4488 |
|
| 4489 |
def show_help(): |
| 4490 |
print ("\nThis is the test/debug mode of the new-printer dialog of " \ |
| 4491 |
"system-config-printer.\n\n" |
| 4492 |
"Options:\n\n" |
| 4493 |
" --setup-printer URI\n" |
| 4494 |
" Select the (detected) CUPS device URI on start up\n" |
| 4495 |
" and run the new-printer wizard for it.\n\n" |
| 4496 |
" --devid Supply a device ID which should be used for the\n" |
| 4497 |
" setup of the new printer with \"--setup-printer\".\n" |
| 4498 |
" This can be any printer's ID, so that driver \n" |
| 4499 |
" selection can be tested for printers which are not\n" |
| 4500 |
" physically available.\n") |
| 4501 |
|
| 4502 |
if __name__ == '__main__': |
| 4503 |
import getopt |
| 4504 |
try: |
| 4505 |
opts, args = getopt.gnu_getopt (sys.argv[1:], '', |
| 4506 |
['setup-printer=', |
| 4507 |
'devid=']) |
| 4508 |
except getopt.GetoptError: |
| 4509 |
show_help () |
| 4510 |
sys.exit (1) |
| 4511 |
|
| 4512 |
setup_printer = None |
| 4513 |
devid = "" |
| 4514 |
for opt, optarg in opts: |
| 4515 |
if opt == '--setup-printer': |
| 4516 |
setup_printer = optarg |
| 4517 |
elif opt == '--devid': |
| 4518 |
devid = optarg |
| 4519 |
|
| 4520 |
os.environ["SYSTEM_CONFIG_PRINTER_UI"] = "ui" |
| 4521 |
import ppdippstr |
| 4522 |
locale.setlocale (locale.LC_ALL, "") |
| 4523 |
ppdippstr.init () |
| 4524 |
set_debugging (True) |
| 4525 |
cupshelpers.set_debugprint_fn (debugprint) |
| 4526 |
|
| 4527 |
n = NewPrinterGUI () |
| 4528 |
def on_signal (*args): |
| 4529 |
Gtk.main_quit () |
| 4530 |
|
| 4531 |
n.connect ("printer-added", on_signal) |
| 4532 |
n.connect ("printer-modified", on_signal) |
| 4533 |
n.connect ("dialog-canceled", on_signal) |
| 4534 |
if setup_printer is not None: |
| 4535 |
n.init ("printer_with_uri", device_uri=setup_printer, devid=devid) |
| 4536 |
else: |
| 4537 |
n.init ("printer") |
| 4538 |
Gtk.main () |