diff --git a/CMakeLists.txt b/CMakeLists.txt index 47db02f362581022379de023759165276a7724e7..1281812f33c2f7b65fcf85bfbdaa4c3f713301f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,7 +237,7 @@ SET( libringclient_LIB_SRCS src/collectioneditor.cpp src/fallbackpersoncollection.cpp src/daemoncertificatecollection.cpp - src/fallbacklocalcertificatecollection.cpp + src/foldercertificatecollection.cpp #Communication src/dbus/configurationmanager.cpp @@ -330,7 +330,7 @@ SET( libringclient_LIB_HDRS src/collectioneditor.hpp src/fallbackpersoncollection.h src/daemoncertificatecollection.h - src/fallbacklocalcertificatecollection.h + src/foldercertificatecollection.h src/securityflaw.h src/collectioncreationinterface.h src/collectionconfigurationinterface.h diff --git a/src/callmodel.cpp b/src/callmodel.cpp index 402c5e42abaf72cc4e8cd5b6387c479c9bfc28a8..1fe531bacfc8164172ccc0905a48043d56388348 100644 --- a/src/callmodel.cpp +++ b/src/callmodel.cpp @@ -1051,7 +1051,7 @@ void CallModelPrivate::slotCallStateChanged(const QString& callID, const QString //Add to history if (call->lifeCycleState() == Call::LifeCycleState::FINISHED) { if (!call->collection()) { - foreach (CollectionInterface* backend, CategorizedHistoryModel::instance()->collections(CollectionInterface::ADD)) { + foreach (CollectionInterface* backend, CategorizedHistoryModel::instance()->collections(CollectionInterface::SupportedFeatures::ADD)) { if (backend->editor<Call>()->addNew(call)) call->setCollection(backend); } diff --git a/src/categorizedbookmarkmodel.cpp b/src/categorizedbookmarkmodel.cpp index 66df2e0ae15cfc2290b116c9329648e9fbd8657a..39abc18d491b9908e10561dc7126f8c607842f17 100644 --- a/src/categorizedbookmarkmodel.cpp +++ b/src/categorizedbookmarkmodel.cpp @@ -582,7 +582,7 @@ bool CategorizedBookmarkModel::removeItemCallback(const ContactMethod* item) bool CategorizedBookmarkModel::clearAllCollections() const { foreach (CollectionInterface* backend, collections()) { - if (backend->supportedFeatures() & CollectionInterface::ADD) { + if (backend->supportedFeatures() & CollectionInterface::SupportedFeatures::ADD) { backend->clear(); } } diff --git a/src/categorizedhistorymodel.cpp b/src/categorizedhistorymodel.cpp index 12a4a9675bd80d87e68aab71f1ce5d7d3ba0fd23..8b60ace4a231c42c2959218dfd7bc174899016cc 100644 --- a/src/categorizedhistorymodel.cpp +++ b/src/categorizedhistorymodel.cpp @@ -617,7 +617,7 @@ void CategorizedHistoryModel::collectionAddedCallback(CollectionInterface* backe bool CategorizedHistoryModel::clearAllCollections() const { foreach (CollectionInterface* backend, collections()) { //TODO use the filter API - if (backend->supportedFeatures() & CollectionInterface::CLEAR) { + if (backend->supportedFeatures() & CollectionInterface::SupportedFeatures::CLEAR) { backend->clear(); } } diff --git a/src/certificatemodel.cpp b/src/certificatemodel.cpp index e2d842cc6a3cc5eba5eba4bb11d7440ca9c2adc6..4a37eb6892b68c1a78eb5e5eecacdcfbaa72fe5c 100644 --- a/src/certificatemodel.cpp +++ b/src/certificatemodel.cpp @@ -32,7 +32,7 @@ //Ring #include "certificate.h" #include "account.h" -#include "fallbacklocalcertificatecollection.h" +#include "foldercertificatecollection.h" #include "private/matrixutils.h" enum class DetailType : uchar @@ -112,7 +112,7 @@ private: }; //TODO remove -static FallbackLocalCertificateCollection* m_pFallbackCollection = nullptr; +static FolderCertificateCollection* m_pFallbackCollection = nullptr; CertificateModel* CertificateModelPrivate::m_spInstance = nullptr; @@ -141,7 +141,7 @@ CertificateModel::CertificateModel(QObject* parent) : QAbstractItemModel(parent) { setObjectName("CertificateModel"); //TODO replace with something else - m_pFallbackCollection = addCollection<FallbackLocalCertificateCollection>(); + m_pFallbackCollection = addCollection<FolderCertificateCollection>(); m_pFallbackCollection->load(); } diff --git a/src/collectioninterface.h b/src/collectioninterface.h index 345e94a935f2eb157c623b2cb4b9f6fbc68a4ed5..11d4d0a77e6b587e661a8c8d8e6065ececaf98d0 100644 --- a/src/collectioninterface.h +++ b/src/collectioninterface.h @@ -61,7 +61,7 @@ public: * are implemented anyway, each backend should list what is officially supposed to work and what is * not. */ - enum SupportedFeatures { + enum class SupportedFeatures { NONE = 0x0 , LOAD = 0x1 << 0, /*!< Load this backend, DO NOT load anything before "load" is called */ SAVE = 0x1 << 1, /*!< Save an item */ @@ -83,11 +83,6 @@ public: typedef QByteArray Element; - //Constructor - template<typename T> - explicit CollectionInterface(CollectionEditor<T>* editor, CollectionInterface* parent = nullptr); - virtual ~CollectionInterface(); - //Generic information getters /** @@ -132,7 +127,7 @@ public: * * @see SupportedFeatures */ - virtual SupportedFeatures supportedFeatures() const = 0; + virtual FlagPack<SupportedFeatures> supportedFeatures() const = 0; //Management methods @@ -260,6 +255,12 @@ public: CollectionConfigurationInterface* configurator() const; protected: + + //Constructor + template<typename T> + explicit CollectionInterface(CollectionEditor<T>* editor, CollectionInterface* parent = nullptr); + virtual ~CollectionInterface(); + void setConfigurator(std::function<CollectionConfigurationInterface*()> getter); void addChildren(CollectionInterface* c); @@ -275,6 +276,8 @@ private: CollectionInterfacePrivate* d_ptr; }; +DECLARE_ENUM_FLAGS(CollectionInterface::SupportedFeatures) + #include <collectioninterface.hpp> #endif diff --git a/src/collectionmanagerinterface.h b/src/collectionmanagerinterface.h index 8920c56f5de1a53f1e70f151b6b83732661436a6..f212b2444ff27b4eb59202b8c1d1b08320a8d156 100644 --- a/src/collectionmanagerinterface.h +++ b/src/collectionmanagerinterface.h @@ -48,8 +48,8 @@ class CollectionConfigurationInterface; */ class LIB_EXPORT CollectionManagerInterfaceBase { public: - virtual bool hasEnabledCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const = 0; - virtual bool hasCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const = 0; + virtual bool hasEnabledCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const = 0; + virtual bool hasCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const = 0; ///Enable / disable a collection virtual bool enableCollection( CollectionInterface* collection, bool enabled) = 0; @@ -140,12 +140,12 @@ public: /// Do this manager have active collections - virtual bool hasEnabledCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; - virtual bool hasCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual bool hasEnabledCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual bool hasCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; /// List all Collections - virtual const QVector< CollectionInterface* > collections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; - virtual const QVector< CollectionInterface* > enabledCollections(CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual const QVector< CollectionInterface* > collections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual const QVector< CollectionInterface* > enabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; ///Enable / disable a collection virtual bool enableCollection( CollectionInterface* collection, bool enabled) final; diff --git a/src/collectionmanagerinterface.hpp b/src/collectionmanagerinterface.hpp index a17da5ddeda37ae568c813d01670b5df82cbf10f..fa0d2851ebc774ab47e5fc89c936bf960e8b441a 100644 --- a/src/collectionmanagerinterface.hpp +++ b/src/collectionmanagerinterface.hpp @@ -33,7 +33,7 @@ public: CollectionManagerInterface<T>* i_ptr; CollectionMediator<T>* itemMediator() const; - inline const QVector< CollectionInterface* > filterCollections(QVector< CollectionInterface* > in, CollectionInterface::SupportedFeatures features) const; + inline const QVector< CollectionInterface* > filterCollections(QVector< CollectionInterface* > in, FlagPack<CollectionInterface::SupportedFeatures> features) const; }; template<class T> @@ -113,7 +113,7 @@ CollectionManagerInterface<T>::CollectionManagerInterface(QAbstractItemModel* se } template<class T> -const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filterCollections(QVector< CollectionInterface* > in, CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filterCollections(QVector< CollectionInterface* > in, FlagPack<CollectionInterface::SupportedFeatures> features) const { QVector< CollectionInterface* > out; for (CollectionInterface* col : in) { @@ -129,7 +129,7 @@ const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filt * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections(CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lCollections, features); @@ -143,7 +143,7 @@ const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledCollections(CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lEnabledCollections, features); @@ -157,7 +157,7 @@ const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledColl * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -bool CollectionManagerInterface<T>::hasEnabledCollections(CollectionInterface::SupportedFeatures features) const +bool CollectionManagerInterface<T>::hasEnabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lEnabledCollections, features).size(); @@ -171,7 +171,7 @@ bool CollectionManagerInterface<T>::hasEnabledCollections(CollectionInterface::S * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -bool CollectionManagerInterface<T>::hasCollections(CollectionInterface::SupportedFeatures features) const +bool CollectionManagerInterface<T>::hasCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lCollections, features).size(); diff --git a/src/collectionmodel.cpp b/src/collectionmodel.cpp index 0898b5769c21c877c3590e78bd499100f14e60c0..8162e532701647a26795b08556675f8c3df11208 100644 --- a/src/collectionmodel.cpp +++ b/src/collectionmodel.cpp @@ -128,7 +128,7 @@ QVariant CollectionModel::data (const QModelIndex& idx, int role) const //Retro-map to the SupportedFeatures //WARNING if SupportedFeatures change, this need to be updated if (role > Qt::UserRole && role <= Qt::UserRole+14) { - return (bool) (role == Qt::UserRole+1 ? true : item->collection->supportedFeatures() & (0x01 << (role - Qt::UserRole - 2))); + return (bool) (role == Qt::UserRole+1 ? true : bool(item->collection->supportedFeatures() & ((CollectionInterface::SupportedFeatures)(0x01 << (role - Qt::UserRole - 2))))); } } else { diff --git a/src/daemoncertificatecollection.cpp b/src/daemoncertificatecollection.cpp index cd92899eb82eae9fa243b854e5ad9b2aaab93be6..d9febb4678f4052bb3c7754bb15fa199182f4b51 100644 --- a/src/daemoncertificatecollection.cpp +++ b/src/daemoncertificatecollection.cpp @@ -94,15 +94,15 @@ QByteArray DaemonCertificateCollection::id() const return "DaemonCertificateCollection"; } -CollectionInterface::SupportedFeatures DaemonCertificateCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> DaemonCertificateCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::REMOVE | CollectionInterface::SupportedFeatures::LISTABLE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } diff --git a/src/daemoncertificatecollection.h b/src/daemoncertificatecollection.h index b0f7dbbb00b49903348e38bb1901f49ece6a33fb..299aeec67a6ae63e50d46ef4a0b7f7c85dc87f22 100644 --- a/src/daemoncertificatecollection.h +++ b/src/daemoncertificatecollection.h @@ -48,7 +48,7 @@ public: virtual bool isEnabled() const override; virtual QByteArray id () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: DaemonCertificateCollectionPrivate* d_ptr; diff --git a/src/fallbackpersoncollection.cpp b/src/fallbackpersoncollection.cpp index a94c44c29d6b5a74c6ab0b785ee0c25eb1f0c980..1df40ac90bd7c55837bcd373cbd4d8be1ae12f6b 100644 --- a/src/fallbackpersoncollection.cpp +++ b/src/fallbackpersoncollection.cpp @@ -211,15 +211,15 @@ bool FallbackPersonCollection::reload() return false; } -CollectionInterface::SupportedFeatures FallbackPersonCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> FallbackPersonCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::MANAGEABLE | CollectionInterface::SupportedFeatures::REMOVE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } bool FallbackPersonCollection::clear() diff --git a/src/fallbackpersoncollection.h b/src/fallbackpersoncollection.h index 956e9e806e1eeb957b82a4fa039d4ec3cb8be726..1a2570aca9e5d762052137ef46352ffb8730137b 100644 --- a/src/fallbackpersoncollection.h +++ b/src/fallbackpersoncollection.h @@ -50,7 +50,7 @@ public: virtual bool isEnabled() const override; virtual QByteArray id () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: FallbackPersonCollectionPrivate* d_ptr; diff --git a/src/fallbacklocalcertificatecollection.cpp b/src/foldercertificatecollection.cpp similarity index 83% rename from src/fallbacklocalcertificatecollection.cpp rename to src/foldercertificatecollection.cpp index c1d5df8dafc0dc499e628d27df11ee48e4670388..fa73d175a958a9bfc3d1a59953b0f5bce102ae98 100644 --- a/src/fallbacklocalcertificatecollection.cpp +++ b/src/foldercertificatecollection.cpp @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ -#include "fallbacklocalcertificatecollection.h" +#include "foldercertificatecollection.h" //Qt #include <QtCore/QThread> @@ -71,13 +71,13 @@ class BackgroundLoader : public QThread { Q_OBJECT public: - BackgroundLoader(FallbackLocalCertificateCollection* parent); + BackgroundLoader(FolderCertificateCollection* parent); //Attributes QList<QByteArray> m_lIdList ; QMutex m_LoaderMutex; QList<QByteArray> m_lQueue ; - FallbackLocalCertificateCollection* m_pFallbackCollection; + FolderCertificateCollection* m_pFallbackCollection; //Helpers QByteArray loadCertificate(const QByteArray& id); @@ -89,7 +89,7 @@ Q_SIGNALS: void listLoaded(const QList<QByteArray>& list); }; -class FallbackLocalCertificateCollectionPrivate +class FolderCertificateCollectionPrivate { public: @@ -97,13 +97,17 @@ public: QList<CollectionInterface::Element> getCertificateList(); }; -FallbackLocalCertificateCollection::FallbackLocalCertificateCollection(CollectionMediator<Certificate>* mediator, const QString& path) : -CollectionInterface(new FallbackLocalCertificateEditor(mediator,path)),d_ptr(new FallbackLocalCertificateCollectionPrivate()) +FolderCertificateCollection::FolderCertificateCollection(CollectionMediator<Certificate>* mediator, + const QString& path , + const FlagPack<Options>& options , + const QString& name + ) : +CollectionInterface(new FallbackLocalCertificateEditor(mediator,path)),d_ptr(new FolderCertificateCollectionPrivate()) { } -FallbackLocalCertificateCollection::~FallbackLocalCertificateCollection() +FolderCertificateCollection::~FolderCertificateCollection() { delete d_ptr; } @@ -120,7 +124,7 @@ QByteArray BackgroundLoader::loadCertificate(const QByteArray& id) return file.readAll(); } -bool FallbackLocalCertificateCollection::load() +bool FolderCertificateCollection::load() { //Load the stored certificates BackgroundLoader* loader = new BackgroundLoader(this); @@ -136,55 +140,55 @@ bool FallbackLocalCertificateCollection::load() return false; } -bool FallbackLocalCertificateCollection::reload() +bool FolderCertificateCollection::reload() { return false; } -QList<CollectionInterface::Element> FallbackLocalCertificateCollection::listId() const +QList<CollectionInterface::Element> FolderCertificateCollection::listId() const { return d_ptr->getCertificateList(); } -bool FallbackLocalCertificateCollection::clear() +bool FolderCertificateCollection::clear() { return false; } -QString FallbackLocalCertificateCollection::name() const +QString FolderCertificateCollection::name() const { return QObject::tr("Local certificate store"); } -QString FallbackLocalCertificateCollection::category() const +QString FolderCertificateCollection::category() const { return QObject::tr("Certificate"); } -QVariant FallbackLocalCertificateCollection::icon() const +QVariant FolderCertificateCollection::icon() const { return QVariant(); } -bool FallbackLocalCertificateCollection::isEnabled() const +bool FolderCertificateCollection::isEnabled() const { return true; } -QByteArray FallbackLocalCertificateCollection::id() const +QByteArray FolderCertificateCollection::id() const { - return "FallbackLocalCertificateCollection"; + return "FolderCertificateCollection"; } -CollectionInterface::SupportedFeatures FallbackLocalCertificateCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> FolderCertificateCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LISTABLE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::REMOVE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } @@ -264,13 +268,13 @@ QVector<Certificate*> FallbackLocalCertificateEditor::items() const * * ******************************************************************************/ -BackgroundLoader::BackgroundLoader(FallbackLocalCertificateCollection* parent) : QThread(nullptr), +BackgroundLoader::BackgroundLoader(FolderCertificateCollection* parent) : QThread(nullptr), m_pFallbackCollection(parent) { } -QList<CollectionInterface::Element> FallbackLocalCertificateCollectionPrivate::getCertificateList() +QList<CollectionInterface::Element> FolderCertificateCollectionPrivate::getCertificateList() { // QMutexLocker(&this->m_Mutex); QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)+"/certs/"); @@ -299,4 +303,4 @@ void BackgroundLoader::run() QThread::exit(0); } -#include "fallbacklocalcertificatecollection.moc" +#include "foldercertificatecollection.moc" diff --git a/src/fallbacklocalcertificatecollection.h b/src/foldercertificatecollection.h similarity index 60% rename from src/fallbacklocalcertificatecollection.h rename to src/foldercertificatecollection.h index 6cb32a2139b7faed823d2cf088158867d8505fe5..b001a571c784b95cc216c8aae12e92d49f856098 100644 --- a/src/fallbacklocalcertificatecollection.h +++ b/src/foldercertificatecollection.h @@ -15,11 +15,13 @@ * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ +#ifndef FOLDERCERTIFICATECOLLECTION_H +#define FOLDERCERTIFICATECOLLECTION_H #include <collectioninterface.h> #include <typedefs.h> -class FallbackLocalCertificateCollectionPrivate; +class FolderCertificateCollectionPrivate; class Certificate; template<typename T> class CollectionMediator; @@ -29,11 +31,23 @@ template<typename T> class CollectionMediator; * locally by LibRingClient. If the platform have a certificate hosting facility, * it is strongly encourages to use it rather than this. */ -class LIB_EXPORT FallbackLocalCertificateCollection : public CollectionInterface +class LIB_EXPORT FolderCertificateCollection : public CollectionInterface { public: - explicit FallbackLocalCertificateCollection(CollectionMediator<Certificate>* mediator, const QString& path = QString()); - virtual ~FallbackLocalCertificateCollection(); + ///@enum Options load and behavior parameters for a FolderCertificateCollection + enum class Options { + READ_WRITE = 0 << 0, /*!< Try to add certificates to that store (default) */ + READ_ONLY = 1 << 0, /*!< Do not try to add certificate to that store */ + RECURSIVE = 1 << 1, /*!< Read all sub-folders recursively */ + TOP_LEVEL = 1 << 2, /*!< Consider those certificates as top-level entities */ + }; + + explicit FolderCertificateCollection(CollectionMediator<Certificate>* mediator, + const QString& path = QString(), + const FlagPack<Options>& options = Options::READ_WRITE, + const QString& name = QString() + ); + virtual ~FolderCertificateCollection(); virtual bool load () override; virtual bool reload() override; @@ -46,10 +60,13 @@ public: virtual QByteArray id () const override; virtual QList<Element> listId () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: - FallbackLocalCertificateCollectionPrivate* d_ptr; - Q_DECLARE_PRIVATE(FallbackLocalCertificateCollection) + FolderCertificateCollectionPrivate* d_ptr; + Q_DECLARE_PRIVATE(FolderCertificateCollection) }; -Q_DECLARE_METATYPE(FallbackLocalCertificateCollection*) \ No newline at end of file +Q_DECLARE_METATYPE(FolderCertificateCollection*) +DECLARE_ENUM_FLAGS(FolderCertificateCollection::Options) + +#endif diff --git a/src/numbercategory.cpp b/src/numbercategory.cpp index 756293642da5f1d846596cdf90e3612d843ab6e7..83171cc0c28d2f179fab84f77675af1c646b8c49 100644 --- a/src/numbercategory.cpp +++ b/src/numbercategory.cpp @@ -96,12 +96,11 @@ int NumberCategory::key() const return d_ptr->m_Key; } -CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> NumberCategory::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( - CollectionInterface::SupportedFeatures::NONE | - CollectionInterface::SupportedFeatures::MANAGEABLE | - CollectionInterface::SupportedFeatures::LOAD ); + return CollectionInterface::SupportedFeatures::NONE | + CollectionInterface::SupportedFeatures::MANAGEABLE | + CollectionInterface::SupportedFeatures::LOAD ; } void NumberCategory::setIcon(const QVariant& pixmap) diff --git a/src/numbercategory.h b/src/numbercategory.h index 33ff30214476020d1136e3f77dffb7c84a5398cc..ec82cd4159f19000ecee60c5a4b1c99e401d569d 100644 --- a/src/numbercategory.h +++ b/src/numbercategory.h @@ -50,7 +50,7 @@ public: QVariant icon(bool isTracked, bool isPresent = false) const; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; virtual bool load() override; //Setter diff --git a/src/profilemodel.cpp b/src/profilemodel.cpp index 7f86804b36fc0817b299ebe8ed3db3f342ffcc8d..8a3fbe21a7ab18a218a9ab4a56629d96a01eb131 100644 --- a/src/profilemodel.cpp +++ b/src/profilemodel.cpp @@ -83,7 +83,7 @@ public: virtual QByteArray id ( ) const override; virtual bool load ( ) override; virtual bool reload ( ) override; - SupportedFeatures supportedFeatures( ) const override; + FlagPack<SupportedFeatures> supportedFeatures( ) const override; //Attributes bool m_needSaving; @@ -395,16 +395,16 @@ bool ProfileContentBackend::saveAll() return true; } -ProfileContentBackend::SupportedFeatures ProfileContentBackend::supportedFeatures() const +FlagPack<ProfileContentBackend::SupportedFeatures> ProfileContentBackend::supportedFeatures() const { - return (ProfileContentBackend::SupportedFeatures)(SupportedFeatures::NONE - | SupportedFeatures::LOAD //= 0x1 << 0, /* Load this backend, DO NOT load anything before "load" is called */ - | SupportedFeatures::EDIT //= 0x1 << 2, /* Edit, but **DOT NOT**, save an item) */ - | SupportedFeatures::ADD //= 0x1 << 4, /* Add (and save) a new item to the backend */ - | SupportedFeatures::SAVE_ALL //= 0x1 << 5, /* Save all items at once, this may or may not be faster than "add" */ - | SupportedFeatures::REMOVE //= 0x1 << 7, /* Remove a single item */ - | SupportedFeatures::ENABLEABLE //= 0x1 << 10, /*Can be enabled, I know, it is not a word, but Java use it too */ - | SupportedFeatures::MANAGEABLE); //= 0x1 << 12, /* Can be managed the config GUI */ + return SupportedFeatures::NONE + | SupportedFeatures::LOAD //= 0x1 << 0, /* Load this backend, DO NOT load anything before "load" is called */ + | SupportedFeatures::EDIT //= 0x1 << 2, /* Edit, but **DOT NOT**, save an item) */ + | SupportedFeatures::ADD //= 0x1 << 4, /* Add (and save) a new item to the backend */ + | SupportedFeatures::SAVE_ALL //= 0x1 << 5, /* Save all items at once, this may or may not be faster than "add" */ + | SupportedFeatures::REMOVE //= 0x1 << 7, /* Remove a single item */ + | SupportedFeatures::ENABLEABLE //= 0x1 << 10, /*Can be enabled, I know, it is not a word, but Java use it too */ + | SupportedFeatures::MANAGEABLE; //= 0x1 << 12, /* Can be managed the config GUI */ //TODO ^^ Remove that one once debugging is done } diff --git a/src/transitionalpersonbackend.cpp b/src/transitionalpersonbackend.cpp index 32dbd5d18bca9607f10e80fa0a9b071f78f38e24..9200b95e2633f8f7c96a6130ff6cae9fe3f00cd6 100644 --- a/src/transitionalpersonbackend.cpp +++ b/src/transitionalpersonbackend.cpp @@ -138,7 +138,7 @@ bool TransitionalPersonBackend::isEnabled() const return false; } -CollectionInterface::SupportedFeatures TransitionalPersonBackend::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> TransitionalPersonBackend::supportedFeatures() const { return CollectionInterface::SupportedFeatures::NONE; } diff --git a/src/transitionalpersonbackend.h b/src/transitionalpersonbackend.h index 3f854011526679c1a75e0e513d6a8455166f2bc6..4a7301f3ba7144e3d9f78e4eb177c8b7a235d4a0 100644 --- a/src/transitionalpersonbackend.h +++ b/src/transitionalpersonbackend.h @@ -55,7 +55,7 @@ public: virtual QString name () const override; virtual QString category () const override; virtual QVariant icon () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; //Mutators virtual bool load ( ) override; diff --git a/src/typedefs.h b/src/typedefs.h index ba28af33185e850ab402523fd0eb8dce6397edca..09ca00d574d485a95c6c96b1d384b38b608f4db5 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -61,5 +61,73 @@ template<typename A> constexpr int enum_class_size() { #define IGNORE_NULL(content) content #endif //ENABLE_IGNORE_NULL +/** + * Create a safe pack of flags from an enum class. + * + * This class exist to ensure all sources come from the same enum and that it is + * never accidentally accidentally into an integer. + * + * This assume that the enum has been setup as flags. + */ +template<class T> +class LIB_EXPORT FlagPack +{ +public: + FlagPack(const T& base) : m_Flags(static_cast<int>(base)) {} + + //Operator + FlagPack<T>& operator|(const T& other) { + m_Flags |= static_cast<int>(other); + return *this; + } + + FlagPack<T>& operator|(const FlagPack<T>& other) { + m_Flags |= other.m_Flags; + return *this; + } + + FlagPack<T> operator&(const T& other) { + return FlagPack<T>(m_Flags & static_cast<int>(other)); + } + + FlagPack<T> operator&(const FlagPack<T>& other) { + return FlagPack<T>(m_Flags & other.m_Flags); + } + + bool operator!=(const T& other) { + return m_Flags != static_cast<int>(other); + } + + bool operator==(const T& other) { + return m_Flags == static_cast<int>(other); + } + + bool operator==(const FlagPack<T>& other) { + return m_Flags == other.m_Flags; + } + + bool operator!() { + return !m_Flags; + } + + operator bool() { + return m_Flags != 0; + } + + int value() const { + return m_Flags; + } + +private: + FlagPack(int base) : m_Flags(base) {} + int m_Flags; +}; + +#define DECLARE_ENUM_FLAGS(T)\ +static FlagPack<T> operator|(const T& first, const T& second) { \ + FlagPack<T> p (first); \ + return p | second; \ +} + #endif //TYPEDEFS_H