2012-04-25 20:13:17 +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
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
2012-04-26 08:21:40 +04:00
|
|
|
of the License.
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Drive.hh"
|
|
|
|
|
2012-05-06 18:27:52 +04:00
|
|
|
#include "CommonUri.hh"
|
2012-05-09 19:09:19 +04:00
|
|
|
#include "Entry.hh"
|
2012-05-13 21:07:23 +04:00
|
|
|
#include "File.hh"
|
2012-05-01 19:24:40 +04:00
|
|
|
|
2012-05-09 20:22:27 +04:00
|
|
|
#include "http/Agent.hh"
|
2012-05-09 21:20:38 +04:00
|
|
|
#include "http/ResponseLog.hh"
|
2012-05-06 13:13:48 +04:00
|
|
|
#include "http/XmlResponse.hh"
|
2012-05-13 21:41:27 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "protocol/OAuth2.hh"
|
2012-05-13 21:07:23 +04:00
|
|
|
#include "util/Destroy.hh"
|
2012-05-13 12:10:18 +04:00
|
|
|
#include "util/Log.hh"
|
2012-05-01 13:44:17 +04:00
|
|
|
#include "util/Path.hh"
|
2012-05-06 13:13:48 +04:00
|
|
|
#include "xml/Node.hh"
|
2012-05-12 13:38:41 +04:00
|
|
|
#include "xml/NodeSet.hh"
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 21:07:23 +04:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
// standard C++ library
|
2012-04-26 08:03:30 +04:00
|
|
|
#include <algorithm>
|
2012-04-26 20:55:10 +04:00
|
|
|
#include <cassert>
|
2012-05-13 21:41:27 +04:00
|
|
|
#include <cstdlib>
|
2012-04-26 08:03:30 +04:00
|
|
|
#include <fstream>
|
2012-04-26 20:55:10 +04:00
|
|
|
#include <map>
|
2012-04-29 06:22:58 +04:00
|
|
|
#include <sstream>
|
2012-04-26 08:03:30 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
// for debugging only
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
2012-05-15 20:54:58 +04:00
|
|
|
Drive::Drive( OAuth2& auth, const Json& state ) :
|
|
|
|
m_auth( auth ),
|
|
|
|
m_state( state )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
m_http_hdr.push_back( "Authorization: Bearer " + m_auth.AccessToken() ) ;
|
|
|
|
m_http_hdr.push_back( "GData-Version: 3.0" ) ;
|
2012-05-15 20:54:58 +04:00
|
|
|
|
|
|
|
std::string prev_change_stamp ;
|
|
|
|
if ( m_state.Has( "change_stamp" ) )
|
|
|
|
prev_change_stamp = m_state["change_stamp"].Str() ;
|
|
|
|
|
2012-05-06 14:26:45 +04:00
|
|
|
http::Agent http ;
|
2012-05-12 13:38:41 +04:00
|
|
|
http::XmlResponse xrsp ;
|
2012-05-15 20:54:58 +04:00
|
|
|
http.Get( feed_metadata, &xrsp, m_http_hdr ) ;
|
2012-04-29 20:03:04 +04:00
|
|
|
|
2012-05-15 20:54:58 +04:00
|
|
|
std::string change_stamp = xrsp.Response()["docs:largestChangestamp"]["@value"] ;
|
|
|
|
Trace( "change stamp is %1%", change_stamp ) ;
|
|
|
|
|
|
|
|
m_state.Add( "change_stamp", Json( change_stamp ) ) ;
|
2012-05-13 21:41:27 +04:00
|
|
|
|
2012-05-15 20:54:58 +04:00
|
|
|
ConstructDirTree( &http ) ;
|
|
|
|
|
|
|
|
std::string uri = feed_base + "?showfolders=true&showroot=true" ;
|
|
|
|
/* if ( !change_stamp.empty() )
|
2012-05-13 21:41:27 +04:00
|
|
|
{
|
|
|
|
int ichangestamp = std::atoi( change_stamp.c_str() ) + 1 ;
|
|
|
|
uri = (boost::format( "%1%&start-index=%2%" ) % uri % ichangestamp ).str() ;
|
|
|
|
}
|
2012-05-15 20:54:58 +04:00
|
|
|
*/
|
|
|
|
http.Get( uri, &xrsp, m_http_hdr ) ;
|
2012-05-12 13:38:41 +04:00
|
|
|
xml::Node resp = xrsp.Response() ;
|
2012-05-06 14:14:36 +04:00
|
|
|
|
2012-05-12 13:38:41 +04:00
|
|
|
m_resume_link = resp["link"].
|
|
|
|
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
|
2012-05-13 21:41:27 +04:00
|
|
|
|
2012-05-15 20:54:58 +04:00
|
|
|
// change_stamp = resp["docs:largestChangestamp"]["@value"] ;
|
2012-05-13 21:41:27 +04:00
|
|
|
|
2012-05-15 20:54:58 +04:00
|
|
|
// std::ofstream osfile( ".grive_state" ) ;
|
|
|
|
// Json state ;
|
|
|
|
// state.Add( "change_stamp", Json( change_stamp ) ) ;
|
|
|
|
// osfile << state ;
|
|
|
|
// osfile.close() ;
|
2012-05-13 21:41:27 +04:00
|
|
|
|
2012-05-06 14:14:36 +04:00
|
|
|
bool has_next = false ;
|
|
|
|
do
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-05-12 13:38:41 +04:00
|
|
|
xml::NodeSet entries = resp["entry"] ;
|
|
|
|
for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.end() ; ++i )
|
2012-04-26 20:55:10 +04:00
|
|
|
{
|
2012-05-13 21:41:27 +04:00
|
|
|
if ( (*i)["content"] == "" )
|
|
|
|
continue ;
|
|
|
|
|
2012-05-12 21:57:15 +04:00
|
|
|
Entry file( *i ) ;
|
|
|
|
if ( file.Kind() != "folder" )
|
2012-05-06 14:14:36 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
FolderListIterator pit = FindFolder( file.ParentHref() ) ;
|
|
|
|
if ( pit != m_coll.end() && pit->IsInRootTree() )
|
|
|
|
UpdateFile( file, *pit, &http ) ;
|
|
|
|
else
|
2012-05-13 13:27:40 +04:00
|
|
|
Log( "file \"%1%\" parent doesn't exist, ignored", file.Title() ) ;
|
2012-05-06 14:14:36 +04:00
|
|
|
}
|
2012-04-26 20:55:10 +04:00
|
|
|
}
|
2012-05-06 14:14:36 +04:00
|
|
|
|
2012-05-12 13:38:41 +04:00
|
|
|
xml::NodeSet nss = resp["link"].Find( "@rel", "next" ) ;
|
|
|
|
has_next = !nss.empty() ;
|
2012-05-09 18:25:42 +04:00
|
|
|
|
2012-05-06 14:14:36 +04:00
|
|
|
if ( has_next )
|
2012-05-06 14:26:45 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
http::ResponseLog log2( "second-", ".xml", &xrsp ) ;
|
|
|
|
http.Get( nss["@href"], &log2, m_http_hdr ) ;
|
2012-05-12 13:38:41 +04:00
|
|
|
resp = xrsp.Response() ;
|
2012-05-06 14:26:45 +04:00
|
|
|
}
|
2012-05-06 14:14:36 +04:00
|
|
|
} while ( has_next ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Drive::~Drive( )
|
|
|
|
{
|
2012-05-13 21:07:23 +04:00
|
|
|
std::for_each( m_files.begin(), m_files.end(), Destroy() ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SortCollectionByHref
|
|
|
|
{
|
|
|
|
bool operator()( const Collection& c1, const Collection& c2 ) const
|
|
|
|
{
|
2012-05-12 14:48:55 +04:00
|
|
|
return c1.SelfHref() < c2.SelfHref() ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
Drive::FolderListIterator Drive::FindFolder( const std::string& href )
|
|
|
|
{
|
|
|
|
// try to find the parent by its href
|
2012-05-12 21:57:15 +04:00
|
|
|
std::pair<FolderListIterator,FolderListIterator> its =
|
|
|
|
std::equal_range(
|
2012-04-26 19:40:38 +04:00
|
|
|
m_coll.begin(),
|
|
|
|
m_coll.end(),
|
2012-04-26 20:55:10 +04:00
|
|
|
Collection( "", href ),
|
2012-04-26 19:40:38 +04:00
|
|
|
SortCollectionByHref() ) ;
|
|
|
|
|
2012-05-12 21:57:15 +04:00
|
|
|
return (its.first != its.second) ? its.first : m_coll.end() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
Drive::FolderListIterator Drive::Root( )
|
|
|
|
{
|
|
|
|
FolderListIterator root = FindFolder( root_href ) ;
|
|
|
|
assert( root != m_coll.end() ) ;
|
|
|
|
return root ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-06 14:26:45 +04:00
|
|
|
void Drive::ConstructDirTree( http::Agent *http )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-05-09 18:25:42 +04:00
|
|
|
http::XmlResponse xml ;
|
2012-05-10 21:27:34 +04:00
|
|
|
http::ResponseLog log( "dir-", ".xml", &xml ) ;
|
2012-05-09 21:20:38 +04:00
|
|
|
|
2012-05-12 14:48:55 +04:00
|
|
|
http->Get( feed_base + "/-/folder?max-results=10&showroot=true", &log, m_http_hdr ) ;
|
2012-05-12 13:38:41 +04:00
|
|
|
|
|
|
|
xml::Node resp = xml.Response() ;
|
2012-05-09 18:25:42 +04:00
|
|
|
|
2012-04-26 20:55:10 +04:00
|
|
|
assert( m_coll.empty() ) ;
|
2012-05-12 21:57:15 +04:00
|
|
|
m_coll.push_back( Collection( ".", root_href ) ) ;
|
2012-05-06 14:14:36 +04:00
|
|
|
|
|
|
|
while ( true )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
2012-05-12 13:38:41 +04:00
|
|
|
xml::NodeSet entries = resp["entry"] ;
|
2012-05-06 14:14:36 +04:00
|
|
|
|
|
|
|
// first, get all collections from the query result
|
2012-05-12 13:38:41 +04:00
|
|
|
for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.end() ; ++i )
|
2012-04-26 20:55:10 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
Entry e( *i ) ;
|
|
|
|
if ( e.Kind() == "folder" )
|
|
|
|
{
|
|
|
|
if ( e.ParentHrefs().size() == 1 )
|
|
|
|
m_coll.push_back( Collection( e ) ) ;
|
|
|
|
else
|
2012-05-13 13:27:40 +04:00
|
|
|
Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::warning ) ;
|
2012-05-12 21:57:15 +04:00
|
|
|
}
|
2012-04-26 20:55:10 +04:00
|
|
|
}
|
2012-05-06 14:14:36 +04:00
|
|
|
|
2012-05-12 13:38:41 +04:00
|
|
|
xml::NodeSet next = resp["link"].Find( "@rel", "next" ) ;
|
|
|
|
if ( next.empty() )
|
2012-05-06 14:14:36 +04:00
|
|
|
break ;
|
|
|
|
|
2012-05-12 13:38:41 +04:00
|
|
|
http->Get( next["@href"], &xml, m_http_hdr ) ;
|
|
|
|
resp = xml.Response() ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
2012-05-09 18:25:42 +04:00
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
// second, build up linkage between parent and child
|
|
|
|
std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ;
|
|
|
|
for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
FolderListIterator pit = FindFolder( i->ParentHref() ) ;
|
|
|
|
if ( pit != m_coll.end() )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
// it shouldn't happen, just in case
|
|
|
|
if ( &*i == &*pit )
|
2012-05-13 12:45:27 +04:00
|
|
|
Log( "the parent of folder %1% is itself, ignored.", i->Title(), log::warning ) ;
|
2012-05-12 14:48:55 +04:00
|
|
|
else
|
2012-05-12 21:57:15 +04:00
|
|
|
pit->AddChild( &*i ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
2012-05-12 21:57:15 +04:00
|
|
|
else
|
2012-05-13 13:27:40 +04:00
|
|
|
Log( "can't find folder \"%1%\" (\"%2%\")", i->Title(), i->ParentHref(), log::warning ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
2012-05-09 18:25:42 +04:00
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
// lastly, iterating from the root, create the directories in the local file system
|
2012-05-12 21:57:15 +04:00
|
|
|
assert( Root()->Parent() == 0 ) ;
|
|
|
|
Root()->CreateSubDir( Path() ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
2012-05-13 21:07:23 +04:00
|
|
|
void Drive::UpdateFile( Entry& entry, Collection& parent, http::Agent *http )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
// only handle uploaded files
|
2012-05-13 21:07:23 +04:00
|
|
|
if ( !entry.Filename().empty() )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
2012-05-13 21:07:23 +04:00
|
|
|
File *file = new File( entry, &parent ) ;
|
|
|
|
m_files.push_back( file ) ;
|
|
|
|
parent.AddLeaf( file ) ;
|
2012-05-09 20:22:27 +04:00
|
|
|
|
2012-05-13 21:07:23 +04:00
|
|
|
// file->Update( http, m_http_hdr ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
2012-05-12 21:57:15 +04:00
|
|
|
else
|
|
|
|
{
|
2012-05-13 21:07:23 +04:00
|
|
|
Log( "file \"%1%\" is a google document, ignored", entry.Title() ) ;
|
2012-05-12 21:57:15 +04:00
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
|
|
|
|
2012-05-13 21:07:23 +04:00
|
|
|
void Drive::Update()
|
|
|
|
{
|
|
|
|
http::Agent http ;
|
|
|
|
std::for_each( m_files.begin(), m_files.end(),
|
|
|
|
boost::bind( &File::Update, _1, &http, m_http_hdr ) ) ;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
} // end of namespace
|