[gaim-migrate @ 3063]
[pidgin-git.git] / src / idle.c
blob20d9a19295e72ddb7cb80ebbb55bc70d07b0fab2
1 /*
2 * gaim
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <time.h>
26 #include <stdio.h>
27 #include <unistd.h>
29 #ifdef USE_SCREENSAVER
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/extensions/scrnsaver.h>
33 #include <gdk/gdkx.h>
34 #endif /* USE_SCREENSAVER */
36 #include "multi.h"
37 #include "gaim.h"
38 #include "prpl.h"
41 gint check_idle(gpointer data)
43 struct gaim_connection *gc = data;
44 time_t t;
45 #ifdef USE_SCREENSAVER
46 static XScreenSaverInfo *mit_info = NULL;
47 #endif
48 time_t idle_time;
50 /* Not idle, really... :) */
51 update_idle_times();
53 plugin_event(event_blist_update, 0, 0, 0, 0);
55 time(&t);
58 #ifdef USE_SCREENSAVER
59 if (report_idle == IDLE_SCREENSAVER) {
60 int event_base, error_base;
61 if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) {
62 if (mit_info == NULL) {
63 mit_info = XScreenSaverAllocInfo();
65 XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info);
66 idle_time = (mit_info->idle) / 1000;
67 } else
68 idle_time = 0;
69 } else
70 #endif /* USE_SCREENSAVER */
71 idle_time = t - gc->lastsent;
73 if ((away_options & OPT_AWAY_AUTO) && (idle_time > (60 * auto_away)) && (!gc->is_auto_away)) {
74 if (!gc->away) {
75 debug_printf("making %s away automatically\n", gc->username);
76 if (g_slist_length(connections) == 1)
77 do_away_message(NULL, default_away);
78 else if (default_away)
79 serv_set_away(gc, GAIM_AWAY_CUSTOM, default_away->message);
80 gc->is_auto_away = 1;
81 set_default_away(NULL, (gpointer)g_slist_index(away_messages, default_away));
82 } else
83 gc->is_auto_away = 2;
84 } else if (gc->is_auto_away && idle_time < 60 * auto_away) {
85 if (gc->is_auto_away == 2) {
86 gc->is_auto_away = 0;
87 return TRUE;
89 gc->is_auto_away = 0;
90 if (awaymessage == NULL) {
91 debug_printf("removing auto-away message for %s\n", gc->username);
92 serv_set_away(gc, GAIM_AWAY_CUSTOM, NULL);
93 } else {
94 if (g_slist_length(connections) == 1)
95 do_im_back(0, 0);
96 else {
97 debug_printf("replacing auto-away with global for %s\n", gc->username);
98 serv_set_away(gc, GAIM_AWAY_CUSTOM, awaymessage->message);
104 /* If we're not reporting idle times to the server, still use Gaim
105 usage for auto-away, but quit here so we don't report to the
106 server */
107 if (report_idle == IDLE_NONE) {
108 return TRUE;
111 if (idle_time > 600 && !gc->is_idle) { /* 10 minutes! */
112 debug_printf("setting %s idle %d seconds\n", gc->username, idle_time);
113 serv_set_idle(gc, idle_time);
114 gc->is_idle = 1;
115 system_log(log_idle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON);
116 } else if (idle_time < 600 && gc->is_idle) {
117 debug_printf("setting %s unidle\n", gc->username);
118 serv_touch_idle(gc);
119 system_log(log_unidle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON);
122 return TRUE;