~larsu/indicator-messages/lp1060618

« back to all changes in this revision

Viewing changes to libmessaging-menu/messaging-menu.c

  • Committer: Lars Uebernickel
  • Date: 2012-08-31 17:19:21 UTC
  • Revision ID: lars.uebernickel@canonical.com-20120831171921-57yrig7ulozg93qe
libmessaging-menu: allow changing label and icon of sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
898
898
  return g_simple_action_group_lookup (app->source_actions, source_id) != NULL;
899
899
}
900
900
 
 
901
static GMenuItem *
 
902
g_menu_find_item_with_action (GMenu        *menu,
 
903
                              const gchar  *action,
 
904
                              gint         *out_pos)
 
905
{
 
906
  gint i;
 
907
  gint n_elements;
 
908
  GMenuItem *item = NULL;
 
909
 
 
910
  n_elements = g_menu_model_get_n_items (G_MENU_MODEL (menu));
 
911
 
 
912
  for (i = 0; i < n_elements && item == NULL; i++)
 
913
    {
 
914
      GVariant *attr;
 
915
 
 
916
      item = g_menu_item_new_from_model (G_MENU_MODEL (menu), i);
 
917
      attr = g_menu_item_get_attribute_value (item, G_MENU_ATTRIBUTE_ACTION, G_VARIANT_TYPE_STRING);
 
918
 
 
919
      if (!g_str_equal (action, g_variant_get_string (attr, NULL)))
 
920
        g_clear_object (&item);
 
921
 
 
922
      g_variant_unref (attr);
 
923
    }
 
924
 
 
925
  if (item && out_pos)
 
926
    *out_pos = i - 1;
 
927
 
 
928
  return item;
 
929
}
 
930
 
 
931
static void
 
932
g_menu_replace_item (GMenu     *menu,
 
933
                     gint       pos,
 
934
                     GMenuItem *item)
 
935
{
 
936
  g_menu_remove (menu, pos);
 
937
  g_menu_insert_item (menu, pos, item);
 
938
}
 
939
 
 
940
/**
 
941
 * messaging_menu_app_set_source_label:
 
942
 * @app: a #MessagingMenuApp
 
943
 * @source_id: a source id
 
944
 * @label: the new label for the source
 
945
 *
 
946
 * Changes the label of @source_id to @label.
 
947
 */
 
948
void
 
949
messaging_menu_app_set_source_label (MessagingMenuApp *app,
 
950
                                     const gchar      *source_id,
 
951
                                     const gchar      *label)
 
952
{
 
953
  gint pos;
 
954
  GMenuItem *item;
 
955
 
 
956
  g_return_if_fail (MESSAGING_MENU_IS_APP (app));
 
957
  g_return_if_fail (source_id != NULL);
 
958
  g_return_if_fail (label != NULL);
 
959
 
 
960
  item = g_menu_find_item_with_action (app->menu, source_id, &pos);
 
961
  if (item == NULL)
 
962
    return;
 
963
 
 
964
  g_menu_item_set_attribute (item, G_MENU_ATTRIBUTE_LABEL, "s", label);
 
965
  g_menu_replace_item (app->menu, pos, item);
 
966
 
 
967
  g_object_unref (item);
 
968
}
 
969
 
 
970
/**
 
971
 * messaging_menu_app_set_source_icon:
 
972
 * @app: a #MessagingMenuApp
 
973
 * @source_id: a source id
 
974
 * @icon: the new icon for the source
 
975
 *
 
976
 * Changes the icon of @source_id to @icon.
 
977
 */
 
978
void
 
979
messaging_menu_app_set_source_icon (MessagingMenuApp *app,
 
980
                                    const gchar      *source_id,
 
981
                                    GIcon            *icon)
 
982
{
 
983
  gint pos;
 
984
  GMenuItem *item;
 
985
  gchar *iconstr;
 
986
 
 
987
  g_return_if_fail (MESSAGING_MENU_IS_APP (app));
 
988
  g_return_if_fail (source_id != NULL);
 
989
 
 
990
  item = g_menu_find_item_with_action (app->menu, source_id, &pos);
 
991
  if (item == NULL)
 
992
    return;
 
993
 
 
994
  iconstr = icon ? g_icon_to_string (icon) : NULL;
 
995
  g_menu_item_set_attribute (item, "x-canonical-icon", "s", iconstr);
 
996
  g_menu_replace_item (app->menu, pos, item);
 
997
 
 
998
  g_free (iconstr);
 
999
  g_object_unref (item);
 
1000
}
 
1001
 
901
1002
/**
902
1003
 * messaging_menu_app_set_source_count:
903
1004
 * @app: a #MessagingMenuApp