~ubuntu-branches/ubuntu/saucy/hplip/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
#
# Thanks to Henrique M. Holschuh <hmh@debian.org> for various security patches
#

__version__ = '15.0'
__mod__ = 'hp-toolbox'
__title__ = 'HP Device Manager'
__doc__ = """The HP Device Manager (aka "Toolbox") for HPLIP supported devices. Provides access to status, tools, and supplies levels."""

# Std Lib
import sys
import os
import getopt
import signal

# Local
from base.g import *
import base.utils as utils
from base import status, tui, module


w = None # write pipe
app = None
toolbox  = None
session_bus = None


def handle_session_signal(*args, **kwds):
    if kwds['interface'] == 'com.hplip.Toolbox' and \
        kwds['member'] == 'Event':

        event = device.Event(*args)
        event.debug()

        if event.event_code > EVENT_MAX_EVENT:
            event.event_code = status.MapPJLErrorCode(event.event_code)

        # regular user/device status event
        log.debug("Received event notifier: %d" % event.event_code)

        if not event.send_via_pipe(w, 'toolbox ui'):
            sys.exit(1)
            # if this fails, then hp-toolbox must be killed.
            # No need to continue running...


mod = module.Module(__mod__, __title__, __version__, __doc__, None,
                    (GUI_MODE,), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
mod.lockInstance()

mod.setUsage(module.USAGE_FLAG_NONE,
             extra_options=[("Disable dbus (Qt3 only):", "-x or --disable-dbus", "option", False)],
             see_also_list = ['hp-align', 'hp-clean', 'hp-colorcal', 'hp-devicesettings',
                              'hp-faxsetup', 'hp-firmware', 'hp-info', 'hp-levels',
                              'hp-linefeedcal', 'hp-makecopies', 'hp-plugin',
                              'hp-pqdiag', 'hp-print', 'hp-printsettings', 'hp-scan',
                              'hp-sendfax', 'hp-testpage', 'hp-timedate', 'hp-unload'])

opts, device_uri, printer_name, mode, ui_toolkit, loc = \
    mod.parseStdOpts('x', ['disable-dbus'])

disable_dbus = False

for o, a in opts:
    if o in ('-x', '--disable-dbus') and ui_toolkit == 'qt3':
        disable_dbus = True

if ui_toolkit == 'qt3':
    if not utils.canEnterGUIMode():
        log.error("%s requires GUI support. Exiting." % __mod__)
        sys.exit(1)
elif ui_toolkit == 'qt4':
    if not utils.canEnterGUIMode4():
        log.error("%s requires GUI support. Exiting." % __mod__)
        sys.exit(1)

child_pid, w, r = 0, 0, 0

if ui_toolkit == 'qt3':
    try:
        from dbus import SessionBus
        import dbus.service
        from dbus.mainloop.glib import DBusGMainLoop, threads_init
        from gobject import MainLoop
        import glib
        glib.threads_init()
        dbus.mainloop.glib.threads_init()
    except ImportError:
        log.error("Unable to load dbus - Automatic status updates in HPLIP Device Manager will be disabled.")
        disable_dbus = True

    if not disable_dbus:
        r, w = os.pipe()
        parent_pid = os.getpid()
        log.debug("Parent PID=%d" % parent_pid)
        child_pid = os.fork()

    if disable_dbus or child_pid: # qt3/ui
        # parent (UI)
        log.set_module("hp-toolbox(UI)")

        if w:
            os.close(w)

        try:
            from qt import *
            from ui.devmgr4 import DevMgr4
        except ImportError:
            log.error("Unable to load Qt3 support. Is it installed?")
            sys.exit(1)

        # create the main application object
        app = QApplication(sys.argv)

        if loc is None:
            loc = user_conf.get('ui', 'loc', 'system')
            if loc.lower() == 'system':
                loc = str(QTextCodec.locale())
                log.debug("Using system locale: %s" % loc)

        if loc.lower() != 'c':
            e = 'utf8'
            try:
                l, x = loc.split('.')
                loc = '.'.join([l, e])
            except ValueError:
                l = loc
                loc = '.'.join([loc, e])

            log.debug("Trying to load .qm file for %s locale." % loc)
            trans = QTranslator(None)

            qm_file = 'hplip_%s.qm' % l
            log.debug("Name of .qm file: %s" % qm_file)
            loaded = trans.load(qm_file, prop.localization_dir)

            if loaded:
                app.installTranslator(trans)
            else:
                loc = 'c'

        if loc == 'c':
            log.debug("Using default 'C' locale")
        else:
            log.debug("Using locale: %s" % loc)
            QLocale.setDefault(QLocale(loc))
            prop.locale = loc
            try:
                locale.setlocale(locale.LC_ALL, locale.normalize(loc))
            except locale.Error:
                pass

        toolbox = DevMgr4(r, __version__, device_uri, disable_dbus)
        app.setMainWidget(toolbox)

        toolbox.show()

        try:
            try:
                log.debug("Starting GUI loop...")
                app.exec_loop()
            except KeyboardInterrupt:
                sys.exit(0)

        finally:
            if child_pid:
                log.debug("Killing child toolbox process (pid=%d)..." % child_pid)
                try:
                    os.kill(child_pid, signal.SIGKILL)
                except OSError, e:
                    log.debug("Failed: %s" % e.message)

            mod.unlockInstance()
            sys.exit(0)

    elif not disable_dbus: # qt3/dbus
        # dBus
        log.set_module("hp-toolbox(dbus)")
        from base import device

        try:
            # child (dbus connector)
            os.close(r)

            dbus_loop = DBusGMainLoop(set_as_default=True)

            try:
                session_bus = dbus.SessionBus()
            except dbus.exceptions.DBusException, e:
                if os.getuid() != 0:
                    log.error("Unable to connect to dbus session bus. Exiting.")
                    sys.exit(1)
                else:
                    log.error("Unable to connect to dbus session bus (running as root?)")
                    sys.exit(1)

            # Receive events from the session bus
            session_bus.add_signal_receiver(handle_session_signal, sender_keyword='sender',
                destination_keyword='dest', interface_keyword='interface',
                member_keyword='member', path_keyword='path')

            log.debug("Entering main loop...")

            try:
                MainLoop().run()
            except KeyboardInterrupt:
                log.debug("Ctrl-C: Exiting...")

        finally:
            if parent_pid:
                log.debug("Killing parent toolbox process (pid=%d)..." % parent_pid)
                try:
                    os.kill(parent_pid, signal.SIGKILL)
                except OSError, e:
                    log.debug("Failed: %s" % e.message)

            mod.unlockInstance()

        sys.exit(0)

else: # qt4
    try:
        from PyQt4.QtGui import QApplication

    except ImportError:
        log.error("Unable to load Qt4 support. Is it installed?")
        sys.exit(1)

    from ui4.devmgr5 import DevMgr5

    log.set_module("hp-toolbox(UI)")

    if 1:
    #try:
        app = QApplication(sys.argv)

        toolbox = DevMgr5(__version__, device_uri,  None)
        toolbox.show()
        try:
            log.debug("Starting GUI loop...")
            app.exec_()
        except KeyboardInterrupt:
            sys.exit(0)

    if 1:
    #finally:
        mod.unlockInstance()
        sys.exit(0)