def __init__(self, addressbook=None, addressbookimages=None, work_dir=None):
        QtGui.QMainWindow.__init__(self)

        self.name = "contactsGUI"
        
        if work_dir is not None:
            self.work_dir = work_dir
        else:
            self.work_dir = makeTmpDir()

        self.ui = Ui_frmContacts()
        self.ui.setupUi(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        QtCore.QObject.connect(self.ui.treeWidgetContacts, QtCore.SIGNAL("itemSelectionChanged()"), self.onTreeClick)
        QtCore.QObject.connect(self.ui.actionReport, QtCore.SIGNAL("triggered(bool)"), self.printReport)
        QtCore.QObject.connect(self.ui.actionExtract, QtCore.SIGNAL("triggered(bool)"), self.extractApp)

        self.ui.treeWidgetContacts.setHeaderLabel('Contacts')
        self.ui.treeWidgetContacts.setColumnHidden(1, False)
        self.ui.treeWidgetContacts.setColumnHidden(2, False)
        self.ui.treeWidgetContacts.setColumnHidden(3, False)
        self.ui.treeWidgetContacts.setColumnHidden(4, False)
        self.ui.treeWidgetContacts.setColumnHidden(5, False)
        self.ui.treeWidgetContacts.setColumnHidden(6, False)
        self.ui.treeWidgetContacts.setColumnHidden(7, False)
        self.ui.treeWidgetContacts.setColumnHidden(8, False)
        self.ui.treeWidgetContacts.setColumnHidden(9, False)

        self.address_book_list = get_addressbook(addressbook, addressbookimages)
        for contact in self.address_book_list:
            name = ""
            if contact.last_name != "" and contact.last_name is not None:
                name = contact.last_name
            if contact.first_name != "" and contact.first_name is not None:
                if name is not "":
                    name = name + ", " + contact.first_name
                else:
                    name = contact.first_name
            if name == "":
                name = contact.organization

            newItem = QtGui.QTreeWidgetItem(None)
            newItem.setText(0, name)
            newItem.setText(1, contact.rowid)
            newItem.setText(2, contact.last_name)
            newItem.setText(3, contact.first_name)
            newItem.setText(4, contact.organization)
            newItem.setText(5, str(contact.creation_date))
            newItem.setText(6, str(contact.modification_date))
            newItem.setText(7, str(contact.birthday))
            newItem.setText(8, contact.image_url)
            newItem.setText(9, str(contact.multivalue))
            self.ui.treeWidgetContacts.addTopLevelItem(newItem)
class contactsGUI(QtGui.QMainWindow):
    def __init__(self, addressbook=None, addressbookimages=None, work_dir=None):
        QtGui.QMainWindow.__init__(self)

        self.name = "contactsGUI"
        
        if work_dir is not None:
            self.work_dir = work_dir
        else:
            self.work_dir = makeTmpDir()

        self.ui = Ui_frmContacts()
        self.ui.setupUi(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        QtCore.QObject.connect(self.ui.treeWidgetContacts, QtCore.SIGNAL("itemSelectionChanged()"), self.onTreeClick)
        QtCore.QObject.connect(self.ui.actionReport, QtCore.SIGNAL("triggered(bool)"), self.printReport)
        QtCore.QObject.connect(self.ui.actionExtract, QtCore.SIGNAL("triggered(bool)"), self.extractApp)

        self.ui.treeWidgetContacts.setHeaderLabel('Contacts')
        self.ui.treeWidgetContacts.setColumnHidden(1, False)
        self.ui.treeWidgetContacts.setColumnHidden(2, False)
        self.ui.treeWidgetContacts.setColumnHidden(3, False)
        self.ui.treeWidgetContacts.setColumnHidden(4, False)
        self.ui.treeWidgetContacts.setColumnHidden(5, False)
        self.ui.treeWidgetContacts.setColumnHidden(6, False)
        self.ui.treeWidgetContacts.setColumnHidden(7, False)
        self.ui.treeWidgetContacts.setColumnHidden(8, False)
        self.ui.treeWidgetContacts.setColumnHidden(9, False)

        self.address_book_list = get_addressbook(addressbook, addressbookimages)
        for contact in self.address_book_list:
            name = ""
            if contact.last_name != "" and contact.last_name is not None:
                name = contact.last_name
            if contact.first_name != "" and contact.first_name is not None:
                if name is not "":
                    name = name + ", " + contact.first_name
                else:
                    name = contact.first_name
            if name == "":
                name = contact.organization

            newItem = QtGui.QTreeWidgetItem(None)
            newItem.setText(0, name)
            newItem.setText(1, contact.rowid)
            newItem.setText(2, contact.last_name)
            newItem.setText(3, contact.first_name)
            newItem.setText(4, contact.organization)
            newItem.setText(5, str(contact.creation_date))
            newItem.setText(6, str(contact.modification_date))
            newItem.setText(7, str(contact.birthday))
            newItem.setText(8, contact.image_url)
            newItem.setText(9, str(contact.multivalue))
            self.ui.treeWidgetContacts.addTopLevelItem(newItem)

    def onTreeClick(self):

        self.ui.listWidgetMulitvalue.clear()
        self.ui.lblLastname.setText('')
        self.ui.lblFirstname.setText('')
        self.ui.lblOrganization.setText('')
        self.ui.lblPicture.setPixmap(None)

        selectedItem = self.ui.treeWidgetContacts.currentItem()

        person = self.getPerson(selectedItem.text(1))

        if person is None:
            return

        self.ui.lblLastname.setText(person.last_name)
        self.ui.lblFirstname.setText(person.first_name)
        self.ui.lblOrganization.setText(person.organization)

        for value in person.multivalue:
            if value != "" and value is not None:
                item = QtGui.QListWidgetItem()
                item.setText(value)
                if person.isMailAddress(value):
                    item.setIcon(QtGui.QIcon('data/img/mail.png'))
                elif person.isURLAddress(value):
                    item.setIcon(QtGui.QIcon('data/img/web.png'))
                elif person.isPhoneNumber(value):
                    item.setIcon(QtGui.QIcon('data/img/phone.png'))

                self.ui.listWidgetMulitvalue.addItem(item)

        if person.has_image:
            pixmap = QtGui.QPixmap(person.image_url)
            self.ui.lblPicture.setPixmap(pixmap)
            self.ui.lblPicture.setScaledContents(True)

    def getPerson(self, rowid):

        for person in self.address_book_list:
            if person.rowid == rowid:
                return person
        return None

    def extractApp(self):
        pass

    def printReport(self):

        path = expanduser("~")
        outdir = QtGui.QFileDialog.getExistingDirectory(self, "Open Directory", path,
                                                            QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks);

        if (outdir == None) or (len(outdir) == 0):
            return None

        outfile = outdir + "/AB-Report"
        printHTMLReport(outfile, self.address_book_list)