2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # Thanks to Henrique M. Holschuh <hmh@debian.org> for various security patches
26 __mod__ = 'hp-toolbox'
27 __title__ = 'HP Device Manager'
28 __doc__ = """The HP Device Manager (aka "Toolbox") for HPLIP supported devices. Provides access to status, tools, and supplies levels."""
38 import base.utils as utils
39 from base import status, tui, module
48 def handle_session_signal(*args, **kwds):
49 if kwds['interface'] == 'com.hplip.Toolbox' and \
50 kwds['member'] == 'Event':
52 event = device.Event(*args)
55 if event.event_code > EVENT_MAX_EVENT:
56 event.event_code = status.MapPJLErrorCode(event.event_code)
58 # regular user/device status event
59 log.debug("Received event notifier: %d" % event.event_code)
61 if not event.send_via_pipe(w, 'toolbox ui'):
63 # if this fails, then hp-toolbox must be killed.
64 # No need to continue running...
67 mod = module.Module(__mod__, __title__, __version__, __doc__, None,
68 (GUI_MODE,), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
71 mod.setUsage(module.USAGE_FLAG_NONE,
72 extra_options=[("Disable dbus (Qt3 only):", "-x or --disable-dbus", "option", False)],
73 see_also_list = ['hp-align', 'hp-clean', 'hp-colorcal', 'hp-devicesettings',
74 'hp-faxsetup', 'hp-firmware', 'hp-info', 'hp-levels',
75 'hp-linefeedcal', 'hp-makecopies', 'hp-plugin',
76 'hp-pqdiag', 'hp-print', 'hp-printsettings', 'hp-scan',
77 'hp-sendfax', 'hp-testpage', 'hp-timedate', 'hp-unload'])
79 opts, device_uri, printer_name, mode, ui_toolkit, loc = \
80 mod.parseStdOpts('x', ['disable-dbus'])
85 if o in ('-x', '--disable-dbus') and ui_toolkit == 'qt3':
88 if ui_toolkit == 'qt3':
89 if not utils.canEnterGUIMode():
90 log.error("%s requires GUI support. Exiting." % __mod__)
92 elif ui_toolkit == 'qt4':
93 if not utils.canEnterGUIMode4():
94 log.error("%s requires GUI support. Exiting." % __mod__)
97 child_pid, w, r = 0, 0, 0
99 if ui_toolkit == 'qt3':
101 from dbus import SessionBus
103 from dbus.mainloop.glib import DBusGMainLoop, threads_init
104 from gobject import MainLoop
107 dbus.mainloop.glib.threads_init()
109 log.error("Unable to load dbus - Automatic status updates in HPLIP Device Manager will be disabled.")
114 parent_pid = os.getpid()
115 log.debug("Parent PID=%d" % parent_pid)
116 child_pid = os.fork()
118 if disable_dbus or child_pid: # qt3/ui
120 log.set_module("hp-toolbox(UI)")
127 from ui.devmgr4 import DevMgr4
129 log.error("Unable to load Qt3 support. Is it installed?")
132 # create the main application object
133 app = QApplication(sys.argv)
136 loc = user_conf.get('ui', 'loc', 'system')
137 if loc.lower() == 'system':
138 loc = str(QTextCodec.locale())
139 log.debug("Using system locale: %s" % loc)
141 if loc.lower() != 'c':
144 l, x = loc.split('.')
145 loc = '.'.join([l, e])
148 loc = '.'.join([loc, e])
150 log.debug("Trying to load .qm file for %s locale." % loc)
151 trans = QTranslator(None)
153 qm_file = 'hplip_%s.qm' % l
154 log.debug("Name of .qm file: %s" % qm_file)
155 loaded = trans.load(qm_file, prop.localization_dir)
158 app.installTranslator(trans)
163 log.debug("Using default 'C' locale")
165 log.debug("Using locale: %s" % loc)
166 QLocale.setDefault(QLocale(loc))
169 locale.setlocale(locale.LC_ALL, locale.normalize(loc))
173 toolbox = DevMgr4(r, __version__, device_uri, disable_dbus)
174 app.setMainWidget(toolbox)
180 log.debug("Starting GUI loop...")
182 except KeyboardInterrupt:
187 log.debug("Killing child toolbox process (pid=%d)..." % child_pid)
189 os.kill(child_pid, signal.SIGKILL)
191 log.debug("Failed: %s" % e.message)
196 elif not disable_dbus: # qt3/dbus
198 log.set_module("hp-toolbox(dbus)")
199 from base import device
202 # child (dbus connector)
205 dbus_loop = DBusGMainLoop(set_as_default=True)
208 session_bus = dbus.SessionBus()
209 except dbus.exceptions.DBusException, e:
211 log.error("Unable to connect to dbus session bus. Exiting.")
214 log.error("Unable to connect to dbus session bus (running as root?)")
217 # Receive events from the session bus
218 session_bus.add_signal_receiver(handle_session_signal, sender_keyword='sender',
219 destination_keyword='dest', interface_keyword='interface',
220 member_keyword='member', path_keyword='path')
222 log.debug("Entering main loop...")
226 except KeyboardInterrupt:
227 log.debug("Ctrl-C: Exiting...")
231 log.debug("Killing parent toolbox process (pid=%d)..." % parent_pid)
233 os.kill(parent_pid, signal.SIGKILL)
235 log.debug("Failed: %s" % e.message)
243 from PyQt4.QtGui import QApplication
246 log.error("Unable to load Qt4 support. Is it installed?")
249 from ui4.devmgr5 import DevMgr5
251 log.set_module("hp-toolbox(UI)")
255 app = QApplication(sys.argv)
257 toolbox = DevMgr5(__version__, device_uri, None)
260 log.debug("Starting GUI loop...")
262 except KeyboardInterrupt: