Qt/QML App for Friendiqa
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xhr.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // This file is part of Friendiqa
  2. // https://github.com/lubuwest/Friendiqa
  3. // Copyright (C) 2017 Marco R. <thomasschmidt45@gmx.net>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // In addition, as a special exception, the copyright holders give
  11. // permission to link the code of portions of this program with the
  12. // OpenSSL library under certain conditions as described in each
  13. // individual source file, and distribute linked combinations including
  14. // the two.
  15. //
  16. // You must obey the GNU General Public License in all respects for all
  17. // of the code used other than OpenSSL. If you modify file(s) with this
  18. // exception, you may extend this exception to your version of the
  19. // file(s), but you are not obligated to do so. If you do not wish to do
  20. // so, delete this exception statement from your version. If you delete
  21. // this exception statement from all source files in the program, then
  22. // also delete it here.
  23. //
  24. // This program is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. // GNU General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU General Public License
  30. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. #ifndef XHR_H
  32. #define XHR_H
  33. #include <QNetworkAccessManager>
  34. #include <QNetworkReply>
  35. #include <QNetworkRequest>
  36. #include <QObject>
  37. #include <QJsonObject>
  38. #include <QNetworkConfiguration>
  39. class XHR : public QObject
  40. {
  41. Q_OBJECT
  42. Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
  43. Q_PROPERTY(QString login READ login WRITE setLogin NOTIFY loginChanged)
  44. Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
  45. Q_PROPERTY(QString imagedir READ imagedir WRITE setImagedir NOTIFY imagedirChanged)
  46. Q_PROPERTY(QList<QString> contactlist READ contactlist WRITE setContactlist NOTIFY contactlistChanged)
  47. Q_PROPERTY(QList<QString> filelist READ filelist WRITE setFilelist NOTIFY filelistChanged)
  48. Q_PROPERTY(QString downloadtype READ downloadtype WRITE setDownloadtype NOTIFY downloadtypeChanged)
  49. Q_PROPERTY(QString networktype READ networktype NOTIFY networktypeChanged)
  50. public:
  51. static XHR *instance();
  52. explicit XHR(QObject *parent = 0);
  53. QString url() const;
  54. QString api() const;
  55. QString login() const;
  56. QString filename() const;
  57. QList<QString> contactlist() const;
  58. QList<QString> filelist() const;
  59. QString imagedir() const;
  60. QString downloadtype() const;
  61. QString networktype() const;
  62. signals:
  63. void urlChanged();
  64. void apiChanged();
  65. void loginChanged();
  66. void filenameChanged();
  67. void contactlistChanged();
  68. void filelistChanged();
  69. void imagedirChanged();
  70. void downloadtypeChanged();
  71. void networktypeChanged();
  72. void downloaded(QString type, QString url, QString filename, int i);
  73. void downloadedjson(QString type, QString url, QString filename, int i,QJsonObject jsonObject);
  74. void success(QByteArray data, QString api);
  75. void error(QString data, QString url,QString api, int code);
  76. public slots:
  77. void setUrl(QString url);
  78. void setApi(QString api);
  79. void setLogin(QString login);
  80. void setDownloadtype(QString downloadtype);
  81. void setFilename(QString filename);
  82. void setContactlist(QList<QString> filename);
  83. void setFilelist(QList<QString> filename);
  84. void setImagedir(QString filename);
  85. void setParam(QString name, QString value);
  86. void setImageFileParam(QString name, QString url);
  87. void clearParams();
  88. void post();
  89. void get();
  90. void getlist();
  91. void download();
  92. // void networktype();
  93. private slots:
  94. void onReplyError(QNetworkReply::NetworkError code);
  95. void onReplySuccess();
  96. void onRequestFinished();
  97. void onReadyRead();
  98. void onSSLError(const QList<QSslError> &errors);
  99. //void updateDownloadProgress(qint64 bytesRead, qint64 totalBytes);
  100. private:
  101. QByteArray buffer;
  102. QString m_url;
  103. QString m_api;
  104. QString m_login;
  105. QString m_filename;
  106. QString m_downloadtype;
  107. QString m_networktype;
  108. QHash<QString, QString> params;
  109. QHash<QString, QString> files;
  110. QList<QString> m_filelist;
  111. QList<QString> m_contactlist;
  112. QString m_imagedir;
  113. int dlindex;
  114. QNetworkAccessManager manager;
  115. QNetworkRequest request;
  116. QNetworkReply *reply;
  117. QNetworkConfiguration nc;
  118. QString bufferToString();
  119. };
  120. #endif // XHR_H