grive2/libgrive/src/drive/Resource.hh

109 lines
2.9 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.
*/
#pragma once
2012-05-09 19:52:06 +04:00
#include "Entry.hh"
2012-05-19 12:18:33 +04:00
#include "http/Agent.hh"
2012-05-13 11:27:58 +04:00
#include "util/Exception.hh"
2012-05-16 21:20:02 +04:00
#include "util/FileSystem.hh"
#include <string>
#include <vector>
2012-05-19 12:18:33 +04:00
#include <iosfwd>
namespace gr {
2012-05-19 21:44:46 +04:00
/*! \brief A resource can be a file or a folder in the google drive
The google drive contains a number of resources, which is represented by this class.
It also contains linkage to other resources, such as parent and childrens.
*/
2012-05-19 11:41:21 +04:00
class Resource
{
2012-05-19 13:14:04 +04:00
public :
struct Error : virtual Exception {} ;
public :
2012-05-19 11:41:21 +04:00
explicit Resource( const xml::Node& entry ) ;
2012-05-19 12:18:33 +04:00
explicit Resource( const Entry& entry, Resource *parent = 0 ) ;
2012-05-19 13:14:04 +04:00
Resource(
const std::string& name,
const std::string& kind,
const std::string& href ) ;
void Swap( Resource& coll ) ;
// default copy ctor & op= are fine
2012-05-19 21:44:46 +04:00
bool IsFolder() const ;
2012-05-19 13:14:04 +04:00
std::string Name() const ;
std::string SelfHref() const ;
2012-05-19 12:18:33 +04:00
std::string ResourceID() const ;
2012-05-19 21:44:46 +04:00
std::string ParentHref() const ;
2012-05-19 12:18:33 +04:00
2012-05-19 11:41:21 +04:00
const Resource* Parent() const ;
Resource* Parent() ;
2012-05-19 21:44:46 +04:00
void AddChild( Resource *child ) ;
Resource* FindChild( const std::string& title ) ;
2012-05-19 12:18:33 +04:00
fs::path Path() const ;
bool IsInRootTree() const ;
2012-05-19 21:44:46 +04:00
void FromRemote( const Entry& e ) ;
2012-05-19 12:18:33 +04:00
void Update( http::Agent *http, const http::Headers& auth ) ;
void Delete( http::Agent* http, const http::Headers& auth ) ;
2012-05-19 21:44:46 +04:00
private :
/// State of the resource. indicating what to do with the resource
enum State
{
/// The best state: the file is the same in google drive and in local.
sync,
/// Resource created in local, but google drive does not have it.
/// We should create the resource in google drive and upload new content
new_local,
/// Resource created in google drive, but not exist in local.
/// We should download the file.
new_remote
} ;
2012-05-19 12:18:33 +04:00
private :
void Download( http::Agent* http, const fs::path& file, const http::Headers& auth ) const ;
bool Upload( http::Agent* http, std::streambuf *file, const http::Headers& auth ) ;
private :
2012-05-19 13:14:04 +04:00
Entry m_entry ;
// not owned
2012-05-19 11:41:21 +04:00
Resource *m_parent ;
std::vector<Resource*> m_child ;
2012-05-19 21:44:46 +04:00
State m_state ;
} ;
} // end of namespace
namespace std
{
2012-05-19 11:41:21 +04:00
void swap( gr::Resource& c1, gr::Resource& c2 ) ;
}