kexi

kexidbconnectionset.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003,2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexidbconnectionset.h"
00021 #include "kexidbshortcutfile.h"
00022 
00023 #include <kdebug.h>
00024 #include <kstandarddirs.h>
00025 
00026 #include <qfile.h>
00027 
00029 class KexiDBConnectionSetPrivate
00030 {
00031 public:
00032     KexiDBConnectionSetPrivate()
00033      : dataForFilenames(101)
00034     {
00035         list.setAutoDelete(true);
00036         maxid=-1;
00037     }
00038     KexiDB::ConnectionData::List list;
00039     QMap<KexiDB::ConnectionData*, QString> filenamesForData;
00040     QDict<KexiDB::ConnectionData> dataForFilenames;
00041     int maxid;
00042 };
00043 
00044 KexiDBConnectionSet::KexiDBConnectionSet()
00045 : QObject()
00046 , d(new KexiDBConnectionSetPrivate())
00047 {
00048 }
00049 
00050 KexiDBConnectionSet::~KexiDBConnectionSet()
00051 {
00052     delete d;
00053 }
00054 
00055 bool KexiDBConnectionSet::addConnectionData(KexiDB::ConnectionData *data, const QString& _filename)
00056 {
00057     if (!data)
00058         return false;
00059     if (data->id<0)
00060         data->id = d->maxid+1;
00061     //TODO:     check for id-duplicates
00062     
00063     d->maxid = QMAX(d->maxid,data->id);
00064 //  d->list.append(data);
00065 
00066     QString filename( _filename );
00067     bool generateUniqueFilename = filename.isEmpty() 
00068         || !filename.isEmpty() && data==d->dataForFilenames[filename];
00069 
00070     if (generateUniqueFilename) {
00071         QString dir = KGlobal::dirs()->saveLocation("data", "kexi/connections/", false );
00072         if (dir.isEmpty())
00073             return false;
00074         QString baseFilename( dir + (data->hostName.isEmpty() ? "localhost" : data->hostName) );
00075         int i = 0;
00076         while (KStandardDirs::exists(baseFilename+(i>0 ? QString::number(i) : QString::null)+".kexic"))
00077             i++;
00078         if (!KStandardDirs::exists(dir)) {
00079             //make 'connections' dir and protect it
00080             if (!KStandardDirs::makeDir(dir, 0700))
00081                 return false;
00082         }
00083         filename = baseFilename+(i>0 ? QString::number(i) : QString::null)+".kexic";
00084     }
00085     addConnectionDataInternal(data, filename);
00086     bool result = saveConnectionData(data, data);
00087     if (!result)
00088         removeConnectionDataInternal(data);
00089     return result;
00090 }
00091 
00092 void KexiDBConnectionSet::addConnectionDataInternal(KexiDB::ConnectionData *data, const QString& filename)
00093 {
00094     d->filenamesForData.insert(data, filename);
00095     d->dataForFilenames.insert(filename, data);
00096     d->list.append(data);
00097 }
00098 
00099 bool KexiDBConnectionSet::saveConnectionData(KexiDB::ConnectionData *oldData, 
00100     KexiDB::ConnectionData *newData)
00101 {
00102     if (!oldData || !newData)
00103         return false;
00104     QMap<KexiDB::ConnectionData*, QString>::ConstIterator it = d->filenamesForData.find( oldData );
00105     if (it == d->filenamesForData.constEnd() || it.data().isEmpty())
00106         return false;
00107     const QString filename( it.data() );
00108     KexiDBConnShortcutFile shortcutFile(filename);
00109     if (!shortcutFile.saveConnectionData(*newData, newData->savePassword)) // true/*savePassword*/))
00110         return false;
00111     if (oldData!=newData)
00112         *oldData = *newData;
00113     return true;
00114 }
00115 
00116 void KexiDBConnectionSet::removeConnectionDataInternal(KexiDB::ConnectionData *data)
00117 {
00118     QMap<KexiDB::ConnectionData*, QString>::ConstIterator it = d->filenamesForData.find( data );
00119     const QString filename( it.data() );
00120     d->filenamesForData.remove(data);
00121     d->dataForFilenames.remove(filename);
00122     d->list.removeRef(data);
00123 }
00124 
00125 bool KexiDBConnectionSet::removeConnectionData(KexiDB::ConnectionData *data)
00126 {
00127     if (!data)
00128         return false;
00129     QMap<KexiDB::ConnectionData*, QString>::ConstIterator it = d->filenamesForData.find( data );
00130     if (it == d->filenamesForData.constEnd() || it.data().isEmpty())
00131         return false;
00132     QFile file( it.data() );
00133     if (!file.remove())
00134         return false;
00135     removeConnectionDataInternal(data);
00136     return true;
00137 }
00138 
00139 const KexiDB::ConnectionData::List& KexiDBConnectionSet::list() const
00140 {
00141     return d->list;
00142 }
00143 
00144 void KexiDBConnectionSet::clear()
00145 {
00146     d->list.clear();
00147     d->filenamesForData.clear();
00148     d->dataForFilenames.clear();
00149 }
00150 
00151 void KexiDBConnectionSet::load()
00152 {
00153     clear();
00154 //  QStringList dirs( KGlobal::dirs()->findDirs("data", "kexi/connections") );
00155 //  kexidbg << dirs << endl;
00156     QStringList files( KGlobal::dirs()->findAllResources("data", "kexi/connections/*.kexic") );
00157 //  //also try for capital file extension
00158 //  files += KGlobal::dirs()->findAllResources("data", "kexi/connections/*.KEXIC");
00159 //  kexidbg << files << endl;
00160 
00161     foreach(QStringList::ConstIterator, it, files) {
00162         KexiDB::ConnectionData *data = new KexiDB::ConnectionData();
00163         KexiDBConnShortcutFile shortcutFile( *it );
00164         if (!shortcutFile.loadConnectionData(*data)) {
00165             delete data;
00166             continue;
00167         }
00168         addConnectionDataInternal(data, *it);
00169     }
00170 }
00171 
00172 QString KexiDBConnectionSet::fileNameForConnectionData(KexiDB::ConnectionData *data) const
00173 {
00174     if (!data)
00175         return QString::null;
00176     QMap<KexiDB::ConnectionData*, QString>::ConstIterator it = d->filenamesForData.find( data );
00177     return (it == d->filenamesForData.constEnd()) ? QString::null : it.data();
00178 }
00179 
00180 KexiDB::ConnectionData* KexiDBConnectionSet::connectionDataForFileName(const QString& fileName) const
00181 {
00182     return d->dataForFilenames[fileName];
00183 }
KDE Home | KDE Accessibility Home | Description of Access Keys