grive2/libgrive/src/drive/Drive.cc

175 lines
4.4 KiB
C++
Raw Normal View History

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
as published by the Free Software Foundation version 2
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"
#include "Entry.hh"
2012-05-09 20:22:27 +04:00
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
2012-05-06 13:13:48 +04:00
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#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-06 13:13:48 +04:00
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
2012-04-25 20:13:17 +04:00
2012-05-13 21:07:23 +04:00
#include <boost/bind.hpp>
// standard C++ library
#include <algorithm>
2012-04-26 20:55:10 +04:00
#include <cassert>
#include <cstdlib>
#include <fstream>
2012-04-26 20:55:10 +04:00
#include <map>
2012-04-29 06:22:58 +04:00
#include <sstream>
2012-04-25 20:13:17 +04:00
// for debugging only
#include <iostream>
namespace gr {
2012-05-16 20:52:17 +04:00
namespace
{
const std::string state_file = ".grive_state" ;
}
Drive::Drive( OAuth2& auth ) :
m_auth( auth ),
2012-05-16 20:52:17 +04:00
m_state( state_file )
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" ) ;
http::Agent http ;
http::XmlResponse xrsp ;
http.Get( feed_metadata, &xrsp, m_http_hdr ) ;
std::string change_stamp = xrsp.Response()["docs:largestChangestamp"]["@value"] ;
Trace( "change stamp is %1%", change_stamp ) ;
2012-05-16 20:52:17 +04:00
m_state.ChangeStamp( change_stamp ) ;
2012-05-19 21:44:46 +04:00
m_state.FromLocal( "." ) ;
ConstructDirTree( &http ) ;
std::string uri = feed_base + "?showfolders=true&showroot=true" ;
/* if ( !change_stamp.empty() )
{
int ichangestamp = std::atoi( change_stamp.c_str() ) + 1 ;
uri = (boost::format( "%1%&start-index=%2%" ) % uri % ichangestamp ).str() ;
}
*/
http.Get( uri, &xrsp, m_http_hdr ) ;
xml::Node resp = xrsp.Response() ;
2012-05-06 14:14:36 +04:00
m_resume_link = resp["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
2012-05-17 20:37:11 +04:00
2012-05-06 14:14:36 +04:00
bool has_next = false ;
do
{
xml::NodeSet entries = resp["entry"] ;
for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.end() ; ++i )
2012-04-26 20:55:10 +04:00
{
if ( (*i)["content"] == "" )
continue ;
Entry file( *i ) ;
if ( file.Kind() != "folder" )
2012-05-06 14:14:36 +04:00
{
Resource *p = m_state.FindFolderByHref( file.ParentHref() ) ;
2012-05-19 14:27:53 +04:00
if ( file.Filename().empty() )
Log( "file \"%1%\" is a google document, ignored", file.Title() ) ;
else if ( file.ParentHref().empty() || p == 0 || !p->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", file.Title() ) ;
2012-05-19 14:27:53 +04:00
else if ( p != 0 && !p->IsFolder() )
Log( "entry %1% has parent %2% which is not a folder, ignored",
file.Title(), p->Name() ) ;
else
2012-05-19 21:44:46 +04:00
m_state.FromRemote( file ) ;
2012-05-06 14:14:36 +04:00
}
2012-04-26 20:55:10 +04:00
}
2012-05-06 14:14:36 +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 )
{
http::ResponseLog log2( "second-", ".xml", &xrsp ) ;
http.Get( nss["@href"], &log2, m_http_hdr ) ;
resp = xrsp.Response() ;
}
2012-05-06 14:14:36 +04:00
} while ( has_next ) ;
2012-05-16 20:52:17 +04:00
m_state.Write( state_file ) ;
}
void Drive::ConstructDirTree( http::Agent *http )
{
2012-05-09 18:25:42 +04:00
http::XmlResponse xml ;
http::ResponseLog log( "dir-", ".xml", &xml ) ;
http->Get( feed_base + "/-/folder?max-results=10&showroot=true", &log, m_http_hdr ) ;
xml::Node resp = xml.Response() ;
2012-05-09 18:25:42 +04:00
2012-05-06 14:14:36 +04:00
while ( true )
2012-04-25 20:13:17 +04:00
{
xml::NodeSet entries = resp["entry"] ;
2012-05-06 14:14:36 +04:00
// first, get all collections from the query result
for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.end() ; ++i )
2012-04-26 20:55:10 +04:00
{
Entry e( *i ) ;
if ( e.Kind() == "folder" )
{
if ( e.ParentHrefs().size() == 1 )
2012-05-19 21:44:46 +04:00
m_state.FromRemote( e ) ;
else
Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::warning ) ;
}
2012-04-26 20:55:10 +04:00
}
2012-05-06 14:14:36 +04:00
xml::NodeSet next = resp["link"].Find( "@rel", "next" ) ;
if ( next.empty() )
2012-05-06 14:14:36 +04:00
break ;
http->Get( next["@href"], &xml, m_http_hdr ) ;
resp = xml.Response() ;
}
2012-05-09 18:25:42 +04:00
2012-05-17 20:37:11 +04:00
m_state.ResolveEntry() ;
}
2012-05-19 13:14:04 +04:00
2012-05-13 21:07:23 +04:00
void Drive::Update()
{
http::Agent http ;
std::for_each( m_state.begin(), m_state.end(),
2012-05-20 10:57:25 +04:00
boost::bind( &Resource::Sync, _1, &http, m_http_hdr ) ) ;
2012-05-13 21:07:23 +04:00
}
2012-04-25 20:13:17 +04:00
} // end of namespace