example-live

   1 /*
   2  * This example shows the live-functionality of libsoylent, e.g. starting a
   3  * chat with someone or getting the online-presence of a person.
   4  */
   5 
   6 #include "example.h"
   7 #include <libsoylent/soylent.h>
   8 
   9 static void print_person_with_presence (SlPerson *person);
  10 static gboolean start_random_chat (gpointer data);
  11 static void person_presence_changed (SlBook *book, SlPerson *person, gpointer user_data);
  12 
  13 /*
  14  * Prints a person in the form:
  15  *  * [presence] name
  16  * e.g.:
  17  *  * [on] Tim
  18  */
  19 static void
  20 print_person_with_presence (SlPerson *person)
  21 {
  22   g_return_if_fail (person != NULL && SL_IS_PERSON (person));
  23 
  24   gchar *presence_string = NULL;
  25 
  26   /* get a printable name and the online-presence of this person */
  27   gchar *name = sl_person_get_string (person);
  28   McPresence presence = sl_person_get_presence (person);
  29   
  30   /* print presence as on(line), aw(ay) or of(fline) */
  31   if (presence == MC_PRESENCE_UNSET || presence == MC_PRESENCE_OFFLINE ||
  32       presence == MC_PRESENCE_HIDDEN)
  33     {
  34       presence_string = "of";
  35     }
  36   if (presence == MC_PRESENCE_EXTENDED_AWAY || presence == MC_PRESENCE_AWAY ||
  37       presence == MC_PRESENCE_DO_NOT_DISTURB)
  38     {
  39       presence_string = "aw";
  40     }
  41   if (presence == MC_PRESENCE_AVAILABLE)
  42     {
  43       presence_string = "on";
  44     }
  45   
  46   /* print person with presence */
  47   g_print ("[%s] %s\n", presence_string, name);
  48 }
  49 
  50 /*
  51  * Starts a chat with a randomly choosen person that is currently online. This
  52  * is a timeout-function, i.e. returning TRUE means that the function should be
  53  * called again after 5 seconds.
  54  */
  55 static gboolean
  56 start_random_chat (gpointer data)
  57 {
  58   GError *error = NULL;
  59   int index = 0;
  60   SlPerson *person = NULL;
  61   
  62   /* get a list of people that are online */
  63   GList *online_people = sl_book_get_online_people (SL_BOOK_DEFAULT);
  64   
  65   /* if nobody is online, retry in 5 seconds */
  66   if (online_people == NULL)
  67     {
  68       return TRUE;
  69     }
  70   
  71   /* choose a person randomly */
  72   index = g_random_int_range (0, g_list_length (online_people));
  73   person = g_list_nth_data (online_people, index);
  74   
  75   /* launch a chat with the chosen person */
  76   g_print ("launching a random chat with %s\n", sl_person_get_string (person));
  77   if (!sl_person_communicate_chat (person, &error))
  78     {
  79       example_error (error);
  80     }
  81   
  82   g_list_free (online_people);
  83   return FALSE;
  84 }
  85 
  86 static void
  87 person_presence_changed (SlBook *book, SlPerson *person, gpointer user_data)
  88 {
  89   print_person_with_presence (person);
  90 }
  91 
  92 void
  93 live (void)
  94 {
  95   GError *error = NULL;
  96   GMainLoop *main_loop = NULL;
  97   GList *people = NULL;
  98   
  99   /* initialize libsoylent */
 100   if (!sl_init (&error))
 101     {
 102       example_error (error);
 103     }
 104   
 105   /* get notified when the presence of someone changed */
 106   g_signal_connect (SL_BOOK_DEFAULT, "person-presence-changed", G_CALLBACK (person_presence_changed), NULL);
 107   
 108   /* print the name and presence of all people... */
 109   people = sl_book_get_people (SL_BOOK_DEFAULT);
 110   for (; people != NULL; people = people->next)
 111     {
 112       SlPerson *person = people->data;
 113       
 114       /* ... but only where this information is available */
 115       if (sl_person_has_iminfo (person))
 116         {
 117           print_person_with_presence (person);
 118         }
 119     }
 120   
 121   /* add \"Tom Beatle\" to the addressbook */
 122   SlPerson *tom = sl_person_new (g_strdup ("Tom"), g_strdup ("Beatle"));
 123   if (!sl_book_add_person (SL_BOOK_DEFAULT, tom, &error))
 124     {
 125       example_error (error);
 126     }
 127   
 128   /* Set Tom's Jabber account. Now online-information of Tom should be
 129    * available.*/
 130   sl_person_add (tom, SL_ATTR_JABBER, "foobar@jabber.org");
 131   
 132   /* Launch a random chat with someone. If no-one is online, retry every 5
 133    * seconds. */
 134   g_timeout_add_seconds (5, start_random_chat, NULL);
 135   
 136   /* run... */
 137   main_loop = g_main_loop_new (NULL, FALSE);
 138   g_main_loop_run (main_loop);
 139   
 140   sl_cleanup ();
 141 }

Attic/Soylent/libsoylent/example-live (last edited 2018-08-18 14:59:21 by AndreKlapper)