File indexing completed on 2024-05-12 05:15:07

0001 /*
0002   Copyright (c) 2016 Sandro Knauß <sknauss@kde.org>
0003 
0004   This program is free software; you can redistribute it and/or modify
0005   it under the terms of the GNU General Public License as published by
0006   the Free Software Foundation; either version 2 of the License, or
0007   (at your option) any later version.
0008 
0009   This program is distributed in the hope that it will be useful,
0010   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012   GNU General Public License for more details.
0013 
0014   You should have received a copy of the GNU General Public License along
0015   with this program; if not, write to the Free Software Foundation, Inc.,
0016   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include <KDAV2/Utils>
0020 #include <KDAV2/DavCollectionDeleteJob>
0021 #include <KDAV2/DavCollectionModifyJob>
0022 #include <KDAV2/DavCollectionsFetchJob>
0023 #include <KDAV2/DavItemFetchJob>
0024 #include <KDAV2/DavItemCreateJob>
0025 #include <KDAV2/DavItemsFetchJob>
0026 #include <KDAV2/DavItemModifyJob>
0027 #include <KDAV2/DavItemDeleteJob>
0028 #include <KDAV2/DavItemsListJob>
0029 #include <KDAV2/Utils>
0030 
0031 #include <QtCore/QDebug>
0032 #include <QtCore/QStringList>
0033 #include <QCoreApplication>
0034 
0035 int main(int argc, char **argv)
0036 {
0037     QCoreApplication app(argc, argv);
0038 
0039     QUrl mainUrl(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org"));
0040     mainUrl.setUserName(QStringLiteral("test1@kolab.org"));
0041     mainUrl.setPassword(QStringLiteral("Welcome2KolabSystems"));
0042     KDAV2::DavUrl davUrl(mainUrl, KDAV2::CardDav);
0043 
0044     auto *job = new KDAV2::DavCollectionsFetchJob(davUrl);
0045     job->exec();
0046     if (job->error()) {
0047         qWarning() << "DavCollectionsFetchJob failed " << job->errorString();
0048     } else {
0049         qInfo() << "DavCollectionsFetchJob succeeded";
0050     }
0051 
0052     for(const auto &collection : job->collections()) {
0053         qInfo() << collection.displayName() << "PRIVS: " << collection.privileges();
0054         auto collectionUrl = collection.url();
0055         int anz = -1;
0056         {
0057             auto itemListJob = new KDAV2::DavItemsListJob(collectionUrl);
0058             itemListJob->exec();
0059             anz = itemListJob->items().size();
0060             qInfo() << "items:" << itemListJob->items().size();
0061             for (const auto &item : itemListJob->items()) {
0062                 qInfo() << item.url().url() << item.contentType() << item.data();
0063                 auto itemFetchJob = new KDAV2::DavItemFetchJob(item);
0064                 itemFetchJob->exec();
0065                 const auto fetchedItem = itemFetchJob->item();
0066                 qInfo() << fetchedItem.contentType() << fetchedItem.data();
0067 
0068                 auto itemsFetchJob = new KDAV2::DavItemsFetchJob(collectionUrl, QStringList() << item.url().toDisplayString());
0069                 itemsFetchJob->exec();
0070                 if (itemsFetchJob->item(item.url().toDisplayString()).contentType() != fetchedItem.contentType()) {       //itemsfetchjob do not get contentType
0071                     qInfo() << "Fetched same item but got different contentType:" << itemsFetchJob->item(item.url().toDisplayString()).contentType();
0072                 }
0073 
0074                 if (itemsFetchJob->item(item.url().toDisplayString()).data() != fetchedItem.data()) {
0075                     qInfo() << "Fetched same item but got different data:" << itemsFetchJob->item(item.url().toDisplayString()).data();
0076                 }
0077             }
0078         }
0079         {
0080             qInfo() << "second run: (should be empty).";
0081             auto itemListJob = new KDAV2::DavItemsListJob(collectionUrl);
0082             itemListJob->exec();
0083             if (itemListJob->items().size() != anz) {
0084                 qInfo() << "Items have added/deleted on server.";
0085             }
0086         }
0087     }
0088 
0089     {
0090         QUrl url(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/cbbf386d-7e9b-4e72-947d-0b813ea9b347/"));
0091         url.setUserInfo(mainUrl.userInfo());
0092         KDAV2::DavUrl collectionUrl(url, KDAV2::CardDav);
0093         auto collectionDeleteJob = new KDAV2::DavCollectionDeleteJob(collectionUrl);
0094         collectionDeleteJob->exec();
0095         if (collectionDeleteJob->error()) {
0096             qWarning() << "Delete job failed: " << collectionDeleteJob->errorString();
0097         }
0098     }
0099 
0100     {
0101         QUrl url(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/9290e784-c876-412f-8385-be292d64b2c6/"));
0102         url.setUserInfo(mainUrl.userInfo());
0103         KDAV2::DavUrl testCollectionUrl(url, KDAV2::CardDav);
0104         auto collectionModifyJob = new KDAV2::DavCollectionModifyJob(testCollectionUrl);
0105         collectionModifyJob->setProperty(QStringLiteral("displayname"), QStringLiteral("test234"));
0106         collectionModifyJob->exec();
0107         if (collectionModifyJob->error()) {
0108             qWarning() << "Modify job failed:" << collectionModifyJob->errorString();
0109         }
0110     }
0111 
0112     //create element with "wrong put url" test if we get the correct url back
0113     {
0114         QUrl url(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/9290e784-c876-412f-8385-be292d64b2c6/xxx.vcf"));
0115         url.setUserInfo(mainUrl.userInfo());
0116         KDAV2::DavUrl testItemUrl(url, KDAV2::CardDav);
0117         QByteArray data = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Kolab//iRony DAV Server 0.3.1//Sabre//Sabre VObject 2.1.7//EN\r\nUID:12345678-1234-1234-1234-123456789abc\r\nFN:John Doe\r\nN:Doe;John;;;\r\nEMAIL;TYPE=INTERNET;TYPE=HOME:john.doe@example.com\r\nREV;VALUE=DATE-TIME:20161221T145611Z\r\nEND:VCARD\r\n";
0118         KDAV2::DavItem item(testItemUrl, QStringLiteral("text/x-vcard"), data, QString());
0119         auto createJob = new KDAV2::DavItemCreateJob(item);
0120         createJob->exec();
0121         if (createJob->error()) {
0122             qWarning() << "Create job failed: " << createJob->errorString();
0123         }
0124         if (createJob->item().url().toDisplayString() != QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/9290e784-c876-412f-8385-be292d64b2c6/12345678-1234-1234-1234-123456789abc.vcf")) {
0125             qWarning() << "unexpected url" << createJob->item().url().url();
0126         }
0127     }
0128 
0129     {
0130         QUrl url(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/9290e784-c876-412f-8385-be292d64b2c6/12345678-1234-1234-1234-123456789abc.vcf"));
0131         url.setUserInfo(mainUrl.userInfo());
0132         KDAV2::DavUrl testItemUrl(url, KDAV2::CardDav);
0133         QByteArray data = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Kolab//iRony DAV Server 0.3.1//Sabre//Sabre VObject 2.1.7//EN\r\nUID:12345678-1234-1234-1234-123456789abc\r\nFN:John2 Doe\r\nN:Doe;John2;;;\r\nEMAIL;TYPE=INTERNET;TYPE=HOME:john2.doe@example.com\r\nREV;VALUE=DATE-TIME:20161221T145611Z\r\nEND:VCARD\r\n";
0134         KDAV2::DavItem item(testItemUrl, QStringLiteral("text/x-vcard"), data, QString());
0135         auto modifyJob = new KDAV2::DavItemModifyJob(item);
0136         modifyJob->exec();
0137         if (modifyJob->error()) {
0138             qWarning() << "Modify job failed " << modifyJob->errorString();
0139         }
0140     }
0141 
0142     {
0143         QUrl url(QStringLiteral("https://apps.kolabnow.com/addressbooks/test1%40kolab.org/9290e784-c876-412f-8385-be292d64b2c6/12345678-1234-1234-1234-123456789abc.vcf"));
0144         url.setUserInfo(mainUrl.userInfo());
0145         KDAV2::DavUrl testItemUrl(url, KDAV2::CardDav);
0146         QByteArray data = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Kolab//iRony DAV Server 0.3.1//Sabre//Sabre VObject 2.1.7//EN\r\nUID:12345678-1234-1234-1234-123456789abc\r\nFN:John2 Doe\r\nN:Doe;John2;;;\r\nEMAIL;TYPE=INTERNET;TYPE=HOME:john2.doe@example.com\r\nREV;VALUE=DATE-TIME:20161221T145611Z\r\nEND:VCARD\r\n";
0147         KDAV2::DavItem item(testItemUrl, QStringLiteral("text/x-vcard"), data, QString());
0148         auto deleteJob = new KDAV2::DavItemDeleteJob(item);
0149         deleteJob->exec();
0150         if (deleteJob->error()) {
0151             qWarning() << "Delete job failed " << deleteJob->errorString();
0152         }
0153     }
0154 }