Back to index

unity  6.0.0
Indicators.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  *              Tim Penhey <tim.penhey@canonical.com>
00019  */
00020 
00021 #include "Indicators.h"
00022 #include "AppmenuIndicator.h"
00023 
00024 namespace unity
00025 {
00026 namespace indicator
00027 {
00028 
00029 class Indicators::Impl
00030 {
00031 public:
00032   typedef std::map<std::string, Indicator::Ptr> IndicatorMap;
00033   typedef std::map<std::string, Entry::Ptr> EntryMap;
00034 
00035   Impl(Indicators* owner)
00036     : owner_(owner)
00037   {}
00038 
00039   void ActivateEntry(std::string const& entry_id, nux::Rect const& geometry);
00040   void SetEntryShowNow(std::string const& entry_id, bool show_now);
00041 
00042   IndicatorsList GetIndicators() const;
00043 
00044   Indicator::Ptr GetIndicator(std::string const& name);
00045   Indicator::Ptr AddIndicator(std::string const& name);
00046   void RemoveIndicator(std::string const& name);
00047 
00048   void OnEntryAdded(Entry::Ptr const& entry);
00049 
00050   Entry::Ptr GetEntry(std::string const& entry_id);
00051 
00052 private:
00053   Indicators* owner_;
00054   IndicatorMap indicators_;
00055   Entry::Ptr active_entry_;
00056 };
00057 
00058 
00059 Indicators::Indicators()
00060   : pimpl(new Impl(this))
00061 {}
00062 
00063 Indicators::~Indicators()
00064 {}
00065 
00066 void Indicators::ActivateEntry(std::string const& entry_id, nux::Rect const& geometry)
00067 {
00068   pimpl->ActivateEntry(entry_id, geometry);
00069 }
00070 
00071 void Indicators::SetEntryShowNow(std::string const& entry_id, bool show_now)
00072 {
00073   pimpl->SetEntryShowNow(entry_id, show_now);
00074 }
00075 
00076 Indicators::IndicatorsList Indicators::GetIndicators() const
00077 {
00078   return pimpl->GetIndicators();
00079 }
00080 
00081 Indicator::Ptr Indicators::AddIndicator(std::string const& name)
00082 {
00083   return pimpl->AddIndicator(name);
00084 }
00085 
00086 Indicator::Ptr Indicators::GetIndicator(std::string const& name)
00087 {
00088   return pimpl->GetIndicator(name);
00089 }
00090 
00091 void Indicators::RemoveIndicator(std::string const& name)
00092 {
00093   return pimpl->RemoveIndicator(name);
00094 }
00095 
00096 void Indicators::Impl::ActivateEntry(std::string const& entry_id, nux::Rect const& geometry)
00097 {
00098   if (active_entry_)
00099   {
00100     active_entry_->set_geometry(nux::Rect());
00101     active_entry_->set_active(false);
00102   }
00103 
00104   active_entry_ = GetEntry(entry_id);
00105 
00106   if (active_entry_)
00107   {
00108     active_entry_->set_geometry(geometry);
00109     active_entry_->set_active(true);
00110     owner_->on_entry_activated.emit(entry_id, geometry);
00111   }
00112   else
00113   {
00114     owner_->on_entry_activated.emit(std::string(), nux::Rect());
00115   }
00116 }
00117 
00118 void Indicators::Impl::SetEntryShowNow(std::string const& entry_id,
00119                                        bool show_now)
00120 {
00121   Entry::Ptr entry = GetEntry(entry_id);
00122   if (entry)
00123   {
00124     entry->set_show_now(show_now);
00125   }
00126 }
00127 
00128 Indicators::IndicatorsList Indicators::Impl::GetIndicators() const
00129 {
00130   Indicators::IndicatorsList list;
00131 
00132   for (auto it = indicators_.begin(); it != indicators_.end(); it++)
00133   {
00134     list.push_back(it->second);
00135   }
00136 
00137   return list;
00138 }
00139 
00140 Indicator::Ptr Indicators::Impl::AddIndicator(std::string const& name)
00141 {
00142   Indicator::Ptr indicator(GetIndicator(name));
00143 
00144   if (indicator)
00145     return indicator;
00146 
00147   if (name == "libappmenu.so")
00148   {
00149     auto appmenu = std::make_shared<AppmenuIndicator>(name);
00150     appmenu->on_show_appmenu.connect(sigc::mem_fun(owner_, &Indicators::OnShowAppMenu));
00151     indicator = appmenu;
00152   }
00153   else
00154   {
00155     indicator = std::make_shared<Indicator>(name);
00156   }
00157 
00158   // The owner Indicators class is interested in the other events.
00159   indicator->on_show_menu.connect(sigc::mem_fun(owner_, &Indicators::OnEntryShowMenu));
00160   indicator->on_secondary_activate.connect(sigc::mem_fun(owner_, &Indicators::OnEntrySecondaryActivate));
00161   indicator->on_scroll.connect(sigc::mem_fun(owner_, &Indicators::OnEntryScroll));
00162 
00163   indicators_[name] = indicator;
00164   owner_->on_object_added.emit(indicator);
00165 
00166   return indicator;
00167 }
00168 
00169 Indicator::Ptr Indicators::Impl::GetIndicator(std::string const& name)
00170 {
00171   IndicatorMap::iterator i = indicators_.find(name);
00172   if (i != indicators_.end())
00173     return i->second;
00174 
00175   return Indicator::Ptr();
00176 }
00177 
00178 void Indicators::Impl::RemoveIndicator(std::string const& name)
00179 {
00180   auto indicator = GetIndicator(name);
00181 
00182   if (indicator)
00183   {
00184     owner_->on_object_removed.emit(indicator);
00185     indicators_.erase(name);
00186   }
00187 }
00188 
00189 Entry::Ptr Indicators::Impl::GetEntry(std::string const& entry_id)
00190 {
00191   for (auto it = indicators_.begin(); it != indicators_.end(); ++it)
00192   {
00193     Entry::Ptr entry = it->second->GetEntry(entry_id);
00194 
00195     if (entry)
00196       return entry;
00197   }
00198 
00199   return Entry::Ptr();
00200 }
00201 
00202 
00203 } // namespace indicator
00204 } // namespace unity