Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/ibanbicdata/ibanbicdata.cpp
- This file was moved from kmymoney/payeeidentifier/ibanandbic/ibanbicdata.cpp.
Show All 13 Lines | |||||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU General Public License | 15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "ibanbicdata.h" | 19 | #include "ibanbicdata.h" | ||
20 | 20 | | |||
21 | #include <KServiceTypeTrader> | 21 | #include <KServiceTypeTrader> | ||
22 | #include <KLocalizedString> | ||||
23 | | ||||
22 | #include <QSqlQuery> | 24 | #include <QSqlQuery> | ||
23 | #include <QSqlError> | 25 | #include <QSqlError> | ||
24 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
25 | #include <QDebug> | 27 | #include <QDebug> | ||
26 | 28 | | |||
27 | #include "ibanbic.h" | 29 | #include "payeeidentifier/ibanbic/ibanbic.h" | ||
30 | #include "ibanbicdataenums.h" | ||||
31 | #include "bicmodel.h" | ||||
32 | | ||||
33 | ibanBicData::ibanBicData(QObject *parent, const QVariantList &args) : | ||||
34 | KMyMoneyPlugin::Plugin(parent, "ibanbicdata"/*must be the same as X-KDE-PluginInfo-Name*/) | ||||
35 | { | ||||
36 | Q_UNUSED(args) | ||||
37 | setComponentName("ibanbicdata", i18n("IBAN/BIC Data")); | ||||
38 | | ||||
39 | // For information, announce that we have been loaded. | ||||
40 | qDebug("Plugins: ibanbicdata loaded"); | ||||
41 | } | ||||
28 | 42 | | |||
29 | ibanBicData::~ibanBicData() | 43 | ibanBicData::~ibanBicData() | ||
30 | { | 44 | { | ||
45 | qDebug("Plugins: ibanbicdata unloaded"); | ||||
46 | } | ||||
47 | | ||||
48 | QVariant ibanBicData::requestData(const QString &arg, uint type) | ||||
49 | { | ||||
50 | switch (type) { | ||||
51 | case eIBANBIC::DataType::bbanLength: | ||||
52 | return QVariant::fromValue(bbanLength(arg)); | ||||
53 | case eIBANBIC::DataType::bankIdentifierPosition: | ||||
54 | return QVariant::fromValue(bankIdentifierPosition(arg)); | ||||
55 | case eIBANBIC::DataType::bankIdentifierLength: | ||||
56 | return QVariant::fromValue(bankIdentifierLength(arg)); | ||||
57 | case eIBANBIC::DataType::iban2Bic: | ||||
58 | return QVariant::fromValue(iban2Bic(arg)); | ||||
59 | case eIBANBIC::DataType::bankNameByBic: | ||||
60 | return QVariant::fromValue(bankNameByBic(arg)); | ||||
61 | case eIBANBIC::DataType::bankNameAndBic: | ||||
62 | return QVariant::fromValue(bankNameAndBic(arg)); | ||||
63 | case eIBANBIC::DataType::extractBankIdentifier: | ||||
64 | return QVariant::fromValue(extractBankIdentifier(arg)); | ||||
65 | case eIBANBIC::DataType::isBicAllocated: | ||||
66 | return QVariant::fromValue(static_cast<uint>(isBicAllocated(arg))); | ||||
67 | case eIBANBIC::DataType::bicModel: | ||||
68 | return QVariant::fromValue(static_cast<QAbstractItemModel *>(new bicModel())); | ||||
69 | default: | ||||
70 | return QVariant(); | ||||
71 | } | ||||
31 | } | 72 | } | ||
32 | 73 | | |||
33 | int ibanBicData::bankIdentifierLength(const QString& countryCode) | 74 | int ibanBicData::bankIdentifierLength(const QString& countryCode) | ||
34 | { | 75 | { | ||
35 | const QVariant value = findPropertyByCountry(countryCode, QLatin1String("X-KMyMoney-BankIdentifier-Length"), QVariant::Int); | 76 | const QVariant value = findPropertyByCountry(countryCode, QLatin1String("X-KMyMoney-BankIdentifier-Length"), QVariant::Int); | ||
36 | if (value.isValid()) | 77 | if (value.isValid()) | ||
37 | return value.toInt(); | 78 | return value.toInt(); | ||
38 | return 0; | 79 | return 0; | ||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Line(s) | 183 | { | |||
179 | 220 | | |||
180 | if (query.next()) { | 221 | if (query.next()) { | ||
181 | return QPair<QString, QString>(query.value(0).toString(), query.value(1).toString()); | 222 | return QPair<QString, QString>(query.value(0).toString(), query.value(1).toString()); | ||
182 | } | 223 | } | ||
183 | 224 | | |||
184 | return QPair<QString, QString>(QString(""), QString("")); | 225 | return QPair<QString, QString>(QString(""), QString("")); | ||
185 | } | 226 | } | ||
186 | 227 | | |||
187 | ibanBicData::bicAllocationStatus ibanBicData::isBicAllocated(const QString& bic) | 228 | eIBANBIC::bicAllocationStatus ibanBicData::isBicAllocated(const QString& bic) | ||
188 | { | 229 | { | ||
189 | // Get countryCode | 230 | // Get countryCode | ||
190 | const QString countryCode = bic.mid(4, 2); | 231 | const QString countryCode = bic.mid(4, 2); | ||
191 | if (countryCode.length() != 2) | 232 | if (countryCode.length() != 2) | ||
192 | return bicNotAllocated; | 233 | return eIBANBIC::bicAllocationStatus::bicNotAllocated; | ||
193 | 234 | | |||
194 | // Get services which have a database entry | 235 | // Get services which have a database entry | ||
195 | KService::List services = KServiceTypeTrader::self()->query("KMyMoney/IbanBicData", | 236 | KService::List services = KServiceTypeTrader::self()->query("KMyMoney/IbanBicData", | ||
196 | QString("(\'%1' ~in [X-KMyMoney-CountryCodes] or '*' in [X-KMyMoney-CountryCodes]) and exist [X-KMyMoney-Bankdata-Database]").arg(countryCode) | 237 | QString("(\'%1' ~in [X-KMyMoney-CountryCodes] or '*' in [X-KMyMoney-CountryCodes]) and exist [X-KMyMoney-Bankdata-Database]").arg(countryCode) | ||
197 | ); | 238 | ); | ||
198 | 239 | | |||
199 | if (services.isEmpty()) | 240 | if (services.isEmpty()) | ||
200 | return bicAllocationUncertain; | 241 | return eIBANBIC::bicAllocationStatus::bicAllocationUncertain; | ||
201 | 242 | | |||
202 | QSqlDatabase db = createDatabaseConnection(services.first()->property(QLatin1String("X-KMyMoney-Bankdata-Database"), QVariant::String).toString()); | 243 | QSqlDatabase db = createDatabaseConnection(services.first()->property(QLatin1String("X-KMyMoney-Bankdata-Database"), QVariant::String).toString()); | ||
203 | if (!db.isOpen()) // This is an error | 244 | if (!db.isOpen()) // This is an error | ||
204 | return bicAllocationUncertain; | 245 | return eIBANBIC::bicAllocationStatus::bicAllocationUncertain; | ||
205 | 246 | | |||
206 | QSqlQuery query(db); | 247 | QSqlQuery query(db); | ||
207 | query.prepare("SELECT ? IN (SELECT bic FROM institutions)"); | 248 | query.prepare("SELECT ? IN (SELECT bic FROM institutions)"); | ||
208 | query.bindValue(0, bic); | 249 | query.bindValue(0, bic); | ||
209 | 250 | | |||
210 | if (!query.exec() || !query.next()) { | 251 | if (!query.exec() || !query.next()) { | ||
211 | qWarning() << QString("Could not execute query on \"%1\" to check if bic exists. Error: %2").arg(db.databaseName()).arg(query.lastError().text()); | 252 | qWarning() << QString("Could not execute query on \"%1\" to check if bic exists. Error: %2").arg(db.databaseName()).arg(query.lastError().text()); | ||
212 | return bicAllocationUncertain; | 253 | return eIBANBIC::bicAllocationStatus::bicAllocationUncertain; | ||
213 | } | 254 | } | ||
214 | 255 | | |||
215 | if (query.value(0).toBool()) // Bic found | 256 | if (query.value(0).toBool()) // Bic found | ||
216 | return bicAllocated; | 257 | return eIBANBIC::bicAllocationStatus::bicAllocated; | ||
217 | 258 | | |||
218 | // Bic not found, test if database is complete | 259 | // Bic not found, test if database is complete | ||
219 | if (services.first()->property(QLatin1String("X-KMyMoney-Bankdata-IsComplete"), QVariant::Bool).toBool()) | 260 | if (services.first()->property(QLatin1String("X-KMyMoney-Bankdata-IsComplete"), QVariant::Bool).toBool()) | ||
220 | return bicNotAllocated; | 261 | return eIBANBIC::bicAllocationStatus::bicNotAllocated; | ||
221 | 262 | | |||
222 | return bicAllocationUncertain; | 263 | return eIBANBIC::bicAllocationStatus::bicAllocationUncertain; | ||
223 | } | 264 | } | ||
224 | 265 | | |||
225 | QVariant ibanBicData::findPropertyByCountry(const QString& countryCode, const QString& property, const QVariant::Type type) | 266 | QVariant ibanBicData::findPropertyByCountry(const QString& countryCode, const QString& property, const QVariant::Type type) | ||
226 | { | 267 | { | ||
227 | const KService::List services = KServiceTypeTrader::self()->query("KMyMoney/IbanBicData", | 268 | const KService::List services = KServiceTypeTrader::self()->query("KMyMoney/IbanBicData", | ||
228 | QString("'%1' ~in [X-KMyMoney-CountryCodes] and exist [%2]").arg(countryCode).arg(property) | 269 | QString("'%1' ~in [X-KMyMoney-CountryCodes] and exist [%2]").arg(countryCode).arg(property) | ||
229 | ); | 270 | ); | ||
230 | if (!services.isEmpty()) | 271 | if (!services.isEmpty()) | ||
Show All 38 Lines | 291 | { | |||
269 | db.setConnectOptions("QSQLITE_OPEN_READONLY=1;QSQLITE_ENABLE_SHARED_CACHE=1;"); | 310 | db.setConnectOptions("QSQLITE_OPEN_READONLY=1;QSQLITE_ENABLE_SHARED_CACHE=1;"); | ||
270 | const bool opened = db.open(); | 311 | const bool opened = db.open(); | ||
271 | if (!opened) { | 312 | if (!opened) { | ||
272 | qWarning() << QString("Could not open database \"%1\" to receive IBAN and BIC data.").arg(path); | 313 | qWarning() << QString("Could not open database \"%1\" to receive IBAN and BIC data.").arg(path); | ||
273 | } | 314 | } | ||
274 | 315 | | |||
275 | return db; | 316 | return db; | ||
276 | } | 317 | } | ||
318 | | ||||
319 | K_PLUGIN_FACTORY_WITH_JSON(ibanBicDataFactory, "ibanbicdata.json", registerPlugin<ibanBicData>();) | ||||
320 | | ||||
321 | #include "ibanbicdata.moc" |