def toolbox_triggered(self): try: os.waitpid(-1, os.WNOHANG) except OSError: pass # See if it is already running... ok, lock_file = utils.lock_app('hp-toolbox', True) if ok: # able to lock, not running... utils.unlock(lock_file) path = utils.which('hp-toolbox') if path: path = os.path.join(path, 'hp-toolbox') else: log.error("Unable to find hp-toolbox on PATH.") self.tray_icon.showMessage( "HPLIP Status Service", self.__tr("Unable to locate hp-toolbox on system PATH."), self.icon_error, 5000) return log.debug(path) os.spawnlp(os.P_NOWAIT, path, 'hp-toolbox') else: # ...already running, raise it device.Event('', '', EVENT_RAISE_DEVICE_MANAGER).send_via_dbus( SessionBus(), 'com.hplip.Toolbox')
def notifier_activated(self, s): m = '' while True: ready = select.select([self.read_pipe], [], [], 1.0) if ready[0]: m = ''.join([m, os.read(self.read_pipe, self.fmt_size)]) if len(m) == self.fmt_size: event = device.Event(*struct.unpack(self.fmt, m)) if event.event_code > EVENT_MAX_USER_EVENT: continue desc = device.queryString(event.event_code) #print "BUBBLE:", event.device_uri, event.event_code, event.username error_state = STATUS_TO_ERROR_STATE_MAP.get( event.event_code, ERROR_STATE_CLEAR) icon = self.ERROR_STATE_TO_ICON.get( error_state, self.icon_info) if self.tray_icon.supportsMessages(): if event.job_id and event.title: self.tray_icon.showMessage("HPLIP Device Status", QString("%1\n%2\n%3\n(%4/%5/%6)").\ arg(event.device_uri).arg(event.event_code).\ arg(desc).arg(event.username).arg(event.job_id).arg(event.title), icon, 5000) else: self.tray_icon.showMessage("HPLIP Device Status", QString("%1\n%2\n%3").arg(event.device_uri).\ arg(event.event_code).arg(desc), icon, 5000) else: break
def handle_hpdio_event(event, bytes_written): log.debug("Reading %d bytes from hpdio pipe..." % bytes_written) total_read, data = 0, '' while True: r, w, e = select.select([r3], [], [r3], 0.0) if not r: break x = os.read(r3, PIPE_BUF) if not x: break data = ''.join([data, x]) total_read += len(x) if total_read == bytes_written: break log.debug("Read %d bytes" % total_read) if total_read == bytes_written: dq = loads(data) if check_device(event.device_uri) == ERROR_SUCCESS: devices[event.device_uri].dq = dq.copy() handle_event( device.Event(event.device_uri, '', dq.get('status-code', STATUS_PRINTER_IDLE), prop.username, 0, '')) send_toolbox_event(event, EVENT_DEVICE_UPDATE_REPLY)
def getHistory(self, service): if service is not None and self.needs_update: device_uri, h = service.GetHistory(self.device_uri) self.history = [ device.Event(*tuple(e)) for e in list(h)[:-MAX_MENU_EVENTS:-1] ] self.needs_update = False
def sendMessage(self, device_uri, printer_name, event_code, username=prop.username, job_id=0, title='', pipe_name='', interface='com.hplip.StatusService'): #device.Event(device_uri, printer_name, event_code, username, job_id, title).send_via_dbus(SessionBus(), interface) device.Event(device_uri, printer_name, event_code, username, job_id, title).send_via_dbus(self.session_bus, interface)
def run( read_pipe2=None, # pipe from hpssd write_pipe3=None): # pipe to hpssd global r2, w3 # tmp_dir = '/tmp' os.umask(0o111) try: log.set_module("hp-systray(hpdio)") log.debug("PID=%d" % os.getpid()) r2, w3 = read_pipe2, write_pipe3 fmt = "80s80sI32sI80sf" # TODO: Move to Event class fmt_size = struct.calcsize(fmt) response = {} dev = None m = '' while True: try: r, w, e = select.select([r2], [], [r2], 1.0) except KeyboardInterrupt: break except select.error as e: if e[0] == errno.EINTR: continue else: break if not r: continue if e: break m = os.read(r2, fmt_size) if not m: break while len(m) >= fmt_size: response.clear() event = device.Event(*[ x.rstrip(b'\x00').decode('utf-8') if isinstance(x, bytes ) else x for x in struct.unpack(fmt, m[:fmt_size]) ]) m = m[fmt_size:] action = event.event_code if PY3: device_uri = event.device_uri else: device_uri = str(event.device_uri) log.debug("Handling event...") event.debug() send_message(device_uri, EVENT_DEVICE_UPDATE_ACTIVE) if action in (EVENT_DEVICE_UPDATE_REQUESTED, EVENT_POLLING_REQUEST): #try: if 1: #log.debug("%s starting for %s" % (ACTION_NAMES[action], device_uri)) try: dev = devices[device_uri] except KeyError: dev = devices[device_uri] = device.Device( device_uri, disable_dbus=True) try: #print "Device.open()" dev.open() except Error as e: log.error(e.msg) response = { 'error-state': ERROR_STATE_ERROR, 'device-state': DEVICE_STATE_NOT_FOUND, 'status-code': EVENT_ERROR_DEVICE_IO_ERROR } if dev.device_state == DEVICE_STATE_NOT_FOUND: dev.error_state = ERROR_STATE_ERROR else: if action == EVENT_DEVICE_UPDATE_REQUESTED: try: #print "Device.queryDevice()" dev.queryDevice() except Error as e: log.error("Query device error (%s)." % e.msg) dev.error_state = ERROR_STATE_ERROR dev.status_code = EVENT_ERROR_DEVICE_IO_ERROR response = dev.dq #print response log.debug("Device state = %d" % dev.device_state) log.debug("Status code = %d" % dev.status_code) log.debug("Error state = %d" % dev.error_state) else: # EVENT_POLLING_REQUEST try: dev.pollDevice() except Error as e: log.error("Poll device error (%s)." % e.msg) dev.error_state = ERROR_STATE_ERROR else: response = {'test': 1} #finally: if 1: if dev is not None: dev.close() #thread_activity_lock.release() elif action == EVENT_USER_CONFIGURATION_CHANGED: pass elif action == EVENT_SYSTEMTRAY_EXIT: log.debug("Exiting") sys.exit(1) send_message(device_uri, EVENT_DEVICE_UPDATE_INACTIVE) if action == EVENT_DEVICE_UPDATE_REQUESTED: #print response data = dumps(response, HIGHEST_PROTOCOL) log.debug("Sending data through pipe to hpssd...") total_written = 0 while True: total_written += os.write(w3, data[:PIPE_BUF]) data = data[PIPE_BUF:] if not data: break log.debug("Wrote %d bytes" % total_written) send_message(device_uri, EVENT_DEVICE_UPDATE_REPLY, total_written) elif action == EVENT_POLLING_REQUEST: # TODO: Translate into event: scan requested, copy requested, etc.. send as event #try: # os.write pass except KeyboardInterrupt: log.debug("Ctrl-C: Exiting...")
def quit_triggered(self): device.Event('', '', EVENT_SYSTEMTRAY_EXIT).send_via_dbus(SessionBus()) self.quit()
def notifierActivated(self, s): m = '' while True: try: r, w, e = select.select([self.read_pipe], [], [self.read_pipe], 1.0) except select.error: log.debug("Error in select()") break if e: log.error("Pipe error: %s" % e) break if r: #m = ''.join([m, os.read(self.read_pipe, self.fmt_size)]) m = os.read(self.read_pipe, self.fmt_size) while len(m) >= self.fmt_size: event = device.Event(*[x.rstrip(b'\x00').decode('utf-8') if isinstance(x, bytes) else x for x in struct.unpack(self.fmt, m[:self.fmt_size])]) m = m[self.fmt_size:] if event.event_code == EVENT_CUPS_QUEUES_REMOVED or event.event_code == EVENT_CUPS_QUEUES_ADDED: self.resetDevice() for d in device.getSupportedCUPSDevices(back_end_filter=['hp', 'hpfax']): self.addDevice(d) self.setMenu() if event.event_code == EVENT_USER_CONFIGURATION_CHANGED: log.debug("Re-reading configuration (EVENT_USER_CONFIGURATION_CHANGED)") self.user_settings.load() self.user_settings.debug() elif event.event_code == EVENT_SYSTEMTRAY_EXIT: self.quit() return if self.user_settings.systray_visible in \ (SYSTRAY_VISIBLE_SHOW_ALWAYS, SYSTRAY_VISIBLE_HIDE_WHEN_INACTIVE): log.debug("Showing...") self.tray_icon.setVisible(True) if event.event_code == EVENT_DEVICE_UPDATE_ACTIVE: if not self.active_icon: self.tray_icon.setIcon(self.prop_active_icon) self.active_icon = True continue elif event.event_code == EVENT_DEVICE_UPDATE_INACTIVE: if self.active_icon: self.tray_icon.setIcon(self.prop_icon) self.active_icon = False continue elif event.event_code == EVENT_DEVICE_UPDATE_BLIP: if not self.active_icon: self.tray_icon.setIcon(self.prop_active_icon) self.active_icon = True QTimer.singleShot(BLIP_DELAY, self.blipTimeout) continue if self.user_settings.systray_visible in (SYSTRAY_VISIBLE_HIDE_WHEN_INACTIVE, SYSTRAY_VISIBLE_HIDE_ALWAYS): log.debug("Waiting to hide...") QTimer.singleShot(HIDE_INACTIVE_DELAY, self.timeoutHideWhenInactive) if event.event_code <= EVENT_MAX_USER_EVENT or \ event.event_code == EVENT_CUPS_QUEUES_REMOVED or event.event_code == EVENT_CUPS_QUEUES_ADDED: if event.event_code != EVENT_CUPS_QUEUES_REMOVED: self.addDevice(event.device_uri) self.setMenu() if self.tray_icon.supportsMessages(): log.debug("Tray icon message:") event.debug() error_state = STATUS_TO_ERROR_STATE_MAP.get(event.event_code, ERROR_STATE_CLEAR) desc = device.queryString(event.event_code) show_message = False if self.user_settings.systray_messages == SYSTRAY_MESSAGES_SHOW_ALL: # OK, Busy show_message = True elif self.user_settings.systray_messages in (SYSTRAY_MESSAGES_SHOW_ERRORS_AND_WARNINGS, SYSTRAY_MESSAGES_SHOW_ERRORS_ONLY): if error_state == ERROR_STATE_ERROR: show_message = True elif self.user_settings.systray_messages == SYSTRAY_MESSAGES_SHOW_ERRORS_AND_WARNINGS and \ error_state in (ERROR_STATE_WARNING, ERROR_STATE_LOW_SUPPLIES, ERROR_STATE_LOW_PAPER): show_message = True if event.printer_name: d = event.printer_name else: back_end, is_hp, bus, model, serial, dev_file, host, zc, port = \ device.parseDeviceURI(event.device_uri) if bus == 'usb': idd = serial elif bus == 'net': idd = host elif bus == 'par': idd = dev_file else: idd = 'unknown' self.model = models.normalizeModelUIName(model) if back_end == 'hp': d = self.__tr("%s Printer (%s)"%(model,idd)) elif back_end == 'hpaio': d = self.__tr("%s Scanner (%s)"%(model,idd)) elif back_end == 'hpfax': d = self.__tr("%s Fax (%s)"%(model,idd)) else: d = self.__tr("%s (%s)"%(model,idd)) if show_message: if have_pynotify and pynotify.init("hplip"): # Use libnotify/pynotify icon, urgency = ERROR_STATE_TO_ICON_AND_URGENCY_PYNOTIFY.get(error_state, (getPynotifyIcon('info'), pynotify.URGENCY_NORMAL)) if event.job_id and event.title: msg = "%s\n%s: %s\n(%s/%s)" % (to_unicode(d), desc, event.title, event.username, event.job_id) log.debug("Notify: uri=%s desc=%s title=%s user=%s job_id=%d code=%d" % (event.device_uri, desc, event.title, event.username, event.job_id, event.event_code)) else: msg = "%s\n%s (%s)" % (to_unicode(d), desc, event.event_code) log.debug("Notify: uri=%s desc=%s code=%d" % (event.device_uri, desc, event.event_code)) n = pynotify.Notification("HPLIP Device Status", msg, icon) # CRID: 11833 Debian Traceback error notification exceeded n.set_hint('transient', True) n.set_urgency(urgency) if error_state == ERROR_STATE_ERROR: n.set_timeout(pynotify.EXPIRES_NEVER) else: n.set_timeout(TRAY_MESSAGE_DELAY) n.show() else: # Use "standard" message bubbles icon = ERROR_STATE_TO_ICON.get(error_state, QSystemTrayIcon.Information) if event.job_id and event.title: log.debug("Bubble: uri=%s desc=%s title=%s user=%s job_id=%d code=%d" % (event.device_uri, desc, event.title, event.username, event.job_id, event.event_code)) self.tray_icon.showMessage(self.__tr("HPLIP Device Status"), "%s\n%s: %s\n(%s/%s)"%(d,desc, event.title,event.username,event.job_id), icon, TRAY_MESSAGE_DELAY) else: log.debug("Bubble: uri=%s desc=%s code=%d" % (event.device_uri, desc, event.event_code)) self.tray_icon.showMessage(self.__tr("HPLIP Device Status"), "%s\n%s (%s)"%(d,desc,event.event_code), icon, TRAY_MESSAGE_DELAY) else: break
def handle_signal(typ, *args, **kwds): if kwds['interface'] == 'com.hplip.StatusService' and \ kwds['member'] == 'Event': event = device.Event(*args[:6]) return handle_event(event, args[6:])
def send_systray_blip(): send_event_to_systray_ui(device.Event('', '', EVENT_DEVICE_UPDATE_BLIP))
def SendEvent(self, device_uri, printer_name, event_code, username, job_id, title): event = device.Event(device_uri, printer_name, event_code, username, job_id, title) handle_event(event)
def run(read_pipe2=None, # pipe from hpssd write_pipe3=None): # pipe to hpssd global r2, w3 # tmp_dir = '/tmp' os.umask(0111) try: log.set_module("hp-systray(hpdio)") log.debug("PID=%d" % os.getpid()) r2, w3 = read_pipe2, write_pipe3 fmt = "80s80sI32sI80sf" # TODO: Move to Event class fmt_size = struct.calcsize(fmt) response = {} dev = None m = '' while True: try: r, w, e = select.select([r2], [], [r2], 1.0) except KeyboardInterrupt: break except select.error, e: if e[0] == errno.EINTR: continue else: break if not r: continue if e: break m = ''.join([m, os.read(r2, fmt_size)]) if not m: break while len(m) >= fmt_size: response.clear() event = device.Event(*struct.unpack(fmt, m[:fmt_size])) m = m[fmt_size:] action = event.event_code device_uri = event.device_uri log.debug("Handling event...") event.debug() send_message(device_uri, EVENT_DEVICE_UPDATE_ACTIVE) if action in (EVENT_DEVICE_UPDATE_REQUESTED, EVENT_POLLING_REQUEST): #try: if 1: #log.debug("%s starting for %s" % (ACTION_NAMES[action], device_uri)) try: dev = devices[device_uri] except KeyError: dev = devices[device_uri] = device.Device(device_uri, disable_dbus=True) try: #print "Device.open()" dev.open() except Error, e: log.error(e.msg) response = {'error-state': ERROR_STATE_ERROR, 'device-state': DEVICE_STATE_NOT_FOUND, 'status-code' : EVENT_ERROR_DEVICE_IO_ERROR} if dev.device_state == DEVICE_STATE_NOT_FOUND: dev.error_state = ERROR_STATE_ERROR else: if action == EVENT_DEVICE_UPDATE_REQUESTED: try: #print "Device.queryDevice()" dev.queryDevice() except Error, e: log.error("Query device error (%s)." % e.msg) dev.error_state = ERROR_STATE_ERROR dev.status_code = EVENT_ERROR_DEVICE_IO_ERROR response = dev.dq #print response log.debug("Device state = %d" % dev.device_state) log.debug("Status code = %d" % dev.status_code) log.debug("Error state = %d" % dev.error_state) else: # EVENT_POLLING_REQUEST try: dev.pollDevice() except Error, e: log.error("Poll device error (%s)." % e.msg) dev.error_state = ERROR_STATE_ERROR else: response = {'test' : 1}