/* GKrellM giFT plugin * Copyright (C) 2002, 2003 Tilman Sauerbeck * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * 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 "common.h" #include "event_ids.h" #include "gift.h" static void get_stats_data (Interface *iface, InterfaceNode *node, giFTStats *stats) { if (!strcasecmp (node->key, "users")) stats->users += ATOUL (node->value); else if (!strcasecmp (node->key, "files")) stats->files_remote += ATOUL (node->value); else if (!strcasecmp (node->key, "size")) stats->size_remote += strtod (node->value, NULL); } static void traverse_tree_stats (Interface *iface, InterfaceNode *node, giFTStats *stats) { if (strcasecmp (node->key, "gift")) interface_foreach (iface, node->key, (InterfaceForeach) get_stats_data, stats); } static void stats_handle(Interface *iface) { giFTStats s = gift_stats_get(); /* local stuff (giFT) */ s.files_local = INTERFACE_GETLU(iface, "gift/files"); s.size_local = strtod(interface_get(iface, "gift/size"), NULL); /* remote stuff (OpenFT, Gnutella, ...) */ s.users = s.files_remote = s.size_remote = 0; interface_foreach(iface, NULL, (InterfaceForeach) traverse_tree_stats, &s); gift_stats_set(s); } static void transfer_get_data(Interface *iface, Transfer **t, gboolean is_update) { gchar *filename; gulong size; gulong transmit; gulong throughput; gulong elapsed; if (is_update) { throughput = INTERFACE_GETLU(iface, "throughput"); elapsed = INTERFACE_GETLU(iface, "elapsed"); if (elapsed) (*t)->speed = (gfloat) throughput / elapsed; if (!throughput) { /* transmit didn't change, so we don't need to * update the decals */ return; } } filename = interface_get(iface, "file"); size = INTERFACE_GETLU(iface, "size"); transmit = INTERFACE_GETLU(iface, "transmit"); snprintf((*t)->dcl_fname_txt, sizeof((*t)->dcl_fname_txt), "%s %i%%", filename, (gint) ((gfloat) transmit / size * 100)); (*t)->x_off = -gdk_string_width(gkrellm_default_font(1), (*t)->dcl_fname_txt); } static void transfer_handle(Interface *iface, TransferDirection dir, gboolean is_update) { EventID *id; Transfer *t; gint id_num = atoi(iface->value); id = (is_update) ? gift_event_id_lookup(id_num) : gift_event_id_add(id_num, dir); g_assert(id); t = (is_update) ? gift_lookup_transfer(id) : g_new0(Transfer, 1); g_assert(t); transfer_get_data(iface, &t, is_update); if (!is_update) { t->id = id; t->visible = gift_is_free_transfer_slot(dir); gift_transfer_add(t); } } static void transfer_handle_del(Interface *iface) { EventID *id = gift_event_id_lookup(atoi(iface->value)); gift_transfer_remove(gift_lookup_transfer(id)); gift_event_id_remove(id); /* check whether we need to make a waiting transfer visible */ gift_transfer_make_visible(id->dir); } void gift_daemon_parse(gchar *input) { Interface *iface = interface_unserialize(input, strlen(input)); #define IS_CMD(val) !strcasecmp(iface->command, (val)) if (!iface) return; if (IS_CMD("adddownload")) transfer_handle(iface, DIR_DOWN, FALSE); else if (IS_CMD("chgdownload")) transfer_handle(iface, DIR_DOWN, TRUE); else if (IS_CMD("addupload")) transfer_handle(iface, DIR_UP, FALSE); else if (IS_CMD("chgupload")) transfer_handle(iface, DIR_UP, TRUE); else if (IS_CMD("deldownload") || IS_CMD("delupload")) transfer_handle_del(iface); else if (IS_CMD("stats")) stats_handle(iface); interface_free(iface); }