[pan2/download-meter: 1/3] added download meter (rough basic impl)



commit f86fcfcc7e58152de0eb7f925c85a9e1c03669b6
Author: Heinrich MÃller <henmull src gnome org>
Date:   Sun Sep 23 19:15:15 2012 +0200

    added download meter (rough basic impl)

 pan/gui/Makefile.am              |    6 +++-
 pan/gui/download-meter.cc        |   50 ++++++++++++++++++++++++++++++++++++++
 pan/gui/gui.cc                   |   13 ++++++++-
 pan/gui/gui.h                    |   11 ++++++--
 pan/gui/pan.cc                   |    5 ++-
 pan/tasks/nntp-pool.cc           |   10 ++++---
 pan/tasks/nntp-pool.h            |   12 ++++----
 pan/tasks/nntp.cc                |    7 +++++
 pan/tasks/nntp.h                 |   12 +++++++-
 pan/tasks/queue.cc               |    4 ++-
 pan/tasks/queue.h                |    3 +-
 pan/tasks/socket-impl-gio.cc     |    1 +
 pan/tasks/socket-impl-openssl.cc |    1 +
 pan/tasks/socket.h               |    5 +++-
 14 files changed, 116 insertions(+), 24 deletions(-)
---
diff --git a/pan/gui/Makefile.am b/pan/gui/Makefile.am
index 67c0090..93673f5 100644
--- a/pan/gui/Makefile.am
+++ b/pan/gui/Makefile.am
@@ -35,7 +35,8 @@ libpangui_a_SOURCES = \
  server-ui.cc \
  task-pane.cc \
  xface.c \
- url.cc
+ url.cc \
+ download-meter.cc
 
 noinst_HEADERS = \
  action-manager.h \
@@ -84,7 +85,8 @@ noinst_HEADERS = \
  url.h \
  wait.h \
  xface.h \
- pan-colors.h
+ pan-colors.h \
+ download-meter.h
 
 EXTRA_DIST = \
  panrc.rc
diff --git a/pan/gui/download-meter.cc b/pan/gui/download-meter.cc
new file mode 100644
index 0000000..2791a0a
--- /dev/null
+++ b/pan/gui/download-meter.cc
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Pan - A Newsreader for Gtk+
+ * Copyright (C) 2002-2006  Charles Kerr <charles rebelbase com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <config.h>
+#include "download-meter.h"
+#include <iostream>
+
+using namespace pan;
+
+DownloadMeter :: DownloadMeter () : _val(0ul) {}
+
+void
+DownloadMeter :: add (unsigned long bytes)
+{
+  if (bytes > 0)
+  {
+    _val += bytes;
+    fire_xfer_bytes(bytes);
+  }
+
+}
+
+void
+DownloadMeter :: reset ()
+{
+  _val = 0ul;
+}
+
+void
+DownloadMeter :: fire_xfer_bytes (unsigned long bytes)
+{
+  for (lit it(_listeners.begin()), end(_listeners.end()); it!=end; )
+    (*it++)->on_xfer_bytes (bytes);
+}
diff --git a/pan/gui/gui.cc b/pan/gui/gui.cc
index 528a73b..8ca6acb 100644
--- a/pan/gui/gui.cc
+++ b/pan/gui/gui.cc
@@ -207,7 +207,7 @@ GUI :: root_realized_cb (GtkWidget*, gpointer self_gpointer)
   }
 }
 
-GUI :: GUI (Data& data, Queue& queue, Prefs& prefs, GroupPrefs& group_prefs):
+GUI :: GUI (Data& data, Queue& queue, Prefs& prefs, GroupPrefs& group_prefs, DownloadMeter& meter):
   _data (data),
   _queue (queue),
   _prefs (prefs),
@@ -227,7 +227,8 @@ GUI :: GUI (Data& data, Queue& queue, Prefs& prefs, GroupPrefs& group_prefs):
   _queue_size_label (0),
   _queue_size_button (0),
   _taskbar (0),
-  _certstore(data.get_certstore())
+  _certstore(data.get_certstore()),
+  _meter(meter)
 {
 
   char * filename = g_build_filename (file::get_pan_home().c_str(), "pan.ui", NULL);
@@ -364,6 +365,7 @@ GUI :: GUI (Data& data, Queue& queue, Prefs& prefs, GroupPrefs& group_prefs):
   _certstore.add_listener(this);
   Log::get().add_listener (this);
   _data.add_listener (this);
+  _meter.add_listener (this);
 
   gtk_accel_map_load (get_accel_filename().c_str());
 
@@ -454,6 +456,7 @@ GUI :: ~GUI ()
   _prefs.remove_listener (this);
   _queue.remove_listener (this);
   Log::get().remove_listener (this);
+  _meter.remove_listener(this);
 
 }
 
@@ -985,6 +988,12 @@ void GUI :: on_progress_finished (Progress & p, int status)
   }
 }
 
+void GUI :: on_xfer_bytes (unsigned long bytes)
+{
+  // todo set new "progress" for download meter
+  std::cerr<<bytes<<"\n";
+}
+
 void GUI :: do_read_selected_article ()
 {
   const Article* article (_header_pane->get_first_selected_article ());
diff --git a/pan/gui/gui.h b/pan/gui/gui.h
index ff3cfd2..aab8a46 100644
--- a/pan/gui/gui.h
+++ b/pan/gui/gui.h
@@ -32,7 +32,7 @@
 #include <pan/gui/prefs.h>
 #include <pan/gui/group-prefs.h>
 #include <pan/gui/wait.h>
-//#include <pan/general/typedefs.h>
+#include <pan/gui/download-meter.h>
 
 #include "gtk-compat.h"
 
@@ -56,14 +56,15 @@ namespace pan
     private Queue::Listener,
     private Prefs::Listener,
     private CertStore::Listener,
-    private Data::Listener
+    private Data::Listener,
+    private DownloadMeter::Listener
   {
 
     public:
 
       typedef std::vector<Quark> mid_sequence_t;
 
-      GUI (Data& data, Queue&, Prefs&, GroupPrefs&);
+      GUI (Data& data, Queue&, Prefs&, GroupPrefs&, DownloadMeter&);
       virtual ~GUI ();
       GtkWidget* root () { return _root; }
       typedef std::vector<std::string> strings_t;
@@ -224,6 +225,9 @@ namespace pan
     private: // Progress::Listener
       virtual void on_progress_finished (Progress&, int status);
 
+    private: // downloadmeter::listener
+      virtual void on_xfer_bytes (unsigned long);
+
     public: // WaitUI
       virtual void watch_cursor_on ();
       virtual void watch_cursor_off ();
@@ -239,6 +243,7 @@ namespace pan
       Prefs& _prefs;
       GroupPrefs& _group_prefs;
       CertStore& _certstore;
+      DownloadMeter& _meter;
 
     private:
       GtkWidget * _root;
diff --git a/pan/gui/pan.cc b/pan/gui/pan.cc
index 5eaea8c..8c6cc54 100644
--- a/pan/gui/pan.cc
+++ b/pan/gui/pan.cc
@@ -967,7 +967,8 @@ main (int argc, char *argv[])
     // instantiate the queue...
     WorkerPool worker_pool (4, true);
     SocketCreator socket_creator(data, certstore);
-    Queue queue (data, data, &socket_creator, certstore, prefs, worker_pool, false, 32768);
+    DownloadMeter meter;
+    Queue queue (data, data, &socket_creator, certstore, prefs, worker_pool, meter, false, 32768);
 
     data.set_queue (&queue);
 
@@ -1076,7 +1077,7 @@ main (int argc, char *argv[])
       gtk_window_set_default_icon (pixbuf);
 
 
-      gui_ptr = new GUI (data, queue, prefs, group_prefs);
+      gui_ptr = new GUI (data, queue, prefs, group_prefs, meter);
 
 #ifdef HAVE_LIBNOTIFY
       if (!notify_is_initted ())
diff --git a/pan/tasks/nntp-pool.cc b/pan/tasks/nntp-pool.cc
index 17a7c09..2a92309 100644
--- a/pan/tasks/nntp-pool.cc
+++ b/pan/tasks/nntp-pool.cc
@@ -41,7 +41,8 @@ NNTP_Pool :: NNTP_Pool (const Quark        & server,
                         ServerInfo         & server_info,
                         Prefs              & prefs,
                         SocketCreator      * creator,
-                        CertStore          & store):
+                        CertStore          & store,
+                        DownloadMeter      & meter):
 
   _server_info (server_info),
   _prefs (prefs),
@@ -50,7 +51,8 @@ NNTP_Pool :: NNTP_Pool (const Quark        & server,
   _pending_connections (0),
   _active_count (0),
   _time_to_allow_new_connections (0),
-  _certstore(store)
+  _certstore(store),
+  _meter(meter)
 {
 }
 
@@ -191,11 +193,11 @@ NNTP_Pool :: on_socket_created (const StringView  & host,
     {
       std::string pw (pass ? pass : "");
       if (pass) g_free(pass);
-      nntp = new NNTP (_server, user, pw, socket);
+      nntp = new NNTP (_server, user, pw, _meter, socket);
     }
     else
     {
-      nntp = new NNTP (_server, user, pass, socket);
+      nntp = new NNTP (_server, user, pass, _meter, socket);
     }
     nntp->handshake (this);
   }
diff --git a/pan/tasks/nntp-pool.h b/pan/tasks/nntp-pool.h
index 7399b72..2d8a98f 100644
--- a/pan/tasks/nntp-pool.h
+++ b/pan/tasks/nntp-pool.h
@@ -28,9 +28,10 @@
 #include <pan/tasks/nntp.h>
 #include <pan/tasks/socket-impl-main.h>
 #include <pan/gui/prefs.h>
+#include <pan/gui/download-meter.h>
 
 #ifdef HAVE_GNUTLS
-#include <pan/data/cert-store.h>
+  #include <pan/data/cert-store.h>
 #endif
 
 namespace pan {
@@ -48,7 +49,7 @@ class NNTP_Pool: public NNTP::Source,
 public:
 
 	NNTP_Pool(const Quark & server, ServerInfo & server_info, Prefs& prefs, SocketCreator *,
-			CertStore &);
+			CertStore &, DownloadMeter&);
 	virtual ~NNTP_Pool();
 
 	virtual void check_in(NNTP*, Health);
@@ -87,10 +88,8 @@ private:
 
 private:
 	// Socket::Creator::Listener
-	virtual void on_socket_created(const StringView& host, int port, bool ok,
-			Socket*);
-	virtual void on_socket_shutdown(const StringView& host, int port, Socket*) {
-	}
+	virtual void on_socket_created(const StringView& host, int port, bool ok,	Socket*);
+	virtual void on_socket_shutdown(const StringView& host, int port, Socket*) {}
 #ifdef HAVE_GNUTLS
 private:
 	// CertStore::Listener
@@ -116,6 +115,7 @@ private:
 	int _pending_connections;
 	CertStore& _certstore;
 	Prefs& _prefs;
+	DownloadMeter& _meter;
 
 	struct PoolItem {
 		NNTP * nntp;
diff --git a/pan/tasks/nntp.cc b/pan/tasks/nntp.cc
index fc6b560..69ee914 100644
--- a/pan/tasks/nntp.cc
+++ b/pan/tasks/nntp.cc
@@ -26,6 +26,7 @@ extern "C" {
   #include <glib.h>
   #include <glib/gi18n.h>
 }
+#include <pan/gui/download-meter.h>
 #include <pan/general/debug.h>
 #include <pan/general/log.h>
 #include <pan/general/messages.h>
@@ -265,6 +266,12 @@ NNTP :: on_socket_error (Socket * sock UNUSED)
    fire_done_func (ERR_NETWORK, StringView());
 }
 
+void
+NNTP :: on_socket_bytes_transferred (unsigned long bytes, Socket*)
+{
+   _meter.add (bytes);
+}
+
 namespace
 {
    void
diff --git a/pan/tasks/nntp.h b/pan/tasks/nntp.h
index c74ae06..a4fb40c 100644
--- a/pan/tasks/nntp.h
+++ b/pan/tasks/nntp.h
@@ -27,6 +27,7 @@
 #include <pan/general/string-view.h>
 #include <pan/tasks/health.h>
 #include <pan/tasks/socket.h>
+#include <pan/gui/download-meter.h>
 
 namespace
 {
@@ -137,7 +138,8 @@ namespace pan
                                     const Quark        & group         UNUSED,
                                     unsigned long        estimated_qty UNUSED,
                                     uint64_t             low           UNUSED,
-                                    uint64_t             high          UNUSED) {}
+                                    uint64_t             high
+                                           UNUSED) {}
 
        };
 
@@ -146,6 +148,7 @@ namespace pan
         NNTP (const Quark        & server,
               const std::string  & username,
               const std::string  & password,
+              DownloadMeter      & meter,
               Socket         * socket):
           _server(server),
           _socket(socket),
@@ -153,7 +156,8 @@ namespace pan
           _listener(0),
           _username(username),
           _password(password),
-          _nntp_response_text(false)
+          _nntp_response_text(false),
+          _meter(meter)
        {}
 
        virtual ~NNTP ()
@@ -309,6 +313,9 @@ namespace pan
 
     protected:
 
+      /** Listener for download progress */
+      DownloadMeter& _meter;
+
       Listener * _listener;
       /** Kept in case the server gives prompts us for it. */
       const std::string _username;
@@ -331,6 +338,7 @@ namespace pan
       virtual bool on_socket_response (Socket*, const StringView& line);
       virtual void on_socket_error (Socket*);
       virtual void on_socket_abort (Socket*);
+      virtual void on_socket_bytes_transferred (unsigned long bytes, Socket*) ;
 
     public:
 
diff --git a/pan/tasks/queue.cc b/pan/tasks/queue.cc
index cab893f..066efd0 100644
--- a/pan/tasks/queue.cc
+++ b/pan/tasks/queue.cc
@@ -39,6 +39,7 @@ Queue :: Queue (ServerInfo         & server_info,
                 CertStore          & certstore,
                 Prefs              & prefs,
                 WorkerPool         & pool,
+                DownloadMeter      & meter,
                 bool                 online,
                 int                  save_delay_secs):
   _server_info (server_info),
@@ -46,6 +47,7 @@ Queue :: Queue (ServerInfo         & server_info,
   _is_online (online),
   _socket_creator (socket_creator),
   _worker_pool (pool),
+  _meter(meter),
   _decoder (pool),
   _encoder (pool),
   _decoder_task (0),
@@ -106,7 +108,7 @@ Queue :: get_pool (const Quark& servername)
   }
   else // have to build one
   {
-    pool = new NNTP_Pool (servername, _server_info, _prefs, _socket_creator, _certstore);
+    pool = new NNTP_Pool (servername, _server_info, _prefs, _socket_creator, _certstore, _meter);
     pool->add_listener (this);
     _pools[servername] = pool;
   }
diff --git a/pan/tasks/queue.h b/pan/tasks/queue.h
index 04e3b8a..fef98d5 100644
--- a/pan/tasks/queue.h
+++ b/pan/tasks/queue.h
@@ -75,7 +75,7 @@ namespace pan
     private AdaptableSet<Task*, TaskWeakOrdering>::Listener
   {
     public:
-      Queue (ServerInfo&, TaskArchive&, SocketCreator*, CertStore&, Prefs&, WorkerPool&,
+      Queue (ServerInfo&, TaskArchive&, SocketCreator*, CertStore&, Prefs&, WorkerPool&, DownloadMeter&,
              bool online, int save_delay_secs);
       virtual ~Queue ();
 
@@ -255,6 +255,7 @@ namespace pan
       int _uploads_total, _downloads_total;
       CertStore& _certstore;
       Prefs& _prefs;
+      DownloadMeter& _meter;
 
     private:
       typedef AdaptableSet<Task*, TaskWeakOrdering> TaskSet;
diff --git a/pan/tasks/socket-impl-gio.cc b/pan/tasks/socket-impl-gio.cc
index fb2eb36..69e7456 100644
--- a/pan/tasks/socket-impl-gio.cc
+++ b/pan/tasks/socket-impl-gio.cc
@@ -336,6 +336,7 @@ GIOChannelSocket :: do_read ()
       if (g_str_has_suffix (g->str, "\r\n"))
         g_string_truncate (g, g->len-2);
       more = _listener->on_socket_response (this, StringView (g->str, g->len));
+      _listener->on_socket_bytes_transferred(g->len, this);
     }
     else if (status == G_IO_STATUS_AGAIN)
     {
diff --git a/pan/tasks/socket-impl-openssl.cc b/pan/tasks/socket-impl-openssl.cc
index 5a15436..390b0f2 100644
--- a/pan/tasks/socket-impl-openssl.cc
+++ b/pan/tasks/socket-impl-openssl.cc
@@ -618,6 +618,7 @@ GIOChannelSocketGnuTLS :: do_read ()
       if (g_str_has_suffix (g->str, "\r\n"))
         g_string_truncate (g, g->len-2);
       more = _listener->on_socket_response (this, StringView (g->str, g->len));
+      _listener->on_socket_bytes_transferred(g->len, this);
     }
     else if (status == G_IO_STATUS_AGAIN)
     {
diff --git a/pan/tasks/socket.h b/pan/tasks/socket.h
index 6947229..2fed292 100644
--- a/pan/tasks/socket.h
+++ b/pan/tasks/socket.h
@@ -30,6 +30,7 @@
 namespace pan
 {
   class StringView;
+  class Quark;
   class WorkerPool;
   class Data;
 
@@ -49,9 +50,11 @@ namespace pan
       /** Interface class for objects that listen to a Socket's events */
       struct Listener {
         virtual ~Listener () {}
+
         virtual bool on_socket_response (Socket*, const StringView& line) = 0;
         virtual void on_socket_error (Socket*) = 0;
         virtual void on_socket_abort (Socket*) = 0;
+        virtual void on_socket_bytes_transferred (unsigned long bytes, Socket*) = 0;
       };
 
     public:
@@ -90,7 +93,7 @@ namespace pan
           virtual ~Listener () {}
           virtual void on_socket_created (const StringView& host, int port, bool ok, Socket*) = 0;
           virtual void on_socket_shutdown (const StringView& host, int port, Socket*) = 0;
-        };
+      };
 
         virtual ~Creator () { }
         virtual void create_socket (Data&, const StringView& host, int port, WorkerPool&, Listener*, bool) = 0;



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