Mercurial > pidgin.yaz
annotate plugins/lagmeter.c @ 1287:cbb8d7a9be43
[gaim-migrate @ 1297]
sorry :)
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 15 Dec 2000 02:31:36 +0000 |
parents | 5d1ded9f88b7 |
children | 13e68fb13744 |
rev | line source |
---|---|
193 | 1 /* KNOWN BUGS: |
2 * if you are also using notify.so, it will open a new window to yourself. | |
3 * it will not, however, write anything in that window. this is a problem | |
4 * with notify.c. maybe one day i'll modify notify.c so that these two | |
5 * plugins are more compatible. we'll see. | |
6 * | |
7 * This lagometer has a tendency to not at all show the same lag that the | |
8 * built-in lagometer shows. My guess as to why this is (because they use the | |
9 * exact same code) is because it sends the string more often. That's why I | |
10 * included the configuration option to set the delay between updates. | |
11 * | |
12 * You can load this plugin even when you're not signed on, even though it | |
13 * modifies the buddy list. This is because it checks to see that the buddy | |
14 * list is actually there. In every case that I've been able to think of so | |
15 * far, it does the right thing (tm). | |
16 */ | |
17 | |
18 #define GAIM_PLUGINS | |
19 #include "gaim.h" | |
20 | |
21 #include <time.h> | |
22 #include <sys/types.h> | |
23 #include <sys/time.h> | |
1167
5d1ded9f88b7
[gaim-migrate @ 1177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1122
diff
changeset
|
24 #include <string.h> |
193 | 25 #include <unistd.h> |
26 #include <math.h> | |
27 | |
28 #define MY_LAG_STRING "ZYXCHECKLAGXYZ" | |
29 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
30 GModule *handle; |
193 | 31 GtkWidget *lagbox; |
32 GtkWidget *my_lagometer; | |
33 struct timeval my_lag_tv; | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
34 guint check_timeout = 0; |
193 | 35 guint delay = 10; |
36 static GtkWidget *confdlg; | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
37 struct gaim_connection *my_gc = NULL; |
193 | 38 |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
39 static void avail_now(struct gaim_connection *, void *); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
40 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
41 static void update_lag(int us) { |
193 | 42 double pct; |
43 | |
44 if (lagbox == NULL) { | |
45 /* guess we better build it then :P */ | |
46 GtkWidget *label = gtk_label_new("Lag-O-Meter: "); | |
47 GList *tmp = gtk_container_children(GTK_CONTAINER(blist)); | |
48 GtkWidget *vbox2 = (GtkWidget *)tmp->data; | |
49 lagbox = gtk_hbox_new(FALSE, 0); | |
50 my_lagometer = gtk_progress_bar_new(); | |
51 | |
52 gtk_box_pack_start(GTK_BOX(lagbox), label, FALSE, FALSE, 5); | |
53 gtk_box_pack_start(GTK_BOX(lagbox), my_lagometer, TRUE, TRUE, 5); | |
54 gtk_widget_set_usize(my_lagometer, 5, 5); | |
55 | |
56 gtk_widget_show(label); | |
57 gtk_widget_show(my_lagometer); | |
58 | |
59 gtk_box_pack_start(GTK_BOX(vbox2), lagbox, FALSE, TRUE, 0); | |
60 gtk_box_reorder_child(GTK_BOX(vbox2), lagbox, 1); | |
61 gtk_widget_show(lagbox); | |
62 } | |
63 | |
64 pct = us/100000; | |
65 if (pct > 0) | |
66 pct = 25 * log(pct); | |
67 if (pct < 0) | |
68 pct = 0; | |
69 if (pct > 100) | |
70 pct = 100; | |
71 pct /= 100; | |
72 | |
73 gtk_progress_bar_update(GTK_PROGRESS_BAR(my_lagometer), pct); | |
74 } | |
75 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
76 static void check_lag(struct gaim_connection *gc, char **who, char **message, void *m) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
77 char *name; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
78 if (gc != my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
79 return; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
80 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
81 name = g_strdup(normalize(*who)); |
1000
91b7377e7b45
[gaim-migrate @ 1010]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
983
diff
changeset
|
82 if (!strcasecmp(normalize(gc->username), name) && |
983 | 83 (*message != NULL) && |
193 | 84 !strcmp(*message, MY_LAG_STRING)) { |
85 struct timeval tv; | |
86 int ms; | |
87 | |
88 gettimeofday(&tv, NULL); | |
89 | |
90 ms = 1000000 * (tv.tv_sec - my_lag_tv.tv_sec); | |
91 | |
92 ms += tv.tv_usec - my_lag_tv.tv_usec; | |
93 | |
94 update_lag(ms); | |
95 *message = NULL; | |
96 } | |
97 g_free(name); | |
98 } | |
99 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
100 static gint send_lag(struct gaim_connection *gc) { |
193 | 101 gettimeofday(&my_lag_tv, NULL); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
102 if (g_slist_find(connections, gc)) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
103 serv_send_im(gc, gc->username, MY_LAG_STRING, 1); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
104 return TRUE; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
105 } else { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
106 debug_printf("LAGMETER: send_lag called for connection that no longer exists\n"); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
107 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
108 return FALSE; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
109 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
110 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
111 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
112 static void got_signoff(struct gaim_connection *gc, void *m) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
113 if (gc != my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
114 return; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
115 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
116 if (check_timeout > 0) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
117 gtk_timeout_remove(check_timeout); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
118 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
119 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
120 if (confdlg) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
121 gtk_widget_destroy(confdlg); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
122 confdlg = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
123 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
124 if (lagbox) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
125 gtk_widget_destroy(lagbox); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
126 lagbox = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
127 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
128 if (g_slist_length(connections) > 1) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
129 if (connections->data == my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
130 avail_now(connections->next->data, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
131 else |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
132 avail_now(connections->data, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
133 } else { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
134 my_gc = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
135 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
136 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
137 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
138 static void avail_now(struct gaim_connection *gc, void *m) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
139 update_lag(0); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
140 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, gc); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
141 my_gc = gc; |
193 | 142 } |
143 | |
144 void gaim_plugin_remove() { | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
145 if (check_timeout > 0) |
848
5f19ec4a91f7
[gaim-migrate @ 858]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
193
diff
changeset
|
146 gtk_timeout_remove(check_timeout); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
147 check_timeout = 0; |
193 | 148 if (confdlg) |
149 gtk_widget_destroy(confdlg); | |
983 | 150 if (lagbox) |
151 gtk_widget_destroy(lagbox); | |
152 | |
193 | 153 confdlg = NULL; |
983 | 154 lagbox = NULL; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
155 my_gc = NULL; |
193 | 156 } |
157 | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
158 char *gaim_plugin_init(GModule *h) { |
193 | 159 handle = h; |
160 | |
983 | 161 confdlg = NULL; |
162 lagbox = NULL; | |
163 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
164 gaim_signal_connect(handle, event_im_recv, check_lag, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
165 gaim_signal_connect(handle, event_signoff, got_signoff, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
166 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
167 if (!connections) |
193 | 168 gaim_signal_connect(handle, event_signon, avail_now, NULL); |
169 else | |
1000
91b7377e7b45
[gaim-migrate @ 1010]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
983
diff
changeset
|
170 avail_now(connections->data, NULL); |
1073
6b013aff4de3
[gaim-migrate @ 1083]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
171 |
6b013aff4de3
[gaim-migrate @ 1083]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
172 return NULL; |
193 | 173 } |
174 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
175 static void adjust_timeout(GtkWidget *button, GtkWidget *spinner) { |
193 | 176 delay = CLAMP(gtk_spin_button_get_value_as_int( |
177 GTK_SPIN_BUTTON(spinner)), 0, 3600); | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
178 debug_printf("LAGMETER: new updates: %d\n", delay); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
179 if (check_timeout > 0) |
848
5f19ec4a91f7
[gaim-migrate @ 858]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
193
diff
changeset
|
180 gtk_timeout_remove(check_timeout); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
181 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
182 if (my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
183 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, my_gc); |
193 | 184 gtk_widget_destroy(confdlg); |
185 confdlg = NULL; | |
186 } | |
187 | |
188 void gaim_plugin_config() { | |
189 GtkWidget *label; | |
190 GtkAdjustment *adj; | |
191 GtkWidget *spinner; | |
192 GtkWidget *button; | |
193 GtkWidget *box; | |
194 | |
195 if (confdlg) { | |
983 | 196 gtk_widget_show_all(confdlg); |
193 | 197 return; |
198 } | |
199 | |
200 confdlg = gtk_window_new(GTK_WINDOW_DIALOG); | |
201 gtk_window_set_title(GTK_WINDOW(confdlg), "Gaim Lag Delay"); | |
202 | |
203 box = gtk_hbox_new(FALSE, 0); | |
204 gtk_container_set_border_width(GTK_CONTAINER(box), 5); | |
205 gtk_container_add(GTK_CONTAINER(confdlg), box); | |
206 | |
207 label = gtk_label_new("Delay between updates: "); | |
208 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
209 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0); | |
210 | |
211 adj = (GtkAdjustment *)gtk_adjustment_new(delay, 0, 3600, 1, 0, 0); | |
212 spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0, 0); | |
213 gtk_box_pack_start(GTK_BOX(box), spinner, TRUE, TRUE, 0); | |
214 | |
215 button = gtk_button_new_with_label("OK"); | |
216 gtk_signal_connect(GTK_OBJECT(button), "clicked", | |
217 (GtkSignalFunc)adjust_timeout, spinner); | |
218 gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0); | |
219 | |
983 | 220 gtk_widget_show_all(confdlg); |
193 | 221 } |
222 | |
223 char *name() { | |
224 return "Lag-O-Meter, Pluggified"; | |
225 } | |
226 | |
227 char *description() { | |
228 return "Your old familiar Lag-O-Meter, in a brand new form."; | |
229 } |