dc949320720fdf450ce277f94e7d35c40d7dc3f0
[doldaconnect.git] / clients / gnome-trans-applet / dolcon-trans-applet.c
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     size_t l;
103     
104     switch(data->conduit->state)
105     {
106     case CNDS_IDLE:
107         gtk_progress_bar_set_text(data->pbar, _("Not connected"));
108         gtk_label_set_text(data->label, "");
109         break;
110     case CNDS_SYN:
111         gtk_progress_bar_set_text(data->pbar, _("Connecting..."));
112         gtk_label_set_text(data->label, "");
113         break;
114     case CNDS_EST:
115         if(data->conduit->transfers == NULL)
116         {
117             gtk_progress_bar_set_fraction(data->pbar, 0);
118             gtk_progress_bar_set_text(data->pbar, "");
119             gtk_label_set_text(data->label, _("No transfers to display"));
120         } else if(data->curdisplay == NULL) {
121             gtk_progress_bar_set_fraction(data->pbar, 0);
122             gtk_progress_bar_set_text(data->pbar, "");
123             gtk_label_set_text(data->label, _("No transfer selected"));
124         } else {
125             if((data->curdisplay->pos > 0) && (data->curdisplay->size > 0))
126             {
127                 sprintf(buf, "%'i/%'i", data->curdisplay->pos, data->curdisplay->size);
128                 gtk_progress_bar_set_fraction(data->pbar, (double)data->curdisplay->pos / (double)data->curdisplay->size);
129                 gtk_progress_bar_set_text(data->pbar, buf);
130             } else {
131                 gtk_progress_bar_set_fraction(data->pbar, 0);
132                 gtk_progress_bar_set_text(data->pbar, _("Initializing"));
133             }
134             if((l = strlen(data->curdisplay->tag)) > 50) {
135                 memcpy(buf, data->curdisplay->tag, 20);
136                 memcpy(buf + 20, "...", 3);
137                 memcpy(buf + 23 , data->curdisplay->tag + l - 20, 20);
138                 buf[43] = 0;
139                 gtk_label_set_text(data->label, buf);
140             } else {
141                 gtk_label_set_text(data->label, data->curdisplay->tag);
142             }
143         }
144         break;
145     }
146 }
147
148 static void trsize(struct transfer *transfer, struct appletdata *data)
149 {
150     update(data);
151 }
152
153 static void trpos(struct transfer *transfer, struct appletdata *data)
154 {
155     update(data);
156 }
157
158 static void trnew(struct transfer *transfer, struct appletdata *data)
159 {
160     if(data->curdisplay == NULL)
161         data->curdisplay = transfer;
162     update(data);
163 }
164
165 static void trfree(struct transfer *transfer, struct appletdata *data)
166 {
167     if(data->curdisplay == transfer)
168         data->curdisplay = data->conduit->transfers;
169     update(data);
170 }
171
172 static void condstate(struct conduit *conduit, struct appletdata *data)
173 {
174     if(conduit->state == CNDS_IDLE)
175         g_timeout_add(10000, (gboolean (*)(gpointer))reconncb, data);
176     update(data);
177 }
178
179 static void initcond(void)
180 {
181     static int inited = 0;
182     
183     if(!inited)
184     {
185         cb_trsize = (void (*)(struct transfer *, void *))trsize;
186         cb_trpos = (void (*)(struct transfer *, void *))trpos;
187         cb_trnew = (void (*)(struct transfer *, void *))trnew;
188         cb_trfree = (void (*)(struct transfer *, void *))trfree;
189         cb_condstate = (void (*)(struct conduit *, void *))condstate;
190         inited = 1;
191     }
192 }
193
194 static gboolean trview_applet_button_press(GtkWidget *widget, GdkEventButton *event, struct appletdata *data)
195 {
196     if(event->button == 1)
197     {
198         if(data->curdisplay == NULL)
199             data->curdisplay = data->conduit->transfers;
200         else if(data->curdisplay->next == NULL)
201             data->curdisplay = data->conduit->transfers;
202         else
203             data->curdisplay = data->curdisplay->next;
204         update(data);
205     }
206     return(FALSE);
207 }
208
209 static gboolean trview_applet_scroll(GtkWidget *widget, GdkEventScroll *event, struct appletdata *data)
210 {
211     struct transfer *tr;
212     
213     if(event->direction == GDK_SCROLL_DOWN)
214     {
215         if(data->curdisplay == NULL)
216             data->curdisplay = data->conduit->transfers;
217         else if(data->curdisplay->next == NULL)
218             data->curdisplay = data->conduit->transfers;
219         else
220             data->curdisplay = data->curdisplay->next;
221         update(data);
222     } else if(event->direction == GDK_SCROLL_UP) {
223         if(data->curdisplay == NULL)
224         {
225             data->curdisplay = data->conduit->transfers;
226         } else if(data->curdisplay->prev == NULL) {
227             for(tr = data->conduit->transfers; tr->next != NULL; tr = tr->next);
228             data->curdisplay = tr;
229         } else {
230             data->curdisplay = data->curdisplay->prev;
231         }
232         update(data);
233     }
234     return(TRUE);
235 }
236
237 static void trview_applet_destroy(GtkWidget *widget, struct appletdata *data)
238 {
239     freeconduit(data->conduit);
240     g_source_remove(data->tiptimeout);
241     g_object_unref(data->applet);
242     g_object_unref(data->tips);
243     free(data);
244 }
245
246 static gboolean trview_applet_fill(PanelApplet *applet, const gchar *iid, gpointer uudata)
247 {
248     GtkWidget *hbox, *pbar, *label;
249     struct appletdata *data;
250     
251     initcond();
252     if(strcmp(iid, "OAFIID:Dolcon_Transferapplet"))
253         return(FALSE);
254     
255     hbox = gtk_hbox_new(FALSE, 0);
256     label = gtk_label_new("");
257     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
258     pbar = gtk_progress_bar_new();
259     gtk_box_pack_start(GTK_BOX(hbox), pbar, TRUE, TRUE, 0);
260     gtk_container_add(GTK_CONTAINER(applet), hbox);
261     gtk_widget_show_all(GTK_WIDGET(applet));
262     
263     data = smalloc(sizeof(*data));
264     memset(data, 0, sizeof(*data));
265     g_object_ref(data->applet = applet);
266     data->conduit = newconduit(conduit_dclib, data);
267     data->pbar = GTK_PROGRESS_BAR(pbar);
268     g_object_ref(data->tips = gtk_tooltips_new());
269     data->tiptimeout = g_timeout_add(500, (gboolean (*)(gpointer))updatetip, data);
270     data->label = GTK_LABEL(label);
271     
272     panel_applet_setup_menu(applet, ctxtmenu, ctxtmenuverbs, data);
273
274     g_signal_connect(applet, "button-press-event", (GCallback)trview_applet_button_press, data);
275     g_signal_connect(applet, "scroll-event", (GCallback)trview_applet_scroll, data);
276     g_signal_connect(applet, "destroy", (GCallback)trview_applet_destroy, data);
277     
278     condtryconn(data->conduit);
279     
280     update(data);
281     
282     return(TRUE);
283 }
284
285 #define GETTEXT_PACKAGE PACKAGE
286 #define GNOMELOCALEDIR LOCALEDIR
287
288 PANEL_APPLET_BONOBO_FACTORY("OAFIID:Dolcon_Transferapplet_Factory",
289                             PANEL_TYPE_APPLET,
290                             "Doldaconnect Transfer Viewer",
291                             "0",
292                             trview_applet_fill,
293                             NULL);