Yattm - unified GTK instant-messaging client logo
   [Generated for version 0.2-17 - Mon Jan 6 19:01:23 GMT+1 2003]

Home - Main Page - Data Structures - File List - Data Fields - Globals

service.c

Go to the documentation of this file.
00001 /*
00002  * Yattm 
00003  *
00004  * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #  include <config.h>
00024 #endif
00025 
00026 #include "intl.h"
00027 #include <gdk/gdkprivate.h>
00028 
00029 #ifndef __MINGW32__
00030 #include <gdk/gdkx.h>
00031 #include <X11/Xlib.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <time.h>
00037 #include <stdlib.h>
00038 #include <assert.h>
00039 
00040 #include "globals.h"
00041 #include "account.h"
00042 #include "dialog.h"
00043 #include "util.h"
00044 #include "nomodule.h"
00045 #include "prefs.h"
00046 
00047 #ifdef HAVE_MIT_SAVER_EXTENSION
00048 #include <X11/extensions/scrnsaver.h>
00049 #endif /* HAVE_MIT_SAVER_EXTENSION */
00050 
00051 
00052 static guint idle_timer;
00053 static time_t lastsent = 0;
00054 static int is_idle = 0;
00055 static int scrnsaver_ext = 0;
00056 int NUM_SERVICES=0;     /* Used in prefs.c */
00057 
00058 
00059 /* Following chunk will be moved to a 'real' code file */
00060 /* FIXME: Should be a dynamic array */
00061 struct service eb_services[255];
00062 
00063 /* Add a new service, or replace an existing one */
00064 gint add_service(struct service *Service_Info)
00065 {
00066     int i;
00067     GList *session_prefs=NULL;
00068 
00069     assert(Service_Info);
00070 
00071     eb_debug(DBG_CORE, ">Entering\n");
00072     if(Service_Info->sc->read_prefs_config) {
00073         session_prefs=GetPref(Service_Info->name);
00074         Service_Info->sc->read_prefs_config(session_prefs);
00075     }
00076 
00077     for (i=0; i < NUM_SERVICES; i++ )
00078     {
00079         /* Check to see if another service exists for the same protocol, if so, replace it */
00080         if(!strcasecmp(eb_services[i].name,Service_Info->name))
00081         {
00082                 eb_debug(DBG_CORE, "Replacing %s service\n", Service_Info->name);
00083 //              free(eb_services[i].name);
00084                 free(eb_services[i].sc);
00085                 Service_Info->protocol_id=i;
00086                 memcpy(&eb_services[i], Service_Info, sizeof(struct service));
00087                 refresh_service_contacts(i);
00088                 reload_service_accounts(i);
00089                 eb_debug(DBG_CORE, "<Replaced existing service\n");
00090                 return(i);
00091         }
00092         
00093     }
00094     Service_Info->protocol_id=NUM_SERVICES++;
00095     memcpy(&eb_services[Service_Info->protocol_id], Service_Info, sizeof(struct service));
00096     refresh_service_contacts(i);
00097     reload_service_accounts(Service_Info->protocol_id);
00098     eb_debug(DBG_CORE, "<Added new service \n");
00099     return(Service_Info->protocol_id);
00100 }
00101 
00102 /* This now creates a service if the name is not recognized */
00103 gint get_service_id( gchar * servicename )
00104 {
00105     int i;
00106     char buf[1024];
00107 
00108     for (i=0; i < NUM_SERVICES; i++ )
00109     {
00110         if(strcasecmp(eb_services[i].name,servicename)==0)
00111         {
00112             return i;   
00113         }
00114     }
00115     eb_debug(DBG_CORE, "Creating empty service for %s\n", servicename);
00116     memcpy(&eb_services[NUM_SERVICES], &nomodule_SERVICE_INFO, sizeof(struct service));
00117     eb_services[NUM_SERVICES].sc=eb_nomodule_query_callbacks();
00118     eb_services[NUM_SERVICES].name = strdup(servicename);
00119     eb_services[NUM_SERVICES].protocol_id=NUM_SERVICES;
00120     NUM_SERVICES++;
00121     sprintf(buf, "%s::path", servicename);
00122     cSetLocalPref(buf, "Empty Module");
00123     return(NUM_SERVICES-1);
00124 }
00125 
00126 gchar *get_service_name( gint service_id )
00127 {
00128     if ((service_id >= 0) && (service_id < NUM_SERVICES))
00129     {
00130         return (eb_services[service_id].name);
00131     }
00132 
00133     fprintf(stderr, "warning: unknown service id: %d\n", service_id);
00134     return "unknown";
00135 }
00136 
00137 GList * get_service_list()
00138 {
00139     GList * newlist = NULL;
00140     int i;
00141     for( i = 0; i < NUM_SERVICES; i++ )
00142     {
00143         newlist = g_list_append( newlist, eb_services[i].name );
00144     }
00145     return newlist;
00146 }
00147 
00148 void serv_touch_idle()
00149 {
00150     /* Are we idle?  If so, not anymore */
00151     if (is_idle > 0) {
00152         GList * node;
00153         is_idle = 0;
00154         for(node = accounts; node; node = node->next )
00155         {
00156             if(((eb_local_account *)(node->data))->connected)
00157             {
00158                 if(RUN_SERVICE(((eb_local_account*)(node->data)))->set_idle)
00159                 {
00160                     RUN_SERVICE(((eb_local_account*)node->data))->set_idle(
00161                                  (eb_local_account*)node->data, 0);
00162                 }
00163             }
00164         }
00165     }
00166     time(&lastsent);
00167 }
00168 
00169 static guint idle_time = 0;
00170 
00171 static gint report_idle()
00172 {
00173     GList * node;
00174 
00175     if (!is_idle)
00176         return FALSE;
00177 
00178     for(node = accounts; node; node = node->next )
00179     {
00180         if(((eb_local_account *)node->data)->connected)
00181         {
00182             if(RUN_SERVICE(((eb_local_account*)node->data))->set_idle)
00183             {
00184                 RUN_SERVICE(((eb_local_account*)node->data))->set_idle(
00185                         (eb_local_account*)node->data, idle_time);
00186             }
00187         }
00188     }
00189     return TRUE;
00190 }
00191 
00192 static gint check_idle()
00193 {
00194     gint idle_reporter = -1;
00195 
00196 #ifdef HAVE_MIT_SAVER_EXTENSION
00197     if (scrnsaver_ext) {
00198         static XScreenSaverInfo * mit_info = NULL;
00199         mit_info = XScreenSaverAllocInfo();
00200         XScreenSaverQueryInfo(gdk_display, DefaultRootWindow(gdk_display), mit_info);
00201         idle_time = mit_info->idle/1000;
00202         free(mit_info);
00203     } else
00204 #endif
00205     {
00206         time_t t;
00207 
00208         if (is_idle)
00209             return TRUE;
00210         time(&t);
00211         
00212         idle_time = t - lastsent;
00213     }
00214     if ((idle_time >= 600) && do_send_idle_time) {
00215         if (is_idle == 0) {
00216             GList * node;
00217             idle_reporter = gtk_timeout_add(60000, (GtkFunction)report_idle, NULL);
00218             for(node = accounts; node; node = node->next )
00219             {
00220                 if(((eb_local_account *)node->data)->connected)
00221                 {
00222                     if(RUN_SERVICE(((eb_local_account*)node->data))->set_idle)
00223                     {
00224                         RUN_SERVICE(((eb_local_account*)node->data))->set_idle(
00225                                 (eb_local_account*)node->data, idle_time);
00226                     }
00227                 }
00228             }
00229         }
00230         is_idle = 1;
00231     } else if ((idle_time < 600) && do_send_idle_time && (is_idle == 1)) {
00232         if (idle_reporter != -1)
00233             gtk_idle_remove(idle_reporter);
00234         serv_touch_idle();
00235     }
00236     return TRUE;
00237 
00238 }
00239 
00240 void add_idle_check()
00241 {
00242     int eventnum, errornum;
00243     idle_timer = gtk_timeout_add(5000, (GtkFunction)check_idle, NULL);
00244     serv_touch_idle();
00245 #ifdef HAVE_MIT_SAVER_EXTENSION
00246     if (XScreenSaverQueryExtension(gdk_display, &eventnum, &errornum)) {
00247         scrnsaver_ext = 1;
00248     }
00249 #endif
00250 }
00251 
00252 //FIXME: Don't blow away an existing log
00253 void rename_nick_log(char *oldnick, char *newnick)
00254 {
00255    gchar oldnicklog[255], newnicklog[255];
00256    strcpy(oldnicklog, config_dir);
00257    strcat(oldnicklog, "logs/");
00258    strcpy(newnicklog, oldnicklog);
00259    strcat(oldnicklog, oldnick);
00260    strcat(newnicklog, newnick);
00261    rename(oldnicklog, newnicklog);
00262    printf("Renamed log from %s to %s\n", oldnicklog, newnicklog);
00263 }
00264 

Contact: Andy Maloney     [Documentation generated by doxygen]