From 1eeaede474b080713d25641e3e21d39dce05d684 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sun, 6 May 2012 18:14:36 +0800 Subject: [PATCH] fetching the next link --- libgrive/src/drive/Drive.cc | 62 +++++++++++++++++++++++++------------ libgrive/src/drive/Drive.hh | 2 +- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 84b1f55..e1d1bda 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -50,22 +50,32 @@ Drive::Drive( OAuth2& auth ) : m_http_hdr.push_back( "Authorization: Bearer " + m_auth.AccessToken() ) ; m_http_hdr.push_back( "GData-Version: 3.0" ) ; - Json resp = Json::Parse( http::Get( root_url + "?alt=json&showfolders=true", m_http_hdr )) ; + ConstructDirTree( ) ; + + Json resp = Json::Parse( http::Get( root_url + "?alt=json", m_http_hdr )) ; Json resume_link ; if ( resp["feed"]["link"].FindInArray( "rel", "http://schemas.google.com/g/2005#resumable-create-media", resume_link ) ) m_resume_link = resume_link["href"].As() ; - - Json::Array entries = resp["feed"]["entry"].As() ; - ConstructDirTree( entries ) ; - - for ( Json::Array::iterator i = entries.begin() ; i != entries.end() ; ++i ) + + bool has_next = false ; + do { - if ( !Collection::IsCollection( *i ) ) + Json::Array entries = resp["feed"]["entry"].As() ; + for ( Json::Array::iterator i = entries.begin() ; i != entries.end() ; ++i ) { - UpdateFile( *i ) ; + if ( !Collection::IsCollection( *i ) ) + { + UpdateFile( *i ) ; + } } - } + + Json next ; + has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ; + + if ( has_next ) + resp = Json::Parse( http::Get( next["href"].Str(), m_http_hdr ) ) ; + } while ( has_next ) ; } Drive::~Drive( ) @@ -93,24 +103,36 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href ) return (it != m_coll.end() && it->Href() == href) ? it : m_coll.end() ; } -void Drive::ConstructDirTree( const std::vector& entries ) +void Drive::ConstructDirTree( ) { + Json resp = Json::Parse( http::Get( root_url + "/-/folder?alt=json", m_http_hdr )) ; assert( m_coll.empty() ) ; + std::map parent_href ; - - // first, get all collections from the query result - for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i ) + while ( true ) { - if ( Collection::IsCollection( *i ) ) + Json::Array entries = resp["feed"]["entry"].As() ; + + // first, get all collections from the query result + for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i ) { - m_coll.push_back( Collection( *i ) ) ; - parent_href.insert( - std::make_pair( - m_coll.back().Href(), - Collection::ParentHref( *i ) ) ) ; + if ( Collection::IsCollection( *i ) ) + { + m_coll.push_back( Collection( *i ) ) ; + parent_href.insert( + std::make_pair( + m_coll.back().Href(), + Collection::ParentHref( *i ) ) ) ; + } } + assert( m_coll.size() == parent_href.size() ) ; + + Json next ; + if ( !resp["feed"]["link"].FindInArray( "rel", "next", next ) ) + break ; + + resp = Json::Parse( http::Get( next["href"].Str(), m_http_hdr ) ) ; } - assert( m_coll.size() == parent_href.size() ) ; // second, build up linkage between parent and child std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ; diff --git a/libgrive/src/drive/Drive.hh b/libgrive/src/drive/Drive.hh index 347e527..0f8f38a 100644 --- a/libgrive/src/drive/Drive.hh +++ b/libgrive/src/drive/Drive.hh @@ -42,7 +42,7 @@ public : private : void UpdateFile( const Json& entry ) ; - void ConstructDirTree( const std::vector& entries ) ; + void ConstructDirTree( ) ; FolderListIterator FindFolder( const std::string& href ) ;