summaryrefslogtreecommitdiff
path: root/ui/faxaddrbookform.py
blob: 38b03006dbb090eeff3acee7ed6c99d064fb1ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
#

# Std Lib
import sys
import os
import os.path

# Local
from base.g import *
from base import utils
from ui_utils import load_pixmap

try:
    from fax import fax
except ImportError:
    # This can fail on Python < 2.3 due to the datetime module
    log.error("Fax address book disabled - Python 2.3+ required.")
    sys.exit(1)

# Qt
from qt import *
from faxaddrbookform_base import FaxAddrBookForm_base
from faxaddrbookeditform_base import FaxAddrBookEditForm_base
from faxaddrbookgroupsform_base import FaxAddrBookGroupsForm_base
from faxaddrbookgroupeditform_base import FaxAddrBookGroupEditForm_base

# globals
db = None

# ****************************************************************************

class AddressBookItem2(QListViewItem):

    def __init__(self, parent, entry):
        QListViewItem.__init__(self, parent)
        self.entry = entry
        self.setText(0, entry['name'])
        self.setText(1, entry['title'])
        self.setText(2, entry['firstname'])
        self.setText(3, entry['lastname'])
        self.setText(4, entry['fax'])
        self.setText(5, ', '.join(entry['groups']))
        self.setText(6, entry['notes'])

class GroupValidator(QValidator):
    def __init__(self, parent=None, name=None):
        QValidator.__init__(self, parent, name)

    def validate(self, input, pos):
        input = unicode(input)
        if input.find(u',') > 0:
            return QValidator.Invalid, pos
        elif len(input) > 50:
            return QValidator.Invalid, pos
        else:
            return QValidator.Acceptable, pos


class PhoneNumValidator(QValidator):
    def __init__(self, parent=None, name=None):
        QValidator.__init__(self, parent, name)

    def validate(self, input, pos):
        input = unicode(input)
        if not input:
            return QValidator.Acceptable, pos
        elif input[pos-1] not in u'0123456789-(+) *#':
            return QValidator.Invalid, pos
        elif len(input) > 50:
            return QValidator.Invalid, pos
        else:
            return QValidator.Acceptable, pos


# **************************************************************************** #

class FaxAddrBookGroupEditForm(FaxAddrBookGroupEditForm_base):
    """
        Called when clicking New... or Edit... from the Group Dialog
    """
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        FaxAddrBookGroupEditForm_base.__init__(self,parent,name,modal,fl)
        self.edit_mode = False
        self.okButton.setEnabled(True)
        self.all_groups = db.get_all_groups()
        self.groupnameEdit.setValidator(GroupValidator(self.groupnameEdit))

    def setDlgData(self, group_name):
        self.edit_mode = True
        self.groupnameEdit.setText(group_name)
        self.groupnameEdit.setReadOnly(True)
        self.setEntries(group_name)

    def setEntries(self, group_name=''):
        self.entriesListView.clear()
        all_entries = db.get_all_records()

        for e, v in all_entries.items():
            i = QCheckListItem(self.entriesListView, e, QCheckListItem.CheckBox)

            if group_name and group_name in v['groups']:
                i.setState(QCheckListItem.On)

        self.CheckOKButton()


    def getDlgData(self):
        group_name = unicode(self.groupnameEdit.text())
        entries = []

        i = self.entriesListView.firstChild()

        while i is not None:
            if i.isOn():
                entries.append(unicode(i.text()))

            i = i.itemBelow()

        return group_name, entries

    def groupnameEdit_textChanged(self,a0):
        self.CheckOKButton()

    def entriesListView_clicked(self,a0):
        self.CheckOKButton()

    def CheckOKButton(self):
        group_name = unicode(self.groupnameEdit.text())

        if not group_name or \
            (not self.edit_mode and group_name in self.all_groups):

            self.okButton.setEnabled(False)
            return

        i = self.entriesListView.firstChild()

        while i is not None:
            if i.isOn():
                break

            i = i.itemBelow()

        else:
            self.okButton.setEnabled(False)
            return

        self.okButton.setEnabled(True)

    def __tr(self,s,c = None):
        return qApp.translate("FaxAddrBookGroupEditForm",s,c)


# **************************************************************************** #

class FaxAddrBookGroupsForm(FaxAddrBookGroupsForm_base):

    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        FaxAddrBookGroupsForm_base.__init__(self,parent,name,modal,fl)
        self.current = None
        QTimer.singleShot(0, self.InitialUpdate)

    def InitialUpdate(self):
        self.UpdateList()

    def UpdateList(self):
        self.groupListView.clear()
        first_rec = None
        all_groups = db.get_all_groups()
        if all_groups:

            for group in all_groups:
                i = QListViewItem(self.groupListView, group,
                                  u', '.join(db.group_members(group)))

                if first_rec is None:
                    first_rec = i

            self.groupListView.setCurrentItem(i)
            self.current = i

            self.editButton.setEnabled(True)
            self.deleteButton.setEnabled(True)

        else:
            self.editButton.setEnabled(False)
            self.deleteButton.setEnabled(False)

    def newButton_clicked(self):
        dlg = FaxAddrBookGroupEditForm(self)
        dlg.setEntries()
        if dlg.exec_loop() == QDialog.Accepted:
            group_name, entries = dlg.getDlgData()
            db.update_groups(group_name, entries)
            self.UpdateList()

    def editButton_clicked(self):
        dlg = FaxAddrBookGroupEditForm(self)
        group_name = unicode(self.current.text(0))
        dlg.setDlgData(group_name)
        if dlg.exec_loop() == QDialog.Accepted:
            group_name, entries = dlg.getDlgData()
            db.update_groups(group_name, entries)
            self.UpdateList()


    def deleteButton_clicked(self):
        x = QMessageBox.critical(self,
                                 self.caption(),
                                 self.__tr("<b>Annoying Confirmation: Are you sure you want to delete this group?</b>"),
                                  QMessageBox.Yes,
                                  QMessageBox.No | QMessageBox.Default,
                                  QMessageBox.NoButton)
        if x == QMessageBox.Yes:
            db.delete_group(unicode(self.current.text(0)))
            self.UpdateList()

    def groupListView_currentChanged(self, a0):
        self.current = a0

    def groupListView_doubleClicked(self, a0):
        self.editButton_clicked()

    def groupListView_rightButtonClicked(self, item, pos, a2):
        popup = QPopupMenu(self)

        popup.insertItem(self.__tr("New..."), self.newButton_clicked)

        if item is not None:
            popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
            popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)

        popup.insertSeparator()
        popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
        popup.popup(pos)

    def __tr(self,s,c = None):
        return qApp.translate("FaxAddrBookGroupsForm",s,c)


# **************************************************************************** #


class FaxAddrBookEditForm(FaxAddrBookEditForm_base):
    def __init__(self, editing=True, parent = None,name = None,modal = 0,fl = 0):
        FaxAddrBookEditForm_base.__init__(self,parent,name,modal,fl)
        self.editing = editing
        self.faxEdit.setValidator(PhoneNumValidator(self.faxEdit))
        self.initial_nickname = ''
        self.OKButton.setEnabled(self.editing)

    def setDlgData(self, name, title, firstname, lastname, fax, group_list, notes):
        self.initial_nickname = name
        self.name = name
        self.titleEdit.setText(title)
        self.firstnameEdit.setText(firstname)
        self.lastnameEdit.setText(lastname)
        self.faxEdit.setText(fax)
        self.notesEdit.setText(notes)
        self.nicknameEdit.setText(name)
        self.setGroups(group_list)

    def setGroups(self, entry_groups=[]):
        self.groupListView.clear()
        for g in db.get_all_groups():
            i = QCheckListItem(self.groupListView, g, QCheckListItem.CheckBox)

            if g in entry_groups:
                i.setState(QCheckListItem.On)

    def getDlgData(self):
        in_groups = []
        i = self.groupListView.firstChild()

        while i is not None:
            if i.isOn():
                in_groups.append(unicode(i.text()))
            i = i.itemBelow()

        return {'name': unicode(self.nicknameEdit.text()),
                'title': unicode(self.titleEdit.text()),
                'firstname': unicode(self.firstnameEdit.text()),
                'lastname': unicode(self.lastnameEdit.text()),
                'fax': unicode(self.faxEdit.text()),
                'groups': in_groups,
                'notes': unicode(self.notesEdit.text())}

    def firstnameEdit_textChanged(self,a0):
        pass

    def lastnameEdit_textChanged(self,a0):
        pass

    def nicknameEdit_textChanged(self, nickname):
        self.CheckOKButton(nickname, None)

    def faxEdit_textChanged(self, fax):
        self.CheckOKButton(None, fax)

    def CheckOKButton(self, nickname=None, fax=None):
        if nickname is None:
            nickname = unicode(self.nicknameEdit.text())

        if fax is None:
            fax = unicode(self.faxEdit.text())

        ok = bool(len(nickname) and len(fax))

        if nickname:
            all_entries = db.get_all_records()
            for e, v in all_entries.items():
                if nickname == e and nickname != self.initial_nickname:
                    ok = False

        self.OKButton.setEnabled(ok)

    def __tr(self,s,c = None):
        return qApp.translate("FaxAddrBookEditForm",s,c)

# **************************************************************************** #

class FaxAddrBookForm(FaxAddrBookForm_base):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        FaxAddrBookForm_base.__init__(self,parent,name,modal,fl)

        self.setIcon(load_pixmap('hp_logo', '128x128'))

        global db
        db =  fax.FaxAddressBook()
        self.init_problem = False

        QTimer.singleShot(0, self.InitialUpdate)


    def InitialUpdate(self):
        if self.init_problem:
            self.close()
            return

        self.UpdateList()

    def UpdateList(self):
        self.addressListView.clear()
        first_rec = None
        all_entries = db.get_all_records()
        log.debug("Number of records is: %d" % len(all_entries))

        if all_entries:
            for e, v in all_entries.items():

                if v['name'].startswith('__'):
                    continue

                i = AddressBookItem2(self.addressListView, v)

                if first_rec is None:
                    first_rec = i

            self.addressListView.setCurrentItem(i)
            self.current = i

            self.editButton.setEnabled(True)
            self.deleteButton.setEnabled(True)

        else:
            self.editButton.setEnabled(False)
            self.deleteButton.setEnabled(False)

    def groupButton_clicked(self):
        FaxAddrBookGroupsForm(self).exec_loop()
        self.sendUpdateEvent()
        self.UpdateList()

    def newButton_clicked(self):
        dlg = FaxAddrBookEditForm(False, self)
        dlg.setGroups()
        if dlg.exec_loop() == QDialog.Accepted:
            d = dlg.getDlgData()
            db.set(**d)
            self.sendUpdateEvent()
            self.UpdateList()

    def editButton_clicked(self):
        dlg = FaxAddrBookEditForm(True, self)
        c = self.current.entry
        dlg.setDlgData(c['name'], c['title'], c['firstname'],
            c['lastname'], c['fax'], c['groups'], c['notes'])
        prev_name = c['name']
        if dlg.exec_loop() == QDialog.Accepted:
            d = dlg.getDlgData()

            if prev_name != d['name']:
                db.delete(prev_name)

            db.set(**d)
            self.sendUpdateEvent()
            self.UpdateList()


    def deleteButton_clicked(self):
        if QMessageBox.critical(self,
             self.caption(),
             self.__tr("<b>Annoying Confirmation: Are you sure you want to delete this address book entry?</b>"),
              QMessageBox.Yes,
              QMessageBox.No | QMessageBox.Default,
              QMessageBox.NoButton) == QMessageBox.Yes:
            db.delete(self.current.entry['name'])
            self.UpdateList()
            self.sendUpdateEvent()


    def addressListView_rightButtonClicked(self, item, pos, a2):
        popup = QPopupMenu(self)
        popup.insertItem(self.__tr("New..."), self.newButton_clicked)
        if item is not None:
            popup.insertItem(self.__tr("Edit..."), self.editButton_clicked)
            popup.insertItem(self.__tr("Delete..."), self.deleteButton_clicked)

        popup.insertSeparator()
        popup.insertItem(self.__tr("Refresh List"), self.UpdateList)
        popup.popup(pos)

    def addressListView_doubleClicked(self,a0):
        self.editButton_clicked()

    def addressListView_currentChanged(self,item):
        self.current = item

    def FailureUI(self, error_text):
        log.error(unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
        QMessageBox.critical(self,
                             self.caption(),
                             QString(error_text),
                              QMessageBox.Ok,
                              QMessageBox.NoButton,
                              QMessageBox.NoButton)

    def __tr(self,s,c = None):
        return qApp.translate("FaxAddrBookForm",s,c)

    def accept(self):
        self.sendUpdateEvent()

        FaxAddrBookForm_base.accept(self)

    def sendUpdateEvent(self):
        pass # TODO:

    def importPushButton_clicked(self):
        dlg = QFileDialog(user_conf.workingDirectory(), "LDIF (*.ldif *.ldi);;vCard (*.vcf)", None, None, True)

        dlg.setCaption("openfile")
        dlg.setMode(QFileDialog.ExistingFile)
        dlg.show()

        if dlg.exec_loop() == QDialog.Accepted:
                result = str(dlg.selectedFile())
                working_directory = unicode(dlg.dir().absPath())
                log.debug("result: %s" % result)
                user_conf.setWorkingDirectory(working_directory)

                if result:
                    if result.endswith('.vcf'):
                        ok, error_str = db.import_vcard(result)
                    else:
                        ok, error_str = db.import_ldif(result)

                    if not ok:
                        self.FailureUI(error_str)

                    else:
                        self.UpdateList()