diff --git a/bgrive/src/DriveModel.cc b/bgrive/src/DriveModel.cc index e207fb4..8d65271 100644 --- a/bgrive/src/DriveModel.cc +++ b/bgrive/src/DriveModel.cc @@ -20,12 +20,17 @@ #include "DriveModel.hh" +#include "drive2/Resource.hh" + #include namespace gr { -DriveModel::DriveModel( ) +using namespace v2; + +DriveModel::DriveModel( http::Agent *agent ) { + m_drv.Refresh( agent ) ; } Qt::ItemFlags DriveModel::flags( const QModelIndex& ) const @@ -35,7 +40,17 @@ Qt::ItemFlags DriveModel::flags( const QModelIndex& ) const QVariant DriveModel::data( const QModelIndex& index, int role ) const { - return role == Qt::DisplayRole ? QString("wow") : QVariant() ; + const Resource *res = Res(index) ; + if ( role == Qt::DisplayRole && res != 0 ) + { + switch ( index.column() ) + { + case 0: return QString::fromUtf8(res->Title().c_str()) ; + default: break ; + } + } + + return QVariant() ; } QVariant DriveModel::headerData( int section, Qt::Orientation orientation, int role ) const @@ -45,7 +60,7 @@ QVariant DriveModel::headerData( int section, Qt::Orientation orientation, int r int DriveModel::rowCount( const QModelIndex& parent ) const { - return 10 ; + return Res(parent)->ChildCount() ; } int DriveModel::columnCount( const QModelIndex& parent ) const @@ -55,12 +70,19 @@ int DriveModel::columnCount( const QModelIndex& parent ) const bool DriveModel::hasChildren( const QModelIndex& parent ) const { - return parent.isValid() ? false : true ; + return Res(parent)->ChildCount() > 0 ; } QModelIndex DriveModel::index( int row, int column, const QModelIndex & parent ) const { - return parent.isValid() ? QModelIndex() : createIndex( row, column, 0 ) ; + return createIndex( row, column, const_cast(m_drv.Child(Res(parent), row)) ) ; +} + +const Resource* DriveModel::Res( const QModelIndex& idx ) const +{ + return idx.isValid() + ? reinterpret_cast(idx.internalPointer()) + : m_drv.Root() ; } QModelIndex DriveModel::parent( const QModelIndex& idx ) const diff --git a/bgrive/src/DriveModel.hh b/bgrive/src/DriveModel.hh index f7ee26f..1a74283 100644 --- a/bgrive/src/DriveModel.hh +++ b/bgrive/src/DriveModel.hh @@ -22,12 +22,19 @@ #include +#include "drive2/Drive.hh" + namespace gr { +namespace http +{ + class Agent ; +} + class DriveModel : public QAbstractItemModel { public : - DriveModel( ) ; + DriveModel( http::Agent *agent ) ; // QAbstractItemModel overrides Qt::ItemFlags flags( const QModelIndex & index ) const ; @@ -38,6 +45,12 @@ public : bool hasChildren ( const QModelIndex& parent ) const ; QModelIndex index( int row, int column, const QModelIndex& parent ) const ; QModelIndex parent( const QModelIndex& idx ) const ; + +private : + const v2::Resource* Res( const QModelIndex& idx ) const ; + +private : + v2::Drive m_drv ; } ; } // end of namespace diff --git a/bgrive/src/MainWnd.cc b/bgrive/src/MainWnd.cc index d73c9c9..84bf64f 100644 --- a/bgrive/src/MainWnd.cc +++ b/bgrive/src/MainWnd.cc @@ -24,7 +24,8 @@ namespace gr { -MainWnd::MainWnd( ) +MainWnd::MainWnd( http::Agent *agent ) : + m_drive( agent ) { m_ui.setupUi(this) ; diff --git a/bgrive/src/MainWnd.hh b/bgrive/src/MainWnd.hh index d5be664..5551a6b 100644 --- a/bgrive/src/MainWnd.hh +++ b/bgrive/src/MainWnd.hh @@ -27,12 +27,17 @@ namespace gr { +namespace http +{ + class Agent ; +} + class MainWnd : public QMainWindow { Q_OBJECT public : - MainWnd( ) ; + MainWnd( http::Agent *agent ) ; private : Ui::MainWindow m_ui ; diff --git a/bgrive/src/main.cc b/bgrive/src/main.cc index fd9a0fc..dd4bd93 100644 --- a/bgrive/src/main.cc +++ b/bgrive/src/main.cc @@ -78,11 +78,11 @@ int main( int argc, char **argv ) std::cout << feed.Content() << std::endl ; } */ - Drive drive ; - drive.Refresh( &agent ) ; +// Drive drive ; +// drive.Refresh( &agent ) ; QApplication app( argc, argv ) ; - MainWnd wnd ; + MainWnd wnd( &agent ) ; wnd.show(); return app.exec() ; diff --git a/libgrive/src/drive2/Drive.cc b/libgrive/src/drive2/Drive.cc index e731ef2..9267be0 100644 --- a/libgrive/src/drive2/Drive.cc +++ b/libgrive/src/drive2/Drive.cc @@ -27,6 +27,8 @@ #include #include +#include + namespace gr { namespace v2 { Drive::Drive( ) @@ -36,37 +38,64 @@ 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( feeds::files ) ; -// folders.Query( "mimeType", "application/vnd.google-apps.folder" ) ; + std::vector > parent_child ; 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 ) ; + Resource *r = NewResource( *i ) ; + + Json parents ; + if ( i->Get( "parents", parents ) ) + { + std::vector pids ; + parents.Select( "id", std::back_inserter(pids) ) ; + + for ( std::vector::iterator p = pids.begin() ; p != pids.end() ; ++p ) + 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 >::iterator i = parent_child.begin() ; + i != parent_child.end() ; ++i ) + { + Resource *parent = Find( i->first ) ; + if ( parent != 0 ) + parent->AddChild( i->second->ID() ) ; + } } -const Resource* Drive::Add( const Json& item ) +Resource* Drive::FindRoot( http::Agent *agent ) +{ + // 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 ; + + return Find( folders.Content()["id"].Str() ) ; +} + +Resource* Drive::NewResource( const Json& item ) { Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ; // initialize parent IDs - Json parents ; - if ( item.Get( "parents", parents ) ) - { - std::vector parent_ids ; - parents.Select( "id", std::back_inserter(parent_ids) ) ; - - r->SetParent( parent_ids.begin(), parent_ids.end() ) ; - std::cout << r->Title() << " " << r->ID() << " " << parent_ids.size() << std::endl ; - } + std::cout << r->Title() << " " << r->ID() << std::endl ; - return *m_db.insert(r).first ; + m_db.insert(r) ; + assert( Find(r->ID()) == r ) ; + + return r ; } Resource* Drive::Find( const std::string& id ) @@ -75,5 +104,26 @@ Resource* Drive::Find( const std::string& id ) return i != m_db.get().end() ? *i : 0 ; } +const Resource* Drive::Find( const std::string& id ) const +{ + details::ID::const_iterator i = m_db.get().find(id) ; + return i != m_db.get().end() ? *i : 0 ; +} + +Resource* Drive::Root() +{ + return &m_root ; +} + +const Resource* Drive::Root() const +{ + return &m_root ; +} + +const Resource* Drive::Child( const Resource *parent, std::size_t idx ) const +{ + return Find( parent->At(idx) ) ; +} + } } // end of namespace gr::v2 diff --git a/libgrive/src/drive2/Drive.hh b/libgrive/src/drive2/Drive.hh index 241ab7f..ba5ecd5 100644 --- a/libgrive/src/drive2/Drive.hh +++ b/libgrive/src/drive2/Drive.hh @@ -65,12 +65,21 @@ public : void Refresh( http::Agent *agent ) ; Resource* Find( const std::string& id ) ; + const Resource* Find( const std::string& id ) const ; + + Resource* Root() ; + const Resource* Root() const ; + + const Resource* Child( const Resource *parent, std::size_t idx ) const ; private : - const Resource* Add( const Json& item ) ; - + Resource* NewResource( const Json& item ) ; + Resource* FindRoot( http::Agent *agent ) ; + private : details::DB m_db ; + + Resource m_root ; } ; } } // end of namespace gr::v2 diff --git a/libgrive/src/drive2/Resource.cc b/libgrive/src/drive2/Resource.cc index 6724d1e..3db446b 100644 --- a/libgrive/src/drive2/Resource.cc +++ b/libgrive/src/drive2/Resource.cc @@ -60,4 +60,19 @@ bool Resource::IsFolder() const return m_mime == "application/vnd.google-apps.folder" ; } +void Resource::AddChild( const std::string& child ) +{ + m_children.push_back( child ) ; +} + +std::size_t Resource::ChildCount() const +{ + return m_children.size() ; +} + +std::string Resource::At( std::size_t idx ) const +{ + return m_children.at(idx) ; +} + } } // end of namespace gr::v2 diff --git a/libgrive/src/drive2/Resource.hh b/libgrive/src/drive2/Resource.hh index ca4a599..ccfc477 100644 --- a/libgrive/src/drive2/Resource.hh +++ b/libgrive/src/drive2/Resource.hh @@ -31,24 +31,24 @@ public : Resource() ; Resource( const std::string& id, const std::string& mime, const std::string& title ) ; - template - void SetParent( InputIt first, InputIt last ) - { - m_parent.assign( first, last ) ; - } - std::string ID() const ; std::string Mime() const ; std::string Title() const ; bool IsFolder() const ; + void AddChild( const std::string& child ) ; + + std::size_t ChildCount() const ; + + std::string At( std::size_t idx ) const ; + private : std::string m_id ; std::string m_mime ; std::string m_title ; - std::vector m_parent ; + std::vector m_children ; } ; } } // end of namespace gr::v2