Friendiqa v0.2
This commit is contained in:
parent
74fb551755
commit
a3be940192
123 changed files with 9156 additions and 2455 deletions
|
@ -1,12 +1,43 @@
|
|||
#include "xhr.h"
|
||||
// This file is part of Friendiqa
|
||||
// https://github.com/lubuwest/Friendiqa
|
||||
// Copyright (C) 2017 Marco R. <thomasschmidt45@gmx.net>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "xhr.h"
|
||||
|
||||
#include <QHttpPart>
|
||||
#include <QTextCodec>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkCookieJar>
|
||||
#include <QNetworkCookie>
|
||||
#include <QList>
|
||||
#include <QDataStream>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include "uploadableimage.h"
|
||||
|
||||
XHR *XHR::instance()
|
||||
|
@ -44,6 +75,29 @@ void XHR::setFilename(QString filename)
|
|||
}
|
||||
}
|
||||
|
||||
void XHR::setContactlist(QList<QString> contactlist)
|
||||
{
|
||||
if (contactlist!=m_contactlist) {
|
||||
m_contactlist = contactlist;
|
||||
emit contactlistChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setFilelist(QList<QString> filelist)
|
||||
{
|
||||
if (filelist!=m_filelist) {
|
||||
m_filelist = filelist;
|
||||
emit filelistChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setImagedir(QString imagedir)
|
||||
{
|
||||
if (imagedir!=m_imagedir) {
|
||||
m_imagedir = imagedir;
|
||||
emit imagedirChanged();
|
||||
}
|
||||
}
|
||||
void XHR::setDownloadtype(QString downloadtype)
|
||||
{
|
||||
if (downloadtype!=m_downloadtype) {
|
||||
|
@ -67,6 +121,21 @@ QString XHR::filename() const
|
|||
return m_filename;
|
||||
}
|
||||
|
||||
QList<QString> XHR::contactlist() const
|
||||
{
|
||||
return m_contactlist;
|
||||
}
|
||||
|
||||
QList<QString> XHR::filelist() const
|
||||
{
|
||||
return m_filelist;
|
||||
}
|
||||
|
||||
QString XHR::imagedir() const
|
||||
{
|
||||
return m_imagedir;
|
||||
}
|
||||
|
||||
QString XHR::downloadtype() const
|
||||
{
|
||||
return m_downloadtype;
|
||||
|
@ -91,9 +160,14 @@ void XHR::clearParams()
|
|||
void XHR::download()
|
||||
{
|
||||
QUrl requrl(m_url);
|
||||
if(m_downloadtype=="picturelist"){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
}
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
// reply->ignoreSslErrors();
|
||||
reply->ignoreSslErrors();
|
||||
connect(reply, &QNetworkReply::readyRead,this, &XHR::onReadyRead);
|
||||
//connect(reply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(updateDownloadProgress(qint64,qint64)));
|
||||
connect(reply, &QNetworkReply::finished,this, &XHR::onRequestFinished);
|
||||
|
@ -117,9 +191,6 @@ void XHR::get()
|
|||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
// QNetworkCookieJar* cJar = new QNetworkCookieJar;
|
||||
// manager.setCookieJar(cJar);
|
||||
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
|
||||
|
@ -129,6 +200,20 @@ void XHR::get()
|
|||
connect(reply, &QNetworkReply::sslErrors, this, &XHR::onSSLError);
|
||||
}
|
||||
|
||||
void XHR::getlist()
|
||||
{
|
||||
if(dlindex < m_filelist.size()) {
|
||||
QString cleanfilename;
|
||||
if (m_downloadtype=="contactlist"){cleanfilename = m_contactlist.at(dlindex)+"-"+ m_filelist.at(dlindex).section('/',-1).section('?',0,0);
|
||||
XHR::setFilename(imagedir()+"contacts/"+cleanfilename);
|
||||
XHR::setUrl(m_filelist.at(dlindex));}
|
||||
else {
|
||||
XHR::setUrl(m_filelist.at(dlindex));}
|
||||
XHR::download();
|
||||
} else {dlindex=0;}
|
||||
}
|
||||
|
||||
|
||||
void XHR::post()
|
||||
{
|
||||
qDebug() << "start post to " << m_url;
|
||||
|
@ -178,7 +263,7 @@ void XHR::post()
|
|||
void XHR::onReplyError(QNetworkReply::NetworkError code)
|
||||
{
|
||||
qDebug() << code;
|
||||
emit this->error( bufferToString(), (int) code);
|
||||
emit this->error( bufferToString(), m_url, (int) code);
|
||||
buffer.clear();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
@ -194,14 +279,45 @@ void XHR::onReplySuccess()
|
|||
void XHR::onRequestFinished()
|
||||
{
|
||||
// Save the file here
|
||||
//qDebug() << "buffer downloaded "<<buffer;
|
||||
if (buffer.isNull()){qDebug() << "File empty"<<m_url; buffer.clear(); emit this->error(m_downloadtype,1);}
|
||||
else {QFile file(m_filename);
|
||||
file.open(QIODevice::WriteOnly);
|
||||
file.write(buffer);
|
||||
buffer.clear();
|
||||
file.close();
|
||||
emit this->downloaded(m_downloadtype);
|
||||
if (buffer.isNull()){qDebug() << "File empty"<<m_url; buffer.clear(); emit this->error(m_downloadtype,m_url,1);}
|
||||
else if (m_downloadtype=="picturelist") {
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(buffer);
|
||||
QJsonObject jsonObject = jsonResponse.object();
|
||||
int arraystart=buffer.indexOf('{"data":"')+8;
|
||||
int arraylength=buffer.indexOf('"',9)-arraystart;
|
||||
QByteArray b64=buffer.mid(arraystart,arraylength);
|
||||
QString helpfilename=jsonObject["filename"].toString();
|
||||
QString helpfile=helpfilename.left(helpfilename.lastIndexOf("."));
|
||||
QString filesuffix="";
|
||||
if (jsonObject["type"].toString()=="image/jpeg"){filesuffix=".jpg";}
|
||||
else if (jsonObject["type"].toString()=="image/png"){filesuffix=".png";}
|
||||
else {filesuffix="";}
|
||||
if (helpfilename==""){// check if file has any filename
|
||||
helpfile=jsonObject["id"].toString();
|
||||
setFilename(imagedir()+"albums/"+jsonObject["album"].toString()+"/"+jsonObject["id"].toString()+filesuffix);
|
||||
}
|
||||
else{setFilename(imagedir()+"albums/"+jsonObject["album"].toString()+"/"+helpfile+filesuffix);}
|
||||
//qDebug()<<"Filename "<<m_filename;
|
||||
QFile file(m_filename);
|
||||
file.open(QIODevice::WriteOnly);
|
||||
file.write(QByteArray::fromBase64(b64));
|
||||
buffer.clear();
|
||||
b64.clear();
|
||||
file.close();
|
||||
jsonObject["data"]="";
|
||||
jsonObject["filename"]=helpfile+filesuffix;
|
||||
emit this->downloadedjson(m_downloadtype,m_url,m_filename,dlindex,jsonObject);
|
||||
if(downloadtype()=="picturelist"){dlindex=dlindex+1;XHR::getlist();}
|
||||
}
|
||||
else {
|
||||
QFile file(m_filename);
|
||||
file.open(QIODevice::WriteOnly);
|
||||
file.write(buffer);
|
||||
buffer.clear();
|
||||
file.close();
|
||||
emit this->downloaded(m_downloadtype,m_url,m_filename,dlindex);
|
||||
if(downloadtype()=="contactlist"){dlindex=dlindex+1;XHR::getlist();}
|
||||
|
||||
//reply->deleteLater();
|
||||
}
|
||||
}
|
||||
|
@ -210,15 +326,6 @@ void XHR::onReadyRead()
|
|||
{
|
||||
qDebug() << ".";
|
||||
buffer += reply->readAll();
|
||||
// QList<QNetworkCookie> list = manager.cookieJar()->cookiesForUrl(m_url);
|
||||
// QFile f("/home/pankraz/cookie.txt");
|
||||
// f.open(QIODevice::ReadWrite);
|
||||
// for(int i = 0; i < list.size(); ++i){
|
||||
// QDataStream s(&f);
|
||||
// s << list.at(i).toRawForm();
|
||||
// }
|
||||
// f.close();
|
||||
|
||||
}
|
||||
|
||||
//void XHR::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue