1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* Copyright (c) 2000 Jason Petrone <jp@demonseed.net>
*
* This code released under the GNU GPL.
* Read the file COPYING for more information.
*
*/
#include <gnome.h>
#include <panel-applet.h>
#include <panel-applet-gconf.h>
#include <time.h>
#define GETTEXT_PACKAGE "netmon_applet"
#include "data.h"
#include "config.h"
guint timeout_handle = -1;
PanelApplet *applet = NULL;
GtkWidget *frame = NULL;
GtkWidget *box = NULL;
GtkWidget *fixed = NULL;
GtkWidget *inlabel, *outlabel;
GdkFont *font;
AppData *ad;
static time_t starttime;
gint netmon_draw(void);
/* void about(PanelApplet *widget, gpointer data); */
void about(BonoboUIComponent *ui, gpointer data, const char *cname);
extern void property_load(PanelApplet *applet, AppData *ad);
extern void property_save(PanelApplet *applet, AppData *ad);
extern void property_show(BonoboUIComponent *ui, gpointer data, const char *cname);
static const char netmon_menu_xml [] =
"<popup name=\"button3\">\n"
" <menuitem name=\"Properties Item\" verb=\"NetmonProperties\" _label=\"Properties ...\"\n"
" pixtype=\"stock\" pixname=\"gtk-properties\"/>\n"
" <menuitem name=\"About Item\" verb=\"NetmonAbout\" _label=\"About ...\"\n"
" pixtype=\"stock\" pixname=\"gnome-stock-about\"/>\n"
"</popup>\n";
static const BonoboUIVerb netmon_menu_verbs[] = {
BONOBO_UI_VERB("NetmonProperties", property_show),
BONOBO_UI_VERB("NetmonAbout", about),
BONOBO_UI_VERB_END
};
void init_ui(){
char buf[2048];
if (box) gtk_widget_destroy(box);
if (frame) gtk_widget_destroy(frame);
sprintf(buf, "<span font_desc=\"%s\">rx: 0.0k/s</span>", ad->font);
inlabel = gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(inlabel), buf);
sprintf(buf, "<span font_desc=\"%s\">tx: 0.0k/s</span>", ad->font);
outlabel = gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(outlabel), buf);
netmon_draw();
if (ad->layout == NETMON_LAYOUT_VERTICAL){
box = gtk_vbox_new(FALSE, FALSE);
}
else {
box = gtk_hbox_new(FALSE, 5);
}
gtk_box_pack_start(GTK_BOX(box), inlabel, FALSE, 0, 0);
gtk_box_pack_start(GTK_BOX(box), outlabel, FALSE, 0, 0);
if (ad->show_frame){
frame = gtk_frame_new(ad->interface);
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
gtk_container_add(GTK_CONTAINER(frame), box);
gtk_container_add(GTK_CONTAINER(ad->applet), frame);
}
else {
gtk_container_add(GTK_CONTAINER(ad->applet), box);
}
gtk_widget_show_all(GTK_WIDGET(ad->applet));
}
static void change_orient_cb(PanelApplet *w, PanelAppletOrient o, gpointer data) {
AppData *ad = (AppData*)data;
ad->panel_orient = o;
init_ui();
}
static void change_size_cb(PanelApplet *w, gint s, gpointer data) {
AppData *ad = (AppData*)data;
ad->panel_size = s;
init_ui();
}
gboolean create_netmon_applet(PanelApplet *applet,
const gchar *iid, gpointer data){
ad = g_new0(AppData, 1);
ad->applet = applet;
panel_applet_add_preferences(applet,"/schemas/apps/netmon_applet/prefs",NULL);
property_load(applet, ad);
/* property_save(applet, ad); */
if (initialize(ad->interface, 5) < 0){
fprintf(stderr, "Cannot init %s!\n", ad->interface);
fflush(stderr);
}
panel_applet_setup_menu(PANEL_APPLET (applet),
netmon_menu_xml,
netmon_menu_verbs,
ad);
/*
gtk_signal_connect(GTK_OBJECT(applet),"save_session",
GTK_SIGNAL_FUNC(property_save), ad);
*/
g_signal_connect (G_OBJECT(applet), "change_size",
G_CALLBACK(change_size_cb), ad);
g_signal_connect (G_OBJECT(applet), "change_orient",
G_CALLBACK(change_orient_cb), ad);
/* gtk_signal_connect (GTK_OBJECT(frame), "destroy", */
/* GTK_SIGNAL_FUNC(gtk_main_quit), NULL); */
time(&starttime);
timeout_handle = gtk_timeout_add(UPDATE_TIMEOUT, (void *)netmon_draw, NULL);
init_ui();
return TRUE;
}
gint netmon_draw(void){
int i;
char buf[128];
char num[16];
time_t newtime;
long hr, min, sec; /* time vars */
float ink, outk;
update_avg(1);
ink = average.in/1024;
outk = average.out/1024;
if (ink < 100.0) sprintf(num, "%.1fk/s", ink);
else if (ink < 1000.0) sprintf(num, "%ik/s", (int)ink);
else sprintf(num, "%.1fM/s", ink/1024);
sprintf(buf, "<span font_desc=\"%s\">rx: %s</span>", ad->font, num);
gtk_label_set_markup(GTK_LABEL(inlabel), buf);
if (outk < 100.0) sprintf(num, "%.1fk/s", outk);
else if (outk < 1000.0) sprintf(num, "%ik/s", (int)outk);
else sprintf(num, "%.1fM/s", outk/1024);
sprintf(buf, "<span font_desc=\"%s\">tx: %s</span>", ad->font, num);
gtk_label_set_markup(GTK_LABEL(outlabel), buf);
return TRUE;
}
void about(BonoboUIComponent *ui, gpointer data, const char *cname) {
const gchar *authors[] = {
"Jason Petrone <jp@demonseed.net>",
NULL
};
static GtkWidget *about_dialog = NULL;
if (about_dialog != NULL)
{
gdk_window_show(about_dialog->window);
gdk_window_raise(about_dialog->window);
return;
}
about_dialog = gnome_about_new ("GNOME Network Monitor", VERSION,
_("Copyright (c) 2000-2001 Jason Petrone"),
_("GNOME network load monitor applet."),
authors, NULL, NULL, NULL);
gtk_signal_connect( GTK_OBJECT(about_dialog), "destroy",
GTK_SIGNAL_FUNC(gtk_widget_destroyed), &about_dialog );
gtk_widget_show(about_dialog);
return;
}
PANEL_APPLET_BONOBO_FACTORY("OAFIID:GNOME_NetmonApplet_Factory",
PANEL_TYPE_APPLET, "netmon_applet", "0", create_netmon_applet, NULL);
|