comparison plugins/lagmeter.c @ 1122:889ca2b8697b

[gaim-migrate @ 1132] wow. that wasn't exactly easy, now was it. i hope other plugins are easier cases to upgrade. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Wed, 22 Nov 2000 07:01:07 +0000
parents 6b013aff4de3
children 5d1ded9f88b7
comparison
equal deleted inserted replaced
1121:d0c35780792b 1122:889ca2b8697b
24 #include <unistd.h> 24 #include <unistd.h>
25 #include <math.h> 25 #include <math.h>
26 26
27 #define MY_LAG_STRING "ZYXCHECKLAGXYZ" 27 #define MY_LAG_STRING "ZYXCHECKLAGXYZ"
28 28
29 void *handle; 29 GModule *handle;
30 GtkWidget *lagbox; 30 GtkWidget *lagbox;
31 GtkWidget *my_lagometer; 31 GtkWidget *my_lagometer;
32 struct timeval my_lag_tv; 32 struct timeval my_lag_tv;
33 int check_timeout = -1; 33 guint check_timeout = 0;
34 guint delay = 10; 34 guint delay = 10;
35 static GtkWidget *confdlg; 35 static GtkWidget *confdlg;
36 36 struct gaim_connection *my_gc = NULL;
37 void update_lag(int us) { 37
38 static void avail_now(struct gaim_connection *, void *);
39
40 static void update_lag(int us) {
38 double pct; 41 double pct;
39 42
40 if (lagbox == NULL) { 43 if (lagbox == NULL) {
41 /* guess we better build it then :P */ 44 /* guess we better build it then :P */
42 GtkWidget *label = gtk_label_new("Lag-O-Meter: "); 45 GtkWidget *label = gtk_label_new("Lag-O-Meter: ");
67 pct /= 100; 70 pct /= 100;
68 71
69 gtk_progress_bar_update(GTK_PROGRESS_BAR(my_lagometer), pct); 72 gtk_progress_bar_update(GTK_PROGRESS_BAR(my_lagometer), pct);
70 } 73 }
71 74
72 void check_lag(struct gaim_connection *gc, char **who, char **message, void *m) { 75 static void check_lag(struct gaim_connection *gc, char **who, char **message, void *m) {
73 char *name = g_strdup(normalize(*who)); 76 char *name;
77 if (gc != my_gc)
78 return;
79
80 name = g_strdup(normalize(*who));
74 if (!strcasecmp(normalize(gc->username), name) && 81 if (!strcasecmp(normalize(gc->username), name) &&
75 (*message != NULL) && 82 (*message != NULL) &&
76 !strcmp(*message, MY_LAG_STRING)) { 83 !strcmp(*message, MY_LAG_STRING)) {
77 struct timeval tv; 84 struct timeval tv;
78 int ms; 85 int ms;
87 *message = NULL; 94 *message = NULL;
88 } 95 }
89 g_free(name); 96 g_free(name);
90 } 97 }
91 98
92 void send_lag(struct gaim_connection *gc) { 99 static gint send_lag(struct gaim_connection *gc) {
93 gettimeofday(&my_lag_tv, NULL); 100 gettimeofday(&my_lag_tv, NULL);
94 serv_send_im(gc, gc->username, MY_LAG_STRING, 1); 101 if (g_slist_find(connections, gc)) {
102 serv_send_im(gc, gc->username, MY_LAG_STRING, 1);
103 return TRUE;
104 } else {
105 debug_printf("LAGMETER: send_lag called for connection that no longer exists\n");
106 check_timeout = 0;
107 return FALSE;
108 }
109 }
110
111 static void got_signoff(struct gaim_connection *gc, void *m) {
112 if (gc != my_gc)
113 return;
114
115 if (check_timeout > 0)
116 gtk_timeout_remove(check_timeout);
117 check_timeout = 0;
118
119 if (confdlg)
120 gtk_widget_destroy(confdlg);
121 confdlg = NULL;
122
123 if (lagbox)
124 gtk_widget_destroy(lagbox);
125 lagbox = NULL;
126
127 if (g_slist_length(connections) > 1) {
128 if (connections->data == my_gc)
129 avail_now(connections->next->data, NULL);
130 else
131 avail_now(connections->data, NULL);
132 } else {
133 my_gc = NULL;
134 }
135 }
136
137 static void avail_now(struct gaim_connection *gc, void *m) {
138 update_lag(0);
139 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, gc);
140 my_gc = gc;
95 } 141 }
96 142
97 void gaim_plugin_remove() { 143 void gaim_plugin_remove() {
98 if (check_timeout != -1) 144 if (check_timeout > 0)
99 gtk_timeout_remove(check_timeout); 145 gtk_timeout_remove(check_timeout);
146 check_timeout = 0;
100 if (confdlg) 147 if (confdlg)
101 gtk_widget_destroy(confdlg); 148 gtk_widget_destroy(confdlg);
102 if (lagbox) 149 if (lagbox)
103 gtk_widget_destroy(lagbox); 150 gtk_widget_destroy(lagbox);
104 151
105 confdlg = NULL; 152 confdlg = NULL;
106 lagbox = NULL; 153 lagbox = NULL;
107 } 154 my_gc = NULL;
108
109 void avail_now(struct gaim_connection *gc, void *m) {
110 update_lag(0);
111 gaim_signal_connect(handle, event_im_recv, check_lag, NULL);
112 gaim_signal_connect(handle, event_signoff, gaim_plugin_remove, NULL);
113 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, gc);
114 } 155 }
115 156
116 char *gaim_plugin_init(GModule *h) { 157 char *gaim_plugin_init(GModule *h) {
117 handle = h; 158 handle = h;
118 159
119 confdlg = NULL; 160 confdlg = NULL;
120 lagbox = NULL; 161 lagbox = NULL;
121 162
122 if (!blist) 163 gaim_signal_connect(handle, event_im_recv, check_lag, NULL);
164 gaim_signal_connect(handle, event_signoff, got_signoff, NULL);
165
166 if (!connections)
123 gaim_signal_connect(handle, event_signon, avail_now, NULL); 167 gaim_signal_connect(handle, event_signon, avail_now, NULL);
124 else 168 else
125 avail_now(connections->data, NULL); 169 avail_now(connections->data, NULL);
126 170
127 return NULL; 171 return NULL;
128 } 172 }
129 173
130 void adjust_timeout(GtkWidget *button, GtkWidget *spinner) { 174 static void adjust_timeout(GtkWidget *button, GtkWidget *spinner) {
131 delay = CLAMP(gtk_spin_button_get_value_as_int( 175 delay = CLAMP(gtk_spin_button_get_value_as_int(
132 GTK_SPIN_BUTTON(spinner)), 0, 3600); 176 GTK_SPIN_BUTTON(spinner)), 0, 3600);
133 sprintf(debug_buff, "new updates: %d\n", delay); 177 debug_printf("LAGMETER: new updates: %d\n", delay);
134 debug_print(debug_buff); 178 if (check_timeout > 0)
135 if (check_timeout >= 0)
136 gtk_timeout_remove(check_timeout); 179 gtk_timeout_remove(check_timeout);
137 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, NULL); 180 check_timeout = 0;
181 if (my_gc)
182 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, my_gc);
138 gtk_widget_destroy(confdlg); 183 gtk_widget_destroy(confdlg);
139 confdlg = NULL; 184 confdlg = NULL;
140 } 185 }
141 186
142 void gaim_plugin_config() { 187 void gaim_plugin_config() {