File indexing completed on 2025-01-05 04:58:41

0001 /*
0002  *   Copyright (C) 2018 Christian Mollekopf <chrigi_1@fastmail.fm>
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
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #pragma once
0021 
0022 #include "synchronizer.h"
0023 
0024 #include <KDAV2/DavCollection>
0025 #include <KDAV2/DavItem>
0026 #include <KDAV2/DavUrl>
0027 
0028 class WebDavSynchronizer : public Sink::Synchronizer
0029 {
0030 public:
0031     WebDavSynchronizer(const Sink::ResourceContext &, KDAV2::Protocol, const QByteArray &collectionName, const QByteArrayList &itemNames);
0032 
0033     QList<Synchronizer::SyncRequest> getSyncRequests(const Sink::QueryBase &query) Q_DECL_OVERRIDE;
0034     KAsync::Job<void> synchronizeWithSource(const Sink::QueryBase &query) Q_DECL_OVERRIDE;
0035 
0036 protected:
0037     KAsync::Job<QByteArray> createItem(const QByteArray &vcard, const QByteArray &contentType, const QByteArray &uid, const QByteArray &collectionRid);
0038     KAsync::Job<QByteArray> moveItem(const QByteArray &vcard, const QByteArray &contentType, const QByteArray &uid, const QByteArray &collectionRid, const QByteArray &oldRemoteId);
0039     KAsync::Job<QByteArray> modifyItem(const QByteArray &oldRemoteId, const QByteArray &vcard, const QByteArray &contentType, const QByteArray &collectionRid);
0040     KAsync::Job<QByteArray> removeItem(const QByteArray &oldRemoteId);
0041 
0042     KAsync::Job<QByteArray> createCollection(const KDAV2::DavCollection &collection, const KDAV2::Protocol protocol);
0043     KAsync::Job<QByteArray> removeCollection(const QByteArray &collectionRid);
0044     KAsync::Job<QByteArray> modifyCollection(const QByteArray &collectionRid, const KDAV2::DavCollection &collection);
0045 
0046     /**
0047      * Called with the list of discovered collections. It's purpose should be
0048      * adding the said collections to the store.
0049      */
0050     virtual void updateLocalCollections(KDAV2::DavCollection::List collections) = 0;
0051 
0052     /**
0053      * Called when discovering a new item, or when an item has been modified.
0054      * It's purpose should be adding the said item to the store.
0055      *
0056      * `collectionLocalId` is the local collection id of the item.
0057      */
0058     virtual void updateLocalItem(const KDAV2::DavItem &item, const QByteArray &collectionLocalId) = 0;
0059 
0060     KAsync::Job<void> synchronizeCollection(const KDAV2::DavUrl &collectionUrl, const QByteArray &collectionRid, const QByteArray &collectionLocalId, const QByteArray &ctag);
0061 
0062 
0063     static QByteArray resourceID(const KDAV2::DavCollection &);
0064     static QByteArray resourceID(const KDAV2::DavItem &);
0065 
0066     /**
0067      * Used to get the url of an item / collection with the given remote ID
0068      */
0069     KDAV2::DavUrl urlOf(const KDAV2::DavUrl &serverUrl, const QByteArray &remoteId);
0070 
0071     /**
0072      * Used to get the url of an item / collection with the given remote ID,
0073      * and append `itemPath` to the path of the URI.
0074      *
0075      * Useful when adding a new item to a collection
0076      */
0077     KDAV2::DavUrl urlOf(const KDAV2::DavUrl &serverUrl, const QByteArray &collectionRemoteId, const QString &itemPath);
0078 
0079 private:
0080     KAsync::Job<KDAV2::DavUrl> discoverServer();
0081     KAsync::Job<QPair<QUrl, QStringList>> discoverHome(const KDAV2::DavUrl &serverUrl);
0082 
0083     KDAV2::Protocol mProtocol;
0084     const QByteArray mCollectionType;
0085     const QByteArrayList mEntityTypes;
0086 
0087     KDAV2::DavUrl mCachedServer;
0088     QUrl mServer;
0089     QString mUsername;
0090 };