LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
resourceloader.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "resourceloader.h"
31 #include <QApplication>
32 #include <QFile>
33 #include <QDir>
34 #include <QStandardItemModel>
35 #include <QSortFilterProxyModel>
36 #include <QFileSystemWatcher>
37 #include <QTimer>
38 #include <QtDebug>
39 #include <QBuffer>
40 
41 namespace LeechCraft
42 {
43  namespace Util
44  {
45  ResourceLoader::ResourceLoader (const QString& relPath, QObject* parent)
46  : QObject (parent)
47  , RelativePath_ (relPath)
48  , SubElemModel_ (new QStandardItemModel (this))
49  , AttrFilters_ (QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable)
50  , SortModel_ (new QSortFilterProxyModel (this))
51  , Watcher_ (new QFileSystemWatcher (this))
52  , CacheFlushTimer_ (new QTimer (this))
53  , CachePathContents_ (0)
54  , CachePixmaps_ (0)
55  {
56  if (RelativePath_.startsWith ('/'))
57  RelativePath_ = RelativePath_.mid (1);
58  if (!RelativePath_.endsWith ('/'))
59  RelativePath_.append ('/');
60 
61  SortModel_->setDynamicSortFilter (true);
62  SortModel_->setSourceModel (SubElemModel_);
63  SortModel_->sort (0);
64 
65  connect (Watcher_,
66  SIGNAL (directoryChanged (const QString&)),
67  this,
68  SLOT (handleDirectoryChanged (const QString&)));
69 
70  connect (CacheFlushTimer_,
71  SIGNAL (timeout ()),
72  this,
73  SLOT (handleFlushCaches ()));
74  }
75 
76  void ResourceLoader::AddLocalPrefix (QString prefix)
77  {
78  if (!prefix.isEmpty () &&
79  !prefix.endsWith ('/'))
80  prefix.append ('/');
81  QString result = QDir::homePath () + "/.leechcraft/data/" + prefix;
82  LocalPrefixesChain_ << result;
83 
84  QDir testDir = QDir::home ();
85  if (!testDir.exists (".leechcraft/data/" + prefix + RelativePath_))
86  {
87  qDebug () << Q_FUNC_INFO
88  << ".leechcraft/data/" + prefix + RelativePath_
89  << "doesn't exist, trying to create it...";
90 
91  if (!testDir.mkpath (".leechcraft/data/" + prefix + RelativePath_))
92  {
93  qWarning () << Q_FUNC_INFO
94  << "failed to create"
95  << ".leechcraft/data/" + prefix + RelativePath_;
96  }
97  }
98 
99  ScanPath (result + RelativePath_);
100 
101  Watcher_->addPath (result + RelativePath_);
102  }
103 
105  {
106 #if defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
107  const QStringList prefixes { QApplication::applicationDirPath () + "/../Resources/share/" };
108 #elif defined (Q_OS_WIN32)
109  const QStringList prefixes { QApplication::applicationDirPath () + "/share/" };
110 #elif defined (INSTALL_PREFIX)
111  const QStringList prefixes { INSTALL_PREFIX "/share/leechcraft/" };
112 #else
113  const QStringList prefixes
114  {
115  "/usr/local/share/leechcraft/",
116  "/usr/share/leechcraft/"
117  };
118 #endif
119  bool hasBeenAdded = false;
120  for (const auto& prefix : prefixes)
121  {
122  GlobalPrefixesChain_ << prefix;
123  ScanPath (prefix + RelativePath_);
124 
125  if (QFile::exists (prefix + RelativePath_))
126  {
127  Watcher_->addPath (prefix + RelativePath_);
128  hasBeenAdded = true;
129  }
130  }
131 
132  if (!hasBeenAdded)
133  qWarning () << Q_FUNC_INFO
134  << "no prefixes have been added:"
135  << prefixes
136  << "; rel path:"
137  << RelativePath_;
138  }
139 
140  void ResourceLoader::SetCacheParams (int size, int timeout)
141  {
142  if (qApp->property ("no-resource-caching").toBool ())
143  return;
144 
145  if (size <= 0)
146  {
147  CacheFlushTimer_->stop ();
148 
149  handleFlushCaches ();
150  }
151  else
152  {
153  if (timeout > 0)
154  CacheFlushTimer_->start (timeout);
155 
156  CachePathContents_.setMaxCost (size * 1024);
157  CachePixmaps_.setMaxCost (size * 1024);
158  }
159  }
160 
162  {
163  handleFlushCaches ();
164  }
165 
166  QFileInfoList ResourceLoader::List (const QString& option,
167  const QStringList& nameFilters, QDir::Filters filters) const
168  {
169  QSet<QString> alreadyListed;
170  QFileInfoList result;
171  for (const auto& prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
172  {
173  const QDir dir { prefix + RelativePath_ + option };
174  const auto& list = dir.entryInfoList (nameFilters, filters);
175  for (const auto& info : list)
176  {
177  const auto& fname = info.fileName ();
178  if (alreadyListed.contains (fname))
179  continue;
180 
181  alreadyListed << fname;
182  result << info;
183  }
184  }
185 
186  return result;
187  }
188 
189  QString ResourceLoader::GetPath (const QStringList& pathVariants) const
190  {
191  for (const auto& prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
192  for (const auto& path : pathVariants)
193  {
194  const QString& can = QFileInfo (prefix + RelativePath_ + path).absoluteFilePath ();
195  if (QFile::exists (can))
196  return can;
197  }
198 
199  return QString ();
200  }
201 
202  namespace
203  {
204  QStringList IconizeBasename (const QString& basename)
205  {
206  return
207  {
208  basename + ".svg",
209  basename + ".png",
210  basename + ".jpg",
211  basename + ".gif"
212  };
213  }
214  }
215 
216  QString ResourceLoader::GetIconPath (const QString& basename) const
217  {
218  return GetPath (IconizeBasename (basename));
219  }
220 
221  QIODevice_ptr ResourceLoader::Load (const QStringList& pathVariants, bool open) const
222  {
223  const auto& path = GetPath (pathVariants);
224  if (path.isNull ())
225  return {};
226 
227  if (CachePathContents_.contains (path))
228  {
229  auto result = std::make_shared<QBuffer> ();
230  result->setData (*CachePathContents_ [path]);
231  if (open)
232  result->open (QIODevice::ReadOnly);
233  return result;
234  }
235 
236  auto result = std::make_shared<QFile> (path);
237 
238  if (!result->isSequential () &&
239  result->size () < CachePathContents_.maxCost () / 2)
240  {
241  if (result->open (QIODevice::ReadOnly))
242  {
243  const auto& data = result->readAll ();
244  CachePathContents_.insert (path, new QByteArray { data }, data.size ());
245  if (!open)
246  result->close ();
247  else
248  result->seek (0);
249  }
250  }
251 
252  if (open && !result->isOpen ())
253  if (!result->open (QIODevice::ReadOnly))
254  qWarning () << Q_FUNC_INFO
255  << "unable to open file"
256  << path
257  << result->errorString ();
258 
259  return result;
260  }
261 
262  QIODevice_ptr ResourceLoader::Load (const QString& pathVariant, bool open) const
263  {
264  return Load (QStringList { pathVariant }, open);
265  }
266 
267  QIODevice_ptr ResourceLoader::GetIconDevice (const QString& basename, bool open) const
268  {
269  return Load (IconizeBasename (basename), open);
270  }
271 
272  QPixmap ResourceLoader::LoadPixmap (const QString& basename) const
273  {
274  if (CachePixmaps_.contains (basename))
275  return *CachePixmaps_ [basename];
276 
277  auto dev = GetIconDevice (basename, true);
278  if (!dev)
279  return QPixmap ();
280 
281  const auto& data = dev->readAll ();
282 
283  QPixmap result;
284  result.loadFromData (data);
285  CachePixmaps_.insert (basename, new QPixmap (result), data.size ());
286  return result;
287  }
288 
289  QAbstractItemModel* ResourceLoader::GetSubElemModel () const
290  {
291  return SortModel_;
292  }
293 
294  void ResourceLoader::SetAttrFilters (QDir::Filters filters)
295  {
296  AttrFilters_ = filters;
297  }
298 
299  void ResourceLoader::SetNameFilters (const QStringList& filters)
300  {
301  NameFilters_ = filters;
302  }
303 
304  void ResourceLoader::ScanPath (const QString& path)
305  {
306  for (const auto& entry : QDir (path).entryList (NameFilters_, AttrFilters_))
307  {
308  Entry2Paths_ [entry] << path;
309  if (SubElemModel_->findItems (entry).size ())
310  continue;
311 
312  SubElemModel_->appendRow (new QStandardItem (entry));
313  }
314  }
315 
316  void ResourceLoader::handleDirectoryChanged (const QString& path)
317  {
319 
320  for (auto i = Entry2Paths_.begin (), end = Entry2Paths_.end (); i != end; ++i)
321  i->removeAll (path);
322 
323  QFileInfo fi (path);
324  if (fi.exists () &&
325  fi.isDir () &&
326  fi.isReadable ())
327  ScanPath (path);
328 
329  QStringList toRemove;
330  for (auto i = Entry2Paths_.begin (), end = Entry2Paths_.end (); i != end; ++i)
331  if (i->isEmpty ())
332  toRemove << i.key ();
333 
334  for (const auto& entry : toRemove)
335  {
336  Entry2Paths_.remove (entry);
337 
338  auto items = SubElemModel_->findItems (entry);
339  for (auto item : SubElemModel_->findItems (entry))
340  SubElemModel_->removeRow (item->row ());
341  }
342  }
343 
344  void ResourceLoader::handleFlushCaches ()
345  {
346  CachePathContents_.clear ();
347  CachePixmaps_.clear ();
348  }
349  }
350 }
QIODevice_ptr GetIconDevice(const QString &basename, bool open=false) const
Returns the QIODevice for the corresponding icon.
QString GetIconPath(const QString &basename) const
Calls GetPath() with standard variants for the icon extensions.
void SetAttrFilters(QDir::Filters)
Sets the attribute filters for the subelement model.
void AddLocalPrefix(QString prefix=QString())
Registers a local search prefix.
QIODevice_ptr Load(const QStringList &pathVariants, bool open=false) const
Returns the QIODevice for the corresponding resource.
QFileInfoList List(const QString &option, const QStringList &names={}, QDir::Filters filters=QDir::NoFilter) const
Lists the available files for the given option.
void SetNameFilters(const QStringList &)
Sets the name filters for the subelement model.
QAbstractItemModel * GetSubElemModel() const
Returns the subelement model with the contents of registered paths.
void SetCacheParams(int size, int timeout)
Sets the caching parameters of this loader.
void AddGlobalPrefix()
Registers global OS-dependent prefixes.
QString GetPath(const QStringList &pathVariants) const
Returns the first found path for the list of variants.
void FlushCache()
Forcefully flushes the cache.
ResourceLoader(const QString &relPath, QObject *obj=0)
Initializes the loader with the given path.
QPixmap LoadPixmap(const QString &basename) const
Returns the pixmap for the given basename.
std::shared_ptr< QIODevice > QIODevice_ptr