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

List:       kde-commits
Subject:    [kdepim-runtime/akregator_port] krsslocal: Added: methods to write collection changes back to opml.
From:       Alessandro Cosentino <cosenal () gmail ! com>
Date:       2012-03-31 15:16:46
Message-ID: 20120331151646.80698A611A () git ! kde ! org
[Download RAW message or body]

Git commit 596f1c375c32d1116536bc1a1f4d8f50b310c97a by Alessandro Cosentino.
Committed on 02/09/2011 at 06:45.
Pushed by osterfeld into branch 'akregator_port'.

Added: methods to write collection changes back to opml. NOT TESTED YET

M  +1    -0    krsslocal/CMakeLists.txt
M  +66   -29   krsslocal/krsslocalresource.cpp
M  +5    -3    krsslocal/krsslocalresource.h
M  +13   -6    krsslocal/util.cpp
M  +11   -16   krsslocal/util.h

http://commits.kde.org/kdepim-runtime/596f1c375c32d1116536bc1a1f4d8f50b310c97a

diff --git a/krsslocal/CMakeLists.txt b/krsslocal/CMakeLists.txt
index 05615b7..f8a8ec3 100644
--- a/krsslocal/CMakeLists.txt
+++ b/krsslocal/CMakeLists.txt
@@ -47,6 +47,7 @@ set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}" \
)  
 set( krsslocalresource_SRCS
   krsslocalresource.cpp
+  util.cpp
 )
 
 install( FILES krsslocalresource.desktop DESTINATION \
                "${CMAKE_INSTALL_PREFIX}/share/akonadi/agents" )
diff --git a/krsslocal/krsslocalresource.cpp b/krsslocal/krsslocalresource.cpp
index cb7a362..e2895ee 100644
--- a/krsslocal/krsslocalresource.cpp
+++ b/krsslocal/krsslocalresource.cpp
@@ -9,11 +9,15 @@
 #include <KStandardDirs>
 #include <KLocale>
 #include <QtXml/QXmlStreamReader>
+#include <QtXml/QXmlStreamWriter>
 #include <QMessageBox>
 #include <Akonadi/EntityDisplayAttribute>
 #include <Akonadi/ItemFetchJob>
 #include <Akonadi/ItemFetchScope>
 #include <Akonadi/ChangeRecorder>
+#include <Akonadi/Collection>
+#include <Akonadi/CollectionFetchJob>
+#include <Akonadi/CollectionFetchScope>
 #include <krss/rssitem.h>
 #include <krssresource/krssresource_export.h>
 
@@ -30,17 +34,17 @@ KRssLocalResource::KRssLocalResource( const QString &id )
 
   //policy.setCacheTimeout( CACHE_TIMEOUT );
   //policy.setIntervalCheckTime( INTERVAL_CHECK_TIME );
-  //policy.setSyncOnDemand( true );
 
   policy.setInheritFromParent( false );
-  policy.setSyncOnDemand( true );
-  policy.setLocalParts( QStringList() << KRss::Item::HeadersPart << \
KRss::Item::ContentPart << Akonadi::Item::FullPayload ); +  policy.setSyncOnDemand( \
false ); +  policy.setLocalParts( QStringList() << KRss::Item::HeadersPart << \
KRss::Item::ContentPart ); +  
+  
+  //changeRecorder()->fetchCollection( true );
   
-  /*
-  changeRecorder()->fetchCollection( true );
   changeRecorder()->itemFetchScope().fetchFullPayload( false );
-  changeRecorder()->itemFetchScope().fetchAllAttributes( true );
-  */
+  //changeRecorder()->itemFetchScope().fetchAllAttributes( true );
+  
 }
 
 KRssLocalResource::~KRssLocalResource()
@@ -116,7 +120,8 @@ Collection::List KRssLocalResource::buildCollectionTree( \
QList<shared_ptr<const  foreach(const shared_ptr<const ParsedNode> parsedNode, \
listOfNodes) {  if (!parsedNode->isFolder()) {
 	    Collection c = (static_pointer_cast<const \
                ParsedFeed>(parsedNode))->toAkonadiCollection();
-	    c.setParent(parent);
+	    c.setContentMimeTypes( c.contentMimeTypes() );
+	    c.setParent( parent );
 
 	    c.setCachePolicy( policy );
 	    
@@ -128,11 +133,11 @@ Collection::List KRssLocalResource::buildCollectionTree( \
QList<shared_ptr<const  else {
 	    shared_ptr<const ParsedFolder> parsedFolder = static_pointer_cast<const \
ParsedFolder>(parsedNode);  Collection folder;
-	    folder.setParent(parent);
-	    folder.setName(parsedFolder->title());
-	    folder.setRemoteId(Settings::self()->path() + parsedFolder->title());
+	    folder.setParent( parent );
+	    folder.setName( parsedFolder->title() );
+	    folder.setRemoteId( Settings::self()->path() + parsedFolder->title() );
 	    folder.setContentMimeTypes( QStringList( Collection::mimeType() ) );
-	    list = buildCollectionTree(parsedFolder->children(), list, folder);
+	    list = buildCollectionTree( parsedFolder->children(), list, folder );
 	}
     }
   
@@ -171,7 +176,7 @@ void KRssLocalResource::slotLoadingComplete(Syndication::Loader* \
loader, Syndica  foreach ( const Syndication::ItemPtr& syndItem, m_syndItems ) {
 	  Akonadi::Item item( mimeType() );
 	  item.setRemoteId( syndItem->id() );
-	  item.setPayload<KRss::RssItem>( fromSyndicationItem( syndItem ) );
+	  item.setPayload<KRss::RssItem>( Util::fromSyndicationItem( syndItem ) );
 	  item.setFlag( KRss::RssItem::flagNew() );
 	  items << item;
      }
@@ -225,18 +230,16 @@ void KRssLocalResource::configure( WId windowId )
 
 void KRssLocalResource::itemAdded( const Akonadi::Item &item, const \
Akonadi::Collection &collection )  {
-  //Q_UNUSED( item );
   Q_UNUSED( collection );
-
-  // TODO: this method is called when somebody else, e.g. a client application,
-  // has created an item in a collection managed by your resource.
-
-  // NOTE: There is an equivalent method for collections, but it isn't part
-  // of this template code to keep it simple
   
   changeCommitted( item );
 }
 
+void KRssLocalResource::itemRemoved( const Akonadi::Item &item )
+{  
+  changeCommitted( item );
+}
+
 void KRssLocalResource::itemChanged( const Akonadi::Item &item, const \
QSet<QByteArray> &parts )  {
   Q_UNUSED( item );
@@ -244,22 +247,56 @@ void KRssLocalResource::itemChanged( const Akonadi::Item &item, \
const QSet<QByte  
   // TODO: this method is called when somebody else, e.g. a client application,
   // has changed an item managed by your resource.
-
-  // NOTE: There is an equivalent method for collections, but it isn't part
-  // of this template code to keep it simple
 }
 
-void KRssLocalResource::itemRemoved( const Akonadi::Item &item )
+void KRssLocalResource::collectionChanged(const Akonadi::Collection& collection)
 {
-  Q_UNUSED( item );
+  using namespace Akonadi;
+  
+  Q_UNUSED( collection );
+  
+  // fetching all collections containing rss feeds recursively, starting at the root \
collection +  CollectionFetchJob *job = new CollectionFetchJob( Collection::root(), \
CollectionFetchJob::Recursive, this ); +  job->fetchScope().setContentMimeTypes( \
QStringList() << mimeType() ); +  connect( job, SIGNAL( result( KJob* ) ), SLOT( \
fetchCollectionsFinished( KJob* ) ) ); +  
+}
 
-  // TODO: this method is called when somebody else, e.g. a client application,
-  // has deleted an item managed by your resource.
+void KRssLocalResource::fetchCollectionsFinished(KJob *job) {
+  
+   if ( job->error() ) {
+     qDebug() << "Error occurred";
+     return;
+   }
+
+   CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
 
-  // NOTE: There is an equivalent method for collections, but it isn't part
-  // of this template code to keep it simple
+   const Collection::List collections = fetchJob->collections();
+  
+   const QString path = Settings::self()->path();
+  
+   writeFeedsToOpml( path, collections );
 }
 
+void KRssLocalResource::writeFeedsToOpml(const QString &path, const \
QList<Akonadi::Collection>& feeds) +{
+  QFile file( path );
+  /* If we can't open it, let's show an error message. */
+  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+      error(i18n("Couldn't open ") + path);
+      return;
+  }
+        
+  QXmlStreamWriter writer( &file );
+  writer.setAutoFormatting( true );
+  writer.writeStartDocument();
+  OpmlWriter::writeOpml( writer, Util::toParsedFeedList( feeds ));
+  writer.writeEndDocument();
+
+}
+
+
+
 AKONADI_RESOURCE_MAIN( KRssLocalResource )
 
 #include "krsslocalresource.moc"
diff --git a/krsslocal/krsslocalresource.h b/krsslocal/krsslocalresource.h
index 60d0323..aa123f2 100644
--- a/krsslocal/krsslocalresource.h
+++ b/krsslocal/krsslocalresource.h
@@ -18,6 +18,7 @@ class KRssLocalResource : public Akonadi::ResourceBase,
     Akonadi::Collection::List buildCollectionTree( QList<boost::shared_ptr<const \
KRssResource::ParsedNode> > listOfNodes,   Akonadi::Collection::List &list, \
Akonadi::Collection &parent);  QString mimeType();
+    void writeFeedsToOpml(const QString &path, const QList<Akonadi::Collection>& \
feeds);  
   public Q_SLOTS:
     virtual void configure( WId windowId );
@@ -28,15 +29,16 @@ class KRssLocalResource : public Akonadi::ResourceBase,
     bool retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts );
     void slotLoadingComplete(Syndication::Loader* loader, Syndication::FeedPtr feed, \
  Syndication::ErrorCode status );
-   
+    void fetchCollectionsFinished( KJob *job );
+
+			
   protected:
     virtual void aboutToQuit();
 
     virtual void itemAdded( const Akonadi::Item &item, const Akonadi::Collection \
                &collection );
     virtual void itemChanged( const Akonadi::Item &item, const QSet<QByteArray> \
&parts );  virtual void itemRemoved( const Akonadi::Item &item );
-    
-    void fetchFeed( const Akonadi::Collection &feed );
+    virtual void collectionChanged( const Akonadi::Collection &collection );
     
   private:    
     Akonadi::CachePolicy policy;
diff --git a/krsslocal/util.cpp b/krsslocal/util.cpp
index 49cace7..3c04b3c 100644
--- a/krsslocal/util.cpp
+++ b/krsslocal/util.cpp
@@ -1,5 +1,6 @@
 /*
     Copyright (C) 2009    Dmitry Ivanov <vonami@gmail.com>
+    Copyright (C) 2011    Alessandro Cosentino <cosenal@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
@@ -36,9 +37,7 @@
 #include <QtCore/QMultiMap>
 #include <QtCore/QMapIterator>
 #include <QtXml/QDomElement>
-
-namespace Util
-{
+  
 // from akregator/src/utils.cpp
 static inline int calcHash( const QString& str )
 {
@@ -46,7 +45,7 @@ static inline int calcHash( const QString& str )
     return qChecksum( array.constData(), array.size() );
 }
 
-static KRss::RssItem KRssResource::fromSyndicationItem( const Syndication::ItemPtr& \
syndItem ) +KRss::RssItem KRssResource::Util::fromSyndicationItem(const \
Syndication::ItemPtr& syndItem)  {
     KRss::RssItem rssItem;
     rssItem.setHeadersLoaded( true );
@@ -133,6 +132,16 @@ static KRss::RssItem KRssResource::fromSyndicationItem( const \
Syndication::ItemP  return rssItem;
 }
 
+QList< boost::shared_ptr< KRssResource::ParsedFeed > > \
KRssResource::Util::toParsedFeedList(const QList< Akonadi::Collection >& feeds) +{
+    QList<boost::shared_ptr<ParsedFeed> > parsedFeeds;
+    Q_FOREACH( const Akonadi::Collection& feed, feeds ) {
+        if ( feed.parent() != Akonadi::Collection::root().id() )
+            parsedFeeds.append( ParsedFeed::fromAkonadiCollection( feed ) );
+    }
+    return parsedFeeds;
+}
+
 /*
 QString KRssResource::generateCollectionName( const Akonadi::Collection& collection \
)  {
@@ -142,5 +151,3 @@ QString KRssResource::generateCollectionName( const \
Akonadi::Collection& collect  
     return static_cast<KRss::FeedCollection>( collection ).title() + QLatin1Char( \
'-' ) + collection.remoteId();  }*/
-
-}
\ No newline at end of file
diff --git a/krsslocal/util.h b/krsslocal/util.h
index c275e37..c1abec8 100644
--- a/krsslocal/util.h
+++ b/krsslocal/util.h
@@ -18,22 +18,17 @@
 #ifndef KRSSRESOURCE_UTIL_H
 #define KRSSRESOURCE_UTIL_H
 
+#include <krss/rssitem.h>
 #include <Syndication/Item>
-
-namespace KRss {
-class RssItem;
-}
-
-namespace Akonadi {
-class Collection;
-}
-
-namespace KRssResource
-{
-
-KRss::RssItem fromSyndicationItem( const Syndication::ItemPtr& syndItem );
-//QString generateCollectionName( const Akonadi::Collection& collection );
-
-} // namespace KRssResource
+#include <krssresource/opmlparser.h>
+
+namespace KRssResource {
+  namespace Util {
+  
+    KRss::RssItem fromSyndicationItem( const Syndication::ItemPtr& syndItem );
+    QList<boost::shared_ptr<ParsedFeed> > toParsedFeedList( const \
QList<Akonadi::Collection>& feeds ); +  
+  };
+};
 
 #endif // KRSSRESOURCE_UTIL_H


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

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