| 1 | /*
|
|---|
| 2 | * vcardfactory.cpp - class for caching vCards
|
|---|
| 3 | * Copyright (C) 2003 Michail Pishchagin
|
|---|
| 4 | *
|
|---|
| 5 | * This program is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * as published by the Free Software Foundation; either version 2
|
|---|
| 8 | * of the License, or (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * This program is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License
|
|---|
| 16 | * along with this library; if not, write to the Free Software
|
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 18 | *
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "vcardfactory.h"
|
|---|
| 22 |
|
|---|
| 23 | #include <qobject.h>
|
|---|
| 24 | #include <qapplication.h>
|
|---|
| 25 |
|
|---|
| 26 | #include <qdict.h>
|
|---|
| 27 | #include <qmap.h>
|
|---|
| 28 |
|
|---|
| 29 | #include <qdom.h>
|
|---|
| 30 | #include <qfile.h>
|
|---|
| 31 | #include <qtextstream.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "common.h" // jidEncode
|
|---|
| 34 | #include "profiles.h"
|
|---|
| 35 |
|
|---|
| 36 | //----------------------------------------------------------------------------
|
|---|
| 37 | // VCardFactory
|
|---|
| 38 | //----------------------------------------------------------------------------
|
|---|
| 39 |
|
|---|
| 40 | class VCardFactoryPrivate : public QObject
|
|---|
| 41 | {
|
|---|
| 42 | Q_OBJECT
|
|---|
| 43 |
|
|---|
| 44 | private:
|
|---|
| 45 | static const uint dictSize;
|
|---|
| 46 | static QStringList vcardList;
|
|---|
| 47 | static QDict<VCard> *vcardDict;
|
|---|
| 48 |
|
|---|
| 49 | public slots:
|
|---|
| 50 | void taskFinished();
|
|---|
| 51 |
|
|---|
| 52 | signals:
|
|---|
| 53 | void vcardChanged(const Jid&);
|
|---|
| 54 |
|
|---|
| 55 | public:
|
|---|
| 56 | VCardFactoryPrivate();
|
|---|
| 57 | ~VCardFactoryPrivate();
|
|---|
| 58 |
|
|---|
| 59 | static QDict<VCard> *vcards();
|
|---|
| 60 | static void checkLimit(QString jid, VCard *vcard);
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | const uint VCardFactoryPrivate::dictSize = 5; // don't cache too many vCards at a time
|
|---|
| 64 | QDict<VCard> *VCardFactoryPrivate::vcardDict = 0;
|
|---|
| 65 | QStringList VCardFactoryPrivate::vcardList;
|
|---|
| 66 |
|
|---|
| 67 | VCardFactoryPrivate::VCardFactoryPrivate()
|
|---|
| 68 | : QObject(qApp, "VCardFactoryPrivate")
|
|---|
| 69 | {
|
|---|
| 70 | vcardDict = new QDict<VCard>;
|
|---|
| 71 | vcardDict->setAutoDelete(true);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | VCardFactoryPrivate::~VCardFactoryPrivate()
|
|---|
| 75 | {
|
|---|
| 76 | delete vcardDict;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | QDict<VCard> *VCardFactoryPrivate::vcards()
|
|---|
| 80 | {
|
|---|
| 81 | return vcardDict;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void VCardFactoryPrivate::checkLimit(QString jid, VCard *vcard)
|
|---|
| 85 | {
|
|---|
| 86 | if ( vcardList.find(jid) != vcardList.end() ) {
|
|---|
| 87 | vcardDict->remove(jid);
|
|---|
| 88 | vcardList.remove(jid);
|
|---|
| 89 | }
|
|---|
| 90 | else if ( vcardList.size() > dictSize ) {
|
|---|
| 91 | vcardDict->remove( vcardList.back() );
|
|---|
| 92 | vcardList.pop_back();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | vcardDict->insert(jid, vcard);
|
|---|
| 96 | vcardList.push_front(jid);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void VCardFactoryPrivate::taskFinished()
|
|---|
| 100 | {
|
|---|
| 101 | JT_VCard *task = (JT_VCard *)sender();
|
|---|
| 102 | if ( task->success() ) {
|
|---|
| 103 | Jid j = task->jid();
|
|---|
| 104 |
|
|---|
| 105 | VCard *vcard = new VCard;
|
|---|
| 106 | *vcard = task->vcard();
|
|---|
| 107 | checkLimit(j.userHost(), vcard);
|
|---|
| 108 |
|
|---|
| 109 | // save vCard to disk
|
|---|
| 110 |
|
|---|
| 111 | // ensure that there's a vcard directory to save into
|
|---|
| 112 | QDir p(pathToProfile(activeProfile));
|
|---|
| 113 | QDir v(pathToProfile(activeProfile) + "/vcard");
|
|---|
| 114 | if(!v.exists())
|
|---|
| 115 | p.mkdir("vcard");
|
|---|
| 116 |
|
|---|
| 117 | QFile file ( getVCardDir() + "/" + jidEncode(j.userHost()).lower() + ".xml" );
|
|---|
| 118 | file.open ( IO_WriteOnly );
|
|---|
| 119 | QTextStream out ( &file );
|
|---|
| 120 | out.setEncoding ( QTextStream::UnicodeUTF8 );
|
|---|
| 121 | QDomDocument doc;
|
|---|
| 122 | doc.appendChild( vcard->toXml ( &doc ) );
|
|---|
| 123 | out << doc.toString(4);
|
|---|
| 124 |
|
|---|
| 125 | emit vcardChanged(j);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static VCardFactoryPrivate *vcardFactoryPrivate = 0;
|
|---|
| 130 |
|
|---|
| 131 | const VCard *VCardFactory::vcard(const Jid &j)
|
|---|
| 132 | {
|
|---|
| 133 | if ( !vcardFactoryPrivate )
|
|---|
| 134 | vcardFactoryPrivate = new VCardFactoryPrivate;
|
|---|
| 135 |
|
|---|
| 136 | // first, try to get vCard from runtime cache
|
|---|
| 137 | QDict<VCard> *vcards = VCardFactoryPrivate::vcards();
|
|---|
| 138 | if ( vcards->find( j.userHost() ) )
|
|---|
| 139 | return vcards->find( j.userHost() );
|
|---|
| 140 |
|
|---|
| 141 | // then try to load from cache on disk
|
|---|
| 142 | QFile file ( getVCardDir() + "/" + jidEncode(j.userHost()).lower() + ".xml" );
|
|---|
| 143 | file.open (IO_ReadOnly);
|
|---|
| 144 | QDomDocument doc;
|
|---|
| 145 | VCard *vcard = new VCard;
|
|---|
| 146 | if ( doc.setContent(&file, false) ) {
|
|---|
| 147 | vcard->fromXml( doc.documentElement() );
|
|---|
| 148 | VCardFactoryPrivate::checkLimit(j.userHost(), vcard);
|
|---|
| 149 |
|
|---|
| 150 | return vcard;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | delete vcard;
|
|---|
| 154 | return 0;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void VCardFactory::setVCard(const Jid &j, const VCard &v)
|
|---|
| 158 | {
|
|---|
| 159 | if ( !vcardFactoryPrivate )
|
|---|
| 160 | vcardFactoryPrivate = new VCardFactoryPrivate;
|
|---|
| 161 |
|
|---|
| 162 | VCard *vcard = new VCard;
|
|---|
| 163 | *vcard = v;
|
|---|
| 164 | VCardFactoryPrivate::checkLimit(j.userHost(), vcard);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | JT_VCard *VCardFactory::getVCard(const Jid &jid, Task *rootTask, const QObject *obj, const char *slot, bool cacheVCard)
|
|---|
| 168 | {
|
|---|
| 169 | if ( !vcardFactoryPrivate )
|
|---|
| 170 | vcardFactoryPrivate = new VCardFactoryPrivate;
|
|---|
| 171 |
|
|---|
| 172 | JT_VCard *task = new JT_VCard( rootTask );
|
|---|
| 173 | if ( cacheVCard )
|
|---|
| 174 | task->connect(task, SIGNAL(finished()), vcardFactoryPrivate, SLOT(taskFinished()));
|
|---|
| 175 | task->connect(task, SIGNAL(finished()), obj, slot);
|
|---|
| 176 | task->get(jid);
|
|---|
| 177 | task->go(true);
|
|---|
| 178 | return task;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | void VCardFactory::registerVCardChanged(const QObject* obj, const char* slot)
|
|---|
| 182 | {
|
|---|
| 183 | if ( !vcardFactoryPrivate )
|
|---|
| 184 | vcardFactoryPrivate = new VCardFactoryPrivate;
|
|---|
| 185 | QObject::connect(vcardFactoryPrivate,SIGNAL(vcardChanged(const Jid&)),obj,slot);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | #include"vcardfactory.moc"
|
|---|
| 189 |
|
|---|