summaryrefslogtreecommitdiff
path: root/src/conn-mail-notif.c
blob: 6a2c70cc4505f913787e78b0a16e923b3460b7b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
 * conn-mail-notif - Gabble mail notification interface
 * Copyright (C) 2009 Collabora Ltd.
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* This is implementation of Gmail Notification protocol as documented at
 * http://code.google.com/intl/fr/apis/talk/jep_extensions/gmail.html
 * This is the only protocol supported at the moment. To add new protocol,
 * one would have to split the google specific parts wich are the
 * update_unread_mails() function and the new-mail signal on google xml
 * namespace. The data structure and suscription mechanism shall remain across
 * protocols.
 */

#include "config.h"
#include "conn-mail-notif.h"
#include "namespaces.h"

#include <string.h>

#include "extensions/extensions.h"
#include <telepathy-glib/svc-connection.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>

#define DEBUG_FLAG GABBLE_DEBUG_MAIL_NOTIF

#include "connection.h"
#include "debug.h"
#include "util.h"


enum
{
  PROP_MAIL_NOTIFICATION_FLAGS,
  PROP_UNREAD_MAIL_COUNT,
  PROP_UNREAD_MAILS,
  PROP_MAIL_ADDRESS,
  NUM_OF_PROP,
};

struct MailsHelperData
{
  GabbleConnection *conn;
  GHashTable *unread_mails;
  GHashTable *old_mails;
  GPtrArray *mails_added;
};


static GPtrArray empty_array = { 0 };


static void unsubscribe (GabbleConnection *conn, const gchar *name,
    gboolean remove_all);
static void update_unread_mails (GabbleConnection *conn);


static void
sender_name_owner_changed (TpDBusDaemon *dbus_daemon,
                           const gchar *name,
                           const gchar *new_owner,
                           gpointer user_data)
{
  GabbleConnection *conn = GABBLE_CONNECTION (user_data);

  if (CHECK_STR_EMPTY (new_owner))
    {
      DEBUG ("Sender removed: %s", name);
      unsubscribe (conn, name, TRUE);
    }
}


static void
unsubscribe (GabbleConnection *conn,
    const gchar *name, gboolean remove_all)
{
  guint count;

  g_return_if_fail (g_hash_table_size (conn->mail_subscribers) > 0);

  count = GPOINTER_TO_UINT (
          g_hash_table_lookup (conn->mail_subscribers, name));

  if (count == 1 || remove_all)
    {
      tp_dbus_daemon_cancel_name_owner_watch (conn->daemon, name,
          sender_name_owner_changed, conn);

      g_hash_table_remove (conn->mail_subscribers, name);

      if (g_hash_table_size (conn->mail_subscribers) == 0)
        {
          DEBUG ("Last sender unsubscribed, cleaning up!");
          g_free (conn->inbox_url);
          conn->inbox_url = NULL;
          if (conn->unread_mails)
            {
              g_hash_table_unref (conn->unread_mails);
              conn->unread_mails = NULL;
            }
        }
    }
  else
    g_hash_table_insert (conn->mail_subscribers, g_strdup(name),
        GUINT_TO_POINTER(--count));
}


static inline gboolean
check_supported_or_dbus_return (GabbleConnection *conn,
    DBusGMethodInvocation *context)
{
  if (TP_BASE_CONNECTION (conn)->status != TP_CONNECTION_STATUS_CONNECTED)
    {
      GError e = { TP_ERRORS, TP_ERROR_DISCONNECTED, "Not connected" };
      dbus_g_method_return_error (context, &e);
      return TRUE;
    }
  
  if (!(conn->features & GABBLE_CONNECTION_FEATURES_GOOGLE_MAIL_NOTIFY))
    {
      tp_dbus_g_method_return_not_implemented (context);
      return TRUE;
    }

  return FALSE;
}


static void
gabble_mail_notification_subscribe (GabbleSvcConnectionInterfaceMailNotification *iface,
    DBusGMethodInvocation *context)
{
  GabbleConnection *conn = GABBLE_CONNECTION (iface);
  gchar *sender;
  guint count;

  if (check_supported_or_dbus_return(conn, context))
      return;

  sender = dbus_g_method_get_sender (context);

  DEBUG ("Subscribe called by: %s", sender);

  count = GPOINTER_TO_UINT (
      g_hash_table_lookup (conn->mail_subscribers, sender));

  /* Gives sender ownership to mail_subscribers hash table */
  g_hash_table_insert (conn->mail_subscribers, sender, 
      GUINT_TO_POINTER (++count));

  if (count == 1)
    {
      if (g_hash_table_size (conn->mail_subscribers) == 1)
        update_unread_mails(conn);
     
      tp_dbus_daemon_watch_name_owner (conn->daemon, sender,
          sender_name_owner_changed, conn, NULL);
    }

  gabble_svc_connection_interface_mail_notification_return_from_subscribe (
      context);
}


static void
gabble_mail_notification_unsubscribe (GabbleSvcConnectionInterfaceMailNotification *iface,
    DBusGMethodInvocation *context)
{
  GabbleConnection *conn = GABBLE_CONNECTION (iface);
  gchar *sender;

  if (check_supported_or_dbus_return(conn, context))
      return;

  sender = dbus_g_method_get_sender (context);

  DEBUG ("Unsubscribe called by: %s", sender);

  if (!g_hash_table_lookup_extended (conn->mail_subscribers, sender,
                                    NULL, NULL))
    {
      GError e = { TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "Not subscribed" };

      DEBUG ("Sender '%s' is not subscribed!", sender);

      dbus_g_method_return_error (context, &e);
      g_free (sender);
      return;
    }

  unsubscribe (conn, sender, FALSE);

  g_free (sender);
  gabble_svc_connection_interface_mail_notification_return_from_unsubscribe (context);
}


static void
gabble_mail_notification_request_inbox_url (
    GabbleSvcConnectionInterfaceMailNotification *iface,
    DBusGMethodInvocation *context)
{
  GabbleConnection *conn = GABBLE_CONNECTION (iface);
  GValueArray *result;

  if (check_supported_or_dbus_return(conn, context))
    return;

  /* TODO Make sure we don't have to authenticate again */

  result = tp_value_array_build (3,
      G_TYPE_STRING, conn->inbox_url ?: "",
      G_TYPE_UINT, GABBLE_HTTP_METHOD_GET,
      GABBLE_ARRAY_TYPE_HTTP_POST_DATA_LIST, &empty_array,
      G_TYPE_INVALID);
  
  gabble_svc_connection_interface_mail_notification_return_from_request_inbox_url (
      context, result);

  g_value_array_free (result);
}


static void
gabble_mail_notification_request_mail_url (
    GabbleSvcConnectionInterfaceMailNotification *iface,
    const gchar *in_id,
    const gchar *in_url_data,
    DBusGMethodInvocation *context)
{
  GabbleConnection *conn = GABBLE_CONNECTION (iface);
  GValueArray *result;
  gchar *url = NULL;

  if (check_supported_or_dbus_return(conn, context))
    return;

  /* TODO Make sure we don't have to authenticate again */

  if (conn->inbox_url)
    {
      guint64 tid = g_ascii_strtoull (in_id, NULL, 0);
      url = g_strdup_printf ("%s/#inbox/%" G_GINT64_MODIFIER "x",
          conn->inbox_url, tid);
    }

  result = tp_value_array_build (3,
      G_TYPE_STRING, url ?: "",
      G_TYPE_UINT, GABBLE_HTTP_METHOD_GET,
      GABBLE_ARRAY_TYPE_HTTP_POST_DATA_LIST, &empty_array,
      G_TYPE_INVALID);

  gabble_svc_connection_interface_mail_notification_return_from_request_mail_url (
      context, result);

  g_value_array_free (result);

}


static gboolean
sender_each (WockyXmppNode *node,
    gpointer user_data)
{
  GPtrArray *senders = user_data;

  if (!tp_strdiff ("1", wocky_xmpp_node_get_attribute (node, "unread")))
    {
      GType addr_type = GABBLE_STRUCT_TYPE_MAIL_ADDRESS;
      GValue sender = {0};

      g_value_init (&sender, addr_type);
      g_value_set_static_boxed (&sender,
          dbus_g_type_specialized_construct (addr_type));

      dbus_g_type_struct_set (&sender,
          0, wocky_xmpp_node_get_attribute (node, "name") ?: "",
          1, wocky_xmpp_node_get_attribute (node, "address") ?: "",
          G_MAXUINT);

      g_ptr_array_add (senders, g_value_get_boxed(&sender));
      g_value_unset (&sender);
    }
  return TRUE;
}


static void
handle_senders (WockyXmppNode *parent_node,
    GHashTable *mail,
    gboolean *dirty)
{
  WockyXmppNode *node;

  node = wocky_xmpp_node_get_child (parent_node, "senders");
  if (node)
    {
      GType addr_list_type = GABBLE_ARRAY_TYPE_MAIL_ADDRESS_LIST;
      GPtrArray *senders, *old_senders;

      senders = g_ptr_array_new ();
      wocky_xmpp_node_each_child (node, sender_each, senders);

      old_senders = tp_asv_get_boxed (mail, "senders", addr_list_type);
      if (!old_senders || senders->len != old_senders->len)
            *dirty = TRUE;

      tp_asv_take_boxed (mail, "senders", addr_list_type, senders);
    }
}


static void
handle_subject (WockyXmppNode *parent_node,
    GHashTable *mail,
    gboolean *dirty)
{
  WockyXmppNode *node;

  node = wocky_xmpp_node_get_child (parent_node, "subject");
  if (node)
    {
      if (tp_strdiff (node->content, tp_asv_get_string (mail, "subject")))
        {
          *dirty = TRUE;
          tp_asv_set_string (mail, "subject", node->content);
        }
    }
}


static void
handle_snippet (WockyXmppNode *parent_node,
    GHashTable *mail,
    gboolean *dirty)
{
  WockyXmppNode *node;

  node = wocky_xmpp_node_get_child (parent_node, "snippet");
  if (node)
    {
      if (tp_strdiff (node->content, tp_asv_get_string (mail, "content")))
        {
          *dirty = TRUE;
          tp_asv_set_boolean (mail, "truncated", TRUE);
          tp_asv_set_string (mail, "content", node->content);
        }
    }
}


static gboolean
mail_thread_info_each (WockyXmppNode *node,
    gpointer user_data)
{
  struct MailsHelperData *data = user_data;

  if (!tp_strdiff (node->name, "mail-thread-info"))
    {
      GHashTable *mail = NULL;
      const gchar *val_str;
      gchar *tid;
      gboolean dirty = FALSE;

      val_str = wocky_xmpp_node_get_attribute (node, "tid");

      /* We absolutly need an ID */
      if (!val_str)
        return TRUE;

      tid = g_strdup (val_str);

      if (data->old_mails)
        mail = g_hash_table_lookup (data->old_mails, tid);

      if (mail)
        {
          g_hash_table_ref (mail);
          g_hash_table_remove (data->old_mails, tid);
        }
      else
        {
          mail = tp_asv_new ("id", G_TYPE_STRING, tid,
                             "type", G_TYPE_UINT, GABBLE_MAIL_TYPE_THREAD,
                             "url-data", G_TYPE_STRING, "",
                             NULL);
          dirty = TRUE;
        }

      val_str = wocky_xmpp_node_get_attribute (node, "date");
      if (val_str)
        {
          gint64 date;

          date = (g_ascii_strtoll (val_str, NULL, 0) / 1000l);
          if (date != tp_asv_get_int64 (mail, "received-timestamp", NULL))
            dirty = TRUE;

          tp_asv_set_int64 (mail, "received-timestamp", date);
        }

      handle_senders (node, mail, &dirty);
      handle_subject (node, mail, &dirty);
      handle_snippet (node, mail, &dirty);

      /* gives tid ownership to unread_mails hash table */
      g_hash_table_insert (data->unread_mails, tid, mail);
      if (dirty)
        g_ptr_array_add (data->mails_added, mail);
    }

  return TRUE;
}


static void
store_unread_mails (GabbleConnection *conn,
    WockyXmppNode *mailbox)
{
  GHashTableIter iter;
  GPtrArray *mails_removed;
  struct MailsHelperData data;
  const gchar *url;

  data.unread_mails = g_hash_table_new_full (g_str_hash, g_str_equal,
      g_free, (GDestroyNotify) g_hash_table_unref);
  data.conn = conn;
  data.old_mails = conn->unread_mails;
  conn->unread_mails = data.unread_mails;
  data.mails_added = g_ptr_array_new ();

  url = wocky_xmpp_node_get_attribute (mailbox, "url");
  g_free (conn->inbox_url);
  conn->inbox_url = g_strdup (url);

  /* Store new mails */
  wocky_xmpp_node_each_child (mailbox, mail_thread_info_each, &data);

  /* Generate the list of removed thread IDs */
  mails_removed = g_ptr_array_new_with_free_func (g_free);
  if (data.old_mails)
    {
      gpointer key;

      g_hash_table_iter_init (&iter, data.old_mails);
      while (g_hash_table_iter_next (&iter, &key, NULL))
        {
          gchar *tid = key;
          g_ptr_array_add (mails_removed, g_strdup (tid));
        }

      g_hash_table_unref (data.old_mails);
    }
  g_ptr_array_add (mails_removed, NULL);

  if (data.mails_added->len || mails_removed->len)
    gabble_svc_connection_interface_mail_notification_emit_unread_mails_changed (
        conn, g_hash_table_size (conn->unread_mails), data.mails_added, 
        (const char **)mails_removed->pdata);

  DEBUG ("reached");

  g_ptr_array_free (data.mails_added, TRUE);
  g_ptr_array_free (mails_removed, TRUE);
}


static void
query_unread_mails_cb (GObject *source_object,
    GAsyncResult *res,
    gpointer user_data)
{
  GError *error = NULL;
  gchar *result_str;
  WockyXmppNode *node;
  WockyPorter *porter = WOCKY_PORTER (source_object);
  WockyXmppStanza *reply = wocky_porter_send_iq_finish (porter, res, &error);

  if (error)
    {
      DEBUG ("Failed retreive unread emails information: %s", error->message);
      g_error_free (error);
      goto end;
    }

  DEBUG ("Got unread mail details");

  result_str = wocky_xmpp_node_to_string (reply->node);
  DEBUG ("%s", result_str);
  g_free (result_str);

  node = wocky_xmpp_node_get_child (reply->node, "mailbox");
  if (node)
    {
      GabbleConnection *conn = GABBLE_CONNECTION (user_data);
      store_unread_mails (conn, node);
    }

end:
  if (reply)
    g_object_unref (reply);
}


static void
update_unread_mails (GabbleConnection *conn)
{
  WockyXmppStanza *query;
  WockyPorter *porter = wocky_session_get_porter (conn->session);

  DEBUG ("Updating unread mails information");

  query = wocky_xmpp_stanza_build (WOCKY_STANZA_TYPE_IQ,
      WOCKY_STANZA_SUB_TYPE_GET, NULL, NULL,
      WOCKY_NODE, "query",
      WOCKY_NODE_XMLNS, NS_GOOGLE_MAIL_NOTIFY,
      WOCKY_NODE_END,
      WOCKY_STANZA_END);
  wocky_porter_send_iq_async (porter, query, NULL, query_unread_mails_cb, conn);
  g_object_unref (query);
}


static gboolean
new_mail_handler (WockyPorter *porter,
    WockyXmppStanza *stanza,
    gpointer user_data)
{
  GabbleConnection *conn = GABBLE_CONNECTION (user_data);

  if (g_hash_table_size(conn->mail_subscribers) > 0)
    {
      DEBUG ("Got Google <new-mail> notification");
      update_unread_mails (conn);
    }
  return TRUE;
}


static void
connection_status_changed (GabbleConnection *conn,
    TpConnectionStatus status,
    TpConnectionStatusReason reason,
    gpointer user_data)
{
  if (status == TP_CONNECTION_STATUS_CONNECTED
      && conn->features & GABBLE_CONNECTION_FEATURES_GOOGLE_MAIL_NOTIFY)
    {
      DEBUG ("Connected, registering Google 'new-mail' notification");

      conn->new_mail_handler_id =
        wocky_porter_register_handler (wocky_session_get_porter (conn->session),
            WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_SET,
            NULL, WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
            new_mail_handler, conn,
            WOCKY_NODE, "new-mail",
              WOCKY_NODE_XMLNS, NS_GOOGLE_MAIL_NOTIFY,
              WOCKY_NODE_END,
              WOCKY_STANZA_END);
    }
}


void
conn_mail_notif_init (GabbleConnection *conn)
{
  GError *error = NULL;

  conn->daemon = tp_dbus_daemon_dup (&error);
  if (!conn->daemon)
    {
      DEBUG ("Failed to connect to dbus daemon: %s", error->message);
      g_error_free (error);
    }

  conn->mail_subscribers = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, NULL);
  conn->inbox_url = NULL;
  conn->unread_mails = NULL;

  g_signal_connect (conn, "status-changed",
      G_CALLBACK (connection_status_changed), conn);
}


static gboolean
foreach_cancel_watch (gpointer key,
    gpointer value,
    gpointer user_data)
{
  const gchar *sender_name = key;
  GabbleConnection *conn = GABBLE_CONNECTION (user_data);

  tp_dbus_daemon_cancel_name_owner_watch (conn->daemon,
      sender_name, sender_name_owner_changed, conn);

  return TRUE;
}


void
conn_mail_notif_dispose (GabbleConnection *conn)
{
  if (conn->daemon)
    {
      g_hash_table_foreach_remove (conn->mail_subscribers,
          foreach_cancel_watch, conn);
      g_hash_table_unref (conn->mail_subscribers);
      conn->mail_subscribers = NULL;
      g_object_unref (conn->daemon);
      conn->daemon = NULL;
    }

  g_free (conn->inbox_url);
  conn->inbox_url = NULL;
  if (conn->unread_mails)
    g_hash_table_unref (conn->unread_mails);
  conn->unread_mails = NULL;

  if (conn->new_mail_handler_id)
    {
      WockyPorter *porter = wocky_session_get_porter (conn->session);
      wocky_porter_unregister_handler (porter, conn->new_mail_handler_id);
      conn->new_mail_handler_id = 0;
    }
}


void
conn_mail_notif_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  GabbleSvcConnectionInterfaceMailNotificationClass *klass = g_iface;

#define IMPLEMENT(x) gabble_svc_connection_interface_mail_notification_implement_##x (\
    klass, gabble_mail_notification_##x)
  IMPLEMENT(subscribe);
  IMPLEMENT(unsubscribe);
  IMPLEMENT(request_inbox_url);
  IMPLEMENT(request_mail_url);
#undef IMPLEMENT
}


static GPtrArray *
get_unread_mails (GabbleConnection *conn)
{
  GPtrArray *mails = g_ptr_array_new ();
  GHashTableIter iter;
  gpointer value;

  if (conn->unread_mails)
    {
      g_hash_table_iter_init (&iter, conn->unread_mails);
      while (g_hash_table_iter_next (&iter, NULL, &value))
        {
          GHashTable *mail = value;
          g_ptr_array_add (mails, mail);
        }
    }

  return mails;
}


void
conn_mail_notif_properties_getter (GObject *object,
    GQuark interface,
    GQuark name,
    GValue *value,
    gpointer getter_data)
{
  static GQuark prop_quarks[NUM_OF_PROP] = {0};
  GabbleConnection *conn = GABBLE_CONNECTION (object);

  if (G_UNLIKELY (prop_quarks[0] == 0))
    {
      prop_quarks[PROP_MAIL_NOTIFICATION_FLAGS] =
        g_quark_from_static_string ("MailNotificationFlags");
      prop_quarks[PROP_UNREAD_MAIL_COUNT] =
        g_quark_from_static_string ("UnreadMailCount");
      prop_quarks[PROP_UNREAD_MAILS] =
        g_quark_from_static_string ("UnreadMails");
      prop_quarks[PROP_MAIL_ADDRESS] =
        g_quark_from_static_string ("MailAddress");
    }

  DEBUG ("MailNotification get property %s", g_quark_to_string (name));

  if (name == prop_quarks[PROP_MAIL_NOTIFICATION_FLAGS])
    {
      if (conn->features & GABBLE_CONNECTION_FEATURES_GOOGLE_MAIL_NOTIFY)
        g_value_set_uint (value,
            GABBLE_MAIL_NOTIFICATION_FLAG_SUPPORTS_UNREAD_MAIL_COUNT
            | GABBLE_MAIL_NOTIFICATION_FLAG_SUPPORTS_UNREAD_MAILS
            | GABBLE_MAIL_NOTIFICATION_FLAG_SUPPORTS_REQUEST_INBOX_URL
            | GABBLE_MAIL_NOTIFICATION_FLAG_SUPPORTS_REQUEST_MAIL_URL
            );
      else
        g_value_set_uint (value, 0);
    }
  else if (name == prop_quarks[PROP_UNREAD_MAIL_COUNT])
    g_value_set_uint (value,
        conn->unread_mails ? g_hash_table_size (conn->unread_mails) : 0);
  else if (name == prop_quarks[PROP_UNREAD_MAILS])
    {
      GPtrArray *mails = get_unread_mails(conn);
      g_value_set_boxed (value, mails);
      g_ptr_array_free (mails, TRUE);
    }
  else if (name == prop_quarks[PROP_MAIL_ADDRESS])
    {
      gchar *address, *username, *stream_server;

      g_object_get (object, "username", &username, NULL);
      g_object_get (object, "stream-server", &stream_server, NULL);
      
      address = gabble_encode_jid (username, stream_server, NULL);

      g_free (username);
      g_free (stream_server);
      g_value_take_string (value, address);
    }
  else
    g_assert (!"Unknown mail notification property, please file a bug.");
}