[solang] Sanitized ProgressObserver usage



commit 209b8fbabc46e9235b8f54db0809378e46a3d241
Author: Debarshi Ray <rishi gnu org>
Date:   Wed Dec 9 05:15:48 2009 +0200

    Sanitized ProgressObserver usage
    
    + Every operation that uses a ProgressObserver should provide a
      description, number of events and set the current events to 0; and
      reset it on completion. It is good to check whether the pointer is
      0 or not.
    + ProgressObserver::reset now emits a separate Glib::Dispatcher which
      can be obtained though ProgressObserver::dispatcher_reset.
    + ProgressDialog and MainWindow::progress_ use the above dispatcher.
      ProgressDialog::reset is no longer needed and has been removed.
    + Do not expand MainWindow::progress_. Use Gtk::PACK_SHRINK instead.
      Only show it when it is in use.

 po/POTFILES.in                         |    1 +
 src/application/application.cpp        |    1 -
 src/application/engine.cpp             |   16 ++--
 src/application/main-window.cpp        |   37 ++++++++---
 src/application/main-window.h          |    7 ++-
 src/common/progress-dialog.cpp         |   17 ++++-
 src/common/progress-dialog.h           |    6 +-
 src/common/progress-observer.cpp       |    9 ++-
 src/common/progress-observer.h         |    5 ++
 src/database/database.cpp              |  111 +++++++++++++++++++++++---------
 src/exporter/directory-destination.cpp |   20 +++++-
 src/importer/camera-source.cpp         |   20 +++++-
 src/importer/directory-source.cpp      |   19 +++++-
 src/importer/flickr-source.cpp         |   17 ++++-
 14 files changed, 214 insertions(+), 72 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 51cddd3..3786879 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -10,6 +10,7 @@ src/attribute/tag-manager.cpp
 src/attribute/tag-new-dialog.cpp
 src/common/histogram-viewer.cpp
 src/common/progress-dialog.cpp
+src/database/database.cpp
 src/edit-engine/brightness-operation.cpp
 src/edit-engine/buffer.cpp
 src/edit-engine/contrast-operation.cpp
diff --git a/src/application/application.cpp b/src/application/application.cpp
index 827ce44..2293215 100644
--- a/src/application/application.cpp
+++ b/src/application/application.cpp
@@ -486,7 +486,6 @@ void
 Application::hide_progress_dialog() throw()
 {
     progressDialog_.hide();
-    progressDialog_.reset();
     engine_.get_default_observer()->reset();
 }
 
diff --git a/src/application/engine.cpp b/src/application/engine.cpp
index 0449345..e24f789 100644
--- a/src/application/engine.cpp
+++ b/src/application/engine.cpp
@@ -109,7 +109,6 @@ Engine::import(const PhotoPtr & photo,
 #endif
     PhotoPtr imp_photo = source->import(photo, selected_storage,
                                     tags, database_, observer);
-    observer->reset();
     photoImportEnd_.emit();
 
     PhotoList imp_photos;
@@ -150,7 +149,6 @@ Engine::import(const PhotoList & photos,
 #endif
     PhotoList imp_photos = source->import(photos, selected_storage,
                                           tags, database_, observer);
-    observer->reset();
     photoImportEnd_.emit();
 
     {
@@ -177,8 +175,6 @@ Engine::import(const IPhotoSourcePtr & source,
                const TagList & tags,
                const ProgressObserverPtr & observer) throw()
 {
-    observer_->set_event_description(_("Beginning to Import Photos"));
-
     photoImportBegin_.emit();
 #if 0
     for (TagList::const_iterator it = tags.begin();
@@ -189,7 +185,6 @@ Engine::import(const IPhotoSourcePtr & source,
 #endif
     PhotoList imp_photos = source->import(selected_storage, tags,
                                           database_, observer);
-    observer->reset();
     photoImportEnd_.emit();
 
     {
@@ -256,7 +251,6 @@ Engine::export_photos(const IPhotoDestinationPtr & destination,
     photoExportBegin_.emit();
     destination->export_photos(exportQueue_, selected_storage,
                                observer);
-    observer->reset();
     photoExportEnd_.emit();
 
     return;
@@ -327,8 +321,9 @@ Engine::create_renderable_list_from_photos(
 
     if( obs )
     {
-        obs->set_num_events( photos.size() );
-        obs->set_event_description( "Updating path info" );
+        observer->set_event_description(_("Updating path information"));
+        observer->set_num_events(photos.size());
+        observer->set_current_events(0);
     }
 
     for( PhotoList::const_iterator photo = photos.begin();
@@ -348,6 +343,11 @@ Engine::create_renderable_list_from_photos(
         }
     }
 
+    if (0 != obs)
+    {
+        obs->reset();
+    }
+
     return photos;
 }
 
diff --git a/src/application/main-window.cpp b/src/application/main-window.cpp
index 5595a32..920d1da 100644
--- a/src/application/main-window.cpp
+++ b/src/application/main-window.cpp
@@ -346,7 +346,7 @@ MainWindow::MainWindow() throw() :
 
     statusBar_.set_has_resize_grip(true);
     vBox_.pack_start(statusBar_, Gtk::PACK_SHRINK, 0);
-    statusBar_.pack_start( progress_, Gtk::PACK_EXPAND_WIDGET, 0);
+    statusBar_.pack_start(progress_, Gtk::PACK_SHRINK, 0);
 
     show_all_children();
 }
@@ -874,19 +874,38 @@ MainWindow::connect_progress( const ProgressObserverPtr &observer ) throw()
 {
     observer_ = observer;
     progress_.set_fraction( 0.0L );
-    observer_->progress().connect( sigc::mem_fun(*this,
-        &MainWindow::handle_progress_event)    );
+
+    observer_->progress().connect(
+        sigc::mem_fun(*this,
+                      &MainWindow::on_progress));
+
+    observer_->dispatcher_reset().connect(
+        sigc::mem_fun(*this,
+                      &MainWindow::on_reset));
 }
 
 void
-MainWindow::handle_progress_event() throw()
+MainWindow::on_progress() throw()
 {
-    double percentage =
-                static_cast<double>(
-                    observer_->get_current_events())
-                / static_cast<double>(
-                    observer_->get_num_events()) ;
+    const guint64 num_events = observer_->get_num_events();
+    if (0 == num_events)
+    {
+        return;
+    }
+
+    const double percentage =
+        static_cast<double>(observer_->get_current_events())
+        / static_cast<double>(num_events);
+
     progress_.set_fraction( percentage );
+    progress_.show();
+}
+
+void
+MainWindow::on_reset() throw()
+{
+    progress_.hide();
+    progress_.set_fraction(0.0);
 }
 
 } // namespace Solang
diff --git a/src/application/main-window.h b/src/application/main-window.h
index d9364cd..4ddc4d0 100644
--- a/src/application/main-window.h
+++ b/src/application/main-window.h
@@ -116,10 +116,13 @@ class MainWindow :
         virtual bool
         on_delete_event(GdkEventAny * event);
 
-        ApplicationPtr application_;
+        void
+        on_progress() throw();
 
         void
-        handle_progress_event() throw();
+        on_reset() throw();
+
+        ApplicationPtr application_;
 
         ActionGroupPtr actionGroup_;
 
diff --git a/src/common/progress-dialog.cpp b/src/common/progress-dialog.cpp
index 80ee038..8b45773 100644
--- a/src/common/progress-dialog.cpp
+++ b/src/common/progress-dialog.cpp
@@ -58,6 +58,10 @@ ProgressDialog::ProgressDialog(const ProgressObserverPtr & observer)
 
     observer_->progress().connect(sigc::mem_fun(*this,
         &ProgressDialog::on_progress));
+
+    observer_->dispatcher_reset().connect(
+        sigc::mem_fun(*this,
+                      &ProgressDialog::on_reset));
 }
 
 ProgressDialog::~ProgressDialog() throw()
@@ -102,15 +106,20 @@ ProgressDialog::on_observer_description_changed() throw()
 void
 ProgressDialog::on_progress() throw()
 {
-    std::ostringstream sout;
+    const guint64 num_events = observer_->get_num_events();
+    if (0 == num_events)
+    {
+        return;
+    }
 
+    std::ostringstream sout;
     sout << observer_->get_current_events() << " of "
-         << observer_->get_num_events() << " completed";
+         << num_events << " completed";
 
     progressBar_.set_text(sout.str());
     progressBar_.set_fraction(
         static_cast<double>(observer_->get_current_events())
-        / static_cast<double>(observer_->get_num_events()));
+        / static_cast<double>(num_events));
 }
 
 void
@@ -121,7 +130,7 @@ ProgressDialog::on_response(int response_id)
 }
 
 void
-ProgressDialog::reset() throw()
+ProgressDialog::on_reset() throw()
 {
     primaryLabel_.set_markup("");
     progressBar_.set_fraction(0.0);
diff --git a/src/common/progress-dialog.h b/src/common/progress-dialog.h
index 51ecf44..edd5d8f 100644
--- a/src/common/progress-dialog.h
+++ b/src/common/progress-dialog.h
@@ -45,9 +45,6 @@ class ProgressDialog :
         void
         set_progress_title();
 
-        void
-        reset() throw();
-
     protected:
         virtual bool
         on_delete_event(GdkEventAny * event);
@@ -61,6 +58,9 @@ class ProgressDialog :
         void
         on_progress() throw();
 
+        void
+        on_reset() throw();
+
         ProgressObserverPtr observer_;
 
         Gtk::Label primaryLabel_;
diff --git a/src/common/progress-observer.cpp b/src/common/progress-observer.cpp
index ad23a34..c612a78 100644
--- a/src/common/progress-observer.cpp
+++ b/src/common/progress-observer.cpp
@@ -63,6 +63,13 @@ ProgressObserver::~ProgressObserver() throw()
 {
 }
 
+Glib::Dispatcher &
+ProgressObserver::dispatcher_reset() throw()
+{
+    Glib::Mutex::Lock lock(mutex_);
+    return reset_;
+}
+
 void
 ProgressObserver::set_num_events(guint64 num_events) throw()
 {
@@ -113,7 +120,7 @@ ProgressObserver::reset() throw()
     currentEvents_ = 0;
     numEvents_ = 0;
     stop_ = false;
-    progress_.emit();
+    reset_.emit();
 }
 
 } // namespace Solang
diff --git a/src/common/progress-observer.h b/src/common/progress-observer.h
index 187a261..2808548 100644
--- a/src/common/progress-observer.h
+++ b/src/common/progress-observer.h
@@ -72,6 +72,9 @@ class ProgressObserver
         inline Glib::Dispatcher &
         progress() throw();
 
+        Glib::Dispatcher &
+        dispatcher_reset() throw();
+
         void 
         reset() throw();
 
@@ -84,6 +87,8 @@ class ProgressObserver
 
         Glib::Dispatcher progress_;
 
+        Glib::Dispatcher reset_;
+
         mutable Glib::Mutex mutex_;
 
         Glib::ustring eventDescription_;
diff --git a/src/database/database.cpp b/src/database/database.cpp
index dcc263e..f2a3635 100644
--- a/src/database/database.cpp
+++ b/src/database/database.cpp
@@ -25,6 +25,8 @@
 #include <sstream>
 #include <vector>
 
+#include <glibmm/i18n.h>
+
 #include "database.h"
 #include "db-table-factory.h"
 #include "progress-observer.h"
@@ -132,24 +134,40 @@ void
 Database::save( const DBObjectList &objects,
                 ProgressObserverPtr &observer ) throw(Error)
 {
-    observer->set_num_events( objects.size() );
-    observer->set_event_description( "Saving objects" );
+    if (0 != observer)
+    {
+        observer->set_event_description(_("Saving objects"));
+        observer->set_num_events(objects.size());
+        observer->set_current_events(0);
+    }
+
     for( DBObjectList::const_iterator it = objects.begin(); 
                                         it != objects.end(); it++ )
     {
-        if( !observer->get_stop() )
+        if (0 != observer && true == observer->get_stop())
         {
-            try
-            {
-                (*it)->save( *this );
-                observer->receive_event_notifiation();    
-            }
-            catch(Error &e)
-            {
-                e.add_call_info( __FUNCTION__, __FILE__, __LINE__ );
-                throw;
-            }
+            break;
         }
+
+        try
+        {
+            (*it)->save( *this );
+        }
+        catch(Error &e)
+        {
+            e.add_call_info( __FUNCTION__, __FILE__, __LINE__ );
+            throw;
+        }
+
+        if (0 != observer)
+        {
+            observer->receive_event_notifiation();
+        }
+    }
+
+    if (0 != observer)
+    {
+        observer->reset();
     }
 }
 
@@ -256,21 +274,38 @@ Database::search(const PhotoSearchCriteriaList & criterion,
         const gint32 numRows
                          = static_cast<gint32>(model->get_n_rows());
 
-        observer->set_num_events( numRows );
-        observer->set_event_description( "Generating list of photos" );
+        if (0 != observer)
+        {
+            observer->set_event_description(
+                          _("Generating list of photos"));
+            observer->set_num_events(numRows);
+            observer->set_current_events(0);
+        }
+
         DBTablePtr table = getTable( "photos" );
 
         for( gint32 row = 0; row < numRows; row++ )
         {
-            if( !observer->get_stop() )
+            if (0 != observer && true == observer->get_stop())
+            {
+                break;
+            }
+
+            PhotoPtr photo( new Photo() );
+            photo->create( model, row );
+            photo->set_table( table );
+            photos.push_back( photo );
+
+            if (0 != observer)
             {
-                PhotoPtr photo( new Photo() );
-                photo->create( model, row );
-                photo->set_table( table );
-                photos.push_back( photo );
                 observer->receive_event_notifiation();
             }
         }
+
+        if (0 != observer)
+        {
+            observer->reset();
+        }
     }
     catch(Glib::Error &e)
     {
@@ -337,24 +372,38 @@ Database::get_dates_with_picture_count(
         const gint32 numRows
                          = static_cast<gint32>(model->get_n_rows());
 
-        observer->set_num_events( numRows );
-        observer->set_event_description(
-                            "Generating summary of photos" );
+        if (0 != observer)
+        {
+            observer->set_event_description(_("Summarizing photos"));
+            observer->set_num_events(numRows);
+            observer->set_current_events(0);
+        }
 
         for( gint32 row = 0; row < numRows; row++ )
         {
-            if( !observer->get_stop() )
+            if (0 != observer && true == observer->get_stop())
+            {
+                break;
+            }
+
+            infos.push_back(
+                DatePhotoInfo(
+                    ModificationDate(
+                        model->get_value_at( 0, row ).get_int(),
+                        model->get_value_at( 1, row ).get_int(),
+                        model->get_value_at( 2, row ).get_int()),
+                    model->get_value_at( 3, row ).get_int()));
+
+            if (0 != observer)
             {
-                infos.push_back(
-                    DatePhotoInfo(
-                        ModificationDate(
-                            model->get_value_at( 0, row ).get_int(),
-                            model->get_value_at( 1, row ).get_int(),
-                            model->get_value_at( 2, row ).get_int()),
-                        model->get_value_at( 3, row ).get_int()));
                 observer->receive_event_notifiation();
             }
         }
+
+        if (0 != observer)
+        {
+            observer->reset();
+        }
     }
     catch (Glib::Error &e)
     {
diff --git a/src/exporter/directory-destination.cpp b/src/exporter/directory-destination.cpp
index d085e47..dc42a6f 100644
--- a/src/exporter/directory-destination.cpp
+++ b/src/exporter/directory-destination.cpp
@@ -92,8 +92,6 @@ DirectoryDestination::export_photo(
     {
         g_warning("%s", e.what().c_str());
     }
-
-    observer->receive_event_notifiation();
 }
 
 void
@@ -103,8 +101,12 @@ DirectoryDestination::export_photos(
                           const ProgressObserverPtr & observer)
                           throw()
 {
-    observer->set_event_description(_("Exporting Photos"));
-    observer->set_num_events(photos.size());
+    if (0 != observer)
+    {
+        observer->set_event_description(_("Exporting photos"));
+        observer->set_num_events(photos.size());
+        observer->set_current_events(0);
+    }
 
     if (true == createArchive_)
     {
@@ -135,6 +137,11 @@ DirectoryDestination::export_photos(
     for (it = photos.begin(); photos.end() != it; it++)
     {
         export_photo(*it, storage, observer);
+
+        if (0 != observer)
+        {
+            observer->receive_event_notifiation();
+        }
     }
 
     if (true == createArchive_)
@@ -147,6 +154,11 @@ DirectoryDestination::export_photos(
         g_remove(filename_.c_str());
     }
 
+    if (0 != observer)
+    {
+        observer->reset();
+    }
+
     return;
 }
 
diff --git a/src/importer/camera-source.cpp b/src/importer/camera-source.cpp
index b07af06..04a2623 100644
--- a/src/importer/camera-source.cpp
+++ b/src/importer/camera-source.cpp
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <fcntl.h>
 
+#include <glibmm/i18n.h>
 #include <gphoto2.h>
 
 #include "camera-source.h"
@@ -126,8 +127,12 @@ CameraSource::import(const PhotoList & photos,
                      const TagList &tags, Database & db,
                      const ProgressObserverPtr & observer) throw()
 {
-    observer->set_num_events(photos.size());
-    observer->set_event_description("Importing photos");
+    if (0 != observer)
+    {
+        observer->set_event_description(_("Importing photos"));
+        observer->set_num_events(photos.size());
+        observer->set_current_events(0);
+    }
 
     PhotoList imported_photos;
     for (PhotoList::const_iterator it = photos.begin();
@@ -143,7 +148,16 @@ CameraSource::import(const PhotoList & photos,
         PhotoPtr photo = (*it);
         imported_photos.push_back(import(photo, storage, tags, db,
                                          observer));
-        observer->receive_event_notifiation();
+
+        if (0 != observer)
+        {
+            observer->receive_event_notifiation();
+        }
+    }
+
+    if (0 != observer)
+    {
+        observer->reset();
     }
 
     return imported_photos;
diff --git a/src/importer/directory-source.cpp b/src/importer/directory-source.cpp
index b21978d..7697330 100644
--- a/src/importer/directory-source.cpp
+++ b/src/importer/directory-source.cpp
@@ -90,8 +90,12 @@ DirectorySource::import(const PhotoList & photos,
                         const TagList & tags, Database & db,
                         const ProgressObserverPtr & observer) throw()
 {
-    observer->set_event_description(_("Importing Photos"));
-    observer->set_num_events(photos.size());
+    if (0 != observer)
+    {
+        observer->set_event_description(_("Importing photos"));
+        observer->set_num_events(photos.size());
+        observer->set_current_events(0);
+    }
 
     PhotoList imported_photos;
     for (PhotoList::const_iterator it = photos.begin();
@@ -107,7 +111,16 @@ DirectorySource::import(const PhotoList & photos,
         PhotoPtr photo = (*it);
         imported_photos.push_back(import(photo, storage, tags, db,
                                          observer));
-        observer->receive_event_notifiation();
+
+        if (0 != observer)
+        {
+            observer->receive_event_notifiation();
+        }
+    }
+
+    if (0 != observer)
+    {
+        observer->reset();
     }
 
     return imported_photos;
diff --git a/src/importer/flickr-source.cpp b/src/importer/flickr-source.cpp
index ebb63ee..82bbc1d 100644
--- a/src/importer/flickr-source.cpp
+++ b/src/importer/flickr-source.cpp
@@ -435,8 +435,9 @@ FlickrSource::import(const PhotoList & photos,
 {
     if (0 != observer)
     {
-        observer->set_event_description(_("Importing Photos"));
+        observer->set_event_description(_("Importing photos"));
         observer->set_num_events(photos.size());
+        observer->set_current_events(0);
     }
 
     PhotoList imported_photos;
@@ -456,7 +457,16 @@ FlickrSource::import(const PhotoList & photos,
                                       observer);
 
         imported_photos.push_back(photo);
-        observer->receive_event_notifiation();
+
+        if (0 != observer)
+        {
+            observer->receive_event_notifiation();
+        }
+    }
+
+    if (0 != observer)
+    {
+        observer->reset();
     }
 
     return imported_photos;
@@ -485,8 +495,9 @@ FlickrSource::download_photos(PhotoList & files,
 {
     if (0 != observer)
     {
-        observer->set_event_description(_("Downloading Photos"));
+        observer->set_event_description(_("Downloading photos"));
         observer->set_num_events(uris_.size());
+        observer->set_current_events(0);
     }
 
     const std::string cache_dir_path = Glib::get_user_cache_dir()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]