File indexing completed on 2024-04-28 04:44:40

0001 #include "CardDAV.hpp"
0002 
0003 CardDAV::CardDAV(QString host, QString username, QString password) {
0004   this->host = host;
0005   this->username = username;
0006   this->password = password;
0007 
0008   this->networkHelper = new NetworkHelper(host, username, password);
0009   this->xmlHelper = new XMLHelper();
0010 }
0011 
0012 CardDAV::~CardDAV() { delete this->networkHelper; }
0013 
0014 CardDAVReply* CardDAV::testConnection() {
0015   CardDAVReply* reply = new CardDAVReply();
0016   QMap<QString, QString> headers;
0017   QNetworkReply* testReply;
0018 
0019   testReply = this->networkHelper->makeRequest("PROPFIND", headers);
0020 
0021   this->connect(testReply, &QNetworkReply::finished, [=]() {
0022     if (testReply->error()) {
0023       reply->sendTestConnectionResponseSignal(false);
0024       reply->sendError(testReply->error());
0025     }
0026     bool found = false;
0027     QList<CardDAVResponseItem*> responseList =
0028         this->xmlHelper->parseCardDAVMultiStatusResponse(testReply->readAll());
0029     QString host(this->host);
0030     QUrl hostUrl(host.replace("://", "").replace(0, host.indexOf('/'), ""));
0031 
0032     for (CardDAVResponseItem* item : responseList) {
0033       //      qDebug() << item->getHref().matches(hostUrl,
0034       //      QUrl::StripTrailingSlash)
0035       //               << item->isAddressBook();
0036 
0037       if (item->getHref().matches(hostUrl, QUrl::StripTrailingSlash) &&
0038           item->isAddressBook()) {
0039         found = true;
0040 
0041         break;
0042       }
0043     }
0044 
0045     if (found) {
0046       reply->sendTestConnectionResponseSignal(true);
0047     } else {
0048       reply->sendTestConnectionResponseSignal(false);
0049     }
0050   });
0051 
0052   this->connect(
0053       testReply,
0054       QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0055       [=](QNetworkReply::NetworkError err) {
0056         reply->sendTestConnectionResponseSignal(false);
0057         reply->sendError(err);
0058       });
0059 
0060   return reply;
0061 }
0062 
0063 CardDAVReply* CardDAV::listAllContacts() {
0064   CardDAVReply* reply = new CardDAVReply();
0065   QMap<QString, QString> headers;
0066   QNetworkReply* listReply;
0067   QString requestXml =
0068       "<card:addressbook-query xmlns:d=\"DAV:\" xmlns:card=\"urn:ietf:params:xml:ns:carddav\"> \
0069                             <d:prop> \
0070                                 <card:address-data /> \
0071                                 <d:getetag /> \
0072                             </d:prop> \
0073                         </card:addressbook-query>";
0074 
0075   headers.insert("Depth", "1");
0076   headers.insert("Content-Type", "application/xml");
0077 
0078   listReply = this->networkHelper->makeRequest("REPORT", headers, requestXml);
0079 
0080   this->connect(listReply, &QNetworkReply::finished, [=]() {
0081     QList<CardDAVResponseItem*> responses =
0082         this->xmlHelper->parseCardDAVMultiStatusResponse(listReply->readAll());
0083     QList<Contact*> contacts;
0084 
0085     for (CardDAVResponseItem* response : responses) {
0086       if (response->isContact()) {
0087         contacts.push_front(response->getContact());
0088       }
0089     }
0090 
0091     reply->sendListContactsResponseSignal(contacts);
0092   });
0093 
0094   this->connect(
0095       listReply,
0096       QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0097       [=](QNetworkReply::NetworkError err) { reply->sendError(err); });
0098 
0099   return reply;
0100 }
0101 
0102 CardDAVReply* CardDAV::createContact(QString uid, QString vCard,
0103                                      bool shouldOverwrite) {
0104   CardDAVReply* reply = new CardDAVReply();
0105   QMap<QString, QString> headers;
0106   QNetworkReply* createReply;
0107   QUrl contactUrl(this->host + "/" + uid + ".vcf");
0108 
0109   if (!shouldOverwrite) {
0110     headers.insert("If-None-Match", "*");
0111   }
0112 
0113   createReply =
0114       this->networkHelper->makeRequest("PUT", contactUrl, headers, vCard);
0115 
0116   this->connect(createReply, &QNetworkReply::finished, [=]() {
0117     if (createReply->error()) {
0118       reply->sendError(createReply->error());
0119     } else {
0120       QMap<QString, QString> getContactHeaders;
0121       QNetworkReply* getContactReply = this->networkHelper->makeRequest(
0122           "GET", contactUrl.path(), getContactHeaders, "");
0123       this->connect(getContactReply, &QNetworkReply::finished, [=]() {
0124         if (getContactReply->error()) {
0125           reply->sendError(getContactReply->error());
0126         } else {
0127           QString vCard = getContactReply->readAll();
0128           QString etag(getContactReply->rawHeader("ETag"));
0129 
0130           reply->sendCreateContactResponseSignal(
0131               new Contact(vCard, etag, QUrl(contactUrl.path())));
0132         }
0133       });
0134 
0135       this->connect(
0136           getContactReply,
0137           QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0138           [=](QNetworkReply::NetworkError err) { reply->sendError(err); });
0139     }
0140   });
0141 
0142   this->connect(
0143       createReply,
0144       QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0145       [=](QNetworkReply::NetworkError err) { reply->sendError(err); });
0146 
0147   return reply;
0148 }
0149 
0150 CardDAVReply* CardDAV::updateContact(QUrl href, QString vCard, QString etag) {
0151   CardDAVReply* reply = new CardDAVReply();
0152   QMap<QString, QString> headers;
0153   QNetworkReply* updateReply;
0154 
0155   headers.insert("If-Match", etag);
0156 
0157   updateReply = this->networkHelper->makeRequest("PUT", href, headers, vCard);
0158 
0159   this->connect(updateReply, &QNetworkReply::finished, [=]() {
0160     if (updateReply->error()) {
0161       reply->sendError(updateReply->error());
0162     } else {
0163       QMap<QString, QString> getContactHeaders;
0164       QNetworkReply* getContactReply =
0165           this->networkHelper->makeRequest("GET", href, getContactHeaders, "");
0166       this->connect(getContactReply, &QNetworkReply::finished, [=]() {
0167         if (getContactReply->error()) {
0168           reply->sendError(getContactReply->error());
0169         } else {
0170           QString vCard = getContactReply->readAll();
0171           QString etag(getContactReply->rawHeader("ETag"));
0172 
0173           reply->sendUpdateContactResponseSignal(
0174               new Contact(vCard, etag, href));
0175         }
0176       });
0177 
0178       this->connect(
0179           getContactReply,
0180           QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0181           [=](QNetworkReply::NetworkError err) { reply->sendError(err); });
0182     }
0183   });
0184 
0185   this->connect(
0186       updateReply,
0187       QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0188       [=](QNetworkReply::NetworkError err) {
0189         qDebug() << updateReply->readAll();
0190         reply->sendError(err);
0191       });
0192 
0193   return reply;
0194 }
0195 
0196 CardDAVReply* CardDAV::deleteContact(QUrl href) {
0197   CardDAVReply* reply = new CardDAVReply();
0198   QMap<QString, QString> headers;
0199   QNetworkReply* deleteReply =
0200       this->networkHelper->makeRequest("DELETE", href, headers, "");
0201 
0202   this->connect(deleteReply, &QNetworkReply::finished, [=]() {
0203     if (deleteReply->error()) {
0204       reply->sendError(deleteReply->error());
0205     } else {
0206       reply->sendDeleteContactResponseSignal();
0207     }
0208   });
0209 
0210   this->connect(
0211       deleteReply,
0212       QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
0213       [=](QNetworkReply::NetworkError err) { reply->sendError(err); });
0214 
0215   return reply;
0216 }