Example #1
0
int VKontakte::loadHistory(QString idUser) // Загрузка истории переписки //
{
    if( checkAccessToken() == true )
    {
        QUrlQuery request("https://api.vk.com/method/messages.getHistory?access_token=" + access_token);
        request.addQueryItem("user_id", idUser);

        request.addQueryItem("count","50");

        request.addQueryItem("version", "5.37");


        QString urlString = request.toString();
        QUrl url(urlString);
        QByteArray answer = GET(url);


        if(answer.isEmpty())
        {
            qDebug() << "Пустой ответ в VKonatkte::loadHistory";
            return 0x000002;
        }

        QVariantList messageList = parse(answer).toMap().value("response").toList();
        history.clear();

        for(int i = 0; i < messageList.size(); i++)
        {

            QVariantMap message = messageList[i].toMap();

            QString id = message.value("id").toString();
            QString from_id = message.value("from_id").toString();
            QString text_message = message.value("body").toString();
            QString date = message.value("date").toString();
            QString state = message.value("read_state").toString();

            QList< QPair<QString, Attachment> > listAttachments;
            QVariantList attachments = message.value("attachments").toList();

            for(int j = 0; j < attachments.size(); j++)
            {
                QVariantMap attachment = attachments[j].toMap();

                QString type = attachment.value("type").toString();

                Attachment attach;

                if(type == "photo")
                {

                    QVariantMap photo = attachment.value("photo").toMap();

                    QString photo_id = photo.value("id").toString();
                    QString album_id = photo.value("album_id").toString();
                    QString owner_id = photo.value("owner_id").toString();
                    QString photo_75 = photo.value("photo_75").toString();
                    QString photo_130 = photo.value("photo_130").toString();
                    QString photo_1280 = photo.value("photo_1280").toString();
                    QString photo_width = photo.value("width").toString();
                    QString photo_height = photo.value("height").toString();
                    QString date = photo.value("date").toString();

                    QUrl url_photo(photo_75);
                    QByteArray photo_src = GET(url_photo);
                    QImage img = QImage::fromData(photo_src);
                    img.save("messages_photo/"+id+".jpg");
                    photo_75 = "messages_photo/"+id+".jpg";

                    attach.setID(photo_id);
                    attach.setAlbumID(album_id);
                    attach.setOwnerID(owner_id);
                    attach.setPhoto75(photo_75);
                    attach.setPhoto130(photo_130);
                    attach.setPhoto1280(photo_1280);
                    attach.setPhotoWidth(photo_width);
                    attach.setPhotoHeight(photo_height);
                    attach.setType(type);
                }

                listAttachments.append( qMakePair(type, attach) );
            }


            User from_user;
            from_user = users[from_id];

            //qDebug() << ">>>" << from_user.id() << " >:\n" <<  "type: "+type << "\n" <<"src: "+src << endl;


            Message msg;
            msg.setFrom(from_user).setText(text_message).setDate(date).setState(state).setAttachment(listAttachments);


            history[ date.toInt() ] = msg;
        }
    }
    else
    {
        return 0x000001;
    }

    return 0;
}