Back to index

indicator-appmenu  12.10.0
huddebugsource.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 "huddebugsource.h"
00020 
00021 #include "hudsource.h"
00022 #include "hudresult.h"
00023 
00043 struct _HudDebugSource
00044 {
00045   GObject parent_instance;
00046 
00047   HudItem *item;
00048   gint use_count;
00049   gint timeout;
00050 };
00051 
00052 typedef GObjectClass HudDebugSourceClass;
00053 
00054 static void hud_debug_source_iface_init (HudSourceInterface *iface);
00055 G_DEFINE_TYPE_WITH_CODE (HudDebugSource, hud_debug_source, G_TYPE_OBJECT,
00056                          G_IMPLEMENT_INTERFACE (HUD_TYPE_SOURCE, hud_debug_source_iface_init))
00057 
00058 static gboolean
00059 hud_debug_source_timeout (gpointer user_data)
00060 {
00061   HudDebugSource *source = user_data;
00062   HudStringList *tokens;
00063   GDateTime *now;
00064   gchar *time;
00065 
00066   g_clear_object (&source->item);
00067 
00068   now = g_date_time_new_now_local ();
00069   time = g_date_time_format (now, "hud-debug time: %c");
00070   tokens = hud_string_list_cons (time, NULL);
00071   g_date_time_unref (now);
00072   g_free (time);
00073 
00074   source->item = hud_item_new (tokens, NULL, NULL, TRUE);
00075   hud_string_list_unref (tokens);
00076 
00077   hud_source_changed (HUD_SOURCE (source));
00078 
00079   return TRUE;
00080 }
00081 
00082 static void
00083 hud_debug_source_use (HudSource *hud_source)
00084 {
00085   HudDebugSource *source = HUD_DEBUG_SOURCE (hud_source);
00086 
00087   if (source->use_count == 0)
00088     source->timeout = g_timeout_add (1000, hud_debug_source_timeout, source);
00089 
00090   source->use_count++;
00091 }
00092 
00093 static void
00094 hud_debug_source_unuse (HudSource *hud_source)
00095 {
00096   HudDebugSource *source = HUD_DEBUG_SOURCE (hud_source);
00097 
00098   source->use_count--;
00099 
00100   if (source->use_count == 0)
00101     {
00102       g_source_remove (source->timeout);
00103       source->timeout = 0;
00104     }
00105 }
00106 
00107 static void
00108 hud_debug_source_search (HudSource    *hud_source,
00109                          GPtrArray    *results_array,
00110                          HudTokenList *search_string)
00111 {
00112   HudDebugSource *source = HUD_DEBUG_SOURCE (hud_source);
00113 
00114   if (source->item)
00115     {
00116       HudResult *result;
00117 
00118       result = hud_result_get_if_matched (source->item, search_string, 0);
00119       if (result != NULL)
00120         g_ptr_array_add (results_array, result);
00121     }
00122 }
00123 
00124 static void
00125 hud_debug_source_finalize (GObject *object)
00126 {
00127   HudDebugSource *source = HUD_DEBUG_SOURCE (object);
00128 
00129   g_clear_object (&source->item);
00130 
00131   if (source->timeout)
00132     g_source_remove (source->timeout);
00133 
00134   G_OBJECT_CLASS (hud_debug_source_parent_class)
00135     ->finalize (object);
00136 }
00137 
00138 static void
00139 hud_debug_source_init (HudDebugSource *source)
00140 {
00141 }
00142 
00143 static void
00144 hud_debug_source_iface_init (HudSourceInterface *iface)
00145 {
00146   iface->use = hud_debug_source_use;
00147   iface->unuse = hud_debug_source_unuse;
00148   iface->search = hud_debug_source_search;
00149 }
00150 
00151 static void
00152 hud_debug_source_class_init (HudDebugSourceClass *class)
00153 {
00154   class->finalize = hud_debug_source_finalize;
00155 }
00156 
00164 HudDebugSource *
00165 hud_debug_source_new (void)
00166 {
00167   return g_object_new (HUD_TYPE_DEBUG_SOURCE, NULL);
00168 }