LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
item.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  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "item.h"
10 #include <stdexcept>
11 #include <QFile>
12 #include <QUrl>
13 #include <QProcess>
14 #include <util/sll/qtutil.h>
15 #include <util/xpc/util.h>
18 #include "desktopparser.h"
19 #include "xdg.h"
20 
21 namespace LC::Util::XDG
22 {
23  bool operator== (const Item& left, const Item& right)
24  {
25  return left.IsHidden_ == right.IsHidden_ &&
26  left.Type_ == right.Type_ &&
27  left.Name_ == right.Name_ &&
28  left.GenericName_ == right.GenericName_ &&
29  left.Comments_ == right.Comments_ &&
30  left.Categories_ == right.Categories_ &&
31  left.Command_ == right.Command_ &&
32  left.WD_ == right.WD_ &&
33  left.IconName_ == right.IconName_;
34  }
35 
36  bool operator!= (const Item& left, const Item& right)
37  {
38  return !(left == right);
39  }
40 
41  bool Item::IsValid () const
42  {
43  return !Name_.isEmpty ();
44  }
45 
46  bool Item::IsHidden () const
47  {
48  return IsHidden_;
49  }
50 
51  void Item::Execute (const ICoreProxy_ptr& proxy) const
52  {
53  auto command = GetCommand ();
54 
55  if (GetType () == Type::Application)
56  {
57  command.remove (QStringLiteral ("%c"));
58  command.remove (QStringLiteral ("%f"));
59  command.remove (QStringLiteral ("%F"));
60  command.remove (QStringLiteral ("%u"));
61  command.remove (QStringLiteral ("%U"));
62  command.remove (QStringLiteral ("%i"));
63  auto items = command.split (' ', Qt::SkipEmptyParts);
64  auto removePred = [] (const QString& str)
65  { return str.size () == 2 && str.at (0) == '%'; };
66  items.erase (std::remove_if (items.begin (), items.end (), removePred),
67  items.end ());
68  if (items.isEmpty ())
69  return;
70 
71  QProcess::startDetached (items.at (0), items.mid (1), GetWorkingDirectory ());
72  }
73  else if (GetType () == Type::URL)
74  {
75  const auto& e = Util::MakeEntity (QUrl (command),
76  QString (),
78  proxy->GetEntityManager ()->HandleEntity (e);
79  }
80  else
81  {
82  qWarning () << Q_FUNC_INFO
83  << "don't know how to execute this type of app";
84  }
85  }
86 
87  namespace
88  {
89  QString ByLang (const QHash<QString, QString>& cont, const QString& lang)
90  {
91  return cont.value (cont.contains (lang) ? lang : QString ());
92  }
93  }
94 
95  QString Item::GetName (const QString& lang) const
96  {
97  return ByLang (Name_, lang);
98  }
99 
100  QString Item::GetGenericName (const QString& lang) const
101  {
102  return ByLang (GenericName_, lang);
103  }
104 
105  QString Item::GetComment (const QString& lang) const
106  {
107  return ByLang (Comments_, lang);
108  }
109 
110  QString Item::GetIconName () const
111  {
112  return IconName_;
113  }
114 
115  QStringList Item::GetCategories () const
116  {
117  return Categories_;
118  }
119 
120  Type Item::GetType () const
121  {
122  return Type_;
123  }
124 
125  QString Item::GetCommand () const
126  {
127  return Command_;
128  }
129 
130  QString Item::GetWorkingDirectory () const
131  {
132  return WD_;
133  }
134 
135  QString Item::GetPermanentID () const
136  {
137  return GetCommand ();
138  }
139 
140  namespace
141  {
142  QIcon GetIconDevice (const ICoreProxy_ptr& proxy, QString name)
143  {
144  if (name.isEmpty ())
145  return QIcon ();
146 
147  if (name.endsWith (".png"_ql) || name.endsWith (".svg"_ql))
148  name.chop (4);
149 
150  auto result = proxy->GetIconThemeManager ()->GetIcon (name);
151  if (!result.isNull ())
152  return result;
153 
154  result = GetAppIcon (name);
155  if (!result.isNull ())
156  return result;
157 
158  qDebug () << Q_FUNC_INFO << name << "not found";
159 
160  return result;
161  }
162  }
163 
164  QIcon Item::GetIcon (const ICoreProxy_ptr& proxy) const
165  {
166  if (!Icon_)
167  Icon_ = GetIconDevice (proxy, GetIconName ());
168 
169  return *Icon_;
170  }
171 
172  QDebug Item::DebugPrint (QDebug dbg) const
173  {
174  dbg.nospace () << "DesktopItem\n{\n\tNames: " << Name_
175  << "\n\tGenericNames: " << GenericName_
176  << "\n\tComments: " << Comments_
177  << "\n\tCategories: " << Categories_
178  << "\n\tCommand: " << Command_
179  << "\n\tWorkingDir: " << WD_
180  << "\n\tIconName: " << IconName_
181  << "\n\tHidden: " << IsHidden_
182  << "\n}\n";
183  return dbg.space ();
184  }
185 
186  namespace
187  {
188  QHash<QString, QString> FirstValues (const QHash<QString, QStringList>& hash)
189  {
190  QHash<QString, QString> result;
191  for (const auto& [key, values] : Util::Stlize (hash))
192  result [key] = values.value (0);
193  return result;
194  }
195  }
196 
197  Item_ptr Item::FromDesktopFile (const QString& filename)
198  {
199  QFile file (filename);
200  if (!file.open (QIODevice::ReadOnly))
201  throw std::runtime_error ("Unable to open file");
202 
203  const auto& result = Util::XDG::DesktopParser {} (file.readAll ());
204  const auto& group = result [QStringLiteral ("Desktop Entry")];
205 
206  const auto& item = std::make_shared<Item> ();
207  item->Name_ = FirstValues (group [QStringLiteral ("Name")]);
208  item->GenericName_ = FirstValues (group [QStringLiteral ("GenericName")]);
209  item->Comments_ = FirstValues (group [QStringLiteral ("Comment")]);
210 
211  item->Categories_ = group [QStringLiteral ("Categories")] [{}];
212 
213  auto getSingle = [&group] (const QString& name) { return group [name] [{}].value (0); };
214 
215  item->IconName_ = getSingle (QStringLiteral ("Icon"));
216 
217  const auto& typeStr = getSingle (QStringLiteral ("Type"));
218  if (typeStr == "Application"_ql)
219  {
220  item->Type_ = Type::Application;
221  item->Command_ = getSingle (QStringLiteral ("Exec"));
222  item->WD_ = getSingle (QStringLiteral ("Path"));
223  }
224  else if (typeStr == "URL"_ql)
225  {
226  item->Type_ = Type::URL;
227  item->Command_ = getSingle (QStringLiteral ("URL"));
228  }
229  else if (typeStr == "Directory"_ql)
230  item->Type_ = Type::Dir;
231  else
232  item->Type_ = Type::Other;
233 
234  item->IsHidden_ = getSingle (QStringLiteral ("NoDisplay")).toLower () == "true"_ql;
235 
236  return item;
237  }
238 
239  QDebug operator<< (QDebug dbg, const Item& item)
240  {
241  return item.DebugPrint (std::move (dbg));
242  }
243 }
LC::Util::XDG::operator!=
bool operator!=(const Item &left, const Item &right)
Definition: item.cpp:42
LC::Util::XDG::operator<<
QDebug operator<<(QDebug dbg, const Item &item)
Serializes item contents to the debugging stream.
Definition: item.cpp:245
LC::Util::XDG::Item::GetComment
QString GetComment(const QString &language) const
Returns the comment of this item.
Definition: item.cpp:111
LC::Util::XDG::Item_ptr
std::shared_ptr< Item > Item_ptr
Definition: item.h:30
LC::Util::XDG::GetAppIcon
QIcon GetAppIcon(const QString &name)
Definition: xdg.cpp:21
LC::Util::XDG::Item::DebugPrint
QDebug DebugPrint(QDebug stream) const
Serializes item contents to the debugging stream.
Definition: item.cpp:178
LC::Util::XDG::Item::GetName
QString GetName(const QString &language) const
Returns the name of this item.
Definition: item.cpp:101
LC::Util::XDG::Item::GetIconName
QString GetIconName() const
Returns the name of the icon for this item.
Definition: item.cpp:116
LC::Util::XDG::Item::Execute
void Execute(const ICoreProxy_ptr &proxy) const
Executes this item, if possible.
Definition: item.cpp:57
LC::Util::XDG::Item::FromDesktopFile
static Item_ptr FromDesktopFile(const QString &file)
Loads the XDG .desktop item from file.
Definition: item.cpp:203
LC::Util::XDG::Type::Other
@ Other
Unknown type.
LC::Util::Stlize
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:49
LC::FromUserInitiated
@ FromUserInitiated
Definition: structures.h:44
LC::Util::XDG::Item::GetIcon
QIcon GetIcon(const ICoreProxy_ptr &) const
Returns the icon previously set by SetIcon().
Definition: item.cpp:170
LC::Util::XDG::Type::Application
@ Application
A shortcut to an application.
xdg.h
LC::Util::XDG::Type
Type
Describes the various types of XDG .desktop files.
Definition: itemtypes.h:23
LC::Util::MakeEntity
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:82
LC::Util::XDG::Item::GetCommand
QString GetCommand() const
Returns type type-specific command for this item.
Definition: item.cpp:131
LC::Util::XDG::Item::GetWorkingDirectory
QString GetWorkingDirectory() const
Returns the working directory for command execution.
Definition: item.cpp:136
LC::Util::XDG
Definition: desktopparser.cpp:15
desktopparser.h
iiconthememanager.h
util.h
LC::Util::XDG::operator==
bool operator==(const Item &left, const Item &right)
Definition: item.cpp:29
LC::Util::XDG::Item::IsHidden
bool IsHidden() const
Checks whether this XDG item should be hidden.
Definition: item.cpp:52
ICoreProxy_ptr
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
qtutil.h
LC::Util::XDG::Type::URL
@ URL
A shortcut to an URL.
LC::Util::XDG::Item::GetPermanentID
QString GetPermanentID() const
Returns the permanent ID of this item.
Definition: item.cpp:141
LC::Util::XDG::Item
class UTIL_XDG_API Item
Definition: itemsfinder.h:25
LC::Util::XDG::Item::GetGenericName
QString GetGenericName(const QString &language) const
Returns the generic name of this item.
Definition: item.cpp:106
LC::OnlyHandle
@ OnlyHandle
Definition: structures.h:68
LC::Util::XDG::Item::GetType
Type GetType() const
Returns the type of this item.
Definition: item.cpp:126
LC::Util::XDG::Type::Dir
@ Dir
A shortcut to a directory.
ientitymanager.h
LC::Util::XDG::Item::IsValid
bool IsValid() const
Checks whether this XDG item is valid.
Definition: item.cpp:47
LC::Util::XDG::DesktopParser
A parser for XDG .desktop files.
Definition: desktopparser.h:32
LC::Util::XDG::Item::GetCategories
QStringList GetCategories() const
Returns the categories where this item belongs.
Definition: item.cpp:121
item.h