Back to index

unity  6.0.0
test_service_lens.c
Go to the documentation of this file.
00001 #include "test_service_lens.h"
00002 
00003 #include <unity.h>
00004 
00005 G_DEFINE_TYPE(ServiceLens, service_lens, G_TYPE_OBJECT);
00006 
00007 static void add_categories(ServiceLens* self);
00008 static void add_filters(ServiceLens *self);
00009 static void on_search_changed(UnityScope* scope, UnityLensSearch *lens_search, UnitySearchType search_type, GCancellable *canc, ServiceLens* self);
00010 static UnityActivationResponse* on_activate_uri(UnityScope* scope, const char* uri, ServiceLens* self);
00011 static UnityPreview* on_preview_uri(UnityScope* scope, const char* uri, ServiceLens *self);
00012 
00013 struct _ServiceLensPrivate
00014 {
00015   UnityLens* lens;
00016   UnityScope* scope;
00017 };
00018 
00019 static void
00020 service_lens_dispose(GObject* object)
00021 {
00022   ServiceLens* self = SERVICE_LENS(object);
00023 
00024   g_object_unref(self->priv->lens);
00025   g_object_unref(self->priv->scope);
00026 }
00027 
00028 static void
00029 service_lens_class_init(ServiceLensClass* klass)
00030 {
00031   G_OBJECT_CLASS(klass)->dispose = service_lens_dispose;
00032 
00033        g_type_class_add_private (klass, sizeof (ServiceLensPrivate));
00034 }
00035 
00036 static void
00037 service_lens_init(ServiceLens* self)
00038 {
00039   ServiceLensPrivate* priv;
00040   GError* error = NULL;
00041 
00042   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self, SERVICE_TYPE_LENS, ServiceLensPrivate);
00043 
00044   /* Setup Lens */
00045   priv->lens = unity_lens_new("/com/canonical/unity/testlens", "testlens");
00046   g_object_set(priv->lens,
00047                "search-hint", "Search Test Lens",
00048                "visible", TRUE,
00049                "search-in-global", TRUE,
00050                NULL);
00051   add_categories(self);
00052   add_filters(self);
00053 
00054   /* Scope */
00055   priv->scope = unity_scope_new("/com/canonical/unity/testscope");
00056   unity_scope_set_search_in_global(priv->scope, TRUE);
00057 
00058   g_signal_connect(priv->scope, "search-changed",
00059                    G_CALLBACK(on_search_changed), self);
00060   g_signal_connect(priv->scope, "activate-uri",
00061                    G_CALLBACK(on_activate_uri), self);
00062   g_signal_connect(priv->scope, "preview-uri",
00063                    G_CALLBACK(on_preview_uri), self);
00064 
00065   /* Get ready to export and export */
00066   unity_lens_add_local_scope(priv->lens, priv->scope);
00067   unity_lens_export(priv->lens, &error);
00068   if (error)
00069   {
00070     g_error ("Unable to export Lens: %s", error->message);
00071     g_error_free (error);
00072   }
00073 }
00074 
00075 static void
00076 add_categories(ServiceLens* self)
00077 {
00078   GList *cats = NULL;
00079   GIcon *icon;
00080 
00081   icon = g_themed_icon_new("gtk-apply");
00082   cats = g_list_append (cats, unity_category_new("Category1", icon,
00083                                                  UNITY_CATEGORY_RENDERER_VERTICAL_TILE));
00084   g_object_unref (icon);
00085   
00086   icon = g_themed_icon_new("gtk-cancel");
00087   cats = g_list_append (cats, unity_category_new("Category2", icon,
00088                                                  UNITY_CATEGORY_RENDERER_HORIZONTAL_TILE));
00089   g_object_unref (icon);
00090 
00091   icon = g_themed_icon_new("gtk-close");
00092   cats = g_list_append (cats, unity_category_new("Category3", icon,
00093                                                  UNITY_CATEGORY_RENDERER_FLOW));
00094   g_object_unref (icon);
00095 
00096 
00097   unity_lens_set_categories(self->priv->lens, cats);
00098   g_list_free_full (cats, (GDestroyNotify) g_object_unref);
00099 }
00100 
00101 static void
00102 add_filters(ServiceLens *self)
00103 {
00104   GList       *filters = NULL;
00105   UnityFilter *filter;
00106   GIcon       *icon;
00107 
00108   filter = UNITY_FILTER (unity_radio_option_filter_new("when", "When",
00109                                                        NULL, FALSE));
00110   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00111                                   "today", "Today", NULL);
00112   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00113                                   "yesterday", "Yesterday", NULL);
00114   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00115                                   "lastweek", "Last Week", NULL);
00116   filters = g_list_append (filters, filter);
00117 
00118   filter = UNITY_FILTER (unity_check_option_filter_new("type", "Type",
00119                                                        NULL, FALSE));
00120   icon = g_themed_icon_new ("gtk-apps");
00121   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00122                                   "apps", "Apps", icon);
00123   g_object_unref (icon);
00124   icon = g_themed_icon_new ("gtk-files");
00125   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00126                                   "files", "Files", icon);
00127   g_object_unref (icon);
00128   icon = g_themed_icon_new ("gtk-music");
00129   unity_options_filter_add_option(UNITY_OPTIONS_FILTER (filter),
00130                                   "music", "Music", icon);
00131   g_object_unref (icon);
00132   filters = g_list_append (filters, filter);
00133 
00134   filters = g_list_append (filters, unity_ratings_filter_new("ratings",
00135                                                              "Ratings",
00136                                                              NULL, FALSE));
00137 
00138   filter = UNITY_FILTER (unity_multi_range_filter_new("size", "Size", NULL, TRUE));
00139   unity_options_filter_add_option(UNITY_OPTIONS_FILTER(filter), "1MB", "1MB", NULL);
00140   unity_options_filter_add_option(UNITY_OPTIONS_FILTER(filter), "10MB", "10MB", NULL);
00141   unity_options_filter_add_option(UNITY_OPTIONS_FILTER(filter), "100MB", "100MB", NULL);
00142   unity_options_filter_add_option(UNITY_OPTIONS_FILTER(filter), "1000MB", "1000MB", NULL);
00143   filters = g_list_append (filters, filter);
00144  
00145 
00146   unity_lens_set_filters(self->priv->lens, filters);
00147   g_list_free_full (filters, (GDestroyNotify) g_object_unref);
00148 }
00149 
00150 static void
00151 on_search_changed(UnityScope* scope, UnityLensSearch *search,
00152     UnitySearchType search_type, GCancellable *canc, ServiceLens* self)
00153 {
00154   int i = 0;
00155   // to differentiate global and non-global searches, we'll return more items
00156   // in the case of global search
00157   int num_items = search_type == UNITY_SEARCH_TYPE_GLOBAL ? 10 : 5;
00158 
00159   DeeModel* model = (DeeModel*)unity_lens_search_get_results_model(search);
00160 
00161   for (i = 0; i < num_items; i++)
00162   {
00163     gchar* name = g_strdup_printf("%s%d",
00164                                   unity_lens_search_get_search_string(search),
00165                                   i);
00166     dee_model_append(model,
00167                      "file:///test",
00168                      "gtk-apply",
00169                      i,
00170                      "text/html",
00171                      name,
00172                      "kamstrup likes ponies",
00173                      "file:///test");
00174     g_free(name);
00175   }
00176 
00177   g_signal_emit_by_name (search, "finished");
00178 }
00179 
00180 static UnityActivationResponse*
00181 on_activate_uri(UnityScope* scope, const char* uri, ServiceLens* self)
00182 {
00183   return unity_activation_response_new(UNITY_HANDLED_TYPE_HIDE_DASH, "");
00184 }
00185 
00186 static UnityPreview*
00187 on_preview_uri(UnityScope* scope, const char* uri, ServiceLens *self)
00188 {
00189   return NULL;
00190   // FIXME: update when the new preview types are well defined
00191   /*
00192   return (UnityPreview*)unity_generic_preview_new(
00193       "Animus Vox", "The Glitch Mob - Drink The Sea", NULL);
00194   */
00195 }
00196 
00197 ServiceLens*
00198 service_lens_new()
00199 {
00200   return g_object_new(SERVICE_TYPE_LENS, NULL);
00201 }