diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 0eab1f5..93acdbf 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -21,6 +21,7 @@ #include "CommonUri.hh" #include "Entry.hh" +#include "Feed.hh" #include "http/Agent.hh" #include "http/ResponseLog.hh" @@ -83,14 +84,15 @@ Drive::Drive( OAuth2& auth, const Json& options ) : m_resume_link = resp["link"]. Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ; - bool has_next = false ; +// bool has_next = false ; + Feed feed( resp ) ; do { - xml::NodeSet entries = resp["entry"] ; - for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.end() ; ++i ) +// xml::NodeSet entries = resp["entry"] ; + for ( Feed::iterator i = feed.begin() ; i != feed.end() ; ++i ) { - if ( (*i)["content"] == "" ) - continue ; +// if ( (*i)["content"] == "" ) +// continue ; Entry entry( *i ) ; if ( entry.Kind() != "folder" ) @@ -116,15 +118,16 @@ Drive::Drive( OAuth2& auth, const Json& options ) : } } - xml::NodeSet nss = resp["link"].Find( "@rel", "next" ) ; - has_next = !nss.empty() ; +// xml::NodeSet nss = resp["link"].Find( "@rel", "next" ) ; +// has_next = !nss.empty() ; - if ( has_next ) - { - http.Get( nss["@href"], &xrsp, m_http_hdr ) ; - resp = xrsp.Response() ; - } - } while ( has_next ) ; +// std::string next_uri = feed.Next() ; +// if ( !next_uri.empty() ) +// { +// http.Get( next_uri, &xrsp, m_http_hdr ) ; +// resp = xrsp.Response() ; +// } + } while ( feed.GetNext( &http, m_http_hdr ) ) ; // pull the changes feed boost::format changes_uri( "https://docs.google.com/feeds/default/private/changes?start-index=%1%" ) ; diff --git a/libgrive/src/drive/Feed.cc b/libgrive/src/drive/Feed.cc new file mode 100644 index 0000000..a447a4f --- /dev/null +++ b/libgrive/src/drive/Feed.cc @@ -0,0 +1,87 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + 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 version 2 + of the License. + + 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "Feed.hh" + +#include "http/Agent.hh" +#include "http/XmlResponse.hh" +#include "xml/NodeSet.hh" + +#include + +namespace gr { + +Feed::Feed( const xml::Node& root ) : + m_root ( root ), + m_entries ( m_root["entry"] ) +{ +} + +Feed::iterator Feed::begin() const +{ + return iterator( m_entries.begin() ) ; +} + +Feed::iterator Feed::end() const +{ + return iterator( m_entries.end() ) ; +} + +std::string Feed::Next() const +{ + xml::NodeSet nss = m_root["link"].Find( "@rel", "next" ) ; + return nss.empty() ? "" : std::string(nss["@href"]) ; +} + +bool Feed::GetNext( http::Agent *http, const http::Header& auth ) +{ + assert( http != 0 ) ; + + xml::NodeSet nss = m_root["link"].Find( "@rel", "next" ) ; + if ( !nss.empty() ) + { + http::XmlResponse xrsp ; + http->Get( nss["@href"], &xrsp, auth ) ; + + m_root = xrsp.Response() ; + m_entries = m_root["entry"] ; + + return true ; + } + else + return false ; +} + +Feed::iterator::iterator( ) +{ +} + +Feed::iterator::iterator( xml::Node::iterator i ) +{ + // for some reason, gcc 4.4.4 doesn't allow me to initialize the base class + // in the initializer. I have no choice but to initialize here. + base_reference() = i ; +} + +Feed::iterator::reference Feed::iterator::dereference() const +{ + return Entry( *base_reference() ) ; +} + +} // end of namespace diff --git a/libgrive/src/drive/Feed.hh b/libgrive/src/drive/Feed.hh new file mode 100644 index 0000000..df67ac5 --- /dev/null +++ b/libgrive/src/drive/Feed.hh @@ -0,0 +1,76 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + 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 version 2 + of the License. + + 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "Entry.hh" + +#include "xml/Node.hh" +#include "xml/NodeSet.hh" + +#include + +#include + +namespace gr { + +namespace http +{ + class Agent ; + class Header ; +} + +class Feed +{ +public : + class iterator ; + +public : + explicit Feed( const xml::Node& root ) ; + + iterator begin() const ; + iterator end() const ; + + std::string Next() const ; + bool GetNext( http::Agent *http, const http::Header& auth ) ; + +private : + xml::Node m_root ; + xml::NodeSet m_entries ; +} ; + +class Feed::iterator : public boost::iterator_adaptor< + Feed::iterator, + xml::Node::iterator, + Entry, + boost::random_access_traversal_tag, + Entry +> +{ +public : + iterator() ; + explicit iterator( xml::Node::iterator i ) ; + +private : + friend class boost::iterator_core_access; + + reference dereference() const ; +} ; + +} // end of namespace diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index 8bd3b71..be12d31 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -196,6 +196,10 @@ private : ImplVec m_children ; } ; +Node::iterator::iterator( ) +{ +} + Node::iterator::iterator( ImplVec::iterator i ) { // for some reason, gcc 4.4.4 doesn't allow me to initialize the base class diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index b7902bd..85c2a6f 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -100,6 +100,7 @@ class Node::iterator : public boost::iterator_adaptor< > { public : + iterator( ) ; explicit iterator( ImplVec::iterator i ) ; private :