1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* This file is part of the KDE project
Copyright (C) 2005 Jaroslaw Staniek <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexiblobbuffer.h"
#include <assert.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqbuffer.h>
#include <kdebug.h>
#include <kstaticdeleter.h>
#include <kimageio.h>
#include <kexidb/connection.h>
static KStaticDeleter<KexiBLOBBuffer> m_bufferDeleter;
static KexiBLOBBuffer* m_buffer = 0;
//-----------------
class KexiBLOBBuffer::Private
{
public:
Private()
: maxId(0)
, inMemoryItems(1009)
, storedItems(1009)
, itemsByURL(1009)
{
}
Id_t maxId; //!< Used to compute maximal recently used identifier for unstored BLOB
//! @todo will be changed to TQHash<quint64, Item>
TQIntDict<Item> inMemoryItems; //!< for unstored BLOBs
TQIntDict<Item> storedItems; //!< for stored items
TQDict<Item> itemsByURL;
TQGuardedPtr<KexiDB::Connection> conn;
};
//-----------------
KexiBLOBBuffer::Handle::Handle(Item* item)
: m_item(item)
{
if (m_item)
m_item->refs++;
}
KexiBLOBBuffer::Handle::Handle(const Handle& handle)
{
*this = handle;
}
KexiBLOBBuffer::Handle::Handle()
: m_item(0)
{
}
KexiBLOBBuffer::Handle::~Handle()
{
if (m_item) {
m_item->refs--;
if (m_item->refs<=0)
KexiBLOBBuffer::self()->removeItem(m_item->id, m_item->stored);
}
}
KexiBLOBBuffer::Handle& KexiBLOBBuffer::Handle::operator=(const Handle& handle)
{
m_item = handle.m_item;
if (m_item)
m_item->refs++;
return *this;
}
void KexiBLOBBuffer::Handle::setStoredWidthID(KexiBLOBBuffer::Id_t id)
{
if (!m_item)
return;
if (m_item->stored) {
kdWarning() << "KexiBLOBBuffer::Handle::setStoredWidthID(): object for id=" << id
<< " is aleady stored" << endl;
return;
}
KexiBLOBBuffer::self()->takeItem(m_item);
m_item->id = id; //new id
m_item->stored = true;
//! @todo What about other handles for this item?
//! @todo They were assuming it's unstored item, but it's stored now....
KexiBLOBBuffer::self()->insertItem(m_item);
}
//-----------------
KexiBLOBBuffer::Item::Item(const TQByteArray& data, KexiBLOBBuffer::Id_t ident, bool _stored,
const TQString& _name, const TQString& _caption, const TQString& _mimeType,
Id_t _folderId, const TQPixmap& pixmap)
: name(_name), caption(_caption), mimeType(_mimeType), refs(0),
id(ident), folderId(_folderId), stored(_stored),
m_pixmapLoaded(new bool(false)/*workaround for pixmap() const*/)
{
if (pixmap.isNull())
m_pixmap = new TQPixmap();
else
m_pixmap = new TQPixmap(pixmap);
if (data.isEmpty())
m_data = new TQByteArray();
else
m_data = new TQByteArray(data);
}
KexiBLOBBuffer::Item::~Item()
{
kexipluginsdbg << "KexiBLOBBuffer::Item::~Item()" << endl;
delete m_pixmap;
m_pixmap = 0;
delete m_data;
m_data = 0;
delete m_pixmapLoaded;
}
TQPixmap KexiBLOBBuffer::Item::pixmap() const
{
if (!*m_pixmapLoaded && m_pixmap->isNull() && !m_data->isEmpty()) {
TQString type( KImageIO::typeForMime(mimeType) );
if (!KImageIO::canRead( type ) || !m_pixmap->loadFromData(*m_data, type.latin1())) {
//! @todo inform about error?
}
*m_pixmapLoaded = true;
}
return *m_pixmap;
}
TQByteArray KexiBLOBBuffer::Item::data() const
{
if (!m_data->isEmpty())
return *m_data;
if (m_data->isEmpty() && m_pixmap->isNull())
return TQByteArray();
if (m_data->isEmpty() && !m_pixmap->isNull()) {
//convert pixmap to byte array
//(do it only on demand)
TQBuffer buffer( *m_data );
buffer.open( IO_WriteOnly );
m_pixmap->save( &buffer, mimeType.isEmpty() ? (const char*)"PNG"/*! @todo default? */ : mimeType.latin1() );
}
return *m_data;
}
//-----------------
KexiBLOBBuffer::KexiBLOBBuffer()
: TQObject()
, d(new Private())
{
Q_ASSERT(!m_buffer);
d->inMemoryItems.setAutoDelete(true);
d->storedItems.setAutoDelete(true);
}
KexiBLOBBuffer::~KexiBLOBBuffer()
{
delete d;
}
KexiBLOBBuffer::Handle KexiBLOBBuffer::insertPixmap(const KURL& url)
{
if (url.isEmpty() )
return KexiBLOBBuffer::Handle();
if (!url.isValid()) {
kexipluginswarn << "::insertPixmap: INVALID URL '" << url << "'" << endl;
return KexiBLOBBuffer::Handle();
}
//! @todo what about searching by filename only and then compare data?
Item * item = d->itemsByURL.find(url.prettyURL());
if (item)
return KexiBLOBBuffer::Handle(item);
TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
//! @todo download the file if remote, then set fileName properly
TQFile f(fileName);
if (!f.open(IO_ReadOnly)) {
//! @todo err msg
return KexiBLOBBuffer::Handle();
}
TQString mimeType( KImageIO::mimeType( fileName ) );
TQByteArray data( f.readAll() );
if (f.status()!=IO_Ok) {
//! @todo err msg
return KexiBLOBBuffer::Handle();
}
TQFileInfo fi(url.fileName());
TQString caption(fi.baseName().replace('_', " ").simplifyWhiteSpace());
item = new Item(data, ++d->maxId, /*!stored*/false, url.fileName(), caption, mimeType);
insertItem(item);
//cache
item->prettyURL = url.prettyURL();
d->itemsByURL.replace(url.prettyURL(), item);
return KexiBLOBBuffer::Handle(item);
}
KexiBLOBBuffer::Handle KexiBLOBBuffer::insertObject(const TQByteArray& data,
const TQString& name, const TQString& caption, const TQString& mimeType, KexiBLOBBuffer::Id_t identifier)
{
KexiBLOBBuffer::Id_t newIdentifier;
if (identifier>0)
newIdentifier = identifier;
else
newIdentifier = ++d->maxId;
Item *item = new Item(data, newIdentifier, identifier>0, name, caption, mimeType);
insertItem( item );
return KexiBLOBBuffer::Handle(item);
}
KexiBLOBBuffer::Handle KexiBLOBBuffer::insertPixmap(const TQPixmap& pixmap)
{
if (pixmap.isNull())
return KexiBLOBBuffer::Handle();
Item * item = new Item(
TQByteArray(), //(pixmap will be converted to byte array on demand)
++d->maxId,
false, //not stored
TQString(),
TQString(),
"image/png", //!< @todo OK? What about jpegs?
0, //folder id
pixmap);
insertItem(item);
return KexiBLOBBuffer::Handle(item);
}
KexiBLOBBuffer::Handle KexiBLOBBuffer::objectForId(Id_t id, bool stored)
{
if (id<=0)
return KexiBLOBBuffer::Handle();
if (stored) {
Item *item = d->storedItems.find(id);
if (item || !d->conn)
return KexiBLOBBuffer::Handle(item);
//retrieve stored BLOB:
//#if 0
assert(d->conn);
KexiDB::TableSchema *blobsTable = d->conn->tableSchema("kexi__blobs");
if (!blobsTable) {
//! @todo err msg
return KexiBLOBBuffer::Handle();
}
/* TQStringList where;
where << "o_id";
KexiDB::PreparedStatement::Ptr st = d->conn->prepareStatement(
KexiDB::PreparedStatement::SelectStatement, *blobsTable, where);*/
//! @todo use PreparedStatement
KexiDB::QuerySchema schema;
schema.addField( blobsTable->field("o_data") );
schema.addField( blobsTable->field("o_name") );
schema.addField( blobsTable->field("o_caption") );
schema.addField( blobsTable->field("o_mime") );
schema.addField( blobsTable->field("o_folder_id") );
schema.addToWhereExpression(blobsTable->field("o_id"), TQVariant((TQ_LLONG)id));
KexiDB::RowData rowData;
tristate res = d->conn->querySingleRecord(
schema,
// TQString::fromLatin1("SELECT o_data, o_name, o_caption, o_mime FROM kexi__blobs where o_id=")
// +TQString::number(id),
rowData);
if (res!=true || rowData.size()<4) {
//! @todo err msg
kdWarning() << "KexiBLOBBuffer::objectForId("<<id<<","<<stored
<<"): res!=true || rowData.size()<4; res=="<<res.toString()<<" rowData.size()=="<<rowData.size()<< endl;
return KexiBLOBBuffer::Handle();
}
item = new Item(
rowData[0].toByteArray(),
id,
true, //stored
rowData[1].toString(),
rowData[2].toString(),
rowData[3].toString(),
(Id_t)rowData[4].toInt()
);
insertItem(item);
return KexiBLOBBuffer::Handle(item);
//#endif
}
else
return KexiBLOBBuffer::Handle(d->inMemoryItems.find(id));
}
KexiBLOBBuffer::Handle KexiBLOBBuffer::objectForId(Id_t id)
{
KexiBLOBBuffer::Handle h(objectForId(id, false/*!stored*/));
if (h)
return h;
return objectForId(id, true/*stored*/);
}
void KexiBLOBBuffer::removeItem(Id_t id, bool stored)
{
Item *item;
if (stored)
item = d->storedItems.take(id);
else
item = d->inMemoryItems.take(id);
if (item && !item->prettyURL.isEmpty()) {
d->itemsByURL.remove(item->prettyURL);
}
delete item;
}
void KexiBLOBBuffer::takeItem(Item *item)
{
assert(item);
if (item->stored)
d->storedItems.take(item->id);
else
d->inMemoryItems.take(item->id);
}
void KexiBLOBBuffer::insertItem(Item *item)
{
assert(item);
if (item->stored)
d->storedItems.insert(item->id, item);
else
d->inMemoryItems.insert(item->id, item);
}
void KexiBLOBBuffer::setConnection(KexiDB::Connection *conn)
{
KexiBLOBBuffer::self()->d->conn = conn;
}
KexiBLOBBuffer* KexiBLOBBuffer::self()
{
if(!m_buffer) {
m_bufferDeleter.setObject( m_buffer, new KexiBLOBBuffer() );
}
return m_buffer;
}
#include "kexiblobbuffer.moc"
|