Skip to content

Commit

Permalink
added code to fetch all folders
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Apr 29, 2013
1 parent 62dc542 commit 209d0b5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
7 changes: 5 additions & 2 deletions bgrive/src/main.cc
Expand Up @@ -23,7 +23,7 @@
#include <QtGui/QApplication>
#include <QtCore/QDebug>

#include "drive2/Feed.hh"
#include "drive2/Drive.hh"

#include "http/CurlAgent.hh"
#include "http/Header.hh"
Expand Down Expand Up @@ -72,11 +72,14 @@ int main( int argc, char **argv )
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" ) ;
/* Feed feed( "https://www.googleapis.com/drive/v2/files" ) ;
while ( feed.Next(&agent) )
{
std::cout << feed.Content() << std::endl ;
}
*/
Drive drive ;
drive.Refresh( &agent ) ;

QApplication app( argc, argv ) ;
MainWnd wnd ;
Expand Down
27 changes: 25 additions & 2 deletions libgrive/src/drive2/Drive.cc
Expand Up @@ -20,15 +20,38 @@

#include "Drive.hh"

#include "Feed.hh"
#include "protocol/Json.hh"

#include <iostream>

namespace gr { namespace v2 {

Drive::Drive( ) :
m_root( "", "", "" )
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" ) ;

while ( folders.Next( agent ) )
{
std::vector<Json> items = folders.Content()["items"].AsArray() ;
for ( std::vector<Json>::iterator i = items.begin() ; i != items.end() ; ++i )
{
const Resource *r = Add( *i ) ;
std::cout << r->Title() << " " << r->Mime() << std::endl ;
}
}
}

const Resource* Drive::Add( const Json& item )
{
Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ;
return *m_db.insert(r).first ;
}

} } // end of namespace gr::v2
Expand Down
31 changes: 30 additions & 1 deletion libgrive/src/drive2/Drive.hh
Expand Up @@ -22,15 +22,41 @@

#include "Resource.hh"

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>

namespace gr {

namespace http
{
class Agent ;
}

class Json ;

namespace v2 {

namespace details
{
using namespace boost::multi_index ;
struct ByID {} ;
struct ByHref {} ;
struct ByIdentity {} ;

typedef multi_index_container<
Resource*,
indexed_by<
hashed_unique<tag<ByIdentity>, identity<Resource*> >,
hashed_non_unique<tag<ByID>, const_mem_fun<Resource, std::string, &Resource::ID> >
>
> DB ;

typedef DB::index<ByID>::type ID ;
typedef DB::index<ByIdentity>::type Set ;
}

class Drive
{
public :
Expand All @@ -39,7 +65,10 @@ public :
void Refresh( http::Agent *agent ) ;

private :
Resource m_root ;
const Resource* Add( const Json& item ) ;

private :
details::DB m_db ;
} ;

} } // end of namespace gr::v2
13 changes: 11 additions & 2 deletions libgrive/src/drive2/Resource.cc
Expand Up @@ -22,6 +22,15 @@

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_title( "Root folder" )
{
}

Resource::Resource( const std::string& id, const std::string& mime, const std::string& title ) :
m_id( id ),
m_mime( mime ),
Expand Down Expand Up @@ -49,9 +58,9 @@ bool Resource::IsFolder() const
return m_mime == "application/vnd.google-apps.folder" ;
}

void Resource::Add( const Resource& child )
void Resource::Add( const std::string& child_id )
{
m_children.push_back( child ) ;
m_children.push_back( child_id ) ;
}

} } // end of namespace gr::v2
9 changes: 5 additions & 4 deletions libgrive/src/drive2/Resource.hh
Expand Up @@ -28,22 +28,23 @@ namespace gr { namespace v2 {
class Resource
{
public :
Resource() ;
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 ) ;

void Add( const std::string& child_id ) ;

private :
std::string m_id ;
std::string m_mime ;
std::string m_title ;

std::vector<Resource> m_children ;
std::vector<std::string> m_children ;
} ;

} } // end of namespace gr::v2

0 comments on commit 209d0b5

Please sign in to comment.