LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
itemsfinder.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 "itemsfinder.h"
31 #include <QDir>
32 #include <QTimer>
33 #include <QtDebug>
34 #include <QFutureWatcher>
35 #include <QtConcurrentRun>
37 #include "xdg.h"
38 #include "item.h"
39 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44 namespace XDG
45 {
47  const QList<Type>& types, QObject *parent)
48  : QObject (parent)
49  , Proxy_ (proxy)
50  , IsReady_ (false)
51  , Types_ (types)
52  {
53  QTimer::singleShot (1000, this, SLOT (update ()));
54  }
55 
56  bool ItemsFinder::IsReady () const
57  {
58  return IsReady_;
59  }
60 
62  {
63  return Items_;
64  }
65 
66  Item_ptr ItemsFinder::FindItem (const QString& id) const
67  {
68  for (const auto& list : Items_)
69  {
70  const auto pos = std::find_if (list.begin (), list.end (),
71  [&id] (Item_ptr item) { return item->GetPermanentID () == id; });
72  if (pos != list.end ())
73  return *pos;
74  }
75 
76  return Item_ptr ();
77  }
78 
79  namespace
80  {
81  QStringList ScanDir (const QString& path)
82  {
83  const auto& infos = QDir (path).entryInfoList (QStringList ("*.desktop"),
84  QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
85  QStringList result;
86  for (const auto& info : infos)
87  result += info.isDir () ?
88  ScanDir (info.absoluteFilePath ()) :
89  QStringList (info.absoluteFilePath ());
90  return result;
91  }
92 
93  QIcon GetIconDevice (ICoreProxy_ptr proxy, QString name)
94  {
95  if (name.isEmpty ())
96  return QIcon ();
97 
98  if (name.endsWith (".png") || name.endsWith (".svg"))
99  name.chop (4);
100 
101  auto result = proxy->GetIconThemeManager ()->GetIcon (name);
102  if (!result.isNull ())
103  return result;
104 
105  result = GetAppIcon (name);
106  if (!result.isNull ())
107  return result;
108 
109  qDebug () << Q_FUNC_INFO << name << "not found";
110 
111  return result;
112  }
113 
114  void FixIcons (const Cat2Items_t& items, ICoreProxy_ptr proxy)
115  {
116  for (const auto& list : items)
117  for (auto item : list)
118  if (item->GetIcon ().isNull ())
119  item->SetIcon (GetIconDevice (proxy, item->GetIconName ()));
120  }
121 
122  Cat2Items_t FindAndParse (const QList<Type>& types)
123  {
124  Cat2Items_t result;
125 
126  QStringList paths;
127  for (const auto& dir : ToPaths (types))
128  paths << ScanDir (dir);
129 
130  for (const auto& path : paths)
131  {
132  Item_ptr item;
133  try
134  {
135  item = Item::FromDesktopFile (path);
136  }
137  catch (const std::exception& e)
138  {
139  qWarning () << Q_FUNC_INFO
140  << "error parsing"
141  << path
142  << e.what ();
143  continue;
144  }
145  if (!item->IsValid ())
146  {
147  qWarning () << Q_FUNC_INFO
148  << "invalid item"
149  << path;
150  continue;
151  }
152 
153  for (const auto& cat : item->GetCategories ())
154  if (!cat.startsWith ("X-"))
155  result [cat] << item;
156  }
157 
158  return result;
159  }
160  }
161 
163  {
164  if (!IsReady_)
165  {
166  IsReady_ = true;
167  Items_ = FindAndParse (Types_);
168 
169  FixIcons (Items_, Proxy_);
170 
171  emit itemsListChanged ();
172  return;
173  }
174 
175  auto watcher = new QFutureWatcher<Cat2Items_t> ();
176  connect (watcher,
177  SIGNAL (finished ()),
178  this,
179  SLOT (handleScanParseFinished ()));
180  watcher->setFuture (QtConcurrent::run (FindAndParse, Types_));
181  }
182 
183  void ItemsFinder::handleScanParseFinished ()
184  {
185  auto watcher = dynamic_cast<QFutureWatcher<Cat2Items_t>*> (sender ());
186  auto result = watcher->result ();
187  watcher->deleteLater ();
188  if (result == Items_)
189  return;
190 
191  Items_ = std::move (result);
192  FixIcons (Items_, Proxy_);
193 
194  emit itemsListChanged ();
195  }
196 }
197 }
198 }
std::shared_ptr< Item > Item_ptr
Definition: item.h:46
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
static Item_ptr FromDesktopFile(const QString &)
Definition: item.cpp:191
QSet< QString > ToPaths(Type type)
Definition: itemtypes.cpp:40
QHash< QString, QList< Item_ptr > > Cat2Items_t
Definition: itemsfinder.h:47
Item_ptr FindItem(const QString &permanentID) const
Definition: itemsfinder.cpp:66
QIcon GetAppIcon(const QString &name)
Definition: xdg.cpp:40
ItemsFinder(ICoreProxy_ptr, const QList< Type > &, QObject *=0)
Definition: itemsfinder.cpp:46