root / trunk / client / gui / monitor.c @ 1715
History | View | Annotate | Download (6.2 KB)
| 1 | 
                  /* 
                 | 
              
|---|---|
| 2 | 
                  Copyright (C) 2005-2010 Erik van Pienbroek  | 
              
| 3 | 
                   | 
              
| 4 | 
                  This program is free software; you can redistribute it and/or modify  | 
              
| 5 | 
                  it under the terms of the GNU General Public License as published by  | 
              
| 6 | 
                  the Free Software Foundation; either version 2 of the License, or  | 
              
| 7 | 
                  (at your option) any later version.  | 
              
| 8 | 
                   | 
              
| 9 | 
                  This program is distributed in the hope that it will be useful,  | 
              
| 10 | 
                  but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
              
| 11 | 
                  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  | 
              
| 12 | 
                  GNU General Public License for more details.  | 
              
| 13 | 
                   | 
              
| 14 | 
                  You should have received a copy of the GNU General Public License  | 
              
| 15 | 
                  along with this program; if not, write to the Free Software  | 
              
| 16 | 
                  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  | 
              
| 17 | 
                  */  | 
              
| 18 | 
                   | 
              
| 19 | 
                  #include  | 
              
| 20 | 
                  #include  | 
              
| 21 | 
                   | 
              
| 22 | 
                  #include "gui.h"  | 
              
| 23 | 
                  #include "nntpgrab_utils.h"  | 
              
| 24 | 
                   | 
              
| 25 | 
                  static int bytes_received[10];  | 
              
| 26 | 
                  static time_t stamp = 0;  | 
              
| 27 | 
                  static double average_speed = 0;  | 
              
| 28 | 
                   | 
              
| 29 | 
                  #if !GTK_CHECK_VERSION(2,14,0)  | 
              
| 30 | 
                  #define gtk_widget_get_window(x) x->window
                 | 
              
| 31 | 
                  #endif
                 | 
              
| 32 | 
                   | 
              
| 33 | 
                  static gboolean
                 | 
              
| 34 | 
                  expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)  | 
              
| 35 | 
                  {
                 | 
              
| 36 | 
                  static PangoFontDescription *font_description = NULL;  | 
              
| 37 | 
                  PangoLayout *layout;  | 
              
| 38 | 
                  cairo_t *cr;  | 
              
| 39 | 
                      int i;
                 | 
              
| 40 | 
                  int max = 0;  | 
              
| 41 | 
                      int width;
                 | 
              
| 42 | 
                      int height;
                 | 
              
| 43 | 
                      double y_factor;
                 | 
              
| 44 | 
                      int y_offset;
                 | 
              
| 45 | 
                      double x, y;
                 | 
              
| 46 | 
                  char msg[64];  | 
              
| 47 | 
                  char avg_speed[64];  | 
              
| 48 | 
                      int text_width;
                 | 
              
| 49 | 
                   | 
              
| 50 | 
                  #if GTK_CHECK_VERSION(2,18,0)  | 
              
| 51 | 
                  GtkAllocation allocation;  | 
              
| 52 | 
                   | 
              
| 53 | 
                  cr = gdk_cairo_create(GDK_DRAWABLE(gtk_widget_get_window(widget)));  | 
              
| 54 | 
                   | 
              
| 55 | 
                  gtk_widget_get_allocation(widget, &allocation);  | 
              
| 56 | 
                  width = allocation.width;  | 
              
| 57 | 
                  height = allocation.height - (allocation.height / 10); // Take 10% off the top  | 
              
| 58 | 
                  y_offset = allocation.height / 10; // 10%  | 
              
| 59 | 
                  #else
                 | 
              
| 60 | 
                  cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));  | 
              
| 61 | 
                   | 
              
| 62 | 
                  width = widget->allocation.width;  | 
              
| 63 | 
                  height = widget->allocation.height - (widget->allocation.height / 10); // Take 10% off the top  | 
              
| 64 | 
                  y_offset = widget->allocation.height / 10; // 10%  | 
              
| 65 | 
                  #endif
                 | 
              
| 66 | 
                   | 
              
| 67 | 
                      /* If this is the first time we got here, initialize the font */
                 | 
              
| 68 | 
                      if (!font_description) {
                 | 
              
| 69 | 
                  font_description = pango_font_description_new ();  | 
              
| 70 | 
                          //pango_font_description_set_family (font_description, "serif");
                 | 
              
| 71 | 
                          //pango_font_description_set_weight (font_description, PANGO_WEIGHT_BOLD);
                 | 
              
| 72 | 
                          pango_font_description_set_absolute_size (font_description, 12 * PANGO_SCALE);
                 | 
              
| 73 | 
                  }  | 
              
| 74 | 
                   | 
              
| 75 | 
                      // Calculate the peak value
                 | 
              
| 76 | 
                  for (i = 0; i < 10; i++) {  | 
              
| 77 | 
                          if (bytes_received[i] > max) {
                 | 
              
| 78 | 
                  max = bytes_received[i];  | 
              
| 79 | 
                  }  | 
              
| 80 | 
                  }  | 
              
| 81 | 
                   | 
              
| 82 | 
                      // Draw the background
                 | 
              
| 83 | 
                  cairo_set_line_width (cr, 1.0);  | 
              
| 84 | 
                  cairo_set_source_rgb (cr, 255, 255, 255);  | 
              
| 85 | 
                  #if GTK_CHECK_VERSION(2,18,0)  | 
              
| 86 | 
                  cairo_rectangle (cr, 0, 0, width, allocation.height);  | 
              
| 87 | 
                  #else
                 | 
              
| 88 | 
                  cairo_rectangle (cr, 0, 0, width, widget->allocation.height);  | 
              
| 89 | 
                  #endif
                 | 
              
| 90 | 
                  cairo_fill (cr);  | 
              
| 91 | 
                   | 
              
| 92 | 
                  if (max == 0) {  | 
              
| 93 | 
                          // Nothing to draw yet
                 | 
              
| 94 | 
                  cairo_destroy(cr);  | 
              
| 95 | 
                          return TRUE;
                 | 
              
| 96 | 
                  }  | 
              
| 97 | 
                   | 
              
| 98 | 
                  y_factor = height;  | 
              
| 99 | 
                  y_factor /= max;  | 
              
| 100 | 
                   | 
              
| 101 | 
                  cairo_set_source_rgb (cr, 0.565, 0.7, 0.87);  | 
              
| 102 | 
                   | 
              
| 103 | 
                      // Create a new path in which all the coordinates are defined
                 | 
              
| 104 | 
                  cairo_new_path(cr);  | 
              
| 105 | 
                   | 
              
| 106 | 
                      // Use the first coordinate as starting point
                 | 
              
| 107 | 
                      y = bytes_received[0];
                 | 
              
| 108 | 
                  y *= y_factor;  | 
              
| 109 | 
                  y = height - y;  | 
              
| 110 | 
                  y += y_offset;  | 
              
| 111 | 
                   | 
              
| 112 | 
                      cairo_move_to (cr, 0, y);
                 | 
              
| 113 | 
                   | 
              
| 114 | 
                      // Create a path through the rest of the coordinates
                 | 
              
| 115 | 
                  for (i = 1; i < 10; i++) {  | 
              
| 116 | 
                  x = width;  | 
              
| 117 | 
                          x /= 9;
                 | 
              
| 118 | 
                  x *= i;  | 
              
| 119 | 
                   | 
              
| 120 | 
                  y = bytes_received[i];  | 
              
| 121 | 
                  y *= y_factor;  | 
              
| 122 | 
                  y = height - y;  | 
              
| 123 | 
                  y += y_offset;  | 
              
| 124 | 
                   | 
              
| 125 | 
                  cairo_line_to (cr, x, y);  | 
              
| 126 | 
                  }  | 
              
| 127 | 
                   | 
              
| 128 | 
                      // Finalize the path by going to the bottom right corner
                 | 
              
| 129 | 
                  cairo_line_to(cr, width, height + y_offset);  | 
              
| 130 | 
                   | 
              
| 131 | 
                      // and to the bottom right corner
                 | 
              
| 132 | 
                      cairo_line_to(cr, 0, height + y_offset);
                 | 
              
| 133 | 
                   | 
              
| 134 | 
                      // Connect back to the first coordinate
                 | 
              
| 135 | 
                  cairo_close_path(cr);  | 
              
| 136 | 
                   | 
              
| 137 | 
                      // Fill the path
                 | 
              
| 138 | 
                  cairo_set_source_rgb (cr, 0.565, 0.7, 0.87);  | 
              
| 139 | 
                  cairo_fill (cr);  | 
              
| 140 | 
                   | 
              
| 141 | 
                      // Draw the average speed
                 | 
              
| 142 | 
                  memset(&avg_speed, 0, sizeof(avg_speed));  | 
              
| 143 | 
                  memset(&msg, 0, sizeof(msg));  | 
              
| 144 | 
                  nntpgrab_utils_calculate_file_size((guint64) average_speed, avg_speed, sizeof(avg_speed) - 1);  | 
              
| 145 | 
                  snprintf(msg, sizeof(msg) - 1, "%s/sec", avg_speed);  | 
              
| 146 | 
                   | 
              
| 147 | 
                  layout = pango_cairo_create_layout (cr);  | 
              
| 148 | 
                  pango_layout_set_font_description (layout, font_description);  | 
              
| 149 | 
                      pango_layout_set_text (layout, msg, -1);
                 | 
              
| 150 | 
                   | 
              
| 151 | 
                  cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);  | 
              
| 152 | 
                      text_width = 0;
                 | 
              
| 153 | 
                      pango_layout_get_pixel_size(layout, &text_width, NULL);
                 | 
              
| 154 | 
                  cairo_move_to (cr, width - text_width - 5, height - 5);  | 
              
| 155 | 
                  pango_cairo_show_layout (cr, layout);  | 
              
| 156 | 
                   | 
              
| 157 | 
                  g_object_unref (layout);  | 
              
| 158 | 
                   | 
              
| 159 | 
                  cairo_destroy(cr);  | 
              
| 160 | 
                   | 
              
| 161 | 
                      return TRUE;
                 | 
              
| 162 | 
                  }  | 
              
| 163 | 
                   | 
              
| 164 | 
                  static void  | 
              
| 165 | 
                  traffic_monitor_update_cb (NNTPGrabGlue *glue, int bytes_received1, int bytes_received2, int bytes_received3, int bytes_received4, int bytes_received5, int bytes_received6, int bytes_received7, int bytes_received8, int bytes_received9, int bytes_received10, guint64 last_update_stamp, double average)  | 
              
| 166 | 
                  {
                 | 
              
| 167 | 
                  static GtkWidget *windowMain = NULL;  | 
              
| 168 | 
                  static GtkWidget *drawingareaSpeed = NULL;  | 
              
| 169 | 
                   | 
              
| 170 | 
                      if (!windowMain) {
                 | 
              
| 171 | 
                          windowMain = nntpgrab_gui_base_get_widget("windowMain");
                 | 
              
| 172 | 
                  }  | 
              
| 173 | 
                   | 
              
| 174 | 
                      if (!drawingareaSpeed) {
                 | 
              
| 175 | 
                          drawingareaSpeed = nntpgrab_gui_base_get_widget("drawingareaSpeed");
                 | 
              
| 176 | 
                  }  | 
              
| 177 | 
                   | 
              
| 178 | 
                      bytes_received[0] = bytes_received1;
                 | 
              
| 179 | 
                      bytes_received[1] = bytes_received2;
                 | 
              
| 180 | 
                      bytes_received[2] = bytes_received3;
                 | 
              
| 181 | 
                      bytes_received[3] = bytes_received4;
                 | 
              
| 182 | 
                      bytes_received[4] = bytes_received5;
                 | 
              
| 183 | 
                      bytes_received[5] = bytes_received6;
                 | 
              
| 184 | 
                      bytes_received[6] = bytes_received7;
                 | 
              
| 185 | 
                      bytes_received[7] = bytes_received8;
                 | 
              
| 186 | 
                      bytes_received[8] = bytes_received9;
                 | 
              
| 187 | 
                      bytes_received[9] = bytes_received10;
                 | 
              
| 188 | 
                   | 
              
| 189 | 
                  stamp = last_update_stamp;  | 
              
| 190 | 
                  average_speed = average;  | 
              
| 191 | 
                   | 
              
| 192 | 
                      if (gdk_window_get_state(gtk_widget_get_window(windowMain)) != GDK_WINDOW_STATE_ICONIFIED) {
                 | 
              
| 193 | 
                          gdk_window_invalidate_rect(gtk_widget_get_window(drawingareaSpeed), NULL, FALSE);
                 | 
              
| 194 | 
                  }  | 
              
| 195 | 
                  }  | 
              
| 196 | 
                   | 
              
| 197 | 
                  void
                 | 
              
| 198 | 
                  monitor_initialize(void)
                 | 
              
| 199 | 
                  {
                 | 
              
| 200 | 
                      GtkWidget *drawingareaSpeed = nntpgrab_gui_base_get_widget("drawingareaSpeed");
                 | 
              
| 201 | 
                   | 
              
| 202 | 
                  g_signal_connect (G_OBJECT (drawingareaSpeed), "expose_event", G_CALLBACK (expose_event_callback), NULL);  | 
              
| 203 | 
                   | 
              
| 204 | 
                  ng_signal_connect(glue, "traffic_monitor_update", NG_CALLBACK(traffic_monitor_update_cb), NULL);  | 
              
| 205 | 
                  }  | 
              
NNTPGrab

