From 59d0761d6852364c45f9d320120226618c360997 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Thu, 31 May 2012 23:48:30 +0800 Subject: [PATCH] minor refactoring --- libgrive/src/drive/Drive.cc | 2 + libgrive/src/drive/Resource.cc | 89 +++++++++++++++++---------------- libgrive/src/drive/Resource.hh | 1 + libgrive/src/util/DefaultLog.hh | 1 - 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 2786aba..1c34bb5 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -59,11 +59,13 @@ Drive::Drive( OAuth2& auth, const Json& options ) : m_http_hdr.push_back( "Authorization: Bearer " + m_auth.AccessToken() ) ; m_http_hdr.push_back( "GData-Version: 3.0" ) ; + Log( "Reading local directories", log::info ) ; m_state.FromLocal( "." ) ; http::Agent http ; SyncFolders( &http ) ; + Log( "Reading remote server file list", log::info ) ; http::XmlResponse xrsp ; http.Get( feed_base + "?showfolders=true&showroot=true", &xrsp, m_http_hdr ) ; xml::Node resp = xrsp.Response() ; diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index e8d6360..4e88593 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -64,6 +64,49 @@ Resource::Resource( const std::string& name, const std::string& kind ) : { } +void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) +{ + fs::path path = Path() ; + + // already sync + if ( fs::is_directory( path ) ) + { + Log( "folder %1% is in sync", path, log::verbose ) ; + m_state = sync ; + } + + // remote file created after last sync, so remote is newer + else if ( remote.MTime() > last_sync ) + { + if ( fs::exists( path ) ) + { + // TODO: handle type change + Log( "%1% changed from folder to file", path, log::verbose ) ; + m_state = sync ; + } + else + { + Log( "folder %1% is created in local", path, log::verbose ) ; + fs::create_directories( path ) ; + m_state = sync ; + } + } + else + { + if ( fs::exists( path ) ) + { + // TODO: handle type chage + Log( "%1% changed from file to folder", path, log::verbose ) ; + m_state = sync ; + } + else + { + Log( "folder %1% is deleted in local", path, log::verbose ) ; + m_state = local_deleted ; + } + } +} + /// Update the state according to information (i.e. Entry) from remote. This function /// compares the modification time and checksum of both copies and determine which /// one is newer. @@ -73,45 +116,7 @@ void Resource::FromRemote( const Entry& remote, const DateTime& last_sync ) // sync folder if ( remote.Kind() == "folder" && IsFolder() ) - { - // already sync - if ( fs::is_directory( path ) ) - { - Log( "folder %1% is in sync", path, log::verbose ) ; - m_state = sync ; - } - - // remote file created after last sync, so remote is newer - else if ( remote.MTime() > last_sync ) - { - if ( fs::exists( path ) ) - { - // TODO: handle type change - Log( "%1% changed from folder to file", path, log::verbose ) ; - m_state = sync ; - } - else - { - Log( "folder %1% is created in local", path, log::verbose ) ; - fs::create_directories( path ) ; - m_state = sync ; - } - } - else - { - if ( fs::exists( path ) ) - { - // TODO: handle type chage - Log( "%1% changed from file to folder", path, log::verbose ) ; - m_state = sync ; - } - else - { - Log( "folder %1% is deleted in local", path, log::verbose ) ; - m_state = local_deleted ; - } - } - } + FromRemoteFolder( remote, last_sync ) ; // local not exists else if ( !fs::exists( path ) ) @@ -153,8 +158,8 @@ void Resource::FromRemote( const Entry& remote, const DateTime& last_sync ) Log( "file %1% is changed in local", path, log::verbose ) ; m_state = local_changed ; } - - Trace( "%1% state is %2%", Name(), m_state ) ; + else + Trace( "file 1% state is %2%", Name(), m_state ) ; } m_entry.AssignID( remote ) ; @@ -178,8 +183,6 @@ void Resource::FromLocal( const DateTime& last_sync ) m_state = ( mtime > last_sync ? local_new : remote_deleted ) ; m_entry.FromLocal( path ) ; - - Trace( "%1% found on disk: %2%", Name(), m_state ) ; } assert( m_state != unknown ) ; diff --git a/libgrive/src/drive/Resource.hh b/libgrive/src/drive/Resource.hh index 16bd0bd..47a4b52 100644 --- a/libgrive/src/drive/Resource.hh +++ b/libgrive/src/drive/Resource.hh @@ -123,6 +123,7 @@ private : bool EditContent( http::Agent* http, const http::Headers& auth ) ; bool Create( http::Agent* http, const http::Headers& auth ) ; bool Upload( http::Agent* http, const std::string& link, const http::Headers& auth, bool post ) ; + void FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) ; private : Entry m_entry ; diff --git a/libgrive/src/util/DefaultLog.hh b/libgrive/src/util/DefaultLog.hh index e403d37..f0eafa5 100644 --- a/libgrive/src/util/DefaultLog.hh +++ b/libgrive/src/util/DefaultLog.hh @@ -43,5 +43,4 @@ private : std::bitset<5> m_enabled ; } ; - } // end of namespace