Skip to content

Commit

Permalink
can show tree now, but still has issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Apr 29, 2013
1 parent 326ae20 commit 94d66f0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 27 deletions.
15 changes: 14 additions & 1 deletion bgrive/src/DriveModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ int DriveModel::columnCount( const QModelIndex& parent ) const

bool DriveModel::hasChildren( const QModelIndex& parent ) const
{
qDebug() << Res(parent)->Title().c_str() << " has " << Res(parent)->ChildCount() << " children" ;
return Res(parent)->ChildCount() > 0 ;
}

Expand All @@ -87,7 +88,19 @@ const Resource* DriveModel::Res( const QModelIndex& idx ) const

QModelIndex DriveModel::parent( const QModelIndex& idx ) const
{
return QModelIndex() ;
// if I am root, my parent is myself
const Resource *res = Res(idx) ;
if ( res == m_drv.Root() )
return QModelIndex() ;

// if my parent is root, return model index of root (i.e. QModelIndex())
const Resource *parent = m_drv.Parent(res) ;
if ( parent == 0 || parent == m_drv.Root() || idx.column() != 0 )
return QModelIndex() ;

// check grand-parent to know the row() of my parent
const Resource *grand = m_drv.Parent(parent) ;
return createIndex( grand->Index(parent->ID()), 0, const_cast<Resource*>(grand) ) ;
}

} // end of namespace
54 changes: 32 additions & 22 deletions libgrive/src/drive2/Drive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,72 @@

namespace gr { namespace v2 {

Drive::Drive( )
Drive::Drive( ) :
m_root( 0 )
{

}

void Drive::Refresh( http::Agent *agent )
{
// find root node ID
assert( m_root == 0 ) ;
m_root = NewResource( agent, "root" ) ;

// get all folders first
Feed folders( feeds::files ) ;
folders.Query( "mimeType", mime_types::folder ) ;
std::vector<std::pair<std::string, Resource*> > parent_child ;

while ( folders.Next( agent ) )
{
std::vector<Json> items = folders.Content()["items"].AsArray() ;
for ( std::vector<Json>::iterator i = items.begin() ; i != items.end() ; ++i )
{
Resource *r = NewResource( *i ) ;

//std::cout << r->Title() << " " << r->ID() << std::endl ;

Json parents ;
if ( i->Get( "parents", parents ) )
{
std::vector<std::string> pids ;
parents.Select<std::string>( "id", std::back_inserter(pids) ) ;

for ( std::vector<std::string>::iterator p = pids.begin() ; p != pids.end() ; ++p )
{
std::cout << "parent = " << *p << std::endl ;
parent_child.push_back( std::make_pair( *p, r ) ) ;

// add to root node if no parent
if ( pids.empty() )
m_root.AddChild( r->ID() ) ;
}
}
}
}

for ( std::vector<std::pair<std::string, Resource*> >::iterator i = parent_child.begin() ;
i != parent_child.end() ; ++i )
{
Resource *parent = Find( i->first ) ;
Resource *parent = Find( i->first ), *child = i->second ;
std::cout << i->first << " " << child->ID() << std::endl ;
if ( parent != 0 )
parent->AddChild( i->second->ID() ) ;
{
// initialize parent IDs

parent->AddChild( child->ID() ) ;
}
}
}

Resource* Drive::FindRoot( http::Agent *agent )
/// Create resource base on ID
Resource* Drive::NewResource( http::Agent *agent, const std::string& id )
{
// get all folders first
Feed folders( feeds::files + "/root?fields=id" ) ;
folders.Next( agent ) ;

std::string id = folders.Content()["id"].Str() ;
std::cout << "root = " << id << std::endl ;
Feed feed( feeds::files + "/" + id ) ;
feed.Next( agent ) ;

return Find( folders.Content()["id"].Str() ) ;
return NewResource( feed.Content() ) ;
}

Resource* Drive::NewResource( const Json& item )
{
Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ;

// initialize parent IDs
std::cout << r->Title() << " " << r->ID() << std::endl ;

m_db.insert(r) ;
assert( Find(r->ID()) == r ) ;

Expand All @@ -112,18 +117,23 @@ const Resource* Drive::Find( const std::string& id ) const

Resource* Drive::Root()
{
return &m_root ;
return m_root ;
}

const Resource* Drive::Root() const
{
return &m_root ;
return m_root ;
}

const Resource* Drive::Child( const Resource *parent, std::size_t idx ) const
{
return Find( parent->At(idx) ) ;
}

const Resource* Drive::Parent( const Resource *child ) const
{
return Find( child->Parent() ) ;
}

} } // end of namespace gr::v2

5 changes: 3 additions & 2 deletions libgrive/src/drive2/Drive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ public :
const Resource* Root() const ;

const Resource* Child( const Resource *parent, std::size_t idx ) const ;
const Resource* Parent( const Resource *child ) const ;

private :
Resource* NewResource( const Json& item ) ;
Resource* FindRoot( http::Agent *agent ) ;
Resource* NewResource( http::Agent *agent, const std::string& id ) ;

private :
details::DB m_db ;

Resource m_root ;
Resource *m_root ;
} ;

} } // end of namespace gr::v2
17 changes: 17 additions & 0 deletions libgrive/src/drive2/Resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "CommonUri.hh"

#include <algorithm>

namespace gr { namespace v2 {

/** Default constructor construct the resource of the root folder
Expand Down Expand Up @@ -65,6 +67,11 @@ void Resource::AddChild( const std::string& child )
m_children.push_back( child ) ;
}

void Resource::SetParent( const std::string& parent )
{
m_parent = parent ;
}

std::size_t Resource::ChildCount() const
{
return m_children.size() ;
Expand All @@ -75,4 +82,14 @@ std::string Resource::At( std::size_t idx ) const
return m_children.at(idx) ;
}

std::string Resource::Parent() const
{
return m_parent ;
}

std::size_t Resource::Index( const std::string& child ) const
{
return std::find( m_children.begin(), m_children.end(), child ) - m_children.begin() ;
}

} } // end of namespace gr::v2
6 changes: 5 additions & 1 deletion libgrive/src/drive2/Resource.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ public :
bool IsFolder() const ;

void AddChild( const std::string& child ) ;
void SetParent( const std::string& parent ) ;

std::size_t ChildCount() const ;

std::string At( std::size_t idx ) const ;
std::string Parent() const ;
std::size_t Index( const std::string& child ) const ;

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

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

std::string m_parent ;
} ;

} } // end of namespace gr::v2
6 changes: 5 additions & 1 deletion libgrive/src/protocol/Json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public :
{
Array a = AsArray() ;
for ( Array::iterator i = a.begin() ; i != a.end() ; ++i )
*out++ = i->As<T>() ;
{
Json value;
if ( i->Get( key, value ) )
*out++ = value.As<T>() ;
}
return out ;
}

Expand Down

0 comments on commit 94d66f0

Please sign in to comment.