20#include "kexiblobbuffer.h"
25#include <tqfileinfo.h>
29#include <kstaticdeleter.h>
32#include <kexidb/connection.h>
34static KStaticDeleter<KexiBLOBBuffer> m_bufferDeleter;
39class KexiBLOBBuffer::Private
51 TQIntDict<Item> inMemoryItems;
52 TQIntDict<Item> storedItems;
53 TQDict<Item> itemsByURL;
54 TQGuardedPtr<KexiDB::Connection> conn;
76KexiBLOBBuffer::Handle::~Handle()
85KexiBLOBBuffer::Handle& KexiBLOBBuffer::Handle::operator=( const Handle& handle)
87 m_item = handle.m_item;
98 kdWarning() << "KexiBLOBBuffer::Handle::setStoredWidthID(): object for id=" << id
99 << " is aleady stored" << endl;
105 m_item->stored = true;
114 const TQString& _name, const TQString& _caption, const TQString& _mimeType,
115 Id_t _folderId, const TQPixmap& pixmap)
116 : name(_name), caption(_caption), mimeType(_mimeType), refs(0),
117 id(ident), folderId(_folderId), stored(_stored),
118 m_pixmapLoaded(new bool(false))
121 m_pixmap = new TQPixmap();
123 m_pixmap = new TQPixmap(pixmap);
126 m_data = new TQByteArray();
128 m_data = new TQByteArray(data);
131KexiBLOBBuffer::Item::~Item()
133 kexipluginsdbg << "KexiBLOBBuffer::Item::~Item()" << endl;
138 delete m_pixmapLoaded;
141TQPixmap KexiBLOBBuffer::Item::pixmap() const
143 if (!*m_pixmapLoaded && m_pixmap->isNull() && !m_data->isEmpty()) {
144 TQString type( KImageIO::typeForMime(mimeType) );
145 if (!KImageIO::canRead( type ) || !m_pixmap->loadFromData(*m_data, type.latin1())) {
148 *m_pixmapLoaded = true;
153TQByteArray KexiBLOBBuffer::Item::data() const
155 if (!m_data->isEmpty())
158 if (m_data->isEmpty() && m_pixmap->isNull())
159 return TQByteArray();
161 if (m_data->isEmpty() && !m_pixmap->isNull()) {
164 TQBuffer buffer( *m_data );
165 buffer.open( IO_WriteOnly );
166 m_pixmap->save( &buffer, mimeType.isEmpty() ? ( const char*) "PNG" : mimeType.latin1() );
173KexiBLOBBuffer::KexiBLOBBuffer()
178 d->inMemoryItems.setAutoDelete( true);
179 d->storedItems.setAutoDelete( true);
182KexiBLOBBuffer::~KexiBLOBBuffer()
191 if (!url.isValid()) {
192 kexipluginswarn << "::insertPixmap: INVALID URL '" << url << "'" << endl;
196 Item * item = d->itemsByURL.find(url.prettyURL());
200 TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
203 if (!f.open(IO_ReadOnly)) {
207 TQString mimeType( KImageIO::mimeType( fileName ) );
209 TQByteArray data( f.readAll() );
210 if (f.status()!=IO_Ok) {
214 TQFileInfo fi(url.fileName());
215 TQString caption(fi.baseName().replace( '_', " ").simplifyWhiteSpace());
217 item = new Item(data, ++d->maxId, false, url.fileName(), caption, mimeType);
221 item->prettyURL = url.prettyURL();
222 d->itemsByURL.replace(url.prettyURL(), item);
227 const TQString& name, const TQString& caption, const TQString& mimeType, KexiBLOBBuffer::Id_t identifier)
231 newIdentifier = identifier;
233 newIdentifier = ++d->maxId;
235 Item *item = new Item(data, newIdentifier, identifier>0, name, caption, mimeType);
245 Item * item = new Item(
264 Item *item = d->storedItems.find( id);
265 if (item || !d->conn)
290 tristate res = d->conn->querySingleRecord(
295 if (res!= true || rowData.size()<4) {
297 kdWarning() << "KexiBLOBBuffer::objectForId("<< id<< ","<<stored
298 << "): res!=true || rowData.size()<4; res=="<<res. toString()<< " rowData.size()=="<<rowData.size()<< endl;
303 rowData[0].toByteArray(),
306 rowData[1].toString(),
307 rowData[2].toString(),
308 rowData[3].toString(),
309 ( Id_t)rowData[4].toInt()
332 item = d->storedItems.take( id);
334 item = d->inMemoryItems.take( id);
336 if (item && !item->prettyURL.isEmpty()) {
337 d->itemsByURL.remove(item->prettyURL);
346 d->storedItems.take(item->id);
348 d->inMemoryItems.take(item->id);
355 d->storedItems.insert(item->id, item);
357 d->inMemoryItems.insert(item->id, item);
368 m_bufferDeleter.setObject( m_buffer, new KexiBLOBBuffer() );
373#include "kexiblobbuffer.moc"
Object handle used by KexiBLOBBuffer.
void setStoredWidthID(Id_t id)
Application-wide buffer for local BLOB data like pixmaps.
long Id_t long integer for unique identifying blobs
Handle insertObject(const TQByteArray &data, const TQString &name, const TQString &caption, const TQString &mimeType, Id_t identifier=0)
Handle objectForId(Id_t id, bool stored)
void insertItem(Item *item)
Handle insertPixmap(const KURL &url)
static KexiBLOBBuffer * self() Access to KexiBLOBBuffer singleton.
void removeItem(Id_t id, bool stored)
void takeItem(Item *item)
Provides database connection, allowing queries and data modification.
KexiDB::QuerySchema provides information about database query.
FieldList & addField(Field *field, bool visible=true)
void addToWhereExpression(KexiDB::Field *field, const TQVariant &value, int relation='=')
3-state logical type with three values: true, false and cancelled and convenient operators.
TQString toString() const
TQValueVector< TQVariant > RowData structure for storing single record with type information
|