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