refactored old GDoc API into v1 namespace. using v2 namespace for new Google Drive API

pull/40/head
Nestal Wan 2013-04-29 13:23:34 +08:00
parent 738435837b
commit 62dc542341
28 changed files with 375 additions and 36 deletions

View File

@ -23,13 +23,11 @@
#include <QtGui/QApplication>
#include <QtCore/QDebug>
#include "drive/CommonUri.hh"
#include "drive/Entry.hh"
#include "drive/Feed.hh"
#include "drive2/Feed.hh"
#include "http/CurlAgent.hh"
#include "http/Header.hh"
//#include "http/XmlResponse.hh"
#include "protocol/JsonResponse.hh"
#include "protocol/Json.hh"
#include "protocol/OAuth2.hh"
@ -38,11 +36,13 @@
#include "util/File.hh"
#include <string>
#include <iostream>
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
using namespace gr ;
using namespace gr::v2 ;
int main( int argc, char **argv )
{
@ -55,7 +55,7 @@ int main( int argc, char **argv )
OAuth2 token( refresh_token, client_id, client_secret ) ;
AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
Feed feed ;
/* Feed feed ;
feed.Start( &agent, feed_base + "/-/folder?max-results=50&showroot=true" ) ;
do
@ -67,6 +67,16 @@ int main( int argc, char **argv )
qDebug() << e.Name().c_str() ;
}
} while ( feed.GetNext( &agent ) ) ;
*/
/* http::JsonResponse jsp ;
agent.Get( "https://www.googleapis.com/drive/v2/files", &jsp, http::Header() ) ;
std::cout << jsp.Response() << std::endl ;
*/
Feed feed( "https://www.googleapis.com/drive/v2/files" ) ;
while ( feed.Next(&agent) )
{
std::cout << feed.Content() << std::endl ;
}
QApplication app( argc, argv ) ;
MainWnd wnd ;

View File

@ -48,6 +48,7 @@ const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
using namespace gr ;
using namespace gr::v1 ;
namespace po = boost::program_options;
// libgcrypt insist this to be done in application, not library

View File

@ -45,7 +45,7 @@ include_directories(
)
file(GLOB DRIVE_HEADERS
${libgrive_SOURCE_DIR}/src/drive/*.hh
${libgrive_SOURCE_DIR}/src/drive/*.hh
)
file (GLOB PROTOCOL_HEADERS
@ -62,6 +62,7 @@ file (GLOB XML_HEADERS
file (GLOB LIBGRIVE_SRC
src/drive/*.cc
src/drive2/*.cc
src/http/*.cc
src/protocol/*.cc
src/util/*.cc

View File

@ -20,7 +20,7 @@
#include "CommonUri.hh"
#include <boost/format.hpp>
namespace gr {
namespace gr { namespace v1 {
std::string ChangesFeed( int changestamp )
{
@ -28,4 +28,4 @@ std::string ChangesFeed( int changestamp )
return changestamp > 0 ? (feed%changestamp).str() : feed_changes ;
}
}
} }

View File

@ -21,7 +21,7 @@
#include <string>
namespace gr
namespace gr { namespace v1
{
const std::string feed_base = "https://docs.google.com/feeds/default/private/full" ;
const std::string feed_changes = "https://docs.google.com/feeds/default/private/changes" ;
@ -33,4 +33,4 @@ namespace gr
"https://docs.google.com/feeds/upload/create-session/default/private/full" ;
std::string ChangesFeed( int changestamp ) ;
}
} }

View File

@ -44,15 +44,15 @@
// for debugging only
#include <iostream>
namespace gr {
namespace gr { namespace v1 {
namespace
{
const std::string state_file = ".grive_state" ;
}
Drive::Drive( http::Agent *http, const Json& options ) :
m_http ( http ),
Drive::Drive( http::Agent *agent, const Json& options ) :
m_http ( agent ),
m_root ( options["path"].Str() ),
m_state ( m_root / state_file, options ),
m_options ( options )
@ -197,4 +197,4 @@ void Drive::UpdateChangeStamp( )
std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ;
}
} // end of namespace
} } // end of namespace gr::v1

View File

@ -35,12 +35,14 @@ namespace http
class Agent ;
}
namespace v1 {
class Entry ;
class Drive
{
public :
Drive( http::Agent *http, const Json& options ) ;
Drive( http::Agent *agent, const Json& options ) ;
void DetectChanges() ;
void Update() ;
@ -64,4 +66,4 @@ private :
Json m_options ;
} ;
} // end of namespace
} } // end of namespace

View File

@ -29,7 +29,7 @@
#include <algorithm>
#include <iterator>
namespace gr {
namespace gr { namespace v1 {
/// construct an entry for the root folder
Entry::Entry( ) :
@ -195,4 +195,4 @@ std::string Entry::Name() const
return (m_kind == "file" || m_kind == "pdf") ? m_filename : m_title ;
}
} // end of namespace
} } // end of namespace gr::v1

View File

@ -33,6 +33,8 @@ namespace xml
class Node ;
}
namespace v1 {
/*! \brief corresponds to an "entry" in the resource feed
This class is decodes an entry in the resource feed. It will stored the properties like
@ -95,4 +97,4 @@ private :
bool m_is_removed ;
} ;
} // end of namespace
} } // end of namespace gr::v1

View File

@ -29,7 +29,7 @@
#include <cassert>
namespace gr {
namespace gr { namespace v1 {
Feed::Feed( )
{
@ -123,4 +123,4 @@ const xml::Node& Feed::Root() const
return m_root ;
}
} // end of namespace
} } // end of namespace gr::v1

View File

@ -36,6 +36,8 @@ namespace http
class Header ;
}
namespace v1 {
class Feed
{
public :
@ -88,4 +90,4 @@ private :
reference dereference() const ;
} ;
} // end of namespace
} } // end of namespace

View File

@ -45,7 +45,7 @@
// for debugging
#include <iostream>
namespace gr {
namespace gr { namespace v1 {
// hard coded XML file
const std::string xml_meta =
@ -668,11 +668,11 @@ bool Resource::HasID() const
return !m_href.empty() && !m_id.empty() ;
}
} // end of namespace
} } // end of namespace
namespace std
{
void swap( gr::Resource& c1, gr::Resource& c2 )
void swap( gr::v1::Resource& c1, gr::v1::Resource& c2 )
{
c1.Swap( c2 ) ;
}

View File

@ -34,9 +34,12 @@ namespace http
class Agent ;
}
class Entry ;
class Json ;
namespace v1 {
class Entry ;
/*! \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.
@ -158,9 +161,9 @@ private :
State m_state ;
} ;
} // end of namespace
} } // end of namespace gr::v1
namespace std
{
void swap( gr::Resource& c1, gr::Resource& c2 ) ;
void swap( gr::v1::Resource& c1, gr::v1::Resource& c2 ) ;
}

View File

@ -27,7 +27,7 @@
#include <algorithm>
#include <cassert>
namespace gr {
namespace gr { namespace v1 {
using namespace details ;
@ -155,4 +155,4 @@ ResourceTree::iterator ResourceTree::end()
return m_set.get<ByIdentity>().end() ;
}
} // end of namespace
} } // end of namespace gr::v1

View File

@ -32,6 +32,8 @@ namespace gr {
class Json ;
namespace v1 {
namespace details
{
using namespace boost::multi_index ;
@ -96,4 +98,4 @@ private :
Resource* m_root ;
} ;
} // end of namespace
} } // end of namespace gr::v1

View File

@ -31,7 +31,7 @@
#include <fstream>
namespace gr {
namespace gr { namespace v1 {
State::State( const fs::path& filename, const Json& options ) :
m_res ( options["path"].Str() ),
@ -63,7 +63,7 @@ bool State::IsIgnore( const std::string& filename )
return filename[0] == '.' ;
}
void State::FromLocal( const fs::path& p, gr::Resource* folder )
void State::FromLocal( const fs::path& p, Resource* folder )
{
assert( folder != 0 ) ;
assert( folder->IsFolder() ) ;
@ -294,4 +294,4 @@ void State::ChangeStamp( long cstamp )
m_cstamp = cstamp ;
}
} // end of namespace
} } // end of namespace gr::v1

View File

@ -34,6 +34,9 @@ namespace http
}
class Json ;
namespace v1 {
class Resource ;
class Entry ;
@ -80,4 +83,4 @@ private :
std::vector<Entry> m_unresolved ;
} ;
} // end of namespace
} } // end of namespace gr::v1

View File

@ -0,0 +1,35 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2013 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.
*/
#include "Drive.hh"
namespace gr { namespace v2 {
Drive::Drive( ) :
m_root( "", "", "" )
{
}
void Drive::Refresh( http::Agent *agent )
{
}
} } // end of namespace gr::v2

View File

@ -0,0 +1,45 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2013 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
#include "Resource.hh"
namespace gr {
namespace http
{
class Agent ;
}
namespace v2 {
class Drive
{
public :
Drive( ) ;
void Refresh( http::Agent *agent ) ;
private :
Resource m_root ;
} ;
} } // end of namespace gr::v2

View File

@ -0,0 +1,66 @@
/*
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.
*/
#include "Feed.hh"
#include "http/Agent.hh"
#include "http/Header.hh"
#include "protocol/JsonResponse.hh"
#include <iostream>
namespace gr { namespace v2 {
Feed::Feed( const std::string& base )
{
// Next() will grab this link
m_content.Add( "nextLink", Json(base) ) ;
Json url ;
m_content.Get("nextLink", url) ;
std::cout << "link = " << url.Str() << std::endl ;
}
bool Feed::Next( http::Agent *agent )
{
Json url ;
if ( !m_content.Get("nextLink", url) )
return false ;
http::JsonResponse out ;
try
{
agent->Get( url.Str(), &out, http::Header() ) ;
}
catch ( Exception& e )
{
e << DriveFeed_( m_content ) ;
throw ;
}
m_content = out.Response() ;
return true ;
}
Json Feed::Content() const
{
return m_content ;
}
} } // end of namespace

View File

@ -0,0 +1,55 @@
/*
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
#include "protocol/Json.hh"
#include "util/Exception.hh"
#include <string>
namespace gr
{
namespace http
{
class Agent ;
class Header ;
}
class Json ;
namespace v2 {
class Feed
{
public :
// exception info
typedef boost::error_info<struct DriveFeed, Json> DriveFeed_ ;
public :
Feed( const std::string& base ) ;
bool Next( http::Agent *agent ) ;
Json Content() const ;
private :
Json m_content ;
} ;
} } // end of namespace gr::v2

View File

@ -0,0 +1,57 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2013 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.
*/
#include "Resource.hh"
namespace gr { namespace v2 {
Resource::Resource( const std::string& id, const std::string& mime, const std::string& title ) :
m_id( id ),
m_mime( mime ),
m_title( title )
{
}
std::string Resource::ID() const
{
return m_id ;
}
std::string Resource::Mime() const
{
return m_mime ;
}
std::string Resource::Title() const
{
return m_title ;
}
bool Resource::IsFolder() const
{
return m_mime == "application/vnd.google-apps.folder" ;
}
void Resource::Add( const Resource& child )
{
m_children.push_back( child ) ;
}
} } // end of namespace gr::v2

View File

@ -0,0 +1,49 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2013 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
#include <string>
#include <vector>
namespace gr { namespace v2 {
class Resource
{
public :
Resource( const std::string& id, const std::string& mime, const std::string& title ) ;
std::string ID() const ;
std::string Mime() const ;
std::string Title() const ;
bool IsFolder() const ;
void Add( const Resource& child ) ;
private :
std::string m_id ;
std::string m_mime ;
std::string m_title ;
std::vector<Resource> m_children ;
} ;
} } // end of namespace gr::v2

View File

@ -63,9 +63,11 @@ Json::Json( const char *str ) :
) ;
}
/** Note that json_object_new_string_len() is not used.
*/
struct json_object* Json::InitStr( const char *str, std::size_t n )
{
struct json_object *j = ::json_object_new_string_len( str, n ) ;
struct json_object *j = ::json_object_new_string( str ) ;
if ( j == 0 )
BOOST_THROW_EXCEPTION(
Error()

View File

@ -31,6 +31,7 @@
namespace grut {
using namespace gr ;
using namespace gr::v1 ;
EntryTest::EntryTest( )
{

View File

@ -31,6 +31,7 @@
namespace grut {
using namespace gr ;
using namespace gr::v1 ;
ResourceTest::ResourceTest( )
{

View File

@ -29,6 +29,7 @@
namespace grut {
using namespace gr ;
using namespace gr::v1 ;
ResourceTreeTest::ResourceTreeTest( )
{

View File

@ -30,6 +30,7 @@
namespace grut {
using namespace gr ;
using namespace gr::v1 ;
StateTest::StateTest( )
{