[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/pim/krss/resources/libkrssresource
From:       Dmitry Ivanov <vonami () gmail ! com>
Date:       2009-03-28 18:45:22
Message-ID: 1238265922.237715.15558.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 946133 by divanov:

 - use K_GLOBAL_STATIC for the collection cache
 - implement RssResourceBase::exportOpml()


 M  +1 -0      CMakeLists.txt  
 A             exportopmljob.cpp   [License: GPL (v2+)]
 A             exportopmljob.h   [License: GPL (v2+)]
 M  +1 -1      importopmljob.cpp  
 M  +3 -1      org.kde.krss.xml  
 M  +32 -5     rssresourcebase.cpp  
 M  +3 -1      rssresourcebase.h  
 M  +45 -27    rssresourcebasejobs.cpp  
 M  +7 -2      rssresourcebasejobs.h  


--- trunk/playground/pim/krss/resources/libkrssresource/CMakeLists.txt #946132:946133
@@ -3,6 +3,7 @@
     rssresourcebase.cpp
     rssresourcebasejobs.cpp
     importopmljob.cpp
+    exportopmljob.cpp
     batchitemmodifyjob.cpp
     rssitemsync.cpp
     util.cpp
--- trunk/playground/pim/krss/resources/libkrssresource/importopmljob.cpp #946132:946133
@@ -257,7 +257,7 @@
     if ( m_pendingJobs > 0 )
         return;
 
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
     emitResult();
 }
 
--- trunk/playground/pim/krss/resources/libkrssresource/org.kde.krss.xml #946132:946133
@@ -57,8 +57,10 @@
       <arg name="defaultTag" type="s" direction="in"/>
     </method>
     <method name="exportOpml">
+      <arg type="a{sv}" direction="out">
+        <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/>
+      </arg>
       <arg name="path" type="s" direction="in"/>
-      <annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
     </method>
     <method name="importItems">
       <arg type="b" direction="out"/>
--- trunk/playground/pim/krss/resources/libkrssresource/rssresourcebase.cpp #946132:946133
@@ -20,6 +20,7 @@
 #include "rssbackendjobs.h"
 #include "rssresourcebasejobs.h"
 #include "importopmljob.h"
+#include "exportopmljob.h"
 #include "libkrss/resourcemanager.h"
 
 #include <KDE/Akonadi/CollectionFetchJob>
@@ -208,9 +209,16 @@
     return QVariantMap();
 }
 
-void RssResourceBase::exportOpml( const QString &path )
+QVariantMap RssResourceBase::exportOpml( const QString &path )
 {
-    return;
+    ExportOpmlJob * const job = new ExportOpmlJob( KUrl( path ), this );
+    connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotOpmlExported( KJob* ) ) );
+    job->start();
+
+    // send the reply later
+    setDelayedReply( true );
+    d->m_replies.insert( job, QDBusContext::message().createReply() );
+    return QVariantMap();
 }
 
 bool RssResourceBase::importItems( const QString& xmlUrl, const QString& path )
@@ -272,7 +280,7 @@
     changeCommitted( mjob->feed() );
 
     // clear the cache as that collection has been changed in Akonadi
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
 }
 
 void RssResourceBase::slotCollectionDeleted( KJob *job )
@@ -286,7 +294,7 @@
     changeCommitted( djob->feed() );
 
     // clear the cache as that collection has been removed from Akonadi
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
 }
 
 void RssResourceBase::slotItemChanged( KJob *job )
@@ -394,7 +402,7 @@
 void RssResourceBase::slotOpmlImported( KJob *job )
 {
     if ( job->error() ) {
-        kWarning() << "Failed to fetch the feed";
+        kWarning() << "Failed to import the feeds";
         kWarning() << job->errorString();
     }
 
@@ -418,4 +426,23 @@
     QDBusConnection::sessionBus().send( replyMessage );
 }
 
+void RssResourceBase::slotOpmlExported( KJob *job )
+{
+    if ( job->error() ) {
+        kWarning() << "Failed to export the feeds";
+        kWarning() << job->errorString();
+    }
+
+    const ExportOpmlJob* const ejob = qobject_cast<ExportOpmlJob*>( job );
+    Q_ASSERT( ejob );
+    Q_ASSERT( d->m_replies.contains( job ) );
+
+    QDBusMessage replyMessage = d->m_replies.take( job );
+    QVariantMap result;
+    result.insert( "error", ejob->error() );
+    result.insert( "errorString", ejob->errorString() );
+    replyMessage << result;
+    QDBusConnection::sessionBus().send( replyMessage );
+}
+
 #include "rssresourcebase.moc"
--- trunk/playground/pim/krss/resources/libkrssresource/rssresourcebase.h #946132:946133
@@ -50,6 +50,7 @@
     friend class FeedCollectionDeleteJob;
     friend class FeedCollectionFetchJob;
     friend class ImportOpmlJob;
+    friend class ExportOpmlJob;
 
 public:
     explicit RssResourceBase( const QString& id );
@@ -74,7 +75,7 @@
     bool unsubscribe( const KRss::Feed::Id& id, const QString& subscriptionLabel );
     QStringList subscriptions( const KRss::Feed::Id& id );
     QVariantMap importOpml( const QString &path, const QString &defaultTag );
-    void exportOpml( const QString &path );
+    QVariantMap exportOpml( const QString &path );
     bool importItems( const QString& xmlUrl, const QString& path );
 
 Q_SIGNALS:
@@ -102,6 +103,7 @@
     void slotItemsRetrieved( KJob *job );
     void slotFeedFetched( KJob *job );
     void slotOpmlImported( KJob *job );
+    void slotOpmlExported( KJob *job );
 
 private:
     virtual bool flagsSynchronizable() const = 0;
--- trunk/playground/pim/krss/resources/libkrssresource/rssresourcebasejobs.cpp #946132:946133
@@ -37,6 +37,41 @@
 
 using namespace KRssResource;
 
+
+// Cache that contains Akonadi collections owned by the resource.
+// Normally, jobs declared in this file has direct access to it,
+// but some jobs from the outside (namely, Import/ExportOpmlJob)
+// which defined in separate files need to modify the cache as well.
+// They have to use the functions defined in FeedCollectionsCache namespace.
+struct FeedCollectionsCacheStruct
+{
+    FeedCollectionsCacheStruct() : m_cached( false ) {}
+    KRss::FeedCollection m_rootCollection;
+    QHash<KRss::Feed::Id, Akonadi::Collection> m_feeds;
+    bool m_cached;
+};
+
+K_GLOBAL_STATIC( FeedCollectionsCacheStruct, s_feedCollectionsCache )
+
+QList<Akonadi::Collection> KRssResource::FeedCollectionsCache::feedCollections()
+{
+    Q_ASSERT( s_feedCollectionsCache->m_cached );
+    return s_feedCollectionsCache->m_feeds.values();
+}
+
+void KRssResource::FeedCollectionsCache::insertFeedCollection( const Akonadi::Collection& feed )
+{
+    Q_ASSERT( s_feedCollectionsCache->m_cached );
+    s_feedCollectionsCache->m_feeds.insert( feed.id(), feed );
+}
+
+void KRssResource::FeedCollectionsCache::clear()
+{
+    s_feedCollectionsCache->m_rootCollection = Akonadi::Collection();
+    s_feedCollectionsCache->m_feeds.clear();
+    s_feedCollectionsCache->m_cached = false;
+}
+
 class FeedCollectionRetrieveJob::Private
 {
 public:
@@ -49,18 +84,8 @@
     const KRss::Feed::Id m_id;
     const RssResourceBase * const m_resource;
     FeedCollectionRetrieveJob * const q;
-
-    // cache
-    static KRss::FeedCollection m_rootCollection;
-    static QHash<KRss::Feed::Id, KRss::FeedCollection> m_feedsCache;
-    static bool m_cached;
 };
 
-KRss::FeedCollection FeedCollectionRetrieveJob::Private::m_rootCollection = Akonadi::Collection();
-QHash<KRss::Feed::Id, KRss::FeedCollection> FeedCollectionRetrieveJob::Private::m_feedsCache =
-                                            QHash<KRss::Feed::Id, KRss::FeedCollection>();
-bool FeedCollectionRetrieveJob::Private::m_cached = false;
-
 void FeedCollectionRetrieveJob::Private::emitCachedResult()
 {
     q->emitResult();
@@ -81,17 +106,17 @@
     const QList<Akonadi::Collection> cols = fjob->collections();
     Q_FOREACH( const Akonadi::Collection& col, cols ) {
         if ( col.parent() == Akonadi::Collection::root().id() ) {
-            m_rootCollection = col;
+            s_feedCollectionsCache->m_rootCollection = col;
             kDebug() << "Root collection:" << col.id();
         }
         else {
             // assumes that Feed::Id is Akonadi::Collection::Id
-            m_feedsCache.insert( col.id(), col );
+            s_feedCollectionsCache->m_feeds.insert( col.id(), col );
         }
     }
-    m_cached = true;
+    s_feedCollectionsCache->m_cached = true;
 
-    const QList<KRss::FeedCollection> feeds = m_feedsCache.values();
+    const QList<Akonadi::Collection> feeds = s_feedCollectionsCache->m_feeds.values();
     Q_FOREACH( const KRss::FeedCollection& feed, feeds ) {
         kDebug() << "Cached feed:" << feed.id() << ", title:" << feed.title();
     }
@@ -116,17 +141,10 @@
 
 KRss::FeedCollection FeedCollectionRetrieveJob::feedCollection() const
 {
-    return ( d->m_id == -1 ? Private::m_rootCollection :
-                             Private::m_feedsCache.value( d->m_id ) );
+    return ( d->m_id == -1 ? s_feedCollectionsCache->m_rootCollection :
+                             s_feedCollectionsCache->m_feeds.value( d->m_id ) );
 }
 
-void FeedCollectionRetrieveJob::clearCache() // static
-{
-    Private::m_rootCollection = Akonadi::Collection();
-    Private::m_feedsCache.clear();
-    Private::m_cached = false;
-}
-
 QString FeedCollectionRetrieveJob::errorString() const
 {
     QString result;
@@ -146,7 +164,7 @@
 
 void FeedCollectionRetrieveJob::start()
 {
-    if ( Private::m_cached ) {
+    if ( s_feedCollectionsCache->m_cached ) {
         kDebug() << "Already cached";
         QMetaObject::invokeMethod( this, "emitCachedResult", Qt::QueuedConnection );
         return;
@@ -229,7 +247,7 @@
     Q_ASSERT( ajob );
 
     m_collection = ajob->collection();
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
     q->emitResult();
 }
 
@@ -323,7 +341,7 @@
         return;
     }
 
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
     q->emitResult();
 }
 
@@ -427,7 +445,7 @@
         return;
     }
 
-    FeedCollectionRetrieveJob::clearCache();
+    FeedCollectionsCache::clear();
     q->emitResult();
 }
 
--- trunk/playground/pim/krss/resources/libkrssresource/rssresourcebasejobs.h #946132:946133
@@ -26,6 +26,13 @@
 namespace KRssResource
 {
 
+namespace FeedCollectionsCache
+{
+    QList<Akonadi::Collection> feedCollections();
+    void insertFeedCollection( const Akonadi::Collection& feed );
+    void clear();
+} // namespace FeedCollectionsCache
+
 class FeedCollectionRetrieveJob : public KJob
 {
     Q_OBJECT
@@ -44,8 +51,6 @@
         UserDefinedError
     };
 
-    static void clearCache();
-
 private:
     class Private;
     Private * const d;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic