forked from lubuwest/Friendiqa
OAuth and bugfixes
This commit is contained in:
parent
e58a1f69dc
commit
d43c18bb76
65 changed files with 1681 additions and 1494 deletions
|
@ -68,10 +68,7 @@ bool FILESYSTEM::Visibility()
|
|||
|
||||
QString FILESYSTEM::homePath() const
|
||||
{
|
||||
//QDir dir(m_Directory);
|
||||
//
|
||||
QString homeDir=QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);;
|
||||
//qDebug(homeDir);
|
||||
return homeDir;
|
||||
}
|
||||
|
||||
|
@ -96,11 +93,9 @@ void FILESYSTEM::makeDir(QString name)
|
|||
{
|
||||
QDir dir(m_Directory);
|
||||
if (dir.mkdir(name)){
|
||||
//qDebug() << "makedir success" <<name;
|
||||
emit success(name);
|
||||
}
|
||||
else {
|
||||
qDebug() << "makedir error" <<name;
|
||||
emit error(name,1);
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +104,6 @@ void FILESYSTEM::makePath(QString name)
|
|||
{
|
||||
QDir dir(m_Directory);
|
||||
if (dir.mkpath(name)){
|
||||
//qDebug() << "makepath success" <<name;
|
||||
emit success(name);
|
||||
}
|
||||
else {
|
||||
|
@ -120,7 +114,6 @@ void FILESYSTEM::makePath(QString name)
|
|||
void FILESYSTEM::rmDir()
|
||||
{
|
||||
QDir dir(m_Directory);
|
||||
//qDebug()<<m_Directory;
|
||||
if (dir.removeRecursively()){
|
||||
emit success(m_Directory);
|
||||
}
|
||||
|
@ -130,8 +123,6 @@ void FILESYSTEM::rmDir()
|
|||
void FILESYSTEM::rmFile(QString name)
|
||||
{
|
||||
QDir dir(m_Directory);
|
||||
//qDebug()<<m_Directory;
|
||||
//qDebug(name);
|
||||
if(dir.remove(name)){
|
||||
emit success(name);
|
||||
}
|
||||
|
@ -144,8 +135,6 @@ QFileInfoList FILESYSTEM::fileList()
|
|||
filters << "*.png" <<"*.PNG" << "*.jpg" << "*.JPG" << "*.JPEG";
|
||||
dir.setNameFilters(filters);
|
||||
dir.setSorting(QDir::Time | QDir::Reversed);
|
||||
//QStringList m_Filelist=dir.entryInfoList();
|
||||
//qDebug() << "filelist " << m_Filelist;
|
||||
return dir.entryInfoList();
|
||||
}
|
||||
|
||||
|
@ -185,3 +174,13 @@ void FILESYSTEM::setAutostart(bool autostart) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString FILESYSTEM::osType() const
|
||||
{
|
||||
return QSysInfo::productType();
|
||||
}
|
||||
|
||||
QString FILESYSTEM::hostname() const
|
||||
{
|
||||
return QSysInfo::machineHostName();
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <QDir>
|
||||
#include <QObject>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <QSysInfo>
|
||||
//#include <QtAndroidExtras>
|
||||
//#include <QAndroidActivityResultReceiver>
|
||||
|
||||
|
@ -47,6 +47,8 @@ class FILESYSTEM : public QObject//, public QAndroidActivityResultReceiver
|
|||
//Q_PROPERTY(bool direxist READ direxist)
|
||||
Q_PROPERTY(QString homePath READ homePath)
|
||||
Q_PROPERTY(bool isAutostart READ isAutostart)
|
||||
Q_PROPERTY(QString osType READ osType CONSTANT)
|
||||
Q_PROPERTY(QString hostname READ hostname CONSTANT)
|
||||
//Q_PROPERTY(QString cameraPath READ cameraPath)
|
||||
|
||||
|
||||
|
@ -61,7 +63,8 @@ public:
|
|||
QString homePath() const;
|
||||
bool Visibility();
|
||||
bool isAutostart();
|
||||
|
||||
QString osType() const;
|
||||
QString hostname() const;
|
||||
//QString cameraPath() const;
|
||||
// virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
|
||||
|
||||
|
|
|
@ -158,3 +158,13 @@ void FILESYSTEM::setAutostart(bool autostart) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString FILESYSTEM::osType() const
|
||||
{
|
||||
return QSysInfo::productType();
|
||||
}
|
||||
|
||||
QString FILESYSTEM::hostname() const
|
||||
{
|
||||
return QSysInfo::machineHostName();
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
#include "filesystem.h"
|
||||
#include "remoteauthasyncimageprovider.h"
|
||||
#include "alarm.h"
|
||||
#include "oauth.h"
|
||||
//#include "AndroidNative/systemdispatcher.h"
|
||||
//#include "AndroidNative/environment.h"
|
||||
//#include "AndroidNative/debug.h"
|
||||
|
@ -107,6 +108,9 @@ int main(int argc, char *argv[]) {
|
|||
UPDATENEWS* updatenews = UPDATENEWS::instance();
|
||||
view.rootContext()->setContextProperty("updatenews", updatenews);
|
||||
|
||||
OAuthWrapper* oauth2 = OAuthWrapper::instance();
|
||||
view.rootContext()->setContextProperty("oauth2", oauth2);
|
||||
|
||||
qmlRegisterType<QSystemTrayIcon>("QSystemTrayIcon", 1, 0, "QSystemTrayIcon");
|
||||
qRegisterMetaType<QSystemTrayIcon::ActivationReason>("ActivationReason");
|
||||
view.rootContext()->setContextProperty("iconTray", QIcon(":/images/Friendica_monochrome.png"));
|
||||
|
|
88
source-linux/common/oauth.cpp
Normal file
88
source-linux/common/oauth.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
// This file is part of Friendiqa
|
||||
// https://github.com/lubuwest/Friendiqa
|
||||
// Copyright (C) 2020 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 "oauth.h"
|
||||
|
||||
#include <QtGui>
|
||||
#include <QtCore>
|
||||
#include <QtNetworkAuth>
|
||||
|
||||
|
||||
OAuthWrapper *OAuthWrapper::instance()
|
||||
{
|
||||
static OAuthWrapper oa2;
|
||||
return &oa2;
|
||||
}
|
||||
|
||||
|
||||
OAuthWrapper::OAuthWrapper(QObject *parent) : QObject(parent)
|
||||
{
|
||||
auto replyHandler = new QOAuthHttpServerReplyHandler(1337, this);
|
||||
oauth2.setReplyHandler(replyHandler);
|
||||
oauth2.setScope("read+write+follow+push");
|
||||
|
||||
connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged, [=](
|
||||
QAbstractOAuth::Status status) {
|
||||
if (status == QAbstractOAuth::Status::Granted){
|
||||
QMapIterator<QString, QVariant> i(oauth2.extraTokens());
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
//qDebug() << i.key() << ": " << i.value() << Qt::endl;
|
||||
}
|
||||
emit success(oauth2.token());
|
||||
}
|
||||
});
|
||||
connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
|
||||
&QDesktopServices::openUrl);
|
||||
}
|
||||
|
||||
void OAuthWrapper::setClientId(QString clientid)
|
||||
{
|
||||
m_clientid = clientid;
|
||||
}
|
||||
void OAuthWrapper::setServer(QString server)
|
||||
{
|
||||
m_server = server;
|
||||
}
|
||||
void OAuthWrapper::setClientSecret(QString clientsecret)
|
||||
{
|
||||
m_clientsecret = clientsecret;
|
||||
}
|
||||
|
||||
void OAuthWrapper::grant()
|
||||
{
|
||||
oauth2.setClientIdentifier(m_clientid);
|
||||
oauth2.setAuthorizationUrl(QUrl(m_server + "/oauth/authorize"));
|
||||
oauth2.setAccessTokenUrl(QUrl(m_server + "/oauth/token"));
|
||||
|
||||
oauth2.setClientIdentifierSharedKey(m_clientsecret);
|
||||
oauth2.grant();
|
||||
}
|
71
source-linux/common/oauth.h
Normal file
71
source-linux/common/oauth.h
Normal file
|
@ -0,0 +1,71 @@
|
|||
// This file is part of Friendiqa
|
||||
// https://github.com/lubuwest/Friendiqa
|
||||
// Copyright (C) 2020 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/>.
|
||||
|
||||
#ifndef OAUTH_H
|
||||
#define OAUTH_H
|
||||
|
||||
#include <QtCore>
|
||||
#include <QtNetwork>
|
||||
|
||||
#include <QOAuth2AuthorizationCodeFlow>
|
||||
|
||||
|
||||
class OAuthWrapper : public QObject//, public QAndroidActivityResultReceiver
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
|
||||
public:
|
||||
OAuthWrapper(QObject *parent = nullptr);;
|
||||
OAuthWrapper(const QString &clientIdentifier, QObject *parent = nullptr);
|
||||
static OAuthWrapper *instance();
|
||||
// bool isPermanent() const;
|
||||
// void setPermanent(bool value);
|
||||
|
||||
signals:
|
||||
void success(QString token);
|
||||
void error(QString error);
|
||||
|
||||
public slots:
|
||||
void grant();
|
||||
void setServer(QString server);
|
||||
void setClientId(QString clientid);
|
||||
void setClientSecret(QString clientsecret);
|
||||
|
||||
private:
|
||||
QOAuth2AuthorizationCodeFlow oauth2;
|
||||
// bool permanent = false;
|
||||
QString m_clientid;
|
||||
QString m_clientsecret;
|
||||
QString m_server;
|
||||
};
|
||||
|
||||
#endif //OAuthWrapper
|
|
@ -59,14 +59,14 @@ UPDATENEWS::UPDATENEWS(QObject *parent) : QObject(parent)
|
|||
|
||||
}
|
||||
|
||||
void UPDATENEWS::setUrl(QString url)
|
||||
{
|
||||
if (url!=m_url) {
|
||||
m_url = url;
|
||||
xhr.setUrl(url);
|
||||
emit urlChanged(m_url);
|
||||
}
|
||||
}
|
||||
//void UPDATENEWS::setUrl(QString url)
|
||||
//{
|
||||
// if (url!=m_url) {
|
||||
// m_url = url;
|
||||
// xhr.setUrl(url);
|
||||
// emit urlChanged(m_url);
|
||||
// }
|
||||
//}
|
||||
|
||||
void UPDATENEWS::setSyncAll(bool syncAll)
|
||||
{
|
||||
|
@ -77,8 +77,11 @@ void UPDATENEWS::setDatabase()
|
|||
{
|
||||
static QQmlEngine qe;
|
||||
QString db_url=qe.offlineStorageDatabaseFilePath("Friendiqa");
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setDatabaseName(QUrl("file://"+db_url+".sqlite").toLocalFile());
|
||||
if (!m_db.open())
|
||||
{
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setDatabaseName(QUrl("file://"+db_url+".sqlite").toLocalFile());
|
||||
}
|
||||
|
||||
if (!m_db.open())
|
||||
{
|
||||
|
@ -87,9 +90,46 @@ void UPDATENEWS::setDatabase()
|
|||
}
|
||||
|
||||
|
||||
QJsonArray UPDATENEWS::getAccounts(QString filtername, QString filtervalue){
|
||||
QString filterstring="";
|
||||
|
||||
if (filtername!=""){
|
||||
bool ok;
|
||||
int filternumber = filtervalue.toInt(&ok, 10);
|
||||
if(ok){
|
||||
filterstring=" WHERE " + filtername + " = " + filtervalue;
|
||||
}
|
||||
else{
|
||||
filterstring=" WHERE " + filtername + " = '" + filtervalue +"'";
|
||||
}
|
||||
}
|
||||
QSqlQuery query("SELECT * FROM config" + filterstring + " ORDER BY isActive ASC, username ASC",m_db);
|
||||
QJsonArray accountlist;
|
||||
while(query.next()){
|
||||
{
|
||||
QJsonObject accountData;
|
||||
accountData.insert("server",query.value(0).toString());
|
||||
accountData.insert("username",query.value(1).toString());
|
||||
accountData.insert("password",query.value(2).toString());
|
||||
accountData.insert("imagestore",query.value(3).toString());
|
||||
accountData.insert("isActive",query.value(7).toString());
|
||||
QJsonArray m_permissions=query.value(7).toJsonArray();
|
||||
accountData.insert("permissions",m_permissions);
|
||||
accountData.insert("token",QString(QByteArray::fromBase64(query.value(10).toByteArray())));
|
||||
QJsonDocument m_client;
|
||||
if(query.value(12).toByteArray()!=""){
|
||||
m_client=QJsonDocument::fromJson(QByteArray::fromBase64(query.value(12).toByteArray()));
|
||||
}
|
||||
accountData.insert("client",m_client.object());
|
||||
accountlist.append(accountData);
|
||||
}
|
||||
}
|
||||
query.clear();
|
||||
return accountlist;
|
||||
}
|
||||
|
||||
void UPDATENEWS::login()
|
||||
{
|
||||
|
||||
QSqlQuery syncquery("SELECT * FROM globaloptions",m_db);
|
||||
m_updateInterval=0;
|
||||
syncindex=0;
|
||||
|
@ -100,11 +140,9 @@ void UPDATENEWS::login()
|
|||
}
|
||||
if (syncquery.value(0).toString().left(5)=="sync_" && syncquery.value(1).toInt()==1){
|
||||
synclist.append(syncquery.value(0).toString());
|
||||
//qDebug() << " sync " << syncquery.value(0).toString() << " " <<syncquery.value(1).toString();
|
||||
}
|
||||
if (syncquery.value(0).toString().left(7)=="notify_" && syncquery.value(1).toInt()==1){
|
||||
notifylist.append(syncquery.value(0).toString());
|
||||
//qDebug() << " notify " << syncquery.value(0).toString() << " " <<syncquery.value(1).toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -117,28 +155,17 @@ void UPDATENEWS::login()
|
|||
if(!(synctimequery3.exec())) {qDebug() << " synctimequery3 " << synctimequery3.lastError();}
|
||||
}
|
||||
|
||||
QSqlQuery query("SELECT * FROM config ORDER BY isActive ASC, username ASC",m_db);
|
||||
query.last();
|
||||
usernamelength=query.at()+1;
|
||||
|
||||
if (query.isActive()&&(usernameindex<usernamelength))
|
||||
{ query.seek(usernameindex);
|
||||
username = query.value(1).toString();
|
||||
QByteArray bpassword=query.value(2).toByteArray();
|
||||
QString password=QByteArray::fromBase64(bpassword);
|
||||
m_login=username+":"+password ;
|
||||
xhr.setLogin(m_login);
|
||||
m_url=query.value(0).toString();
|
||||
xhr.setUrl(m_url);
|
||||
m_imagedir=query.value(3).toString();
|
||||
xhr.setImagedir(m_imagedir);
|
||||
QString isActive=query.value(7).toString();
|
||||
QJsonArray acc=getAccounts();
|
||||
usernamelength=acc.size();
|
||||
if (usernameindex<usernamelength){
|
||||
QJsonObject currentAccount =acc[usernameindex].toObject();
|
||||
xhr.setAccount(currentAccount.toVariantMap());
|
||||
username = currentAccount["username"].toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void UPDATENEWS::startsync()
|
||||
{ //qDebug()<<"Friendiqa start syncing "<<syncindex <<" of "<<synclist.length() << " m_login "<<m_login;
|
||||
{
|
||||
if (syncindex<synclist.length()){
|
||||
if (synclist[syncindex]=="sync_Timeline"){
|
||||
timeline();
|
||||
|
@ -179,7 +206,7 @@ void UPDATENEWS::timeline()
|
|||
{
|
||||
m_api="/api/statuses/friends_timeline";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
//xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
QSqlQuery query("SELECT status_id FROM news WHERE messagetype=0 AND username='"+ username +"' ORDER BY status_id DESC LIMIT 1",m_db);
|
||||
if (query.isActive() && query.isSelect()){
|
||||
|
@ -199,7 +226,6 @@ void UPDATENEWS::replies()
|
|||
{
|
||||
m_api="/api/statuses/replies";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
QSqlQuery query("SELECT status_id FROM news WHERE messagetype=3 AND username='"+ username +"' ORDER BY status_id DESC LIMIT 1",m_db);
|
||||
if (query.isActive() && query.isSelect()){
|
||||
|
@ -219,7 +245,6 @@ void UPDATENEWS::directmessages()
|
|||
{
|
||||
m_api="/api/direct_messages/all";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
QSqlQuery query("SELECT status_id FROM news WHERE messagetype=1 AND username='"+ username +"' ORDER BY status_id DESC LIMIT 1",m_db);
|
||||
if (query.isActive() && query.isSelect()){
|
||||
|
@ -238,7 +263,6 @@ void UPDATENEWS::notifications()
|
|||
{
|
||||
m_api="/api/friendica/notification";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
xhr.get();
|
||||
QObject::connect(&xhr,SIGNAL(success(QByteArray,QString)),this,SLOT(store(QByteArray,QString)));
|
||||
|
@ -249,7 +273,6 @@ void UPDATENEWS::notifications()
|
|||
void UPDATENEWS::events()
|
||||
{ m_api="/api/friendica/events";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
QSqlQuery query("SELECT id FROM events WHERE username='"+ username +"' ORDER BY id DESC LIMIT 1",m_db);
|
||||
if (query.isActive() && query.isSelect()){
|
||||
|
@ -268,7 +291,6 @@ void UPDATENEWS::events()
|
|||
void UPDATENEWS::friendrequests()
|
||||
{ m_api="/api/v1/follow_requests";
|
||||
xhr.clearParams();
|
||||
xhr.setUrl(m_url);
|
||||
xhr.setApi(m_api);
|
||||
xhr.get();
|
||||
QObject::disconnect(&xhr,SIGNAL(success(QByteArray,QString)),this,SLOT(store(QByteArray,QString)));
|
||||
|
@ -282,7 +304,6 @@ void UPDATENEWS::friendrequests()
|
|||
void UPDATENEWS::store(QByteArray serverreply,QString apiname)
|
||||
{ if (apiname!=m_api || xhr.downloadtype()!=""){} else {
|
||||
QJsonDocument news;
|
||||
//qDebug()<<apiname;
|
||||
QJsonParseError jsonerror;
|
||||
news=QJsonDocument::fromJson(serverreply,&jsonerror);
|
||||
if (news.isArray()){
|
||||
|
@ -421,7 +442,7 @@ void UPDATENEWS::store(QByteArray serverreply,QString apiname)
|
|||
}
|
||||
}
|
||||
else {
|
||||
qDebug()<< "Friendiqa updatenews error " << serverreply;
|
||||
qDebug()<< m_api << "Friendiqa updatenews error " << serverreply <<username ;
|
||||
//emit this->error(m_api,QTextCodec::codecForName("utf-8")->toUnicode(serverreply));
|
||||
emit this->error(m_api,QString(serverreply));
|
||||
if(m_syncAll){
|
||||
|
@ -436,13 +457,11 @@ void UPDATENEWS::store(QByteArray serverreply,QString apiname)
|
|||
|
||||
|
||||
void UPDATENEWS::updateImageLocation(QString downloadtype,QString imageurl, QString filename, int index){
|
||||
//qDebug()<< "update Imagelocation " << downloadtype << " " << imageurl << " " << filename;
|
||||
if (downloadtype=="contactlist"){
|
||||
QSqlQuery testquery("SELECT profile_image FROM contacts WHERE profile_image_url ='"+imageurl+ "' AND username = '" +username+"'",m_db);
|
||||
testquery.first();
|
||||
QSqlQuery query("UPDATE contacts SET profile_image='"+ filename +"' WHERE profile_image_url ='"+imageurl+ "' AND username = '" +username+"'",m_db);
|
||||
if(!(query.exec())) {qDebug()<< "updateImagelocation " << query.lastError();}
|
||||
//qDebug()<< "index " << index << " " << newcontactnames.length()-1 << " " << m_syncAll;
|
||||
if (index==(newcontactnames.length()-1)){
|
||||
newcontactnames.clear();
|
||||
newcontactimagelinks.clear();
|
||||
|
@ -469,7 +488,6 @@ void UPDATENEWS::updateImageLocation(QString downloadtype,QString imageurl, QStr
|
|||
}
|
||||
|
||||
QJsonObject UPDATENEWS::findNotificationContact(QString contacturl){
|
||||
//qDebug()<< "findNotificationContact "<<contacturl;
|
||||
QSqlQuery query("SELECT id,url FROM contacts WHERE url='"+contacturl+"' AND username='"+ username+"'",m_db);
|
||||
query.first();
|
||||
QJsonObject contact{
|
||||
|
@ -806,23 +824,16 @@ void UPDATENEWS::storeEvents(QByteArray serverreply,QString apiname)
|
|||
QObject::disconnect(&xhr,SIGNAL(success(QByteArray,QString)),this,SLOT(storeEvents(QByteArray,QString)));
|
||||
}
|
||||
|
||||
QString UPDATENEWS::url() const
|
||||
{
|
||||
return m_url;
|
||||
}
|
||||
|
||||
void UPDATENEWS::startImagedownload(QString downloadtype)
|
||||
{
|
||||
xhr.setDownloadtype(downloadtype );
|
||||
xhr.setFilelist(newcontactimagelinks);
|
||||
xhr.setContactlist(newcontactnames);
|
||||
xhr.setImagedir(m_imagedir);
|
||||
xhr.getlist();
|
||||
}
|
||||
|
||||
void UPDATENEWS::showError(QString data, QString url,QString api, int code )
|
||||
{
|
||||
//qDebug() << "showerror " << api << " data " << data;
|
||||
emit this->error(api,data);
|
||||
if (api!=m_api || xhr.downloadtype()!=""){} else{
|
||||
if((m_updateInterval!=0) && m_syncAll){
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
|
||||
#include <QObject>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QSqlDatabase>
|
||||
#include "xhr.h"
|
||||
#include "alarm.h"
|
||||
|
@ -42,25 +43,18 @@
|
|||
class UPDATENEWS : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
|
||||
// Q_PROPERTY(QString login READ login NOTIFY loginChanged)
|
||||
|
||||
public:
|
||||
static UPDATENEWS *instance();
|
||||
|
||||
explicit UPDATENEWS(QObject *parent = 0);
|
||||
|
||||
QString url() const;
|
||||
//QString login() const;
|
||||
|
||||
signals:
|
||||
|
||||
void urlChanged(QString url);
|
||||
void success(QString api);
|
||||
void error(QString api, QString content);
|
||||
void quitapp();
|
||||
|
||||
public slots:
|
||||
void setUrl(QString url);
|
||||
void setSyncAll(bool syncAll);
|
||||
void setDatabase();
|
||||
void login();
|
||||
|
@ -71,19 +65,16 @@ public slots:
|
|||
void notifications();
|
||||
void friendrequests();
|
||||
void events();
|
||||
//void startservice(QString type,QVariantMap map);
|
||||
void startImagedownload(QString downloadtype);
|
||||
void updateImageLocation(QString downloadtype,QString imageurl, QString filename, int index);
|
||||
void store(QByteArray serverreply,QString apiname);
|
||||
void storeFriendrequests(QByteArray serverreply,QString apiname);
|
||||
void storeEvents(QByteArray serverreply,QString apiname);
|
||||
void showError(QString data, QString url,QString api, int code);
|
||||
QJsonArray getAccounts(QString filtername="",QString filtervalue="");
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
QString m_api;
|
||||
QString m_imagedir;
|
||||
QString m_login;
|
||||
QString username;
|
||||
bool m_syncAll;
|
||||
int syncindex;
|
||||
|
@ -95,8 +86,6 @@ private:
|
|||
QList<QJsonValue> findNewContacts(QJsonDocument news);
|
||||
QJsonObject findNotificationContact(QString imagelink);
|
||||
int m_updateInterval;
|
||||
//void timeline();
|
||||
//void store(QByteArray serverreply,QString apiname);
|
||||
void updateContacts(QList<QJsonValue> contacts);
|
||||
|
||||
XHR xhr;
|
||||
|
|
|
@ -54,65 +54,90 @@ XHR::XHR(QObject *parent) : QObject(parent)
|
|||
|
||||
void XHR::setUrl(QString url)
|
||||
{
|
||||
if (url!=m_url) {
|
||||
m_url = url;
|
||||
emit urlChanged();
|
||||
}
|
||||
if (url!=m_url) {
|
||||
m_url = url;
|
||||
emit urlChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setApi(QString api)
|
||||
{
|
||||
if (api!=m_api) {
|
||||
m_api = api;
|
||||
emit apiChanged();
|
||||
}
|
||||
if (api!=m_api) {
|
||||
m_api = api;
|
||||
emit apiChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setLogin(QString login)
|
||||
{
|
||||
if (login!=m_login) {
|
||||
m_login = login;
|
||||
emit loginChanged();
|
||||
}
|
||||
if (login!=m_login) {
|
||||
m_login = login;
|
||||
m_token="";
|
||||
emit loginChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setAccount(QVariantMap account)
|
||||
{
|
||||
clearParams();
|
||||
if (account["password"].toString() !=""){
|
||||
setLogin(account["username"].toString()+":"+QByteArray::fromBase64(account["password"].toByteArray()));
|
||||
}
|
||||
else if (account["token"].toString() !="" && !account["token"].isNull()){
|
||||
setToken(account["token"].toString());
|
||||
}
|
||||
setUrl(account["server"].toString());
|
||||
setImagedir(account["imagestore"].toString());
|
||||
m_account = account;
|
||||
emit accountChanged();
|
||||
}
|
||||
|
||||
void XHR::setToken(QString token)
|
||||
{
|
||||
if (token!=m_token) {
|
||||
m_token = token;
|
||||
m_login="";
|
||||
emit tokenChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setFilename(QString filename)
|
||||
{
|
||||
if (filename!=m_filename) {
|
||||
m_filename = filename;
|
||||
emit filenameChanged();
|
||||
}
|
||||
if (filename!=m_filename) {
|
||||
m_filename = filename;
|
||||
emit filenameChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setContactlist(QList<QString> contactlist)
|
||||
{
|
||||
if (contactlist!=m_contactlist) {
|
||||
m_contactlist = contactlist;
|
||||
emit contactlistChanged();
|
||||
}
|
||||
if (contactlist!=m_contactlist) {
|
||||
m_contactlist = contactlist;
|
||||
emit contactlistChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setFilelist(QList<QString> filelist)
|
||||
{
|
||||
if (filelist!=m_filelist) {
|
||||
m_filelist = filelist;
|
||||
emit filelistChanged();
|
||||
}
|
||||
if (filelist!=m_filelist) {
|
||||
m_filelist = filelist;
|
||||
emit filelistChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setImagedir(QString imagedir)
|
||||
{
|
||||
if (imagedir!=m_imagedir) {
|
||||
m_imagedir = imagedir;
|
||||
emit imagedirChanged();
|
||||
}
|
||||
if (imagedir!=m_imagedir) {
|
||||
m_imagedir = imagedir;
|
||||
emit imagedirChanged();
|
||||
}
|
||||
}
|
||||
void XHR::setDownloadtype(QString downloadtype)
|
||||
{
|
||||
if (downloadtype!=m_downloadtype) {
|
||||
m_downloadtype = downloadtype;
|
||||
emit downloadtypeChanged();
|
||||
}
|
||||
if (downloadtype!=m_downloadtype) {
|
||||
m_downloadtype = downloadtype;
|
||||
emit downloadtypeChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString XHR::url() const
|
||||
|
@ -130,6 +155,16 @@ QString XHR::login() const
|
|||
return m_login;
|
||||
}
|
||||
|
||||
QString XHR::token() const
|
||||
{
|
||||
return m_token;
|
||||
}
|
||||
|
||||
QVariantMap XHR::account() const
|
||||
{
|
||||
return m_account;
|
||||
}
|
||||
|
||||
QString XHR::filename() const
|
||||
{
|
||||
return m_filename;
|
||||
|
@ -176,9 +211,14 @@ void XHR::download()
|
|||
QUrl requrl(m_url);
|
||||
QNetworkRequest request;
|
||||
if(m_downloadtype=="picturelist"){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
if(m_login!=""){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
} else{
|
||||
QString headerData = "Bearer " + m_token;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
}
|
||||
}
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
|
@ -198,20 +238,22 @@ void XHR::get()
|
|||
while(i.hasNext()) {
|
||||
i.next();
|
||||
query.addQueryItem(i.key(), i.value());
|
||||
//qDebug()<<i.key() << " value "<< i.value();
|
||||
}
|
||||
|
||||
QUrl requrl(m_url+m_api);
|
||||
//qDebug() << "API "<< requrl<<m_api;
|
||||
requrl.setQuery(query);
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
QNetworkRequest request;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
if(m_login!=""){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
} else if (m_token!=""){
|
||||
QString headerData = "Bearer " + m_token;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
}
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, &XHR::onReplySuccess);
|
||||
//connect(reply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(updateDownloadProgress(qint64,qint64)));
|
||||
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onReplyError(QNetworkReply::NetworkError)));
|
||||
connect(reply, &QNetworkReply::readyRead, this, &XHR::onReadyRead);
|
||||
connect(reply, &QNetworkReply::sslErrors, this, &XHR::onSSLError);
|
||||
|
@ -221,7 +263,8 @@ void XHR::getlist()
|
|||
{
|
||||
if(dlindex < m_filelist.size()) {
|
||||
QString cleanfilename;
|
||||
if (m_downloadtype=="contactlist" || m_downloadtype=="friendrequests" ){cleanfilename = m_contactlist.at(dlindex)+"-"+ m_filelist.at(dlindex).section('/',-1).section('?',0,0);
|
||||
if (m_downloadtype=="contactlist" || m_downloadtype=="friendrequests" ){
|
||||
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 {
|
||||
|
@ -234,7 +277,6 @@ void XHR::getlist()
|
|||
|
||||
void XHR::post()
|
||||
{
|
||||
//qDebug() << "start post to " << m_url;
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
|
||||
QHashIterator<QString, QString> iparams(params);
|
||||
|
@ -250,7 +292,6 @@ void XHR::post()
|
|||
if (files.contains("media")){
|
||||
uimg.setAngle(files.value("angle").toInt());
|
||||
uimg.setSource(files.value("media"));
|
||||
//qDebug() << "\t add media " << files.value("media") << " : " << files.value("angle").toInt();
|
||||
QHttpPart imagePart;
|
||||
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(uimg.mimetype()));
|
||||
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"media\"; filename=\""+uimg.filename()+"\""));
|
||||
|
@ -258,10 +299,15 @@ void XHR::post()
|
|||
multiPart->append(imagePart);
|
||||
}
|
||||
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
QNetworkRequest request;
|
||||
request.setRawHeader(QByteArray("Authorization"), headerData.toLocal8Bit());
|
||||
if(m_login!=""){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
} else if (m_token!=""){
|
||||
QString headerData = "Bearer " + m_token;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
}
|
||||
request.setUrl(m_url+m_api);
|
||||
reply = manager.post(request, multiPart);
|
||||
qDebug() << "\t request sent";
|
||||
|
@ -269,17 +315,21 @@ void XHR::post()
|
|||
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onReplyError(QNetworkReply::NetworkError)));
|
||||
connect(reply, &QNetworkReply::readyRead, this, &XHR::onReadyRead);
|
||||
connect(reply, &QNetworkReply::sslErrors, this, &XHR::onSSLError);
|
||||
qDebug() << "\t reply signals connected";
|
||||
}
|
||||
|
||||
void XHR::postJSON()
|
||||
{
|
||||
if (params.contains("JSON")){
|
||||
QByteArray mJSON=params.value("JSON").toUtf8();
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
QNetworkRequest request;
|
||||
request.setRawHeader(QByteArray("Authorization"), headerData.toLocal8Bit());
|
||||
if(m_login!=""){
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
} else if (m_token!=""){
|
||||
QString headerData = "Bearer " + m_token;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
}
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json; charset=UTF-8");
|
||||
request.setUrl(m_url+m_api);
|
||||
reply = manager.post(request, mJSON);
|
||||
|
@ -306,7 +356,7 @@ void XHR::onReplySuccess()
|
|||
qDebug() << "!";
|
||||
emit this->success(buffer, m_api);
|
||||
buffer.clear();
|
||||
// reply->deleteLater();
|
||||
// reply->deleteLater();
|
||||
}
|
||||
|
||||
void XHR::onRequestFinished()
|
||||
|
@ -317,7 +367,7 @@ void XHR::onRequestFinished()
|
|||
else if (m_downloadtype=="picturelist") {
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(buffer);
|
||||
QJsonObject jsonObject = jsonResponse.object();
|
||||
int arraystart=buffer.indexOf('{"data":"')+8;
|
||||
int arraystart=buffer.indexOf("{\"data\":\"")+8;
|
||||
int arraylength=buffer.indexOf('"',9)-arraystart;
|
||||
QByteArray b64=buffer.mid(arraystart,arraylength);
|
||||
QString helpfilename=jsonObject["filename"].toString();
|
||||
|
@ -352,7 +402,7 @@ void XHR::onRequestFinished()
|
|||
emit this->downloaded(m_downloadtype,m_url,m_filename,dlindex);
|
||||
if(downloadtype()=="contactlist" || downloadtype()=="friendrequests"){dlindex=dlindex+1;XHR::getlist();}
|
||||
|
||||
//reply->deleteLater();
|
||||
//reply->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -365,7 +415,7 @@ void XHR::onReadyRead()
|
|||
//void XHR::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes)
|
||||
//{
|
||||
// qDebug() << "Bytes: " << bytesRead<<" / "<<totalBytes;
|
||||
|
||||
|
||||
//}
|
||||
|
||||
void XHR::onSSLError(const QList<QSslError> &errors)
|
||||
|
|
|
@ -44,6 +44,8 @@ class XHR : public QObject
|
|||
Q_OBJECT
|
||||
Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
|
||||
Q_PROPERTY(QString login READ login WRITE setLogin NOTIFY loginChanged)
|
||||
Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
|
||||
Q_PROPERTY(QVariantMap account READ account WRITE setAccount NOTIFY accountChanged)
|
||||
Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
|
||||
Q_PROPERTY(QString imagedir READ imagedir WRITE setImagedir NOTIFY imagedirChanged)
|
||||
Q_PROPERTY(QList<QString> contactlist READ contactlist WRITE setContactlist NOTIFY contactlistChanged)
|
||||
|
@ -60,6 +62,8 @@ public:
|
|||
QString url() const;
|
||||
QString api() const;
|
||||
QString login() const;
|
||||
QString token() const;
|
||||
QVariantMap account() const;
|
||||
QString filename() const;
|
||||
QList<QString> contactlist() const;
|
||||
QList<QString> filelist() const;
|
||||
|
@ -71,6 +75,8 @@ signals:
|
|||
void urlChanged();
|
||||
void apiChanged();
|
||||
void loginChanged();
|
||||
void tokenChanged();
|
||||
void accountChanged();
|
||||
void filenameChanged();
|
||||
void contactlistChanged();
|
||||
void filelistChanged();
|
||||
|
@ -86,6 +92,8 @@ public slots:
|
|||
void setUrl(QString url);
|
||||
void setApi(QString api);
|
||||
void setLogin(QString login);
|
||||
void setToken(QString token);
|
||||
void setAccount(QVariantMap account);
|
||||
void setDownloadtype(QString downloadtype);
|
||||
void setFilename(QString filename);
|
||||
void setContactlist(QList<QString> filename);
|
||||
|
@ -115,6 +123,8 @@ private:
|
|||
QString m_url;
|
||||
QString m_api;
|
||||
QString m_login;
|
||||
QString m_token;
|
||||
QVariantMap m_account;
|
||||
QString m_filename;
|
||||
QString m_downloadtype;
|
||||
// QString m_networktype;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue