1
2
3
4
5
6 #include
7 #include
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
15
16
17
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
27 gchar *name = sl_person_get_string (person);
28 McPresence presence = sl_person_get_presence (person);
29
30
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
47 g_print ("[%s] %s\n", presence_string, name);
48 }
49
50
51
52
53
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
63 GList *online_people = sl_book_get_online_people (SL_BOOK_DEFAULT);
64
65
66 if (online_people == NULL)
67 {
68 return TRUE;
69 }
70
71
72 index = g_random_int_range (0, g_list_length (online_people));
73 person = g_list_nth_data (online_people, index);
74
75
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
100 if (!sl_init (&error))
101 {
102 example_error (error);
103 }
104
105
106 g_signal_connect (SL_BOOK_DEFAULT, "person-presence-changed", G_CALLBACK (person_presence_changed), NULL);
107
108
109 people = sl_book_get_people (SL_BOOK_DEFAULT);
110 for (; people != NULL; people = people->next)
111 {
112 SlPerson *person = people->data;
113
114
115 if (sl_person_has_iminfo (person))
116 {
117 print_person_with_presence (person);
118 }
119 }
120
121
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
129
130 sl_person_add (tom, SL_ATTR_JABBER, "foobar@jabber.org");
131
132
133
134 g_timeout_add_seconds (5, start_random_chat, NULL);
135
136
137 main_loop = g_main_loop_new (NULL, FALSE);
138 g_main_loop_run (main_loop);
139
140 sl_cleanup ();
141 }