Replace 'tap' to 'spaces' to make gbs build succeed
[platform/upstream/hplip.git] / systray.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
5 #
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.
10 #
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.
15 #
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
19 #
20 # Author: Don Welch
21
22 __version__ = '2.0'
23 __mod__ = 'hp-systray'
24 __title__ = 'System Tray Status Service'
25 __doc__ = ""
26
27 # StdLib
28 import sys
29 import os
30 import getopt
31 import signal
32
33 # Local
34 from base.g import *
35 from base import utils, module
36 from prnt import cups
37
38
39
40 if __name__ == '__main__':
41
42     # Create a new session ID for the tray.  This disassociates the
43     # tray from the controlling terminal so that it cannot receive
44     # keyboard interrupts.
45     #
46     # Only do this if we aren't already a session leader.  This test
47     # only succeeds if we are executed from hp-toolbox.
48     if os.getpgid(os.getpid()) != os.getpid():
49         os.setsid()
50
51     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
52                        (GUI_MODE,), (UI_TOOLKIT_QT4, UI_TOOLKIT_QT3))
53
54     mod.setUsage(module.USAGE_FLAG_NONE,
55         extra_options=[("Startup even if no hplip CUPS queues are present:", "-x or --force-startup", "option", False)])
56
57     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
58         mod.parseStdOpts('x', ['force-startup'], False)
59
60     force_startup = False
61     for o, a in opts:
62         if o in ('-x', '--force-startup'):
63             force_startup = True
64
65     if os.getuid() == 0:
66         log.error("hp-systray cannot be run as root. Exiting.")
67         sys.exit(1)
68
69     if ui_toolkit == 'qt3':
70         if not utils.canEnterGUIMode():
71             log.error("%s requires Qt3 GUI and DBus support. Exiting." % __mod__)
72             sys.exit(1)
73     
74     else:
75         if not utils.canEnterGUIMode4():
76             log.error("%s requires Qt4 GUI and DBus support. Exiting." % __mod__)
77             sys.exit(1)
78
79     if not force_startup:
80         # Check for any hp: or hpfax: queues. If none, exit
81         if not utils.any([p.device_uri for p in cups.getPrinters()], lambda x : x.startswith('hp')):
82             log.warn("No hp: or hpfax: devices found in any installed CUPS queue. Exiting.")
83             sys.exit(1)
84
85     mod.lockInstance()
86     
87     r1, w1 = os.pipe()
88     log.debug("Creating pipe: hpssd (%d) ==> systemtray (%d)" % (w1, r1))
89     
90     parent_pid = os.getpid()
91     child_pid1 = os.fork()
92     
93     if child_pid1:
94         # parent (UI)
95         os.close(w1)
96
97         if ui_toolkit == 'qt3':
98             try:
99                 import ui.systemtray as systray
100             except ImportError:
101                 log.error("Unable to load Qt3 support. Is it installed?")
102                 sys.exit(1)                  
103         
104         else: # qt4
105             try:
106                 import ui4.systemtray as systray
107             except ImportError:
108                 log.error("Unable to load Qt4 support. Is it installed?")
109                 mod.unlockInstance()
110                 sys.exit(1)        
111
112         try:
113             systray.run(r1)
114         finally:
115             mod.unlockInstance()
116
117     else:
118         # child (dbus & device i/o [qt4] or dbus [qt3])
119         os.close(r1)
120         
121         if ui_toolkit == 'qt4':
122             r2, w2 = os.pipe()
123             r3, w3 = os.pipe()
124             
125             log.debug("Creating pipe: hpssd (%d) ==> hpdio (%d)" % (w2, r2))
126             log.debug("Creating pipe: hpdio (%d) ==> hpssd (%d)" % (w3, r3))
127             
128             child_pid2 = os.fork()
129             if child_pid2:
130                 # parent (dbus)
131                 os.close(r2)
132                 
133                 import hpssd
134                 hpssd.run(w1, w2, r3)
135                         
136             else:
137                 # child (device i/o)
138                 os.close(w2)
139                 
140                 import hpdio
141                 hpdio.run(r2, w3) 
142                 
143         else: # qt3
144             import hpssd
145             hpssd.run(w1)