Warning, file /office/calligra/libs/widgets/KoResourceServerAdapter.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 0003 Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com> 0004 Copyright (C) 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com> 0005 Copyright (c) 2013 Sascha Suelzer <s.suelzer@gmail.com> 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Lesser General Public 0009 License as published by the Free Software Foundation; either 0010 version 2.1 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Lesser General Public License for more details. 0016 0017 You should have received a copy of the GNU Lesser General Public 0018 License along with this library; if not, write to the Free Software 0019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0020 */ 0021 0022 0023 #ifndef KO_RESOURCESERVER_ADAPTER_H_ 0024 #define KO_RESOURCESERVER_ADAPTER_H_ 0025 0026 #include "KoResourceServer.h" 0027 #include <KoResource.h> 0028 #include <KoResourceFiltering.h> 0029 0030 #include "kowidgets_export.h" 0031 0032 /// The resource server adapter provides a adapter pattern for a templated resource server 0033 class KOWIDGETS_EXPORT KoAbstractResourceServerAdapter : public QObject 0034 { 0035 Q_OBJECT 0036 public: 0037 KoAbstractResourceServerAdapter(QObject *parent = nullptr); 0038 ~KoAbstractResourceServerAdapter() override = default; 0039 0040 virtual void connectToResourceServer() = 0; 0041 virtual QList<KoResource*> resources() = 0; 0042 virtual QList<KoResource*> serverResources() = 0; 0043 virtual bool addResource(KoResource* resource) = 0; 0044 virtual bool removeResource(KoResource* resource) = 0; 0045 virtual void removeResourceFile(const QString & filename) = 0; 0046 virtual void importResourceFile(const QString & filename, bool fileCreation = true) = 0; 0047 virtual QString extensions() const = 0; 0048 virtual void setCurrentTag(const QString& currentTag) = 0; 0049 virtual void enableResourceFiltering(bool tagSearch) = 0; 0050 virtual void updateServer() = 0; 0051 virtual QStringList assignedTagsList(KoResource* resource) = 0; 0052 virtual QStringList tagNamesList() = 0; 0053 virtual void addTag(const QString& tag) = 0; 0054 virtual void addTag(KoResource* resource, const QString& tag) = 0; 0055 virtual void deleteTag(KoResource* resource, const QString& tag) = 0; 0056 virtual void searchTextChanged(const QString& searchString) = 0; 0057 // these call the server. 0058 virtual void tagCategoryMembersChanged() = 0; 0059 virtual void tagCategoryAdded(const QString& tag) = 0; 0060 virtual void tagCategoryRemoved(const QString& tag) = 0; 0061 0062 virtual void setFilterIncludes(const QStringList& filteredNames) = 0; 0063 virtual QStringList searchTag(const QString& lineEditText) = 0; 0064 virtual void configureFilters(int filterType, bool enable) = 0; 0065 0066 virtual QString serverType() const { return QString(); } 0067 0068 virtual void setSortingEnabled(bool value) = 0; 0069 virtual bool sortingEnabled() const = 0; 0070 0071 Q_SIGNALS: 0072 void resourceAdded(KoResource*); 0073 void removingResource(KoResource*); 0074 void resourceChanged(KoResource*); 0075 void tagsWereChanged(); 0076 void tagCategoryWasAdded(const QString& tag); 0077 void tagCategoryWasRemoved(const QString& tag); 0078 0079 protected: 0080 void emitResourceAdded(KoResource* resource); 0081 void emitRemovingResource(KoResource* resource); 0082 void emitResourceChanged(KoResource* resource); 0083 void emitTagsWereChanged(); 0084 void emitTagCategoryWasAdded(const QString& tag); 0085 void emitTagCategoryWasRemoved(const QString& tag); 0086 }; 0087 0088 /** 0089 * The KoResourceServerAdapter provides adapter to a specific resource server 0090 * It provides a resource type independent interface to the server. 0091 */ 0092 template <class T, class Policy = PointerStoragePolicy<T> > 0093 class KoResourceServerAdapter : public KoAbstractResourceServerAdapter, public KoResourceServerObserver<T, Policy> 0094 { 0095 typedef KoResourceServer<T, Policy> ServerType; 0096 typedef typename Policy::PointerType PointerType; 0097 public: 0098 KoResourceServerAdapter(ServerType* resourceServer, QObject *parent = 0) 0099 : KoAbstractResourceServerAdapter(parent) 0100 , m_resourceServer(resourceServer) 0101 , m_sortingEnabled(false) 0102 { 0103 m_changeCounter = 0; 0104 m_oldChangeCounter = 0; 0105 m_enableFiltering = false; 0106 m_resourceFilter.setResourceServer(m_resourceServer); 0107 } 0108 0109 0110 ~KoResourceServerAdapter() override 0111 { 0112 if (m_resourceServer) 0113 m_resourceServer->removeObserver(this); 0114 } 0115 0116 QString serverType() const override 0117 { 0118 if (m_resourceServer) { 0119 return m_resourceServer->type(); 0120 } 0121 return KoAbstractResourceServerAdapter::serverType(); 0122 } 0123 0124 void unsetResourceServer() override 0125 { 0126 m_resourceServer = 0; 0127 } 0128 0129 void connectToResourceServer() override 0130 { 0131 if (m_resourceServer) 0132 m_resourceServer->addObserver(this); 0133 } 0134 0135 QList<KoResource*> resources() override 0136 { 0137 if (! m_resourceServer) 0138 return QList<KoResource*>(); 0139 0140 bool cacheDirty = serverResourceCacheInvalid(); 0141 if (cacheDirty) { 0142 QList<PointerType> serverResources = 0143 m_sortingEnabled ? 0144 m_resourceServer->sortedResources() : 0145 m_resourceServer->resources(); 0146 0147 cacheServerResources(serverResources); 0148 } 0149 if (m_enableFiltering) { 0150 if (m_resourceFilter.filtersHaveChanged() || cacheDirty) { 0151 m_filteredResources = m_resourceFilter.filterResources(m_serverResources); 0152 } 0153 return m_filteredResources; 0154 } 0155 return m_serverResources; 0156 } 0157 0158 bool addResource(KoResource* resource) override 0159 { 0160 if (! m_resourceServer) 0161 return false; 0162 0163 T* res = dynamic_cast<T*>(resource); 0164 if (res) { 0165 return m_resourceServer->addResource(res); 0166 } 0167 0168 return false; 0169 } 0170 0171 bool removeResource(KoResource* resource) override 0172 { 0173 if (! m_resourceServer) 0174 return false; 0175 0176 T* res = dynamic_cast<T*>(resource); 0177 if (res) { 0178 0179 return m_resourceServer->removeResourceAndBlacklist(res); 0180 0181 } 0182 0183 return false; 0184 } 0185 0186 void importResourceFile(const QString & filename , bool fileCreation = true) override 0187 { 0188 if (! m_resourceServer) 0189 return; 0190 m_resourceServer->importResourceFile(filename, fileCreation); 0191 } 0192 0193 void removeResourceFile(const QString & filename) override 0194 { 0195 if (!m_resourceServer) { 0196 return; 0197 } 0198 0199 m_resourceServer->removeResourceFile(filename); 0200 } 0201 0202 void resourceAdded(PointerType resource) override { 0203 serverResourceCacheInvalid(true); 0204 emitResourceAdded(Policy::toResourcePointer(resource)); 0205 } 0206 0207 void removingResource(PointerType resource) override { 0208 serverResourceCacheInvalid(true); 0209 emitRemovingResource(Policy::toResourcePointer(resource)); 0210 } 0211 0212 void resourceChanged(PointerType resource) override { 0213 serverResourceCacheInvalid(true); 0214 emitResourceChanged(Policy::toResourcePointer(resource)); 0215 } 0216 0217 void syncTaggedResourceView() override { 0218 serverResourceCacheInvalid(true); 0219 m_resourceFilter.rebuildCurrentTagFilenames(); 0220 emitTagsWereChanged(); 0221 } 0222 0223 void syncTagAddition(const QString& tag) override { 0224 emitTagCategoryWasAdded(tag); 0225 } 0226 0227 void syncTagRemoval(const QString& tag) override { 0228 emitTagCategoryWasRemoved(tag); 0229 } 0230 0231 QString extensions() const override { 0232 if (! m_resourceServer) 0233 return QString(); 0234 0235 return m_resourceServer->extensions(); 0236 } 0237 0238 void setCurrentTag(const QString& resourceFileNames) override { 0239 serverResourceCacheInvalid(true); 0240 m_resourceFilter.setCurrentTag(resourceFileNames); 0241 } 0242 0243 void enableResourceFiltering(bool enable) override { 0244 m_enableFiltering = enable; 0245 } 0246 0247 void updateServer() override{ 0248 emitRemovingResource(0); 0249 } 0250 0251 QStringList assignedTagsList(KoResource* resource) override { 0252 return m_resourceServer->assignedTagsList(resource); 0253 } 0254 0255 QStringList tagNamesList() override{ 0256 return m_resourceServer->tagNamesList(); 0257 } 0258 0259 void addTag(const QString& tag) override { 0260 m_resourceServer->addTag(0, tag); 0261 } 0262 0263 void addTag(KoResource* resource, const QString& tag) override { 0264 m_resourceServer->addTag(resource, tag); 0265 } 0266 0267 void deleteTag(KoResource* resource, const QString& tag) override { 0268 m_resourceServer->delTag(resource, tag); 0269 } 0270 0271 void setFilterIncludes(const QStringList& filteredNames) override { 0272 m_resourceFilter.setInclusions(filteredNames); 0273 } 0274 0275 void searchTextChanged(const QString& searchString) override { 0276 m_resourceFilter.setFilters(searchString); 0277 serverResourceCacheInvalid(true); 0278 } 0279 0280 QStringList searchTag(const QString& lineEditText) override { 0281 return m_resourceServer->searchTag(lineEditText); 0282 } 0283 0284 // called by model to notify server of change 0285 void tagCategoryMembersChanged() override { 0286 m_resourceServer->tagCategoryMembersChanged(); 0287 } 0288 0289 void tagCategoryAdded(const QString& tag) override { 0290 m_resourceServer->tagCategoryAdded(tag); 0291 } 0292 0293 void tagCategoryRemoved(const QString& tag) override { 0294 m_resourceServer->tagCategoryRemoved(tag); 0295 } 0296 0297 QList<KoResource*> serverResources() override { 0298 return m_serverResources; 0299 } 0300 0301 void configureFilters(int filterType, bool enable) override { 0302 m_resourceFilter.configure(filterType,enable); 0303 } 0304 0305 void setSortingEnabled(bool value) override { 0306 m_sortingEnabled = value; 0307 serverResourceCacheInvalid(true); 0308 } 0309 0310 bool sortingEnabled() const override { 0311 return m_sortingEnabled; 0312 } 0313 0314 protected: 0315 ServerType* resourceServer() const { 0316 return m_resourceServer; 0317 } 0318 protected: 0319 KoResourceFiltering m_resourceFilter; 0320 private: 0321 bool serverResourceCacheInvalid() const { 0322 return m_changeCounter != m_oldChangeCounter; 0323 } 0324 0325 void serverResourceCacheInvalid(bool yes) { 0326 if (yes) { 0327 ++m_changeCounter; 0328 } else { 0329 m_oldChangeCounter = m_changeCounter; 0330 } 0331 } 0332 0333 void cacheServerResources(const QList<PointerType> &serverResources) { 0334 m_serverResources.clear(); 0335 0336 foreach(PointerType resource, serverResources) { 0337 m_serverResources.append(Policy::toResourcePointer(resource)); 0338 } 0339 serverResourceCacheInvalid(false); 0340 } 0341 0342 ServerType* m_resourceServer; 0343 unsigned int m_changeCounter; 0344 unsigned int m_oldChangeCounter; 0345 QList<KoResource*> m_serverResources; 0346 QList<KoResource*> m_filteredResources; 0347 bool m_enableFiltering; 0348 bool m_sortingEnabled; 0349 }; 0350 0351 #endif // KO_RESOURCESERVER_ADAPTER_H_