Port KProgressBar's to QProgressBar's.
[kdenetwork.git] / dcoprss / cache.cpp
blob76b340b2a79d8c3e4085d160f97edc63b6738e3b
1 /*
2 * cache.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "cache.h"
26 #include "xmlrpciface.h"
28 #include <kdebug.h>
29 #include <kstandarddirs.h>
31 #include <qdatastream.h>
32 #include <qfile.h>
33 //Added by qt3to4:
34 #include <QList>
36 bool CacheEntry::isValid() const
38 // Cache entries get invalid after on hour. One shouldn't hardcode this
39 // but for now it'll do.
40 return m_timeStamp.secsTo( QDateTime::currentDateTime() ) < 3600;
43 Cache *Cache::m_instance = 0;
45 Cache &Cache::self()
47 if ( !m_instance )
48 m_instance = new Cache;
49 return *m_instance;
52 QString Cache::getCacheKey( const QString &server, const QString &method,
53 const QList<QVariant> &args )
55 QString key;
56 key = server + QString::fromLatin1( "__" );
57 key += method + QString::fromLatin1( "__" );
58 QList<QVariant>::ConstIterator it = args.begin();
59 QList<QVariant>::ConstIterator end = args.end();
60 for ( ; it != end; ++it )
61 key += KXMLRPC::Query::marshal( *it );
63 return key;
66 Cache::Cache()
68 load();
71 Cache::~Cache()
73 save();
76 void Cache::load()
78 QFile file( cacheFileName() );
79 if ( !file.open( QIODevice::ReadOnly ) ) {
80 kDebug() << "Failed to open cache file " << cacheFileName() << endl;
81 return;
84 QDataStream stream( &file );
85 while ( !stream.atEnd() ) {
86 QString key;
87 stream >> key;
89 CacheEntry *entry = new CacheEntry;
90 stream >> *entry;
92 Q3Dict<CacheEntry>::insert( key, entry );
96 void Cache::save()
98 QFile file( cacheFileName() );
99 if ( !file.open( QIODevice::WriteOnly ) ) {
100 kDebug() << "Failed to open cache file " << cacheFileName() << endl;
101 return;
104 QDataStream stream( &file );
106 Q3DictIterator<CacheEntry> it( *this );
107 for ( ; it.current() != 0; ++it )
108 stream << it.currentKey() << *it.current();
111 void Cache::touch( const QString &key )
113 CacheEntry *entry = find( key );
114 if ( !entry )
115 return;
116 entry->m_timeStamp = QDateTime::currentDateTime();
119 void Cache::insert( const QString &key, const KXMLRPC::Query::Result &result )
121 CacheEntry *entry = new CacheEntry;
122 entry->m_timeStamp = QDateTime::currentDateTime();
123 entry->m_result = result;
124 Q3Dict<CacheEntry>::insert( key, entry );
127 QString Cache::cacheFileName() const
129 return locateLocal( "appdata", "cache/dcoprss.cache" );