2 * kopetefileengine.h - Kopete file engine
4 * Copyright (c) 2007 by Guillermo A. Amaral B <me@guillermoamaral.com>
5 * Kopete (c) 2007 by the Kopete developers <kopete-devel@kde.org>
7 * Based on Kopete Mime Source Factory
8 * Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
10 *************************************************************************
12 * This library is free software; you can redistribute it and/or *
13 * modify it under the terms of the GNU Lesser General Public *
14 * License as published by the Free Software Foundation; either *
15 * version 2 of the License, or (at your option) any later version. *
17 *************************************************************************
20 #include "kopetefileengine.h"
22 #include "kopeteaccountmanager.h"
23 #include "kopetecontactlist.h"
24 #include "kopeteaccount.h"
25 #include "kopetecontact.h"
26 #include "kopetemetacontact.h"
27 #include "kopetepicture.h"
30 #include <kiconloader.h>
33 #include <qdiriterator.h>
35 #include <qstringlist.h>
39 QAbstractFileEngine
*FileEngineHandler::create(const QString
&fileName
) const
42 handle
|= fileName
.startsWith("kopete-account-icon", Qt::CaseInsensitive
);
43 handle
|= fileName
.startsWith("kopete-contact-icon", Qt::CaseInsensitive
);
44 handle
|= fileName
.startsWith("kopete-metacontact-icon", Qt::CaseInsensitive
);
45 handle
|= fileName
.startsWith("kopete-metacontact-photo", Qt::CaseInsensitive
);
46 handle
|= fileName
.startsWith("kopete-onlinestatus-icon", Qt::CaseInsensitive
);
47 return handle
? new FileEngine(fileName
) : 0;
50 FileEngine::FileEngine()
56 FileEngine::FileEngine(const QString
& fileName
)
57 : m_fileName(fileName
), m_buffer(&m_data
)
59 kDebug(14010) << fileName
;
62 FileEngine::~FileEngine()
64 kDebug(14010) << m_fileName
;
67 bool FileEngine::open(QIODevice::OpenMode openMode
)
69 kDebug(14010) << m_fileName
<< " " << openMode
;
71 // flag used to signal something went wrong when creating a mimesource
72 bool completed
= false;
77 // extract and decode arguments
78 QStringList parts
= m_fileName
.split(QChar(':'), QString::SkipEmptyParts
);
79 QStringList::iterator partsEnd
= parts
.end();
80 for (QStringList::iterator it
= parts
.begin(); it
!= partsEnd
; ++it
)
81 *it
= QUrl::fromPercentEncoding((*it
).toUtf8());
83 if (parts
[0] == QString::fromLatin1("kopete-contact-icon"))
85 if (parts
.size() >= 4)
87 Account
*account
= AccountManager::self()->findAccount(parts
[1], parts
[2]);
91 Contact
*contact
= account
->contacts()[ parts
[3] ];
95 tmpPixmap
= QPixmap(contact
->onlineStatus().iconFor(contact
,16));
100 kDebug(14010) << "kopete-contact-icon: contact not found";
105 kDebug(14010) << "kopete-contact-icon: account not found";
110 kDebug(14010) << "kopete-contact-icon: insufficient information in m_fileName: " << parts
;
114 if (parts
[0] == QString::fromLatin1("kopete-account-icon"))
116 if (parts
.size() >= 3)
118 Account
*account
= AccountManager::self()->findAccount(parts
[1], parts
[2]);
122 tmpPixmap
= QPixmap(account
->myself()->onlineStatus().iconFor(account
->myself(),16));
127 kDebug(14010) << "kopete-account-icon: account not found";
132 kDebug(14010) << "kopete-account-icon: insufficient information in m_fileName: " << parts
;
136 if (parts
[0] == QString::fromLatin1("kopete-metacontact-icon"))
138 if (parts
.size() >= 2)
140 MetaContact
*mc
= ContactList::self()->metaContact(parts
[1]);
144 tmpPixmap
= QPixmap(SmallIcon(mc
->statusIcon()));
150 kDebug(14010) << "kopete-metacontact-icon: insufficient information in m_fileName: " << parts
;
154 if (parts
[0] == QString::fromLatin1("kopete-metacontact-photo"))
156 if (parts
.size() >= 2)
158 MetaContact
*mc
= ContactList::self()->metaContact(parts
[1]);
162 tmpImage
= QImage(mc
->picture().image());
168 kDebug(14010) << "kopete-metacontact-photo: insufficient information in m_fileName: " << parts
;
172 if (parts
[0] == QString::fromLatin1("kopete-onlinestatus-icon"))
174 if (parts
.size() >= 2)
177 * We are using a dirty trick here: this mime source is supposed to return the
178 * icon for an arbitrary KOS instance. To do this, the caller needs to ask
179 * the KOS for the mime source key first, which also ensures the icon is
180 * currently in the cache. The cache is global, so we just need to find any
181 * existing KOS instance to return us the rendered icon from the cache.
182 * To find a valid KOS, we ask Kopete's account manager to locate an existing
183 * account. We'll use the myself() instance of that account to reference its
184 * current KOS object, which in turn has access to the global KOS icon cache.
185 * Note that if the cache has been invalidated in the meantime, we'll just
186 * get an empty pixmap back.
189 Account
*account
= AccountManager::self()->accounts().first();
193 tmpPixmap
= QPixmap(account
->myself()->onlineStatus().iconFor(parts
[1]));
198 kDebug(14010) << "kopete-onlinestatus-icon: no active account found";
203 kDebug(14010) << "kopete-onlinestatus-icon: insufficient information in m_fileName: " << parts
;
211 m_buffer
.open(QIODevice::WriteOnly
);
213 if (!tmpImage
.isNull())
215 tmpImage
.save(&m_buffer
, "JPEG");
219 if (!tmpPixmap
.isNull())
221 tmpPixmap
.save(&m_buffer
, "PNG");
230 m_buffer
.open(openMode
);
234 kDebug(14010) << "return: " << completed
;
239 bool FileEngine::close()
243 if(m_buffer
.isOpen())
253 qint64
FileEngine::size() const
255 return m_buffer
.size();
258 qint64
FileEngine::pos() const
260 return m_buffer
.pos();
263 bool FileEngine::seek(qint64 newPos
)
265 return m_buffer
.seek(newPos
);
268 bool FileEngine::isSequential() const
273 bool FileEngine::remove()
278 bool FileEngine::rename(const QString
&newName
)
284 bool FileEngine::mkdir(const QString
&dirName
, bool createParentDirectories
) const
287 Q_UNUSED(createParentDirectories
);
291 bool FileEngine::rmdir(const QString
&dirName
, bool recurseParentDirectories
) const
294 Q_UNUSED(recurseParentDirectories
);
298 bool FileEngine::setSize(qint64 size
)
304 bool FileEngine::caseSensitive() const
309 bool FileEngine::isRelativePath() const
314 QStringList
FileEngine::entryList(QDir::Filters filters
, const QStringList
&filterNames
) const
317 Q_UNUSED(filterNames
);
318 return QStringList();
321 FileEngine::FileFlags
FileEngine::fileFlags(FileFlags type
) const
327 bool FileEngine::setPermissions(uint perms
)
333 QString
FileEngine::fileName(FileName file
) const
339 uint
FileEngine::ownerId(FileOwner owner
) const
345 QString
FileEngine::owner(FileOwner owner
) const
351 QDateTime
FileEngine::fileTime(FileTime time
) const
357 void FileEngine::setFileName(const QString
&file
)
362 qint64
FileEngine::read(char *data
, qint64 maxlen
)
364 return m_buffer
.read(data
, maxlen
);;
367 qint64
FileEngine::readLine(char *data
, qint64 maxlen
)
369 return m_buffer
.readLine(data
, maxlen
);
372 qint64
FileEngine::write(const char *data
, qint64 len
)
379 bool FileEngine::atEnd() const
381 return m_buffer
.atEnd();
383 } // END namespace Kopete