LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 Item::operator== (const Item& item) const
46  {
47  return IsHidden_ == item.IsHidden_ &&
48  Name_ == item.Name_ &&
49  GenericName_ == item.GenericName_ &&
50  Comments_ == item.Comments_ &&
51  Categories_ == item.Categories_ &&
52  Command_ == item.Command_ &&
53  WD_ == item.WD_ &&
54  IconName_ == item.IconName_;
55  }
56 
57  bool Item::IsValid () const
58  {
59  return !Name_.isEmpty ();
60  }
61 
62  bool Item::IsHidden () const
63  {
64  return IsHidden_;
65  }
66 
67  void Item::Execute (ICoreProxy_ptr proxy) const
68  {
69  auto command = GetCommand ();
70 
71  if (GetType () == Type::Application)
72  {
73  command.remove ("%c");
74  command.remove ("%f");
75  command.remove ("%F");
76  command.remove ("%u");
77  command.remove ("%U");
78  command.remove ("%i");
79  auto items = command.split (' ', QString::SkipEmptyParts);
80  auto removePred = [] (const QString& str)
81  { return str.size () == 2 && str.at (0) == '%'; };
82  items.erase (std::remove_if (items.begin (), items.end (), removePred),
83  items.end ());
84  if (items.isEmpty ())
85  return;
86 
87  QProcess::startDetached (items.at (0), items.mid (1), GetWorkingDirectory ());
88  }
89  else if (GetType () == Type::URL)
90  {
91  const auto& e = Util::MakeEntity (QUrl (command),
92  QString (),
94  proxy->GetEntityManager ()->HandleEntity (e);
95  }
96  else
97  {
98  qWarning () << Q_FUNC_INFO
99  << "don't know how to execute this type of app";
100  }
101  }
102 
103  namespace
104  {
105  QString ByLang (const QHash<QString, QString>& cont, const QString& lang)
106  {
107  return cont.value (cont.contains (lang) ? lang : QString ());
108  }
109  }
110 
111  QString Item::GetName (const QString& lang) const
112  {
113  return ByLang (Name_, lang);
114  }
115 
116  QString Item::GetGenericName (const QString& lang) const
117  {
118  return ByLang (GenericName_, lang);
119  }
120 
121  QString Item::GetComment (const QString& lang) const
122  {
123  return ByLang (Comments_, lang);
124  }
125 
126  QString Item::GetIconName () const
127  {
128  return IconName_;
129  }
130 
131  QStringList Item::GetCategories () const
132  {
133  return Categories_;
134  }
135 
137  {
138  return Type_;
139  }
140 
141  QString Item::GetCommand () const
142  {
143  return Command_;
144  }
145 
146  QString Item::GetWorkingDirectory () const
147  {
148  return WD_;
149  }
150 
151  QString Item::GetPermanentID () const
152  {
153  return GetCommand ();
154  }
155 
156  void Item::SetIcon (const QIcon& icon)
157  {
158  Icon_ = icon;
159  }
160 
161  QIcon Item::GetIcon () const
162  {
163  return Icon_;
164  }
165 
166  QDebug Item::DebugPrint (QDebug dbg) const
167  {
168  dbg.nospace () << "DesktopItem\n{\n\tNames: " << Name_
169  << "\n\tGenericNames: " << GenericName_
170  << "\n\tComments: " << Comments_
171  << "\n\tCategories: " << Categories_
172  << "\n\tCommand: " << Command_
173  << "\n\tWorkingDir: " << WD_
174  << "\n\tIconName: " << IconName_
175  << "\n\tHidden: " << IsHidden_
176  << "\n}\n";
177  return dbg.space ();
178  }
179 
180  namespace
181  {
182  QHash<QString, QString> FirstValues (const QHash<QString, QStringList>& hash)
183  {
184  QHash<QString, QString> result;
185  for (auto i = hash.begin (), end = hash.end (); i != end; ++i)
186  result [i.key ()] = i->value (0);
187  return result;
188  }
189  }
190 
191  Item_ptr Item::FromDesktopFile (const QString& filename)
192  {
193  QFile file (filename);
194  if (!file.open (QIODevice::ReadOnly))
195  throw std::runtime_error ("Unable to open file");
196 
197  const auto& result = Util::XDG::DesktopParser () (file.readAll ());
198  const auto& group = result ["Desktop Entry"];
199 
200  Item_ptr item (new Item);
201  item->Name_ = FirstValues (group ["Name"]);
202  item->GenericName_ = FirstValues (group ["GenericName"]);
203  item->Comments_ = FirstValues (group ["Comment"]);
204 
205  item->Categories_ = group ["Categories"] [QString ()];
206 
207  auto getSingle = [&group] (const QString& name) { return group [name] [QString ()].value (0); };
208 
209  item->IconName_ = getSingle ("Icon");
210 
211  const auto& typeStr = getSingle ("Type");
212  if (typeStr == "Application")
213  {
214  item->Type_ = Type::Application;
215  item->Command_ = getSingle ("Exec");
216  item->WD_ = getSingle ("Path");
217  }
218  else if (typeStr == "URL")
219  {
220  item->Type_ = Type::URL;
221  item->Command_ = getSingle ("URL");
222  }
223  else if (typeStr == "Directory")
224  item->Type_ = Type::Dir;
225  else
226  item->Type_ = Type::Other;
227 
228  item->IsHidden_ = getSingle ("NoDisplay").toLower () == "true";
229 
230  return item;
231  }
232 
233  QDebug operator<< (QDebug dbg, const Item& item)
234  {
235  return item.DebugPrint (dbg);
236  }
237 }
238 }
239 }
bool IsHidden() const
Definition: item.cpp:62
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:101
std::shared_ptr< Item > Item_ptr
Definition: item.h:46
QString GetName(const QString &) const
Definition: item.cpp:111
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
QDebug operator<<(QDebug dbg, const Item &item)
Definition: item.cpp:233
static Item_ptr FromDesktopFile(const QString &)
Definition: item.cpp:191
QString GetIconName() const
Definition: item.cpp:126
Type GetType() const
Definition: item.cpp:136
QString GetComment(const QString &) const
Definition: item.cpp:121
QIcon GetIcon() const
Definition: item.cpp:161
bool operator==(const Item &) const
Definition: item.cpp:45
QString GetWorkingDirectory() const
Definition: item.cpp:146
QString GetCommand() const
Definition: item.cpp:141
QStringList GetCategories() const
Definition: item.cpp:131
QString GetPermanentID() const
Definition: item.cpp:151
void Execute(ICoreProxy_ptr) const
Definition: item.cpp:67
QString GetGenericName(const QString &) const
Definition: item.cpp:116
bool IsValid() const
Definition: item.cpp:57
void SetIcon(const QIcon &)
Definition: item.cpp:156
QDebug DebugPrint(QDebug) const
Definition: item.cpp:166