From c7b814337a3bb2465f38dbc40cf225a012a1ec3e Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 29 Oct 2015 21:47:47 +0100 Subject: [PATCH] Use proper QFlags Now the ShareTypes and Permissions are part of the Share class (which is a bit better abstracted away). --- src/gui/ocssharejob.cpp | 2 +- src/gui/ocssharejob.h | 22 ++-------------------- src/gui/share.cpp | 35 ++++++++++++++++++----------------- src/gui/share.h | 39 +++++++++++++++++++++++++++++++-------- src/gui/sharedialog.cpp | 3 +-- src/gui/sharedialog.h | 5 +++-- 6 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/gui/ocssharejob.cpp b/src/gui/ocssharejob.cpp index f5a871bb4..fe6afa65f 100644 --- a/src/gui/ocssharejob.cpp +++ b/src/gui/ocssharejob.cpp @@ -83,7 +83,7 @@ void OcsShareJob::setPublicUpload(const QString &shareId, bool publicUpload) start(); } -void OcsShareJob::createShare(const QString &path, ShareType shareType, const QString &password, const QDate &date) +void OcsShareJob::createShare(const QString &path, Share::ShareType shareType, const QString &password, const QDate &date) { setVerb("POST"); diff --git a/src/gui/ocssharejob.h b/src/gui/ocssharejob.h index c029bd852..c970df920 100644 --- a/src/gui/ocssharejob.h +++ b/src/gui/ocssharejob.h @@ -15,6 +15,7 @@ #define OCSSHAREJOB_H #include "ocsjob.h" +#include "share.h" #include #include #include @@ -32,25 +33,6 @@ class OcsShareJob : public OcsJob { Q_OBJECT public: - /** - * Support sharetypes - */ - enum ShareType : int { - Link = 3 - }; - - /** - * Possible permissions - */ - enum Permission : int { - Read = 1, - Update = 2, - Create = 4, - Delete = 8, - Share = 16, - All = 31 - }; - /** * Constructor for new shares or listing of shares */ @@ -99,7 +81,7 @@ public: * @param password Optionally a password for the share * @param date Optionally an expire date for the share */ - void createShare(const QString& path, ShareType shareType, const QString& password = "", const QDate& date = QDate()); + void createShare(const QString& path, Share::ShareType shareType, const QString& password = "", const QDate& date = QDate()); signals: /** diff --git a/src/gui/share.cpp b/src/gui/share.cpp index 06ce06613..7a62fe313 100644 --- a/src/gui/share.cpp +++ b/src/gui/share.cpp @@ -19,8 +19,11 @@ namespace OCC { -Share::Share(AccountPtr account, const QString& id, const QString& path, int shareType, - int permissions) +Share::Share(AccountPtr account, + const QString& id, + const QString& path, + ShareType shareType, + Permissions permissions) : _account(account), _id(id), _path(path), @@ -35,12 +38,12 @@ QString Share::getId() const return _id; } -int Share::getShareType() const +Share::ShareType Share::getShareType() const { return _shareType; } -int Share::getPermissions() const +Share::Permissions Share::getPermissions() const { return _permissions; } @@ -81,12 +84,11 @@ bool LinkShare::isPasswordSet() const LinkShare::LinkShare(AccountPtr account, const QString& id, const QString& path, - int shareType, - int permissions, + Permissions permissions, bool passwordSet, const QUrl& url, const QDate& expireDate) -: Share(account, id, path, shareType, permissions), +: Share(account, id, path, Share::TypeLink, permissions), _passwordSet(passwordSet), _expireDate(expireDate), _url(url) @@ -96,8 +98,8 @@ LinkShare::LinkShare(AccountPtr account, bool LinkShare::getPublicUpload() { - return ((_permissions & OcsShareJob::Permission::Update) && - (_permissions & OcsShareJob::Permission::Create)); + return ((_permissions & PermissionUpdate) && + (_permissions & PermissionCreate)); } void LinkShare::setPublicUpload(bool publicUpload) @@ -112,9 +114,9 @@ void LinkShare::slotPublicUploadSet(const QVariantMap&, const QVariant &value) { //TODO FIX permission with names if (value.toBool()) { - _permissions = 7; + _permissions = PermissionRead | PermissionUpdate | PermissionCreate; } else { - _permissions = 1; + _permissions = PermissionRead; } emit publicUploadSet(); @@ -161,7 +163,7 @@ void ShareManager::createLinkShare(const QString &path, OcsShareJob *job = new OcsShareJob(_account, this); connect(job, SIGNAL(shareJobFinished(QVariantMap, QVariant)), SLOT(slotLinkShareCreated(QVariantMap))); connect(job, SIGNAL(ocsError(int, QString)), SLOT(slotOcsError(int, QString))); - job->createShare(path, OcsShareJob::ShareType::Link, password); + job->createShare(path, Share::TypeLink, password); } void ShareManager::slotLinkShareCreated(const QVariantMap &reply) @@ -208,14 +210,14 @@ void ShareManager::slotSharesFetched(const QVariantMap &reply) QSharedPointer newShare; - if (shareType == OcsShareJob::ShareType::Link) { + if (shareType == Share::TypeLink) { newShare = parseLinkShare(data); } else { newShare = QSharedPointer(new Share(_account, data.value("id").toString(), data.value("path").toString(), - shareType, - data.value("permissions").toInt())); + (Share::ShareType)shareType, + (Share::Permissions)data.value("permissions").toInt())); } shares.append(QSharedPointer(newShare)); @@ -249,8 +251,7 @@ QSharedPointer ShareManager::parseLinkShare(const QVariantMap &data) return QSharedPointer(new LinkShare(_account, data.value("id").toString(), data.value("path").toString(), - data.value("share_type").toInt(), - data.value("permissions").toInt(), + (Share::Permissions)data.value("permissions").toInt(), data.value("share_with").isValid(), url, expireDate)); diff --git a/src/gui/share.h b/src/gui/share.h index fca2c2482..41c710605 100644 --- a/src/gui/share.h +++ b/src/gui/share.h @@ -30,14 +30,37 @@ class Share : public QObject { public: + /** + * Possible share types + */ + enum ShareType { + TypeUser = 0, + TypeGroup = 1, + TypeLink = 3, + TypeRemote = 6, + }; + Q_DECLARE_FLAGS(ShareTypes, ShareType) + + /** + * Possible permissions + */ + enum Permission { + PermissionRead = 1, + PermissionUpdate = 2, + PermissionCreate = 4, + PermissionDelete = 8, + PermissionShare = 16 + }; + Q_DECLARE_FLAGS(Permissions, Permission) + /* * Constructor for shares */ explicit Share(AccountPtr account, const QString& id, const QString& path, - int shareType, - int permissions); + ShareType shareType, + Permissions permissions); /* * Get the id @@ -47,12 +70,12 @@ public: /* * Get the shareType */ - int getShareType() const; + ShareType getShareType() const; /* * Get permissions */ - int getPermissions() const; + Permissions getPermissions() const; /* * Set the permissions of a share @@ -79,8 +102,8 @@ protected: AccountPtr _account; QString _id; QString _path; - int _shareType; - int _permissions; + ShareType _shareType; + Permissions _permissions; protected slots: void slotOcsError(int statusCode, const QString &message); @@ -102,8 +125,7 @@ public: explicit LinkShare(AccountPtr account, const QString& id, const QString& path, - int shareType, - int permissions, + Permissions permissions, bool passwordSet, const QUrl& url, const QDate& expireDate); @@ -168,6 +190,7 @@ private: QDate _expireDate; QUrl _url; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(Share::Permissions) /** * The share manager allows for creating, retrieving and deletion diff --git a/src/gui/sharedialog.cpp b/src/gui/sharedialog.cpp index 4cdc28b05..3b47057b7 100644 --- a/src/gui/sharedialog.cpp +++ b/src/gui/sharedialog.cpp @@ -23,7 +23,6 @@ #include "configfile.h" #include "capabilities.h" -#include "ocssharejob.h" #include "thumbnailjob.h" #include "share.h" @@ -272,7 +271,7 @@ void ShareDialog::slotSharesFetched(const QList> &shares) Q_FOREACH(auto share, shares) { - if (share->getShareType() == static_cast(OcsShareJob::ShareType::Link)) { + if (share->getShareType() == Share::TypeLink) { _share = qSharedPointerDynamicCast(share); _ui->pushButton_copy->show(); diff --git a/src/gui/sharedialog.h b/src/gui/sharedialog.h index fea70152b..6b0c089ca 100644 --- a/src/gui/sharedialog.h +++ b/src/gui/sharedialog.h @@ -22,8 +22,6 @@ #include #include -#include "share.h" - namespace OCC { namespace Ui { @@ -33,6 +31,9 @@ class ShareDialog; class AbstractCredentials; class QuotaInfo; class SyncResult; +class LinkShare; +class Share; +class ShareManager; /** * @brief The ShareDialog class