TheTruePadawan

test1

Nov 9th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "qpastebin.h"
  2. #include <QFile>
  3. #include <QTextStream>
  4. #include <QJsonDocument>
  5. #include <QJsonObject>
  6.  
  7. QPasteBin::QPasteBin(QObject *parent) : QObject(parent)
  8. {
  9.     manager = new QNetworkAccessManager(parent);
  10.    // QNetworkReply *pasteData = nullptr;
  11.     //QNetworkReply *apiData = nullptr;
  12.  
  13.     requestApi(); // CAUSES CRASH ON WIN 7
  14. //    connect(manager, &QNetworkAccessManager::destroyed,this,[=]{
  15. //        pasteData->deleteLater();
  16. //        apiData->deleteLater();
  17. //    });
  18. }
  19.  
  20. void QPasteBin::setUpPasting(QString code, QString name, PASTE_MODE mode)
  21. {
  22.     if (API_KEY.isEmpty())
  23.     {
  24.         requestApi();
  25.     }
  26.     _code = code;
  27.     _mode = mode;
  28.     _name = name;
  29.     setUp();
  30. }
  31.  
  32. void QPasteBin::setUp()
  33. {
  34.     postData.addQueryItem("api_dev_key",API_KEY);
  35.     postData.addQueryItem("api_paste_code",_code);
  36.     postData.addQueryItem("api_option", "paste");
  37.     postData.addQueryItem("api_paste_private",QString::number(_mode));
  38.     postData.addQueryItem("api_paste_name",_name);
  39.     postData.addQueryItem("api_paste_format","cpp");
  40. }
  41.  
  42. void QPasteBin::paste()
  43. {
  44.     QNetworkRequest req{QUrl("https://pastebin.com/api/api_post.php")};
  45.     req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
  46.     auto pasteData = manager->post(req, postData.toString(QUrl::FullyEncoded).toUtf8());
  47.  
  48.     //connect(pasteData,&QNetworkReply::finished,this,&QPasteBin::readPasteData);
  49.     connect(pasteData,&QNetworkReply::finished,this, [=] {
  50.         auto buffer = pasteData->readAll();
  51.         val = QUrl::fromPercentEncoding(buffer);
  52.         emit complete();
  53.     });
  54. }
  55.  
  56. //void QPasteBin::readPasteData()
  57. //{
  58. //    auto buffer = pasteData->readAll();
  59. //    val = QUrl::fromPercentEncoding(buffer);
  60. //    //pasteData->re();
  61. //    emit complete();
  62. //}
  63.  
  64. //void QPasteBin::readApiData()
  65. //{
  66. //    qDebug() << "READING API DATA...........";
  67. //    if (apiData->error() != QNetworkReply::NoError)
  68. //    {
  69. //        qDebug() << "Error Retrieving KEY " << apiData->errorString();
  70. //    }else
  71. //    {
  72. //        auto apiDataBuffer = apiData->readAll();
  73. //        auto keyObject = QJsonDocument::fromJson(apiDataBuffer).object();
  74. //        API_KEY = keyObject.value("key").toString();
  75. //        qDebug() << "API DATA " << API_KEY;
  76. //    }
  77. //    qDebug() << "FINISHED READING API DATA...........";
  78. //    //apiData->deleteLater();
  79. //}
  80.  
  81. void QPasteBin::requestApi()
  82. {
  83.     QNetworkRequest API_REQ{QUrl{"https://notepad-proxy.herokuapp.com/api"}};
  84.     auto apiData = manager->get(API_REQ);
  85.     //connect(apiData, &QNetworkReply::finished,this,&QPasteBin::readApiData);
  86.  
  87.     connect(apiData, &QNetworkReply::finished,this, [=] {
  88.         qDebug() << "READING API DATA...........";
  89.         if (apiData->error() != QNetworkReply::NoError)
  90.         {
  91.             qDebug() << "Error Retrieving KEY " << apiData->errorString();
  92.         }else
  93.         {
  94.             auto apiDataBuffer = apiData->readAll();
  95.             auto keyObject = QJsonDocument::fromJson(apiDataBuffer).object();
  96.             API_KEY = keyObject.value("key").toString();
  97.             qDebug() << "API DATA " << API_KEY;
  98.         }
  99.         qDebug() << "FINISHED READING API DATA...........";
  100.         apiData->deleteLater();
  101.     });
  102. }
  103.  
  104. QString QPasteBin::getLink()
  105. {
  106.     return val;
  107. }
  108.  
  109. QPasteBin::~QPasteBin()
  110. {
  111. //    delete pasteData;
  112. //    delete apiData;
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment