LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
extensionsdataimpl_x11.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 "extensionsdataimpl.h"
31 #include <QFile>
32 #include <QIcon>
33 #include <QtDebug>
34 
35 namespace LeechCraft
36 {
37 namespace Util
38 {
39  namespace
40  {
41  QHash<QString, QString> ParseMimeTypes ()
42  {
43  QFile file { "/etc/mime.types" };
44  if (!file.open (QIODevice::ReadOnly))
45  {
46  qWarning () << Q_FUNC_INFO
47  << "cannot open /etc/mime.types:"
48  << file.errorString ();
49  return {};
50  }
51 
52  QRegExp wsRx { "\\s+" };
53 
54  QHash<QString, QString> result;
55  while (!file.atEnd ())
56  {
57  const auto& line = file.readLine ().trimmed ();
58  const auto& elems = QString::fromLatin1 (line).split (wsRx);
59 
60  const auto& mime = elems.at (0);
61  for (int i = 1; i < elems.size (); ++i)
62  result [elems.at (i)] = mime;
63  }
64  return result;
65  }
66 
67  QStringList GetMimeDirs ()
68  {
69  auto list = qgetenv ("XDG_DATA_HOME").split (':') +
70  qgetenv ("XDG_DATA_DIRS").split (':');
71  if (list.isEmpty ())
72  list << "/usr/share";
73 
74  QStringList result;
75  for (const auto& item : list)
76  if (QFile::exists (item + "/mime"))
77  result << item + "/mime/";
78  return result;
79  }
80 
81  void ParseIconsMappings (QHash<QString, QString>& result, const QString& filename)
82  {
83  QFile file { filename };
84  if (!file.open (QIODevice::ReadOnly))
85  {
86  qWarning () << Q_FUNC_INFO
87  << "cannot open"
88  << filename
89  << file.errorString ();
90  return;
91  }
92 
93  while (!file.atEnd ())
94  {
95  const auto& line = QString::fromLatin1 (file.readLine ().trimmed ());
96  if (!line.indexOf (':'))
97  continue;
98 
99  result [line.section (':', 0, 0)] = line.section (':', 1, 1);
100  }
101  }
102 
103  QHash<QString, QString> ParseIconsMappings ()
104  {
105  QHash<QString, QString> result;
106  for (const auto& mimeDir : GetMimeDirs ())
107  {
108  ParseIconsMappings (result, mimeDir + "generic-icons");
109  ParseIconsMappings (result, mimeDir + "icons");
110  }
111  return result;
112  }
113  }
114 
115  struct ExtensionsDataImpl::Details
116  {
117  QHash<QString, QString> MimeDatabase_;
118  QHash<QString, QString> IconsMappings_;
119 
120  Details ();
121  };
122 
124  : MimeDatabase_ { ParseMimeTypes () }
125  , IconsMappings_ { ParseIconsMappings () }
126  {
127  }
128 
130  : Details_ { new Details }
131  {
132  }
133 
134  const QHash<QString, QString>& ExtensionsDataImpl::GetMimeDatabase () const
135  {
136  return Details_->MimeDatabase_;
137  }
138 
139  QIcon ExtensionsDataImpl::GetExtIcon (const QString& extension) const
140  {
141  return GetMimeIcon (GetMimeDatabase ().value (extension));
142  }
143 
144  QIcon ExtensionsDataImpl::GetMimeIcon (const QString& mime) const
145  {
146  auto iconName = Details_->IconsMappings_.value (mime);
147  if (iconName.isEmpty ())
148  iconName = mime.section ('/', 0, 0) + "-x-generic";
149 
150  auto result = QIcon::fromTheme (iconName);
151  if (result.isNull ())
152  result = QIcon::fromTheme (mime.section ('/', 0, 0) + "-x-generic");
153  if (result.isNull ())
154  result = QIcon::fromTheme ("unknown");
155  return result;
156  }
157 }
158 }
const QHash< QString, QString > & GetMimeDatabase() const
QIcon GetExtIcon(const QString &extension) const
QIcon GetMimeIcon(const QString &mime) const