Example #1
0
Media::TextRecording* Media::TextRecording::fromJson(const QList<QJsonObject>& items, const ContactMethod* cm)
{
    TextRecording* t = new TextRecording();

    //Load the history data
    for (const QJsonObject& obj : items) {
        Serializable::Peers* p = SerializableEntityManager::fromJson(obj,cm);
        t->d_ptr->m_lAssociatedPeers << p;
    }

    //Create the model
    t->instantMessagingModel();

    //Reconstruct the conversation
    //TODO do it right, right now it flatten the graph
    for (const Serializable::Peers* p : t->d_ptr->m_lAssociatedPeers) {
        for (const Serializable::Group* g : p->groups) {
            for (Serializable::Message* m : g->messages) {
                ::TextMessageNode* n  = new ::TextMessageNode();
                n->m_pMessage         = m                      ;
                if (!n->m_pMessage->contactMethod) {
                    n->m_pMessage->contactMethod = const_cast<ContactMethod*>(cm); //TODO remove in 2016
                    n->m_pMessage->authorSha1 = cm->sha1();

                    if (p->peers.isEmpty())
                        addPeer(const_cast<Serializable::Peers*>(p), cm);
                }
                n->m_pContactMethod   = m->contactMethod;
                t->d_ptr->m_pImModel->addRowBegin();
                t->d_ptr->m_lNodes << n;
                t->d_ptr->m_pImModel->addRowEnd();
            }
        }
    }

    return t;
}
Media::TextRecording* Media::TextRecording::fromJson(const QList<QJsonObject>& items, const ContactMethod* cm, CollectionInterface* backend)
{
    TextRecording* t = new TextRecording();
    if (backend)
        t->setCollection(backend);

    ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance();

    //Load the history data
    for (const QJsonObject& obj : items) {
        Serializable::Peers* p = SerializableEntityManager::fromJson(obj,cm);
        t->d_ptr->m_lAssociatedPeers << p;
    }

    //Create the model
    bool statusChanged = false; // if a msg status changed during parsing, we need to re-save the model
    t->instantMessagingModel();

    //Reconstruct the conversation
    //TODO do it right, right now it flatten the graph
    for (const Serializable::Peers* p : t->d_ptr->m_lAssociatedPeers) {
        //Seems old version didn't store that
        if (p->peers.isEmpty())
            continue;
        // TODO: for now assume the convo is with only 1 CM at a time
        auto peerCM = p->peers.at(0)->m_pContactMethod;

        // get the latest timestamp to set last used
        time_t lastUsed = 0;
        for (const Serializable::Group* g : p->groups) {
            for (Serializable::Message* m : g->messages) {
                ::TextMessageNode* n  = new ::TextMessageNode();
                n->m_pMessage         = m                      ;
                if (!n->m_pMessage->contactMethod) {
                    if (cm) {
                        n->m_pMessage->contactMethod = const_cast<ContactMethod*>(cm); //TODO remove in 2016
                        n->m_pMessage->authorSha1 = cm->sha1();

                        if (p->peers.isEmpty())
                            addPeer(const_cast<Serializable::Peers*>(p), cm);
                    } else {
                        if (p->m_hSha1.contains(n->m_pMessage->authorSha1)) {
                            n->m_pMessage->contactMethod = p->m_hSha1[n->m_pMessage->authorSha1];
                        } else {
                            // message was outgoing and author sha1 was set to that of the sending account
                            n->m_pMessage->contactMethod = peerCM;
                            n->m_pMessage->authorSha1 = peerCM->sha1();
                        }
                    }
                }
                n->m_pContactMethod   = m->contactMethod;
                t->d_ptr->m_pImModel->addRowBegin();
                t->d_ptr->m_lNodes << n;
                t->d_ptr->m_pImModel->addRowEnd();

                if (lastUsed < n->m_pMessage->timestamp)
                    lastUsed = n->m_pMessage->timestamp;
                if (m->id) {
                    int status = configurationManager.getMessageStatus(m->id);
                    t->d_ptr->m_hPendingMessages[m->id] = n;
                    if (t->d_ptr->updateMessageStatus(m, static_cast<TextRecording::Status>(status)))
                        statusChanged = true;
                }
            }
        }

        if (statusChanged)
            t->save();

        // update the timestamp of the CM
        peerCM->setLastUsed(lastUsed);
    }

    return t;
}