File indexing completed on 2022-11-23 11:08:47

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 "KDbConnectionData.h"
0021 #include "KDbDriverManager.h"
0022 #include "KDbDriverMetaData.h"
0023 #include "KDbConnection.h"
0024 
0025 #if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class"
0026 class KDbConnectionData { Q_DECLARE_TR_FUNCTIONS(KDbConnectionData) };
0027 #endif
0028 
0029 KDbConnectionData::~KDbConnectionData()
0030 {
0031 }
0032 
0033 QString KDbConnectionData::toUserVisibleString(UserVisibleStringOptions options) const
0034 {
0035     KDbDriverManager mananager;
0036     const KDbDriverMetaData *metaData = mananager.driverMetaData(d->driverId);
0037     if (!metaData) {
0038         return d->databaseName;
0039     } else if (metaData->isValid() && metaData->isFileBased()) {
0040         if (d->databaseName.isEmpty()) {
0041             return tr("<file>");
0042         }
0043         return tr("file: %1").arg(d->databaseName);
0044     }
0045     return ((d->userName.isEmpty() || !(options & UserVisibleStringOption::AddUser))
0046                 ? QString()
0047                 : (d->userName + QLatin1Char('@')))
0048         + (d->hostName.isEmpty() ? QLatin1String("localhost") : d->hostName)
0049         + (d->port != 0 ? (QLatin1Char(':') + QString::number(d->port)) : QString());
0050 }
0051 
0052 bool KDbConnectionData::isPasswordNeeded() const
0053 {
0054     KDbDriverManager mananager;
0055     const KDbDriverMetaData *metaData = mananager.driverMetaData(d->driverId);
0056     if (!metaData) {
0057         return false;
0058     }
0059     const bool fileBased = metaData->isValid() && metaData->isFileBased();
0060 
0061     return !d->savePassword && !fileBased; //!< @todo temp.: change this if there are
0062                                            //!< file-based drivers requiring a password
0063 }
0064 
0065 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbConnectionData& data)
0066 {
0067     dbg.nospace() << "CONNDATA";
0068     KDbDriverManager mananager;
0069     const KDbDriverMetaData *metaData = mananager.driverMetaData(data.driverId());
0070     dbg.nospace()
0071         << " databaseName=" << data.databaseName()
0072         << " caption=" << data.caption()
0073         << " description=" << data.description()
0074         << " driverId=" << data.driverId()
0075         << " userName=" << data.userName()
0076         << " hostName=" << data.hostName()
0077         << " port=" << data.port()
0078         << " useLocalSocketFile=" << data.useLocalSocketFile()
0079         << " localSocketFileName=" << data.localSocketFileName()
0080         << " password=" << QString::fromLatin1("*").repeated(data.password().length()) /* security */
0081         << " savePassword=" << data.savePassword()
0082         << " isPasswordNeeded=" <<
0083            qPrintable(metaData ? QString::number(data.isPasswordNeeded())
0084                      : QString::fromLatin1("[don't know, no valid driverId]"))
0085         << " userVisibleString=" << data.toUserVisibleString();
0086     return dbg.nospace();
0087 }