Back to index

unity  6.0.0
Indicator.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010-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  *              Marco Trevisan (TreviƱo) <mail@3v1n0.net>
00019  */
00020 
00021 #include "Indicator.h"
00022 
00023 #include <iostream>
00024 #include <algorithm>
00025 
00026 
00027 namespace unity
00028 {
00029 namespace indicator
00030 {
00031 
00032 Indicator::Indicator(std::string const& name)
00033   : name_(name)
00034 {
00035 }
00036 
00037 Indicator::~Indicator()
00038 {
00039   for (auto entry : entries_)
00040     on_entry_removed.emit(entry->id());
00041 
00042   entries_.clear();
00043 
00044   for (auto it : entries_connections_)
00045   {
00046     for (auto conn : it.second)
00047       conn.disconnect();
00048 
00049     it.second.clear();
00050   }
00051 }
00052 
00053 std::string const& Indicator::name() const
00054 {
00055   return name_;
00056 }
00057 
00058 Indicator::Entries Indicator::GetEntries() const
00059 {
00060   return entries_;
00061 }
00062 
00063 void Indicator::Sync(Indicator::Entries const& new_entries)
00064 {
00065   Entries to_rm;
00066 
00067   if (entries_.size() == 0)
00068   {
00069     to_rm = entries_;
00070   }
00071   else
00072   {
00073     for (auto entry : entries_)
00074     {
00075       if (std::find(new_entries.begin(), new_entries.end(), entry) == new_entries.end())
00076       {
00077         to_rm.push_back(entry);
00078       }
00079     }
00080   }
00081 
00082   for (auto entry : to_rm) {
00083     for (auto conn : entries_connections_[entry])
00084       conn.disconnect();
00085 
00086     entries_connections_[entry].clear();
00087 
00088     on_entry_removed.emit(entry->id());
00089     entries_.remove(entry);
00090   }
00091 
00092   for (auto new_entry : new_entries)
00093   {
00094     if (GetEntry(new_entry->id()))
00095       continue;
00096 
00097     // Just add the new entry, and connect it up.
00098     sigc::connection conn;
00099     std::vector<sigc::connection>& new_entry_connections = entries_connections_[new_entry];
00100 
00101     conn = new_entry->on_show_menu.connect(sigc::mem_fun(this, &Indicator::OnEntryShowMenu));
00102     new_entry_connections.push_back(conn);
00103 
00104     conn = new_entry->on_secondary_activate.connect(sigc::mem_fun(this, &Indicator::OnEntrySecondaryActivate));
00105     new_entry_connections.push_back(conn);
00106 
00107     conn = new_entry->on_scroll.connect(sigc::mem_fun(this, &Indicator::OnEntryScroll));
00108     entries_.push_back(new_entry);
00109     new_entry_connections.push_back(conn);
00110 
00111     on_entry_added.emit(new_entry);
00112   }
00113 }
00114 
00115 Entry::Ptr Indicator::GetEntry(std::string const& entry_id) const
00116 {
00117   for (auto entry : entries_)
00118     if (entry->id() == entry_id)
00119       return entry;
00120 
00121   return Entry::Ptr();
00122 }
00123 
00124 void Indicator::OnEntryShowMenu(std::string const& entry_id, unsigned int xid,
00125                                 int x, int y, unsigned int button, unsigned int timestamp)
00126 {
00127   on_show_menu.emit(entry_id, xid, x, y, button, timestamp);
00128 }
00129 
00130 void Indicator::OnEntrySecondaryActivate(std::string const& entry_id,
00131                                          unsigned int timestamp)
00132 {
00133   on_secondary_activate.emit(entry_id, timestamp);
00134 }
00135 
00136 void Indicator::OnEntryScroll(std::string const& entry_id, int delta)
00137 {
00138   on_scroll.emit(entry_id, delta);
00139 }
00140 
00141 std::ostream& operator<<(std::ostream& out, Indicator const& i)
00142 {
00143   out << "<Indicator " << i.name() << std::endl;
00144   for (auto entry : i.entries_)
00145   {
00146     out << "\t" << entry << std::endl;
00147   }
00148   out << "\t>" << std::endl;
00149   return out;
00150 }
00151 
00152 
00153 } // namespace indicator
00154 } // namespace unity