1 | /* |
2 | * Copyright 2007-2008 Richard J. Moore <rich@kde.org> |
3 | * Copyright 2009 Aaron J. Seigo <aseigo@kde.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU Library General Public License version 2 as |
7 | * published by the Free Software Foundation |
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 Library General Public |
15 | * License along with this program; if not, write to the |
16 | * Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include <QtScript/QScriptEngine> |
21 | #include <QtScript/QScriptValueIterator> |
22 | |
23 | #include <config-kdeclarative.h> |
24 | |
25 | #include <kconfiggroup.h> |
26 | #include <kjob.h> |
27 | #ifndef KDECLARATIVE_NO_KIO |
28 | #include <kio/global.h> |
29 | #include <kio/job.h> |
30 | #endif |
31 | #include <ksharedconfig.h> |
32 | |
33 | Q_DECLARE_METATYPE(KConfigGroup) |
34 | |
35 | Q_DECLARE_METATYPE(KJob *) |
36 | |
37 | typedef KJob* KJobPtr; |
38 | QScriptValue qScriptValueFromKJob(QScriptEngine *engine, const KJobPtr &job) |
39 | { |
40 | return engine->newQObject(const_cast<KJob *>(job), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); |
41 | } |
42 | |
43 | void qKJobFromQScriptValue(const QScriptValue &scriptValue, KJobPtr &job) |
44 | { |
45 | QObject *obj = scriptValue.toQObject(); |
46 | job = static_cast<KJob *>(obj); |
47 | } |
48 | |
49 | #ifndef KDECLARATIVE_NO_KIO |
50 | Q_DECLARE_METATYPE(KIO::Job *) |
51 | typedef KIO::Job* KioJobPtr; |
52 | QScriptValue qScriptValueFromKIOJob(QScriptEngine *engine, const KioJobPtr &job) |
53 | { |
54 | return engine->newQObject(const_cast<KIO::Job *>(job), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); |
55 | } |
56 | |
57 | void qKIOJobFromQScriptValue(const QScriptValue &scriptValue, KioJobPtr &job) |
58 | { |
59 | QObject *obj = scriptValue.toQObject(); |
60 | job = static_cast<KIO::Job *>(obj); |
61 | } |
62 | #endif |
63 | |
64 | QScriptValue qScriptValueFromKConfigGroup(QScriptEngine *engine, const KConfigGroup &config) |
65 | { |
66 | QScriptValue obj = engine->newObject(); |
67 | |
68 | if (!config.isValid()) { |
69 | return obj; |
70 | } |
71 | |
72 | QMap<QString, QString> entryMap = config.entryMap(); |
73 | QMap<QString, QString>::const_iterator it = entryMap.constBegin(); |
74 | QMap<QString, QString>::const_iterator begin = it; |
75 | QMap<QString, QString>::const_iterator end = entryMap.constEnd(); |
76 | |
77 | //setting the group name |
78 | obj.setProperty("__file" , QScriptValue(engine, config.config()->name())); |
79 | obj.setProperty("__name" , QScriptValue(engine, config.name())); |
80 | |
81 | //setting the key/value pairs |
82 | for (it = begin; it != end; ++it) { |
83 | //kDebug() << "setting" << it.key() << "to" << it.value(); |
84 | QString prop = it.key(); |
85 | prop.replace(' ', '_'); |
86 | obj.setProperty(prop, it.value()); |
87 | } |
88 | |
89 | return obj; |
90 | } |
91 | |
92 | void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config) |
93 | { |
94 | config = KConfigGroup(KSharedConfig::openConfig(obj.property("__file" ).toString()), obj.property("__name" ).toString()); |
95 | |
96 | QScriptValueIterator it(obj); |
97 | |
98 | while (it.hasNext()) { |
99 | it.next(); |
100 | //kDebug() << it.name() << "is" << it.value().toString(); |
101 | if (it.name() != "__name" ) { |
102 | config.writeEntry(it.name(), it.value().toString()); |
103 | } |
104 | } |
105 | } |
106 | |
107 | void registerNonGuiMetaTypes(QScriptEngine *engine) |
108 | { |
109 | qScriptRegisterMetaType<KConfigGroup>(engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue()); |
110 | qScriptRegisterMetaType<KJob *>(engine, qScriptValueFromKJob, qKJobFromQScriptValue); |
111 | #ifndef KDECLARATIVE_NO_KIO |
112 | qScriptRegisterMetaType<KIO::Job *>(engine, qScriptValueFromKIOJob, qKIOJobFromQScriptValue); |
113 | #endif |
114 | } |
115 | |
116 | |