2015-11-02 00:23:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Roeland Jago Douma <roeland@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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2015-11-02 00:23:22 +03:00
|
|
|
*
|
|
|
|
* 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 "sharee.h"
|
|
|
|
#include "ocsshareejob.h"
|
|
|
|
|
2017-04-26 12:38:10 +03:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
2015-11-02 00:23:22 +03:00
|
|
|
namespace OCC {
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcSharing, "gui.sharing", QtInfoMsg)
|
|
|
|
|
2015-11-02 00:23:22 +03:00
|
|
|
Sharee::Sharee(const QString shareWith,
|
|
|
|
const QString displayName,
|
|
|
|
const Type type)
|
|
|
|
: _shareWith(shareWith)
|
|
|
|
, _displayName(displayName)
|
|
|
|
, _type(type)
|
|
|
|
{
|
|
|
|
}
|
2015-11-05 00:00:35 +03:00
|
|
|
|
|
|
|
QString Sharee::format() const
|
|
|
|
{
|
|
|
|
QString formatted = _displayName;
|
|
|
|
|
|
|
|
if (_type == Type::Group) {
|
|
|
|
formatted += QLatin1String(" (group)");
|
|
|
|
} else if (_type == Type::Federated) {
|
|
|
|
formatted += QLatin1String(" (remote)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:23:22 +03:00
|
|
|
QString Sharee::shareWith() const
|
|
|
|
{
|
|
|
|
return _shareWith;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Sharee::displayName() const
|
|
|
|
{
|
|
|
|
return _displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sharee::Type Sharee::type() const
|
|
|
|
{
|
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:18:01 +03:00
|
|
|
ShareeModel::ShareeModel(const AccountPtr &account, const QString &type, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, _account(account)
|
|
|
|
, _type(type)
|
|
|
|
{
|
|
|
|
}
|
2015-11-05 00:00:35 +03:00
|
|
|
|
2015-11-16 16:18:01 +03:00
|
|
|
void ShareeModel::fetch(const QString &search, const ShareeSet &blacklist)
|
2015-11-02 00:23:22 +03:00
|
|
|
{
|
2015-11-16 16:18:01 +03:00
|
|
|
_search = search;
|
|
|
|
_shareeBlacklist = blacklist;
|
2015-11-11 15:28:20 +03:00
|
|
|
OcsShareeJob *job = new OcsShareeJob(_account);
|
2017-04-26 12:38:10 +03:00
|
|
|
connect(job, SIGNAL(shareeJobFinished(QJsonDocument)), SLOT(shareesFetched(QJsonDocument)));
|
2016-01-12 16:35:35 +03:00
|
|
|
connect(job, SIGNAL(ocsError(int, QString)), SIGNAL(displayErrorMessage(int, QString)));
|
2015-11-02 00:23:22 +03:00
|
|
|
job->getSharees(_search, _type, 1, 50);
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:38:10 +03:00
|
|
|
void ShareeModel::shareesFetched(const QJsonDocument &reply)
|
2015-11-02 00:23:22 +03:00
|
|
|
{
|
2017-04-26 12:38:10 +03:00
|
|
|
auto data = reply.object().value("ocs").toObject().value("data").toObject();
|
2015-11-02 00:23:22 +03:00
|
|
|
|
|
|
|
QVector<QSharedPointer<Sharee>> newSharees;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Todo properly loop all of this
|
|
|
|
*/
|
2017-04-26 12:38:10 +03:00
|
|
|
auto exact = data.value("exact").toObject();
|
2015-11-02 00:23:22 +03:00
|
|
|
{
|
2017-04-26 12:38:10 +03:00
|
|
|
auto users = exact.value("users").toArray();
|
2015-11-05 00:00:35 +03:00
|
|
|
foreach (auto user, users) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(user.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
2017-04-26 12:38:10 +03:00
|
|
|
auto groups = exact.value("groups").toArray();
|
2015-11-05 00:00:35 +03:00
|
|
|
foreach (auto group, groups) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(group.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
2017-04-26 12:38:10 +03:00
|
|
|
auto remotes = exact.value("remotes").toArray();
|
2015-11-05 00:00:35 +03:00
|
|
|
foreach (auto remote, remotes) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(remote.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-26 12:38:10 +03:00
|
|
|
auto users = data.value("users").toArray();
|
2015-11-02 00:23:22 +03:00
|
|
|
foreach (auto user, users) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(user.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-04-26 12:38:10 +03:00
|
|
|
auto groups = data.value("groups").toArray();
|
2015-11-02 00:23:22 +03:00
|
|
|
foreach (auto group, groups) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(group.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-04-26 12:38:10 +03:00
|
|
|
auto remotes = data.value("remotes").toArray();
|
2015-11-02 00:23:22 +03:00
|
|
|
foreach (auto remote, remotes) {
|
2017-04-26 12:38:10 +03:00
|
|
|
newSharees.append(parseSharee(remote.toObject()));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:30:34 +03:00
|
|
|
// Filter sharees that we have already shared with
|
|
|
|
QVector<QSharedPointer<Sharee>> filteredSharees;
|
|
|
|
foreach (const auto &sharee, newSharees) {
|
|
|
|
bool found = false;
|
|
|
|
foreach (const auto &blacklistSharee, _shareeBlacklist) {
|
|
|
|
if (sharee->type() == blacklistSharee->type() && sharee->shareWith() == blacklistSharee->shareWith()) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found == false) {
|
|
|
|
filteredSharees.append(sharee);
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 00:00:35 +03:00
|
|
|
|
2015-11-16 16:18:01 +03:00
|
|
|
setNewSharees(filteredSharees);
|
2015-11-05 00:00:35 +03:00
|
|
|
shareesReady();
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-26 12:38:10 +03:00
|
|
|
QSharedPointer<Sharee> ShareeModel::parseSharee(const QJsonObject &data)
|
2015-11-02 00:23:22 +03:00
|
|
|
{
|
|
|
|
const QString displayName = data.value("label").toString();
|
2017-04-26 12:38:10 +03:00
|
|
|
const QString shareWith = data.value("value").toObject().value("shareWith").toString();
|
|
|
|
Sharee::Type type = (Sharee::Type)data.value("value").toObject().value("shareType").toInt();
|
2015-11-02 00:23:22 +03:00
|
|
|
|
2015-11-27 16:46:39 +03:00
|
|
|
return QSharedPointer<Sharee>(new Sharee(shareWith, displayName, type));
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:52:13 +03:00
|
|
|
|
|
|
|
// Helper function for setNewSharees (could be a lambda when we can use them)
|
|
|
|
static QSharedPointer<Sharee> shareeFromModelIndex(const QModelIndex &idx)
|
|
|
|
{
|
|
|
|
return idx.data(Qt::UserRole).value<QSharedPointer<Sharee>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FindShareeHelper
|
|
|
|
{
|
|
|
|
const QSharedPointer<Sharee> &sharee;
|
|
|
|
bool operator()(const QSharedPointer<Sharee> &s2) const
|
|
|
|
{
|
|
|
|
return s2->format() == sharee->format() && s2->displayName() == sharee->format();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-16 16:18:01 +03:00
|
|
|
/* Set the new sharee
|
|
|
|
|
|
|
|
Do that while preserving the model index so the selection stays
|
|
|
|
*/
|
|
|
|
void ShareeModel::setNewSharees(const QVector<QSharedPointer<Sharee>> &newSharees)
|
|
|
|
{
|
|
|
|
layoutAboutToBeChanged();
|
|
|
|
const auto persistent = persistentIndexList();
|
|
|
|
QVector<QSharedPointer<Sharee>> oldPersistantSharee;
|
|
|
|
oldPersistantSharee.reserve(persistent.size());
|
|
|
|
|
|
|
|
std::transform(persistent.begin(), persistent.end(), std::back_inserter(oldPersistantSharee),
|
2015-12-03 14:52:13 +03:00
|
|
|
shareeFromModelIndex);
|
2015-11-16 16:18:01 +03:00
|
|
|
|
|
|
|
_sharees = newSharees;
|
|
|
|
|
|
|
|
QModelIndexList newPersistant;
|
|
|
|
newPersistant.reserve(persistent.size());
|
|
|
|
foreach (const QSharedPointer<Sharee> &sharee, oldPersistantSharee) {
|
2015-12-03 14:52:13 +03:00
|
|
|
FindShareeHelper helper = { sharee };
|
|
|
|
auto it = std::find_if(_sharees.constBegin(), _sharees.constEnd(), helper);
|
2015-11-16 16:18:01 +03:00
|
|
|
if (it == _sharees.constEnd()) {
|
|
|
|
newPersistant << QModelIndex();
|
|
|
|
} else {
|
|
|
|
newPersistant << index(it - _sharees.constBegin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changePersistentIndexList(persistent, newPersistant);
|
|
|
|
layoutChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-02 00:23:22 +03:00
|
|
|
int ShareeModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return _sharees.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ShareeModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2015-11-05 00:00:35 +03:00
|
|
|
if (index.row() < 0 || index.row() > _sharees.size()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:56:15 +03:00
|
|
|
const auto &sharee = _sharees.at(index.row());
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
return sharee->format();
|
|
|
|
|
|
|
|
} else if (role == Qt::EditRole) {
|
|
|
|
// This role is used by the completer - it should match
|
|
|
|
// the full name and the user name and thus we include both
|
|
|
|
// in the output here. But we need to take care this string
|
|
|
|
// doesn't leak to the user.
|
|
|
|
return QString(sharee->displayName() + " (" + sharee->shareWith() + ")");
|
|
|
|
|
|
|
|
} else if (role == Qt::UserRole) {
|
|
|
|
return QVariant::fromValue(sharee);
|
2015-11-09 19:21:42 +03:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
|
2015-11-02 00:23:22 +03:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2015-11-05 00:00:35 +03:00
|
|
|
QSharedPointer<Sharee> ShareeModel::getSharee(int at)
|
|
|
|
{
|
|
|
|
if (at < 0 || at > _sharees.size()) {
|
|
|
|
return QSharedPointer<Sharee>(NULL);
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
2015-11-05 00:00:35 +03:00
|
|
|
|
|
|
|
return _sharees.at(at);
|
2015-11-02 00:23:22 +03:00
|
|
|
}
|
|
|
|
}
|