merge the list of the files and folders in the same list

pull/40/head
Matchman Green 2012-05-19 18:03:02 +08:00
parent b46c9debf8
commit 13aa9a455b
8 changed files with 74 additions and 60 deletions

View File

@ -98,8 +98,7 @@ Drive::Drive( OAuth2& auth ) :
{
Resource *p = m_state.FindFolderByHref( file.ParentHref() ) ;
if ( p != 0 && p->IsInRootTree() )
// m_state.OnEntry( file ) ;
UpdateFile( file, p, &http ) ;
m_state.OnEntry( file ) ;
else
Log( "file \"%1%\" parent doesn't exist, ignored", file.Title() ) ;
}
@ -119,11 +118,6 @@ Drive::Drive( OAuth2& auth ) :
m_state.Write( state_file ) ;
}
Drive::~Drive( )
{
std::for_each( m_files.begin(), m_files.end(), Destroy() ) ;
}
void Drive::ConstructDirTree( http::Agent *http )
{
http::XmlResponse xml ;
@ -162,7 +156,7 @@ void Drive::ConstructDirTree( http::Agent *http )
m_state.ResolveEntry() ;
}
/*
void Drive::UpdateFile( Entry& entry, Resource *parent, http::Agent *http )
{
assert( parent != 0 ) ;
@ -181,11 +175,11 @@ void Drive::UpdateFile( Entry& entry, Resource *parent, http::Agent *http )
Log( "file \"%1%\" is a google document, ignored", entry.Title() ) ;
}
}
*/
void Drive::Update()
{
http::Agent http ;
std::for_each( m_files.begin(), m_files.end(),
std::for_each( m_state.begin(), m_state.end(),
boost::bind( &Resource::Update, _1, &http, m_http_hdr ) ) ;
}

View File

@ -19,10 +19,8 @@
#pragma once
#include "Resource.hh"
#include "State.hh"
#include "protocol/Json.hh"
#include "util/Exception.hh"
#include <string>
@ -34,23 +32,13 @@ namespace http
{
class Agent ;
}
namespace xml
{
class Node ;
}
class OAuth2 ;
class File ;
class Drive
{
public :
typedef std::vector<Resource> FolderList ;
typedef std::vector<Resource>::iterator FolderListIterator ;
public :
Drive( OAuth2& auth ) ;
~Drive( ) ;
void Update() ;
void Sync() ;
@ -58,8 +46,6 @@ public :
struct Error : virtual Exception {} ;
private :
void UpdateFile( Entry& file, Resource *parent, http::Agent *http ) ;
void ConstructDirTree( http::Agent *http ) ;
private :
@ -67,8 +53,6 @@ private :
std::vector<std::string> m_http_hdr ;
std::string m_resume_link ;
std::vector<Resource*> m_files ;
State m_state ;
} ;

View File

@ -146,11 +146,13 @@ void Entry::Swap( Entry& e )
m_etag.swap( e.m_etag ) ;
m_resource_id.swap( e.m_resource_id ) ;
m_parent_hrefs.swap( e.m_parent_hrefs ) ;
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 ) ;
}

View File

@ -118,12 +118,14 @@ bool Resource::IsFolder() const
fs::path Resource::Path() const
{
assert( m_parent != this ) ;
assert( m_parent == 0 || m_parent->IsFolder() ) ;
return m_parent != 0 ? (m_parent->Path() / Name()) : "." ;
}
bool Resource::IsInRootTree() const
{
assert( m_parent == 0 || m_parent->IsFolder() ) ;
return m_parent == 0 ? (SelfHref() == root_href) : m_parent->IsInRootTree() ;
}
@ -140,6 +142,10 @@ Resource* Resource::FindChild( const std::string& name )
void Resource::Update( http::Agent *http, const http::Headers& auth )
{
// no need to update for folders
if ( IsFolder() )
return ;
assert( m_parent != 0 ) ;
bool changed = true ;

View File

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "FolderSet.hh"
#include "ResourceTree.hh"
#include "CommonUri.hh"
#include "util/Destroy.hh"
@ -29,13 +29,13 @@ namespace gr {
using namespace details ;
FolderSet::FolderSet( ) :
ResourceTree::ResourceTree( ) :
m_root( new Resource( ".", "folder", root_href ) )
{
m_set.insert( m_root ) ;
}
FolderSet::FolderSet( const FolderSet& fs ) :
ResourceTree::ResourceTree( const ResourceTree& fs ) :
m_root( 0 )
{
const Set& s = fs.m_set.get<ByIdentity>() ;
@ -51,45 +51,48 @@ FolderSet::FolderSet( const FolderSet& fs ) :
assert( m_root != 0 ) ;
}
FolderSet::~FolderSet( )
ResourceTree::~ResourceTree( )
{
// delete all pointers
const Set& s = m_set.get<ByIdentity>() ;
std::for_each( s.begin(), s.end(), Destroy() ) ;
}
Resource* FolderSet::Root()
Resource* ResourceTree::Root()
{
assert( m_root != 0 ) ;
return m_root ;
}
const Resource* FolderSet::Root() const
const Resource* ResourceTree::Root() const
{
assert( m_root != 0 ) ;
return m_root ;
}
void FolderSet::Swap( FolderSet& fs )
void ResourceTree::Swap( ResourceTree& fs )
{
m_set.swap( fs.m_set ) ;
}
FolderSet& FolderSet::operator=( const FolderSet& fs )
ResourceTree& ResourceTree::operator=( const ResourceTree& fs )
{
FolderSet tmp( fs ) ;
ResourceTree tmp( fs ) ;
Swap( tmp ) ;
return *this ;
}
Resource* FolderSet::FindByHref( const std::string& href )
Resource* ResourceTree::FindByHref( const std::string& href )
{
if ( href.empty() )
return 0 ;
HrefMap& map = m_set.get<ByHref>() ;
HrefMap::iterator i = map.find( href ) ;
return i != map.end() ? *i : 0 ;
}
const Resource* FolderSet::FindByHref( const std::string& href ) const
const Resource* ResourceTree::FindByHref( const std::string& href ) const
{
const HrefMap& map = m_set.get<ByHref>() ;
HrefMap::const_iterator i = map.find( href ) ;
@ -97,7 +100,7 @@ const Resource* FolderSet::FindByHref( const std::string& href ) const
}
/// Reinsert should be called when the ID/HREF were updated
bool FolderSet::ReInsert( Resource *coll )
bool ResourceTree::ReInsert( Resource *coll )
{
Set& s = m_set.get<ByIdentity>() ;
Set::iterator i = s.find( coll ) ;
@ -111,29 +114,29 @@ bool FolderSet::ReInsert( Resource *coll )
return false ;
}
void FolderSet::Insert( Resource *coll )
void ResourceTree::Insert( Resource *coll )
{
m_set.insert( coll ) ;
}
void FolderSet::Erase( Resource *coll )
void ResourceTree::Erase( Resource *coll )
{
Set& s = m_set.get<ByIdentity>() ;
s.erase( s.find( coll ) ) ;
}
void FolderSet::Update( Resource *coll, const Entry& e )
void ResourceTree::Update( Resource *coll, const Entry& e )
{
coll->Update( e ) ;
ReInsert( coll ) ;
}
FolderSet::iterator FolderSet::begin()
ResourceTree::iterator ResourceTree::begin()
{
return m_set.get<ByIdentity>().begin() ;
}
FolderSet::iterator FolderSet::end()
ResourceTree::iterator ResourceTree::end()
{
return m_set.get<ByIdentity>().end() ;
}

View File

@ -56,18 +56,18 @@ namespace details
This class stores a set of folders and provide fast search access from ID, HREF etc.
It is a wrapper around multi_index_container from Boost library.
*/
class FolderSet
class ResourceTree
{
public :
typedef details::Set::iterator iterator ;
public :
FolderSet( ) ;
FolderSet( const FolderSet& fs ) ;
~FolderSet( ) ;
ResourceTree( ) ;
ResourceTree( const ResourceTree& fs ) ;
~ResourceTree( ) ;
void Swap( FolderSet& fs ) ;
FolderSet& operator=( const FolderSet& fs ) ;
void Swap( ResourceTree& fs ) ;
ResourceTree& operator=( const ResourceTree& fs ) ;
Resource* FindByHref( const std::string& href ) ;
const Resource* FindByHref( const std::string& href ) const ;

View File

@ -64,9 +64,10 @@ void State::Sync( const fs::path& p )
Sync( p, m_folders.Root() ) ;
}
void State::Sync( const boost::filesystem3::path& p, gr::Resource* folder )
void State::Sync( const fs::path& p, gr::Resource* folder )
{
assert( folder != 0 ) ;
assert( folder->IsFolder() ) ;
for ( fs::directory_iterator i( p ) ; i != fs::directory_iterator() ; ++i )
{
@ -78,8 +79,13 @@ void State::Sync( const boost::filesystem3::path& p, gr::Resource* folder )
Sync( *i, c ) ;
}
// else if ( i->path().filename().string()[0] != '.' )
// m_impl->rs.insert( Resource( i->path() ) ) ;
else if ( i->path().filename().string()[0] != '.' )
{
Trace( "file: %1% %2%", i->path().filename().string(), folder->Path() ) ;
Resource *c = new Resource( i->path().filename().string(), "file", "" ) ;
folder->AddChild( c ) ;
m_folders.Insert( c ) ;
}
}
}
@ -135,9 +141,25 @@ std::size_t State::TryResolveEntry()
bool State::Update( const Entry& e )
{
if ( e.Kind() != "folder" && e.Filename().empty() )
{
Log( "file \"%1%\" is a google document, ignored", e.Title() ) ;
return true ;
}
if ( e.ParentHref().empty() )
{
Log( "\"%1%\" has no parent, ignored", e.Title() ) ;
return true ;
}
Resource *parent = m_folders.FindByHref( e.ParentHref() ) ;
if ( parent != 0 )
{
if ( !parent->IsFolder() )
Trace( "name = \"%1%\" \"%2%\"", e.Title(), parent->Name() ) ;
assert( parent->IsFolder() ) ;
// see if the entry already exist in local
Resource *child = parent->FindChild( e.Title() ) ;
if ( child != 0 )
@ -154,10 +176,13 @@ bool State::Update( const Entry& e )
parent->AddChild( child ) ;
m_folders.Insert( child ) ;
if ( child->IsFolder() )
fs::path child_path = child->Path() ;
Trace( "added %1%", child_path ) ;
if ( child->IsFolder() && !fs::is_directory( child_path ) )
{
Log( "creating %1% directory", child->Path(), log::info ) ;
fs::create_directories( child->Path() ) ;
Log( "creating %1% directory", child_path, log::info ) ;
fs::create_directories( child_path ) ;
}
}
return true ;

View File

@ -19,7 +19,7 @@
#pragma once
#include "FolderSet.hh"
#include "ResourceTree.hh"
#include "util/FileSystem.hh"
#include <memory>
@ -33,7 +33,7 @@ class Entry ;
class State
{
public :
typedef FolderSet::iterator iterator ;
typedef ResourceTree::iterator iterator ;
public :
explicit State( const fs::path& filename ) ;
@ -63,7 +63,7 @@ private :
std::size_t TryResolveEntry() ;
private :
FolderSet m_folders ;
ResourceTree m_folders ;
std::string m_change_stamp ;
std::vector<Entry> m_unresolved ;