LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
21namespace 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
121 {
122 return Type_;
123 }
124
125 QString Item::GetCommand () const
126 {
127 return Command_;
128 }
129
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}
virtual IEntityManager * GetEntityManager() const =0
Returns the entity manager object.
virtual IIconThemeManager * GetIconThemeManager() const =0
Returns the icon theme manager.
virtual bool HandleEntity(LC::Entity entity, QObject *desired=nullptr)=0
Handles the given entity.
virtual QIcon GetIcon(const QString &on, const QString &off=QString())=0
Returns the current theme's icon for the given on and off states.
A parser for XDG .desktop files.
Describes a single XDG .desktop entry.
Definition item.h:36
QIcon GetIcon(const ICoreProxy_ptr &) const
Returns the icon previously set by SetIcon().
Definition item.cpp:164
void Execute(const ICoreProxy_ptr &proxy) const
Executes this item, if possible.
Definition item.cpp:51
QString GetCommand() const
Returns type type-specific command for this item.
Definition item.cpp:125
bool IsValid() const
Checks whether this XDG item is valid.
Definition item.cpp:41
static Item_ptr FromDesktopFile(const QString &file)
Loads the XDG .desktop item from file.
Definition item.cpp:197
Type GetType() const
Returns the type of this item.
Definition item.cpp:120
QString GetWorkingDirectory() const
Returns the working directory for command execution.
Definition item.cpp:130
QString GetComment(const QString &language) const
Returns the comment of this item.
Definition item.cpp:105
QString GetPermanentID() const
Returns the permanent ID of this item.
Definition item.cpp:135
bool IsHidden() const
Checks whether this XDG item should be hidden.
Definition item.cpp:46
QString GetGenericName(const QString &language) const
Returns the generic name of this item.
Definition item.cpp:100
QString GetName(const QString &language) const
Returns the name of this item.
Definition item.cpp:95
QString GetIconName() const
Returns the name of the icon for this item.
Definition item.cpp:110
QStringList GetCategories() const
Returns the categories where this item belongs.
Definition item.cpp:115
QDebug DebugPrint(QDebug stream) const
Serializes item contents to the debugging stream.
Definition item.cpp:172
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition icoreproxy.h:181
std::shared_ptr< Item > Item_ptr
Definition item.h:24
Type
Describes the various types of XDG .desktop files.
Definition itemtypes.h:24
@ Dir
A shortcut to a directory.
Definition itemtypes.h:39
@ Other
Unknown type.
Definition itemtypes.h:27
@ Application
A shortcut to an application.
Definition itemtypes.h:31
@ URL
A shortcut to an URL.
Definition itemtypes.h:35
QDebug operator<<(QDebug dbg, const Item &item)
Serializes item contents to the debugging stream.
Definition item.cpp:239
bool operator!=(const Item &left, const Item &right)
Definition item.cpp:36
bool operator==(const Item &left, const Item &right)
Definition item.cpp:23
QIcon GetAppIcon(const QString &name)
Definition xdg.cpp:15
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition qtutil.h:48
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition util.cpp:82
@ FromUserInitiated
Definition structures.h:44
@ OnlyHandle
Definition structures.h:68