kexi

kexidbconnectionset.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003,2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kexidbconnectionset.h"
21#include "kexidbshortcutfile.h"
22
23#include <kdebug.h>
24#include <kstandarddirs.h>
25
26#include <tqfile.h>
27
29class KexiDBConnectionSetPrivate
30{
31public:
32 KexiDBConnectionSetPrivate()
33 : dataForFilenames(1009)
34 {
35 list.setAutoDelete(true);
36 maxid=-1;
37 }
38 KexiDB::ConnectionData::List list;
39 TQMap<KexiDB::ConnectionData*, TQString> filenamesForData;
40 TQDict<KexiDB::ConnectionData> dataForFilenames;
41 int maxid;
42};
43
44KexiDBConnectionSet::KexiDBConnectionSet()
45: TQObject()
46, d(new KexiDBConnectionSetPrivate())
47{
48}
49
50KexiDBConnectionSet::~KexiDBConnectionSet()
51{
52 delete d;
53}
54
56{
57 if (!data)
58 return false;
59 if (data->id<0)
60 data->id = d->maxid+1;
61 //TODO: check for id-duplicates
62
63 d->maxid = TQMAX(d->maxid,data->id);
64// d->list.append(data);
65
66 TQString filename( _filename );
67 bool generateUniqueFilename = filename.isEmpty()
68 || !filename.isEmpty() && data==d->dataForFilenames[filename];
69
70 if (generateUniqueFilename) {
71 TQString dir = TDEGlobal::dirs()->saveLocation("data", "kexi/connections/", false );
72 if (dir.isEmpty())
73 return false;
74 TQString baseFilename( dir + (data->hostName.isEmpty() ? "localhost" : data->hostName) );
75 int i = 0;
76 while (TDEStandardDirs::exists(baseFilename+(i>0 ? TQString::number(i) : TQString())+".kexic"))
77 i++;
78 if (!TDEStandardDirs::exists(dir)) {
79 //make 'connections' dir and protect it
80 if (!TDEStandardDirs::makeDir(dir, 0700))
81 return false;
82 }
83 filename = baseFilename+(i>0 ? TQString::number(i) : TQString())+".kexic";
84 }
85 addConnectionDataInternal(data, filename);
86 bool result = saveConnectionData(data, data);
87 if (!result)
88 removeConnectionDataInternal(data);
89 return result;
90}
91
92void KexiDBConnectionSet::addConnectionDataInternal(KexiDB::ConnectionData *data, const TQString& filename)
93{
94 d->filenamesForData.insert(data, filename);
95 d->dataForFilenames.insert(filename, data);
96 d->list.append(data);
97}
98
100 KexiDB::ConnectionData *newData)
101{
102 if (!oldData || !newData)
103 return false;
104 TQMap<KexiDB::ConnectionData*, TQString>::ConstIterator it = d->filenamesForData.find( oldData );
105 if (it == d->filenamesForData.constEnd() || it.data().isEmpty())
106 return false;
107 const TQString filename( it.data() );
108 KexiDBConnShortcutFile shortcutFile(filename);
109 if (!shortcutFile.saveConnectionData(*newData, newData->savePassword)) // true/*savePassword*/))
110 return false;
111 if (oldData!=newData)
112 *oldData = *newData;
113 return true;
114}
115
116void KexiDBConnectionSet::removeConnectionDataInternal(KexiDB::ConnectionData *data)
117{
118 TQMap<KexiDB::ConnectionData*, TQString>::ConstIterator it = d->filenamesForData.find( data );
119 const TQString filename( it.data() );
120 d->filenamesForData.remove(data);
121 d->dataForFilenames.remove(filename);
122 d->list.removeRef(data);
123}
124
126{
127 if (!data)
128 return false;
129 TQMap<KexiDB::ConnectionData*, TQString>::ConstIterator it = d->filenamesForData.find( data );
130 if (it == d->filenamesForData.constEnd() || it.data().isEmpty())
131 return false;
132 TQFile file( it.data() );
133 if (!file.remove())
134 return false;
135 removeConnectionDataInternal(data);
136 return true;
137}
138
139const KexiDB::ConnectionData::List& KexiDBConnectionSet::list() const
140{
141 return d->list;
142}
143
144void KexiDBConnectionSet::clear()
145{
146 d->list.clear();
147 d->filenamesForData.clear();
148 d->dataForFilenames.clear();
149}
150
152{
153 clear();
154// TQStringList dirs( TDEGlobal::dirs()->findDirs("data", "kexi/connections") );
155// kexidbg << dirs << endl;
156 TQStringList files( TDEGlobal::dirs()->findAllResources("data", "kexi/connections/*.kexic") );
157// //also try for capital file extension
158// files += TDEGlobal::dirs()->findAllResources("data", "kexi/connections/*.KEXIC");
159// kexidbg << files << endl;
160
161 foreach(TQStringList::ConstIterator, it, files) {
163 KexiDBConnShortcutFile shortcutFile( *it );
164 if (!shortcutFile.loadConnectionData(*data)) {
165 delete data;
166 continue;
167 }
168 addConnectionDataInternal(data, *it);
169 }
170}
171
173{
174 if (!data)
175 return TQString();
176 TQMap<KexiDB::ConnectionData*, TQString>::ConstIterator it = d->filenamesForData.find( data );
177 return (it == d->filenamesForData.constEnd()) ? TQString() : it.data();
178}
179
181{
182 return d->dataForFilenames[fileName];
183}
bool saveConnectionData(const KexiDB::ConnectionData &data, bool savePassword, TQString *groupKey=0, bool overwriteFirstGroup=true)
bool loadConnectionData(KexiDB::ConnectionData &data, TQString *groupKey=0)
TQString fileNameForConnectionData(KexiDB::ConnectionData *data) const
bool addConnectionData(KexiDB::ConnectionData *data, const TQString &filename=TQString())
bool removeConnectionData(KexiDB::ConnectionData *data)
KexiDB::ConnectionData * connectionDataForFileName(const TQString &fileName) const
bool saveConnectionData(KexiDB::ConnectionData *oldData, KexiDB::ConnectionData *newData)
const KexiDB::ConnectionData::List & list() const
bool savePassword
True if password should be saved to a file for the connection.
int id
Used for identifying a single piece of data in a set.
TQString hostName
Host name used for the remote connection.
Database specific connection data, e.g. host, port.