| 1 | /* |
| 2 | * Dolda Connect - Modular multiuser Direct Connect-style client |
| 3 | * Copyright (C) 2005 Fredrik Tolf (fredrik@dolda2000.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | #include <string.h> |
| 24 | #include <doldaconnect/uilib.h> |
| 25 | #include <doldaconnect/utils.h> |
| 26 | #include <panel-applet.h> |
| 27 | #include <gtk/gtk.h> |
| 28 | #include <time.h> |
| 29 | |
| 30 | #include "conduit.h" |
| 31 | |
| 32 | struct appletdata |
| 33 | { |
| 34 | PanelApplet *applet; |
| 35 | GtkLabel *label; |
| 36 | GtkProgressBar *pbar; |
| 37 | GtkTooltips *tips; |
| 38 | gint tiptimeout; |
| 39 | struct conduit *conduit; |
| 40 | struct transfer *curdisplay; |
| 41 | }; |
| 42 | |
| 43 | static char *ctxtmenu = |
| 44 | "<popup name='button3'>" |
| 45 | " <menuitem name='Preferences' verb='dca_pref' _label='Preferences' pixtype='stock' pixname='gtk-properties' />" |
| 46 | " <menuitem name='Cancel transfer' verb='dca_cancel' _label='Cancel transfer' pixtype='stock' pixname='gtk-cancel' />" |
| 47 | "</popup>"; |
| 48 | |
| 49 | static void run_pref_dialog(BonoboUIComponent *uic, gpointer data, const char *cname) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | static void cancel_transfer(BonoboUIComponent *uic, struct appletdata *data, const char *cname) |
| 54 | { |
| 55 | if(data->conduit->iface->cancel != NULL) |
| 56 | data->conduit->iface->cancel(data->conduit, data->curdisplay); |
| 57 | } |
| 58 | |
| 59 | static BonoboUIVerb ctxtmenuverbs[] = |
| 60 | { |
| 61 | BONOBO_UI_VERB("dca_pref", run_pref_dialog), |
| 62 | BONOBO_UI_VERB("dca_cancel", cancel_transfer), |
| 63 | BONOBO_UI_VERB_END |
| 64 | }; |
| 65 | |
| 66 | static gint reconncb(struct appletdata *data) |
| 67 | { |
| 68 | condtryconn(data->conduit); |
| 69 | return(FALSE); |
| 70 | } |
| 71 | |
| 72 | static gboolean updatetip(struct appletdata *data) |
| 73 | { |
| 74 | int diff, speed, left; |
| 75 | time_t now; |
| 76 | char buf[256]; |
| 77 | |
| 78 | if(data->curdisplay == NULL) |
| 79 | return(TRUE); |
| 80 | now = time(NULL); |
| 81 | if(data->curdisplay->cmptime == 0) |
| 82 | { |
| 83 | strcpy(buf, _("Calculating remaining time...")); |
| 84 | } else { |
| 85 | diff = data->curdisplay->pos - data->curdisplay->cmpsize; |
| 86 | speed = diff / (now - data->curdisplay->cmptime); |
| 87 | if(speed == 0) |
| 88 | { |
| 89 | strcpy(buf, _("Time left: Infinite (Transfer is standing still)")); |
| 90 | } else { |
| 91 | left = (data->curdisplay->size - data->curdisplay->pos) / speed; |
| 92 | sprintf(buf, _("Time left: %i:%02i"), left / 3600, (left / 60) % 60); |
| 93 | } |
| 94 | } |
| 95 | gtk_tooltips_set_tip(data->tips, GTK_WIDGET(data->applet), buf, NULL); |
| 96 | return(TRUE); |
| 97 | } |
| 98 | |
| 99 | static void update(struct appletdata *data) |
| 100 | { |
| 101 | char buf[256]; |
| 102 | |
| 103 | switch(data->conduit->state) |
| 104 | { |
| 105 | case CNDS_IDLE: |
| 106 | gtk_progress_bar_set_text(data->pbar, _("Not connected")); |
| 107 | gtk_label_set_text(data->label, ""); |
| 108 | break; |
| 109 | case CNDS_SYN: |
| 110 | gtk_progress_bar_set_text(data->pbar, _("Connecting...")); |
| 111 | gtk_label_set_text(data->label, ""); |
| 112 | break; |
| 113 | case CNDS_EST: |
| 114 | if(data->conduit->transfers == NULL) |
| 115 | { |
| 116 | gtk_progress_bar_set_fraction(data->pbar, 0); |
| 117 | gtk_progress_bar_set_text(data->pbar, ""); |
| 118 | gtk_label_set_text(data->label, _("No transfers to display")); |
| 119 | } else if(data->curdisplay == NULL) { |
| 120 | gtk_progress_bar_set_fraction(data->pbar, 0); |
| 121 | gtk_progress_bar_set_text(data->pbar, ""); |
| 122 | gtk_label_set_text(data->label, _("No transfer selected")); |
| 123 | } else { |
| 124 | if((data->curdisplay->pos > 0) && (data->curdisplay->size > 0)) |
| 125 | { |
| 126 | sprintf(buf, "%'i/%'i", data->curdisplay->pos, data->curdisplay->size); |
| 127 | gtk_progress_bar_set_fraction(data->pbar, (double)data->curdisplay->pos / (double)data->curdisplay->size); |
| 128 | gtk_progress_bar_set_text(data->pbar, buf); |
| 129 | } else { |
| 130 | gtk_progress_bar_set_fraction(data->pbar, 0); |
| 131 | gtk_progress_bar_set_text(data->pbar, _("Initializing")); |
| 132 | } |
| 133 | gtk_label_set_text(data->label, data->curdisplay->tag); |
| 134 | } |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void trsize(struct transfer *transfer, struct appletdata *data) |
| 140 | { |
| 141 | update(data); |
| 142 | } |
| 143 | |
| 144 | static void trpos(struct transfer *transfer, struct appletdata *data) |
| 145 | { |
| 146 | update(data); |
| 147 | } |
| 148 | |
| 149 | static void trnew(struct transfer *transfer, struct appletdata *data) |
| 150 | { |
| 151 | if(data->curdisplay == NULL) |
| 152 | data->curdisplay = transfer; |
| 153 | update(data); |
| 154 | } |
| 155 | |
| 156 | static void trfree(struct transfer *transfer, struct appletdata *data) |
| 157 | { |
| 158 | if(data->curdisplay == transfer) |
| 159 | data->curdisplay = data->conduit->transfers; |
| 160 | update(data); |
| 161 | } |
| 162 | |
| 163 | static void condstate(struct conduit *conduit, struct appletdata *data) |
| 164 | { |
| 165 | if(conduit->state == CNDS_IDLE) |
| 166 | g_timeout_add(10000, (gboolean (*)(gpointer))reconncb, data); |
| 167 | update(data); |
| 168 | } |
| 169 | |
| 170 | static void initcond(void) |
| 171 | { |
| 172 | static int inited = 0; |
| 173 | |
| 174 | if(!inited) |
| 175 | { |
| 176 | cb_trsize = (void (*)(struct transfer *, void *))trsize; |
| 177 | cb_trpos = (void (*)(struct transfer *, void *))trpos; |
| 178 | cb_trnew = (void (*)(struct transfer *, void *))trnew; |
| 179 | cb_trfree = (void (*)(struct transfer *, void *))trfree; |
| 180 | cb_condstate = (void (*)(struct conduit *, void *))condstate; |
| 181 | inited = 1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static gboolean trview_applet_button_press(GtkWidget *widget, GdkEventButton *event, struct appletdata *data) |
| 186 | { |
| 187 | if(event->button == 1) |
| 188 | { |
| 189 | if(data->curdisplay == NULL) |
| 190 | data->curdisplay = data->conduit->transfers; |
| 191 | else if(data->curdisplay->next == NULL) |
| 192 | data->curdisplay = data->conduit->transfers; |
| 193 | else |
| 194 | data->curdisplay = data->curdisplay->next; |
| 195 | update(data); |
| 196 | } |
| 197 | return(FALSE); |
| 198 | } |
| 199 | |
| 200 | static gboolean trview_applet_scroll(GtkWidget *widget, GdkEventScroll *event, struct appletdata *data) |
| 201 | { |
| 202 | struct transfer *tr; |
| 203 | |
| 204 | if(event->direction == GDK_SCROLL_DOWN) |
| 205 | { |
| 206 | if(data->curdisplay == NULL) |
| 207 | data->curdisplay = data->conduit->transfers; |
| 208 | else if(data->curdisplay->next == NULL) |
| 209 | data->curdisplay = data->conduit->transfers; |
| 210 | else |
| 211 | data->curdisplay = data->curdisplay->next; |
| 212 | update(data); |
| 213 | } else if(event->direction == GDK_SCROLL_UP) { |
| 214 | if(data->curdisplay == NULL) |
| 215 | { |
| 216 | data->curdisplay = data->conduit->transfers; |
| 217 | } else if(data->curdisplay->prev == NULL) { |
| 218 | for(tr = data->conduit->transfers; tr->next != NULL; tr = tr->next); |
| 219 | data->curdisplay = tr; |
| 220 | } else { |
| 221 | data->curdisplay = data->curdisplay->prev; |
| 222 | } |
| 223 | update(data); |
| 224 | } |
| 225 | return(TRUE); |
| 226 | } |
| 227 | |
| 228 | static void trview_applet_destroy(GtkWidget *widget, struct appletdata *data) |
| 229 | { |
| 230 | freeconduit(data->conduit); |
| 231 | g_source_remove(data->tiptimeout); |
| 232 | g_object_unref(data->applet); |
| 233 | g_object_unref(data->tips); |
| 234 | free(data); |
| 235 | } |
| 236 | |
| 237 | static gboolean trview_applet_fill(PanelApplet *applet, const gchar *iid, gpointer uudata) |
| 238 | { |
| 239 | GtkWidget *hbox, *pbar, *label; |
| 240 | struct appletdata *data; |
| 241 | |
| 242 | initcond(); |
| 243 | if(strcmp(iid, "OAFIID:Dolcon_Transferapplet")) |
| 244 | return(FALSE); |
| 245 | |
| 246 | hbox = gtk_hbox_new(FALSE, 0); |
| 247 | label = gtk_label_new(""); |
| 248 | gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5); |
| 249 | pbar = gtk_progress_bar_new(); |
| 250 | gtk_box_pack_start(GTK_BOX(hbox), pbar, TRUE, TRUE, 0); |
| 251 | gtk_container_add(GTK_CONTAINER(applet), hbox); |
| 252 | gtk_widget_show_all(GTK_WIDGET(applet)); |
| 253 | |
| 254 | data = smalloc(sizeof(*data)); |
| 255 | memset(data, 0, sizeof(*data)); |
| 256 | g_object_ref(data->applet = applet); |
| 257 | data->conduit = newconduit(conduit_dclib, data); |
| 258 | data->pbar = GTK_PROGRESS_BAR(pbar); |
| 259 | g_object_ref(data->tips = gtk_tooltips_new()); |
| 260 | data->tiptimeout = g_timeout_add(500, (gboolean (*)(gpointer))updatetip, data); |
| 261 | data->label = GTK_LABEL(label); |
| 262 | |
| 263 | panel_applet_setup_menu(applet, ctxtmenu, ctxtmenuverbs, data); |
| 264 | |
| 265 | g_signal_connect(applet, "button-press-event", (GCallback)trview_applet_button_press, data); |
| 266 | g_signal_connect(applet, "scroll-event", (GCallback)trview_applet_scroll, data); |
| 267 | g_signal_connect(applet, "destroy", (GCallback)trview_applet_destroy, data); |
| 268 | |
| 269 | condtryconn(data->conduit); |
| 270 | |
| 271 | update(data); |
| 272 | |
| 273 | return(TRUE); |
| 274 | } |
| 275 | |
| 276 | #define GETTEXT_PACKAGE PACKAGE |
| 277 | #define GNOMELOCALEDIR LOCALEDIR |
| 278 | |
| 279 | PANEL_APPLET_BONOBO_FACTORY("OAFIID:Dolcon_Transferapplet_Factory", |
| 280 | PANEL_TYPE_APPLET, |
| 281 | "Doldaconnect Transfer Viewer", |
| 282 | "0", |
| 283 | trview_applet_fill, |
| 284 | NULL); |