1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
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.
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.
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
29 from base import utils
30 from ui_utils import load_pixmap
35 # This can fail on Python < 2.3 due to the datetime module
36 log.error("Fax address book disabled - Python 2.3+ required.")
41 from faxaddrbookform_base import FaxAddrBookForm_base
42 from faxaddrbookeditform_base import FaxAddrBookEditForm_base
43 from faxaddrbookgroupsform_base import FaxAddrBookGroupsForm_base
44 from faxaddrbookgroupeditform_base import FaxAddrBookGroupEditForm_base
49 # ****************************************************************************
51 class AddressBookItem2(QListViewItem):
53 def __init__(self, parent, entry):
54 QListViewItem.__init__(self, parent)
56 self.setText(0, entry['name'])
57 self.setText(1, entry['title'])
58 self.setText(2, entry['firstname'])
59 self.setText(3, entry['lastname'])
60 self.setText(4, entry['fax'])
61 self.setText(5, ', '.join(entry['groups']))
62 self.setText(6, entry['notes'])
64 class GroupValidator(QValidator):
65 def __init__(self, parent=None, name=None):
66 QValidator.__init__(self, parent, name)
68 def validate(self, input, pos):
69 input = unicode(input)
70 if input.find(u',') > 0:
71 return QValidator.Invalid, pos
73 return QValidator.Invalid, pos
75 return QValidator.Acceptable, pos
78 class PhoneNumValidator(QValidator):
79 def __init__(self, parent=None, name=None):
80 QValidator.__init__(self, parent, name)
82 def validate(self, input, pos):
83 input = unicode(input)
85 return QValidator.Acceptable, pos
86 elif input[pos-1] not in u'0123456789-(+) *#':
87 return QValidator.Invalid, pos
89 return QValidator.Invalid, pos
91 return QValidator.Acceptable, pos
94 # **************************************************************************** #
96 class FaxAddrBookGroupEditForm(FaxAddrBookGroupEditForm_base):
98 Called when clicking New... or Edit... from the Group Dialog
100 def __init__(self,parent = None,name = None,modal = 0,fl = 0):
101 FaxAddrBookGroupEditForm_base.__init__(self,parent,name,modal,fl)
102 self.edit_mode = False
103 self.okButton.setEnabled(True)
104 self.all_groups = db.get_all_groups()
105 self.groupnameEdit.setValidator(GroupValidator(self.groupnameEdit))
107 def setDlgData(self, group_name):
108 self.edit_mode = True
109 self.groupnameEdit.setText(group_name)
110 self.groupnameEdit.setReadOnly(True)
111 self.setEntries(group_name)
113 def setEntries(self, group_name=''):
114 self.entriesListView.clear()
115 all_entries = db.get_all_records()
117 for e, v in all_entries.items():
118 i = QCheckListItem(self.entriesListView, e, QCheckListItem.CheckBox)
120 if group_name and group_name in v['groups']:
121 i.setState(QCheckListItem.On)
126 def getDlgData(self):
127 group_name = unicode(self.groupnameEdit.text())
130 i = self.entriesListView.firstChild()
134 entries.append(unicode(i.text()))
138 return group_name, entries
140 def groupnameEdit_textChanged(self,a0):
143 def entriesListView_clicked(self,a0):
146 def CheckOKButton(self):
147 group_name = unicode(self.groupnameEdit.text())
149 if not group_name or \
150 (not self.edit_mode and group_name in self.all_groups):
152 self.okButton.setEnabled(False)
155 i = self.entriesListView.firstChild()
164 self.okButton.setEnabled(False)
167 self.okButton.setEnabled(True)
169 def __tr(self,s,c = None):
170 return qApp.translate("FaxAddrBookGroupEditForm",s,c)
173 # **************************************************************************** #
175 class FaxAddrBookGroupsForm(FaxAddrBookGroupsForm_base):
177 def __init__(self,parent = None,name = None,modal = 0,fl = 0):
178 FaxAddrBookGroupsForm_base.__init__(self,parent,name,modal,fl)
180 QTimer.singleShot(0, self.InitialUpdate)
182 def InitialUpdate(self):
185 def UpdateList(self):
186 self.groupListView.clear()
188 all_groups = db.get_all_groups()
191 for group in all_groups:
192 i = QListViewItem(self.groupListView, group,
193 u', '.join(db.group_members(group)))
195 if first_rec is None:
198 self.groupListView.setCurrentItem(i)
201 self.editButton.setEnabled(True)
202 self.deleteButton.setEnabled(True)
205 self.editButton.setEnabled(False)
206 self.deleteButton.setEnabled(False)
208 def newButton_clicked(self):
209 dlg = FaxAddrBookGroupEditForm(self)
211 if dlg.exec_loop() == QDialog.Accepted:
212 group_name, entries = dlg.getDlgData()
213 db.update_groups(group_name, entries)
216 def editButton_clicked(self):
217 dlg = FaxAddrBookGroupEditForm(self)
218 group_name = unicode(self.current.text(0))
219 dlg.setDlgData(group_name)
220 if dlg.exec_loop() == QDialog.Accepted:
221 group_name, entries = dlg.getDlgData()
222 db.update_groups(group_name, entries)
226 def deleteButton_clicked(self):
227 x = QMessageBox.critical(self,
229 self.__tr("<b>Annoying Confirmation: Are you sure you want to delete this group?</b>"),
231 QMessageBox.No | QMessageBox.Default,
232 QMessageBox.NoButton)
233 if x == QMessageBox.Yes:
234 db.delete_group(unicode(self.current.text(0)))
237 def groupListView_currentChanged(self, a0):
240 def groupListView_doubleClicked(self, a0):
241 self.editButton_clicked()
243 def groupListView_rightButtonClicked(self, item, pos, a2):
244 popup = QPopupMenu(self)
246 popup.insertItem(self.__tr("New..."), self.newButton_clicked)
249 popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
250 popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)
252 popup.insertSeparator()
253 popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
256 def __tr(self,s,c = None):
257 return qApp.translate("FaxAddrBookGroupsForm",s,c)
260 # **************************************************************************** #
263 class FaxAddrBookEditForm(FaxAddrBookEditForm_base):
264 def __init__(self, editing=True, parent = None,name = None,modal = 0,fl = 0):
265 FaxAddrBookEditForm_base.__init__(self,parent,name,modal,fl)
266 self.editing = editing
267 self.faxEdit.setValidator(PhoneNumValidator(self.faxEdit))
268 self.initial_nickname = ''
269 self.OKButton.setEnabled(self.editing)
271 def setDlgData(self, name, title, firstname, lastname, fax, group_list, notes):
272 self.initial_nickname = name
274 self.titleEdit.setText(title)
275 self.firstnameEdit.setText(firstname)
276 self.lastnameEdit.setText(lastname)
277 self.faxEdit.setText(fax)
278 self.notesEdit.setText(notes)
279 self.nicknameEdit.setText(name)
280 self.setGroups(group_list)
282 def setGroups(self, entry_groups=[]):
283 self.groupListView.clear()
284 for g in db.get_all_groups():
285 i = QCheckListItem(self.groupListView, g, QCheckListItem.CheckBox)
287 if g in entry_groups:
288 i.setState(QCheckListItem.On)
290 def getDlgData(self):
292 i = self.groupListView.firstChild()
296 in_groups.append(unicode(i.text()))
299 return {'name': unicode(self.nicknameEdit.text()),
300 'title': unicode(self.titleEdit.text()),
301 'firstname': unicode(self.firstnameEdit.text()),
302 'lastname': unicode(self.lastnameEdit.text()),
303 'fax': unicode(self.faxEdit.text()),
305 'notes': unicode(self.notesEdit.text())}
307 def firstnameEdit_textChanged(self,a0):
310 def lastnameEdit_textChanged(self,a0):
313 def nicknameEdit_textChanged(self, nickname):
314 self.CheckOKButton(nickname, None)
316 def faxEdit_textChanged(self, fax):
317 self.CheckOKButton(None, fax)
319 def CheckOKButton(self, nickname=None, fax=None):
321 nickname = unicode(self.nicknameEdit.text())
324 fax = unicode(self.faxEdit.text())
326 ok = bool(len(nickname) and len(fax))
329 all_entries = db.get_all_records()
330 for e, v in all_entries.items():
331 if nickname == e and nickname != self.initial_nickname:
334 self.OKButton.setEnabled(ok)
336 def __tr(self,s,c = None):
337 return qApp.translate("FaxAddrBookEditForm",s,c)
339 # **************************************************************************** #
341 class FaxAddrBookForm(FaxAddrBookForm_base):
342 def __init__(self,parent = None,name = None,modal = 0,fl = 0):
343 FaxAddrBookForm_base.__init__(self,parent,name,modal,fl)
345 self.setIcon(load_pixmap('hp_logo', '128x128'))
348 db = fax.FaxAddressBook()
349 self.init_problem = False
351 QTimer.singleShot(0, self.InitialUpdate)
354 def InitialUpdate(self):
355 if self.init_problem:
361 def UpdateList(self):
362 self.addressListView.clear()
364 all_entries = db.get_all_records()
365 log.debug("Number of records is: %d" % len(all_entries))
368 for e, v in all_entries.items():
370 if v['name'].startswith('__'):
373 i = AddressBookItem2(self.addressListView, v)
375 if first_rec is None:
378 self.addressListView.setCurrentItem(i)
381 self.editButton.setEnabled(True)
382 self.deleteButton.setEnabled(True)
385 self.editButton.setEnabled(False)
386 self.deleteButton.setEnabled(False)
388 def groupButton_clicked(self):
389 FaxAddrBookGroupsForm(self).exec_loop()
390 self.sendUpdateEvent()
393 def newButton_clicked(self):
394 dlg = FaxAddrBookEditForm(False, self)
396 if dlg.exec_loop() == QDialog.Accepted:
399 self.sendUpdateEvent()
402 def editButton_clicked(self):
403 dlg = FaxAddrBookEditForm(True, self)
404 c = self.current.entry
405 dlg.setDlgData(c['name'], c['title'], c['firstname'],
406 c['lastname'], c['fax'], c['groups'], c['notes'])
407 prev_name = c['name']
408 if dlg.exec_loop() == QDialog.Accepted:
411 if prev_name != d['name']:
415 self.sendUpdateEvent()
419 def deleteButton_clicked(self):
420 if QMessageBox.critical(self,
422 self.__tr("<b>Annoying Confirmation: Are you sure you want to delete this address book entry?</b>"),
424 QMessageBox.No | QMessageBox.Default,
425 QMessageBox.NoButton) == QMessageBox.Yes:
426 db.delete(self.current.entry['name'])
428 self.sendUpdateEvent()
431 def addressListView_rightButtonClicked(self, item, pos, a2):
432 popup = QPopupMenu(self)
433 popup.insertItem(self.__tr("New..."), self.newButton_clicked)
435 popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
436 popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)
438 popup.insertSeparator()
439 popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
442 def addressListView_doubleClicked(self,a0):
443 self.editButton_clicked()
445 def addressListView_currentChanged(self,item):
448 def FailureUI(self, error_text):
449 log.error(unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
450 QMessageBox.critical(self,
454 QMessageBox.NoButton,
455 QMessageBox.NoButton)
457 def __tr(self,s,c = None):
458 return qApp.translate("FaxAddrBookForm",s,c)
461 self.sendUpdateEvent()
463 FaxAddrBookForm_base.accept(self)
465 def sendUpdateEvent(self):
468 def importPushButton_clicked(self):
469 dlg = QFileDialog(user_conf.workingDirectory(), "LDIF (*.ldif *.ldi);;vCard (*.vcf)", None, None, True)
471 dlg.setCaption("openfile")
472 dlg.setMode(QFileDialog.ExistingFile)
475 if dlg.exec_loop() == QDialog.Accepted:
476 result = str(dlg.selectedFile())
477 working_directory = unicode(dlg.dir().absPath())
478 log.debug("result: %s" % result)
479 user_conf.setWorkingDirectory(working_directory)
482 if result.endswith('.vcf'):
483 ok, error_str = db.import_vcard(result)
485 ok, error_str = db.import_ldif(result)
488 self.FailureUI(error_str)