mirror of https://github.com/vitalif/grive2
reuse the Entry class in Collection
parent
56e605c392
commit
b51e5c593f
|
@ -31,8 +31,7 @@
|
||||||
namespace gr {
|
namespace gr {
|
||||||
|
|
||||||
Collection::Collection( const Json& entry ) :
|
Collection::Collection( const Json& entry ) :
|
||||||
m_title ( entry["title"]["$t"].As<std::string>() ),
|
m_entry ( entry ),
|
||||||
m_href ( entry["link"].FindInArray( "rel", "self" )["href"].As<std::string>() ),
|
|
||||||
m_parent ( 0 )
|
m_parent ( 0 )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -40,8 +39,7 @@ Collection::Collection( const Json& entry ) :
|
||||||
Collection::Collection(
|
Collection::Collection(
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const std::string& href ) :
|
const std::string& href ) :
|
||||||
m_title ( title ),
|
m_entry ( title, href ),
|
||||||
m_href ( href ),
|
|
||||||
m_parent( 0 )
|
m_parent( 0 )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -53,14 +51,14 @@ std::string Collection::ParentHref( const Json& entry )
|
||||||
node["href"].As<std::string>() : std::string() ;
|
node["href"].As<std::string>() : 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
|
const Collection* Collection::Parent() const
|
||||||
|
@ -73,6 +71,11 @@ Collection* Collection::Parent()
|
||||||
return m_parent ;
|
return m_parent ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Collection::ParentHref() const
|
||||||
|
{
|
||||||
|
return m_entry.ParentHref() ;
|
||||||
|
}
|
||||||
|
|
||||||
void Collection::AddChild( Collection *child )
|
void Collection::AddChild( Collection *child )
|
||||||
{
|
{
|
||||||
assert( child != 0 ) ;
|
assert( child != 0 ) ;
|
||||||
|
@ -98,8 +101,7 @@ bool Collection::IsCollection( const Json& entry )
|
||||||
|
|
||||||
void Collection::Swap( Collection& coll )
|
void Collection::Swap( Collection& coll )
|
||||||
{
|
{
|
||||||
m_title.swap( coll.m_title ) ;
|
m_entry.Swap( coll.m_entry ) ;
|
||||||
m_href.swap( coll.m_href ) ;
|
|
||||||
std::swap( m_parent, coll.m_parent ) ;
|
std::swap( m_parent, coll.m_parent ) ;
|
||||||
m_child.swap( coll.m_child ) ;
|
m_child.swap( coll.m_child ) ;
|
||||||
m_leaves.swap( coll.m_leaves ) ;
|
m_leaves.swap( coll.m_leaves ) ;
|
||||||
|
@ -107,7 +109,7 @@ void Collection::Swap( Collection& coll )
|
||||||
|
|
||||||
void Collection::CreateSubDir( const Path& prefix )
|
void Collection::CreateSubDir( const Path& prefix )
|
||||||
{
|
{
|
||||||
Path dir = prefix / m_title ;
|
Path dir = prefix / m_entry.Title() ;
|
||||||
os::MakeDir( dir ) ;
|
os::MakeDir( dir ) ;
|
||||||
|
|
||||||
for ( std::vector<Collection*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
|
for ( std::vector<Collection*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
|
||||||
|
@ -126,7 +128,7 @@ void Collection::ForEachFile(
|
||||||
Path Collection::Dir() const
|
Path Collection::Dir() const
|
||||||
{
|
{
|
||||||
assert( m_parent != this ) ;
|
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
|
} // end of namespace
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Entry.hh"
|
||||||
#include "util/Function.hh"
|
#include "util/Function.hh"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -40,10 +41,11 @@ public :
|
||||||
static bool IsCollection( const Json& entry ) ;
|
static bool IsCollection( const Json& entry ) ;
|
||||||
static std::string ParentHref( const Json& entry ) ;
|
static std::string ParentHref( const Json& entry ) ;
|
||||||
|
|
||||||
const std::string& Title() const ;
|
std::string Title() const ;
|
||||||
const std::string& Href() const ;
|
std::string Href() const ;
|
||||||
const Collection* Parent() const ;
|
const Collection* Parent() const ;
|
||||||
Collection* Parent() ;
|
Collection* Parent() ;
|
||||||
|
std::string ParentHref() const ;
|
||||||
Path Dir() const ;
|
Path Dir() const ;
|
||||||
|
|
||||||
void AddChild( Collection *child ) ;
|
void AddChild( Collection *child ) ;
|
||||||
|
@ -58,8 +60,7 @@ public :
|
||||||
const std::string& prefix = "." ) ;
|
const std::string& prefix = "." ) ;
|
||||||
|
|
||||||
private :
|
private :
|
||||||
std::string m_title ;
|
Entry m_entry ;
|
||||||
std::string m_href ;
|
|
||||||
|
|
||||||
// not owned
|
// not owned
|
||||||
Collection *m_parent ;
|
Collection *m_parent ;
|
||||||
|
|
|
@ -125,7 +125,6 @@ abc << xml.Response()["feed"] ;
|
||||||
|
|
||||||
assert( m_coll.empty() ) ;
|
assert( m_coll.empty() ) ;
|
||||||
|
|
||||||
std::map<std::string, std::string> parent_href ;
|
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
|
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
|
||||||
|
@ -134,15 +133,8 @@ abc << xml.Response()["feed"] ;
|
||||||
for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i )
|
for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i )
|
||||||
{
|
{
|
||||||
if ( Collection::IsCollection( *i ) )
|
if ( Collection::IsCollection( *i ) )
|
||||||
{
|
|
||||||
m_coll.push_back( Collection( *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 ;
|
Json next ;
|
||||||
if ( !resp["feed"]["link"].FindInArray( "rel", "next", 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() ) ;
|
std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ;
|
||||||
for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i )
|
for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i )
|
||||||
{
|
{
|
||||||
assert( parent_href.find( i->Href() ) != parent_href.end() ) ;
|
if ( i->ParentHref().empty() )
|
||||||
std::string parent = parent_href[i->Href()] ;
|
|
||||||
|
|
||||||
if ( parent.empty() )
|
|
||||||
m_root.AddChild( &*i ) ;
|
m_root.AddChild( &*i ) ;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
FolderListIterator pit = FindFolder( parent ) ;
|
FolderListIterator pit = FindFolder( i->ParentHref() ) ;
|
||||||
if ( pit != m_coll.end() )
|
if ( pit != m_coll.end() )
|
||||||
{
|
{
|
||||||
// it shouldn't happen, just in case
|
// it shouldn't happen, just in case
|
||||||
|
@ -193,9 +182,9 @@ void Drive::UpdateFile( const Json& entry )
|
||||||
Path path = Path() / file.Filename() ;
|
Path path = Path() / file.Filename() ;
|
||||||
|
|
||||||
// determine which folder the file belongs to
|
// 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() )
|
if ( pit != m_coll.end() )
|
||||||
path = pit->Dir() / file.Filename() ;
|
path = pit->Dir() / file.Filename() ;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,12 @@ Entry::Entry( const Json& entry )
|
||||||
Update( 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 )
|
void Entry::Update( const Json& entry )
|
||||||
{
|
{
|
||||||
m_title = entry["title"]["$t"].Str() ;
|
m_title = entry["title"]["$t"].Str() ;
|
||||||
|
@ -53,8 +59,9 @@ void Entry::Update( const Json& entry )
|
||||||
m_filename = entry.Has("docs$suggestedFilename") ?
|
m_filename = entry.Has("docs$suggestedFilename") ?
|
||||||
entry["docs$suggestedFilename"]["$t"].Str() : "" ;
|
entry["docs$suggestedFilename"]["$t"].Str() : "" ;
|
||||||
|
|
||||||
m_href = entry["content"]["src"].Str() ;
|
m_content_src = entry["content"]["src"].Str() ;
|
||||||
m_parent = Parent( entry ) ;
|
m_self_href = entry["link"].FindInArray( "rel", "self" )["href"].Str() ;
|
||||||
|
m_parent_href = Parent( entry ) ;
|
||||||
m_server_modified = DateTime( entry["updated"]["$t"].Str() ) ;
|
m_server_modified = DateTime( entry["updated"]["$t"].Str() ) ;
|
||||||
m_etag = entry["gd$etag"].Str() ;
|
m_etag = entry["gd$etag"].Str() ;
|
||||||
|
|
||||||
|
@ -112,21 +119,21 @@ DateTime Entry::ServerModified() const
|
||||||
return m_server_modified ;
|
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
|
void Entry::Download( const Path& file, const http::Headers& auth ) const
|
||||||
{
|
{
|
||||||
gr::Download dl( file.Str(), Download::NoChecksum() ) ;
|
gr::Download dl( file.Str(), Download::NoChecksum() ) ;
|
||||||
http::Agent http ;
|
http::Agent http ;
|
||||||
long r = http.Get( m_href, &dl, auth ) ;
|
long r = http.Get( m_content_src, &dl, auth ) ;
|
||||||
if ( r <= 400 )
|
if ( r <= 400 )
|
||||||
os::SetFileTime( file, m_server_modified ) ;
|
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 ) ;
|
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
|
} // end of namespace
|
||||||
|
|
|
@ -37,6 +37,7 @@ class Entry
|
||||||
public :
|
public :
|
||||||
explicit Entry( const Path& file ) ;
|
explicit Entry( const Path& file ) ;
|
||||||
explicit Entry( const Json& entry ) ;
|
explicit Entry( const Json& entry ) ;
|
||||||
|
Entry( const std::string& title, const std::string& href ) ;
|
||||||
|
|
||||||
std::string Title() const ;
|
std::string Title() const ;
|
||||||
std::string Filename() const ;
|
std::string Filename() const ;
|
||||||
|
@ -47,13 +48,15 @@ public :
|
||||||
std::string ResourceID() const ;
|
std::string ResourceID() const ;
|
||||||
std::string ETag() const ;
|
std::string ETag() const ;
|
||||||
|
|
||||||
std::string Href() const ;
|
std::string SelfHref() const ;
|
||||||
std::string Parent() const ;
|
std::string ParentHref() const ;
|
||||||
|
|
||||||
void Download( const Path& file, const http::Headers& auth ) const ;
|
void Download( const Path& file, const http::Headers& auth ) const ;
|
||||||
bool Upload( std::streambuf *file, const http::Headers& auth ) ;
|
bool Upload( std::streambuf *file, const http::Headers& auth ) ;
|
||||||
void Delete( gr::http::Agent* http, const gr::http::Headers& auth ) ;
|
void Delete( gr::http::Agent* http, const gr::http::Headers& auth ) ;
|
||||||
|
|
||||||
|
void Swap( Entry& e ) ;
|
||||||
|
|
||||||
private :
|
private :
|
||||||
void Update( const Json& entry ) ;
|
void Update( const Json& entry ) ;
|
||||||
static std::string Parent( const Json& entry ) ;
|
static std::string Parent( const Json& entry ) ;
|
||||||
|
@ -66,8 +69,9 @@ private :
|
||||||
std::string m_etag ;
|
std::string m_etag ;
|
||||||
std::string m_resource_id ;
|
std::string m_resource_id ;
|
||||||
|
|
||||||
std::string m_href ;
|
std::string m_self_href ;
|
||||||
std::string m_parent ;
|
std::string m_content_src ;
|
||||||
|
std::string m_parent_href ;
|
||||||
std::string m_upload_link ;
|
std::string m_upload_link ;
|
||||||
|
|
||||||
DateTime m_server_modified ;
|
DateTime m_server_modified ;
|
||||||
|
|
|
@ -129,4 +129,10 @@ bool DateTime::operator<=( const DateTime& dt ) const
|
||||||
return !( *this > dt ) ;
|
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
|
} // end of namespace
|
||||||
|
|
|
@ -47,6 +47,8 @@ public :
|
||||||
bool operator<( const DateTime& dt ) const ;
|
bool operator<( const DateTime& dt ) const ;
|
||||||
bool operator<=( const DateTime& dt ) const ;
|
bool operator<=( const DateTime& dt ) const ;
|
||||||
|
|
||||||
|
void Swap( DateTime& dt ) ;
|
||||||
|
|
||||||
private :
|
private :
|
||||||
std::time_t m_sec ;
|
std::time_t m_sec ;
|
||||||
unsigned long m_nsec ;
|
unsigned long m_nsec ;
|
||||||
|
|
Loading…
Reference in New Issue