Factor out DownloadProgress
authorMichael Andres <ma@suse.de>
Tue, 15 Apr 2014 15:08:50 +0000 (17:08 +0200)
committerMichael Andres <ma@suse.de>
Tue, 15 Apr 2014 15:26:45 +0000 (17:26 +0200)
src/output/Out.h
src/source-download.cc

index 8a40413..d24e282 100644 (file)
@@ -15,6 +15,8 @@
 #include <zypp/Url.h>
 #include <zypp/TriBool.h>
 #include <zypp/ProgressData.h>
+#include <zypp/ZYppCallbacks.h>
+#include <zypp/base/LogTools.h>
 
 #include "utils/prompt.h"
 #include "utils/richtext.h"
@@ -509,6 +511,9 @@ public:
   /** Convenience class for progress output. */
   class ProgressBar;
 
+  /** Convenience class for download progress output. */
+  class DownloadProgress;
+
   /**
    * Start of an operation with reported progress.
    *
@@ -845,6 +850,76 @@ private:
 ///////////////////////////////////////////////////////////////////
 
 ///////////////////////////////////////////////////////////////////
+/// \class Out::DownloadProgress
+/// \brief Listen on media::DownloadProgressReport to feed a ProgressBar.
+///
+/// Connect to media::DownloadProgressReport to feed a ProgressBar, but forward
+/// callbacks to any original receiver.
+///////////////////////////////////////////////////////////////////
+struct Out::DownloadProgress : public callback::ReceiveReport<media::DownloadProgressReport>
+{
+  DownloadProgress( Out::ProgressBar & progressbar_r )
+  : _progressbar( &progressbar_r )
+  , _oldReceiver( Distributor::instance().getReceiver() )
+  {
+    connect();
+  }
+
+  ~DownloadProgress()
+  {
+    if ( _oldReceiver )
+      Distributor::instance().setReceiver( *_oldReceiver );
+    else
+      Distributor::instance().noReceiver();
+  }
+
+  virtual void start( const Url & file, Pathname localfile )
+  {
+    (*_progressbar)->range( 100 );     // we'll receive %
+
+    if ( _oldReceiver )
+      _oldReceiver->start( file, localfile );
+  }
+
+  virtual bool progress( int value, const Url & file, double dbps_avg = -1, double dbps_current = -1 )
+  {
+    (*_progressbar)->set( value );
+
+    if ( _oldReceiver )
+      return _oldReceiver->progress( value, file, dbps_avg, dbps_current );
+    return true;
+  }
+
+  virtual Action problem( const Url & file, Error error, const std::string & description )
+  {
+    ERR << description << endl;
+
+    if ( _oldReceiver )
+      return _oldReceiver->problem( file, error, description );
+    return Receiver::problem( file, error, description );
+  }
+
+  virtual void finish( const Url & file, Error error, const std::string & reason )
+  {
+    if ( error == NO_ERROR )
+      (*_progressbar)->toMax();
+    else
+    {
+      ERR << reason << std::endl;
+      _progressbar->error();
+    }
+
+    if ( _oldReceiver )
+      _oldReceiver->finish( file, error, reason );
+  }
+
+private:
+  Out::ProgressBar * _progressbar;
+  Receiver * _oldReceiver;
+};
+///////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
 /// \class Out::Error
 /// \brief Convenience class Error reporting.
 ///
index 1bb17e3..a0fb4ee 100644 (file)
@@ -13,7 +13,7 @@
 #include <zypp/SrcPackage.h>
 #include <zypp/target/rpm/RpmHeader.h>
 #include <zypp/repo/SrcPackageProvider.h>
-#include <zypp/ZYppCallbacks.h>
+// #include <zypp/ZYppCallbacks.h>
 
 #include "Zypper.h"
 #include "Table.h"
@@ -143,77 +143,6 @@ namespace
     };
     ///////////////////////////////////////////////////////////////////
 
-    ///////////////////////////////////////////////////////////////////
-    /// \class SourceDownloadImpl::DownloadProgress
-    /// \brief Listen on media::DownloadProgressReport to feed a ProgressBar.
-    ///
-    /// Connect to media::DownloadProgressReport to feed a ProgressBar, but forward
-    /// callbacks to any original receiver.
-    /// \todo Probably move this as helper class to \ref Out.
-    ///////////////////////////////////////////////////////////////////
-    struct DownloadProgress : public callback::ReceiveReport<media::DownloadProgressReport>
-    {
-      DownloadProgress( Out::ProgressBar & progressbar_r )
-      : _progressbar( &progressbar_r )
-      , _oldReceiver( Distributor::instance().getReceiver() )
-      {
-       connect();
-      }
-
-      ~DownloadProgress()
-      {
-       if ( _oldReceiver )
-         Distributor::instance().setReceiver( *_oldReceiver );
-       else
-         Distributor::instance().noReceiver();
-      }
-
-      virtual void start( const Url & file, Pathname localfile )
-      {
-       (*_progressbar)->range( 100 );  // we'll receive %
-
-       if ( _oldReceiver )
-         _oldReceiver->start( file, localfile );
-      }
-
-      virtual bool progress( int value, const Url & file, double dbps_avg = -1, double dbps_current = -1 )
-      {
-       (*_progressbar)->set( value );
-
-       if ( _oldReceiver )
-         return _oldReceiver->progress( value, file, dbps_avg, dbps_current );
-       return true;
-      }
-
-      virtual Action problem( const Url & file, Error error, const std::string & description )
-      {
-       ERR << description << endl;
-
-       if ( _oldReceiver )
-         return _oldReceiver->problem( file, error, description );
-       return Receiver::problem( file, error, description );
-      }
-
-      virtual void finish( const Url & file, Error error, const std::string & reason )
-      {
-       if ( error == NO_ERROR )
-         (*_progressbar)->toMax();
-       else
-       {
-         ERR << reason << endl;
-         _progressbar->error();
-       }
-
-       if ( _oldReceiver )
-         _oldReceiver->finish( file, error, reason );
-      }
-
-    private:
-      Out::ProgressBar * _progressbar;
-      Receiver * _oldReceiver;
-    };
-    ///////////////////////////////////////////////////////////////////
-
   public:
     void sourceDownload();
 
@@ -511,7 +440,7 @@ namespace
          ManagedFile localfile;
          {
            report.error(); // error if provideSrcPackage throws
-           DownloadProgress redirect( report );
+           Out::DownloadProgress redirect( report );
            localfile = prov.provideSrcPackage( spkg._srcPackage->asKind<SrcPackage>() );
            DBG << localfile << endl;
            report.error( false );