From b51e5c593f15b27fd17a88fb8c967305bcdd2530 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Wed, 9 May 2012 23:52:06 +0800 Subject: [PATCH] reuse the Entry class in Collection --- libgrive/src/drive/Collection.cc | 26 ++++++++++++---------- libgrive/src/drive/Collection.hh | 9 ++++---- libgrive/src/drive/Drive.cc | 19 ++++------------ libgrive/src/drive/Entry.cc | 38 ++++++++++++++++++++++++++------ libgrive/src/drive/Entry.hh | 12 ++++++---- libgrive/src/util/DateTime.cc | 6 +++++ libgrive/src/util/DateTime.hh | 2 ++ 7 files changed, 70 insertions(+), 42 deletions(-) diff --git a/libgrive/src/drive/Collection.cc b/libgrive/src/drive/Collection.cc index 5b61817..5418601 100644 --- a/libgrive/src/drive/Collection.cc +++ b/libgrive/src/drive/Collection.cc @@ -31,8 +31,7 @@ namespace gr { Collection::Collection( const Json& entry ) : - m_title ( entry["title"]["$t"].As() ), - m_href ( entry["link"].FindInArray( "rel", "self" )["href"].As() ), + m_entry ( entry ), m_parent ( 0 ) { } @@ -40,8 +39,7 @@ Collection::Collection( const Json& entry ) : Collection::Collection( const std::string& title, const std::string& href ) : - m_title ( title ), - m_href ( href ), + m_entry ( title, href ), m_parent( 0 ) { } @@ -53,14 +51,14 @@ std::string Collection::ParentHref( const Json& entry ) node["href"].As() : std::string() ; } -const std::string& Collection::Href() const +std::string Collection::Href() const { - return m_href ; + return m_entry.SelfHref() ; } -const std::string& Collection::Title() const +std::string Collection::Title() const { - return m_title ; + return m_entry.Title() ; } const Collection* Collection::Parent() const @@ -73,6 +71,11 @@ Collection* Collection::Parent() return m_parent ; } +std::string Collection::ParentHref() const +{ + return m_entry.ParentHref() ; +} + void Collection::AddChild( Collection *child ) { assert( child != 0 ) ; @@ -98,8 +101,7 @@ bool Collection::IsCollection( const Json& entry ) void Collection::Swap( Collection& coll ) { - m_title.swap( coll.m_title ) ; - m_href.swap( coll.m_href ) ; + m_entry.Swap( coll.m_entry ) ; std::swap( m_parent, coll.m_parent ) ; m_child.swap( coll.m_child ) ; m_leaves.swap( coll.m_leaves ) ; @@ -107,7 +109,7 @@ void Collection::Swap( Collection& coll ) void Collection::CreateSubDir( const Path& prefix ) { - Path dir = prefix / m_title ; + Path dir = prefix / m_entry.Title() ; os::MakeDir( dir ) ; for ( std::vector::iterator i = m_child.begin() ; i != m_child.end() ; ++i ) @@ -126,7 +128,7 @@ void Collection::ForEachFile( Path Collection::Dir() const { assert( m_parent != this ) ; - return m_parent != 0 ? (m_parent->Dir() / m_title) : Path() ; + return m_parent != 0 ? (m_parent->Dir() / m_entry.Title()) : Path() ; } } // end of namespace diff --git a/libgrive/src/drive/Collection.hh b/libgrive/src/drive/Collection.hh index 3c785fe..9289ae2 100644 --- a/libgrive/src/drive/Collection.hh +++ b/libgrive/src/drive/Collection.hh @@ -19,6 +19,7 @@ #pragma once +#include "Entry.hh" #include "util/Function.hh" #include @@ -40,10 +41,11 @@ public : static bool IsCollection( const Json& entry ) ; static std::string ParentHref( const Json& entry ) ; - const std::string& Title() const ; - const std::string& Href() const ; + std::string Title() const ; + std::string Href() const ; const Collection* Parent() const ; Collection* Parent() ; + std::string ParentHref() const ; Path Dir() const ; void AddChild( Collection *child ) ; @@ -58,8 +60,7 @@ public : const std::string& prefix = "." ) ; private : - std::string m_title ; - std::string m_href ; + Entry m_entry ; // not owned Collection *m_parent ; diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index b65e82c..5bf2cb3 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -125,7 +125,6 @@ abc << xml.Response()["feed"] ; assert( m_coll.empty() ) ; - std::map parent_href ; while ( true ) { Json::Array entries = resp["feed"]["entry"].As() ; @@ -134,15 +133,8 @@ abc << xml.Response()["feed"] ; for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++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 ) ) @@ -156,14 +148,11 @@ abc << xml.Response()["feed"] ; std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ; for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i ) { - assert( parent_href.find( i->Href() ) != parent_href.end() ) ; - std::string parent = parent_href[i->Href()] ; - - if ( parent.empty() ) + if ( i->ParentHref().empty() ) m_root.AddChild( &*i ) ; else { - FolderListIterator pit = FindFolder( parent ) ; + FolderListIterator pit = FindFolder( i->ParentHref() ) ; if ( pit != m_coll.end() ) { // it shouldn't happen, just in case @@ -193,9 +182,9 @@ void Drive::UpdateFile( const Json& entry ) Path path = Path() / file.Filename() ; // determine which folder the file belongs to - if ( !file.Parent().empty() ) + if ( !file.ParentHref().empty() ) { - FolderListIterator pit = FindFolder( file.Parent() ) ; + FolderListIterator pit = FindFolder( file.ParentHref() ) ; if ( pit != m_coll.end() ) path = pit->Dir() / file.Filename() ; } diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index 406e1fc..f52df34 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -46,6 +46,12 @@ Entry::Entry( const Json& entry ) Update( entry ) ; } +Entry::Entry( const std::string& title, const std::string& href ) : + m_title( title ), + m_self_href( href ) +{ +} + void Entry::Update( const Json& entry ) { m_title = entry["title"]["$t"].Str() ; @@ -53,8 +59,9 @@ void Entry::Update( const Json& entry ) m_filename = entry.Has("docs$suggestedFilename") ? entry["docs$suggestedFilename"]["$t"].Str() : "" ; - m_href = entry["content"]["src"].Str() ; - m_parent = Parent( entry ) ; + m_content_src = entry["content"]["src"].Str() ; + m_self_href = entry["link"].FindInArray( "rel", "self" )["href"].Str() ; + m_parent_href = Parent( entry ) ; m_server_modified = DateTime( entry["updated"]["$t"].Str() ) ; m_etag = entry["gd$etag"].Str() ; @@ -112,21 +119,21 @@ DateTime Entry::ServerModified() const return m_server_modified ; } -std::string Entry::Href() const +std::string Entry::SelfHref() const { - return m_href ; + return m_self_href ; } -std::string Entry::Parent() const +std::string Entry::ParentHref() const { - return m_parent ; + return m_parent_href ; } void Entry::Download( const Path& file, const http::Headers& auth ) const { gr::Download dl( file.Str(), Download::NoChecksum() ) ; http::Agent http ; - long r = http.Get( m_href, &dl, auth ) ; + long r = http.Get( m_content_src, &dl, auth ) ; if ( r <= 400 ) os::SetFileTime( file, m_server_modified ) ; } @@ -199,4 +206,21 @@ std::cout << root_url + "/" + m_resource_id + "?delete=true" << std::endl ; http->Custom( "DELETE", root_url + "/" + m_resource_id + "?delete=true", &str, hdr ) ; } +void Entry::Swap( Entry& e ) +{ + m_title.swap( e.m_title ) ; + m_filename.swap( e.m_filename ) ; + m_kind.swap( e.m_kind ) ; + m_server_md5.swap( e.m_server_md5 ) ; + m_etag.swap( e.m_etag ) ; + m_resource_id.swap( e.m_resource_id ) ; + + m_self_href.swap( e.m_self_href ) ; + m_parent_href.swap( e.m_parent_href ) ; + m_content_src.swap( e.m_content_src ) ; + m_upload_link.swap( e.m_upload_link ) ; + + m_server_modified.Swap( e.m_server_modified ) ; +} + } // end of namespace diff --git a/libgrive/src/drive/Entry.hh b/libgrive/src/drive/Entry.hh index 68f28b1..c199241 100644 --- a/libgrive/src/drive/Entry.hh +++ b/libgrive/src/drive/Entry.hh @@ -37,6 +37,7 @@ class Entry public : explicit Entry( const Path& file ) ; explicit Entry( const Json& entry ) ; + Entry( const std::string& title, const std::string& href ) ; std::string Title() const ; std::string Filename() const ; @@ -47,13 +48,15 @@ public : std::string ResourceID() const ; std::string ETag() const ; - std::string Href() const ; - std::string Parent() const ; + std::string SelfHref() const ; + std::string ParentHref() const ; void Download( const Path& file, const http::Headers& auth ) const ; bool Upload( std::streambuf *file, const http::Headers& auth ) ; void Delete( gr::http::Agent* http, const gr::http::Headers& auth ) ; + void Swap( Entry& e ) ; + private : void Update( const Json& entry ) ; static std::string Parent( const Json& entry ) ; @@ -66,8 +69,9 @@ private : std::string m_etag ; std::string m_resource_id ; - std::string m_href ; - std::string m_parent ; + std::string m_self_href ; + std::string m_content_src ; + std::string m_parent_href ; std::string m_upload_link ; DateTime m_server_modified ; diff --git a/libgrive/src/util/DateTime.cc b/libgrive/src/util/DateTime.cc index 76e553e..354ee8e 100644 --- a/libgrive/src/util/DateTime.cc +++ b/libgrive/src/util/DateTime.cc @@ -129,4 +129,10 @@ bool DateTime::operator<=( const DateTime& dt ) const return !( *this > dt ) ; } +void DateTime::Swap( DateTime& dt ) +{ + std::swap( m_sec, dt.m_sec ) ; + std::swap( m_nsec, dt.m_nsec ) ; +} + } // end of namespace diff --git a/libgrive/src/util/DateTime.hh b/libgrive/src/util/DateTime.hh index 0f796ec..585668e 100644 --- a/libgrive/src/util/DateTime.hh +++ b/libgrive/src/util/DateTime.hh @@ -47,6 +47,8 @@ public : bool operator<( const DateTime& dt ) const ; bool operator<=( const DateTime& dt ) const ; + void Swap( DateTime& dt ) ; + private : std::time_t m_sec ; unsigned long m_nsec ;