7 #include <doldaconnect/utils.h>
8 #include <panel-applet.h>
13 void (*cb_condstate)(struct conduit *conduit, void *data) = NULL;
14 void (*cb_trsize)(struct transfer *transfer, void *data) = NULL;
15 void (*cb_trpos)(struct transfer *transfer, void *data) = NULL;
16 void (*cb_trnew)(struct transfer *transfer, void *data) = NULL;
17 void (*cb_trfree)(struct transfer *transfer, void *data) = NULL;
19 struct transfer *findtransferbytag(struct conduit *conduit, char *tag)
21 struct transfer *transfer;
23 for(transfer = conduit->transfers; transfer != NULL; transfer = transfer->next)
25 if((transfer->tag != NULL) && !strcmp(transfer->tag, tag))
31 void transfersetsize(struct transfer *transfer, intmax_t size)
33 transfer->size = size;
34 cb_trsize(transfer, transfer->conduit->udata);
37 void transfersetpos(struct transfer *transfer, intmax_t pos)
40 cb_trpos(transfer, transfer->conduit->udata);
43 static gboolean trupdatetime(struct transfer *transfer)
47 if((transfer->size == -1) || (transfer->pos == -1))
50 if(now - transfer->ckptime >= 10)
52 transfer->cmptime = transfer->ckptime;
53 transfer->cmpsize = transfer->ckpsize;
54 transfer->ckptime = 0;
56 if(transfer->ckptime == 0)
58 transfer->ckptime = now;
59 transfer->ckpsize = transfer->pos;
64 struct transfer *newtransfer(struct conduit *conduit, char *tag, intmax_t size, intmax_t pos)
66 struct transfer *transfer;
68 transfer = smalloc(sizeof(*transfer));
69 memset(transfer, 0, sizeof(*transfer));
71 transfer->tag = sstrdup(tag);
72 transfer->size = size;
74 transfer->timeout = g_timeout_add(1000, (gboolean (*)(gpointer))trupdatetime, transfer);
75 transfer->next = conduit->transfers;
76 transfer->conduit = conduit;
77 if(conduit->transfers != NULL)
78 conduit->transfers->prev = transfer;
79 conduit->transfers = transfer;
80 cb_trnew(transfer, conduit->udata);
84 void freetransfer(struct transfer *transfer)
86 if(transfer->next != NULL)
87 transfer->next->prev = transfer->prev;
88 if(transfer->prev != NULL)
89 transfer->prev->next = transfer->next;
90 if(transfer->conduit->transfers == transfer)
91 transfer->conduit->transfers = transfer->next;
92 cb_trfree(transfer, transfer->conduit->udata);
93 g_source_remove(transfer->timeout);
94 if(transfer->tag != NULL)
99 struct conduit *newconduit(struct conduitiface *iface, void *udata)
101 struct conduit *conduit;
103 conduit = smalloc(sizeof(*conduit));
104 memset(conduit, 0, sizeof(*conduit));
105 conduit->iface = iface;
106 conduit->udata = udata;
107 if(iface->init(conduit))
115 void freeconduit(struct conduit *conduit)
117 conduit->iface->destroy(conduit);
118 while(conduit->transfers != NULL)
119 freetransfer(conduit->transfers);
123 int condtryconn(struct conduit *conduit)
125 if(conduit->state != CNDS_IDLE)
127 if(conduit->iface->connect(conduit))
129 conduit->state = CNDS_SYN;
133 void conddisconn(struct conduit *conduit)
135 while(conduit->transfers != NULL)
136 freetransfer(conduit->transfers);
137 conduit->state = CNDS_IDLE;
138 cb_condstate(conduit, conduit->udata);
141 void condconnected(struct conduit *conduit)
143 conduit->state = CNDS_EST;
144 cb_condstate(conduit, conduit->udata);