Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sharemanager.cpp « gui « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5264051699bec424783669037b3af45dbbec1fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright (C) by Roeland Jago Douma <rullzer@owncloud.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "sharemanager.h"
#include "ocssharejob.h"
#include "account.h"
#include "folderman.h"
#include "accountstate.h"

#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

namespace OCC {

/**
 * When a share is modified, we need to tell the folders so they can adjust overlay icons
 */
static void updateFolder(const AccountPtr &account, const QString &path)
{
    for (auto *f : FolderMan::instance()->folders()) {
        if (f->accountState()->account() != account)
            continue;
        auto folderPath = f->remotePath();
        if (path.startsWith(folderPath) && (path == folderPath || folderPath.endsWith('/') || path[folderPath.size()] == '/')) {
            // Workaround the fact that the server does not invalidate the etags of parent directories
            // when something is shared.
            auto relative = path.midRef(f->remotePathTrailingSlash().length());
            f->journalDb()->schedulePathForRemoteDiscovery(relative.toString());

            // Schedule a sync so it can update the remote permission flag and let the socket API
            // know about the shared icon.
            f->scheduleThisFolderSoon();
        }
    }
}


Share::Share(AccountPtr account,
    const QString &id,
    const QString &path,
    const ShareType shareType,
    const Permissions permissions,
    const QSharedPointer<Sharee> shareWith)
    : _account(account)
    , _id(id)
    , _path(path)
    , _shareType(shareType)
    , _permissions(permissions)
    , _shareWith(shareWith)
{
}

AccountPtr Share::account() const
{
    return _account;
}

QString Share::path() const
{
    return _path;
}

QString Share::getId() const
{
    return _id;
}

Share::ShareType Share::getShareType() const
{
    return _shareType;
}

QSharedPointer<Sharee> Share::getShareWith() const
{
    return _shareWith;
}

void Share::setPermissions(Permissions permissions)
{
    auto *job = OcsShareJob::setPermissions(_account, this, getId(), permissions);
    connect(job, &JsonApiJob::finishedSignal, this, [job, permissions, this] {
        if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            _permissions = permissions;
            emit permissionsSet();
        }
    });
    job->start();
}

Share::Permissions Share::getPermissions() const
{
    return _permissions;
}

void Share::deleteShare()
{
    auto *job = OcsShareJob::deleteShare(_account, this, getId());
    connect(job, &JsonApiJob::finishedSignal, this, [job, this] {
        if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            emit shareDeleted();
            updateFolder(_account, _path);
        }
    });
    job->start();
}

QUrl LinkShare::getLink() const
{
    return _url;
}

QUrl LinkShare::getDirectDownloadLink() const
{
    QUrl url = _url;
    url.setPath(url.path() + "/download");
    return url;
}

QDate LinkShare::getExpireDate() const
{
    return _expireDate;
}

bool LinkShare::isPasswordSet() const
{
    return _passwordSet;
}

LinkShare::LinkShare(AccountPtr account,
    const QString &id,
    const QString &path,
    const QString &name,
    const QString &token,
    Permissions permissions,
    bool passwordSet,
    const QUrl &url,
    const QDate &expireDate)
    : Share(account, id, path, Share::TypeLink, permissions)
    , _name(name)
    , _token(token)
    , _passwordSet(passwordSet)
    , _expireDate(expireDate)
    , _url(url)
{
}

bool LinkShare::getPublicUpload() const
{
    return _permissions & SharePermissionCreate;
}

bool LinkShare::getShowFileListing() const
{
    return _permissions & SharePermissionRead;
}

QString LinkShare::getName() const
{
    return _name;
}

void LinkShare::setName(const QString &name)
{
    auto *job = OcsShareJob::setName(_account, this, getId(), name);
    connect(job, &JsonApiJob::finishedSignal, this, [job, name, this] {
        if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            _name = name;
            emit nameSet();
        }
    });
    job->start();
}

QString LinkShare::getToken() const
{
    return _token;
}

void LinkShare::setPassword(const QString &password)
{
    auto *job = OcsShareJob::setPassword(_account, this, getId(), password);
    connect(job, &JsonApiJob::finishedSignal, this, [job, password, this] {
        if (!job->ocsSuccess()) {
            emit passwordSetError(job->ocsStatus(), job->ocsMessage());
        } else {
            _passwordSet = !password.isEmpty();
            emit passwordSet();
        }
    });
    job->start();
}

void LinkShare::setExpireDate(const QDate &date)
{
    auto *job = OcsShareJob::setExpireDate(_account, this, getId(), date);
    connect(job, &JsonApiJob::finishedSignal, this, [job, date, this] {
        if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            auto data = job->data().value("ocs").toObject().value("data").toObject();
            /*
             * If the reply provides a data back (more REST style)
             * they use this date.
             */
            if (data.value("expiration").isString()) {
                _expireDate = QDate::fromString(data.value("expiration").toString(), "yyyy-MM-dd 00:00:00");
            } else {
                _expireDate = date;
            }
            emit expireDateSet();
        }
    });
    job->start();
}

ShareManager::ShareManager(AccountPtr account, QObject *parent)
    : QObject(parent)
    , _account(account)
{
}

void ShareManager::createLinkShare(const QString &path,
    const QString &name,
    const QString &password,
    const QDate &expireDate,
    const Share::Permissions permissions)
{
    auto *job = OcsShareJob::createLinkShare(_account, this, path, name, password, expireDate, permissions);
    connect(job, &JsonApiJob::finishedSignal, this, [job, password, this] {
        if (job->ocsStatus() == 403) {
            // A 403 generally means some of the settings for the share are not allowed.
            // Maybe a password is required, or the expire date isn't acceptable.
            emit linkShareCreationForbidden(job->ocsMessage());
        } else if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            // Parse share
            auto data = job->data().value("ocs").toObject().value("data").toObject();
            QSharedPointer<LinkShare> share(parseLinkShare(data));

            emit linkShareCreated(share);

            updateFolder(_account, share->path());
        }
    });
    job->start();
}

void ShareManager::createShare(const QString &path,
    const Share::ShareType shareType,
    const QString &shareWith,
    const Share::Permissions desiredPermissions)
{
    auto *job = OcsShareJob::getSharedWithMe(_account, this);
    connect(job, &JsonApiJob::finishedSignal, this, [=] {
        if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            // Note: The following code attempts to determine if the item was shared with
            // the user and what the permissions were. It doesn't do a good job at it since
            // the == path comparison will mean it doesn't work for subitems of shared
            // folders. Also, it's nicer if the calling code determines the share-permissions
            // (see maxSharingPermissions) via a PropFind and passes in valid permissions.
            // Remove this code for >= 2.7.0.
            // TODO: sigh

            // Find existing share permissions (if this was shared with us)
            Share::Permissions existingPermissions = SharePermissionDefault;
            const auto &array = job->data()[QLatin1String("ocs")].toObject()[QLatin1String("data")].toArray();
            for (const auto &element : array) {
                auto map = element.toObject();
                if (map["file_target"] == path)
                    existingPermissions = Share::Permissions(map["permissions"].toInt());
            }

            // Limit the permissions we request for a share to the ones the item
            // was shared with initially.
            auto validPermissions = desiredPermissions;
            if (validPermissions == SharePermissionDefault) {
                validPermissions = existingPermissions;
            }
            if (existingPermissions != SharePermissionDefault) {
                validPermissions &= existingPermissions;
            }

            auto *job2 = OcsShareJob::createShare(_account, this, path, shareType, shareWith, validPermissions);
            connect(job2, &JsonApiJob::finishedSignal, this, [job2, this] {
            if (!job2->ocsSuccess()) {
                emit serverError(job2->ocsStatus(), job2->ocsMessage());
            } else {
                //Parse share
                auto data = job2->data().value("ocs").toObject().value("data").toObject();
                QSharedPointer<Share> share(parseShare(data));

                emit shareCreated(share);
                updateFolder(_account, share->path());

            } });
            job2->start();
        }
    });
    job->start();
}

void ShareManager::fetchShares(const QString &path)
{
    auto *job = OcsShareJob::getShares(_account, this, path);
    connect(job, &JsonApiJob::finishedSignal, this, [job, path, this] {
        // 404 seems to be ok according to refactored code
        if (job->ocsStatus() == 404) {
            emit sharesFetched({});
        } else if (!job->ocsSuccess()) {
            emit serverError(job->ocsStatus(), job->ocsMessage());
        } else {
            const auto &tmpShares = job->data().value(QLatin1String("ocs")).toObject().value(QLatin1String("data")).toArray();
            qCDebug(lcSharing) << "Fetched" << tmpShares.count() << "shares";

            QList<QSharedPointer<Share>> shares;

            for (const auto &share : tmpShares) {
                auto data = share.toObject();

                auto shareType = data.value("share_type").toInt();

                QSharedPointer<Share> newShare;

                if (shareType == Share::TypeLink) {
                    newShare = parseLinkShare(data);
                } else {
                    newShare = parseShare(data);
                }
                shares.append(QSharedPointer<Share>(newShare));
            }
            qCDebug(lcSharing) << "Sending " << shares.count() << "shares";
            emit sharesFetched(shares);
        }
    });
    job->start();
}

QSharedPointer<LinkShare> ShareManager::parseLinkShare(const QJsonObject &data)
{
    QUrl url;

    // From ownCloud server 8.2 the url field is always set for public shares
    if (data.contains("url")) {
        url = QUrl(data.value("url").toString());
    } else {
        // From ownCloud server version 8 on, a different share link scheme is used.
        url = QUrl(Utility::concatUrlPath(_account->url(), QLatin1String("index.php/s/") + data.value("token").toString())).toString();
    }

    QDate expireDate;
    if (data.value("expiration").isString()) {
        expireDate = QDate::fromString(data.value("expiration").toString(), "yyyy-MM-dd 00:00:00");
    }

    return QSharedPointer<LinkShare>(new LinkShare(_account,
        data.value("id").toVariant().toString(), // "id" used to be an integer, support both
        data.value("path").toString(),
        data.value("name").toString(),
        data.value("token").toString(),
        (Share::Permissions)data.value("permissions").toInt(),
        data.value("share_with").isString(), // has password?
        url,
        expireDate));
}

QSharedPointer<Share> ShareManager::parseShare(const QJsonObject &data)
{
    QSharedPointer<Sharee> sharee(new Sharee(data.value("share_with").toString(),
        data.value("share_with_displayname").toString(),
        (Sharee::Type)data.value("share_type").toInt()));

    return QSharedPointer<Share>(new Share(_account,
        data.value("id").toVariant().toString(), // "id" used to be an integer, support both
        data.value("path").toString(),
        (Share::ShareType)data.value("share_type").toInt(),
        (Share::Permissions)data.value("permissions").toInt(),
        sharee));
}
}