From 87d96972f7ceaa1a2bf110892a7cfaceafca2548 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Mon, 29 Apr 2013 14:50:54 +0800 Subject: [PATCH] Added Query() helper to Feed. and common URI header --- libgrive/src/drive2/CommonUri.hh | 36 ++++++++++++++++++++++++++++++++ libgrive/src/drive2/Drive.cc | 17 +++++++++++---- libgrive/src/drive2/Drive.hh | 2 ++ libgrive/src/drive2/Feed.cc | 10 +++++---- libgrive/src/drive2/Feed.hh | 3 +++ libgrive/src/drive2/Resource.cc | 4 +++- 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 libgrive/src/drive2/CommonUri.hh diff --git a/libgrive/src/drive2/CommonUri.hh b/libgrive/src/drive2/CommonUri.hh new file mode 100644 index 0000000..19cc32c --- /dev/null +++ b/libgrive/src/drive2/CommonUri.hh @@ -0,0 +1,36 @@ +/* + 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 + +namespace gr { namespace v2 { + +namespace feeds +{ + const std::string files = "https://www.googleapis.com/drive/v2/files" ; +} + +namespace mime_types +{ + const std::string folder = "application/vnd.google-apps.folder" ; +} + +} } // end of namespace gr::v2 diff --git a/libgrive/src/drive2/Drive.cc b/libgrive/src/drive2/Drive.cc index 9878253..cdfdb76 100644 --- a/libgrive/src/drive2/Drive.cc +++ b/libgrive/src/drive2/Drive.cc @@ -20,6 +20,7 @@ #include "Drive.hh" +#include "CommonUri.hh" #include "Feed.hh" #include "protocol/Json.hh" @@ -34,16 +35,18 @@ Drive::Drive( ) void Drive::Refresh( http::Agent *agent ) { // get all folders first - Feed folders( - "https://www.googleapis.com/drive/v2/files?q=mimeType+%3d+%27application/vnd.google-apps.folder%27" ) ; - +// Feed folders( +// "https://www.googleapis.com/drive/v2/files?q=mimeType+%3d+%27application/vnd.google-apps.folder%27" ) ; + Feed folders( feeds::files ) ; +// folders.Query( "mimeType", "application/vnd.google-apps.folder" ) ; + while ( folders.Next( agent ) ) { std::vector items = folders.Content()["items"].AsArray() ; for ( std::vector::iterator i = items.begin() ; i != items.end() ; ++i ) { const Resource *r = Add( *i ) ; - std::cout << r->Title() << " " << r->Mime() << std::endl ; + std::cout << r->Title() << " " << r->Mime() << std::endl ; } } } @@ -54,5 +57,11 @@ const Resource* Drive::Add( const Json& item ) return *m_db.insert(r).first ; } +Resource* Drive::Find( const std::string& id ) +{ + details::ID::iterator i = m_db.get().find(id) ; + return i != m_db.get().end() ? *i : 0 ; +} + } } // end of namespace gr::v2 diff --git a/libgrive/src/drive2/Drive.hh b/libgrive/src/drive2/Drive.hh index f16a4bb..241ab7f 100644 --- a/libgrive/src/drive2/Drive.hh +++ b/libgrive/src/drive2/Drive.hh @@ -64,6 +64,8 @@ public : void Refresh( http::Agent *agent ) ; + Resource* Find( const std::string& id ) ; + private : const Resource* Add( const Json& item ) ; diff --git a/libgrive/src/drive2/Feed.cc b/libgrive/src/drive2/Feed.cc index a0587ab..dd59ca5 100644 --- a/libgrive/src/drive2/Feed.cc +++ b/libgrive/src/drive2/Feed.cc @@ -31,10 +31,12 @@ 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 ; +} + +void Feed::Query( const std::string& field, const std::string& value ) +{ + std::string url = m_content["nextLink"].Str() ; + m_content.Add( "nextLink", Json( url + "?q=" + field + "+%3d+%27" + value + "%27" ) ) ; } bool Feed::Next( http::Agent *agent ) diff --git a/libgrive/src/drive2/Feed.hh b/libgrive/src/drive2/Feed.hh index dbed1af..157f1e3 100644 --- a/libgrive/src/drive2/Feed.hh +++ b/libgrive/src/drive2/Feed.hh @@ -44,6 +44,9 @@ public : public : Feed( const std::string& base ) ; + void Query( const std::string& field, const std::string& value ) ; + + bool Next( http::Agent *agent ) ; Json Content() const ; diff --git a/libgrive/src/drive2/Resource.cc b/libgrive/src/drive2/Resource.cc index 2c015ae..1e33a58 100644 --- a/libgrive/src/drive2/Resource.cc +++ b/libgrive/src/drive2/Resource.cc @@ -20,13 +20,15 @@ #include "Resource.hh" +#include "CommonUri.hh" + namespace gr { namespace v2 { /** Default constructor construct the resource of the root folder */ Resource::Resource() : m_id( "root" ), - m_mime( "application/vnd.google-apps.folder" ), + m_mime( mime_types::folder ), m_title( "Root folder" ) { }