File indexing completed on 2024-05-12 16:39:40

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "kexiprojectset.h"
0021 #include "kexi.h"
0022 
0023 #include <KDbConnection>
0024 #include <KDbMessageHandler>
0025 #include <KDbDriverManager>
0026 
0027 //! @internal
0028 class KexiProjectSetPrivate
0029 {
0030 public:
0031     KexiProjectSetPrivate()
0032     {
0033     }
0034     ~KexiProjectSetPrivate()
0035     {
0036         qDeleteAll(list);
0037     }
0038     KexiProjectData::List list;
0039 };
0040 
0041 KexiProjectSet::KexiProjectSet(KDbMessageHandler* handler)
0042         : KDbResultable()
0043         , d(new KexiProjectSetPrivate())
0044 {
0045     setMessageHandler(handler);
0046 }
0047 
0048 KexiProjectSet::~KexiProjectSet()
0049 {
0050     delete d;
0051 }
0052 
0053 bool KexiProjectSet::setConnectionData(KDbConnectionData* conndata)
0054 {
0055     Q_ASSERT(conndata);
0056     clearResult();
0057     qDeleteAll(d->list);
0058     d->list.clear();
0059 
0060     KDbMessageGuard mg(this);
0061     KDbDriver *drv = Kexi::driverManager().driver(conndata->driverId());
0062     if (!drv) {
0063         m_result = Kexi::driverManager().result();
0064         return false;
0065     }
0066     QStringList dbnames;
0067     QScopedPointer<KDbConnection> conn(drv->createConnection(*conndata));
0068     {
0069         if (!conn) {
0070             m_result = drv->result();
0071             return false;
0072         }
0073         if (!conn->connect()) {
0074             m_result = conn->result();
0075             return false;
0076         }
0077         dbnames = conn->databaseNames(false/*skip system*/);
0078         if (conn->result().isError()) {
0079             m_result = conn->result();
0080             return false;
0081         }
0082     }
0083     for (QStringList::ConstIterator it = dbnames.constBegin(); it != dbnames.constEnd(); ++it) {
0084         // project's caption is just the same as database name - nothing better is available
0085         KexiProjectData *pdata = new KexiProjectData(*conndata, *it, *it);
0086         addProjectData(pdata);
0087     }
0088     return true;
0089 }
0090 
0091 void KexiProjectSet::addProjectData(KexiProjectData *data)
0092 {
0093     d->list.append(data);
0094 }
0095 
0096 KexiProjectData* KexiProjectSet::takeProjectData(KexiProjectData *data)
0097 {
0098     return d->list.removeOne(data) ? data : 0;
0099 }
0100 
0101 KexiProjectData::List KexiProjectSet::list() const
0102 {
0103     return d->list;
0104 }
0105 
0106 KexiProjectData* KexiProjectSet::findProject(const QString &dbName) const
0107 {
0108     foreach(KexiProjectData* data, d->list) {
0109         if (0 == QString::compare(data->databaseName(), dbName, Qt::CaseInsensitive)) {
0110             return data;
0111         }
0112     }
0113     return 0;
0114 }