Debian Codegraph @

qt5/widgetsaddons/fcitxqtconfiguifactory.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2012~2012 by CSSlayer *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19
20 #include "fcitxqtconfiguifactory.h"
21 #include "fcitxqtconfiguifactory_p.h"
22 #include "fcitxqtconfiguiplugin.h"
23 #include <QDebug>
24 #include <QDir>
25 #include <QLibrary>
26 #include <QPluginLoader>
27 #include <fcitx-config/xdg.h>
28 #include <fcitx-utils/utils.h>
29 #include <libintl.h>
30
31 FcitxQtConfigUIFactoryPrivate::FcitxQtConfigUIFactoryPrivate(
32 FcitxQtConfigUIFactory *factory)
33 : QObject(factory), q_ptr(factory) {}
34
35 FcitxQtConfigUIFactoryPrivate::~FcitxQtConfigUIFactoryPrivate() {}
36
37 FcitxQtConfigUIFactory::FcitxQtConfigUIFactory(QObject *parent)
38 : QObject(parent), d_ptr(new FcitxQtConfigUIFactoryPrivate(this)) {
39 Q_D(FcitxQtConfigUIFactory);
40 d->scan();
41 }
42
43 FcitxQtConfigUIFactory::~FcitxQtConfigUIFactory() {}
44
45 FcitxQtConfigUIWidget *FcitxQtConfigUIFactory::create(const QString &file) {
46 Q_D(FcitxQtConfigUIFactory);
47
48 if (!d->plugins.contains(file))
49 return 0;
50
51 char *localepath = fcitx_utils_get_fcitx_path("localedir");
52 bindtextdomain(d->plugins[file]->domain().toUtf8().data(), localepath);
53 bind_textdomain_codeset(d->plugins[file]->domain().toUtf8().data(),
54 "UTF-8");
55 free(localepath);
56 return d->plugins[file]->create(file);
57 }
58
59 bool FcitxQtConfigUIFactory::test(const QString &file) {
60 Q_D(FcitxQtConfigUIFactory);
61
62 return d->plugins.contains(file);
63 }
64
65 void FcitxQtConfigUIFactoryPrivate::scan() {
66 QStringList dirs;
67 // check plugin files
68 size_t len;
69 char **path = FcitxXDGGetLibPath(&len);
70 for (size_t i = 0; i < len; i++) {
71 dirs << path[i];
72 }
73
74 if (dirs.isEmpty())
75 return;
76 for (QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it) {
77 // qDebug() << QString ("Checking Qt Library Path: %1").arg (*it);
78 QDir libpath(*it);
79 QDir dir(libpath.filePath(QString("qt")));
80 if (!dir.exists()) {
81 continue;
82 }
83
84 QStringList entryList = dir.entryList();
85 // filter out "." and ".." to keep debug output cleaner
86 entryList.removeAll(".");
87 entryList.removeAll("..");
88 if (entryList.isEmpty()) {
89 continue;
90 }
91
92 Q_FOREACH (const QString &maybeFile, entryList) {
93 QFileInfo fi(dir.filePath(maybeFile));
94
95 QString filePath = fi.filePath(); // file name with path
96 QString fileName = fi.fileName(); // just file name
97
98 // qDebug() << filePath;
99
100 if (!QLibrary::isLibrary(filePath)) {
101 continue;
102 }
103
104 QPluginLoader *loader = new QPluginLoader(filePath, this);
105 // qDebug() << loader->load();
106 // qDebug() << loader->errorString();
107 FcitxQtConfigUIFactoryInterface *plugin =
108 qobject_cast<FcitxQtConfigUIFactoryInterface *>(
109 loader->instance());
110 if (plugin) {
111 QStringList list = plugin->files();
112 Q_FOREACH (const QString &s, list) { plugins[s] = plugin; }
113 }
114 }
115 }
116 }