home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / ui_utils.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  15.9 KB  |  429 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os.path as os
  5. import re
  6. import os
  7. from base.g import *
  8. from base.codes import *
  9. from base import utils
  10. from PyQt4.QtCore import *
  11. from PyQt4.QtGui import *
  12. pat_html_remove = re.compile('(?is)<.*?>', re.I)
  13. FAB_NAME_ADD = 0
  14. FAB_NAME_RENAME = 1
  15. FAB_NAME_REMOVE = 2
  16. FAB_NAME_DETAILS_CHANGED = 3
  17. FAB_GROUP_ADD = 4
  18. FAB_GROUP_RENAME = 5
  19. FAB_GROUP_REMOVE = 6
  20. FAB_GROUP_MEMBERSHIP_CHANGED = 7
  21.  
  22. def __translate(t):
  23.     return QApplication.translate('ui_utils', t, None, QApplication.UnicodeUTF8)
  24.  
  25.  
  26. def beginWaitCursor():
  27.     QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  28.  
  29.  
  30. def endWaitCursor():
  31.     QApplication.restoreOverrideCursor()
  32.  
  33.  
  34. def load_pixmap(name, subdir = None, resize_to = None):
  35.     name = ''.join([
  36.         os.path.splitext(name)[0],
  37.         '.png'])
  38.     if subdir is None:
  39.         dir = prop.image_dir
  40.         ldir = os.path.join(os.getcwd(), 'data', 'images')
  41.     else:
  42.         dir = os.path.join(prop.image_dir, subdir)
  43.         ldir = os.path.join(os.getcwd(), 'data', 'images', subdir)
  44.     for d in [
  45.         dir,
  46.         ldir]:
  47.         f = os.path.join(d, name)
  48.         if os.path.exists(f):
  49.             if resize_to is not None:
  50.                 img = QImage(f)
  51.                 (x, y) = resize_to
  52.                 return QPixmap.fromImage(img.scaled(x, y, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
  53.             return QPixmap(f)
  54.         os.path.exists(f)
  55.         for w in utils.walkFiles(dir, recurse = True, abs_paths = True, return_folders = False, pattern = name):
  56.             if resize_to is not None:
  57.                 img = QImage(w)
  58.                 (x, y) = resize_to
  59.                 return QPixmap.fromImage(img.scaled(x, y, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
  60.             return QPixmap(w)
  61.         
  62.     
  63.     log.error("Pixmap '%s' not found!" % name)
  64.     return QPixmap()
  65.  
  66. loadPixmap = load_pixmap
  67.  
  68. class UserSettings(QSettings):
  69.     
  70.     def __init__(self):
  71.         QSettings.__init__(self, os.path.join(prop.user_dir, 'hplip.conf'), QSettings.IniFormat)
  72.         self.systray_visible = SYSTRAY_VISIBLE_SHOW_ALWAYS
  73.         self.last_used_device_uri = ''
  74.         self.last_used_printer = ''
  75.         self.version = ''
  76.         self.date_time = ''
  77.         self.auto_refresh = False
  78.         self.auto_refresh_rate = 30
  79.         self.auto_refresh_type = 1
  80.         self.polling_interval = 5
  81.         self.polling = True
  82.         self.device_list = []
  83.         self.loadDefaults()
  84.  
  85.     
  86.     def __setup(self, cmds):
  87.         for c in cmds:
  88.             basename = c.split()[0]
  89.             path = utils.which(basename)
  90.             if path:
  91.                 return ' '.join([
  92.                     os.path.join(path, basename),
  93.                     ' '.join(c.split()[1:])])
  94.         
  95.         return ''
  96.  
  97.     
  98.     def loadDefaults(self):
  99.         self.cmd_scan = self._UserSettings__setup([
  100.             'xsane -V %SANE_URI%',
  101.             'kooka',
  102.             'xscanimage'])
  103.         self.cmd_fab = self._UserSettings__setup([
  104.             'hp-fab'])
  105.  
  106.     
  107.     def load(self):
  108.         log.debug('Loading user settings...')
  109.         self.sync()
  110.         self.beginGroup('settings')
  111.         (i, ok) = self.value('systray_visible').toInt()
  112.         if ok:
  113.             self.systray_visible = i
  114.         
  115.         self.endGroup()
  116.         self.beginGroup('last_used')
  117.         if not unicode(self.value('device_uri').toString()):
  118.             pass
  119.         self.last_used_device_uri = self.last_used_device_uri
  120.         if not unicode(self.value('printer').toString()):
  121.             pass
  122.         self.last_used_printer = self.last_used_printer
  123.         self.endGroup()
  124.         self.beginGroup('commands')
  125.         if not unicode(self.value('scan').toString()):
  126.             pass
  127.         self.cmd_scan = self.cmd_scan
  128.         self.endGroup()
  129.         self.beginGroup('refresh')
  130.         if not self.value('rate').toString():
  131.             pass
  132.         self.auto_refresh_rate = int(self.auto_refresh_rate)
  133.         self.auto_refresh = bool(self.value('enable').toBool())
  134.         if not self.value('type').toString():
  135.             pass
  136.         self.auto_refresh_type = int(self.auto_refresh_type)
  137.         self.endGroup()
  138.         self.beginGroup('installation')
  139.         self.version = unicode(self.value('version').toString())
  140.         self.date_time = unicode(self.value('date_time').toString())
  141.         self.endGroup()
  142.         self.beginGroup('polling')
  143.         self.polling = bool(self.value('enable').toBool())
  144.         if not self.value('interval').toString():
  145.             pass
  146.         self.polling_interval = int(self.polling_interval)
  147.         if not self.value('device_list').toString():
  148.             pass
  149.         self.polling_device_list = unicode('').split(u',')
  150.         self.endGroup()
  151.  
  152.     
  153.     def save(self):
  154.         log.debug('Saving user settings...')
  155.         self.beginGroup('settings')
  156.         i = QVariant(self.systray_visible)
  157.         self.setValue('systray_visible', QVariant(self.systray_visible))
  158.         self.endGroup()
  159.         self.beginGroup('last_used')
  160.         self.setValue('device_uri', QVariant(self.last_used_device_uri))
  161.         self.setValue('printer', QVariant(self.last_used_printer))
  162.         self.endGroup()
  163.         self.beginGroup('commands')
  164.         self.setValue('scan', QVariant(self.cmd_scan))
  165.         self.endGroup()
  166.         self.beginGroup('refresh')
  167.         self.setValue('rate', QVariant(self.auto_refresh_rate))
  168.         self.setValue('enable', QVariant(self.auto_refresh))
  169.         self.setValue('type', QVariant(self.auto_refresh_type))
  170.         self.endGroup()
  171.         self.beginGroup('polling')
  172.         self.setValue('enable', QVariant(self.polling))
  173.         self.setValue('interval', QVariant(self.polling_interval))
  174.         self.setValue('device_list', QVariant(u','.join(self.polling_device_list)))
  175.         self.endGroup()
  176.         self.sync()
  177.  
  178.     
  179.     def debug(self):
  180.         log.debug('FAB command: %s' % self.cmd_fab)
  181.         log.debug('Scan command: %s' % self.cmd_scan)
  182.         log.debug('Auto refresh: %s' % self.auto_refresh)
  183.         log.debug('Auto refresh rate: %s' % self.auto_refresh_rate)
  184.         log.debug('Auto refresh type: %s' % self.auto_refresh_type)
  185.         log.debug('Systray visible: %d' % self.systray_visible)
  186.         log.debug('Last used device URI: %s' % self.last_used_device_uri)
  187.         log.debug('Last used printer: %s' % self.last_used_printer)
  188.  
  189.  
  190.  
  191. def su_sudo():
  192.     su_sudo_str = None
  193.     if utils.which('kdesu'):
  194.         su_sudo_str = 'kdesu -- %s'
  195.     elif utils.which('kdesudo'):
  196.         su_sudo = 'kdesudo -- %s'
  197.     elif utils.which('gnomesu'):
  198.         su_sudo_str = 'gnomesu -c "%s"'
  199.     elif utils.which('gksu'):
  200.         su_sudo_str = 'gksu "%s"'
  201.     
  202.     return su_sudo_str
  203.  
  204. DEFAULT_TITLE = __translate('HP Device Manager')
  205.  
  206. def FailureUI(parent, error_text, title_text = None):
  207.     log.error(pat_html_remove.sub(' ', unicode(error_text)))
  208.     if title_text is None:
  209.         if parent is not None:
  210.             title_text = parent.windowTitle()
  211.         else:
  212.             title_text = DEFAULT_TITLE
  213.     
  214.     QMessageBox.critical(parent, title_text, error_text, QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton)
  215.  
  216. showFailureUi = FailureUI
  217.  
  218. def WarningUI(parent, warn_text, title_text = None):
  219.     log.warn(pat_html_remove.sub(' ', unicode(warn_text)))
  220.     if title_text is None:
  221.         if parent is not None:
  222.             title_text = parent.windowTitle()
  223.         else:
  224.             title_text = DEFAULT_TITLE
  225.     
  226.     QMessageBox.warning(parent, title_text, warn_text, QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton)
  227.  
  228. showWarningUi = WarningUI
  229.  
  230. def SuccessUI(parent, text, title_text = None):
  231.     log.info(pat_html_remove.sub(' ', unicode(text)))
  232.     if title_text is None:
  233.         if parent is not None:
  234.             title_text = parent.windowTitle()
  235.         else:
  236.             title_text = DEFAULT_TITLE
  237.     
  238.     QMessageBox.information(parent, title_text, text, QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton)
  239.  
  240. showSuccessUi = SuccessUI
  241.  
  242. def CheckDeviceUI(parent, title_text = None):
  243.     text = __translate('<b>Unable to communicate with device or device is in an error state.</b><p>Please check device setup and try again.</p>')
  244.     return FailureUI(parent, text, title_text)
  245.  
  246. checkDeviceUi = CheckDeviceUI
  247.  
  248. class PrinterNameValidator(QValidator):
  249.     
  250.     def __init__(self, parent = None):
  251.         QValidator.__init__(self, parent)
  252.  
  253.     
  254.     def validate(self, input, pos):
  255.         input = unicode(input)
  256.         if not input:
  257.             return (QValidator.Acceptable, pos)
  258.         if input[pos - 1] in u'~`!@#$%^&*()-=+[]{}()\\/,.<>?\'";:| ':
  259.             return (QValidator.Invalid, pos)
  260.         return (QValidator.Acceptable, pos)
  261.  
  262.  
  263.  
  264. class PhoneNumValidator(QValidator):
  265.     
  266.     def __init__(self, parent = None):
  267.         QValidator.__init__(self, parent)
  268.  
  269.     
  270.     def validate(self, input, pos):
  271.         input = unicode(input)
  272.         if not input:
  273.             return (QValidator.Acceptable, pos)
  274.         if input[pos - 1] not in u'0123456789-(+).,#* ':
  275.             return (QValidator.Invalid, pos)
  276.         return (QValidator.Acceptable, pos)
  277.  
  278.  
  279.  
  280. class AddressBookNameValidator(QValidator):
  281.     
  282.     def __init__(self, db, parent = None):
  283.         QValidator.__init__(self, parent)
  284.         self.db = db
  285.  
  286.     
  287.     def validate(self, input, pos):
  288.         input = unicode(input)
  289.         if not input:
  290.             return (QValidator.Acceptable, pos)
  291.         if input in self.db.get_all_names():
  292.             return (QValidator.Invalid, pos)
  293.         if input[pos - 1] in u'|\\/"':
  294.             return (QValidator.Invalid, pos)
  295.         return (QValidator.Acceptable, pos)
  296.  
  297.  
  298. MIME_TYPES_DESC = {
  299.     'application/pdf': (__translate('PDF Document'), '.pdf'),
  300.     'application/postscript': (__translate('Postscript Document'), '.ps'),
  301.     'application/vnd.hp-HPGL': (__translate('HP Graphics Language File'), '.hgl, .hpg, .plt, .prn'),
  302.     'application/x-cshell': (__translate('C Shell Script'), '.csh, .sh'),
  303.     'application/x-csource': (__translate('C Source Code'), '.c'),
  304.     'text/cpp': (__translate('C++ Source Code'), '.cpp, .cxx'),
  305.     'application/x-perl': (__translate('Perl Script'), '.pl'),
  306.     'application/x-python': (__translate('Python Program'), '.py'),
  307.     'application/x-shell': (__translate('Shell Script'), '.sh'),
  308.     'text/plain': (__translate('Plain Text'), '.txt, .log'),
  309.     'text/html': (__translate('HTML Dcoument'), '.htm, .html'),
  310.     'image/gif': (__translate('GIF Image'), '.gif'),
  311.     'image/png': (__translate('PNG Image'), '.png'),
  312.     'image/jpeg': (__translate('JPEG Image'), '.jpg, .jpeg'),
  313.     'image/tiff': (__translate('TIFF Image'), '.tif, .tiff'),
  314.     'image/x-bitmap': (__translate('Bitmap (BMP) Image'), '.bmp'),
  315.     'image/x-bmp': (__translate('Bitmap (BMP) Image'), '.bmp'),
  316.     'image/x-photocd': (__translate('Photo CD Image'), '.pcd'),
  317.     'image/x-portable-anymap': (__translate('Portable Image (PNM)'), '.pnm'),
  318.     'image/x-portable-bitmap': (__translate('Portable B&W Image (PBM)'), '.pbm'),
  319.     'image/x-portable-graymap': (__translate('Portable Grayscale Image (PGM)'), '.pgm'),
  320.     'image/x-portable-pixmap': (__translate('Portable Color Image (PPM)'), '.ppm'),
  321.     'image/x-sgi-rgb': (__translate('SGI RGB'), '.rgb'),
  322.     'image/x-xbitmap': (__translate('X11 Bitmap (XBM)'), '.xbm'),
  323.     'image/x-xpixmap': (__translate('X11 Pixmap (XPM)'), '.xpm'),
  324.     'image/x-sun-raster': (__translate('Sun Raster Format'), '.ras'),
  325.     'application/hplip-fax': (__translate('HPLIP Fax File'), '.g3, .g4') }
  326. status_icons = None
  327.  
  328. def getStatusListIcon(error_state):
  329.     global status_icons
  330.     if status_icons is None:
  331.         status_icons = {
  332.             ERROR_STATE_CLEAR: (load_pixmap('idle', '16x16'), load_pixmap('idle', '16x16')),
  333.             ERROR_STATE_BUSY: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  334.             ERROR_STATE_ERROR: (load_pixmap('error', '16x16'), load_pixmap('error', '16x16')),
  335.             ERROR_STATE_LOW_SUPPLIES: (load_pixmap('inkdrop', '16x16'), load_pixmap('toner', '16x16')),
  336.             ERROR_STATE_OK: (load_pixmap('ok', '16x16'), load_pixmap('ok', '16x16')),
  337.             ERROR_STATE_WARNING: (load_pixmap('warning', '16x16'), load_pixmap('warning', '16x16')),
  338.             ERROR_STATE_LOW_PAPER: (load_pixmap('paper', '16x16'), load_pixmap('paper', '16x16')),
  339.             ERROR_STATE_PRINTING: (load_pixmap('print', '16x16'), load_pixmap('print', '16x16')),
  340.             ERROR_STATE_SCANNING: (load_pixmap('scan', '16x16'), load_pixmap('scan', '16x16')),
  341.             ERROR_STATE_PHOTOCARD: (load_pixmap('pcard', '16x16'), load_pixmap('pcard', '16x16')),
  342.             ERROR_STATE_FAXING: (load_pixmap('fax', '16x16'), load_pixmap('fax', '16x16')),
  343.             ERROR_STATE_COPYING: (load_pixmap('makecopies', '16x16'), load_pixmap('makecopies', '16x16')) }
  344.     
  345.     return status_icons.get(error_state, status_icons[ERROR_STATE_CLEAR])
  346.  
  347. overlay_icons = None
  348.  
  349. def getStatusOverlayIcon(error_state):
  350.     global overlay_icons
  351.     if overlay_icons is None:
  352.         overlay_icons = {
  353.             ERROR_STATE_CLEAR: (None, None),
  354.             ERROR_STATE_BUSY: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  355.             ERROR_STATE_ERROR: (load_pixmap('error', '16x16'), load_pixmap('error', '16x16')),
  356.             ERROR_STATE_LOW_SUPPLIES: (load_pixmap('inkdrop', '16x16'), load_pixmap('toner', '16x16')),
  357.             ERROR_STATE_OK: (load_pixmap('ok', '16x16'), load_pixmap('ok', '16x16')),
  358.             ERROR_STATE_WARNING: (load_pixmap('warning', '16x16'), load_pixmap('warning', '16x16')),
  359.             ERROR_STATE_LOW_PAPER: (load_pixmap('paper', '16x16'), load_pixmap('paper', '16x16')),
  360.             ERROR_STATE_PRINTING: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  361.             ERROR_STATE_SCANNING: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  362.             ERROR_STATE_PHOTOCARD: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  363.             ERROR_STATE_FAXING: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  364.             ERROR_STATE_COPYING: (load_pixmap('busy', '16x16'), load_pixmap('busy', '16x16')),
  365.             ERROR_STATE_REFRESHING: (load_pixmap('refresh1', '16x16'), load_pixmap('refresh1', '16x16')) }
  366.     
  367.     return overlay_icons.get(error_state, overlay_icons[ERROR_STATE_CLEAR])
  368.  
  369. NUM_REPRS = {
  370.     1: __translate('one'),
  371.     2: __translate('two'),
  372.     3: __translate('three'),
  373.     4: __translate('four'),
  374.     5: __translate('five'),
  375.     6: __translate('six'),
  376.     7: __translate('seven'),
  377.     8: __translate('eight'),
  378.     9: __translate('nine'),
  379.     10: __translate('ten'),
  380.     11: __translate('eleven'),
  381.     12: __translate('twelve') }
  382. UNIT_NAMES = {
  383.     'year': (__translate('year'), __translate('years')),
  384.     'month': (__translate('month'), __translate('months')),
  385.     'week': (__translate('week'), __translate('weeks')),
  386.     'day': (__translate('day'), __translate('days')),
  387.     'hour': (__translate('hour'), __translate('hours')),
  388.     'minute': (__translate('minute'), __translate('minutes')),
  389.     'second': (__translate('second'), __translate('seconds')) }
  390.  
  391. def getTimeDeltaDesc(past):
  392.     t1 = QDateTime()
  393.     t1.setTime_t(int(past))
  394.     t2 = QDateTime.currentDateTime()
  395.     delta = t1.secsTo(t2)
  396.     return __translate('(%1 ago)').arg(stringify(delta))
  397.  
  398.  
  399. def getSecondsInUnits(seconds):
  400.     unit_limits = [
  401.         ('year', 31536000),
  402.         ('month', 2592000),
  403.         ('week', 604800),
  404.         ('day', 86400),
  405.         ('hour', 3600),
  406.         ('minute', 60)]
  407.     for unit_name, limit in unit_limits:
  408.         if seconds >= limit:
  409.             amount = int(round(float(seconds) / limit))
  410.             return (amount, unit_name)
  411.     
  412.     return (seconds, 'second')
  413.  
  414.  
  415. def stringify(seconds):
  416.     (amount, unit_name) = getSecondsInUnits(seconds)
  417.     
  418.     try:
  419.         i18n_amount = NUM_REPRS[amount]
  420.     except KeyError:
  421.         i18n_amount = unicode(amount)
  422.  
  423.     if amount == 1:
  424.         i18n_unit = UNIT_NAMES[unit_name][0]
  425.     else:
  426.         i18n_unit = UNIT_NAMES[unit_name][1]
  427.     return QString('%1 %2').arg(i18n_amount).arg(i18n_unit)
  428.  
  429.