soylent r290 - trunk/libsoylent/example



Author: svenp
Date: Mon Aug 18 10:20:17 2008
New Revision: 290
URL: http://svn.gnome.org/viewvc/soylent?rev=290&view=rev

Log:
added example for "live" functionality

Added:
   trunk/libsoylent/example/example-live.c
Modified:
   trunk/libsoylent/example/Makefile.am
   trunk/libsoylent/example/example.c
   trunk/libsoylent/example/example.h

Modified: trunk/libsoylent/example/Makefile.am
==============================================================================
--- trunk/libsoylent/example/Makefile.am	(original)
+++ trunk/libsoylent/example/Makefile.am	Mon Aug 18 10:20:17 2008
@@ -8,7 +8,8 @@
   example-list.c \
   example-create-person.c \
   example-attribute-handlers.c \
-  example-watch-book.c
+  example-watch-book.c \
+  example-live.c
 
 example_CFLAGS = $(WARN_CFLAGS) $(EXAMPLE_CFLAGS)
 example_LDADD = $(top_builddir)/libsoylent/libsoylent.la $(EXAMPLE_LIBS)

Added: trunk/libsoylent/example/example-live.c
==============================================================================
--- (empty file)
+++ trunk/libsoylent/example/example-live.c	Mon Aug 18 10:20:17 2008
@@ -0,0 +1,115 @@
+/*
+ * libsoylent - people management
+ * 
+ * Copyright (C) 2008 Sven Pfaller, Noya <kalterregen gmx net>
+ * Copyright (C) 2008 Travis Reitter <treitter-dev netdrain com>
+ * Copyright (C) 2008 Rob Taylor <rob taylor codethink co uk>
+ * Copyright (C) 2008 Chris Lord <chris o-hand com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "example.h"
+#include <libsoylent/soylent.h>
+
+static gboolean start_random_chat (gpointer data);
+static void print_person_with_presence (SlPerson *person);
+static void person_presence_changed (SlBook *book, SlPerson *person, gpointer user_data);
+
+static void
+person_presence_changed (SlBook *book, SlPerson *person, gpointer user_data)
+{
+  print_person_with_presence (person);
+}
+
+static void
+print_person_with_presence (SlPerson *person)
+{
+  gchar *status = NULL;
+  gchar *name = sl_person_get_string (person);
+  McPresence presence = sl_entity_get_presence (SL_ENTITY (person));
+  if (presence == MC_PRESENCE_UNSET || presence == MC_PRESENCE_OFFLINE ||
+      presence == MC_PRESENCE_HIDDEN)
+    {
+      status = "of";
+    }
+  if (presence == MC_PRESENCE_EXTENDED_AWAY || presence == MC_PRESENCE_AWAY ||
+      presence == MC_PRESENCE_DO_NOT_DISTURB)
+    {
+      status = "aw";
+    }
+  if (presence == MC_PRESENCE_AVAILABLE)
+    {
+      status = "on";
+    }
+  g_print ("[%s] %s\n", status, name);
+}
+
+void
+live (void)
+{
+  GError *error = NULL;
+  GMainLoop *main_loop = NULL;
+  GList *people = NULL;
+  
+  /* initialize libsoylent */
+  if (!sl_init (&error))
+    {
+      example_error (error);
+    }
+  
+  g_signal_connect (SL_BOOK_DEFAULT, "person-presence-changed", G_CALLBACK (person_presence_changed), NULL);
+  
+  people = sl_book_get_people (SL_BOOK_DEFAULT);
+  for (; people != NULL; people = people->next)
+    {
+      SlPerson *person = people->data;
+      if (sl_entity_has_iminfo (SL_ENTITY (person)))
+        {
+          print_person_with_presence (person);
+        }
+    }
+  
+  g_timeout_add_seconds (5, start_random_chat, NULL);
+  
+  main_loop = g_main_loop_new (NULL, FALSE);
+  g_main_loop_run (main_loop);
+  
+  sl_cleanup ();
+}
+
+static gboolean
+start_random_chat (gpointer data)
+{
+  GError *error = NULL;
+  GList *online_people = sl_book_get_online_people (SL_BOOK_DEFAULT);
+  SlPerson *person = NULL;
+  int index = 0;
+  
+  if (online_people == NULL)
+    {
+      return TRUE;
+    }
+  
+  index = g_random_int_range (0, g_list_length (online_people));
+  person = g_list_nth_data (online_people, index);
+  g_print ("launching a random chat with %s\n", sl_person_get_string (person));
+  if (!sl_person_communicate_chat (person, &error))
+    {
+      example_error (error);
+    }
+  g_list_free (online_people);
+  return FALSE;
+}

Modified: trunk/libsoylent/example/example.c
==============================================================================
--- trunk/libsoylent/example/example.c	(original)
+++ trunk/libsoylent/example/example.c	Mon Aug 18 10:20:17 2008
@@ -44,9 +44,11 @@
 
   /*create_person ();*/
   
-  watch_book ();
+  /*watch_book ();*/
   
   /*attribute_handlers ();*/
   
+  live ();
+  
   return EXIT_SUCCESS;
 }

Modified: trunk/libsoylent/example/example.h
==============================================================================
--- trunk/libsoylent/example/example.h	(original)
+++ trunk/libsoylent/example/example.h	Mon Aug 18 10:20:17 2008
@@ -29,3 +29,4 @@
 void create_person (void);
 void watch_book (void);
 void attribute_handlers (void);
+void live (void);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]