LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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>
36 #include <util/sll/futures.h>
38 #include "xdg.h"
39 #include "item.h"
40 
41 namespace LeechCraft
42 {
43 namespace Util
44 {
45 namespace XDG
46 {
48  const QList<Type>& types, QObject *parent)
49  : QObject { parent }
50  , Proxy_ { proxy }
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 {};
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 
146  if (!item->IsValid ())
147  {
148  qWarning () << Q_FUNC_INFO
149  << "invalid item"
150  << path;
151  continue;
152  }
153 
154  for (const auto& cat : item->GetCategories ())
155  if (!cat.startsWith ("X-"))
156  result [cat] << item;
157  }
158 
159  return result;
160  }
161  }
162 
164  {
165  if (!IsReady_)
166  {
167  IsReady_ = true;
168  Items_ = FindAndParse (Types_);
169 
170  FixIcons (Items_, Proxy_);
171 
172  emit itemsListChanged ();
173  return;
174  }
175 
176  ExecuteFuture ([this] { return QtConcurrent::run (FindAndParse, Types_); },
177  [this] (Cat2Items_t result)
178  {
179  if (result == Items_)
180  return;
181 
182  Items_ = std::move (result);
183  FixIcons (Items_, Proxy_);
184 
185  emit itemsListChanged ();
186  },
187  this);
188  }
189 }
190 }
191 }
Cat2Items_t GetItems() const
Returns the categorized list of XDG items.
Definition: itemsfinder.cpp:61
ItemsFinder(ICoreProxy_ptr, const QList< Type > &types, QObject *parent=nullptr)
Constructs the items finder for the given types.
Definition: itemsfinder.cpp:47
std::shared_ptr< Item > Item_ptr
Definition: item.h:48
void update()
Updates the list of items.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
static Item_ptr FromDesktopFile(const QString &file)
Loads the XDG .desktop item from file.
Definition: item.cpp:192
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
Finds an XDG item for the given permanent ID.
Definition: itemsfinder.cpp:66
bool IsReady() const
Checks whether this items finder is ready.
Definition: itemsfinder.cpp:56
void itemsListChanged()
Notifies when the list of items changes in any way.
QIcon GetAppIcon(const QString &name)
Definition: xdg.cpp:40
void ExecuteFuture(Executor f, ResultHandler rh, QObject *parent, Args...args)
Runs a QFuture-returning function and feeding the future to a handler when it is ready.
Definition: futures.h:112