LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
resourceloader.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 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 #ifdef Q_OS_MAC
107  QStringList prefixes = QStringList (QApplication::applicationDirPath () + "/../Resources/share/");
108 #elif defined (Q_OS_WIN32)
109  QStringList prefixes = QStringList (QApplication::applicationDirPath () + "/share/");
110 #elif defined (INSTALL_PREFIX)
111  QStringList prefixes = QStringList (INSTALL_PREFIX "/share/leechcraft/");
112 #else
113  QStringList prefixes = QStringList ("/usr/local/share/leechcraft/")
114  << "/usr/share/leechcraft/";
115 #endif
116  bool hasBeenAdded = false;
117  for (const QString& prefix : prefixes)
118  {
119  GlobalPrefixesChain_ << prefix;
120  ScanPath (prefix + RelativePath_);
121 
122  if (QFile::exists (prefix + RelativePath_))
123  {
124  Watcher_->addPath (prefix + RelativePath_);
125  hasBeenAdded = true;
126  }
127  }
128 
129  if (!hasBeenAdded)
130  qWarning () << Q_FUNC_INFO
131  << "no prefixes have been added:"
132  << prefixes
133  << "; rel path:"
134  << RelativePath_;
135  }
136 
137  void ResourceLoader::SetCacheParams (int size, int timeout)
138  {
139  if (qApp->property ("no-resource-caching").toBool ())
140  return;
141 
142  if (size <= 0)
143  {
144  CacheFlushTimer_->stop ();
145 
146  handleFlushCaches ();
147  }
148  else
149  {
150  if (timeout > 0)
151  CacheFlushTimer_->start (timeout);
152 
153  CachePathContents_.setMaxCost (size * 1024);
154  CachePixmaps_.setMaxCost (size * 1024);
155  }
156  }
157 
159  {
160  handleFlushCaches ();
161  }
162 
163  QFileInfoList ResourceLoader::List (const QString& option,
164  const QStringList& nameFilters, QDir::Filters filters) const
165  {
166  QSet<QString> alreadyListed;
167  QFileInfoList result;
168  for (const auto& prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
169  {
170  const QString& path = prefix + RelativePath_ + option;
171  QDir dir (path);
172  const QFileInfoList& list =
173  dir.entryInfoList (nameFilters, filters);
174  for (const auto& info : list)
175  {
176  const QString& fname = info.fileName ();
177  if (alreadyListed.contains (fname))
178  continue;
179 
180  alreadyListed << fname;
181  result << info;
182  }
183  }
184 
185  return result;
186  }
187 
188  QString ResourceLoader::GetPath (const QStringList& pathVariants) const
189  {
190  for (const auto& prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
191  for (const auto& path : pathVariants)
192  {
193  const QString& can = QFileInfo (prefix + RelativePath_ + path).absoluteFilePath ();
194  if (QFile::exists (can))
195  return can;
196  }
197 
198  return QString ();
199  }
200 
201  namespace
202  {
203  QStringList IconizeBasename (const QString& basename)
204  {
205  QStringList variants;
206  variants << basename + ".svg"
207  << basename + ".png"
208  << basename + ".jpg"
209  << basename + ".gif";
210  return variants;
211  }
212  }
213 
214  QString ResourceLoader::GetIconPath (const QString& basename) const
215  {
216  return GetPath (IconizeBasename (basename));
217  }
218 
219  QIODevice_ptr ResourceLoader::Load (const QStringList& pathVariants, bool open) const
220  {
221  QString path = GetPath (pathVariants);
222  if (path.isNull ())
223  return QIODevice_ptr ();
224 
225  if (CachePathContents_.contains (path))
226  {
227  std::shared_ptr<QBuffer> result (new QBuffer ());
228  result->setData (*CachePathContents_ [path]);
229  if (open)
230  result->open (QIODevice::ReadOnly);
231  return result;
232  }
233 
234  std::shared_ptr<QFile> result (new QFile (path));
235 
236  if (!result->isSequential () &&
237  result->size () < CachePathContents_.maxCost () / 2)
238  {
239  if (result->open (QIODevice::ReadOnly))
240  {
241  const QByteArray& data = result->readAll ();
242  CachePathContents_.insert (path, new QByteArray (data), data.size ());
243  result->close ();
244  }
245  }
246 
247  if (open)
248  result->open (QIODevice::ReadOnly);
249 
250  return result;
251  }
252 
253  QIODevice_ptr ResourceLoader::Load (const QString& pathVariant, bool open) const
254  {
255  return Load (QStringList (pathVariant), open);
256  }
257 
258  QIODevice_ptr ResourceLoader::GetIconDevice (const QString& basename, bool open) const
259  {
260  return Load (IconizeBasename (basename), open);
261  }
262 
263  QPixmap ResourceLoader::LoadPixmap (const QString& basename) const
264  {
265  if (CachePixmaps_.contains (basename))
266  return *CachePixmaps_ [basename];
267 
268  auto dev = GetIconDevice (basename, true);
269  if (!dev)
270  return QPixmap ();
271 
272  const auto& data = dev->readAll ();
273 
274  QPixmap result;
275  result.loadFromData (data);
276  CachePixmaps_.insert (basename, new QPixmap (result), data.size ());
277  return result;
278  }
279 
280  QAbstractItemModel* ResourceLoader::GetSubElemModel () const
281  {
282  return SortModel_;
283  }
284 
285  void ResourceLoader::SetAttrFilters (QDir::Filters filters)
286  {
287  AttrFilters_ = filters;
288  }
289 
290  void ResourceLoader::SetNameFilters (const QStringList& filters)
291  {
292  NameFilters_ = filters;
293  }
294 
295  void ResourceLoader::ScanPath (const QString& path)
296  {
297  for (const auto& entry : QDir (path).entryList (NameFilters_, AttrFilters_))
298  {
299  Entry2Paths_ [entry] << path;
300  if (SubElemModel_->findItems (entry).size ())
301  continue;
302 
303  SubElemModel_->appendRow (new QStandardItem (entry));
304  }
305  }
306 
307  void ResourceLoader::handleDirectoryChanged (const QString& path)
308  {
310 
311  for (auto i = Entry2Paths_.begin (), end = Entry2Paths_.end (); i != end; ++i)
312  i->removeAll (path);
313 
314  QFileInfo fi (path);
315  if (fi.exists () &&
316  fi.isDir () &&
317  fi.isReadable ())
318  ScanPath (path);
319 
320  QStringList toRemove;
321  for (auto i = Entry2Paths_.begin (), end = Entry2Paths_.end (); i != end; ++i)
322  if (i->isEmpty ())
323  toRemove << i.key ();
324 
325  for (const auto& entry : toRemove)
326  {
327  Entry2Paths_.remove (entry);
328 
329  auto items = SubElemModel_->findItems (entry);
330  for (auto item : SubElemModel_->findItems (entry))
331  SubElemModel_->removeRow (item->row ());
332  }
333  }
334 
335  void ResourceLoader::handleFlushCaches ()
336  {
337  CachePathContents_.clear ();
338  CachePixmaps_.clear ();
339  }
340  }
341 }
QPixmap LoadPixmap(const QString &basename) const
Returns the pixmap for the given basename.
QFileInfoList List(const QString &option, const QStringList &names=QStringList(), QDir::Filters filters=QDir::NoFilter) const
Lists the available files for the given option.
void SetAttrFilters(QDir::Filters)
Sets the attribute filters for the subelement model.
void AddLocalPrefix(QString prefix=QString())
Registers a local search prefix.
QAbstractItemModel * GetSubElemModel() const
Returns the subelement model with the contents of registered paths.
void SetNameFilters(const QStringList &)
Sets the name filters for the subelement model.
void SetCacheParams(int size, int timeout)
Sets the caching parameters of this loader.
void AddGlobalPrefix()
Registers global OS-dependent prefixes.
void FlushCache()
Forcefully flushes the cache.
QString GetPath(const QStringList &pathVariants) const
Returns the first found path for the list of variants.
QString GetIconPath(const QString &basename) const
Calls GetPath() with standard variants for the icon extensions.
ResourceLoader(const QString &relPath, QObject *obj=0)
Initializes the loader with the given path.
std::shared_ptr< QIODevice > QIODevice_ptr
QIODevice_ptr GetIconDevice(const QString &basename, bool open=false) const
Returns the QIODevice for the corresponding icon.
QIODevice_ptr Load(const QStringList &pathVariants, bool open=false) const
Returns the QIODevice for the corresponding resource.