2015-10-29 13:09:10 +03:00
|
|
|
/*
|
|
|
|
* 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* 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 "share.h"
|
|
|
|
#include "ocssharejob.h"
|
|
|
|
#include "account.h"
|
|
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2015-10-29 23:47:47 +03:00
|
|
|
Share::Share(AccountPtr account,
|
|
|
|
const QString& id,
|
|
|
|
const QString& path,
|
|
|
|
ShareType shareType,
|
|
|
|
Permissions permissions)
|
2015-10-29 16:27:49 +03:00
|
|
|
: _account(account),
|
2015-10-29 13:09:10 +03:00
|
|
|
_id(id),
|
|
|
|
_path(path),
|
|
|
|
_shareType(shareType),
|
|
|
|
_permissions(permissions)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-29 15:48:53 +03:00
|
|
|
QString Share::getId() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:47:47 +03:00
|
|
|
Share::ShareType Share::getShareType() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _shareType;
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:47:47 +03:00
|
|
|
Share::Permissions Share::getPermissions() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Share::deleteShare()
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotDeleted()));
|
|
|
|
connect(job, SIGNAL(ocsError(int, const QString &)), SLOT(slotOcsError(int, const QString &)));
|
2015-10-29 13:09:10 +03:00
|
|
|
job->deleteShare(getId());
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void Share::slotDeleted()
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
emit shareDeleted();
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void Share::slotOcsError(int statusCode, const QString &message)
|
|
|
|
{
|
|
|
|
emit serverError(statusCode, message);
|
|
|
|
}
|
|
|
|
|
2015-10-29 15:48:53 +03:00
|
|
|
QUrl LinkShare::getLink() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _url;
|
|
|
|
}
|
|
|
|
|
2015-10-29 15:48:53 +03:00
|
|
|
QDate LinkShare::getExpireDate() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _expireDate;
|
|
|
|
}
|
|
|
|
|
2015-10-29 15:48:53 +03:00
|
|
|
bool LinkShare::isPasswordSet() const
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
return _passwordSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkShare::LinkShare(AccountPtr account,
|
|
|
|
const QString& id,
|
|
|
|
const QString& path,
|
2015-10-29 23:47:47 +03:00
|
|
|
Permissions permissions,
|
2015-10-29 13:09:10 +03:00
|
|
|
bool passwordSet,
|
|
|
|
const QUrl& url,
|
2015-10-29 16:27:49 +03:00
|
|
|
const QDate& expireDate)
|
2015-10-29 23:47:47 +03:00
|
|
|
: Share(account, id, path, Share::TypeLink, permissions),
|
2015-10-29 13:09:10 +03:00
|
|
|
_passwordSet(passwordSet),
|
|
|
|
_expireDate(expireDate),
|
|
|
|
_url(url)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:47:14 +03:00
|
|
|
bool LinkShare::getPublicUpload()
|
|
|
|
{
|
2015-10-29 23:47:47 +03:00
|
|
|
return ((_permissions & PermissionUpdate) &&
|
|
|
|
(_permissions & PermissionCreate));
|
2015-10-29 13:47:14 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 13:09:10 +03:00
|
|
|
void LinkShare::setPublicUpload(bool publicUpload)
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotPublicUploadSet(QVariantMap, QVariant)));
|
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString)));
|
2015-10-29 13:09:10 +03:00
|
|
|
job->setPublicUpload(getId(), publicUpload);
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void LinkShare::slotPublicUploadSet(const QVariantMap&, const QVariant &value)
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
//TODO FIX permission with names
|
|
|
|
if (value.toBool()) {
|
2015-10-29 23:47:47 +03:00
|
|
|
_permissions = PermissionRead | PermissionUpdate | PermissionCreate;
|
2015-10-29 13:09:10 +03:00
|
|
|
} else {
|
2015-10-29 23:47:47 +03:00
|
|
|
_permissions = PermissionRead;
|
2015-10-29 13:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
emit publicUploadSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkShare::setPassword(const QString &password)
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotPasswordSet(QVariantMap, QVariant)));
|
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString)));
|
2015-10-29 13:09:10 +03:00
|
|
|
job->setPassword(getId(), password);
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void LinkShare::slotPasswordSet(const QVariantMap&, const QVariant &value)
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
_passwordSet = value.toString() == "";
|
|
|
|
emit passwordSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkShare::setExpireDate(const QDate &date)
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotExpireDateSet(QVariantMap, QVariant)));
|
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString)));
|
2015-10-29 13:09:10 +03:00
|
|
|
job->setExpireDate(getId(), date);
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void LinkShare::slotExpireDateSet(const QVariantMap&, const QVariant &value)
|
2015-10-29 13:09:10 +03:00
|
|
|
{
|
|
|
|
_expireDate = value.toDate();
|
|
|
|
emit expireDateSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
ShareManager::ShareManager(AccountPtr account, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
_account(account)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareManager::createLinkShare(const QString &path,
|
|
|
|
const QString &password)
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotLinkShareCreated(QVariantMap)));
|
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString)));
|
2015-10-29 23:47:47 +03:00
|
|
|
job->createShare(path, Share::TypeLink, password);
|
2015-10-29 13:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShareManager::slotLinkShareCreated(const QVariantMap &reply)
|
|
|
|
{
|
|
|
|
QString message;
|
|
|
|
int code = OcsShareJob::getJsonReturnCode(reply, message);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before we had decent sharing capabilities on the server a 403 "generally"
|
2015-10-29 15:39:29 +03:00
|
|
|
* meant that a share was password protected
|
2015-10-29 13:09:10 +03:00
|
|
|
*/
|
|
|
|
if (code == 403) {
|
|
|
|
emit linkShareRequiresPassword();
|
2015-10-29 15:31:36 +03:00
|
|
|
return;
|
2015-10-29 18:56:23 +03:00
|
|
|
}
|
2015-10-29 13:09:10 +03:00
|
|
|
|
|
|
|
//Parse share
|
|
|
|
auto data = reply.value("ocs").toMap().value("data").toMap();
|
|
|
|
QSharedPointer<LinkShare> share(parseLinkShare(data));
|
|
|
|
|
|
|
|
emit linkShareCreated(share);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareManager::fetchShares(const QString &path)
|
|
|
|
{
|
|
|
|
OcsShareJob *job = new OcsShareJob(_account, this);
|
2015-10-29 18:56:23 +03:00
|
|
|
connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotSharesFetched(QVariantMap)));
|
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString)));
|
2015-10-29 13:09:10 +03:00
|
|
|
job->getShares(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareManager::slotSharesFetched(const QVariantMap &reply)
|
|
|
|
{
|
|
|
|
auto tmpShares = reply.value("ocs").toMap().value("data").toList();
|
|
|
|
const QString versionString = _account->serverVersion();
|
|
|
|
qDebug() << Q_FUNC_INFO << versionString << "Fetched" << tmpShares.count() << "shares";
|
|
|
|
|
|
|
|
QList<QSharedPointer<Share>> shares;
|
|
|
|
|
2015-10-29 16:00:28 +03:00
|
|
|
foreach(const auto &share, tmpShares) {
|
2015-10-29 13:09:10 +03:00
|
|
|
auto data = share.toMap();
|
|
|
|
|
|
|
|
auto shareType = data.value("share_type").toInt();
|
|
|
|
|
2015-10-29 16:27:49 +03:00
|
|
|
QSharedPointer<Share> newShare;
|
2015-10-29 13:09:10 +03:00
|
|
|
|
2015-10-29 23:47:47 +03:00
|
|
|
if (shareType == Share::TypeLink) {
|
2015-10-29 13:09:10 +03:00
|
|
|
newShare = parseLinkShare(data);
|
|
|
|
} else {
|
2015-10-29 16:27:49 +03:00
|
|
|
newShare = QSharedPointer<Share>(new Share(_account,
|
|
|
|
data.value("id").toString(),
|
|
|
|
data.value("path").toString(),
|
2015-10-29 23:47:47 +03:00
|
|
|
(Share::ShareType)shareType,
|
|
|
|
(Share::Permissions)data.value("permissions").toInt()));
|
2015-10-29 13:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
shares.append(QSharedPointer<Share>(newShare));
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "Sending " << shares.count() << "shares";
|
|
|
|
emit sharesFetched(shares);
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:27:49 +03:00
|
|
|
QSharedPointer<LinkShare> ShareManager::parseLinkShare(const QVariantMap &data) {
|
2015-10-29 13:09:10 +03:00
|
|
|
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());
|
2015-10-29 15:48:53 +03:00
|
|
|
} else if (_account->serverVersionInt() >= (8 << 16)) {
|
2015-10-29 13:09:10 +03:00
|
|
|
// From ownCloud server version 8 on, a different share link scheme is used.
|
|
|
|
url = QUrl(Account::concatUrlPath(_account->url(), QString("index.php/s/%1").arg(data.value("token").toString())).toString());
|
|
|
|
} else {
|
|
|
|
QList<QPair<QString, QString>> queryArgs;
|
|
|
|
queryArgs.append(qMakePair(QString("service"), QString("files")));
|
|
|
|
queryArgs.append(qMakePair(QString("t"), data.value("token").toString()));
|
|
|
|
url = QUrl(Account::concatUrlPath(_account->url(), QLatin1String("public.php"), queryArgs).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QDate expireDate;
|
|
|
|
if (data.value("expiration").isValid()) {
|
|
|
|
expireDate = QDate::fromString(data.value("expiration").toString(), "yyyy-MM-dd 00:00:00");
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:27:49 +03:00
|
|
|
return QSharedPointer<LinkShare>(new LinkShare(_account,
|
|
|
|
data.value("id").toString(),
|
|
|
|
data.value("path").toString(),
|
2015-10-29 23:47:47 +03:00
|
|
|
(Share::Permissions)data.value("permissions").toInt(),
|
2015-10-29 16:27:49 +03:00
|
|
|
data.value("share_with").isValid(),
|
|
|
|
url,
|
|
|
|
expireDate));
|
2015-10-29 13:09:10 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 18:56:23 +03:00
|
|
|
void ShareManager::slotOcsError(int statusCode, const QString &message)
|
|
|
|
{
|
|
|
|
emit serverError(statusCode, message);
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:09:10 +03:00
|
|
|
}
|