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

List:       kde-commits
Subject:    [kdepim-runtime/akregator_port] krsslocal: bypass the default ItemSync
From:       Alessandro Cosentino <cosenal () gmail ! com>
Date:       2012-03-31 15:16:46
Message-ID: 20120331151646.CB724A613E () git ! kde ! org
[Download RAW message or body]

Git commit 230e40a38f737246d449ea58c808c61788c84378 by Alessandro Cosentino.
Committed on 22/09/2011 at 05:37.
Pushed by osterfeld into branch 'akregator_port'.

bypass the default ItemSync

M  +17   -16   krsslocal/krsslocalresource.cpp
M  +3    -0    krsslocal/krsslocalresource.h
A  +86   -0    krsslocal/rssitemsync.cpp     [License: GPL (v2+)]
A  +38   -0    krsslocal/rssitemsync.h     [License: GPL (v2+)]

http://commits.kde.org/kdepim-runtime/230e40a38f737246d449ea58c808c61788c84378

diff --git a/krsslocal/krsslocalresource.cpp b/krsslocal/krsslocalresource.cpp
index f8f9ac0..52f7919 100644
--- a/krsslocal/krsslocalresource.cpp
+++ b/krsslocal/krsslocalresource.cpp
@@ -48,7 +48,7 @@ using namespace KRssResource;
 using namespace boost;
 
 KRssLocalResource::KRssLocalResource( const QString &id )
-  : ResourceBase( id )
+  : ResourceBase( id ), m_syncer( 0 )
 {
   new SettingsAdaptor( Settings::self() );
   QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
@@ -89,9 +89,6 @@ QString KRssLocalResource::mimeType()
   return QLatin1String("application/rss+xml");
 }
 
-// this method is called when Akonadi wants to have all the
-// collections your resource provides.
-// Be sure to set the remote ID and the content MIME types
 void KRssLocalResource::retrieveCollections()
 {
     
@@ -184,12 +181,6 @@ Collection::List KRssLocalResource::buildCollectionTree( \
QList<shared_ptr<const  
 void KRssLocalResource::retrieveItems( const Akonadi::Collection &collection )
 {   
-// TODO: this method is called when Akonadi wants to know about all the
-// items in the given collection. You can but don't have to provide all the
-// data for each item, remote ID and MIME type are enough at this stage.
-// Depending on how your resource accesses the data, there are several
-// different ways to tell Akonadi when you are done.
-
     Syndication::Loader * const loader = Syndication::Loader::create();
     connect( loader, SIGNAL( loadingComplete( Syndication::Loader*, \
                Syndication::FeedPtr, Syndication::ErrorCode ) ),
             this, SLOT( slotLoadingComplete( Syndication::Loader*, \
Syndication::FeedPtr, Syndication::ErrorCode ) ) ); @@ -229,18 +220,28 @@ void \
KRssLocalResource::slotLoadingComplete(Syndication::Loader* loader, Syndica  items << \
item;  }
 
-    itemsRetrieved( items );
+    //--- a replacement of itemsRetrieved that uses a custom ItemSync---
+    if (!m_syncer) {
+	m_syncer = new RssItemSync( fc );
+	connect( m_syncer, SIGNAL(result(KJob*)), this, SLOT(slotItemSyncDone(KJob*)) );
+    }
+    m_syncer->setFullSyncItems( items );
+    //------------------------------------------------------------------
  
 }
 
+void KRssLocalResource::slotItemSyncDone( KJob *job ) {
+  m_syncer = 0;
+  if ( job->error() && job->error() != Job::UserCanceled ) {
+    emit error( job->errorString() );
+  }
+  itemsRetrievalDone();
+}
+
 bool KRssLocalResource::retrieveItem( const Akonadi::Item &item, const \
QSet<QByteArray> &parts )  {
   Q_UNUSED( parts );
-    
-  // TODO: this method is called when Akonadi wants more data for a given item.
-  // You can only provide the parts that have been requested but you are allowed
-  // to provide all in one go
-
+ 
   itemRetrieved( item );
   return true;
 }
diff --git a/krsslocal/krsslocalresource.h b/krsslocal/krsslocalresource.h
index bf94585..2a0bfc1 100644
--- a/krsslocal/krsslocalresource.h
+++ b/krsslocal/krsslocalresource.h
@@ -25,6 +25,7 @@
 #include <QTimer>
 #include <QHash>
 #include "opmlparser.h"
+#include "rssitemsync.h"
 
 class KRssLocalResource : public Akonadi::ResourceBase,
                            public Akonadi::AgentBase::Observer
@@ -52,6 +53,7 @@ class KRssLocalResource : public Akonadi::ResourceBase,
 			Syndication::ErrorCode status );
     void fetchCollections();
     void fetchCollectionsFinished( KJob *job );
+    void slotItemSyncDone( KJob *job );
 
 			
   protected:
@@ -69,6 +71,7 @@ class KRssLocalResource : public Akonadi::ResourceBase,
     static const int CacheTimeout = -1, IntervalCheckTime = 5; 
     static const int WriteBackTimeout = 30000; // in milliseconds
     QString titleOpml;
+    RssItemSync *m_syncer;
 };
 
 #endif
diff --git a/krsslocal/rssitemsync.cpp b/krsslocal/rssitemsync.cpp
new file mode 100644
index 0000000..08aee7b
--- /dev/null
+++ b/krsslocal/rssitemsync.cpp
@@ -0,0 +1,86 @@
+/*
+    Copyright (C) 2008    Dmitry Ivanov <vonami@gmail.com>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "rssitemsync.h"
+
+#include <krss/rssitem.h>
+#include <krss/item.h>
+
+#include <KDebug>
+
+RssItemSync::RssItemSync( const Akonadi::Collection &collection, QObject *parent )
+    : ItemSync( collection, parent ), m_synchronizeFlags( false )
+{
+}
+
+void RssItemSync::setSynchronizeFlags( bool synchronizeFlags )
+{
+    m_synchronizeFlags = synchronizeFlags;
+}
+
+bool RssItemSync::synchronizeFlags() const
+{
+    return m_synchronizeFlags;
+}
+
+bool RssItemSync::updateItem( const Akonadi::Item &storedItem, Akonadi::Item \
&newItem ) +{
+    if ( storedItem.mimeType() != KRss::Item::mimeType() ||
+        newItem.mimeType() != KRss::Item::mimeType() ) {
+        kWarning() << "Either storedItem or newItem doesn't have mimetype \
\'application/rss+xml\'"; +        kWarning() << "Remote id:" << \
storedItem.remoteId(); +        return false;
+    }
+
+    if ( !storedItem.hasPayload() || !newItem.hasPayload() ) {
+        kWarning() << "Either storedItem or newItem doesn't have payload";
+        kWarning() << "Remote id:" << storedItem.remoteId();
+        return false;
+    }
+
+    const KRss::RssItem newRssItem = newItem.payload<KRss::RssItem>();
+    const int newHash = newRssItem.hash();
+    const int storedHash = storedItem.payload<KRss::RssItem>().hash();
+
+    if ( !newRssItem.guidIsHash() && storedHash != newHash ) {
+        kDebug() << "The article's content is updated:" << newItem.remoteId();
+        // dont overwrite the existing flags
+        // and set 'New'
+        newItem.setFlags( storedItem.flags() );
+        return true;
+    }
+
+    if ( m_synchronizeFlags ) {
+        const bool readFlagsDiffer = storedItem.hasFlag( KRss::RssItem::flagRead() ) \
!= +                                     newItem.hasFlag( KRss::RssItem::flagRead() \
); +        const bool importantFlagsDiffer = storedItem.hasFlag( \
KRss::RssItem::flagImportant() ) != +                                          \
newItem.hasFlag( KRss::RssItem::flagImportant() ); +
+        // either 'Seen' or 'Important' was changed in the backend
+        // push the item to Akonadi clearing 'New'
+        if ( readFlagsDiffer || importantFlagsDiffer ) {
+            kDebug() << "The article's flags are updated:" << newItem.remoteId();
+            // We need to explicitely overwrite the item's flags
+            // otherwise Akonadi::ItemModifyJob just ignores them,
+            // see Akonadi::ItemModifyJob::doStart()
+            newItem.setFlags( newItem.flags() );
+            return true;
+        }
+    }
+
+    return false;
+}
diff --git a/krsslocal/rssitemsync.h b/krsslocal/rssitemsync.h
new file mode 100644
index 0000000..f221d74
--- /dev/null
+++ b/krsslocal/rssitemsync.h
@@ -0,0 +1,38 @@
+/*
+    Copyright (C) 2008    Dmitry Ivanov <vonami@gmail.com>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef KRSSRESOURCE_RSSITEMSYNC
+#define KRSSRESOURCE_RSSITEMSYNC
+
+#include <akonadi/itemsync.h>
+
+class RssItemSync : public Akonadi::ItemSync
+{
+public:
+    explicit RssItemSync( const Akonadi::Collection& collection, QObject *parent = 0 \
); +
+    void setSynchronizeFlags( bool synchronizeFlags );
+    bool synchronizeFlags() const;
+
+protected:
+    bool updateItem( const Akonadi::Item& storedItem, Akonadi::Item& newItem );
+
+private:
+    bool m_synchronizeFlags;
+};
+
+#endif // KRSSRESOURCE_RSSITEMSYNC


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

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