RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / razorplugininfo.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * Razor - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
9 * Alexander Sokoloff <sokoloff.a@gmail.com>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28
29 #include "razorplugininfo.h"
30 #include <QtCore/QObject>
31 #include <QtCore/QFileInfo>
32 #include <QtCore/QDir>
33 #include <QtCore/QTranslator>
34 #include <QtCore/QCoreApplication>
35 #include <QLibrary>
36 #include <QDebug>
37
38
39 /************************************************
40
41 ************************************************/
42 RazorPluginInfo::RazorPluginInfo():
43 XdgDesktopFile()
44 {
45
46 }
47
48
49 /************************************************
50
51 ************************************************/
52 bool RazorPluginInfo::load(const QString& fileName)
53 {
54 XdgDesktopFile::load(fileName);
55 mId = QFileInfo(fileName).completeBaseName();
56 return isValid();
57 }
58
59
60 /************************************************
61
62 ************************************************/
63 bool RazorPluginInfo::isValid() const
64 {
65 return XdgDesktopFile::isValid();
66 }
67
68
69 /************************************************
70
71 ************************************************/
72 QLibrary* RazorPluginInfo::loadLibrary(const QString& libDir) const
73 {
74 QString baseName, path;
75 QFileInfo fi = QFileInfo(fileName());
76 path = fi.canonicalPath();
77 baseName = value("X-Razor-Library", fi.completeBaseName()).toString();
78
79 QString soPath = QDir(libDir).filePath(QString("lib%2.so").arg(baseName));
80 QLibrary* library = new QLibrary(soPath);
81
82 if (!library->load())
83 {
84 qWarning() << QString("Can't load plugin lib \"%1\"").arg(soPath) << library->errorString();
85 delete library;
86 return 0;
87 }
88
89 QString locale = QLocale::system().name();
90 QTranslator* translator = new QTranslator(library);
91
92 translator->load(QString("%1/%2/%2_%3.qm").arg(path, baseName, locale));
93 qApp->installTranslator(translator);
94
95 return library;
96 }
97
98
99 /************************************************
100
101 ************************************************/
102 RazorPluginInfoList RazorPluginInfo::search(const QStringList& desktopFilesDirs, const QString& serviceType, const QString& nameFilter)
103 {
104 QList<RazorPluginInfo> res;
105 QSet<QString> processed;
106
107 foreach (QString desktopFilesDir, desktopFilesDirs)
108 {
109 QDir dir(desktopFilesDir);
110 QFileInfoList files = dir.entryInfoList(QStringList(nameFilter), QDir::Files | QDir::Readable);
111 foreach (QFileInfo file, files)
112 {
113 if (processed.contains(file.fileName()))
114 continue;
115
116 processed << file.fileName();
117
118 RazorPluginInfo item;
119 item.load(file.canonicalFilePath());
120
121 if (item.isValid() && item.serviceType() == serviceType)
122 res.append(item);
123 }
124 }
125 return res;
126 }
127
128
129 /************************************************
130
131 ************************************************/
132 RazorPluginInfoList RazorPluginInfo::search(const QString& desktopFilesDir, const QString& serviceType, const QString& nameFilter)
133 {
134 return search(QStringList(desktopFilesDir), serviceType, nameFilter);
135 }
136
137
138 /************************************************
139
140 ************************************************/
141 QDebug operator<<(QDebug dbg, const RazorPluginInfo &pluginInfo)
142 {
143 dbg.nospace() << QString("%1").arg(pluginInfo.id());
144 return dbg.space();
145 }
146
147
148 /************************************************
149
150 ************************************************/
151 QDebug operator<<(QDebug dbg, const RazorPluginInfo * const pluginInfo)
152 {
153 return operator<<(dbg, *pluginInfo);
154 }
155
156
157 /************************************************
158
159 ************************************************/
160 QDebug operator<<(QDebug dbg, const RazorPluginInfoList& list)
161 {
162 dbg.nospace() << '(';
163 for (int i=0; i<list.size(); ++i)
164 {
165 if (i) dbg.nospace() << ", ";
166 dbg << list.at(i);
167 }
168 dbg << ')';
169 return dbg.space();
170 }
171
172
173 /************************************************
174
175 ************************************************/
176 QDebug operator<<(QDebug dbg, const RazorPluginInfoList* const pluginInfoList)
177 {
178 return operator<<(dbg, *pluginInfoList);
179 }