home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / ui / faxaddrbookform.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  15.8 KB  |  535 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2006 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. import sys, os, os.path, socket
  23.  
  24. from base.g import *
  25. from base import utils, service
  26.  
  27. try:
  28.     from fax import fax
  29. except ImportError:
  30.     # This can fail on Python < 2.3 due to the datetime module
  31.     log.error("Fax address book disabled - Python 2.3+ required.")
  32.     sys.exit(1)
  33.  
  34.  
  35. from qt import *
  36. from faxaddrbookform_base import FaxAddrBookForm_base
  37. from faxaddrbookeditform_base import FaxAddrBookEditForm_base
  38. from faxaddrbookgroupsform_base import FaxAddrBookGroupsForm_base
  39. from faxaddrbookgroupeditform_base import FaxAddrBookGroupEditForm_base
  40.  
  41. # globals
  42. db = None # kirbybase instance
  43.  
  44. # **************************************************************************** #
  45.  
  46. class AddressBookItem(QListViewItem):
  47.  
  48.     def __init__(self, parent, abe):
  49.         QListViewItem.__init__(self, parent)
  50.         self.abe = abe
  51.         self.recno = abe.recno
  52.         self.setText(0, abe.name)
  53.         self.setText(1, abe.title)
  54.         self.setText(2, abe.firstname)
  55.         self.setText(3, abe.lastname)
  56.         self.setText(4, abe.fax)
  57.         self.setText(5, ', '.join(abe.group_list))
  58.         self.setText(6, abe.notes)
  59.  
  60.  
  61. class GroupValidator(QValidator):
  62.     def __init__(self, parent=None, name=None):
  63.         QValidator.__init__(self, parent, name)
  64.  
  65.     def validate(self, input, pos):
  66.         input = str(input)
  67.         if input.find(',') > 0:
  68.             return QValidator.Invalid, pos
  69.         elif len(input) > 50:
  70.             return QValidator.Invalid, pos
  71.         else:
  72.             return QValidator.Acceptable, pos
  73.  
  74.  
  75. class PhoneNumValidator(QValidator):
  76.     def __init__(self, parent=None, name=None):
  77.         QValidator.__init__(self, parent, name)
  78.  
  79.     def validate(self, input, pos):
  80.         input = str(input)
  81.         if not input:
  82.             return QValidator.Acceptable, pos
  83.         elif input[pos-1] not in '0123456789-(+) ':
  84.             return QValidator.Invalid, pos
  85.         elif len(input) > 50:
  86.             return QValidator.Invalid, pos
  87.         else:
  88.             return QValidator.Acceptable, pos
  89.             
  90.  
  91. # **************************************************************************** #
  92.  
  93. class FaxAddrBookGroupEditForm(FaxAddrBookGroupEditForm_base):
  94.  
  95.     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
  96.         FaxAddrBookGroupEditForm_base.__init__(self,parent,name,modal,fl)
  97.         self.edit_mode = False
  98.         self.okButton.setEnabled(True)
  99.         self.all_groups = db.AllGroups()
  100.         self.groupnameEdit.setValidator(GroupValidator(self.groupnameEdit))
  101.  
  102.     def setDlgData(self, group_name):
  103.         self.edit_mode = True
  104.         self.groupnameEdit.setText(group_name)
  105.         self.groupnameEdit.setReadOnly(True)
  106.         self.setEntries(group_name)
  107.  
  108.     def setEntries(self, group_name=''):
  109.         self.entriesListView.clear()
  110.  
  111.         all_entries = db.AllRecordEntries()
  112.  
  113.         for e in all_entries:
  114.             i = QCheckListItem(self.entriesListView, e.name, QCheckListItem.CheckBox)
  115.  
  116.             if group_name and group_name in e.group_list:
  117.                 i.setState(QCheckListItem.On)
  118.  
  119.         self.CheckOKButton()
  120.  
  121.  
  122.     def getDlgData(self):
  123.         group_name = str(self.groupnameEdit.text())
  124.         entries = []
  125.  
  126.         i = self.entriesListView.firstChild()
  127.  
  128.         while i is not None:
  129.             if i.isOn():
  130.                 entries.append(str(i.text()))
  131.  
  132.             i = i.itemBelow()
  133.  
  134.         return group_name, entries
  135.  
  136.  
  137.     def groupnameEdit_textChanged(self,a0):
  138.         self.CheckOKButton()
  139.  
  140.  
  141.     def entriesListView_clicked(self,a0):
  142.         self.CheckOKButton()
  143.  
  144.  
  145.     def CheckOKButton(self):
  146.         group_name = str(self.groupnameEdit.text())
  147.  
  148.         if not group_name or \
  149.             (not self.edit_mode and group_name in self.all_groups):
  150.  
  151.             self.okButton.setEnabled(False)
  152.             return
  153.  
  154.         i = self.entriesListView.firstChild()
  155.  
  156.         while i is not None:
  157.             if i.isOn():
  158.                 break
  159.  
  160.             i = i.itemBelow()
  161.  
  162.         else:
  163.             self.okButton.setEnabled(False)
  164.             return
  165.  
  166.         self.okButton.setEnabled(True)
  167.  
  168. # **************************************************************************** #
  169.  
  170. class FaxAddrBookGroupsForm(FaxAddrBookGroupsForm_base):
  171.  
  172.     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
  173.         FaxAddrBookGroupsForm_base.__init__(self,parent,name,modal,fl)
  174.  
  175.         self.current = None
  176.         QTimer.singleShot(0, self.InitialUpdate)
  177.  
  178.  
  179.     def InitialUpdate(self):
  180.         self.UpdateList()
  181.  
  182.  
  183.     def UpdateList(self):
  184.         self.groupListView.clear()
  185.         first_rec = None
  186.         all_groups = db.AllGroups()
  187.  
  188.         if len(all_groups):
  189.  
  190.             for group in all_groups:
  191.                 i = QListViewItem(self.groupListView, group,
  192.                                   ', '.join(db.GroupEntries(group)))
  193.  
  194.                 if first_rec is None:
  195.                     first_rec = i
  196.  
  197.             self.groupListView.setCurrentItem(i)
  198.             self.current = i
  199.  
  200.             self.editButton.setEnabled(True)
  201.             self.deleteButton.setEnabled(True)
  202.  
  203.         else:
  204.             self.editButton.setEnabled(False)
  205.             self.deleteButton.setEnabled(False)
  206.  
  207.  
  208.     def newButton_clicked(self):
  209.         dlg = FaxAddrBookGroupEditForm(self)
  210.         dlg.setEntries()
  211.         if dlg.exec_loop() == QDialog.Accepted:
  212.             group_name, entries = dlg.getDlgData()
  213.             db.UpdateGroupEntries(group_name, entries)
  214.             self.UpdateList()
  215.  
  216.     def editButton_clicked(self):
  217.         dlg = FaxAddrBookGroupEditForm(self)
  218.         group_name = str(self.current.text(0))
  219.         dlg.setDlgData(group_name)
  220.         if dlg.exec_loop() == QDialog.Accepted:
  221.             group_name, entries = dlg.getDlgData()
  222.             db.UpdateGroupEntries(group_name, entries)
  223.             self.UpdateList()
  224.  
  225.  
  226.     def deleteButton_clicked(self):
  227.         x = QMessageBox.critical(self,
  228.                                  self.caption(),
  229.                                  "<b>Annoying Confirmation: Are you sure you want to delete this group?</b>" ,
  230.                                   QMessageBox.Yes,
  231.                                   QMessageBox.No | QMessageBox.Default,
  232.                                   QMessageBox.NoButton)
  233.         if x == QMessageBox.Yes:
  234.             db.DeleteGroup(str(self.current.text(0)))
  235.             self.UpdateList()
  236.  
  237.  
  238.     def groupListView_currentChanged(self, a0):
  239.         self.current = a0
  240.  
  241.  
  242.     def groupListView_doubleClicked(self, a0):
  243.         self.editButton_clicked()
  244.  
  245.  
  246.     def groupListView_rightButtonClicked(self, item, pos, a2):
  247.         popup = QPopupMenu(self)
  248.  
  249.         popup.insertItem(self.__tr("New..."), self.newButton_clicked)
  250.  
  251.         if item is not None:
  252.             popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
  253.             popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)
  254.  
  255.         popup.insertSeparator()
  256.         popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
  257.         popup.popup(pos)
  258.  
  259.     def __tr(self,s,c = None):
  260.         return qApp.translate("FAB",s,c)
  261.  
  262.  
  263. # **************************************************************************** #
  264.  
  265.  
  266. class FaxAddrBookEditForm(FaxAddrBookEditForm_base):
  267.  
  268.     def __init__(self, editing=True, parent = None,name = None,modal = 0,fl = 0):
  269.         FaxAddrBookEditForm_base.__init__(self,parent,name,modal,fl)
  270.         self.recno = -1
  271.         self.editing = editing
  272.         self.faxEdit.setValidator(PhoneNumValidator(self.faxEdit))
  273.  
  274.     def setDlgData(self, abe):
  275.         self.recno = abe.recno
  276.         self.titleEdit.setText(abe.title)
  277.         self.firstnameEdit.setText(abe.firstname)
  278.         self.lastnameEdit.setText(abe.lastname)
  279.         self.faxEdit.setText(abe.fax)
  280.         self.notesEdit.setText(abe.notes)
  281.         self.nicknameEdit.setText(abe.name)
  282.  
  283.         self.setGroups(abe.group_list)
  284.  
  285.     def setGroups(self, entry_groups=[]):
  286.         self.groupListView.clear()
  287.  
  288.         for g in db.AllGroups():
  289.             i = QCheckListItem(self.groupListView, g, QCheckListItem.CheckBox)
  290.  
  291.             if g in entry_groups:
  292.                 i.setState(QCheckListItem.On)
  293.  
  294.     def getDlgData(self):
  295.         in_groups = []
  296.         i = self.groupListView.firstChild()
  297.  
  298.         while i is not None:
  299.             if i.isOn():
  300.                 in_groups.append(str(i.text()))
  301.             i = i.itemBelow()
  302.  
  303.         return fax.AddressBookEntry((
  304.             self.recno,
  305.             str(self.nicknameEdit.text()),
  306.             str(self.titleEdit.text()),
  307.             str(self.firstnameEdit.text()),
  308.             str(self.lastnameEdit.text()),
  309.             str(self.faxEdit.text()),
  310.             ', '.join(in_groups),
  311.             str(self.notesEdit.text())))
  312.  
  313.  
  314.     def firstnameEdit_textChanged(self,a0):
  315.         pass
  316.  
  317.  
  318.     def lastnameEdit_textChanged(self,a0):
  319.         pass
  320.  
  321.  
  322.     def groupsButton2_clicked(self): # New Group...
  323.         new_group_name, ok = QInputDialog.getText(self.__tr("New Fax Group"),
  324.                                                    self.__tr("New Group Name:"))
  325.  
  326.         if ok and len(new_group_name):
  327.             new_group_name = str(new_group_name)
  328.             abe = db.GetEntryByRecno(self.recno)
  329.  
  330.             if new_group_name not in abe.group_list:
  331.                 abe.group_list.append(new_group_name)
  332.                 db.update(['recno'], [self.recno], [','.join(abe.group_list)], ['groups'])
  333.                 self.setGroups(abe.group_list)
  334.             
  335.  
  336.  
  337.     def nicknameEdit_textChanged(self, nickname):
  338.         self.CheckOKButton(nickname, None)
  339.  
  340.  
  341.     def faxEdit_textChanged(self, fax):
  342.         self.CheckOKButton(None, fax)
  343.  
  344.  
  345.     def CheckOKButton(self, nickname=None, fax=None):
  346.         if nickname is None:
  347.             nickname = str(self.nicknameEdit.text())
  348.         
  349.         if fax is None:
  350.             fax = str(self.faxEdit.text())
  351.             
  352.         ok = len(nickname) and len(fax)
  353.         
  354.         if nickname and not self.editing:
  355.             for x in db.AllRecordEntries():
  356.                 if nickname == x.name:
  357.                     ok = False
  358.  
  359.         self.OKButton.setEnabled(ok)
  360.  
  361.  
  362.     def __tr(self,s,c = None):
  363.         return qApp.translate("FAB",s,c)
  364.  
  365. # **************************************************************************** #
  366.  
  367. class FaxAddrBookForm(FaxAddrBookForm_base):
  368.  
  369.     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
  370.         FaxAddrBookForm_base.__init__(self,parent,name,modal,fl)
  371.  
  372.         icon = QPixmap(os.path.join(prop.image_dir, 'HPmenu.png'))
  373.         self.setIcon(icon)
  374.  
  375.         global db
  376.         db =  fax.FaxAddressBook()
  377.         self.init_problem = False
  378.         self.current = None
  379.  
  380.         try:
  381.             invalids = db.validate()
  382.         except:
  383.             invalids = True
  384.  
  385.         if invalids:
  386.             log.error("Fax address book file is invalid")
  387.  
  388.             if type(invalids) == type([]):
  389.                 log.error(invalids)
  390.  
  391.             self.FailureUI(self.__tr("<b>Fax address book file %s is invalid.</b><p>Please check the file for problems." % db.filename()))
  392.             self.init_problem = True
  393.  
  394.         db.pack()
  395.  
  396.         self.all_groups = []
  397.  
  398.         self.hpssd_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  399.         try:
  400.             self.hpssd_sock.connect((prop.hpssd_host, prop.hpssd_port))
  401.         except socket.error:
  402.             log.error("Unable to contact HPLIP I/O (hpssd).")
  403.             self.hpssd_sock.close()
  404.             self.hpssd_sock = None
  405.  
  406.         QTimer.singleShot(0, self.InitialUpdate)
  407.  
  408.  
  409.     def InitialUpdate(self):
  410.         if self.init_problem:
  411.             self.close()
  412.             return
  413.  
  414.         self.UpdateList()
  415.  
  416.  
  417.     def UpdateList(self):
  418.         self.addressListView.clear()
  419.         first_rec = None
  420.         all_entries = db.AllRecordEntries()
  421.         log.debug("Number of records is: %d" % len(all_entries))
  422.  
  423.         if len(all_entries) > 0:
  424.  
  425.             for abe in all_entries:
  426.                 i = AddressBookItem(self.addressListView, abe)
  427.  
  428.                 if first_rec is None:
  429.                     first_rec = i
  430.  
  431.             self.addressListView.setCurrentItem(i)
  432.             self.current = i
  433.  
  434.             self.editButton.setEnabled(True)
  435.             self.deleteButton.setEnabled(True)
  436.  
  437.         else:
  438.             self.editButton.setEnabled(False)
  439.             self.deleteButton.setEnabled(False)
  440.  
  441.  
  442.     def groupButton_clicked(self):
  443.         FaxAddrBookGroupsForm(self).exec_loop()
  444.         self.sendUpdateEvent()
  445.         self.UpdateList()
  446.  
  447.     def newButton_clicked(self):
  448.         dlg = FaxAddrBookEditForm(False, self)
  449.         dlg.setGroups()
  450.         dlg.groupsButton2.setEnabled(False)
  451.         if dlg.exec_loop() == QDialog.Accepted:
  452.             db.insert(dlg.getDlgData())
  453.             self.sendUpdateEvent()
  454.             self.UpdateList()
  455.  
  456.     def CurrentRecordEntry(self):
  457.         return fax.AddressBookEntry(db.select(['recno'], [self.current.recno])[0])
  458.  
  459.     def editButton_clicked(self):
  460.         dlg = FaxAddrBookEditForm(True, self)
  461.         dlg.setDlgData(self.CurrentRecordEntry())
  462.         if dlg.exec_loop() == QDialog.Accepted:
  463.             db.update(['recno'], [self.current.recno], dlg.getDlgData())
  464.             self.sendUpdateEvent()
  465.             self.UpdateList()
  466.  
  467.  
  468.     def deleteButton_clicked(self):
  469.         x = QMessageBox.critical(self,
  470.                                  self.caption(),
  471.                                  "<b>Annoying Confirmation: Are you sure you want to delete this address book entry?</b>" ,
  472.                                   QMessageBox.Yes,
  473.                                   QMessageBox.No | QMessageBox.Default,
  474.                                   QMessageBox.NoButton)
  475.         if x == QMessageBox.Yes:
  476.             db.delete(['recno'], [self.current.recno])
  477.             self.UpdateList()
  478.             self.sendUpdateEvent()
  479.  
  480.  
  481.     def addressListView_rightButtonClicked(self, item, pos, a2):
  482.         popup = QPopupMenu(self)
  483.  
  484.         popup.insertItem(self.__tr("New..."), self.newButton_clicked)
  485.  
  486.         if item is not None:
  487.             popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
  488.             popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)
  489.  
  490.         popup.insertSeparator()
  491.         popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
  492.         popup.popup(pos)
  493.  
  494.  
  495.     def addressListView_doubleClicked(self,a0):
  496.         self.editButton_clicked()
  497.  
  498.  
  499.     def addressListView_currentChanged(self,item):
  500.         self.current = item
  501.  
  502.  
  503.     def FailureUI(self, error_text):
  504.         QMessageBox.critical(self,
  505.                              self.caption(),
  506.                              error_text,
  507.                               QMessageBox.Ok,
  508.                               QMessageBox.NoButton,
  509.                               QMessageBox.NoButton)
  510.  
  511.  
  512.     def WarningUI(self, msg):
  513.         QMessageBox.warning(self,
  514.                              self.caption(),
  515.                              msg,
  516.                               QMessageBox.Ok,
  517.                               QMessageBox.NoButton,
  518.                               QMessageBox.NoButton)
  519.  
  520.  
  521.     def __tr(self,s,c = None):
  522.         return qApp.translate("FAB",s,c)
  523.  
  524.     def accept(self):
  525.         self.sendUpdateEvent()
  526.         if self.hpssd_sock is not None:
  527.             self.hpssd_sock.close()
  528.             
  529.         FaxAddrBookForm_base.accept(self)
  530.  
  531.     def sendUpdateEvent(self):
  532.         if self.hpssd_sock is not None:
  533.             service.sendEvent(self.hpssd_sock, EVENT_FAX_ADDRESS_BOOK_UPDATED)
  534.  
  535.