Beispiel #1
0
 def __init__(self, store):
     super(WAContacts, self).__init__()
     self.store = store
     self.contacts = []
     self.raw_contacts = None
     self.manager = ContactsManager()
     self.imageProcessor = WAImageProcessor()
Beispiel #2
0
	def __init__(self,store):
		super(WAContacts,self).__init__();
		self.store = store;
		self.contacts = [];
		self.raw_contacts = None;
		self.manager = ContactsManager();
		self.imageProcessor = WAImageProcessor();
		
		self.syncer = ContactsSyncer(self.store, self, "SYNC");
		
		self.syncer.contactsRefreshSuccess.connect(self.contactsRefreshed);
		self.syncer.contactsRefreshFail.connect(self.contactsRefreshFailed);
		self.syncer.contactsSyncStatus.connect(self.contactsSyncStatusChanged);
Beispiel #3
0
class WAContacts(QObject):

    refreshing = QtCore.Signal()
    contactsRefreshed = QtCore.Signal(str, dict)
    contactsRefreshFailed = QtCore.Signal()
    contactsSyncStatusChanged = QtCore.Signal(str)
    contactUpdated = QtCore.Signal(str)
    contactPictureUpdated = QtCore.Signal(str)
    contactAdded = QtCore.Signal(str)
    contactExported = QtCore.Signal(str, str)

    def __init__(self, store):
        super(WAContacts, self).__init__()
        self.store = store
        self.contacts = []
        self.raw_contacts = None
        self.manager = ContactsManager()
        self.imageProcessor = WAImageProcessor()

    def initiateSyncer(self, mode, userid):
        self.syncer = ContactsSyncer(self.store, mode, userid)
        # self.syncer.done.connect(self.syncer.updateContacts);
        self.syncer.contactsRefreshSuccess.connect(self.contactsRefreshed)
        self.syncer.contactsRefreshFail.connect(self.contactsRefreshFailed)
        self.syncer.contactsSyncStatus.connect(self.contactsSyncStatusChanged)

    def resync(self, mode, userid=None):
        self.initiateSyncer(mode, userid)
        self.refreshing.emit()
        self.syncer.start()

    def updateContact(self, jid):
        # if "@g.us" in jid:
        # 	user_img = QImage("/opt/waxmppplugin/bin/wazapp/UI/common/images/group.png")
        # else:
        # 	user_img = QImage("/opt/waxmppplugin/bin/wazapp/UI/common/images/user.png")

        jname = jid.replace("@s.whatsapp.net", "").replace("@g.us", "")
        if os.path.isfile(WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg"):
            user_img = QImage(WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg")

            user_img.save(WAConstants.CACHE_PROFILE + "/" + jname + ".jpg", "JPEG")

            self.imageProcessor.createSquircle(
                WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg", WAConstants.CACHE_CONTACTS + "/" + jname + ".png"
            )
            self.contactPictureUpdated.emit(jid)

    def checkPicture(self, jname, sourcePath):

        sourcePath = str(sourcePath)
        if os.path.isfile(WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg"):
            # Don't overwrite if profile picture exists
            if os.path.isfile(WAConstants.CACHE_PROFILE + "/" + jname + ".jpg"):
                return
            user_img = WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg"
        else:
            if os.path.isfile(WAConstants.CACHE_PROFILE + "/" + jname + ".jpg"):
                os.remove(WAConstants.CACHE_PROFILE + "/" + jname + ".jpg")
            user_img = sourcePath.replace("file://", "")

        self.imageProcessor.createSquircle(user_img, WAConstants.CACHE_CONTACTS + "/" + jname + ".png")

        if os.path.isfile(WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg"):
            os.remove(WAConstants.CACHE_CONTACTS + "/" + jname + ".jpg")

    def getContacts(self):
        contacts = self.store.Contact.fetchAll()
        if len(contacts) == 0:
            print "NO CONTACTS FOUNDED IN DATABASE"
            # self.resync();
            return contacts
            # O(n2) matching, need to change that
        cm = self.manager
        phoneContacts = cm.getContacts()
        tmp = []
        self.contacts = {}

        for wc in contacts:
            jname = wc.jid.replace("@s.whatsapp.net", "")
            founded = False
            myname = ""
            picturePath = WAConstants.CACHE_CONTACTS + "/" + jname + ".png"
            for c in phoneContacts:
                if wc.number[-8:] == c["number"][-8:]:
                    founded = True
                    if c["picture"]:
                        self.checkPicture(jname, c["picture"] if type(c["picture"]) == str else c["picture"].toString())

                    c["picture"] = picturePath if os.path.isfile(picturePath) else None
                    myname = c["name"]
                    wc.setRealTimeData(myname, c["picture"], "yes")
                    QtCore.QCoreApplication.processEvents()
                    break

            if founded is False and wc.number is not None:
                # self.checkPicture(jname,"")
                myname = wc.pushname.decode("utf8") if wc.pushname is not None else ""
                mypicture = picturePath if os.path.isfile(picturePath) else None
                wc.setRealTimeData(myname, mypicture, "no")

            if wc.status is not None:
                wc.status = wc.status.decode("utf-8")
            if wc.pushname is not None:
                wc.pushname = wc.pushname.decode("utf-8")

            if wc.name is not "" and wc.name is not None:
                # print "ADDING CONTACT : " + myname
                tmp.append(wc.getModelData())
                self.contacts[wc.number] = wc

        if len(tmp) == 0:
            print "NO CONTACTS ADDED!"
            return []

        print "TOTAL CONTACTS ADDED FROM DATABASE: " + str(len(tmp))
        self.store.cacheContacts(self.contacts)
        return sorted(tmp, key=lambda k: k["name"].upper())

    def getPhoneContacts(self):
        cm = self.manager
        phoneContacts = cm.getPhoneContacts()
        tmp = []

        for c in phoneContacts:
            wc = []
            c["picture"] = QUrl(c["picture"]).toString().replace("file://", "")
            wc.append(c["name"])
            # wc.append(c['id'])
            wc.append(c["picture"])
            wc.append(c["numbers"])
            if len(c["numbers"]) > 0:
                tmp.append(wc)
        return sorted(tmp)

    def exportContact(self, jid, name):
        cm = self.manager
        phoneContacts = cm.getQtContacts()
        contacts = []

        for c in phoneContacts:
            if name == c.displayLabel():
                if os.path.isfile(WAConstants.CACHE_CONTACTS + "/" + name + ".vcf"):
                    os.remove(WAConstants.CACHE_CONTACTS + "/" + name + ".vcf")
                print "founded contact: " + c.displayLabel()
                contacts.append(c)
                openfile = QFile(WAConstants.VCARD_PATH + "/" + name + ".vcf")
                openfile.open(QIODevice.WriteOnly)
                if openfile.isWritable():
                    exporter = QVersitContactExporter()
                    if exporter.exportContacts(contacts, QVersitDocument.VCard30Type):
                        documents = exporter.documents()
                        writer = QVersitWriter()
                        writer.setDevice(openfile)
                        writer.startWriting(documents)
                        writer.waitForFinished()
                openfile.close()
                self.contactExported.emit(jid, name)
                break