Back to index

unity  6.0.0
Filter.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 "Filter.h"
00021 
00022 #include <NuxCore/Logger.h>
00023 
00024 #include "CheckOptionFilter.h"
00025 #include "MultiRangeFilter.h"
00026 #include "RadioOptionFilter.h"
00027 #include "RatingsFilter.h"
00028 
00029 namespace unity
00030 {
00031 namespace dash
00032 {
00033 
00034 namespace
00035 {
00036 nux::logging::Logger logger("unity.dash.filter");
00037 }
00038 
00039 using unity::glib::Signal;
00040 
00041 Filter::Filter(DeeModel* model, DeeModelIter* iter)
00042   : model_(model)
00043   , iter_(iter)
00044   , ignore_changes_(false)
00045 {
00046   typedef Signal<void, DeeModel*, DeeModelIter*> RowSignalType;
00047 
00048   // If the model is destroyed (say if the lens restarts) then we should handle
00049   // that gracefully
00050   g_object_weak_ref(reinterpret_cast<GObject*>(model_),
00051                     (GWeakNotify)Filter::OnModelDestroyed, this);
00052 
00053   // Add some filters to handle updates and removed
00054   signal_manager_.Add(new RowSignalType(model_,
00055                                         "row-changed",
00056                                         sigc::mem_fun(this, &Filter::OnRowChanged)));
00057   signal_manager_.Add(new RowSignalType(model_,
00058                                         "row-removed",
00059                                         sigc::mem_fun(this, &Filter::OnRowRemoved)));
00060 
00061   SetupGetters();
00062 }
00063 
00064 Filter::~Filter()
00065 {
00066   if (model_)
00067     g_object_weak_unref(reinterpret_cast<GObject*>(model_),
00068                         (GWeakNotify)Filter::OnModelDestroyed, this);
00069 }
00070 
00071 void Filter::SetupGetters()
00072 {
00073   id.SetGetterFunction(sigc::mem_fun(this, &Filter::get_id));
00074   name.SetGetterFunction(sigc::mem_fun(this, &Filter::get_name));
00075   icon_hint.SetGetterFunction(sigc::mem_fun(this, &Filter::get_icon_hint));
00076   renderer_name.SetGetterFunction(sigc::mem_fun(this, &Filter::get_renderer_name));
00077   visible.SetGetterFunction(sigc::mem_fun(this, &Filter::get_visible));
00078   collapsed.SetGetterFunction(sigc::mem_fun(this, &Filter::get_collapsed));
00079   filtering.SetGetterFunction(sigc::mem_fun(this, &Filter::get_filtering));
00080 }
00081 
00082 Filter::Ptr Filter::FilterFromIter(DeeModel* model, DeeModelIter* iter)
00083 {
00084   std::string renderer = dee_model_get_string(model, iter, 3);
00085 
00086   if (renderer == "filter-ratings")
00087     return std::make_shared<RatingsFilter>(model, iter);
00088   else if (renderer == "filter-radiooption")
00089     return std::make_shared<RadioOptionFilter>(model, iter);
00090   else if (renderer == "filter-checkoption")
00091     return std::make_shared<CheckOptionFilter>(model, iter);
00092   else if (renderer == "filter-checkoption-compact")
00093     return std::make_shared<CheckOptionFilter>(model, iter);
00094   else if (renderer == "filter-multirange")
00095     return std::make_shared<MultiRangeFilter>(model, iter);
00096   else
00097     return std::make_shared<RatingsFilter>(model, iter);
00098 }
00099 
00100 bool Filter::IsValid() const
00101 {
00102   return model_ && iter_;
00103 }
00104 
00105 void Filter::Refresh()
00106 {
00107   if (model_ && iter_)
00108     OnRowChanged(model_, iter_);
00109 }
00110 
00111 void Filter::IgnoreChanges(bool ignore)
00112 {
00113   ignore_changes_ = ignore;
00114 }
00115 
00116 void Filter::OnModelDestroyed(Filter* self, DeeModel* old_location)
00117 {
00118   self->OnRowRemoved(self->model_, self->iter_);
00119 }
00120 
00121 void Filter::OnRowChanged(DeeModel* model, DeeModelIter* iter)
00122 {
00123   if (iter_ != iter || ignore_changes_)
00124     return;
00125 
00126   // Ask our sub-classes to update their state
00127   Hints hints;
00128   HintsToMap(hints);
00129   Update(hints);
00130 
00131   visible.EmitChanged(get_visible());
00132   filtering.EmitChanged(get_filtering());
00133 }
00134 
00135 void Filter::OnRowRemoved(DeeModel* model, DeeModelIter* iter)
00136 {
00137   if (iter_ != iter)
00138     return;
00139 
00140   model_ = 0;
00141   iter_ = 0;
00142   removed.emit();
00143 }
00144 
00145 void Filter::HintsToMap(Hints& map)
00146 {
00147   GVariant* row_value = dee_model_get_value(model_, iter_, FilterColumn::RENDERER_STATE);
00148 
00149   GVariantIter iter;
00150   g_variant_iter_init(&iter, row_value);
00151 
00152   char* key = NULL;
00153   GVariant* value = NULL;
00154   while (g_variant_iter_loop(&iter, "{sv}", &key, &value))
00155   {
00156     map[key] = value;
00157   }
00158   g_variant_unref(row_value);
00159 }
00160 
00161 std::string Filter::get_id() const
00162 {
00163   if (IsValid())
00164     return dee_model_get_string(model_, iter_, FilterColumn::ID);
00165   return "";
00166 }
00167 
00168 std::string Filter::get_name() const
00169 {
00170   if (IsValid())
00171     return dee_model_get_string(model_, iter_, FilterColumn::NAME);
00172   return "";
00173 }
00174 
00175 std::string Filter::get_icon_hint() const
00176 {
00177   if (IsValid())
00178     return dee_model_get_string(model_, iter_, FilterColumn::ICON_HINT);
00179   return "";
00180 }
00181 
00182 std::string Filter::get_renderer_name() const
00183 {
00184   if (IsValid())
00185     return dee_model_get_string(model_, iter_, FilterColumn::RENDERER_NAME);
00186   return "";
00187 }
00188 
00189 bool Filter::get_visible() const
00190 {
00191   if (IsValid())
00192     return dee_model_get_bool(model_, iter_, FilterColumn::VISIBLE);
00193   return false;
00194 }
00195 
00196 bool Filter::get_collapsed() const
00197 {
00198   if (IsValid())
00199     return dee_model_get_bool(model_, iter_, FilterColumn::COLLAPSED);
00200   return true;
00201 }
00202 
00203 bool Filter::get_filtering() const
00204 {
00205   if (IsValid())
00206     return dee_model_get_bool(model_, iter_, FilterColumn::FILTERING);
00207   return false;
00208 }
00209 
00210 }
00211 }