2012-04-26 19:40:38 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2012-04-28 14:33:08 +04:00
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-05-19 12:18:33 +04:00
|
|
|
#include <iosfwd>
|
2012-04-26 19:40:38 +04:00
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
class Json ;
|
|
|
|
|
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-04-26 19:40:38 +04:00
|
|
|
{
|
2012-05-19 13:14:04 +04:00
|
|
|
public :
|
|
|
|
struct Error : virtual Exception {} ;
|
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
typedef std::vector<Resource*> Children ;
|
|
|
|
typedef Children::const_iterator iterator ;
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
public :
|
2012-05-20 10:57:25 +04:00
|
|
|
Resource() ;
|
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-20 10:57:25 +04:00
|
|
|
explicit Resource( const fs::path& path ) ;
|
2012-05-20 13:12:01 +04:00
|
|
|
explicit Resource( const Json& json, Resource *parent = 0 ) ;
|
2012-05-19 13:14:04 +04:00
|
|
|
void Swap( Resource& coll ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
|
|
|
|
// 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 ;
|
2012-05-12 14:48:55 +04:00
|
|
|
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 ;
|
2012-05-12 21:57:15 +04:00
|
|
|
bool IsInRootTree() const ;
|
2012-05-27 20:24:37 +04:00
|
|
|
bool IsRoot() const ;
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
void FromRemote( const Entry& e ) ;
|
2012-05-20 15:28:44 +04:00
|
|
|
void FromLocal() ;
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
void Sync( http::Agent *http, const http::Headers& auth ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
void Delete( http::Agent* http, const http::Headers& auth ) ;
|
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
Json Serialize() const ;
|
|
|
|
|
|
|
|
// children access
|
|
|
|
iterator begin() const ;
|
|
|
|
iterator end() const ;
|
|
|
|
std::size_t size() const ;
|
|
|
|
|
2012-05-24 13:47:26 +04:00
|
|
|
std::string StateStr() const ;
|
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
private :
|
|
|
|
/// State of the resource. indicating what to do with the resource
|
|
|
|
enum State
|
|
|
|
{
|
2012-05-20 10:57:25 +04:00
|
|
|
/// The best state: the file is the same in remote and in local.
|
2012-05-19 21:44:46 +04:00
|
|
|
sync,
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
/// Resource created in local, but remote does not have it.
|
2012-05-19 21:44:46 +04:00
|
|
|
/// We should create the resource in google drive and upload new content
|
2012-05-20 10:57:25 +04:00
|
|
|
local_new,
|
|
|
|
|
|
|
|
/// Resource exists in both local & remote, but changes in local is newer
|
|
|
|
/// than remote. We should upload local copy to overwrite remote.
|
|
|
|
local_changed,
|
2012-05-19 21:44:46 +04:00
|
|
|
|
2012-05-20 15:28:44 +04:00
|
|
|
/// Resource deleted from local since last time grive has checked.
|
|
|
|
local_deleted,
|
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
/// Resource created in google drive, but not exist in local.
|
|
|
|
/// We should download the file.
|
2012-05-20 10:57:25 +04:00
|
|
|
remote_new,
|
|
|
|
|
|
|
|
/// Resource exists in both local & remote, but remote is newer.
|
|
|
|
remote_changed
|
2012-05-19 21:44:46 +04:00
|
|
|
} ;
|
2012-05-20 19:16:59 +04:00
|
|
|
|
|
|
|
friend std::ostream& operator<<( std::ostream& os, State s ) ;
|
2012-05-19 21:44:46 +04:00
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
private :
|
|
|
|
void Download( http::Agent* http, const fs::path& file, const http::Headers& auth ) const ;
|
2012-05-20 21:18:16 +04:00
|
|
|
bool EditContent( http::Agent* http, const http::Headers& auth ) ;
|
|
|
|
bool Create( http::Agent* http, const http::Headers& auth ) ;
|
|
|
|
bool Upload( http::Agent* http, const std::string& link, const http::Headers& auth, bool post ) ;
|
2012-05-20 19:16:59 +04:00
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
private :
|
2012-05-19 13:14:04 +04:00
|
|
|
Entry m_entry ;
|
2012-04-26 19:40:38 +04:00
|
|
|
|
|
|
|
// 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 ;
|
2012-04-26 19:40:38 +04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
} // end of namespace
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2012-05-19 11:41:21 +04:00
|
|
|
void swap( gr::Resource& c1, gr::Resource& c2 ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|