Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/scripting/kexidb/kexidbmodule.cpp
Show All 17 Lines | |||||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "kexidbmodule.h" | 20 | #include "kexidbmodule.h" | ||
21 | #include "kexidbconnection.h" | 21 | #include "kexidbconnection.h" | ||
22 | #include "kexidbdriver.h" | 22 | #include "kexidbdriver.h" | ||
23 | #include "kexidbconnectiondata.h" | 23 | #include "kexidbconnectiondata.h" | ||
24 | #include "kexidbfield.h" | 24 | #include "kexidbfield.h" | ||
25 | #include "kexidbschema.h" | 25 | #include "kexidbschema.h" | ||
26 | #include "KexiScriptingDebug.h" | ||||
26 | 27 | | |||
27 | #include <KDbDriver> | 28 | #include <KDbDriver> | ||
28 | #include <KDbConnectionData> | 29 | #include <KDbConnectionData> | ||
29 | #include <KDbField> | 30 | #include <KDbField> | ||
30 | #include <KDbTableSchema> | 31 | #include <KDbTableSchema> | ||
31 | #include <KDbQuerySchema> | 32 | #include <KDbQuerySchema> | ||
32 | 33 | | |||
33 | #include <KConfigGroup> | 34 | #include <KConfigGroup> | ||
35 | #include <KConfig> | ||||
34 | 36 | | |||
35 | #include <QDebug> | 37 | #include <QDebug> | ||
36 | #include <QMimeDatabase> | 38 | #include <QMimeDatabase> | ||
39 | #include <QUrl> | ||||
37 | 40 | | |||
38 | // The as version() published versionnumber of this kross-module. | 41 | // The as version() published versionnumber of this kross-module. | ||
39 | #define KROSS_KEXIDB_VERSION 1 | 42 | #define KROSS_KEXIDB_VERSION 1 | ||
40 | 43 | | |||
41 | extern "C" | 44 | extern "C" | ||
42 | { | 45 | { | ||
43 | /** | 46 | /** | ||
44 | * Exported an loadable function as entry point to use | 47 | * Exported an loadable function as entry point to use | ||
45 | * the \a KexiDBModule. | 48 | * the \a KexiDBModule. | ||
46 | */ | 49 | */ | ||
47 | KDE_EXPORT QObject* krossmodule() { | 50 | /*KEXI_EXPORT*/ QObject* krossmodule() { | ||
48 | return new Scripting::KexiDBModule(); | 51 | return new Scripting::KexiDBModule(); | ||
49 | } | 52 | } | ||
50 | } | 53 | } | ||
51 | 54 | | |||
52 | using namespace Scripting; | 55 | using namespace Scripting; | ||
53 | 56 | | |||
54 | KexiDBModule::KexiDBModule(QObject* parent) | 57 | KexiDBModule::KexiDBModule(QObject* parent) | ||
55 | : QObject(parent) | 58 | : QObject(parent) | ||
56 | { | 59 | { | ||
57 | setObjectName("KexiDB"); | 60 | setObjectName("KexiDB"); | ||
58 | } | 61 | } | ||
59 | 62 | | |||
60 | KexiDBModule::~KexiDBModule() | 63 | KexiDBModule::~KexiDBModule() | ||
61 | { | 64 | { | ||
62 | } | 65 | } | ||
63 | 66 | | |||
64 | int KexiDBModule::version() | 67 | int KexiDBModule::version() | ||
65 | { | 68 | { | ||
66 | return KROSS_KEXIDB_VERSION; | 69 | return KROSS_KEXIDB_VERSION; | ||
67 | } | 70 | } | ||
68 | 71 | | |||
69 | const QStringList KexiDBModule::driverNames() | 72 | QStringList KexiDBModule::driverNames() | ||
70 | { | 73 | { | ||
71 | return m_drivermanager.driverNames(); | 74 | return m_drivermanager.driverIds(); | ||
72 | } | 75 | } | ||
73 | 76 | | |||
74 | QObject* KexiDBModule::driver(const QString& drivername) | 77 | QObject* KexiDBModule::driver(const QString& drivername) | ||
75 | { | 78 | { | ||
76 | QPointer< KDbDriver > driver = m_drivermanager.driver(drivername); // caching is done by the DriverManager | 79 | QPointer< KDbDriver > driver = m_drivermanager.driver(drivername); // caching is done by the DriverManager | ||
77 | if (! driver) { | 80 | if (! driver) { | ||
78 | qWarning() << "No such driver '%1'" << drivername; | 81 | KexiScriptingWarning() << "No such driver:" << drivername; | ||
79 | return 0; | 82 | return 0; | ||
80 | } | 83 | } | ||
81 | if (driver->error()) { | 84 | if (driver->result().isError()) { | ||
82 | qWarning() << "Error for drivername" << drivername << driver->errorMsg(); | 85 | KexiScriptingWarning() << "Error for drivername" << drivername << driver->result().message(); | ||
83 | return 0; | 86 | return 0; | ||
84 | } | 87 | } | ||
85 | return new KexiDBDriver(this, driver); | 88 | return new KexiDBDriver(this, driver); | ||
86 | } | 89 | } | ||
87 | 90 | | |||
88 | const QString KexiDBModule::lookupByMime(const QString& mimetype) | 91 | QString KexiDBModule::lookupByMime(const QString& mimetype) | ||
staniek: Remove const from return type | |||||
89 | { | 92 | { | ||
90 | return m_drivermanager.lookupByMime(mimetype); | 93 | QStringList ids = m_drivermanager.driverIdsForMimeType(mimetype); | ||
94 | if (ids.size() > 0) { | ||||
95 | return ids.at(0); | ||||
96 | } | ||||
97 | return QString(); | ||||
staniek: check is the list is not empty | |||||
91 | } | 98 | } | ||
92 | 99 | | |||
93 | const QString KexiDBModule::mimeForFile(const QString& filename) | 100 | QString KexiDBModule::mimeForFile(const QString& filename) | ||
94 | { | 101 | { | ||
95 | QMimeDatabase db; | 102 | QMimeDatabase db; | ||
96 | QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name(); | 103 | QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name(); | ||
97 | if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") { | 104 | if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") { | ||
98 | mimename = db.mimeTypeForUrl(filename).name(); | 105 | mimename = db.mimeTypeForUrl(QUrl::fromLocalFile(filename)).name(); | ||
probably QUrl::fromLocalFile is better if we assume local files, otherwise only true URLs will work staniek: probably QUrl::fromLocalFile is better if we assume local files, otherwise only true URLs will… | |||||
99 | } | 106 | } | ||
100 | return mimename; | 107 | return mimename; | ||
101 | } | 108 | } | ||
102 | 109 | | |||
103 | QObject* KexiDBModule::createConnectionData() | 110 | QObject* KexiDBModule::createConnectionData() | ||
104 | { | 111 | { | ||
105 | return new KexiDBConnectionData(this, new KDbConnectionData(), true); | 112 | return new KexiDBConnectionData(); | ||
106 | } | 113 | } | ||
107 | 114 | | |||
108 | QObject* KexiDBModule::createConnectionDataByFile(const QString& filename) | 115 | QObject* KexiDBModule::createConnectionDataByFile(const QString& filename) | ||
109 | { | 116 | { | ||
110 | //! @todo reuse the original code! | 117 | //! @todo reuse the original code! | ||
111 | QMimeDatabase db; | 118 | QMimeDatabase db; | ||
112 | QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name(); | 119 | QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name(); | ||
113 | if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") { | 120 | if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") { | ||
114 | mimename = db.mimeTypeForUrl(filename).name(); | 121 | mimename = db.mimeTypeForUrl(QUrl::fromLocalFile(filename)).name(); | ||
staniek: Like above | |||||
115 | } | 122 | } | ||
116 | if (mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") { | 123 | if (mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") { | ||
117 | KConfig _config(filename, KConfig::NoGlobals); | 124 | KConfig _config(filename, KConfig::NoGlobals); | ||
118 | 125 | | |||
119 | QString groupkey; | 126 | QString groupkey; | ||
120 | foreach(const QString &s, _config.groupList()) { | 127 | foreach(const QString &s, _config.groupList()) { | ||
121 | if (s.toLower() != "file information") { | 128 | if (s.toLower() != "file information") { | ||
122 | groupkey = s; | 129 | groupkey = s; | ||
123 | break; | 130 | break; | ||
124 | } | 131 | } | ||
125 | } | 132 | } | ||
133 | | ||||
126 | if (groupkey.isNull()) { | 134 | if (groupkey.isNull()) { | ||
127 | qDebug() << "No groupkey, filename=" << filename; | 135 | KexiScriptingWarning() << "No groupkey, filename=" << filename; | ||
staniek: -> KexiScriptingWarning | |||||
128 | return 0; | 136 | return 0; | ||
129 | } | 137 | } | ||
130 | 138 | | |||
131 | KConfigGroup config(&_config, groupkey); | 139 | KConfigGroup config(&_config, groupkey); | ||
132 | KDbConnectionData* data = new KDbConnectionData(); | 140 | KDbConnectionData* data = new KDbConnectionData(); | ||
133 | int version = config.readEntry("version", 2); //KexiDBShortcutFile_version | 141 | int version = config.readEntry("version", 2); //KexiDBShortcutFile_version | ||
134 | data->setDatabaseName(QString()); | 142 | data->setDatabaseName(QString()); | ||
135 | data->setCaption(config.readEntry("caption")); | 143 | data->setCaption(config.readEntry("caption")); | ||
Show All 15 Lines | |||||
151 | } | 159 | } | ||
152 | if (data->password().isEmpty()) { | 160 | if (data->password().isEmpty()) { | ||
153 | data->setPassword(config.readEntry("password")); | 161 | data->setPassword(config.readEntry("password")); | ||
154 | } | 162 | } | ||
155 | 163 | | |||
156 | data->setSavePassword(!data->password().isEmpty()); | 164 | data->setSavePassword(!data->password().isEmpty()); | ||
157 | data->setUserName(config.readEntry("user")); | 165 | data->setUserName(config.readEntry("user")); | ||
158 | 166 | | |||
159 | KexiDBConnectionData* c = new KexiDBConnectionData(this, data, true); | 167 | KexiDBConnectionData* c = new KexiDBConnectionData(this, data); | ||
160 | c->setDatabaseName(dbname); | 168 | c->setDatabaseName(dbname); | ||
161 | return c; | 169 | return c; | ||
162 | } | 170 | } | ||
163 | 171 | | |||
164 | const QStringList driverIds = m_drivermanager.driverIdsForMimeType(mimename); | 172 | const QStringList driverIds = m_drivermanager.driverIdsForMimeType(mimename); | ||
165 | if (driverIds.isEmpty()) { | 173 | if (driverIds.isEmpty()) { | ||
166 | qDebug() << "No driver, filename=" << filename << "mimename=" << mimename; | 174 | KexiScriptingWarning() << "No driver, filename=" << filename << "mimename=" << mimename; | ||
staniek: -> KexiScriptingWarning | |||||
167 | return 0; | 175 | return 0; | ||
168 | } | 176 | } | ||
169 | 177 | | |||
170 | KDbConnectionData* data = new KDbConnectionData(); | 178 | KDbConnectionData* data = new KDbConnectionData(); | ||
171 | data->setDatabaseName(filename); | 179 | data->setDatabaseName(filename); | ||
172 | //! @todo there can be more than one driver | 180 | //! @todo there can be more than one driver | ||
173 | data->setDriverId(driverIds.first()); | 181 | data->setDriverId(driverIds.first()); | ||
174 | return new KexiDBConnectionData(this, data, true); | 182 | return new KexiDBConnectionData(this, data); | ||
175 | } | 183 | } | ||
176 | 184 | | |||
177 | QObject* KexiDBModule::field() | 185 | QObject* KexiDBModule::field() | ||
178 | { | 186 | { | ||
179 | return new KexiDBField(this, new KDbField(), true); | 187 | return new KexiDBField(this, new KDbField(), true); | ||
180 | } | 188 | } | ||
181 | 189 | | |||
182 | QObject* KexiDBModule::tableSchema(const QString& tablename) | 190 | QObject* KexiDBModule::tableSchema(const QString& tablename) | ||
183 | { | 191 | { | ||
184 | return new KexiDBTableSchema(this, new KDbTableSchema(tablename), true); | 192 | return new KexiDBTableSchema(this, new KDbTableSchema(tablename), true); | ||
185 | } | 193 | } | ||
186 | 194 | | |||
187 | QObject* KexiDBModule::querySchema() | 195 | QObject* KexiDBModule::querySchema() | ||
188 | { | 196 | { | ||
189 | return new KexiDBQuerySchema(this, new KDbQuerySchema(), true); | 197 | return new KexiDBQuerySchema(this, new KDbQuerySchema(), true); | ||
190 | } | 198 | } | ||
191 | 199 | | |||
192 | QObject* KexiDBModule::connectionWrapper(QObject* connection) | 200 | QObject* KexiDBModule::connectionWrapper(KDbConnection* connection) | ||
193 | { | 201 | { | ||
194 | KDbConnection* c = dynamic_cast< KDbConnection* >(connection); | 202 | return new KexiDBConnection(connection); | ||
195 | return c ? new KexiDBConnection(c) : 0; | | |||
196 | } | 203 | } | ||
197 | 204 | |
Remove const from return type