~larsu/evolution-indicator/messaging-menu-fixes

« back to all changes in this revision

Viewing changes to src/evolution-indicator.c

  • Committer: Lars Uebernickel
  • Date: 2013-03-21 23:13:00 UTC
  • Revision ID: lars.uebernickel@canonical.com-20130321231300-9j7i884gsh3plvt6
Update the list of accounts on-the-fly

We don't need any information about sources until new messages arrive.  Create
the account objects then, instead of listening to "source-added" and
"source-removed" signals.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    gint n_count;
101
101
} MessagingMailAccount;
102
102
MessagingMailAccount * find_account_for_uid(GSList*, const gchar*);
 
103
static MessagingMailAccount * ensure_account_for_uid(const gchar*);
103
104
 
104
105
static GtkWidget *
105
106
get_cfg_widget (void)
201
202
 
202
203
  g_debug ("EI:mail_new_notify: %s/%s", uid, t->folder_name);
203
204
 
204
 
  account = find_account_for_uid (accounts, uid);
 
205
  account = ensure_account_for_uid (uid);
205
206
  if(account) {
206
207
        account->n_count += t->new;
207
208
        message_count += t->new;
296
297
 
297
298
  g_debug ("EI: mail_read_notify");
298
299
 
299
 
  account = find_account_for_uid (accounts, uid);
 
300
  account = ensure_account_for_uid (uid);
300
301
  if(account)
301
302
  {
302
303
      messaging_menu_app_remove_source (MESSAGING_MENU_APP (mmapp), uid);
420
421
    g_free(m);
421
422
}
422
423
 
423
 
gboolean
424
 
account_name_and_uid_from_source (ESource  *source,
425
 
                                  gchar   **name,
426
 
                                  gchar   **uid)
427
 
{
428
 
  ESourceExtension *extension;
429
 
  const gchar *protocol;
430
 
 
431
 
  if (!e_source_get_removable(source) || !e_source_get_enabled(source))
432
 
    return FALSE;
433
 
 
434
 
  extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
435
 
  protocol = e_source_backend_dup_backend_name (E_SOURCE_BACKEND (extension));
436
 
 
437
 
  if (g_str_has_prefix (protocol, "pop"))
438
 
    *name = g_strdup (g_dgettext (EVO_I18N_DOMAIN, "Inbox"));
439
 
  else
440
 
    *name = e_source_dup_display_name (source);
441
 
 
442
 
  *uid = e_source_dup_uid (source);
443
 
 
444
 
  return TRUE;
445
 
}
446
 
 
447
 
static void
448
 
update_accounts (void)
449
 
{
450
 
  ESourceRegistry * registry = NULL;
451
 
  GError   *error = NULL;
 
424
static ESource *
 
425
mail_source_from_uid (const gchar *uid)
 
426
{
 
427
  ESourceRegistry *registry;
 
428
  GError *error = NULL;
 
429
  GList *sources;
 
430
  GList *it;
 
431
  ESource *source = NULL;
452
432
 
453
433
  registry = e_source_registry_new_sync (NULL, &error);
454
 
 
455
 
  if (!registry || error)
456
 
    {
457
 
      g_warning ("Failed to get access to source registry: %s\n"
458
 
                 "defaulting number of account to '1'",
459
 
                 error ? error->message : "unknown");
460
 
      if (error)
461
 
        g_error_free (error);
462
 
    }
463
 
  else
464
 
    {
465
 
      GList *list_sources = e_source_registry_list_sources (registry, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
466
 
      GList *a;
467
 
 
468
 
      g_slist_free_full (accounts, (GDestroyNotify) unref_account);
469
 
 
470
 
      for (a = list_sources; a; a = a->next)
471
 
        {
472
 
          ESource *source = E_SOURCE (a->data);
473
 
          gchar *name;
474
 
          gchar *uid;
475
 
 
476
 
          if (account_name_and_uid_from_source (source, &name, &uid))
477
 
            {
478
 
              MessagingMailAccount *account;
479
 
 
480
 
              account = g_new(MessagingMailAccount, 1);
481
 
              account->name = g_strdup(name);
482
 
              account->uid = g_strdup(uid);
483
 
              account->n_count = 0;
484
 
              accounts = g_slist_append (accounts, account);
485
 
              g_debug ("EI: New account: %s (%s)", name, uid);
486
 
 
487
 
              g_free (name);
488
 
              g_free (uid);
489
 
            }
490
 
        }
491
 
 
492
 
      g_list_free_full (list_sources, g_object_unref);
493
 
    }
 
434
  if (!registry)
 
435
    {
 
436
      g_warning ("Failed to get access to source registry: %s\n", error->message);
 
437
      g_error_free (error);
 
438
      return NULL;
 
439
    }
 
440
 
 
441
  sources = e_source_registry_list_sources (registry, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
 
442
  for (it = sources; it; it = it->next)
 
443
    {
 
444
      if (g_str_equal (uid, e_source_get_uid (it->data)))
 
445
        {
 
446
          source = g_object_ref (it->data);
 
447
          break;
 
448
        }
 
449
    }
 
450
 
 
451
  g_list_free_full (sources, g_object_unref);
 
452
  g_object_unref (registry);
 
453
  return source;
 
454
}
 
455
 
 
456
static MessagingMailAccount *
 
457
ensure_account_for_uid (const gchar *uid)
 
458
{
 
459
  MessagingMailAccount *account;
 
460
  ESource *source;
 
461
 
 
462
  if ((account = find_account_for_uid (accounts, uid)))
 
463
    return account;
 
464
 
 
465
  if ((source = mail_source_from_uid (uid)))
 
466
    {
 
467
      if (e_source_get_removable(source) && e_source_get_enabled(source))
 
468
        {
 
469
          ESourceExtension *extension;
 
470
          const gchar *protocol;
 
471
 
 
472
          extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
 
473
          protocol = e_source_backend_get_backend_name (E_SOURCE_BACKEND (extension));
 
474
 
 
475
          account = g_new(MessagingMailAccount, 1);
 
476
          account->uid = g_strdup (uid);
 
477
          account->n_count = 0;
 
478
 
 
479
          if (g_str_has_prefix (protocol, "pop"))
 
480
            account->name = g_strdup (g_dgettext (EVO_I18N_DOMAIN, "Inbox"));
 
481
          else
 
482
            account->name = e_source_dup_display_name (source);
 
483
 
 
484
          accounts = g_slist_append (accounts, account);
 
485
          g_debug ("EI: New account: %s (%s)", account->name, account->uid);
 
486
        }
 
487
 
 
488
      g_object_unref (source);
 
489
    }
 
490
 
 
491
  return account;
494
492
}
495
493
 
496
494
#define EVO_CONTACTS_CMD  "evolution -c contacts"
561
559
    show_count_id = gconf_client_notify_add (client, SHOW_NEW_IN_PANEL, 
562
560
                             show_new_in_panel_changed, NULL, NULL, NULL);
563
561
 
564
 
    update_accounts ();
565
 
 
566
 
    // TODO: find a way to be notify and run update_accounts(); on accounts changes
567
 
 
568
562
    if (show_count)
569
563
      messaging_menu_app_register (mmapp);
570
564
    else
772
766
    gtk_action_activate (action);
773
767
 
774
768
    /* Setup the indicators */
775
 
    account = find_account_for_uid (accounts, uid);
 
769
    account = ensure_account_for_uid (uid);
776
770
    if(account)
777
771
    {
778
772
      gchar *uid = account->uid;