Skip to content

Commit

Permalink
show files now. working good.
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Apr 29, 2013
1 parent 93fe68c commit bba65c0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
4 changes: 0 additions & 4 deletions bgrive/src/DriveModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,12 @@ QModelIndex DriveModel::parent( const QModelIndex& idx ) const
if ( res == m_drv.Root() )
return QModelIndex() ;

qDebug() << "getting parent of " << res->Title().c_str() ;

// 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() ;

qDebug() << "parent of " << res->Title().c_str() << " should be " << parent->Title().c_str();

// 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*>(parent) ) ;
Expand Down
72 changes: 39 additions & 33 deletions libgrive/src/drive2/Drive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,39 @@ void Drive::Refresh( http::Agent *agent )
// 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) ) ;

// onlly the first parent counts
if ( !pids.empty() )
parent_child.push_back( std::make_pair( pids.front(), r ) ) ;

// 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 ) ) ;
}
}
}
}
NewResource( agent, folders ) ;

for ( std::vector<std::pair<std::string, Resource*> >::iterator i = parent_child.begin() ;
i != parent_child.end() ; ++i )
// get all files
Feed files( feeds::files ) ;
NewResource( agent, files ) ;

// build parent-child linkage between folders
for ( details::DB::iterator i = m_db.begin() ; i != m_db.end() ; ++i )
{
Resource *parent = Find( i->first ), *child = i->second ;
Resource *parent = Find( (*i)->Parent() ), *child = *i ;
assert( child != 0 ) ;

if ( parent != 0 )
{
std::cout << "parent of " << child->Title() << " is " << parent->Title() << std::endl ;

// initialize parent IDs
parent->AddChild( child->ID() ) ;
child->SetParent( parent->ID() ) ;
}
}
}

void Drive::NewResource( http::Agent *agent, Feed& items )
{
assert( agent != 0 ) ;

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

/// Create resource base on ID
Resource* Drive::NewResource( http::Agent *agent, const std::string& id )
{
Expand All @@ -102,7 +90,25 @@ Resource* Drive::NewResource( http::Agent *agent, const std::string& id )

Resource* Drive::NewResource( const Json& item )
{
Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ;
// assume resource is directly under root
std::string parent_id = m_root != 0 ? m_root->ID() : "" ;

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

// only the first parent counts
if ( !pids.empty() )
parent_id = pids.front() ;
}

Resource *r = new Resource(
item["id"].Str(),
item["mimeType"].Str(),
item["title"].Str(),
parent_id ) ;

m_db.insert(r) ;
assert( Find(r->ID()) == r ) ;
Expand Down
3 changes: 3 additions & 0 deletions libgrive/src/drive2/Drive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace details
typedef DB::index<ByIdentity>::type Set ;
}

class Feed ;

class Drive
{
public :
Expand All @@ -76,6 +78,7 @@ public :
private :
Resource* NewResource( const Json& item ) ;
Resource* NewResource( http::Agent *agent, const std::string& id ) ;
void NewResource( http::Agent *agent, Feed& items ) ;

private :
details::DB m_db ;
Expand Down
13 changes: 9 additions & 4 deletions libgrive/src/drive2/Resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ Resource::Resource() :
{
}

Resource::Resource( const std::string& id, const std::string& mime, const std::string& title ) :
m_id( id ),
m_mime( mime ),
m_title( title )
Resource::Resource(
const std::string& id,
const std::string& mime,
const std::string& title,
const std::string& parent ) :
m_id ( id ),
m_mime ( mime ),
m_title ( title ),
m_parent( parent )
{
}

Expand Down
6 changes: 5 additions & 1 deletion libgrive/src/drive2/Resource.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class Resource
{
public :
Resource() ;
Resource( const std::string& id, const std::string& mime, const std::string& title ) ;
Resource(
const std::string& id,
const std::string& mime,
const std::string& title,
const std::string& parent ) ;

std::string ID() const ;
std::string Mime() const ;
Expand Down

0 comments on commit bba65c0

Please sign in to comment.