Exemplo n.º 1
0
void GetMsg::finished(QNetworkReply* reply) 
{
    QString replyStr = QString(reply->readAll());
#if QWX_DEBUG
    QFile file("getmsg.json");
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream out(&file);
        out << replyStr;
        file.close();
    }
    qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
    qDebug() << "DEBUG:" << replyStr;
#endif
    QJsonDocument doc = QJsonDocument::fromJson(replyStr.toUtf8());
    if (!doc.isObject()) { Q_EMIT error(); return; }
    QJsonObject obj = doc.object();
    if (obj["AddMsgCount"].toInt() == 0)
        Q_EMIT noNewMsg();

    Q_FOREACH (const QJsonValue & val, obj["AddMsgList"].toArray()) {
        QJsonObject msg = val.toObject();
        QString fromUserNameStr = msg["FromUserName"].toString();
        QString toUserNameStr = msg["ToUserName"].toString();
        int createTime = msg["CreateTime"].toInt();
        QString content = msg["Content"].toString();
        QString msgId = msg["MsgId"].toString();
        int msgType = msg["MsgType"].toInt();

        if (content.contains("msg")) {
            content = "";
            if (msgType == 3) {
                QString url = m_v2 ? WX_V2_SERVER_HOST : WX_SERVER_HOST +
                    WX_CGI_PATH + "webwxgetmsgimg?MsgID=" + msgId + "&skey=" +
                    m_skey;
                QString msgImgPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/img_" + msgId + ".jpg";
                Download *downLoad = new Download;
                downLoad->get(url, msgImgPath, true, false);
                connect(downLoad, &Download::finished, [=] {
                    content = "<img src=\"file://" + msgImgPath +
                        "\" width=\"128\" height=\"128\">";
                    m_handleNewMsg(msgId, content, fromUserNameStr,
                        toUserNameStr, time(nullptr));
                    downLoad->deleteLater();
                });
            } else if (msgType == 34) {
                QString url = m_v2 ? WX_V2_SERVER_HOST : WX_SERVER_HOST +
                    WX_CGI_PATH + "webwxgetvoice?msgid=" + msgId + "&skey=" +
                    m_skey;
                QString msgVoicePath = QStandardPaths::writableLocation(QStandardPaths::MusicLocation) + "/voice_" + msgId + ".mp3";
                Download *downLoad = new Download;
                downLoad->get(url, msgVoicePath, true, false);
                connect(downLoad, &Download::finished, [=] {
                    content = "<a href=\"file://" + msgVoicePath + "\">" +
                        tr("Voice") + "</a>";
                    m_handleNewMsg(msgId, content, fromUserNameStr,
                        toUserNameStr, time(nullptr));
                    downLoad->deleteLater();
                });
            } else if (msgType == 62) {
                QString url = m_v2 ? WX_V2_SERVER_HOST : WX_SERVER_HOST +
                    WX_CGI_PATH + "webwxgetvideo?msgid=" + msgId + "&skey=" +
                    m_skey;
                QString msgVideoPath = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation) + "/video_" + msgId + ".mp4";
                Download *downLoad = new Download;
                downLoad->get(url, msgVideoPath, true, false);
                connect(downLoad, &Download::finished, [=] {
                    content = "<a href=\"file://" + msgVideoPath + "\">" +
                        tr("Video") + "</a>";
                    m_handleNewMsg(msgId, content, fromUserNameStr,
                        toUserNameStr, time(nullptr));
                    downLoad->deleteLater();
                });
            } else if (msgType == 49) {
                content = tr("Please view it on your phone") + " <a href=\"" +
                    msg["Url"].toString() + "\">" + msg["FileName"].toString() +
                    "</a>";
            } else if (msgType == 10002) {
                content = tr("Withdraw a message");
            } else if (msgType == 51) {
                // TODO: you are tapping on your phone ;-)
            } else {
                content = tr("Unsupport MsgType %1").arg(msgType);
            }
        }

        m_handleNewMsg(msgId, content, fromUserNameStr, toUserNameStr, createTime);
    }
    
    m_syncKey.clear();
    Q_FOREACH (const QJsonValue & val, obj["SyncKey"].toObject()["List"].toArray()) {
        m_syncKey.append(QString::number(val.toObject()["Key"].toInt()) + "|" + 
                QString::number(val.toObject()["Val"].toInt()));
    }
    Q_EMIT syncKeyChanged();
}