grive2/libgrive/src/drive/Resource.cc

155 lines
3.0 KiB
C++
Raw Normal View History

/*
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 11:41:21 +04:00
#include "Resource.hh"
#include "CommonUri.hh"
#include "util/OS.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
2012-04-26 20:55:10 +04:00
#include <cassert>
// for debugging
#include <iostream>
namespace gr {
2012-05-19 11:41:21 +04:00
Resource::Resource( const xml::Node& entry ) :
m_entry ( entry ),
m_parent ( 0 )
{
}
2012-05-19 11:41:21 +04:00
Resource::Resource( const Entry& entry ) :
m_entry ( entry ),
m_parent ( 0 )
{
}
2012-05-19 11:41:21 +04:00
Resource::Resource(
const std::string& title,
2012-04-26 20:55:10 +04:00
const std::string& href ) :
2012-05-09 19:52:06 +04:00
m_entry ( title, href ),
2012-04-26 20:55:10 +04:00
m_parent( 0 )
{
}
2012-05-19 11:41:21 +04:00
void Resource::Update( const Entry& e )
2012-05-17 20:37:11 +04:00
{
m_entry = e ;
}
2012-05-19 11:41:21 +04:00
std::string Resource::SelfHref() const
{
2012-05-09 19:52:06 +04:00
return m_entry.SelfHref() ;
}
2012-05-19 11:41:21 +04:00
std::string Resource::Title() const
{
2012-05-09 19:52:06 +04:00
return m_entry.Title() ;
}
2012-05-19 11:41:21 +04:00
std::string Resource::ResourceID() const
2012-05-17 20:37:11 +04:00
{
return m_entry.ResourceID() ;
}
2012-05-19 11:41:21 +04:00
const Resource* Resource::Parent() const
2012-04-26 20:55:10 +04:00
{
return m_parent ;
}
2012-05-19 11:41:21 +04:00
Resource* Resource::Parent()
{
return m_parent ;
}
2012-05-19 11:41:21 +04:00
std::string Resource::ParentHref() const
2012-05-09 19:52:06 +04:00
{
return m_entry.ParentHref() ;
}
2012-05-19 11:41:21 +04:00
void Resource::AddChild( Resource *child )
{
2012-04-26 20:55:10 +04:00
assert( child != 0 ) ;
assert( child->m_parent == 0 ) ;
assert( child != this ) ;
child->m_parent = this ;
m_child.push_back( child ) ;
}
2012-05-19 11:41:21 +04:00
void Resource::AddLeaf( File *file )
{
2012-05-13 21:07:23 +04:00
m_leaf.push_back( file ) ;
}
2012-05-19 11:41:21 +04:00
void Resource::Swap( Resource& coll )
{
2012-05-09 19:52:06 +04:00
m_entry.Swap( coll.m_entry ) ;
2012-04-26 20:55:10 +04:00
std::swap( m_parent, coll.m_parent ) ;
m_child.swap( coll.m_child ) ;
2012-05-13 21:07:23 +04:00
m_leaf.swap( coll.m_leaf ) ;
}
2012-05-19 11:41:21 +04:00
void Resource::CreateSubDir( const fs::path& prefix )
{
2012-05-16 21:20:02 +04:00
fs::path dir = prefix / m_entry.Title() ;
fs::create_directories( dir ) ;
2012-05-19 11:41:21 +04:00
for ( std::vector<Resource*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
{
2012-04-26 20:55:10 +04:00
assert( (*i)->m_parent == this ) ;
2012-05-01 13:44:17 +04:00
(*i)->CreateSubDir( dir ) ;
}
}
2012-05-19 11:41:21 +04:00
fs::path Resource::Dir() const
2012-04-26 20:55:10 +04:00
{
assert( m_parent != this ) ;
2012-05-17 20:37:11 +04:00
return m_parent != 0 ? (m_parent->Dir() / m_entry.Title()) : "." ;
2012-04-26 20:55:10 +04:00
}
2012-05-19 11:41:21 +04:00
bool Resource::IsInRootTree() const
{
return m_parent == 0 ? (SelfHref() == root_href) : m_parent->IsInRootTree() ;
}
2012-05-19 11:41:21 +04:00
Resource* Resource::FindChild( const std::string& title )
2012-05-17 20:37:11 +04:00
{
2012-05-19 11:41:21 +04:00
for ( std::vector<Resource*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
2012-05-17 20:37:11 +04:00
{
assert( (*i)->m_parent == this ) ;
if ( (*i)->Title() == title )
return *i ;
}
return 0 ;
}
} // end of namespace
namespace std
{
2012-05-19 11:41:21 +04:00
void swap( gr::Resource& c1, gr::Resource& c2 )
{
c1.Swap( c2 ) ;
}
}