Back to index

unity  6.0.0
RadioOptionFilter.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 "RadioOptionFilter.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.radiooptionfilter");
00032 }
00033 
00034 RadioOptionFilter::RadioOptionFilter(DeeModel* model, DeeModelIter* iter)
00035   : Filter(model, iter)
00036   , ignore_changes_(false)
00037 {
00038   options.SetGetterFunction(sigc::mem_fun(this, &RadioOptionFilter::get_options));
00039   Refresh();
00040 }
00041 
00042 void RadioOptionFilter::Clear()
00043 {
00044   for(auto option: options_)
00045     option->active = false;
00046 }
00047 
00048 void RadioOptionFilter::Update(Filter::Hints& hints)
00049 {
00050   GVariant* options_variant = hints["options"];
00051   GVariantIter* options_iter;
00052 
00053   g_variant_get(options_variant, "a(sssb)", &options_iter);
00054 
00055   char *id = NULL;
00056   char *name = NULL;
00057   char *icon_hint = NULL;
00058   gboolean active = false;
00059 
00060   for (auto option: options_)
00061     option_removed.emit(option);
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, &RadioOptionFilter::OptionChanged), data));
00070     options_.push_back(option);
00071     option_added.emit(option);
00072   }
00073 
00074   g_variant_iter_free(options_iter);
00075 
00076   options.EmitChanged(options_);
00077 }
00078 
00079 void RadioOptionFilter::OptionChanged(bool is_active, std::string const& id)
00080 {
00081   if (ignore_changes_)
00082     return;
00083 
00084   ignore_changes_ = true;
00085   if (is_active)
00086   {
00087     for(auto option: options_)
00088     {
00089       if (option->id != id)
00090         option->active = false;
00091     }
00092   }
00093   ignore_changes_ = false;
00094 
00095   UpdateState();
00096 }
00097 
00098 RadioOptionFilter::RadioOptions const& RadioOptionFilter::get_options() const
00099 {
00100   return options_;
00101 }
00102 
00103 void RadioOptionFilter::UpdateState()
00104 {
00105   if (!IsValid())
00106     return;
00107   gboolean raw_filtering = FALSE;
00108 
00109   GVariantBuilder options;
00110   g_variant_builder_init(&options, G_VARIANT_TYPE("a(sssb)"));
00111 
00112   for(auto option: options_)
00113   {
00114     std::string id = option->id;
00115     std::string name = option->name;
00116     std::string icon_hint = option->icon_hint;
00117     bool active = option->active;
00118 
00119     raw_filtering = raw_filtering ? TRUE : active;
00120 
00121     g_variant_builder_add(&options, "(sssb)",
00122                           id.c_str(), name.c_str(),
00123                           icon_hint.c_str(), active ? TRUE : FALSE);
00124   }
00125   
00126   GVariantBuilder hints;
00127   g_variant_builder_init(&hints, G_VARIANT_TYPE("a{sv}"));
00128   g_variant_builder_add(&hints, "{sv}", "options", g_variant_builder_end(&options));
00129 
00130   IgnoreChanges(true);
00131   dee_model_set_value(model_,iter_,
00132                       FilterColumn::RENDERER_STATE,
00133                       g_variant_builder_end(&hints));
00134   dee_model_set_value(model_, iter_,
00135                       FilterColumn::FILTERING,
00136                       g_variant_new("b", raw_filtering));
00137   IgnoreChanges(false);
00138 
00139   filtering.EmitChanged(filtering);
00140 }
00141 
00142 }
00143 }