📄 gui_engine.c
字号:
/*-*-linux-c-*-*//* * gnewtellium - Newtella for Unix * Copyright (C) 2001 Elias Athanasopoulos * * 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; either version 2 of the License, or * (at your option) any later version. * * 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 "../pixmaps/gnewtellium.xpm"#include <gtk/gtk.h>#include <stdlib.h>#include <unistd.h>#include <netdb.h>#include <string.h>#include <stdio.h>#include <errno.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <time.h>#include "newtella.h"#include "global.h"#include "gui.h"#include "gnutella.h"#include "config.h"#include "file.h"#include "downloads.h"#include "uploads.h"#include "hosts.h"void gui_add_con_to_list(struct newtella_connection *con){ gchar *dummy[4] = { "", "", "", "" }; gchar l_ip[255]; gint row = gtk_clist_append(GTK_CLIST(con_list), dummy); gtk_clist_set_row_data(GTK_CLIST(con_list), row, (gpointer) con); /* Set IP:Port for each connection in the GUI */ g_snprintf(l_ip, sizeof(l_ip), "%s:%d", ip2str(con->ip), con->port); gtk_clist_set_text(GTK_CLIST(con_list), row, 0, l_ip); gtk_clist_set_text(GTK_CLIST(con_list), row, 2, "0/0"); gtk_clist_set_text(GTK_CLIST(con_list), row, 3, "0/0/0"); gl_options->cons++; gui_update_con_status(con);}void gui_update_con_status(struct newtella_connection *con){ static guint32 last = 0; guint32 now; gint row; gchar l_sent[255], l_recv[255]; GList *l; gint pending_packets_len = 0; struct gpacket *packet; row = gtk_clist_find_row_from_data(GTK_CLIST(con_list), (gpointer) con); now = time((time_t)NULL); if ((now - last < 1) && (con->total_handled > 50)) return; else last = now; switch (con->state) { case GS_WAITINGCONNECT: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "Connecting..."); break; case GS_HANDSHAKE: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "Handshaking..."); break; case GS_CONNECTED: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "OK"); break; case GS_FAILED: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "Failed"); break; case GS_TIMEOUT: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "Time Out"); break; case GS_HOSTFEED: gtk_clist_set_text(GTK_CLIST(con_list), row, 1, "Feeding..."); break; default: break; } for (l = con->queue; l; l = l->next) { packet = l->data; pending_packets_len += packet->len; } /* packets received/dropped */ g_snprintf(l_sent, sizeof(l_sent), "%d/%d (%d:%d)", con->total_sent, con->total_dropped_to_send, g_list_length(con->queue), pending_packets_len); g_snprintf(l_recv, sizeof(l_recv), "%d/%d/%d", con->total_handled, con->total_received_dropped, con->total_bad_received); gtk_clist_set_text(GTK_CLIST(con_list), row, 2, l_sent); gtk_clist_set_text(GTK_CLIST(con_list), row, 3, l_recv);}void gui_update_found_results(int id, char *vendor){ GdkColor blue = { 0, 0, 0, 65535 }; GdkColor gnew = { 0, 65535, 0, 0 }; GList *iter; struct file_found *found; gchar buf[255], l_size[255], l_speed[255], l_loc[255] = "Push"; gint row = id; iter = g_list_nth(gl_results, row); while (iter != NULL) { found = iter->data; if (found->size < 1024) g_snprintf(l_size, sizeof(l_size), "%u Bytes", found->size); else if (found->size < 1048576) g_snprintf(l_size, sizeof(l_size), "%.1fKB", (float)found->size/1024.0); else if (found->size < 1073741824) g_snprintf(l_size, sizeof(l_size), "%.1fMB", (float)found->size/1048576.0); else g_snprintf(l_size, sizeof(l_size), "%.1fGB", (float)found->size/1073741824.0); g_snprintf(l_speed, sizeof(l_speed), "%d", found->speed); if (!found->push) g_snprintf(l_loc, sizeof(l_loc), "%s", ip2str(found->ip)); gtk_clist_set_text(GTK_CLIST(search_list), row, 0, found->file_name); gtk_clist_set_text(GTK_CLIST(search_list), row, 1, l_size); gtk_clist_set_text(GTK_CLIST(search_list), row, 2, l_speed); gtk_clist_set_text(GTK_CLIST(search_list), row, 3, l_loc); gtk_clist_set_text(GTK_CLIST(search_list), row, 4, vendor); if (!found->busy) gtk_clist_set_foreground(GTK_CLIST(search_list), row, &blue); if (!strcmp(vendor, "Gnewtellium")) gtk_clist_set_foreground(GTK_CLIST(search_list), row, &gnew); row++; iter = g_list_next(iter); } /* update the found files label */ g_snprintf(buf, sizeof(buf), "%d", gl_options->files_found); gtk_label_set_text(GTK_LABEL(files_found_label), buf);}void gui_clean_found_results(void){ GList *iter; struct file_found *found; iter = g_list_first(gl_results); while (iter) { found = (struct file_found *) iter->data; newtella_free(found->file_name); newtella_free(found); iter = g_list_next(iter); } g_list_free(gl_results); gtk_clist_clear(GTK_CLIST(search_list)); gtk_label_set_text(GTK_LABEL(files_found_label), "0"); gl_options->files_found = 0; gl_results = NULL;}void gui_add_download(struct newtella_connection *dcon){ gchar *dummy[5] = { "", "", "", "", "" }; gchar l_size[255], l_status[255], l_speed[255], l_server[255]; gint row = gtk_clist_append(GTK_CLIST(download_list), dummy); gtk_clist_set_row_data(GTK_CLIST(download_list), row, (gpointer) dcon); if (dcon->fi->size < 1024) g_snprintf(l_size, sizeof(l_size), "%u Bytes", dcon->fi->size); else if (dcon->fi->size < 1048576) g_snprintf(l_size, sizeof(l_size), "%.1fKB", (float)dcon->fi->size/1024.0); else if (dcon->fi->size < 1073741824) g_snprintf(l_size, sizeof(l_size), "%.1fMB", (float)dcon->fi->size/1048576.0); else g_snprintf(l_size, sizeof(l_size), "%.1fGB", (float)dcon->fi->size/1073741824.0); switch (dcon->state) { case GS_WAITINGCONNECT: strcpy(l_status, "Connecting..."); break; case GS_WAITINGPUSH: strcpy(l_status, "Push sent"); break; case GS_FAILED: strcpy(l_status, "Failed"); break; default: break; } g_snprintf(l_speed, sizeof(l_speed), "%d", dcon->fi->speed); g_snprintf(l_server, sizeof(l_server), "%s:%d", ip2str(dcon->fi->ip), dcon->fi->port); gtk_clist_set_text(GTK_CLIST(download_list), row, 0, dcon->fi->file_name); gtk_clist_set_text(GTK_CLIST(download_list), row, 1, l_size); gtk_clist_set_text(GTK_CLIST(download_list), row, 2, l_status); gtk_clist_set_text(GTK_CLIST(download_list), row, 3, l_speed); gtk_clist_set_text(GTK_CLIST(download_list), row, 4, l_server); gl_options->dcons++; /* go to downloads */ gtk_notebook_set_page(GTK_NOTEBOOK(main_notebook), 1);}void gui_update_download(struct newtella_connection *dcon){ gchar l_status[255], l_speed[255] = "0.0 k/s"; gint row; row = gtk_clist_find_row_from_data(GTK_CLIST(download_list), (gpointer) dcon); switch (dcon->state) { case GS_WAITINGCONNECT: strcpy(l_status, "Connecting..."); break; case GS_WAITINGPUSH: strcpy(l_status, "Push sent"); break; case GS_FAILED: strcpy(l_status, "Failed"); break; case GS_NOTFOUND: strcpy(l_status, "Not Found"); break; case GS_BUSY: strcpy(l_status, "Busy"); break; case GS_COMPLETE: strcpy(l_status, "Done!"); break; case GS_STOPPED: strcpy(l_status, "Stopped"); break; case GS_CONNECTED: g_snprintf(l_speed, sizeof(l_speed), "%.1f k/s", (float) (dcon->total_handled/(float)(time((time_t *)NULL)-dcon->last_time)/1000.0)); g_snprintf(l_status, sizeof(l_status), "%.1f %%", (float) (dcon->total_handled/(float)dcon->fi->size)*100.0); break; default: break; } gtk_clist_set_text(GTK_CLIST(download_list), row, 2, l_status); gtk_clist_set_text(GTK_CLIST(download_list), row, 3, l_speed);}void gui_update_download_status(struct newtella_connection *dcon, const gchar *reason){ gint row = gtk_clist_find_row_from_data(GTK_CLIST(download_list), (gpointer) dcon); gtk_clist_set_text(GTK_CLIST(download_list), row, 2, reason);}void gui_add_upload(struct newtella_connection *ucon, char *file_name){ gchar *dummy[5] = { "", "", "", "", "" }; gchar l_size[255], l_status[255], l_speed[255] = "0.0 k/s", l_server[255]; gint row = gtk_clist_append(GTK_CLIST(upload_list), dummy); gtk_clist_set_row_data(GTK_CLIST(upload_list), row, (gpointer) ucon); if (ucon->u_file_size < 1024) g_snprintf(l_size, sizeof(l_size), "%u Bytes", ucon->u_file_size); else if (ucon->u_file_size < 1048576) g_snprintf(l_size, sizeof(l_size), "%.1fKB", (float)ucon->u_file_size/1024.0); else if (ucon->u_file_size < 1073741824) g_snprintf(l_size, sizeof(l_size), "%.1fMB", (float)ucon->u_file_size/1048576.0); else g_snprintf(l_size, sizeof(l_size), "%.1fGB", (float)ucon->u_file_size/1073741824.0); switch (ucon->state) { case GS_HANDSHAKE: strcpy(l_status, "Handshaking..."); break; case GS_FAILED: strcpy(l_status, "Failed"); break; default: break; } g_snprintf(l_server, sizeof(l_server), "%s:%d", ip2str(ucon->ip), ucon->port); gtk_clist_set_text(GTK_CLIST(upload_list), row, 0, file_name); gtk_clist_set_text(GTK_CLIST(upload_list), row, 1, l_size); gtk_clist_set_text(GTK_CLIST(upload_list), row, 2, l_status); gtk_clist_set_text(GTK_CLIST(upload_list), row, 3, l_speed); gtk_clist_set_text(GTK_CLIST(upload_list), row, 4, l_server); gl_options->ucons++; /* go to uploads */ gtk_notebook_set_page(GTK_NOTEBOOK(main_notebook), 2);}void gui_update_upload(struct newtella_connection *ucon){ gchar l_status[255], l_speed[255] = "0.0 k/s"; gint row; static guint32 last = 0; guint32 now = time((time_t)NULL); row = gtk_clist_find_row_from_data(GTK_CLIST(upload_list), (gpointer) ucon); if (!last) last = now; if ((now - last < 1) && ucon->state == GS_CONNECTED) return; else last = now; switch (ucon->state) { case GS_WAITINGCONNECT: strcpy(l_status, "Connecting..."); break; case GS_FAILED: strcpy(l_status, "Failed"); break; case GS_STOPPED: strcpy(l_status, "Stopped"); break; case GS_HANDSHAKE: strcpy(l_status, "Handshaking..."); break; case GS_CONNECTED: g_snprintf(l_status, sizeof(l_status), "%.1f %%", (float) (ucon->total_sent/(float)ucon->u_file_size)*100.0); g_snprintf(l_speed, sizeof(l_speed), "%.1f k/s", (float) (ucon->total_sent/(float)(time((time_t *)NULL)-ucon->last_time)/1000.0)); break; case GS_COMPLETE: strcpy(l_status, "Done!"); break; default: break; } gtk_clist_set_text(GTK_CLIST(upload_list), row, 2, l_status); gtk_clist_set_text(GTK_CLIST(upload_list), row, 3, l_speed);}void gui_remove_con_from_list(struct newtella_connection *con){ gint row = gtk_clist_find_row_from_data(GTK_CLIST(con_list), (gpointer) con); gl_options->cons--; gtk_clist_freeze(GTK_CLIST(con_list)); gtk_clist_remove(GTK_CLIST(con_list), row); gtk_clist_thaw(GTK_CLIST(con_list));}void gui_remove_download(struct newtella_connection *dcon){ gint row = gtk_clist_find_row_from_data(GTK_CLIST(download_list), (gpointer) dcon); gl_options->dcons--; gtk_clist_remove(GTK_CLIST(download_list), row);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -