| 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 sys, os, time, re |
| 28 |
import _thread |
| 29 |
import dbus |
| 30 |
import gi |
| 31 |
try: |
| 32 |
gi.require_version('Polkit', '1.0') |
| 33 |
from gi.repository import Polkit |
| 34 |
except: |
| 35 |
Polkit = False |
| 36 |
|
| 37 |
gi.require_version('GdkPixbuf', '2.0') |
| 38 |
from gi.repository import GdkPixbuf |
| 39 |
try: |
| 40 |
gi.require_version('Gdk', '3.0') |
| 41 |
from gi.repository import Gdk |
| 42 |
gi.require_version('Gtk', '3.0') |
| 43 |
from gi.repository import Gtk |
| 44 |
Gtk.init (sys.argv) |
| 45 |
except RuntimeError as e: |
| 46 |
print ("system-config-printer:", e) |
| 47 |
print ("This is a graphical application and requires DISPLAY to be set.") |
| 48 |
sys.exit (1) |
| 49 |
|
| 50 |
try: |
| 51 |
# Optional dependency, requires libhandy >= 1.5 |
| 52 |
gi.require_version('Handy', '1') |
| 53 |
from gi.repository import Handy |
| 54 |
Handy.init() |
| 55 |
# Support GNOME 42 dark mode |
| 56 |
Handy.StyleManager.get_default().set_color_scheme(Handy.ColorScheme.PREFER_LIGHT) |
| 57 |
except: |
| 58 |
pass |
| 59 |
|
| 60 |
def show_help(): |
| 61 |
print ("\nThis is system-config-printer, " \ |
| 62 |
"a CUPS server configuration program.\n\n" |
| 63 |
"Options:\n\n" |
| 64 |
" --debug Enable debugging output.\n" |
| 65 |
" --show-jobs <printer> Show the print queue for <printer>\n" |
| 66 |
" --embedded Enable to start in Embedded mode.\n " |
| 67 |
" --help Show this message.\n") |
| 68 |
|
| 69 |
if len(sys.argv)>1 and sys.argv[1] == '--help': |
| 70 |
show_help () |
| 71 |
sys.exit (0) |
| 72 |
|
| 73 |
import cups |
| 74 |
cups.require ("1.9.46") |
| 75 |
cups.ppdSetConformance (cups.PPD_CONFORM_RELAXED) |
| 76 |
|
| 77 |
import locale |
| 78 |
try: |
| 79 |
locale.setlocale (locale.LC_ALL, "") |
| 80 |
except locale.Error: |
| 81 |
os.environ['LC_ALL'] = 'C' |
| 82 |
locale.setlocale (locale.LC_ALL, "") |
| 83 |
import gettext |
| 84 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 85 |
|
| 86 |
import cupshelpers |
| 87 |
from gi.repository import GObject |
| 88 |
from gi.repository import GLib |
| 89 |
from gui import GtkGUI |
| 90 |
from debug import * |
| 91 |
import urllib.request, urllib.parse, urllib.error |
| 92 |
import troubleshoot |
| 93 |
import installpackage |
| 94 |
import jobviewer |
| 95 |
import authconn |
| 96 |
import monitor |
| 97 |
import errordialogs |
| 98 |
from errordialogs import * |
| 99 |
import userdefault |
| 100 |
from serversettings import ServerSettings |
| 101 |
from ToolbarSearchEntry import * |
| 102 |
from SearchCriterion import * |
| 103 |
import statereason |
| 104 |
import newprinter |
| 105 |
from newprinter import busy, ready |
| 106 |
import printerproperties |
| 107 |
|
| 108 |
import ppdippstr |
| 109 |
ppdippstr.init () |
| 110 |
pkgdata = config.pkgdatadir |
| 111 |
iconpath = os.path.join (pkgdata, 'icons/') |
| 112 |
sys.path.append (pkgdata) |
| 113 |
|
| 114 |
PlugWindow = None |
| 115 |
PlugWindowId = None |
| 116 |
|
| 117 |
#set program name |
| 118 |
GLib.set_prgname("system-config-printer") |
| 119 |
|
| 120 |
def CUPS_server_hostname (): |
| 121 |
host = cups.getServer () |
| 122 |
if host[0] == '/': |
| 123 |
return 'localhost' |
| 124 |
return host |
| 125 |
|
| 126 |
class ServiceStart: |
| 127 |
|
| 128 |
def _get_iface (self, iface): |
| 129 |
bus = dbus.SystemBus () |
| 130 |
obj = bus.get_object (self.NAME, self.PATH) |
| 131 |
proxy = dbus.Interface (obj, iface) |
| 132 |
return proxy |
| 133 |
|
| 134 |
def can_start (self): |
| 135 |
try: |
| 136 |
proxy = self._get_iface (dbus.INTROSPECTABLE_IFACE) |
| 137 |
introspect = proxy.Introspect() |
| 138 |
except: |
| 139 |
return False |
| 140 |
return True |
| 141 |
|
| 142 |
def start(self, reply_handler, error_handler): |
| 143 |
proxy = self._get_iface(self.IFACE) |
| 144 |
self._start(proxy, reply_handler, error_handler) |
| 145 |
|
| 146 |
|
| 147 |
class SysVServiceStart(ServiceStart): |
| 148 |
NAME="org.fedoraproject.Config.Services" |
| 149 |
PATH="/org/fedoraproject/Config/Services/ServiceHerders/SysVServiceHerder/Services/cups" |
| 150 |
IFACE="org.fedoraproject.Config.Services.SysVService" |
| 151 |
|
| 152 |
def _start(self, proxy, reply_handler, error_handler): |
| 153 |
proxy.start(reply_handler=reply_handler, |
| 154 |
error_handler=error_handler) |
| 155 |
|
| 156 |
|
| 157 |
class SystemDServiceStart(ServiceStart): |
| 158 |
NAME="org.freedesktop.systemd1" |
| 159 |
PATH="/org/freedesktop/systemd1" |
| 160 |
IFACE="org.freedesktop.systemd1.Manager" |
| 161 |
CUPS_SERVICE="org.cups.cupsd.service" |
| 162 |
|
| 163 |
def _start(self, proxy, reply_handler, error_handler): |
| 164 |
proxy.StartUnit(self.CUPS_SERVICE, 'fail', |
| 165 |
reply_handler=reply_handler, |
| 166 |
error_handler=error_handler) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
class GUI(GtkGUI): |
| 171 |
|
| 172 |
printer_states = { cups.IPP_PRINTER_IDLE: _("Idle"), |
| 173 |
cups.IPP_PRINTER_PROCESSING: _("Processing"), |
| 174 |
cups.IPP_PRINTER_BUSY: _("Busy"), |
| 175 |
cups.IPP_PRINTER_STOPPED: _("Stopped") } |
| 176 |
|
| 177 |
DESTS_PAGE_DESTS=0 |
| 178 |
DESTS_PAGE_NO_PRINTERS=1 |
| 179 |
DESTS_PAGE_NO_SERVICE=2 |
| 180 |
|
| 181 |
def __init__(self): |
| 182 |
|
| 183 |
super (GtkGUI, self).__init__ () |
| 184 |
|
| 185 |
try: |
| 186 |
self.language = locale.getlocale(locale.LC_MESSAGES) |
| 187 |
self.encoding = locale.getlocale(locale.LC_CTYPE) |
| 188 |
except: |
| 189 |
nonfatalException() |
| 190 |
os.environ['LC_ALL'] = 'C' |
| 191 |
locale.setlocale (locale.LC_ALL, "") |
| 192 |
self.language = locale.getlocale(locale.LC_MESSAGES) |
| 193 |
self.encoding = locale.getlocale(locale.LC_CTYPE) |
| 194 |
|
| 195 |
self.printers = {} |
| 196 |
self.connect_server = cups.getServer() |
| 197 |
self.connect_encrypt = cups.getEncryption () |
| 198 |
self.connect_user = cups.getUser() |
| 199 |
self.monitor = None |
| 200 |
self.populateList_timer = None |
| 201 |
|
| 202 |
self.servers = set((self.connect_server,)) |
| 203 |
self.server_is_publishing = None # not known |
| 204 |
self.changed = set() # of options |
| 205 |
|
| 206 |
# WIDGETS |
| 207 |
# ======= |
| 208 |
self.updating_widgets = False |
| 209 |
self.getWidgets({"PrintersWindow": |
| 210 |
["PrintersWindow", |
| 211 |
"hboxMenuBar", |
| 212 |
"view_area_vbox", |
| 213 |
"view_area_scrolledwindow", |
| 214 |
"dests_notebook", |
| 215 |
"dests_iconview", |
| 216 |
"btnAddFirstPrinter", |
| 217 |
"btnStartService", |
| 218 |
"btnConnectNoService", |
| 219 |
"statusbarMain", |
| 220 |
"toolbar", |
| 221 |
"server_menubar_item", |
| 222 |
"printer_menubar_item", |
| 223 |
"view_discovered_printers"], |
| 224 |
"AboutDialog": |
| 225 |
["AboutDialog"], |
| 226 |
"ConnectDialog": |
| 227 |
["ConnectDialog", |
| 228 |
"chkEncrypted", |
| 229 |
"cmbServername", |
| 230 |
"btnConnect"], |
| 231 |
"ConnectingDialog": |
| 232 |
["ConnectingDialog", |
| 233 |
"lblConnecting", |
| 234 |
"pbarConnecting"], |
| 235 |
"NewPrinterName": |
| 236 |
["NewPrinterName", |
| 237 |
"entDuplicateName", |
| 238 |
"btnDuplicateOk"], |
| 239 |
"InstallDialog": |
| 240 |
["InstallDialog", |
| 241 |
"lblInstall"]}, |
| 242 |
|
| 243 |
domain=config.PACKAGE) |
| 244 |
|
| 245 |
if PlugWindowId: |
| 246 |
self.PrintersWindow.hide() |
| 247 |
# the "vbox4" widget |
| 248 |
vbox = self.PrintersWindow.get_children()[0] |
| 249 |
PlugWindow = Gtk.Plug.new(PlugWindowId) |
| 250 |
Gtk.Container.remove(self.PrintersWindow, vbox) |
| 251 |
PlugWindow.add(vbox) |
| 252 |
self.PrintersWindow.set_transient_for(PlugWindow) |
| 253 |
PlugWindow.show_all() |
| 254 |
self.PrintersWindow = PlugWindow |
| 255 |
|
| 256 |
# Since some dialogs are reused we can't let the delete-event's |
| 257 |
# default handler destroy them |
| 258 |
self.ConnectingDialog.connect ("delete-event", |
| 259 |
self.on_connectingdialog_delete) |
| 260 |
|
| 261 |
Gtk.Window.set_default_icon_name ('printer') |
| 262 |
|
| 263 |
edit_action = 'org.opensuse.cupspkhelper.mechanism.all-edit' |
| 264 |
self.edit_permission = None |
| 265 |
if Polkit: |
| 266 |
try: |
| 267 |
self.edit_permission = Polkit.Permission.new_sync (edit_action, |
| 268 |
None, None) |
| 269 |
except GLib.GError: |
| 270 |
pass # Maybe cups-pk-helper isn't installed. |
| 271 |
|
| 272 |
self.unlock_button = Gtk.LockButton () |
| 273 |
if self.edit_permission is not None: |
| 274 |
self.edit_permission.connect ("notify::allowed", |
| 275 |
self.polkit_permission_changed) |
| 276 |
|
| 277 |
self.unlock_button.connect ("notify::permission", |
| 278 |
self.polkit_permission_changed) |
| 279 |
self.hboxMenuBar.pack_start (self.unlock_button, False, False, 12) |
| 280 |
|
| 281 |
# Printer Actions |
| 282 |
printer_manager_action_group = \ |
| 283 |
Gtk.ActionGroup (name="PrinterManagerActionGroup") |
| 284 |
printer_manager_action_group.add_actions ([ |
| 285 |
("connect-to-server", Gtk.STOCK_CONNECT, _("_Connect..."), |
| 286 |
None, _("Choose a different CUPS server"), |
| 287 |
self.on_connect_activate), |
| 288 |
("server-settings", Gtk.STOCK_PREFERENCES, _("_Settings..."), |
| 289 |
None, _("Adjust server settings"), |
| 290 |
self.on_server_settings_activate), |
| 291 |
("new-printer", Gtk.STOCK_PRINT, _("_Printer"), |
| 292 |
None, None, self.on_new_printer_activate), |
| 293 |
("new-class", Gtk.STOCK_DND_MULTIPLE, _("_Class"), |
| 294 |
None, None, self.on_new_class_activate), |
| 295 |
("quit", Gtk.STOCK_QUIT, None, None, None, |
| 296 |
self.on_quit_activate)]) |
| 297 |
printer_manager_action_group.add_actions ([ |
| 298 |
("rename-printer", None, _("_Rename"), |
| 299 |
None, None, self.on_rename_activate), |
| 300 |
("duplicate-printer", Gtk.STOCK_COPY, _("_Duplicate"), |
| 301 |
"<Ctrl>d", None, self.on_duplicate_activate), |
| 302 |
("delete-printer", Gtk.STOCK_DELETE, None, |
| 303 |
None, None, self.on_delete_activate), |
| 304 |
("set-default-printer", Gtk.STOCK_HOME, _("Set As De_fault"), |
| 305 |
None, None, self.on_set_as_default_activate), |
| 306 |
("edit-printer", Gtk.STOCK_PROPERTIES, None, |
| 307 |
None, None, self.on_edit_activate), |
| 308 |
("create-class", Gtk.STOCK_DND_MULTIPLE, _("_Create class"), |
| 309 |
None, None, self.on_create_class_activate), |
| 310 |
("view-print-queue", Gtk.STOCK_FIND, _("View Print _Queue"), |
| 311 |
None, None, self.on_view_print_queue_activate), |
| 312 |
]) |
| 313 |
printer_manager_action_group.add_toggle_actions ([ |
| 314 |
("enable-printer", None, _("E_nabled"), |
| 315 |
None, None, self.on_enabled_activate), |
| 316 |
("share-printer", None, _("_Shared"), |
| 317 |
None, None, self.on_shared_activate), |
| 318 |
]) |
| 319 |
printer_manager_action_group.add_radio_actions ([ |
| 320 |
("filter-name", None, _("Name")), |
| 321 |
("filter-description", None, _("Description")), |
| 322 |
("filter-location", None, _("Location")), |
| 323 |
("filter-manufacturer", None, _("Manufacturer / Model")), |
| 324 |
], 1, self.on_filter_criterion_changed) |
| 325 |
for action in printer_manager_action_group.list_actions (): |
| 326 |
action.set_sensitive (False) |
| 327 |
for action in ["connect-to-server", |
| 328 |
"quit", |
| 329 |
"view-print-queue", |
| 330 |
"filter-name", |
| 331 |
"filter-description", |
| 332 |
"filter-location", |
| 333 |
"filter-manufacturer"]: |
| 334 |
act = printer_manager_action_group.get_action (action) |
| 335 |
act.set_sensitive (True) |
| 336 |
|
| 337 |
self.ui_manager = Gtk.UIManager () |
| 338 |
self.ui_manager.insert_action_group (printer_manager_action_group, -1) |
| 339 |
self.ui_manager.add_ui_from_string ( |
| 340 |
""" |
| 341 |
<ui> |
| 342 |
<accelerator action="connect-to-server"/> |
| 343 |
<accelerator action="server-settings"/> |
| 344 |
<accelerator action="new-printer"/> |
| 345 |
<accelerator action="new-class"/> |
| 346 |
<accelerator action="quit"/> |
| 347 |
|
| 348 |
<accelerator action="rename-printer"/> |
| 349 |
<accelerator action="duplicate-printer"/> |
| 350 |
<accelerator action="delete-printer"/> |
| 351 |
<accelerator action="set-default-printer"/> |
| 352 |
<accelerator action="edit-printer"/> |
| 353 |
<accelerator action="create-class"/> |
| 354 |
<accelerator action="view-print-queue"/> |
| 355 |
<accelerator action="enable-printer"/> |
| 356 |
<accelerator action="share-printer"/> |
| 357 |
<accelerator action="filter-name"/> |
| 358 |
<accelerator action="filter-description"/> |
| 359 |
<accelerator action="filter-location"/> |
| 360 |
<accelerator action="filter-manufacturer"/> |
| 361 |
</ui> |
| 362 |
""" |
| 363 |
) |
| 364 |
self.ui_manager.ensure_update () |
| 365 |
self.PrintersWindow.add_accel_group (self.ui_manager.get_accel_group ()) |
| 366 |
|
| 367 |
# Toolbar |
| 368 |
# Glade-3 doesn't have support for MenuToolButton, so we do that here. |
| 369 |
self.btnNew = Gtk.MenuToolButton () |
| 370 |
self.btnNew.set_label (_("Add")) |
| 371 |
self.btnNew.set_icon_name ("list-add") |
| 372 |
self.btnNew.set_is_important (True) |
| 373 |
newmenu = Gtk.Menu () |
| 374 |
action = self.ui_manager.get_action ("/new-printer") |
| 375 |
newprinteritem = action.create_menu_item () |
| 376 |
action = self.ui_manager.get_action ("/new-class") |
| 377 |
newclassitem = action.create_menu_item () |
| 378 |
newprinteritem.show () |
| 379 |
newclassitem.show () |
| 380 |
newmenu.attach (newprinteritem, 0, 1, 0, 1) |
| 381 |
newmenu.attach (newclassitem, 0, 1, 1, 2) |
| 382 |
self.btnNew.set_menu (newmenu) |
| 383 |
self.btnNew.connect ('clicked', self.on_new_printer_activate) |
| 384 |
self.toolbar.add (self.btnNew) |
| 385 |
self.toolbar.add (Gtk.SeparatorToolItem ()) |
| 386 |
self.refreshbutton = Gtk.ToolButton () |
| 387 |
self.refreshbutton.set_label (_("Refresh")) |
| 388 |
self.refreshbutton.set_icon_name ("view-refresh") |
| 389 |
self.refreshbutton.connect ('clicked', self.on_btnRefresh_clicked) |
| 390 |
self.toolbar.add (self.refreshbutton) |
| 391 |
self.toolbar.show_all () |
| 392 |
|
| 393 |
server_context_menu = Gtk.Menu () |
| 394 |
for action_name in ["connect-to-server", |
| 395 |
"server-settings", |
| 396 |
None, |
| 397 |
"new", |
| 398 |
None, |
| 399 |
"quit"]: |
| 400 |
if action_name == "new": |
| 401 |
item = Gtk.MenuItem.new_with_mnemonic(_("_New")) |
| 402 |
item.set_sensitive (True) |
| 403 |
self.menuItemNew = item |
| 404 |
elif not action_name: |
| 405 |
item = Gtk.SeparatorMenuItem () |
| 406 |
else: |
| 407 |
action = printer_manager_action_group.get_action (action_name) |
| 408 |
item = action.create_menu_item () |
| 409 |
item.show () |
| 410 |
server_context_menu.append (item) |
| 411 |
self.server_menubar_item.set_submenu (server_context_menu) |
| 412 |
|
| 413 |
new_menu = Gtk.Menu () |
| 414 |
for action_name in ["new-printer", |
| 415 |
"new-class"]: |
| 416 |
action = printer_manager_action_group.get_action (action_name) |
| 417 |
item = action.create_menu_item () |
| 418 |
item.show () |
| 419 |
new_menu.append (item) |
| 420 |
self.menuItemNew.set_submenu (new_menu) |
| 421 |
|
| 422 |
self.printer_context_menu = Gtk.Menu () |
| 423 |
for action_name in ["edit-printer", |
| 424 |
"duplicate-printer", |
| 425 |
"rename-printer", |
| 426 |
"delete-printer", |
| 427 |
None, |
| 428 |
"enable-printer", |
| 429 |
"share-printer", |
| 430 |
"create-class", |
| 431 |
"set-default-printer", |
| 432 |
None, |
| 433 |
"view-print-queue"]: |
| 434 |
if not action_name: |
| 435 |
item = Gtk.SeparatorMenuItem () |
| 436 |
else: |
| 437 |
action = printer_manager_action_group.get_action (action_name) |
| 438 |
item = action.create_menu_item () |
| 439 |
item.show () |
| 440 |
self.printer_context_menu.append (item) |
| 441 |
self.printer_menubar_item.set_submenu (self.printer_context_menu) |
| 442 |
|
| 443 |
self.jobviewers = [] # to keep track of jobviewer windows |
| 444 |
|
| 445 |
# New Printer Dialog |
| 446 |
self.newPrinterGUI = np = newprinter.NewPrinterGUI() |
| 447 |
np.connect ("printer-added", self.on_new_printer_added) |
| 448 |
np.connect ("printer-modified", self.on_printer_modified) |
| 449 |
np.connect ("dialog-canceled", self.on_new_printer_not_added) |
| 450 |
|
| 451 |
# Set up "About" dialog |
| 452 |
self.AboutDialog.set_program_name(config.PACKAGE) |
| 453 |
self.AboutDialog.set_version(config.VERSION) |
| 454 |
self.AboutDialog.set_icon_name('printer') |
| 455 |
|
| 456 |
try: |
| 457 |
self.cups = authconn.Connection(self.PrintersWindow) |
| 458 |
except RuntimeError: |
| 459 |
self.cups = None |
| 460 |
|
| 461 |
self.status_context_id = self.statusbarMain.get_context_id( |
| 462 |
"Connection") |
| 463 |
|
| 464 |
# Setup search |
| 465 |
self.setup_toolbar_for_search_entry () |
| 466 |
self.current_filter_text = "" |
| 467 |
self.current_filter_mode = "filter-name" |
| 468 |
|
| 469 |
# Search entry drop down menu |
| 470 |
menu = Gtk.Menu () |
| 471 |
for action_name in ["filter-name", |
| 472 |
"filter-description", |
| 473 |
"filter-location", |
| 474 |
"filter-manufacturer"]: |
| 475 |
action = printer_manager_action_group.get_action (action_name) |
| 476 |
item = action.create_menu_item () |
| 477 |
menu.append (item) |
| 478 |
menu.show_all () |
| 479 |
self.search_entry.set_drop_down_menu (menu) |
| 480 |
|
| 481 |
if os.path.exists("/usr/lib/systemd"): |
| 482 |
self.servicestart = SystemDServiceStart() |
| 483 |
else: |
| 484 |
self.servicestart = SysVServiceStart() |
| 485 |
|
| 486 |
# Setup icon view |
| 487 |
self.mainlist = Gtk.ListStore(GObject.TYPE_PYOBJECT, # Object |
| 488 |
GdkPixbuf.Pixbuf, # Pixbuf |
| 489 |
str, # Name |
| 490 |
str) # Tooltip |
| 491 |
|
| 492 |
self.dests_iconview.set_model(self.mainlist) |
| 493 |
self.dests_iconview.set_column_spacing (30) |
| 494 |
self.dests_iconview.set_row_spacing (20) |
| 495 |
self.dests_iconview.set_pixbuf_column (1) |
| 496 |
self.dests_iconview.set_text_column (2) |
| 497 |
self.dests_iconview.set_tooltip_column (3) |
| 498 |
self.dests_iconview.set_has_tooltip(True) |
| 499 |
self.dests_iconview.connect ('key-press-event', |
| 500 |
self.dests_iconview_key_press_event) |
| 501 |
self.dests_iconview.connect ('item-activated', |
| 502 |
self.dests_iconview_item_activated) |
| 503 |
self.dests_iconview.connect ('selection-changed', |
| 504 |
self.dests_iconview_selection_changed) |
| 505 |
self.dests_iconview.connect ('button-press-event', |
| 506 |
self.dests_iconview_button_press_event) |
| 507 |
self.dests_iconview.connect ('popup-menu', |
| 508 |
self.dests_iconview_popup_menu) |
| 509 |
self.dests_iconview_selection_changed (self.dests_iconview) |
| 510 |
self.dests_iconview.enable_model_drag_source (Gdk.ModifierType.BUTTON1_MASK, |
| 511 |
# should use a variable |
| 512 |
# instead of 0 |
| 513 |
[Gtk.TargetEntry.new("queue", 0, 0)], |
| 514 |
Gdk.DragAction.COPY) |
| 515 |
self.dests_iconview.connect ("drag-data-get", |
| 516 |
self.dests_iconview_drag_data_get) |
| 517 |
self.btnStartService.connect ('clicked', self.on_start_service_clicked) |
| 518 |
self.btnConnectNoService.connect ('clicked', self.on_connect_activate) |
| 519 |
self.btnAddFirstPrinter.connect ('clicked', |
| 520 |
self.on_new_printer_activate) |
| 521 |
|
| 522 |
# Printer Properties dialog |
| 523 |
self.propertiesDlg = printerproperties.PrinterPropertiesDialog () |
| 524 |
self.propertiesDlg.connect ("dialog-closed", |
| 525 |
self.on_properties_dialog_closed) |
| 526 |
|
| 527 |
self.connect_signals () |
| 528 |
|
| 529 |
try: |
| 530 |
self.populateList() |
| 531 |
except cups.HTTPError as e: |
| 532 |
(s,) = e.args |
| 533 |
self.cups = None |
| 534 |
self.populateList() |
| 535 |
show_HTTP_Error(s, self.PrintersWindow) |
| 536 |
|
| 537 |
self.setConnected() |
| 538 |
|
| 539 |
if len (self.printers) > 4: |
| 540 |
self.PrintersWindow.set_default_size (720, 345) |
| 541 |
elif len (self.printers) > 2: |
| 542 |
self.PrintersWindow.set_default_size (500, 345) |
| 543 |
elif len (self.printers) > 1: |
| 544 |
self.PrintersWindow.set_default_size (500, 180) |
| 545 |
|
| 546 |
|
| 547 |
self.PrintersWindow.show() |
| 548 |
|
| 549 |
def display_properties_dialog_for (self, queue): |
| 550 |
model = self.dests_iconview.get_model () |
| 551 |
iter = model.get_iter_first () |
| 552 |
while iter is not None: |
| 553 |
name = model.get_value (iter, 2) |
| 554 |
if name == queue: |
| 555 |
path = model.get_path (iter) |
| 556 |
self.dests_iconview.scroll_to_path (path, True, 0.5, 0.5) |
| 557 |
self.dests_iconview.set_cursor (path=path, cell=None, |
| 558 |
start_editing=False) |
| 559 |
self.dests_iconview.item_activated (path) |
| 560 |
break |
| 561 |
iter = model.iter_next (iter) |
| 562 |
|
| 563 |
if iter is None: |
| 564 |
raise RuntimeError |
| 565 |
|
| 566 |
def setup_toolbar_for_search_entry (self): |
| 567 |
separator = Gtk.SeparatorToolItem () |
| 568 |
separator.set_draw (False) |
| 569 |
|
| 570 |
self.toolbar.insert (separator, -1) |
| 571 |
self.toolbar.child_set_property (separator, "expand", True) |
| 572 |
|
| 573 |
self.search_entry = ToolbarSearchEntry () |
| 574 |
self.search_entry.connect ('search', self.on_search_entry_search) |
| 575 |
|
| 576 |
tool_item = Gtk.ToolItem () |
| 577 |
tool_item.add (self.search_entry) |
| 578 |
self.toolbar.insert (tool_item, -1) |
| 579 |
self.toolbar.show_all () |
| 580 |
|
| 581 |
def on_search_entry_search (self, UNUSED, text): |
| 582 |
self.current_filter_text = text |
| 583 |
self.populateList () |
| 584 |
|
| 585 |
def on_filter_criterion_changed (self, UNUSED, selected_action): |
| 586 |
self.current_filter_mode = selected_action.get_name () |
| 587 |
self.populateList () |
| 588 |
|
| 589 |
def dests_iconview_item_activated (self, iconview, path): |
| 590 |
model = iconview.get_model () |
| 591 |
iter = model.get_iter (path) |
| 592 |
name = model.get_value (iter, 2) |
| 593 |
object = model.get_value (iter, 0) |
| 594 |
|
| 595 |
self.desensitise_main_window_widgets () |
| 596 |
try: |
| 597 |
self.propertiesDlg.show (name, host=self.connect_server, |
| 598 |
encryption=self.connect_encrypt, |
| 599 |
parent=self.PrintersWindow) |
| 600 |
except cups.IPPError as e: |
| 601 |
(e, m) = e.args |
| 602 |
self.sensitise_main_window_widgets () |
| 603 |
show_IPP_Error (e, m, self.PrintersWindow) |
| 604 |
if e == cups.IPP_SERVICE_UNAVAILABLE: |
| 605 |
self.cups = None |
| 606 |
self.setConnected () |
| 607 |
self.populateList () |
| 608 |
return |
| 609 |
except RuntimeError: |
| 610 |
self.sensitise_main_window_widgets () |
| 611 |
# Perhaps cupsGetPPD2 failed for a browsed printer. |
| 612 |
|
| 613 |
# Check that we're still connected. |
| 614 |
self.monitor.update () |
| 615 |
return |
| 616 |
|
| 617 |
def on_properties_dialog_closed (self, obj): |
| 618 |
self.sensitise_main_window_widgets () |
| 619 |
|
| 620 |
def dests_iconview_selection_changed (self, iconview): |
| 621 |
self.updating_widgets = True |
| 622 |
permission = self.unlock_button.get_permission () |
| 623 |
if permission: |
| 624 |
can_edit = permission.get_allowed () |
| 625 |
else: |
| 626 |
can_edit = True |
| 627 |
|
| 628 |
if not can_edit: |
| 629 |
return |
| 630 |
|
| 631 |
paths = iconview.get_selected_items () |
| 632 |
any_disabled = False |
| 633 |
any_enabled = False |
| 634 |
any_discovered = False |
| 635 |
any_shared = False |
| 636 |
any_unshared = False |
| 637 |
model = iconview.get_model () |
| 638 |
for path in paths: |
| 639 |
iter = model.get_iter (path) |
| 640 |
object = model.get_value (iter, 0) |
| 641 |
name = model.get_value (iter, 2) |
| 642 |
if object.discovered: |
| 643 |
any_discovered = True |
| 644 |
if object.enabled: |
| 645 |
any_enabled = True |
| 646 |
else: |
| 647 |
any_disabled = True |
| 648 |
if object.is_shared: |
| 649 |
any_shared = True |
| 650 |
else: |
| 651 |
any_unshared = True |
| 652 |
|
| 653 |
n = len (paths) |
| 654 |
self.ui_manager.get_action ("/edit-printer").set_sensitive (n == 1) |
| 655 |
|
| 656 |
self.ui_manager.get_action ("/duplicate-printer").set_sensitive (n == 1) |
| 657 |
|
| 658 |
self.ui_manager.get_action ("/rename-printer").set_sensitive ( |
| 659 |
n == 1 and not any_discovered) |
| 660 |
|
| 661 |
userdef = userdefault.UserDefaultPrinter ().get () |
| 662 |
if (n != 1 or |
| 663 |
(userdef is None and self.default_printer == name)): |
| 664 |
set_default_sensitivity = False |
| 665 |
else: |
| 666 |
set_default_sensitivity = True |
| 667 |
|
| 668 |
self.ui_manager.get_action ("/set-default-printer").set_sensitive ( |
| 669 |
set_default_sensitivity) |
| 670 |
|
| 671 |
action = self.ui_manager.get_action ("/enable-printer") |
| 672 |
action.set_sensitive (n > 0 and not any_discovered) |
| 673 |
for widget in action.get_proxies (): |
| 674 |
if isinstance (widget, Gtk.CheckMenuItem): |
| 675 |
widget.set_inconsistent (n > 1 and any_enabled and any_disabled) |
| 676 |
action.set_active (any_discovered or not any_disabled) |
| 677 |
|
| 678 |
action = self.ui_manager.get_action ("/share-printer") |
| 679 |
action.set_sensitive (n > 0 and not any_discovered) |
| 680 |
for widget in action.get_proxies (): |
| 681 |
if isinstance (widget, Gtk.CheckMenuItem): |
| 682 |
widget.set_inconsistent (n > 1 and any_shared and any_unshared) |
| 683 |
action.set_active (any_discovered or not any_unshared) |
| 684 |
|
| 685 |
self.ui_manager.get_action ("/delete-printer").set_sensitive ( |
| 686 |
n > 0 and not any_discovered) |
| 687 |
|
| 688 |
self.ui_manager.get_action ("/create-class").set_sensitive (n > 1) |
| 689 |
|
| 690 |
self.updating_widgets = False |
| 691 |
|
| 692 |
def dests_iconview_popup_menu (self, iconview): |
| 693 |
self.printer_context_menu.popup_for_device (None, None, None, None, |
| 694 |
None, 0, 0) |
| 695 |
|
| 696 |
def dests_iconview_button_press_event (self, iconview, event): |
| 697 |
if event.button > 1: |
| 698 |
click_path = iconview.get_path_at_pos (int (event.x), |
| 699 |
int (event.y)) |
| 700 |
paths = iconview.get_selected_items () |
| 701 |
if click_path is None: |
| 702 |
iconview.unselect_all () |
| 703 |
elif click_path not in paths: |
| 704 |
iconview.unselect_all () |
| 705 |
iconview.select_path (click_path) |
| 706 |
cells = iconview.get_cells () |
| 707 |
for cell in cells: |
| 708 |
if type (cell) == Gtk.CellRendererText: |
| 709 |
break |
| 710 |
iconview.set_cursor (click_path, cell, False) |
| 711 |
self.printer_context_menu.popup_for_device (None, None, None, None, |
| 712 |
None, event.button, event.time) |
| 713 |
return False |
| 714 |
|
| 715 |
def dests_iconview_key_press_event (self, iconview, event): |
| 716 |
modifiers = Gtk.accelerator_get_default_mod_mask () |
| 717 |
|
| 718 |
if ((event.keyval == Gdk.KEY_BackSpace or |
| 719 |
event.keyval == Gdk.KEY_Delete or |
| 720 |
event.keyval == Gdk.KEY_KP_Delete) and |
| 721 |
((event.get_state() & modifiers) == 0)): |
| 722 |
|
| 723 |
self.ui_manager.get_action ("/delete-printer").activate () |
| 724 |
return True |
| 725 |
|
| 726 |
if ((event.keyval == Gdk.KEY_F2) and |
| 727 |
((event.get_state() & modifiers) == 0)): |
| 728 |
|
| 729 |
self.ui_manager.get_action ("/rename-printer").activate () |
| 730 |
return True |
| 731 |
|
| 732 |
return False |
| 733 |
|
| 734 |
def dests_iconview_drag_data_get (self, iconview, context, |
| 735 |
selection_data, info, timestamp): |
| 736 |
if info == 0: # FIXME: should use an "enum" here |
| 737 |
model = iconview.get_model () |
| 738 |
paths = iconview.get_selected_items () |
| 739 |
selected_printer_names = "" |
| 740 |
for path in paths: |
| 741 |
selected_printer_names += \ |
| 742 |
model.get_value (model.get_iter (path), 2) + "\n" |
| 743 |
|
| 744 |
if len (selected_printer_names) > 0: |
| 745 |
selection_data.set ("queue", 8, selected_printer_names) |
| 746 |
else: |
| 747 |
nonfatalException () |
| 748 |
|
| 749 |
def dests_iconview_set_accessible_names (self): |
| 750 |
iv_acc = self.dests_iconview.get_accessible () |
| 751 |
for i in range (iv_acc.get_n_accessible_children ()): |
| 752 |
acc = iv_acc.ref_accessible_child (i) |
| 753 |
acc.set_name (acc.get_text (0, -1)) |
| 754 |
|
| 755 |
def on_server_settings_activate (self, menuitem): |
| 756 |
try: |
| 757 |
self.serverSettings = ServerSettings (host=self.connect_server, |
| 758 |
encryption=self.connect_encrypt, |
| 759 |
parent=self.PrintersWindow) |
| 760 |
self.serverSettings.connect ('problems-clicked', |
| 761 |
self.on_problems_button_clicked) |
| 762 |
except (cups.IPPError, cups.HTTPError): |
| 763 |
# Not authorized. |
| 764 |
return |
| 765 |
except RuntimeError: |
| 766 |
self.monitor.update () |
| 767 |
|
| 768 |
def setConnected(self): |
| 769 |
connected = bool(self.cups) |
| 770 |
|
| 771 |
host = CUPS_server_hostname () |
| 772 |
self.PrintersWindow.set_title(_("Print Settings - %s") % host) |
| 773 |
|
| 774 |
if connected: |
| 775 |
status_msg = _("Connected to %s") % host |
| 776 |
else: |
| 777 |
status_msg = _("Not connected") |
| 778 |
self.statusbarMain.push(self.status_context_id, status_msg) |
| 779 |
|
| 780 |
for widget in (self.btnNew, |
| 781 |
self.menuItemNew): |
| 782 |
widget.set_sensitive(connected) |
| 783 |
|
| 784 |
for action_name in ["/server-settings", |
| 785 |
"/new-printer", |
| 786 |
"/new-class"]: |
| 787 |
action = self.ui_manager.get_action (action_name) |
| 788 |
action.set_sensitive (connected) |
| 789 |
|
| 790 |
if connected: |
| 791 |
if self.monitor: |
| 792 |
self.monitor.cleanup () |
| 793 |
|
| 794 |
self.monitor = monitor.Monitor (monitor_jobs=False, |
| 795 |
host=self.connect_server, |
| 796 |
encryption=self.connect_encrypt) |
| 797 |
self.monitor.connect ('printer-added', self.printer_added) |
| 798 |
self.monitor.connect ('printer-event', self.printer_event) |
| 799 |
self.monitor.connect ('printer-removed', self.printer_removed) |
| 800 |
self.monitor.connect ('cups-connection-error', |
| 801 |
self.cups_connection_error) |
| 802 |
self.monitor.connect ('cups-connection-recovered', |
| 803 |
self.cups_connection_recovered) |
| 804 |
GLib.idle_add (self.monitor.refresh) |
| 805 |
self.propertiesDlg.set_monitor (self.monitor) |
| 806 |
|
| 807 |
if connected: |
| 808 |
if self.cups._using_polkit (): |
| 809 |
self.unlock_button.set_permission (self.edit_permission) |
| 810 |
else: |
| 811 |
self.unlock_button.set_permission (None) |
| 812 |
|
| 813 |
else: |
| 814 |
self.unlock_button.set_permission (None) |
| 815 |
|
| 816 |
def polkit_permission_changed (self, widget, UNUSED): |
| 817 |
permission = self.unlock_button.get_permission () |
| 818 |
if permission: |
| 819 |
can_edit = permission.get_allowed () |
| 820 |
else: |
| 821 |
can_edit = True |
| 822 |
|
| 823 |
self.btnNew.set_sensitive (can_edit) |
| 824 |
self.btnAddFirstPrinter.set_sensitive (can_edit) |
| 825 |
for action in ["/new-printer", |
| 826 |
"/new-class"]: |
| 827 |
act = self.ui_manager.get_action (action) |
| 828 |
act.set_sensitive (can_edit) |
| 829 |
|
| 830 |
if can_edit: |
| 831 |
self.dests_iconview_selection_changed (self.dests_iconview) |
| 832 |
else: |
| 833 |
for action in ["/rename-printer", |
| 834 |
"/duplicate-printer", |
| 835 |
"/delete-printer", |
| 836 |
"/edit-printer", |
| 837 |
"/create-class", |
| 838 |
"/enable-printer", |
| 839 |
"/share-printer"]: |
| 840 |
act = self.ui_manager.get_action (action) |
| 841 |
act.set_sensitive (False) |
| 842 |
|
| 843 |
def getServers(self): |
| 844 |
self.servers.discard(None) |
| 845 |
known_servers = list(self.servers) |
| 846 |
known_servers.sort() |
| 847 |
return known_servers |
| 848 |
|
| 849 |
def populateList(self, prompt_allowed=True): |
| 850 |
# Save selection of printers. |
| 851 |
selected_printers = set() |
| 852 |
paths = self.dests_iconview.get_selected_items () |
| 853 |
model = self.dests_iconview.get_model () |
| 854 |
for path in paths: |
| 855 |
iter = model.get_iter (path) |
| 856 |
name = model.get_value (iter, 2) |
| 857 |
selected_printers.add (name) |
| 858 |
|
| 859 |
if self.cups: |
| 860 |
kill_connection = False |
| 861 |
self.cups._set_prompt_allowed (prompt_allowed) |
| 862 |
self.cups._begin_operation (_("obtaining queue details")) |
| 863 |
try: |
| 864 |
# get Printers |
| 865 |
self.printers = cupshelpers.getPrinters(self.cups) |
| 866 |
|
| 867 |
# Get default printer. |
| 868 |
self.default_printer = self.cups.getDefault () |
| 869 |
except cups.IPPError as e: |
| 870 |
(e, m) = e.args |
| 871 |
show_IPP_Error(e, m, self.PrintersWindow) |
| 872 |
self.printers = {} |
| 873 |
self.default_printer = None |
| 874 |
if e == cups.IPP_SERVICE_UNAVAILABLE: |
| 875 |
kill_connection = True |
| 876 |
|
| 877 |
self.cups._end_operation () |
| 878 |
self.cups._set_prompt_allowed (True) |
| 879 |
if kill_connection: |
| 880 |
self.cups = None |
| 881 |
else: |
| 882 |
self.printers = {} |
| 883 |
self.default_printer = None |
| 884 |
|
| 885 |
for name, printer in self.printers.items(): |
| 886 |
self.servers.add(printer.getServer()) |
| 887 |
|
| 888 |
userdef = userdefault.UserDefaultPrinter ().get () |
| 889 |
|
| 890 |
local_printers = [] |
| 891 |
local_classes = [] |
| 892 |
remote_printers = [] |
| 893 |
remote_classes = [] |
| 894 |
|
| 895 |
delete_action = self.ui_manager.get_action ("/delete-printer") |
| 896 |
delete_action.set_properties (label = None) |
| 897 |
printers_set = self.printers |
| 898 |
|
| 899 |
# Filter printers |
| 900 |
if len (self.current_filter_text) > 0: |
| 901 |
printers_subset = {} |
| 902 |
pattern = re.compile (self.current_filter_text, re.I) # ignore case |
| 903 |
|
| 904 |
if self.current_filter_mode == "filter-name": |
| 905 |
for name in printers_set.keys (): |
| 906 |
if pattern.search (name) is not None: |
| 907 |
printers_subset[name] = printers_set[name] |
| 908 |
elif self.current_filter_mode == "filter-description": |
| 909 |
for name, printer in printers_set.items (): |
| 910 |
if pattern.search (printer.info) is not None: |
| 911 |
printers_subset[name] = printers_set[name] |
| 912 |
elif self.current_filter_mode == "filter-location": |
| 913 |
for name, printer in printers_set.items (): |
| 914 |
if pattern.search (printer.location) is not None: |
| 915 |
printers_subset[name] = printers_set[name] |
| 916 |
elif self.current_filter_mode == "filter-manufacturer": |
| 917 |
for name, printer in printers_set.items (): |
| 918 |
if pattern.search (printer.make_and_model) is not None: |
| 919 |
printers_subset[name] = printers_set[name] |
| 920 |
else: |
| 921 |
nonfatalException () |
| 922 |
|
| 923 |
printers_set = printers_subset |
| 924 |
|
| 925 |
if not self.view_discovered_printers.get_active (): |
| 926 |
printers_subset = {} |
| 927 |
for name, printer in printers_set.items (): |
| 928 |
if not printer.discovered: |
| 929 |
printers_subset[name] = printer |
| 930 |
|
| 931 |
printers_set = printers_subset |
| 932 |
|
| 933 |
for name, printer in list(printers_set.items()): |
| 934 |
if printer.remote: |
| 935 |
if printer.is_class: remote_classes.append(name) |
| 936 |
else: remote_printers.append(name) |
| 937 |
else: |
| 938 |
if printer.is_class: local_classes.append(name) |
| 939 |
else: local_printers.append(name) |
| 940 |
|
| 941 |
local_printers.sort() |
| 942 |
local_classes.sort() |
| 943 |
remote_printers.sort() |
| 944 |
remote_classes.sort() |
| 945 |
|
| 946 |
# remove old printers/classes |
| 947 |
self.mainlist.clear () |
| 948 |
|
| 949 |
# add new |
| 950 |
PRINTER_TYPE = { 'discovered-printer': |
| 951 |
(_("Network printer (discovered)"), |
| 952 |
'i-network-printer'), |
| 953 |
'discovered-class': |
| 954 |
(_("Network class (discovered)"), |
| 955 |
'i-network-printer'), |
| 956 |
'local-printer': |
| 957 |
(_("Printer"), |
| 958 |
'printer'), |
| 959 |
'local-fax': |
| 960 |
(_("Fax"), |
| 961 |
'printer'), |
| 962 |
'local-class': |
| 963 |
(_("Class"), |
| 964 |
'printer'), |
| 965 |
'ipp-printer': |
| 966 |
(_("Network printer"), |
| 967 |
'i-network-printer'), |
| 968 |
'smb-printer': |
| 969 |
(_("Network print share"), |
| 970 |
'i-network-printer'), |
| 971 |
'network-printer': |
| 972 |
(_("Network printer"), |
| 973 |
'i-network-printer'), |
| 974 |
} |
| 975 |
theme = Gtk.IconTheme.get_default () |
| 976 |
for printers in (local_printers, |
| 977 |
local_classes, |
| 978 |
remote_printers, |
| 979 |
remote_classes): |
| 980 |
if not printers: continue |
| 981 |
for name in printers: |
| 982 |
type = 'local-printer' |
| 983 |
object = printers_set[name] |
| 984 |
if object.discovered: |
| 985 |
if object.is_class: |
| 986 |
type = 'discovered-class' |
| 987 |
else: |
| 988 |
type = 'discovered-printer' |
| 989 |
elif object.is_class: |
| 990 |
type = 'local-class' |
| 991 |
else: |
| 992 |
parsed = urllib.parse.urlparse (object.device_uri) |
| 993 |
if parsed.scheme in ['ipp', 'ipps']: |
| 994 |
if parsed.netloc.startswith("localhost"): # IPP-over-USB |
| 995 |
type = 'local-printer' |
| 996 |
else: # IPP network printer |
| 997 |
type = 'ipp-printer' |
| 998 |
elif parsed.scheme == 'smb': |
| 999 |
type = 'smb-printer' |
| 1000 |
elif parsed.scheme == 'hpfax': |
| 1001 |
type = 'local-fax' |
| 1002 |
elif parsed.scheme in ['socket', 'lpd', 'dnssd']: |
| 1003 |
type = 'network-printer' |
| 1004 |
elif object.device_uri.startswith('hp:/net/'): |
| 1005 |
type = 'network-printer' |
| 1006 |
elif object.device_uri.startswith('hpfax:/net/'): |
| 1007 |
type = 'network-printer' |
| 1008 |
elif parsed.scheme == 'implicitclass': # cups-browsed-discovered |
| 1009 |
type = 'discovered-printer' |
| 1010 |
|
| 1011 |
(tip, icon) = PRINTER_TYPE[type] |
| 1012 |
(result, w, h) = Gtk.icon_size_lookup (Gtk.IconSize.DIALOG) |
| 1013 |
try: |
| 1014 |
pixbuf = theme.load_icon (icon, w, 0) |
| 1015 |
except GLib.GError: |
| 1016 |
# Not in theme. |
| 1017 |
pixbuf = None |
| 1018 |
for p in [iconpath, 'icons/']: |
| 1019 |
try: |
| 1020 |
pixbuf = GdkPixbuf.Pixbuf.new_from_file ("%s%s.png" % |
| 1021 |
(p, icon)) |
| 1022 |
break |
| 1023 |
except GLib.GError: |
| 1024 |
pass |
| 1025 |
|
| 1026 |
if pixbuf is None: |
| 1027 |
try: |
| 1028 |
pixbuf = theme.load_icon ('printer', w, 0) |
| 1029 |
except: |
| 1030 |
# Just create an empty pixbuf. |
| 1031 |
pixbuf = GdkPixbuf.Pixbuf.new (GdkPixbuf.Colorspace.RGB, |
| 1032 |
True, 8, w, h) |
| 1033 |
pixbuf.fill (0) |
| 1034 |
|
| 1035 |
def_emblem = None |
| 1036 |
emblem = None |
| 1037 |
if name == self.default_printer: |
| 1038 |
def_emblem = 'emblem-default' |
| 1039 |
elif name == userdef: |
| 1040 |
def_emblem = 'emblem-favorite' |
| 1041 |
|
| 1042 |
if not emblem: |
| 1043 |
attrs = object.other_attributes |
| 1044 |
reasons = attrs.get ('printer-state-reasons', []) |
| 1045 |
worst_reason = None |
| 1046 |
for reason in reasons: |
| 1047 |
if reason == "none": |
| 1048 |
break |
| 1049 |
|
| 1050 |
if reason == "paused": |
| 1051 |
emblem = "media-playback-pause" |
| 1052 |
continue |
| 1053 |
|
| 1054 |
r = statereason.StateReason (object.name, reason) |
| 1055 |
if worst_reason is None: |
| 1056 |
worst_reason = r |
| 1057 |
elif r > worst_reason: |
| 1058 |
worst_reason = r |
| 1059 |
|
| 1060 |
if worst_reason: |
| 1061 |
level = worst_reason.get_level () |
| 1062 |
emblem = worst_reason.LEVEL_ICON[level] |
| 1063 |
|
| 1064 |
if not emblem and not object.enabled: |
| 1065 |
emblem = "media-playback-pause" |
| 1066 |
|
| 1067 |
if object.rejecting: |
| 1068 |
# Show the icon as insensitive |
| 1069 |
copy = pixbuf.copy () |
| 1070 |
copy.fill (0) |
| 1071 |
pixbuf.composite (copy, 0, 0, |
| 1072 |
pixbuf.get_width(), pixbuf.get_height(), |
| 1073 |
0, 0, 1.0, 1.0, |
| 1074 |
GdkPixbuf.InterpType.BILINEAR, 127) |
| 1075 |
pixbuf = copy |
| 1076 |
|
| 1077 |
if def_emblem: |
| 1078 |
(result, w, h) = Gtk.icon_size_lookup (Gtk.IconSize.DIALOG) |
| 1079 |
try: |
| 1080 |
default_emblem = theme.load_icon (def_emblem, w/2, 0) |
| 1081 |
copy = pixbuf.copy () |
| 1082 |
default_emblem.composite (copy, 0, 0, |
| 1083 |
default_emblem.get_width (), |
| 1084 |
default_emblem.get_height (), |
| 1085 |
0, 0, |
| 1086 |
1.0, 1.0, |
| 1087 |
GdkPixbuf.InterpType.BILINEAR, |
| 1088 |
255) |
| 1089 |
pixbuf = copy |
| 1090 |
except GLib.GError: |
| 1091 |
debugprint ("No %s icon available" % def_emblem) |
| 1092 |
|
| 1093 |
if emblem: |
| 1094 |
(result, w, h) = Gtk.icon_size_lookup (Gtk.IconSize.DIALOG) |
| 1095 |
try: |
| 1096 |
other_emblem = theme.load_icon (emblem, w/2, 0) |
| 1097 |
copy = pixbuf.copy () |
| 1098 |
other_emblem.composite (copy, |
| 1099 |
copy.get_width () / 2, |
| 1100 |
copy.get_height () / 2, |
| 1101 |
other_emblem.get_width (), |
| 1102 |
other_emblem.get_height (), |
| 1103 |
copy.get_width () / 2, |
| 1104 |
copy.get_height () / 2, |
| 1105 |
1.0, 1.0, |
| 1106 |
GdkPixbuf.InterpType.BILINEAR, |
| 1107 |
255) |
| 1108 |
pixbuf = copy |
| 1109 |
except GLib.GError: |
| 1110 |
debugprint ("No %s icon available" % emblem) |
| 1111 |
|
| 1112 |
self.mainlist.append (row=[object, pixbuf, name, tip]) |
| 1113 |
|
| 1114 |
# Restore selection of printers. |
| 1115 |
model = self.dests_iconview.get_model () |
| 1116 |
def maybe_select (model, path, iter, UNUSED): |
| 1117 |
name = model.get_value (iter, 2) |
| 1118 |
if name in selected_printers: |
| 1119 |
self.dests_iconview.select_path (path) |
| 1120 |
model.foreach (maybe_select, None) |
| 1121 |
|
| 1122 |
# Set up the dests_notebook page. |
| 1123 |
page = self.DESTS_PAGE_DESTS |
| 1124 |
if self.cups: |
| 1125 |
if (not self.current_filter_text and |
| 1126 |
not self.mainlist.get_iter_first ()): |
| 1127 |
page = self.DESTS_PAGE_NO_PRINTERS |
| 1128 |
else: |
| 1129 |
page = self.DESTS_PAGE_NO_SERVICE |
| 1130 |
can_start = (self.connect_server == 'localhost' or |
| 1131 |
self.connect_server[0] != '/') |
| 1132 |
tooltip_text = None |
| 1133 |
if can_start: |
| 1134 |
can_start = self.servicestart.can_start () |
| 1135 |
if not can_start: |
| 1136 |
tooltip_text = _("Service framework not available") |
| 1137 |
else: |
| 1138 |
tooltip_text = _("Cannot start service on remote server") |
| 1139 |
|
| 1140 |
self.btnStartService.set_sensitive (can_start) |
| 1141 |
self.btnStartService.set_tooltip_text (tooltip_text) |
| 1142 |
|
| 1143 |
self.dests_notebook.set_current_page (page) |
| 1144 |
self.dests_iconview_set_accessible_names () |
| 1145 |
|
| 1146 |
# Connect to Server |
| 1147 |
|
| 1148 |
def on_connect_servername_changed(self, widget): |
| 1149 |
self.btnConnect.set_sensitive (len (widget.get_active_text () or '') > 0) |
| 1150 |
|
| 1151 |
def on_connect_activate(self, widget): |
| 1152 |
# Use browsed queues to build up a list of known IPP servers |
| 1153 |
servers = self.getServers() |
| 1154 |
current_server = (self.propertiesDlg.printer and |
| 1155 |
self.propertiesDlg.printer.getServer()) \ |
| 1156 |
or cups.getServer() |
| 1157 |
|
| 1158 |
store = Gtk.ListStore (str) |
| 1159 |
self.cmbServername.set_model(store) |
| 1160 |
self.cmbServername.set_entry_text_column (0) |
| 1161 |
for server in servers: |
| 1162 |
self.cmbServername.append_text(server) |
| 1163 |
self.cmbServername.show() |
| 1164 |
|
| 1165 |
self.cmbServername.get_child().set_text (current_server) |
| 1166 |
self.chkEncrypted.set_active (cups.getEncryption() == |
| 1167 |
cups.HTTP_ENCRYPT_ALWAYS) |
| 1168 |
|
| 1169 |
self.cmbServername.get_child().set_activates_default (True) |
| 1170 |
self.cmbServername.grab_focus () |
| 1171 |
self.ConnectDialog.set_transient_for (self.PrintersWindow) |
| 1172 |
response = self.ConnectDialog.run() |
| 1173 |
|
| 1174 |
self.ConnectDialog.hide() |
| 1175 |
|
| 1176 |
if response != Gtk.ResponseType.OK: |
| 1177 |
return |
| 1178 |
|
| 1179 |
if self.chkEncrypted.get_active(): |
| 1180 |
cups.setEncryption(cups.HTTP_ENCRYPT_ALWAYS) |
| 1181 |
else: |
| 1182 |
cups.setEncryption(cups.HTTP_ENCRYPT_IF_REQUESTED) |
| 1183 |
self.connect_encrypt = cups.getEncryption () |
| 1184 |
|
| 1185 |
servername = self.cmbServername.get_child().get_text() |
| 1186 |
|
| 1187 |
self.lblConnecting.set_markup(_("<i>Opening connection to %s</i>") |
| 1188 |
% servername) |
| 1189 |
self.ConnectingDialog.set_transient_for(self.PrintersWindow) |
| 1190 |
self.ConnectingDialog.show() |
| 1191 |
GLib.timeout_add (40, self.update_connecting_pbar) |
| 1192 |
self.connect_server = servername |
| 1193 |
# We need to set the connecting user in this thread as well. |
| 1194 |
cups.setServer(self.connect_server) |
| 1195 |
cups.setUser('') |
| 1196 |
self.connect_user = cups.getUser() |
| 1197 |
# Now start a new thread for connection. |
| 1198 |
self.connect_thread = _thread.start_new_thread(self.connect, |
| 1199 |
(self.PrintersWindow,)) |
| 1200 |
|
| 1201 |
def update_connecting_pbar (self): |
| 1202 |
ret = True |
| 1203 |
Gdk.threads_enter () |
| 1204 |
try: |
| 1205 |
if not self.ConnectingDialog.get_property ("visible"): |
| 1206 |
ret = False # stop animation |
| 1207 |
else: |
| 1208 |
self.pbarConnecting.pulse () |
| 1209 |
finally: |
| 1210 |
Gdk.threads_leave () |
| 1211 |
|
| 1212 |
return ret |
| 1213 |
|
| 1214 |
def on_connectingdialog_delete (self, widget, event): |
| 1215 |
self.on_cancel_connect_clicked (widget) |
| 1216 |
return True |
| 1217 |
|
| 1218 |
def on_cancel_connect_clicked(self, widget): |
| 1219 |
""" |
| 1220 |
Stop connection to new server |
| 1221 |
(Doesn't really stop but sets flag for the connecting thread to |
| 1222 |
ignore the connection) |
| 1223 |
""" |
| 1224 |
self.connect_thread = None |
| 1225 |
self.ConnectingDialog.hide() |
| 1226 |
|
| 1227 |
def connect(self, parent=None): |
| 1228 |
""" |
| 1229 |
Open a connection to a new server. Is executed in a separate thread! |
| 1230 |
""" |
| 1231 |
cups.setUser(self.connect_user) |
| 1232 |
if self.connect_server[0] == '/': |
| 1233 |
# UNIX domain socket. This may potentially fail if the server |
| 1234 |
# settings have been changed and cupsd has written out a |
| 1235 |
# configuration that does not include a Listen line for the |
| 1236 |
# UNIX domain socket. To handle this special case, try to |
| 1237 |
# connect once and fall back to "localhost" on failure. |
| 1238 |
try: |
| 1239 |
connection = cups.Connection (host=self.connect_server, |
| 1240 |
encryption=self.connect_encrypt) |
| 1241 |
|
| 1242 |
# Worked fine. Disconnect, and we'll connect for real |
| 1243 |
# shortly. |
| 1244 |
del connection |
| 1245 |
except RuntimeError: |
| 1246 |
# When we connect, avoid the domain socket. |
| 1247 |
cups.setServer ("localhost") |
| 1248 |
except: |
| 1249 |
nonfatalException () |
| 1250 |
|
| 1251 |
try: |
| 1252 |
connection = authconn.Connection(parent, |
| 1253 |
host=self.connect_server, |
| 1254 |
encryption=self.connect_encrypt) |
| 1255 |
except RuntimeError as s: |
| 1256 |
if self.connect_thread != _thread.get_ident(): return |
| 1257 |
Gdk.threads_enter() |
| 1258 |
try: |
| 1259 |
self.ConnectingDialog.hide() |
| 1260 |
self.cups = None |
| 1261 |
self.setConnected() |
| 1262 |
self.populateList() |
| 1263 |
show_IPP_Error(None, s, parent) |
| 1264 |
finally: |
| 1265 |
Gdk.threads_leave() |
| 1266 |
return |
| 1267 |
except cups.IPPError as e: |
| 1268 |
(e, s) = e.args |
| 1269 |
if self.connect_thread != _thread.get_ident(): return |
| 1270 |
Gdk.threads_enter() |
| 1271 |
try: |
| 1272 |
self.ConnectingDialog.hide() |
| 1273 |
self.cups = None |
| 1274 |
self.setConnected() |
| 1275 |
self.populateList() |
| 1276 |
show_IPP_Error(e, s, parent) |
| 1277 |
finally: |
| 1278 |
Gdk.threads_leave() |
| 1279 |
return |
| 1280 |
except: |
| 1281 |
nonfatalException () |
| 1282 |
|
| 1283 |
if self.connect_thread != _thread.get_ident(): return |
| 1284 |
Gdk.threads_enter() |
| 1285 |
|
| 1286 |
try: |
| 1287 |
self.ConnectingDialog.hide() |
| 1288 |
self.cups = connection |
| 1289 |
self.setConnected() |
| 1290 |
self.populateList() |
| 1291 |
except cups.HTTPError as e: |
| 1292 |
(s,) = e.args |
| 1293 |
self.cups = None |
| 1294 |
self.setConnected() |
| 1295 |
self.populateList() |
| 1296 |
show_HTTP_Error(s, parent) |
| 1297 |
except: |
| 1298 |
nonfatalException () |
| 1299 |
|
| 1300 |
Gdk.threads_leave() |
| 1301 |
|
| 1302 |
def reconnect (self): |
| 1303 |
"""Reconnect to CUPS after the server has reloaded.""" |
| 1304 |
# libcups would handle the reconnection if we just told it to |
| 1305 |
# do something, for example fetching a list of classes. |
| 1306 |
# However, our local authentication certificate would be |
| 1307 |
# invalidated by a server restart, so it is better for us to |
| 1308 |
# handle the reconnection ourselves. |
| 1309 |
|
| 1310 |
attempt = 1 |
| 1311 |
while attempt <= 5: |
| 1312 |
try: |
| 1313 |
time.sleep(1) |
| 1314 |
self.cups._connect () |
| 1315 |
break |
| 1316 |
except RuntimeError: |
| 1317 |
# Connection failed. |
| 1318 |
attempt += 1 |
| 1319 |
|
| 1320 |
def on_btnCancelConnect_clicked(self, widget): |
| 1321 |
"""Close Connect dialog""" |
| 1322 |
self.ConnectWindow.hide() |
| 1323 |
|
| 1324 |
# refresh |
| 1325 |
|
| 1326 |
def on_btnRefresh_clicked(self, button): |
| 1327 |
if self.cups is None: |
| 1328 |
try: |
| 1329 |
self.cups = authconn.Connection(self.PrintersWindow) |
| 1330 |
except RuntimeError: |
| 1331 |
pass |
| 1332 |
|
| 1333 |
self.setConnected() |
| 1334 |
|
| 1335 |
self.populateList() |
| 1336 |
|
| 1337 |
# set default printer |
| 1338 |
def set_system_or_user_default_printer (self, name): |
| 1339 |
# First, decide if this is already the system default, in which |
| 1340 |
# case we only need to clear the user default. |
| 1341 |
userdef = userdefault.UserDefaultPrinter () |
| 1342 |
if name == self.default_printer: |
| 1343 |
userdef.clear () |
| 1344 |
self.populateList () |
| 1345 |
return |
| 1346 |
|
| 1347 |
userdefault.UserDefaultPrompt (self.set_default_printer, |
| 1348 |
self.populateList, |
| 1349 |
name, |
| 1350 |
_("Set Default Printer"), |
| 1351 |
self.PrintersWindow, |
| 1352 |
_("Do you want to set this as " |
| 1353 |
"the system-wide default printer?"), |
| 1354 |
_("Set as the _system-wide " |
| 1355 |
"default printer"), |
| 1356 |
_("_Clear my personal default setting"), |
| 1357 |
_("Set as my _personal default printer")) |
| 1358 |
|
| 1359 |
def set_default_printer (self, name): |
| 1360 |
printer = self.printers[name] |
| 1361 |
reload = False |
| 1362 |
self.cups._begin_operation (_("setting default printer")) |
| 1363 |
try: |
| 1364 |
reload = printer.setAsDefault () |
| 1365 |
except cups.HTTPError as e: |
| 1366 |
(s,) = e.args |
| 1367 |
show_HTTP_Error (s, self.PrintersWindow) |
| 1368 |
self.cups._end_operation () |
| 1369 |
return |
| 1370 |
except cups.IPPError as e: |
| 1371 |
(e, msg) = e.args |
| 1372 |
show_IPP_Error(e, msg, self.PrintersWindow) |
| 1373 |
self.cups._end_operation () |
| 1374 |
return |
| 1375 |
|
| 1376 |
self.cups._end_operation () |
| 1377 |
|
| 1378 |
# Now reconnect in case the server needed to reload. This may |
| 1379 |
# happen if we replaced the lpoptions file. |
| 1380 |
if reload: |
| 1381 |
self.reconnect () |
| 1382 |
|
| 1383 |
try: |
| 1384 |
self.populateList() |
| 1385 |
except cups.HTTPError as e: |
| 1386 |
(s,) = e.args |
| 1387 |
self.cups = None |
| 1388 |
self.setConnected() |
| 1389 |
self.populateList() |
| 1390 |
show_HTTP_Error(s, self.PrintersWindow) |
| 1391 |
|
| 1392 |
# Quit |
| 1393 |
|
| 1394 |
def on_quit_activate(self, widget, event=None): |
| 1395 |
if self.populateList_timer: |
| 1396 |
GLib.source_remove (self.populateList_timer) |
| 1397 |
|
| 1398 |
self.populateList_timer = None |
| 1399 |
if self.monitor: |
| 1400 |
self.monitor.cleanup () |
| 1401 |
|
| 1402 |
while len (self.jobviewers) > 0: |
| 1403 |
# this will call on_jobviewer_exit |
| 1404 |
self.jobviewers[0].on_delete_event () |
| 1405 |
self.propertiesDlg.destroy () |
| 1406 |
self.newPrinterGUI.destroy () |
| 1407 |
Gtk.main_quit() |
| 1408 |
del self.mainlist |
| 1409 |
del self.printers |
| 1410 |
|
| 1411 |
# Rename |
| 1412 |
def is_rename_possible (self, name): |
| 1413 |
jobs = self.printers[name].jobsQueued (limit=1) |
| 1414 |
if len (jobs) > 0: |
| 1415 |
show_error_dialog (_("Cannot Rename"), |
| 1416 |
_("There are queued jobs."), |
| 1417 |
parent=self.PrintersWindow) |
| 1418 |
return False |
| 1419 |
|
| 1420 |
return True |
| 1421 |
|
| 1422 |
def rename_confirmed_by_user (self, name): |
| 1423 |
""" |
| 1424 |
Renaming deletes job history. So if we have some completed jobs, |
| 1425 |
inform the user and let him confirm the renaming. |
| 1426 |
""" |
| 1427 |
preserved_jobs = self.printers[name].jobsPreserved(limit=1) |
| 1428 |
if len (preserved_jobs) > 0: |
| 1429 |
dialog = Gtk.MessageDialog (parent=self.PrintersWindow, |
| 1430 |
modal=True, destroy_with_parent=True, |
| 1431 |
message_type=Gtk.MessageType.WARNING, |
| 1432 |
buttons=Gtk.ButtonsType.OK_CANCEL, |
| 1433 |
text=_("Renaming will lose history")) |
| 1434 |
|
| 1435 |
dialog.format_secondary_text (_("Completed jobs will no longer " |
| 1436 |
"be available for re-printing.")) |
| 1437 |
result = dialog.run() |
| 1438 |
dialog.destroy () |
| 1439 |
if result == Gtk.ResponseType.CANCEL: |
| 1440 |
return False |
| 1441 |
|
| 1442 |
return True |
| 1443 |
|
| 1444 |
def on_rename_activate(self, *UNUSED): |
| 1445 |
tuple = self.dests_iconview.get_cursor () |
| 1446 |
if tuple is None: |
| 1447 |
return |
| 1448 |
|
| 1449 |
(res, path, cell) = tuple |
| 1450 |
if path is None: |
| 1451 |
# Printer removed? |
| 1452 |
return |
| 1453 |
|
| 1454 |
if type (cell) != Gtk.CellRendererText: |
| 1455 |
cells = self.dests_iconview.get_cells () |
| 1456 |
for cell in cells: |
| 1457 |
if type (cell) == Gtk.CellRendererText: |
| 1458 |
break |
| 1459 |
if type (cell) != Gtk.CellRendererText: |
| 1460 |
return |
| 1461 |
|
| 1462 |
model = self.dests_iconview.get_model () |
| 1463 |
iter = model.get_iter (path) |
| 1464 |
name = model.get_value (iter, 2) |
| 1465 |
if not self.is_rename_possible (name): |
| 1466 |
return |
| 1467 |
if not self.rename_confirmed_by_user (name): |
| 1468 |
return |
| 1469 |
cell.set_property ('editable', True) |
| 1470 |
ids = [] |
| 1471 |
ids.append (cell.connect ('editing-started', |
| 1472 |
self.printer_name_edit_start)) |
| 1473 |
ids.append (cell.connect ('editing-canceled', |
| 1474 |
self.printer_name_edit_cancel)) |
| 1475 |
self.rename_sigids = ids |
| 1476 |
self.rename_entry_sigids = [] |
| 1477 |
self.dests_iconview.set_cursor (path, cell, True) |
| 1478 |
|
| 1479 |
def printer_name_edit_start (self, cell, editable, path): |
| 1480 |
debugprint ("editing-started with cell=%s, editable=%s" % |
| 1481 |
(repr (cell), |
| 1482 |
repr (editable))) |
| 1483 |
if isinstance(editable, Gtk.Entry): |
| 1484 |
id = editable.connect('changed', self.printer_name_editing) |
| 1485 |
self.rename_entry_sigids.append ((editable, id)) |
| 1486 |
|
| 1487 |
model = self.dests_iconview.get_model () |
| 1488 |
iter = model.get_iter (path) |
| 1489 |
name = model.get_value (iter, 2) |
| 1490 |
id = editable.connect('editing-done', |
| 1491 |
self.printer_name_editing_done, |
| 1492 |
cell, name) |
| 1493 |
self.rename_entry_sigids.append ((editable, id)) |
| 1494 |
|
| 1495 |
def printer_name_editing (self, entry): |
| 1496 |
newname = origname = entry.get_text() |
| 1497 |
newname = newname.replace("/", "") |
| 1498 |
newname = newname.replace("#", "") |
| 1499 |
newname = newname.replace(" ", "") |
| 1500 |
if origname != newname: |
| 1501 |
debugprint ("removed disallowed character %s" % origname[-1]) |
| 1502 |
entry.set_text(newname) |
| 1503 |
|
| 1504 |
def printer_name_editing_done (self, entry, cell, name): |
| 1505 |
debugprint (repr (cell)) |
| 1506 |
newname = entry.get_text () |
| 1507 |
debugprint ("edited: %s -> %s" % (name, newname)) |
| 1508 |
try: |
| 1509 |
self.rename_printer (name, newname) |
| 1510 |
finally: |
| 1511 |
cell.stop_editing (False) |
| 1512 |
cell.set_property ('editable', False) |
| 1513 |
for id in self.rename_sigids: |
| 1514 |
cell.disconnect (id) |
| 1515 |
for obj, id in self.rename_entry_sigids: |
| 1516 |
obj.disconnect (id) |
| 1517 |
|
| 1518 |
def printer_name_edit_cancel (self, cell): |
| 1519 |
debugprint ("editing-canceled (%s)" % repr (cell)) |
| 1520 |
cell.stop_editing (True) |
| 1521 |
cell.set_property ('editable', False) |
| 1522 |
for id in self.rename_sigids: |
| 1523 |
cell.disconnect (id) |
| 1524 |
for obj, id in self.rename_entry_sigids: |
| 1525 |
obj.disconnect (id) |
| 1526 |
|
| 1527 |
def rename_printer (self, old_name, new_name): |
| 1528 |
if old_name.lower() == new_name.lower(): |
| 1529 |
return |
| 1530 |
|
| 1531 |
try: |
| 1532 |
self.propertiesDlg.load (old_name, |
| 1533 |
host=self.connect_server, |
| 1534 |
encryption=self.connect_encrypt, |
| 1535 |
parent=self.PrintersWindow) |
| 1536 |
except RuntimeError: |
| 1537 |
# Perhaps cupsGetPPD2 failed for a browsed printer |
| 1538 |
pass |
| 1539 |
except cups.IPPError as e: |
| 1540 |
(e, m) = e.args |
| 1541 |
show_IPP_Error (e, m, self.PrintersWindow) |
| 1542 |
self.populateList () |
| 1543 |
return |
| 1544 |
|
| 1545 |
if not self.is_rename_possible (old_name): |
| 1546 |
return |
| 1547 |
|
| 1548 |
self.cups._begin_operation (_("renaming printer")) |
| 1549 |
rejecting = self.propertiesDlg.printer.rejecting |
| 1550 |
if not rejecting: |
| 1551 |
try: |
| 1552 |
self.propertiesDlg.printer.setAccepting (False) |
| 1553 |
if not self.is_rename_possible (old_name): |
| 1554 |
self.propertiesDlg.printer.setAccepting (True) |
| 1555 |
self.cups._end_operation () |
| 1556 |
return |
| 1557 |
except cups.IPPError as e: |
| 1558 |
(e, msg) = e.args |
| 1559 |
show_IPP_Error (e, msg, self.PrintersWindow) |
| 1560 |
self.cups._end_operation () |
| 1561 |
return |
| 1562 |
|
| 1563 |
if self.duplicate_printer (new_name): |
| 1564 |
# Failure. |
| 1565 |
self.monitor.update () |
| 1566 |
|
| 1567 |
# Restore original accepting/rejecting state. |
| 1568 |
if not rejecting and self.propertiesDlg.printer: |
| 1569 |
try: |
| 1570 |
self.propertiesDlg.printer.name = old_name |
| 1571 |
self.propertiesDlg.printer.setAccepting (True) |
| 1572 |
except cups.HTTPError as e: |
| 1573 |
(s,) = e.args |
| 1574 |
show_HTTP_Error (s, self.PrintersWindow) |
| 1575 |
except cups.IPPError as e: |
| 1576 |
(e, msg) = e.args |
| 1577 |
show_IPP_Error (e, msg, self.PrintersWindow) |
| 1578 |
|
| 1579 |
self.cups._end_operation () |
| 1580 |
self.populateList () |
| 1581 |
return |
| 1582 |
|
| 1583 |
if not self.propertiesDlg.printer: |
| 1584 |
self.cups._end_operation () |
| 1585 |
self.populateList () |
| 1586 |
return |
| 1587 |
|
| 1588 |
# Restore rejecting state. |
| 1589 |
if not rejecting: |
| 1590 |
try: |
| 1591 |
self.propertiesDlg.printer.setAccepting (True) |
| 1592 |
except cups.HTTPError as e: |
| 1593 |
(s,) = e.args |
| 1594 |
show_HTTP_Error (s, self.PrintersWindow) |
| 1595 |
# Not fatal. |
| 1596 |
except cups.IPPError as e: |
| 1597 |
(e, msg) = e.args |
| 1598 |
show_IPP_Error (e, msg, self.PrintersWindow) |
| 1599 |
# Not fatal. |
| 1600 |
|
| 1601 |
# Fix up default printer. |
| 1602 |
if self.default_printer == old_name: |
| 1603 |
reload = False |
| 1604 |
try: |
| 1605 |
reload = self.propertiesDlg.printer.setAsDefault () |
| 1606 |
except cups.HTTPError as e: |
| 1607 |
(s,) = e.args |
| 1608 |
show_HTTP_Error (s, self.PrintersWindow) |
| 1609 |
# Not fatal. |
| 1610 |
except cups.IPPError as e: |
| 1611 |
(e, msg) = e.args |
| 1612 |
show_IPP_Error (e, msg, self.PrintersWindow) |
| 1613 |
# Not fatal. |
| 1614 |
|
| 1615 |
if reload: |
| 1616 |
self.reconnect () |
| 1617 |
|
| 1618 |
# Finally, delete the old printer. |
| 1619 |
try: |
| 1620 |
self.cups.deletePrinter (old_name) |
| 1621 |
except cups.HTTPError as e: |
| 1622 |
(s,) = e.args |
| 1623 |
show_HTTP_Error (s, self.PrintersWindow) |
| 1624 |
# Not fatal |
| 1625 |
except cups.IPPError as e: |
| 1626 |
(e, msg) = e.args |
| 1627 |
show_IPP_Error (e, msg, self.PrintersWindow) |
| 1628 |
# Not fatal. |
| 1629 |
|
| 1630 |
self.cups._end_operation () |
| 1631 |
|
| 1632 |
# ..and select the new printer. |
| 1633 |
def select_new_printer (model, path, iter, UNUSED): |
| 1634 |
name = model.get_value (iter, 2) |
| 1635 |
if name == new_name: |
| 1636 |
self.dests_iconview.select_path (path) |
| 1637 |
self.populateList () |
| 1638 |
model = self.dests_iconview.get_model () |
| 1639 |
model.foreach (select_new_printer, None) |
| 1640 |
|
| 1641 |
# Duplicate |
| 1642 |
|
| 1643 |
def duplicate_printer (self, new_name): |
| 1644 |
self.propertiesDlg.printer.name = new_name |
| 1645 |
self.propertiesDlg.printer.class_members = [] # for classes make sure all members |
| 1646 |
# will get added |
| 1647 |
|
| 1648 |
ret = self.propertiesDlg.save_printer(self.propertiesDlg.printer, |
| 1649 |
saveall=True, |
| 1650 |
parent=self.PrintersWindow) |
| 1651 |
return ret |
| 1652 |
|
| 1653 |
def on_duplicate_activate(self, *UNUSED): |
| 1654 |
iconview = self.dests_iconview |
| 1655 |
paths = iconview.get_selected_items () |
| 1656 |
model = self.dests_iconview.get_model () |
| 1657 |
iter = model.get_iter (paths[0]) |
| 1658 |
name = model.get_value (iter, 2) |
| 1659 |
self.entDuplicateName.set_text(name) |
| 1660 |
self.NewPrinterName.set_transient_for (self.PrintersWindow) |
| 1661 |
result = self.NewPrinterName.run() |
| 1662 |
self.NewPrinterName.hide() |
| 1663 |
|
| 1664 |
if result == Gtk.ResponseType.CANCEL: |
| 1665 |
return |
| 1666 |
|
| 1667 |
try: |
| 1668 |
self.propertiesDlg.load (name, |
| 1669 |
host=self.connect_server, |
| 1670 |
encryption=self.connect_encrypt, |
| 1671 |
parent=self.PrintersWindow) |
| 1672 |
except RuntimeError: |
| 1673 |
# Perhaps cupsGetPPD2 failed for a browsed printer |
| 1674 |
pass |
| 1675 |
except cups.IPPError as e: |
| 1676 |
(e, m) = e.args |
| 1677 |
show_IPP_Error (e, m, self.PrintersWindow) |
| 1678 |
self.populateList () |
| 1679 |
return |
| 1680 |
|
| 1681 |
self.duplicate_printer (self.entDuplicateName.get_text ()) |
| 1682 |
self.monitor.update () |
| 1683 |
|
| 1684 |
def on_entDuplicateName_changed(self, widget): |
| 1685 |
# restrict |
| 1686 |
text = widget.get_text() |
| 1687 |
new_text = text |
| 1688 |
new_text = new_text.replace("/", "") |
| 1689 |
new_text = new_text.replace("#", "") |
| 1690 |
new_text = new_text.replace(" ", "") |
| 1691 |
if text!=new_text: |
| 1692 |
widget.set_text(new_text) |
| 1693 |
self.btnDuplicateOk.set_sensitive( |
| 1694 |
newprinter.checkNPName(self.printers, new_text)) |
| 1695 |
|
| 1696 |
# Delete |
| 1697 |
|
| 1698 |
def on_delete_activate(self, *UNUSED): |
| 1699 |
self.delete_selected_printer_queues () |
| 1700 |
|
| 1701 |
def delete_selected_printer_queues (self): |
| 1702 |
paths = self.dests_iconview.get_selected_items () |
| 1703 |
model = self.dests_iconview.get_model () |
| 1704 |
to_delete = [] |
| 1705 |
n = len (paths) |
| 1706 |
if n == 1: |
| 1707 |
itr = model.get_iter (paths[0]) |
| 1708 |
obj = model.get_value (itr, 0) |
| 1709 |
name = model.get_value (itr, 2) |
| 1710 |
if obj.is_class: |
| 1711 |
message_format = (_("Really delete class '%s'?") % name) |
| 1712 |
else: |
| 1713 |
message_format = (_("Really delete printer '%s'?") % name) |
| 1714 |
|
| 1715 |
to_delete.append (name) |
| 1716 |
else: |
| 1717 |
message_format = _("Really delete selected destinations?") |
| 1718 |
for path in paths: |
| 1719 |
itr = model.get_iter (path) |
| 1720 |
name = model.get_value (itr, 2) |
| 1721 |
to_delete.append (name) |
| 1722 |
dialog = Gtk.MessageDialog(parent=self.PrintersWindow, |
| 1723 |
modal=True, destroy_with_parent=True, |
| 1724 |
message_type=Gtk.MessageType.WARNING, |
| 1725 |
buttons=Gtk.ButtonsType.NONE, |
| 1726 |
text=message_format) |
| 1727 |
dialog.add_buttons (_("_Cancel"), Gtk.ResponseType.REJECT, |
| 1728 |
_("_Delete"), Gtk.ResponseType.ACCEPT) |
| 1729 |
dialog.set_default_response (Gtk.ResponseType.REJECT) |
| 1730 |
result = dialog.run() |
| 1731 |
dialog.destroy() |
| 1732 |
|
| 1733 |
if result != Gtk.ResponseType.ACCEPT: |
| 1734 |
return |
| 1735 |
|
| 1736 |
try: |
| 1737 |
for name in to_delete: |
| 1738 |
self.cups._begin_operation (_("deleting printer %s") % name) |
| 1739 |
self.cups.deletePrinter (name) |
| 1740 |
self.cups._end_operation () |
| 1741 |
except cups.IPPError as e: |
| 1742 |
(e, msg) = e.args |
| 1743 |
self.cups._end_operation () |
| 1744 |
show_IPP_Error(e, msg, self.PrintersWindow) |
| 1745 |
|
| 1746 |
self.monitor.update () |
| 1747 |
|
| 1748 |
# Enable/disable |
| 1749 |
def on_enabled_activate(self, toggle_action): |
| 1750 |
if self.updating_widgets: |
| 1751 |
return |
| 1752 |
enable = toggle_action.get_active () |
| 1753 |
iconview = self.dests_iconview |
| 1754 |
paths = iconview.get_selected_items () |
| 1755 |
model = iconview.get_model () |
| 1756 |
printers = [] |
| 1757 |
for path in paths: |
| 1758 |
itr = model.get_iter (path) |
| 1759 |
printer = model.get_value (itr, 0) |
| 1760 |
printers.append (printer) |
| 1761 |
|
| 1762 |
for printer in printers: |
| 1763 |
self.cups._begin_operation (_("modifying printer %s") % printer.name) |
| 1764 |
try: |
| 1765 |
printer.setEnabled (enable) |
| 1766 |
except cups.IPPError as e: |
| 1767 |
(e, m) = e.args |
| 1768 |
errordialogs.show_IPP_Error (e, m, self.PrintersWindow) |
| 1769 |
# Give up on this operation. |
| 1770 |
self.cups._end_operation () |
| 1771 |
break |
| 1772 |
|
| 1773 |
self.cups._end_operation () |
| 1774 |
|
| 1775 |
self.monitor.update () |
| 1776 |
|
| 1777 |
# Shared |
| 1778 |
def on_shared_activate(self, menuitem): |
| 1779 |
if self.updating_widgets: |
| 1780 |
return |
| 1781 |
share = menuitem.get_active () |
| 1782 |
iconview = self.dests_iconview |
| 1783 |
paths = iconview.get_selected_items () |
| 1784 |
model = iconview.get_model () |
| 1785 |
printers = [] |
| 1786 |
for path in paths: |
| 1787 |
itr = model.get_iter (path) |
| 1788 |
printer = model.get_value (itr, 0) |
| 1789 |
printers.append (printer) |
| 1790 |
|
| 1791 |
success = False |
| 1792 |
for printer in printers: |
| 1793 |
self.cups._begin_operation (_("modifying printer %s") |
| 1794 |
% printer.name) |
| 1795 |
try: |
| 1796 |
printer.setShared (share) |
| 1797 |
success = True |
| 1798 |
except cups.IPPError as e: |
| 1799 |
(e, m) = e.args |
| 1800 |
show_IPP_Error(e, m, self.PrintersWindow) |
| 1801 |
self.cups._end_operation () |
| 1802 |
# Give up on this operation. |
| 1803 |
break |
| 1804 |
|
| 1805 |
self.cups._end_operation () |
| 1806 |
|
| 1807 |
if success and share: |
| 1808 |
if self.server_is_publishing is None: |
| 1809 |
# We haven't yet seen a server-is-sharing-printers attribute. |
| 1810 |
# Assuming CUPS 1.4, this means we haven't opened a |
| 1811 |
# properties dialog yet. Fetch the attributes now and |
| 1812 |
# look for it. |
| 1813 |
try: |
| 1814 |
printer.getAttributes () |
| 1815 |
p = printer.other_attributes['server-is-sharing-printers'] |
| 1816 |
self.server_is_publishing = p |
| 1817 |
except (cups.IPPError, KeyError): |
| 1818 |
pass |
| 1819 |
|
| 1820 |
self.advise_publish () |
| 1821 |
|
| 1822 |
# For some reason CUPS doesn't give us a notification about |
| 1823 |
# printers changing 'shared' state, so refresh instead of |
| 1824 |
# update. We have to defer this to prevent signal problems. |
| 1825 |
self.defer_refresh () |
| 1826 |
|
| 1827 |
def advise_publish(self): |
| 1828 |
if not self.server_is_publishing: |
| 1829 |
show_info_dialog (_("Publish Shared Printers"), |
| 1830 |
_("Shared printers are not available " |
| 1831 |
"to other people unless the " |
| 1832 |
"'Publish shared printers' option is " |
| 1833 |
"enabled in the server settings."), |
| 1834 |
parent=self.PrintersWindow) |
| 1835 |
|
| 1836 |
# Set As Default |
| 1837 |
def on_set_as_default_activate(self, *UNUSED): |
| 1838 |
iconview = self.dests_iconview |
| 1839 |
paths = iconview.get_selected_items () |
| 1840 |
model = iconview.get_model () |
| 1841 |
try: |
| 1842 |
iter = model.get_iter (paths[0]) |
| 1843 |
except IndexError: |
| 1844 |
return |
| 1845 |
|
| 1846 |
name = model.get_value (iter, 2) |
| 1847 |
self.set_system_or_user_default_printer (name) |
| 1848 |
|
| 1849 |
def on_edit_activate (self, *UNUSED): |
| 1850 |
paths = self.dests_iconview.get_selected_items () |
| 1851 |
self.dests_iconview_item_activated (self.dests_iconview, paths[0]) |
| 1852 |
|
| 1853 |
def on_create_class_activate (self, UNUSED): |
| 1854 |
paths = self.dests_iconview.get_selected_items () |
| 1855 |
class_members = [] |
| 1856 |
model = self.dests_iconview.get_model () |
| 1857 |
for path in paths: |
| 1858 |
iter = model.get_iter (path) |
| 1859 |
name = model.get_value (iter, 2) |
| 1860 |
class_members.append (name) |
| 1861 |
if not self.newPrinterGUI.init ("class", |
| 1862 |
host=self.connect_server, |
| 1863 |
encryption=self.connect_encrypt, |
| 1864 |
parent=self.PrintersWindow): |
| 1865 |
self.monitor.update () |
| 1866 |
return |
| 1867 |
|
| 1868 |
out_model = self.newPrinterGUI.tvNCNotMembers.get_model () |
| 1869 |
in_model = self.newPrinterGUI.tvNCMembers.get_model () |
| 1870 |
iter = out_model.get_iter_first () |
| 1871 |
while iter is not None: |
| 1872 |
next = out_model.iter_next (iter) |
| 1873 |
data = out_model.get (iter, 0) |
| 1874 |
if data[0] in class_members: |
| 1875 |
in_model.append (data) |
| 1876 |
out_model.remove (iter) |
| 1877 |
iter = next |
| 1878 |
|
| 1879 |
def on_view_print_queue_activate (self, *UNUSED): |
| 1880 |
paths = self.dests_iconview.get_selected_items () |
| 1881 |
if len (paths): |
| 1882 |
specific_dests = [] |
| 1883 |
model = self.dests_iconview.get_model () |
| 1884 |
for path in paths: |
| 1885 |
iter = model.get_iter (path) |
| 1886 |
name = model.get_value (iter, 2) |
| 1887 |
specific_dests.append (name) |
| 1888 |
viewer = jobviewer.JobViewer (None, None, my_jobs=False, |
| 1889 |
specific_dests=specific_dests, |
| 1890 |
parent=self.PrintersWindow) |
| 1891 |
viewer.connect ('finished', self.on_jobviewer_exit) |
| 1892 |
else: |
| 1893 |
viewer = jobviewer.JobViewer (None, None, my_jobs=False, |
| 1894 |
parent=self.PrintersWindow) |
| 1895 |
viewer.connect ('finished', self.on_jobviewer_exit) |
| 1896 |
|
| 1897 |
self.jobviewers.append (viewer) |
| 1898 |
|
| 1899 |
def on_jobviewer_exit (self, viewer): |
| 1900 |
try: |
| 1901 |
i = self.jobviewers.index (viewer) |
| 1902 |
del self.jobviewers[i] |
| 1903 |
except ValueError: |
| 1904 |
# This shouldn't happen, but does (bug #757520). |
| 1905 |
debugprint ("Jobviewer exited but not in list:\n" |
| 1906 |
"%s\n%s" % (repr (viewer), repr (self.jobviewers))) |
| 1907 |
|
| 1908 |
def on_view_discovered_printers_activate (self, UNUSED): |
| 1909 |
self.populateList () |
| 1910 |
|
| 1911 |
def on_troubleshoot_activate(self, widget): |
| 1912 |
if 'troubleshooter' not in self.__dict__: |
| 1913 |
self.troubleshooter = troubleshoot.run (self.on_troubleshoot_quit) |
| 1914 |
|
| 1915 |
def on_troubleshoot_quit(self, troubleshooter): |
| 1916 |
del self.troubleshooter |
| 1917 |
|
| 1918 |
def sensitise_main_window_widgets (self, sensitive=True): |
| 1919 |
self.dests_iconview.set_sensitive (sensitive) |
| 1920 |
self.btnNew.set_sensitive (sensitive) |
| 1921 |
self.btnAddFirstPrinter.set_sensitive (sensitive) |
| 1922 |
self.refreshbutton.set_sensitive (sensitive) |
| 1923 |
self.view_discovered_printers.set_sensitive (sensitive) |
| 1924 |
self.search_entry.set_sensitive (sensitive) |
| 1925 |
for action in ["/connect-to-server", |
| 1926 |
"/server-settings", |
| 1927 |
"/new-printer", |
| 1928 |
"/new-class", |
| 1929 |
"/rename-printer", |
| 1930 |
"/duplicate-printer", |
| 1931 |
"/delete-printer", |
| 1932 |
"/set-default-printer", |
| 1933 |
"/edit-printer", |
| 1934 |
"/create-class", |
| 1935 |
"/enable-printer", |
| 1936 |
"/share-printer", |
| 1937 |
"/filter-name", |
| 1938 |
"/filter-description", |
| 1939 |
"/filter-location", |
| 1940 |
"/filter-manufacturer"]: |
| 1941 |
self.ui_manager.get_action (action).set_sensitive (sensitive) |
| 1942 |
|
| 1943 |
self.polkit_permission_changed (None, None) |
| 1944 |
|
| 1945 |
def desensitise_main_window_widgets (self): |
| 1946 |
self.sensitise_main_window_widgets (False) |
| 1947 |
|
| 1948 |
# About dialog |
| 1949 |
def on_about_activate(self, widget): |
| 1950 |
self.AboutDialog.set_transient_for (self.PrintersWindow) |
| 1951 |
self.AboutDialog.run() |
| 1952 |
self.AboutDialog.hide() |
| 1953 |
|
| 1954 |
########################################################################## |
| 1955 |
### Server settings |
| 1956 |
########################################################################## |
| 1957 |
|
| 1958 |
### The "Problems?" clickable label |
| 1959 |
def on_problems_button_clicked (self, serversettings): |
| 1960 |
if 'troubleshooter' not in self.__dict__: |
| 1961 |
self.troubleshooter = troubleshoot.run (self.on_troubleshoot_quit, |
| 1962 |
parent=serversettings.get_dialog ()) |
| 1963 |
|
| 1964 |
# ==================================================================== |
| 1965 |
# == New Printer Dialog ============================================== |
| 1966 |
# ==================================================================== |
| 1967 |
|
| 1968 |
def sensitise_new_printer_widgets(self, sensitive=True): |
| 1969 |
self.btnNew.set_sensitive (sensitive) |
| 1970 |
self.btnAddFirstPrinter.set_sensitive (sensitive) |
| 1971 |
self.ui_manager.get_action ("/new-printer").set_sensitive (sensitive) |
| 1972 |
self.ui_manager.get_action ("/new-class").set_sensitive (sensitive) |
| 1973 |
self.polkit_permission_changed (None, None) |
| 1974 |
|
| 1975 |
def desensitise_new_printer_widgets(self): |
| 1976 |
self.sensitise_new_printer_widgets (False) |
| 1977 |
|
| 1978 |
# new printer |
| 1979 |
def on_new_printer_activate(self, widget, *UNUSED): |
| 1980 |
busy (self.PrintersWindow) |
| 1981 |
self.desensitise_new_printer_widgets () |
| 1982 |
if not self.newPrinterGUI.init("printer", |
| 1983 |
host=self.connect_server, |
| 1984 |
encryption=self.connect_encrypt, |
| 1985 |
parent=self.PrintersWindow): |
| 1986 |
self.sensitise_new_printer_widgets () |
| 1987 |
self.monitor.update () |
| 1988 |
ready (self.PrintersWindow) |
| 1989 |
|
| 1990 |
# new class |
| 1991 |
def on_new_class_activate(self, widget, *UNUSED): |
| 1992 |
self.desensitise_new_printer_widgets () |
| 1993 |
if not self.newPrinterGUI.init("class", |
| 1994 |
host=self.connect_server, |
| 1995 |
encryption=self.connect_encrypt, |
| 1996 |
parent=self.PrintersWindow): |
| 1997 |
self.sensitise_new_printer_widgets () |
| 1998 |
self.monitor.update () |
| 1999 |
|
| 2000 |
def on_new_printer_not_added (self, obj): |
| 2001 |
self.sensitise_new_printer_widgets () |
| 2002 |
|
| 2003 |
def on_new_printer_added (self, obj, name): |
| 2004 |
debugprint ("New printer added: %s" % name) |
| 2005 |
|
| 2006 |
self.sensitise_new_printer_widgets () |
| 2007 |
self.populateList () |
| 2008 |
|
| 2009 |
if name not in self.printers: |
| 2010 |
# At this stage the printer has disappeared even though we |
| 2011 |
# only added it moments ago. |
| 2012 |
debugprint ("New printer disappeared") |
| 2013 |
return |
| 2014 |
|
| 2015 |
# Now select it. |
| 2016 |
model = self.dests_iconview.get_model () |
| 2017 |
iter = model.get_iter_first () |
| 2018 |
while iter is not None: |
| 2019 |
queue = model.get_value (iter, 2) |
| 2020 |
if queue == name: |
| 2021 |
path = model.get_path (iter) |
| 2022 |
self.dests_iconview.scroll_to_path (path, True, 0.5, 0.5) |
| 2023 |
self.dests_iconview.unselect_all () |
| 2024 |
self.dests_iconview.set_cursor (path=path, cell=None, |
| 2025 |
start_editing=False) |
| 2026 |
self.dests_iconview.select_path (path) |
| 2027 |
break |
| 2028 |
|
| 2029 |
iter = model.iter_next (iter) |
| 2030 |
|
| 2031 |
# Any missing drivers? |
| 2032 |
self.propertiesDlg.load (name) |
| 2033 |
if (self.propertiesDlg.ppd and |
| 2034 |
not (self.propertiesDlg.printer.discovered or |
| 2035 |
self.propertiesDlg.printer.remote)): |
| 2036 |
try: |
| 2037 |
self.checkDriverExists (self.PrintersWindow, name, |
| 2038 |
ppd=self.propertiesDlg.ppd) |
| 2039 |
except: |
| 2040 |
nonfatalException() |
| 2041 |
|
| 2042 |
# Finally, suggest printing a test page. |
| 2043 |
if self.propertiesDlg.ppd: |
| 2044 |
q = Gtk.MessageDialog (parent=self.PrintersWindow, |
| 2045 |
modal=True, destroy_with_parent=True, |
| 2046 |
message_type=Gtk.MessageType.QUESTION, |
| 2047 |
buttons=Gtk.ButtonsType.NONE, |
| 2048 |
text=_("Would you like to print a test page?")) |
| 2049 |
q.add_buttons (Gtk.STOCK_CANCEL, Gtk.ResponseType.NO, |
| 2050 |
_("Print Test Page"), Gtk.ResponseType.YES) |
| 2051 |
response = q.run () |
| 2052 |
q.destroy () |
| 2053 |
if response == Gtk.ResponseType.YES: |
| 2054 |
self.propertiesDlg.dialog.hide () |
| 2055 |
|
| 2056 |
properties_shown = False |
| 2057 |
try: |
| 2058 |
# Load the printer details but hide the properties dialog. |
| 2059 |
self.display_properties_dialog_for (name) |
| 2060 |
properties_shown = True |
| 2061 |
except RuntimeError: |
| 2062 |
pass |
| 2063 |
|
| 2064 |
if properties_shown: |
| 2065 |
# Click the test button. |
| 2066 |
self.propertiesDlg.btnPrintTestPage.clicked () |
| 2067 |
|
| 2068 |
## Service start-up |
| 2069 |
def on_start_service_clicked (self, button): |
| 2070 |
button.set_sensitive (False) |
| 2071 |
self.servicestart.start (reply_handler=self.on_start_service_reply, |
| 2072 |
error_handler=self.on_start_service_reply) |
| 2073 |
|
| 2074 |
def on_start_service_reply (self, *args): |
| 2075 |
GLib.timeout_add_seconds (1, self.service_started_try) |
| 2076 |
|
| 2077 |
def service_started_try (self): |
| 2078 |
Gdk.threads_enter () |
| 2079 |
try: |
| 2080 |
self.on_btnRefresh_clicked (None) |
| 2081 |
finally: |
| 2082 |
Gdk.threads_leave () |
| 2083 |
|
| 2084 |
GLib.timeout_add_seconds (1, self.service_started_retry) |
| 2085 |
return False |
| 2086 |
|
| 2087 |
def service_started_retry (self): |
| 2088 |
if not self.cups: |
| 2089 |
Gdk.threads_enter () |
| 2090 |
try: |
| 2091 |
self.on_btnRefresh_clicked (None) |
| 2092 |
self.btnStartService.set_sensitive (True) |
| 2093 |
finally: |
| 2094 |
Gdk.threads_leave () |
| 2095 |
|
| 2096 |
return False |
| 2097 |
|
| 2098 |
def checkDriverExists(self, parent, name, ppd=None): |
| 2099 |
"""Check that the driver for an existing queue actually |
| 2100 |
exists, and prompt to install the appropriate package |
| 2101 |
if not. |
| 2102 |
|
| 2103 |
ppd: cups.PPD object, if already created""" |
| 2104 |
|
| 2105 |
# Is this queue on the local machine? If not, we can't check |
| 2106 |
# anything at all. |
| 2107 |
server = cups.getServer () |
| 2108 |
if not (self.connect_server == 'localhost' or |
| 2109 |
self.connect_server[0] == '/'): |
| 2110 |
return |
| 2111 |
|
| 2112 |
# Fetch the PPD if we haven't already. |
| 2113 |
if not ppd: |
| 2114 |
try: |
| 2115 |
filename = self.cups.getPPD(name) |
| 2116 |
except cups.IPPError as e: |
| 2117 |
(e, msg) = e.args |
| 2118 |
if e == cups.IPP_NOT_FOUND: |
| 2119 |
# This is a raw queue. Nothing to check. |
| 2120 |
return |
| 2121 |
else: |
| 2122 |
self.show_IPP_Error(e, msg) |
| 2123 |
return |
| 2124 |
|
| 2125 |
ppd = cups.PPD(filename) |
| 2126 |
os.unlink(filename) |
| 2127 |
|
| 2128 |
(pkgs, exes) = cupshelpers.missingPackagesAndExecutables (ppd) |
| 2129 |
if len (pkgs) > 0 or len (exes) > 0: |
| 2130 |
# We didn't find a necessary executable. Complain. |
| 2131 |
can_install = False |
| 2132 |
if len (pkgs) > 0: |
| 2133 |
try: |
| 2134 |
pk = installpackage.PackageKit () |
| 2135 |
can_install = True |
| 2136 |
except: |
| 2137 |
pass |
| 2138 |
|
| 2139 |
if can_install and len (pkgs) > 0: |
| 2140 |
pkg = pkgs[0] |
| 2141 |
install_text = ('<span weight="bold" size="larger">' + |
| 2142 |
_('Install driver') + '</span>\n\n' + |
| 2143 |
_("Printer '%s' requires the %s package but " |
| 2144 |
"it is not currently installed.") |
| 2145 |
% (name, pkg)) |
| 2146 |
dialog = self.InstallDialog |
| 2147 |
self.lblInstall.set_markup(install_text) |
| 2148 |
dialog.set_transient_for (parent) |
| 2149 |
response = dialog.run () |
| 2150 |
dialog.hide () |
| 2151 |
if response == Gtk.ResponseType.OK: |
| 2152 |
# Install the package. |
| 2153 |
try: |
| 2154 |
pk.InstallPackageName (0, 0, pkg) |
| 2155 |
except: |
| 2156 |
pass # should handle error |
| 2157 |
else: |
| 2158 |
show_error_dialog (_('Missing driver'), |
| 2159 |
_("Printer '%s' requires the '%s' program " |
| 2160 |
"but it is not currently installed. " |
| 2161 |
"Please install it before using this " |
| 2162 |
"printer.") % |
| 2163 |
(name, |
| 2164 |
(exes + pkgs)[0]), |
| 2165 |
parent) |
| 2166 |
|
| 2167 |
def on_printer_modified (self, obj, name, ppd_has_changed): |
| 2168 |
debugprint ("Printer modified by user: %s" % name) |
| 2169 |
# Load information about the printer, |
| 2170 |
# e.g. self.propertiesDlg.server_side_options and self.propertiesDlg.ppd |
| 2171 |
# (both used below). |
| 2172 |
self.propertiesDlg.load (name) |
| 2173 |
|
| 2174 |
if self.propertiesDlg.ppd: |
| 2175 |
try: |
| 2176 |
self.checkDriverExists (self.propertiesDlg.dialog, |
| 2177 |
name, ppd=self.propertiesDlg.ppd) |
| 2178 |
except: |
| 2179 |
nonfatalException() |
| 2180 |
|
| 2181 |
# Also check to see whether the media option has become |
| 2182 |
# invalid. This can happen if it had previously been |
| 2183 |
# explicitly set to a page size that is not offered with |
| 2184 |
# the new PPD (see bug #441836). |
| 2185 |
try: |
| 2186 |
option = self.propertiesDlg.server_side_options['media'] |
| 2187 |
if option.get_current_value () is None: |
| 2188 |
debugprint ("Invalid media option: resetting") |
| 2189 |
option.reset () |
| 2190 |
self.propertiesDlg.changed.add (option) |
| 2191 |
self.propertiesDlg.save_printer (self.printer) |
| 2192 |
except KeyError: |
| 2193 |
pass |
| 2194 |
except: |
| 2195 |
nonfatalException() |
| 2196 |
|
| 2197 |
def defer_refresh (self): |
| 2198 |
def deferred_refresh (): |
| 2199 |
self.populateList_timer = None |
| 2200 |
Gdk.threads_enter () |
| 2201 |
try: |
| 2202 |
self.populateList (prompt_allowed=False) |
| 2203 |
finally: |
| 2204 |
Gdk.threads_leave () |
| 2205 |
return False |
| 2206 |
|
| 2207 |
if self.populateList_timer: |
| 2208 |
GLib.source_remove (self.populateList_timer) |
| 2209 |
|
| 2210 |
self.populateList_timer = GLib.timeout_add (200, deferred_refresh) |
| 2211 |
debugprint ("Deferred populateList by 200ms") |
| 2212 |
|
| 2213 |
## Monitor signal helpers |
| 2214 |
def printer_added_or_removed (self): |
| 2215 |
# Just fetch the list of printers again. This is too simplistic. |
| 2216 |
self.defer_refresh () |
| 2217 |
|
| 2218 |
## Monitor signal handlers |
| 2219 |
def printer_added (self, mon, printer): |
| 2220 |
self.printer_added_or_removed () |
| 2221 |
|
| 2222 |
def printer_event (self, mon, printer, eventname, event): |
| 2223 |
if printer in self.printers: |
| 2224 |
self.printers[printer].update (**event) |
| 2225 |
self.dests_iconview_selection_changed (self.dests_iconview) |
| 2226 |
self.printer_added_or_removed () |
| 2227 |
|
| 2228 |
def printer_removed (self, mon, printer): |
| 2229 |
self.printer_added_or_removed () |
| 2230 |
|
| 2231 |
def cups_connection_error (self, mon): |
| 2232 |
self.cups = None |
| 2233 |
self.setConnected () |
| 2234 |
self.populateList (prompt_allowed=False) |
| 2235 |
|
| 2236 |
def cups_connection_recovered (self, mon): |
| 2237 |
debugprint ("Trying to recover connection") |
| 2238 |
GLib.idle_add (self.service_started_try) |
| 2239 |
|
| 2240 |
def main(show_jobs): |
| 2241 |
cups.setUser (os.environ.get ("CUPS_USER", cups.getUser())) |
| 2242 |
Gdk.threads_init () |
| 2243 |
from dbus.glib import DBusGMainLoop |
| 2244 |
DBusGMainLoop (set_as_default=True) |
| 2245 |
|
| 2246 |
if show_jobs: |
| 2247 |
viewer = jobviewer.JobViewer (None, None, my_jobs=False, |
| 2248 |
specific_dests=[show_jobs]) |
| 2249 |
viewer.connect ('finished', Gtk.main_quit) |
| 2250 |
else: |
| 2251 |
mainwindow = GUI() |
| 2252 |
|
| 2253 |
Gdk.threads_enter () |
| 2254 |
try: |
| 2255 |
Gtk.main() |
| 2256 |
finally: |
| 2257 |
Gdk.threads_leave () |
| 2258 |
|
| 2259 |
if __name__ == "__main__": |
| 2260 |
import getopt |
| 2261 |
try: |
| 2262 |
opts, args = getopt.gnu_getopt (sys.argv[1:], '', |
| 2263 |
['embedded=', |
| 2264 |
'debug', 'show-jobs=']) |
| 2265 |
except getopt.GetoptError: |
| 2266 |
show_help () |
| 2267 |
sys.exit (1) |
| 2268 |
|
| 2269 |
show_jobs = False |
| 2270 |
|
| 2271 |
for opt, optarg in opts: |
| 2272 |
if opt == '--debug': |
| 2273 |
set_debugging (True) |
| 2274 |
cupshelpers.set_debugprint_fn (debugprint) |
| 2275 |
elif opt == '--show-jobs': |
| 2276 |
show_jobs = optarg |
| 2277 |
|
| 2278 |
if opt == "--embedded": |
| 2279 |
PlugWindowId = int(optarg) |
| 2280 |
|
| 2281 |
main(show_jobs) |