Fixed build error by changing buildrequires from pkgconfig(turbo-jpeg) to libjpeg...
[platform/upstream/hplip.git] / clean.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2008 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
23 __version__ = '4.0'
24 __title__ = 'Printer Cartridge Cleaning Utility'
25 __mod__ = 'hp-clean'
26 __doc__ = "Cartridge cleaning utility for HPLIP supported inkjet printers."
27
28 #Std Lib
29 import sys
30 import re
31 import getopt
32 import time
33 import operator
34 import os
35
36 # Local
37 from base.g import *
38 from base import device, utils, maint, tui, module
39 from prnt import cups
40
41
42 def CleanUIx(level):
43     global d
44     ok = tui.continue_prompt("Ready to perform level %d cleaning (Note: Wait for previous print to finish)." % level)
45
46     if ok:
47         timeout = 0
48         time.sleep(5)
49
50         try:
51             while True:
52                 update_spinner()
53                 try:
54                     d.open()
55                 except Error:
56                     time.sleep(2)
57                     timeout += 2
58                     continue
59
60                 if d.isIdleAndNoError():
61                     break
62
63                 time.sleep(1)
64                 timeout += 1
65
66                 if timeout > 45:
67                     log.error("Timeout waiting for print to finish.")
68                     sys.exit(0)
69
70
71         finally:
72             cleanup_spinner()
73             d.close()
74
75     return ok
76
77 def CleanUI1():
78     log.note("Please wait for page to complete printing before continuing.")
79     log.info("\nLevel 1 cleaning complete. If the printout looks OK, enter 'q' to quit or <enter> to do a level 2 cleaning.")
80     return CleanUIx(2)
81
82
83 def CleanUI2():
84     log.note("Please wait for page to complete printing before continuing.")
85     log.info("\nLevel 2 cleaning complete. If the printout looks OK, enter 'q' to quit or <enter> to do a level 3 cleaning.")
86     log.warn("Level 3 uses a lot of ink.")
87     return CleanUIx(3)
88
89 def CleanUI3():
90     log.info("\nLevel 3 cleaning complete. Check this page to see if the problem was fixed. If the test page was not printed OK, replace the print cartridge(s).")
91
92
93 try:
94     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
95                         (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT4,))
96
97     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
98                  see_also_list=['hp-align', 'hp-clean', 'hp-linefeedcal',
99                                 'hp-pqdiag'])
100
101     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
102         mod.parseStdOpts()
103
104     device_uri = mod.getDeviceUri(device_uri, printer_name,
105        filter={'clean-type': (operator.ne, CLEAN_TYPE_NONE)})
106
107     if mode == GUI_MODE:
108         if not utils.canEnterGUIMode4():
109             log.error("%s -u/--gui requires Qt4 GUI support. Entering interactive mode." % __mod__)
110             mode = INTERACTIVE_MODE
111
112     if mode == INTERACTIVE_MODE:
113         try:
114             d = device.Device(device_uri, printer_name)
115         except Error, e:
116             log.error("Unable to open device: %s" % e.msg)
117             sys.exit(0)
118
119         try:
120             try:
121                 d.open()
122             except Error:
123                 log.error("Unable to print to printer. Please check device and try again.")
124                 sys.exit(1)
125
126             if d.isIdleAndNoError():
127                 clean_type = d.mq.get('clean-type', CLEAN_TYPE_NONE)
128                 log.debug("Clean type=%d" % clean_type)
129                 d.close()
130
131                 try:
132                     if clean_type == CLEAN_TYPE_UNSUPPORTED:
133                         log.error("Cleaning through HPLIP not supported for this printer. Please use the printer's front panel to perform cartridge cleaning.")
134
135                     elif clean_type == CLEAN_TYPE_PCL:
136                         maint.cleaning(d, clean_type, maint.cleanType1, maint.primeType1,
137                                         maint.wipeAndSpitType1, tui.load_paper_prompt,
138                                         CleanUI1, CleanUI2, CleanUI3,
139                                         None)
140
141                     elif clean_type == CLEAN_TYPE_LIDIL:
142                         maint.cleaning(d, clean_type, maint.cleanType2, maint.primeType2,
143                                         maint.wipeAndSpitType2, tui.load_paper_prompt,
144                                         CleanUI1, CleanUI2, CleanUI3,
145                                         None)
146
147                     elif clean_type == CLEAN_TYPE_PCL_WITH_PRINTOUT:
148                         maint.cleaning(d, clean_type, maint.cleanType1, maint.primeType1,
149                                         maint.wipeAndSpitType1, tui.load_paper_prompt,
150                                         CleanUI1, CleanUI2, CleanUI3,
151                                         None)
152
153                     else:
154                         log.error("Cleaning not needed or supported on this device.")
155
156                 except Error, e:
157                     log.error("An error occured: %s" % e[0])
158
159             else:
160                 log.error("Device is busy or in an error state. Please check device and try again.")
161                 sys.exit(1)
162         finally:
163             d.close()
164
165     else:
166         try:
167             from PyQt4.QtGui import QApplication
168             from ui4.cleandialog import CleanDialog
169         except ImportError:
170             log.error("Unable to load Qt4 support. Is it installed?")
171             sys.exit(1)
172
173
174         #try:
175         if 1:
176             app = QApplication(sys.argv)
177
178             dlg = CleanDialog(None, device_uri)
179             dlg.show()
180             try:
181                 log.debug("Starting GUI loop...")
182                 app.exec_()
183             except KeyboardInterrupt:
184                 sys.exit(0)
185
186         #finally:
187         if 1:
188             sys.exit(0)
189
190 except KeyboardInterrupt:
191     log.error("User exit")
192
193 log.info("")
194 log.info("Done.")