Back to index

indicator-appmenu  12.10.0
huditem.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2012 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Author: Ryan Lortie <desrt@desrt.ca>
00017  */
00018 
00019 #include "huditem.h"
00020 
00021 #include "usage-tracker.h"
00022 #include "hudtoken.h"
00023 
00047 static GHashTable *hud_item_table;
00048 static guint64 hud_item_next_id;
00049 
00050 struct _HudItemPrivate
00051 {
00052   GObject parent_instance;
00053 
00054   gchar *desktop_file;
00055 
00056   HudTokenList *token_list;
00057   HudStringList *tokens;
00058   gchar *usage_tag;
00059   gchar *app_icon;
00060   gboolean enabled;
00061   guint usage;
00062   guint64 id;
00063 };
00064 
00065 G_DEFINE_TYPE (HudItem, hud_item, G_TYPE_OBJECT)
00066 
00067 static void
00068 hud_item_finalize (GObject *object)
00069 {
00070   HudItem *item = HUD_ITEM (object);
00071 
00072   g_hash_table_remove (hud_item_table, &item->priv->id);
00073   hud_token_list_free (item->priv->token_list);
00074   hud_string_list_unref (item->priv->tokens);
00075   g_free (item->priv->desktop_file);
00076   g_free (item->priv->app_icon);
00077   g_free (item->priv->usage_tag);
00078 
00079   G_OBJECT_CLASS (hud_item_parent_class)
00080     ->finalize (object);
00081 }
00082 
00083 static void
00084 hud_item_init (HudItem *item)
00085 {
00086   item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, HUD_TYPE_ITEM, HudItemPrivate);
00087 }
00088 
00089 static void
00090 hud_item_class_init (HudItemClass *class)
00091 {
00092   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
00093 
00094   hud_item_table = g_hash_table_new (g_int64_hash, g_int64_equal);
00095 
00096   gobject_class->finalize = hud_item_finalize;
00097 
00098   g_type_class_add_private (class, sizeof (HudItemPrivate));
00099 }
00100 
00101 static void
00102 hud_item_format_tokens (GString       *string,
00103                         HudStringList *tokens)
00104 {
00105   HudStringList *tail;
00106 
00107   tail = hud_string_list_get_tail (tokens);
00108 
00109   if (tail)
00110     {
00111       hud_item_format_tokens (string, tail);
00112       g_string_append (string, "||");
00113     }
00114 
00115   g_string_append (string, hud_string_list_get_head (tokens));
00116 }
00117 
00118 static void
00119 hud_item_setup_usage (HudItem *item)
00120 {
00121   GString *tag;
00122 
00123   if (item->priv->tokens && item->priv->enabled)
00124     {
00125       tag = g_string_new (NULL);
00126       hud_item_format_tokens (tag, item->priv->tokens);
00127       item->priv->usage_tag = g_string_free (tag, FALSE);
00128       item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
00129                                                    item->priv->desktop_file, item->priv->usage_tag);
00130     }
00131 }
00132 
00148 gpointer
00149 hud_item_construct (GType          g_type,
00150                     HudStringList *tokens,
00151                     const gchar   *desktop_file,
00152                     const gchar   *app_icon,
00153                     gboolean       enabled)
00154 {
00155   HudItem *item;
00156 
00157   item = g_object_new (g_type, NULL);
00158   item->priv->tokens = hud_string_list_ref (tokens);
00159   item->priv->desktop_file = g_strdup (desktop_file);
00160   item->priv->app_icon = g_strdup (app_icon);
00161   item->priv->enabled = enabled;
00162   item->priv->id = hud_item_next_id++;
00163   item->priv->token_list = hud_token_list_new_from_string_list (tokens);
00164 
00165   g_hash_table_insert (hud_item_table, &item->priv->id, item);
00166 
00167   if (desktop_file)
00168     hud_item_setup_usage (item);
00169 
00170   return item;
00171 }
00172 
00187 HudItem *
00188 hud_item_new (HudStringList *tokens,
00189               const gchar   *desktop_file,
00190               const gchar   *app_icon,
00191               gboolean       enabled)
00192 {
00193   return hud_item_construct (HUD_TYPE_ITEM, tokens, desktop_file, app_icon, enabled);
00194 }
00195 
00207 void
00208 hud_item_activate (HudItem  *item,
00209                    GVariant *platform_data)
00210 {
00211   g_return_if_fail (HUD_IS_ITEM (item));
00212 
00213   HUD_ITEM_GET_CLASS (item)
00214     ->activate (item, platform_data);
00215 
00216   if (item->priv->usage_tag)
00217     {
00218       usage_tracker_mark_usage (usage_tracker_get_instance (), item->priv->desktop_file, item->priv->usage_tag);
00219       item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
00220                                                    item->priv->desktop_file, item->priv->usage_tag);
00221     }
00222 }
00223 
00236 HudStringList *
00237 hud_item_get_tokens (HudItem *item)
00238 {
00239   g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
00240 
00241   return item->priv->tokens;
00242 }
00243 
00252 const gchar *
00253 hud_item_get_item_icon (HudItem *item)
00254 {
00255   return "";
00256 }
00257 
00266 const gchar *
00267 hud_item_get_app_icon (HudItem *item)
00268 {
00269   return item->priv->app_icon ? item->priv->app_icon : "";
00270 }
00271 
00283 guint
00284 hud_item_get_usage (HudItem *item)
00285 {
00286   return item->priv->usage;
00287 }
00288 
00299 gboolean
00300 hud_item_get_enabled (HudItem *item)
00301 {
00302   return item->priv->enabled;
00303 }
00304 
00316 guint64
00317 hud_item_get_id (HudItem *item)
00318 {
00319   return item->priv->id;
00320 }
00321 
00333 HudItem *
00334 hud_item_lookup (guint64 id)
00335 {
00336   return g_hash_table_lookup (hud_item_table, &id);
00337 }
00338 
00339 HudTokenList *
00340 hud_item_get_token_list (HudItem *item)
00341 {
00342   return item->priv->token_list;
00343 }