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-06-10 12:18:21 +04:00
|
|
|
#include "util/DateTime.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 {
|
|
|
|
|
2015-05-17 02:00:00 +03:00
|
|
|
class Syncer ;
|
2012-06-03 12:42:51 +04:00
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
class Val ;
|
2012-05-20 13:12:01 +04:00
|
|
|
|
2013-04-29 09:23:34 +04:00
|
|
|
class Entry ;
|
|
|
|
|
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 :
|
2012-05-20 13:12:01 +04:00
|
|
|
typedef std::vector<Resource*> Children ;
|
|
|
|
typedef Children::const_iterator iterator ;
|
|
|
|
|
2015-05-17 02:00:00 +03:00
|
|
|
/// State of the resource. indicating what to do with the resource
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
/// The best state: the file is the same in remote and in local.
|
|
|
|
sync,
|
|
|
|
|
|
|
|
/// Resource created in local, but remote does not have it.
|
|
|
|
/// We should create the resource in google drive and upload new content
|
|
|
|
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,
|
|
|
|
|
|
|
|
/// Resource deleted from local since last time grive has checked.
|
|
|
|
local_deleted,
|
|
|
|
|
|
|
|
/// Resource created in google drive, but not exist in local.
|
|
|
|
/// We should download the file.
|
|
|
|
remote_new,
|
|
|
|
|
|
|
|
/// Resource exists in both local & remote, but remote is newer.
|
|
|
|
remote_changed,
|
|
|
|
|
|
|
|
/// Resource delete in remote, need to delete in local
|
|
|
|
remote_deleted,
|
|
|
|
|
|
|
|
|
|
|
|
/// invalid value
|
|
|
|
unknown
|
|
|
|
} ;
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
public :
|
2012-07-30 12:52:59 +04:00
|
|
|
Resource(const fs::path& root_folder) ;
|
2012-05-31 19:37:47 +04:00
|
|
|
Resource( const std::string& name, const std::string& kind ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
bool IsFolder() const ;
|
2015-05-17 02:00:00 +03:00
|
|
|
bool IsEditable() const ;
|
2012-05-19 21:44:46 +04:00
|
|
|
|
2012-05-19 13:14:04 +04:00
|
|
|
std::string Name() const ;
|
2015-05-17 02:00:00 +03:00
|
|
|
std::string Kind() const ;
|
|
|
|
DateTime MTime() const ;
|
2012-05-12 14:48:55 +04:00
|
|
|
std::string SelfHref() const ;
|
2015-05-17 02:00:00 +03:00
|
|
|
std::string ContentSrc() const ;
|
|
|
|
std::string ETag() const ;
|
2012-05-19 12:18:33 +04:00
|
|
|
std::string ResourceID() const ;
|
2015-05-17 02:00:00 +03:00
|
|
|
State GetState() 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 ;
|
2015-09-25 13:01:26 +03:00
|
|
|
fs::path RelPath() const ;
|
2012-05-12 21:57:15 +04:00
|
|
|
bool IsInRootTree() const ;
|
2012-05-27 20:24:37 +04:00
|
|
|
bool IsRoot() const ;
|
2012-06-03 11:55:32 +04:00
|
|
|
bool HasID() const ;
|
2012-06-10 19:11:32 +04:00
|
|
|
std::string MD5() const ;
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2015-10-08 13:30:50 +03:00
|
|
|
void FromRemote( const Entry& remote, const DateTime& last_change ) ;
|
2012-05-29 21:30:06 +04:00
|
|
|
void FromLocal( const DateTime& last_sync ) ;
|
2012-05-20 15:28:44 +04:00
|
|
|
|
2015-05-17 02:00:00 +03:00
|
|
|
void Sync( Syncer* syncer, DateTime& sync_time, const Val& options ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
// 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 ;
|
2015-05-17 02:00:00 +03:00
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
private :
|
2015-05-17 02:00:00 +03:00
|
|
|
|
|
|
|
void AssignIDs( const Entry& remote ) ;
|
2012-05-20 19:16:59 +04:00
|
|
|
|
|
|
|
friend std::ostream& operator<<( std::ostream& os, State s ) ;
|
2015-05-17 02:00:00 +03:00
|
|
|
friend class Syncer ;
|
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
private :
|
2012-06-18 19:44:28 +04:00
|
|
|
void SetState( State new_state ) ;
|
2012-06-03 11:55:32 +04:00
|
|
|
|
2015-10-08 13:30:50 +03:00
|
|
|
void FromRemoteFolder( const Entry& remote, const DateTime& last_change ) ;
|
|
|
|
void FromRemoteFile( const Entry& remote, const DateTime& last_change ) ;
|
2012-06-03 11:55:32 +04:00
|
|
|
|
2012-05-31 20:40:07 +04:00
|
|
|
void DeleteLocal() ;
|
2012-06-10 12:18:21 +04:00
|
|
|
|
2015-05-17 02:00:00 +03:00
|
|
|
void SyncSelf( Syncer* syncer, const Val& options ) ;
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
private :
|
2012-06-10 12:18:21 +04:00
|
|
|
std::string m_name ;
|
|
|
|
std::string m_kind ;
|
|
|
|
std::string m_md5 ;
|
|
|
|
DateTime m_mtime ;
|
2015-05-17 02:00:00 +03:00
|
|
|
|
2012-06-10 12:18:21 +04:00
|
|
|
std::string m_id ;
|
|
|
|
std::string m_href ;
|
|
|
|
std::string m_content ;
|
|
|
|
std::string m_etag ;
|
2015-05-17 02:00:00 +03:00
|
|
|
bool m_is_editable ;
|
2012-06-10 12:18:21 +04:00
|
|
|
|
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 ;
|
2015-05-17 02:00:00 +03:00
|
|
|
|
2012-05-19 21:44:46 +04:00
|
|
|
State m_state ;
|
2016-01-01 18:15:34 +03:00
|
|
|
Val* m_json ;
|
2012-04-26 19:40:38 +04:00
|
|
|
} ;
|
|
|
|
|
2015-05-17 02:00:00 +03:00
|
|
|
} // end of namespace gr::v1
|