Back to index

unity  6.0.0
CheckOptionFilter.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  */
00019 
00020 #include "CheckOptionFilter.h"
00021 
00022 #include <NuxCore/Logger.h>
00023 
00024 namespace unity
00025 {
00026 namespace dash
00027 {
00028 
00029 namespace
00030 {
00031 nux::logging::Logger logger("unity.dash.checkoptionfilter");
00032 }
00033 
00034 CheckOptionFilter::CheckOptionFilter(DeeModel* model, DeeModelIter* iter)
00035   : Filter(model, iter)
00036 {
00037   options.SetGetterFunction(sigc::mem_fun(this, &CheckOptionFilter::get_options));
00038   Refresh();
00039 }
00040 
00041 void CheckOptionFilter::Clear()
00042 {
00043   for(auto option: options_)
00044     option->active = false;
00045 }
00046 
00047 void CheckOptionFilter::Update(Filter::Hints& hints)
00048 {
00049   GVariant* options_variant = hints["options"];
00050   GVariantIter* options_iter;
00051 
00052   g_variant_get(options_variant, "a(sssb)", &options_iter);
00053 
00054   char *id = NULL;
00055   char *name = NULL;
00056   char *icon_hint = NULL;
00057   gboolean active = false;
00058 
00059   for (auto option: options_)
00060     option_removed.emit(option);
00061 
00062   options_.clear();
00063 
00064   while (g_variant_iter_loop(options_iter, "(sssb)", &id, &name, &icon_hint, &active))
00065   {
00066     FilterOption::Ptr option(new FilterOption(id, name, icon_hint, active));
00067 
00068     std::string data(id);
00069     option->active.changed.connect(sigc::bind(sigc::mem_fun(this, &CheckOptionFilter::OptionChanged), data));
00070     options_.push_back(option);
00071     option_added.emit(option);
00072   }
00073 
00074   g_variant_iter_free(options_iter);
00075 }
00076 
00077 void CheckOptionFilter::OptionChanged(bool is_active, std::string const& id)
00078 {
00079   UpdateState();
00080 }
00081 
00082 CheckOptionFilter::CheckOptions const& CheckOptionFilter::get_options() const
00083 {
00084   return options_;
00085 }
00086 
00087 void CheckOptionFilter::UpdateState()
00088 {
00089   if (!IsValid())
00090     return;
00091   gboolean raw_filtering = FALSE;
00092 
00093   GVariantBuilder options;
00094   g_variant_builder_init(&options, G_VARIANT_TYPE("a(sssb)"));
00095 
00096   for(auto option: options_)
00097   {
00098     std::string id = option->id;
00099     std::string name = option->name;
00100     std::string icon_hint = option->icon_hint;
00101     bool active = option->active;
00102 
00103     raw_filtering = raw_filtering ? TRUE : active;
00104 
00105     g_variant_builder_add(&options, "(sssb)",
00106                           id.c_str(), name.c_str(),
00107                           icon_hint.c_str(), active ? TRUE : FALSE);
00108   }
00109   
00110   GVariantBuilder hints;
00111   g_variant_builder_init(&hints, G_VARIANT_TYPE("a{sv}"));
00112   g_variant_builder_add(&hints, "{sv}", "options", g_variant_builder_end(&options));
00113 
00114   IgnoreChanges(true);
00115   dee_model_set_value(model_,iter_,
00116                       FilterColumn::RENDERER_STATE,
00117                       g_variant_builder_end(&hints));
00118   dee_model_set_value(model_, iter_,
00119                       FilterColumn::FILTERING,
00120                       g_variant_new("b", raw_filtering));
00121   IgnoreChanges(false);
00122 
00123   filtering.EmitChanged(filtering);
00124 }
00125 
00126 
00127 }
00128 }