2012-05-18 04:52:11 +04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
#include "ResourceTree.hh"
|
2012-05-19 11:26:55 +04:00
|
|
|
#include "CommonUri.hh"
|
2012-05-18 04:52:11 +04:00
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-05-18 04:52:11 +04:00
|
|
|
#include "util/Destroy.hh"
|
2012-05-20 13:12:01 +04:00
|
|
|
#include "util/Log.hh"
|
2012-05-18 04:52:11 +04:00
|
|
|
|
|
|
|
#include <algorithm>
|
2012-05-19 12:18:33 +04:00
|
|
|
#include <cassert>
|
2012-05-18 04:52:11 +04:00
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
|
|
|
using namespace details ;
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree::ResourceTree( ) :
|
2012-05-20 10:57:25 +04:00
|
|
|
m_root( new Resource )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
2012-05-19 11:26:55 +04:00
|
|
|
m_set.insert( m_root ) ;
|
2012-05-18 04:52:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree::ResourceTree( const ResourceTree& fs ) :
|
2012-05-19 12:18:33 +04:00
|
|
|
m_root( 0 )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
|
|
|
const Set& s = fs.m_set.get<ByIdentity>() ;
|
|
|
|
for ( Set::const_iterator i = s.begin() ; i != s.end() ; ++i )
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
2012-05-19 11:41:21 +04:00
|
|
|
Resource *c = new Resource( **i ) ;
|
2012-05-19 11:26:55 +04:00
|
|
|
if ( c->SelfHref() == root_href )
|
|
|
|
m_root = c ;
|
|
|
|
|
|
|
|
m_set.insert( c ) ;
|
|
|
|
}
|
2012-05-19 12:18:33 +04:00
|
|
|
|
|
|
|
assert( m_root != 0 ) ;
|
2012-05-18 04:52:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree::~ResourceTree( )
|
2012-05-20 13:12:01 +04:00
|
|
|
{
|
|
|
|
Clear() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceTree::Clear()
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
|
|
|
// delete all pointers
|
|
|
|
const Set& s = m_set.get<ByIdentity>() ;
|
|
|
|
std::for_each( s.begin(), s.end(), Destroy() ) ;
|
2012-05-20 13:12:01 +04:00
|
|
|
|
|
|
|
m_set.clear() ;
|
|
|
|
m_root = 0 ;
|
2012-05-18 04:52:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
Resource* ResourceTree::Root()
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
2012-05-19 12:18:33 +04:00
|
|
|
assert( m_root != 0 ) ;
|
2012-05-19 11:26:55 +04:00
|
|
|
return m_root ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
const Resource* ResourceTree::Root() const
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
2012-05-19 12:18:33 +04:00
|
|
|
assert( m_root != 0 ) ;
|
2012-05-19 11:26:55 +04:00
|
|
|
return m_root ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
void ResourceTree::Swap( ResourceTree& fs )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
|
|
|
m_set.swap( fs.m_set ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree& ResourceTree::operator=( const ResourceTree& fs )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree tmp( fs ) ;
|
2012-05-18 04:52:11 +04:00
|
|
|
Swap( tmp ) ;
|
|
|
|
return *this ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
Resource* ResourceTree::FindByHref( const std::string& href )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
2012-05-19 14:03:02 +04:00
|
|
|
if ( href.empty() )
|
|
|
|
return 0 ;
|
|
|
|
|
2012-05-18 04:52:11 +04:00
|
|
|
HrefMap& map = m_set.get<ByHref>() ;
|
|
|
|
HrefMap::iterator i = map.find( href ) ;
|
|
|
|
return i != map.end() ? *i : 0 ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
const Resource* ResourceTree::FindByHref( const std::string& href ) const
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
|
|
|
const HrefMap& map = m_set.get<ByHref>() ;
|
|
|
|
HrefMap::const_iterator i = map.find( href ) ;
|
|
|
|
return i != map.end() ? *i : 0 ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
/// Unlike other search functions, this one does not depend on the multi-index
|
|
|
|
/// container. It traverses the tree instead.
|
|
|
|
Resource* ResourceTree::FindByPath( const fs::path& path )
|
|
|
|
{
|
|
|
|
// not yet implemented
|
|
|
|
assert( false ) ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2012-05-18 04:52:11 +04:00
|
|
|
/// Reinsert should be called when the ID/HREF were updated
|
2012-05-19 14:03:02 +04:00
|
|
|
bool ResourceTree::ReInsert( Resource *coll )
|
2012-05-18 04:52:11 +04:00
|
|
|
{
|
|
|
|
Set& s = m_set.get<ByIdentity>() ;
|
|
|
|
Set::iterator i = s.find( coll ) ;
|
|
|
|
if ( i != s.end() )
|
|
|
|
{
|
|
|
|
s.erase( i ) ;
|
|
|
|
m_set.insert( coll ) ;
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
void ResourceTree::Insert( Resource *coll )
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
|
|
|
m_set.insert( coll ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
void ResourceTree::Erase( Resource *coll )
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
|
|
|
Set& s = m_set.get<ByIdentity>() ;
|
|
|
|
s.erase( s.find( coll ) ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
void ResourceTree::Update( Resource *coll, const Entry& e )
|
2012-05-19 11:26:55 +04:00
|
|
|
{
|
2012-05-19 21:44:46 +04:00
|
|
|
assert( coll != 0 ) ;
|
|
|
|
|
|
|
|
coll->FromRemote( e ) ;
|
2012-05-19 11:26:55 +04:00
|
|
|
ReInsert( coll ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree::iterator ResourceTree::begin()
|
2012-05-19 13:14:04 +04:00
|
|
|
{
|
|
|
|
return m_set.get<ByIdentity>().begin() ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:03:02 +04:00
|
|
|
ResourceTree::iterator ResourceTree::end()
|
2012-05-19 13:14:04 +04:00
|
|
|
{
|
|
|
|
return m_set.get<ByIdentity>().end() ;
|
|
|
|
}
|
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
void ResourceTree::Read( const Json& json )
|
|
|
|
{
|
|
|
|
Clear() ;
|
|
|
|
m_root = new Resource( json ) ;
|
|
|
|
AddTree( m_root, json ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceTree::AddTree( Resource *node, const Json& json )
|
|
|
|
{
|
|
|
|
assert( node != 0 ) ;
|
|
|
|
m_set.insert( node ) ;
|
|
|
|
|
|
|
|
std::vector<Json> array = json["child"].AsArray() ;
|
|
|
|
for ( std::vector<Json>::iterator i = array.begin() ; i != array.end() ; ++i )
|
|
|
|
{
|
|
|
|
Resource *c = new Resource( *i, node ) ;
|
|
|
|
node->AddChild( c ) ;
|
|
|
|
AddTree( c, *i ) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Json ResourceTree::Serialize() const
|
|
|
|
{
|
|
|
|
return m_root->Serialize() ;
|
|
|
|
}
|
|
|
|
|
2012-05-18 04:52:11 +04:00
|
|
|
} // end of namespace
|