Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / kopetefileengine.cpp
blobd59615ef3952bf26a67950c170f4c1777e46bd8d
1 /*
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 *************************************************************************
11 * *
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. *
16 * *
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"
29 #include <kdebug.h>
30 #include <kiconloader.h>
32 #include <QPixmap>
33 #include <qdiriterator.h>
34 #include <qstring.h>
35 #include <qstringlist.h>
37 namespace Kopete
39 QAbstractFileEngine *FileEngineHandler::create(const QString &fileName) const
41 bool handle = false;
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()
51 : m_buffer(&m_data)
53 kDebug(14010) ;
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;
74 QPixmap tmpPixmap;
75 QImage tmpImage;
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]);
89 if (account)
91 Contact *contact = account->contacts()[ parts[3] ];
93 if (contact)
95 tmpPixmap = QPixmap(contact->onlineStatus().iconFor(contact,16));
96 completed = true;
98 else
100 kDebug(14010) << "kopete-contact-icon: contact not found";
103 else
105 kDebug(14010) << "kopete-contact-icon: account not found";
108 else
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]);
120 if (account)
122 tmpPixmap = QPixmap(account->myself()->onlineStatus().iconFor(account->myself(),16));
123 completed = true;
125 else
127 kDebug(14010) << "kopete-account-icon: account not found";
130 else
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]);
142 if (mc)
144 tmpPixmap = QPixmap(SmallIcon(mc->statusIcon()));
145 completed = true;
148 else
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]);
160 if (mc)
162 tmpImage = QImage(mc->picture().image());
163 completed = true;
166 else
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();
191 if (account)
193 tmpPixmap = QPixmap(account->myself()->onlineStatus().iconFor(parts[1]));
194 completed = true;
196 else
198 kDebug(14010) << "kopete-onlinestatus-icon: no active account found";
201 else
203 kDebug(14010) << "kopete-onlinestatus-icon: insufficient information in m_fileName: " << parts;
207 close();
209 if (completed)
211 m_buffer.open(QIODevice::WriteOnly);
213 if (!tmpImage.isNull())
215 tmpImage.save(&m_buffer, "JPEG");
217 else
219 if (!tmpPixmap.isNull())
221 tmpPixmap.save(&m_buffer, "PNG");
223 else
225 completed = false;
229 m_buffer.close();
230 m_buffer.open(openMode);
231 m_buffer.seek(0);
234 kDebug(14010) << "return: " << completed;
236 return completed;
239 bool FileEngine::close()
241 kDebug(14010) ;
243 if(m_buffer.isOpen())
245 m_buffer.close();
248 m_data.clear();
250 return true;
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
270 return false;
273 bool FileEngine::remove()
275 return false;
278 bool FileEngine::rename(const QString &newName)
280 Q_UNUSED(newName);
281 return false;
284 bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
286 Q_UNUSED(dirName);
287 Q_UNUSED(createParentDirectories);
288 return false;
291 bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
293 Q_UNUSED(dirName);
294 Q_UNUSED(recurseParentDirectories);
295 return false;
298 bool FileEngine::setSize(qint64 size)
300 Q_UNUSED(size);
301 return false;
304 bool FileEngine::caseSensitive() const
306 return false;
309 bool FileEngine::isRelativePath() const
311 return false;
314 QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
316 Q_UNUSED(filters);
317 Q_UNUSED(filterNames);
318 return QStringList();
321 FileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
323 Q_UNUSED(type);
324 return 0;
327 bool FileEngine::setPermissions(uint perms)
329 Q_UNUSED(perms);
330 return false;
333 QString FileEngine::fileName(FileName file) const
335 Q_UNUSED(file);
336 return m_fileName;
339 uint FileEngine::ownerId(FileOwner owner) const
341 Q_UNUSED(owner);
342 return 0;
345 QString FileEngine::owner(FileOwner owner) const
347 Q_UNUSED(owner);
348 return QString();
351 QDateTime FileEngine::fileTime(FileTime time) const
353 Q_UNUSED(time);
354 return QDateTime();
357 void FileEngine::setFileName(const QString &file)
359 m_fileName = 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)
374 Q_UNUSED(data);
375 Q_UNUSED(len);
376 return -1;
379 bool FileEngine::atEnd() const
381 return m_buffer.atEnd();
383 } // END namespace Kopete