2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
24 #include <doldaconnect/uilib.h>
25 #include <doldaconnect/utils.h>
26 #include <panel-applet.h>
39 struct conduit *conduit;
40 struct transfer *curdisplay;
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' />"
49 static void run_pref_dialog(BonoboUIComponent *uic, gpointer data, const char *cname)
53 static void cancel_transfer(BonoboUIComponent *uic, struct appletdata *data, const char *cname)
55 if(data->conduit->iface->cancel != NULL)
56 data->conduit->iface->cancel(data->conduit, data->curdisplay);
59 static BonoboUIVerb ctxtmenuverbs[] =
61 BONOBO_UI_VERB("dca_pref", run_pref_dialog),
62 BONOBO_UI_VERB("dca_cancel", (void (*)(BonoboUIComponent*, gpointer, const char *))cancel_transfer),
66 static gint reconncb(struct appletdata *data)
68 condtryconn(data->conduit);
72 static gboolean updatetip(struct appletdata *data)
74 int diff, speed, left;
78 if(data->curdisplay == NULL) {
79 gtk_tooltips_set_tip(data->tips, GTK_WIDGET(data->applet), _("No transfer selected"), NULL);
83 if(data->curdisplay->cmptime == 0)
85 strcpy(buf, _("Calculating remaining time..."));
87 diff = data->curdisplay->pos - data->curdisplay->cmpsize;
88 speed = diff / (now - data->curdisplay->cmptime);
91 strcpy(buf, _("Time left: Infinite (Transfer is standing still)"));
93 left = (data->curdisplay->size - data->curdisplay->pos) / speed;
94 sprintf(buf, _("Time left: %i:%02i"), left / 3600, (left / 60) % 60);
97 gtk_tooltips_set_tip(data->tips, GTK_WIDGET(data->applet), buf, NULL);
101 static void update(struct appletdata *data)
106 switch(data->conduit->state)
109 gtk_progress_bar_set_text(data->pbar, _("Not connected"));
110 gtk_label_set_text(data->label, "");
113 gtk_progress_bar_set_text(data->pbar, _("Connecting..."));
114 gtk_label_set_text(data->label, "");
117 if(data->conduit->transfers == NULL)
119 gtk_progress_bar_set_fraction(data->pbar, 0);
120 gtk_progress_bar_set_text(data->pbar, "");
121 gtk_label_set_text(data->label, _("No transfers to display"));
122 } else if(data->curdisplay == NULL) {
123 gtk_progress_bar_set_fraction(data->pbar, 0);
124 gtk_progress_bar_set_text(data->pbar, "");
125 gtk_label_set_text(data->label, _("No transfer selected"));
127 if((data->curdisplay->pos > 0) && (data->curdisplay->size > 0))
129 sprintf(buf, "%'i/%'i", data->curdisplay->pos, data->curdisplay->size);
130 gtk_progress_bar_set_fraction(data->pbar, (double)data->curdisplay->pos / (double)data->curdisplay->size);
131 gtk_progress_bar_set_text(data->pbar, buf);
133 gtk_progress_bar_set_fraction(data->pbar, 0);
134 gtk_progress_bar_set_text(data->pbar, _("Initializing"));
136 if((l = strlen(data->curdisplay->tag)) > 50) {
137 memcpy(buf, data->curdisplay->tag, 20);
138 memcpy(buf + 20, "...", 3);
139 memcpy(buf + 23 , data->curdisplay->tag + l - 20, 20);
141 gtk_label_set_text(data->label, buf);
143 gtk_label_set_text(data->label, data->curdisplay->tag);
150 static void trsize(struct transfer *transfer, struct appletdata *data)
155 static void trpos(struct transfer *transfer, struct appletdata *data)
160 static void trnew(struct transfer *transfer, struct appletdata *data)
162 if(data->curdisplay == NULL)
163 data->curdisplay = transfer;
167 static void trfree(struct transfer *transfer, struct appletdata *data)
169 if(data->curdisplay == transfer)
170 data->curdisplay = data->conduit->transfers;
174 static void condstate(struct conduit *conduit, struct appletdata *data)
176 if(conduit->state == CNDS_IDLE)
177 g_timeout_add(10000, (gboolean (*)(gpointer))reconncb, data);
181 static void initcond(void)
183 static int inited = 0;
187 cb_trsize = (void (*)(struct transfer *, void *))trsize;
188 cb_trpos = (void (*)(struct transfer *, void *))trpos;
189 cb_trnew = (void (*)(struct transfer *, void *))trnew;
190 cb_trfree = (void (*)(struct transfer *, void *))trfree;
191 cb_condstate = (void (*)(struct conduit *, void *))condstate;
196 static gboolean trview_applet_button_press(GtkWidget *widget, GdkEventButton *event, struct appletdata *data)
198 if(event->button == 1)
200 if(data->curdisplay == NULL)
201 data->curdisplay = data->conduit->transfers;
202 else if(data->curdisplay->next == NULL)
203 data->curdisplay = data->conduit->transfers;
205 data->curdisplay = data->curdisplay->next;
211 static gboolean trview_applet_scroll(GtkWidget *widget, GdkEventScroll *event, struct appletdata *data)
215 if(event->direction == GDK_SCROLL_DOWN)
217 if(data->curdisplay == NULL)
218 data->curdisplay = data->conduit->transfers;
219 else if(data->curdisplay->next == NULL)
220 data->curdisplay = data->conduit->transfers;
222 data->curdisplay = data->curdisplay->next;
224 } else if(event->direction == GDK_SCROLL_UP) {
225 if(data->curdisplay == NULL)
227 data->curdisplay = data->conduit->transfers;
228 } else if(data->curdisplay->prev == NULL) {
229 for(tr = data->conduit->transfers; tr->next != NULL; tr = tr->next);
230 data->curdisplay = tr;
232 data->curdisplay = data->curdisplay->prev;
239 static void trview_applet_destroy(GtkWidget *widget, struct appletdata *data)
241 freeconduit(data->conduit);
242 g_source_remove(data->tiptimeout);
243 g_object_unref(data->applet);
244 g_object_unref(data->tips);
248 static gboolean trview_applet_fill(PanelApplet *applet, const gchar *iid, gpointer uudata)
250 GtkWidget *hbox, *pbar, *label;
251 struct appletdata *data;
254 if(strcmp(iid, "OAFIID:Dolcon_Transferapplet"))
257 hbox = gtk_hbox_new(FALSE, 0);
258 label = gtk_label_new("");
259 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
260 pbar = gtk_progress_bar_new();
261 gtk_box_pack_start(GTK_BOX(hbox), pbar, TRUE, TRUE, 0);
262 gtk_container_add(GTK_CONTAINER(applet), hbox);
263 gtk_widget_show_all(GTK_WIDGET(applet));
265 data = smalloc(sizeof(*data));
266 memset(data, 0, sizeof(*data));
267 g_object_ref(data->applet = applet);
268 data->conduit = newconduit(conduit_dclib, data);
269 data->pbar = GTK_PROGRESS_BAR(pbar);
270 g_object_ref(data->tips = gtk_tooltips_new());
271 data->tiptimeout = g_timeout_add(500, (gboolean (*)(gpointer))updatetip, data);
272 data->label = GTK_LABEL(label);
274 panel_applet_setup_menu(applet, ctxtmenu, ctxtmenuverbs, data);
276 g_signal_connect(applet, "button-press-event", (GCallback)trview_applet_button_press, data);
277 g_signal_connect(applet, "scroll-event", (GCallback)trview_applet_scroll, data);
278 g_signal_connect(applet, "destroy", (GCallback)trview_applet_destroy, data);
280 condtryconn(data->conduit);
287 #define GETTEXT_PACKAGE PACKAGE
288 #define GNOMELOCALEDIR LOCALEDIR
290 PANEL_APPLET_BONOBO_FACTORY("OAFIID:Dolcon_Transferapplet_Factory",
292 "Doldaconnect Transfer Viewer",