~sethj/ubuntu/wily/unity/fix-for-1445595

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright (C) 2010 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
*              Marco Trevisan (TreviƱo) <mail@3v1n0.net>
*/

#include "IndicatorEntry.h"

#include <iostream>
#include <algorithm>

namespace unity
{
namespace indicator
{

Entry::Entry(std::string const& id,
             std::string const& name_hint,
             uint32_t parent_window,
             std::string const& label,
             bool label_sensitive,
             bool label_visible,
             int  image_type,
             std::string const& image_data,
             bool image_sensitive,
             bool image_visible,
             int priority)
  : id_(id)
  , name_hint_(name_hint)
  , parent_window_(parent_window)
  , label_(label)
  , label_visible_(label_visible)
  , label_sensitive_(label_sensitive)
  , image_type_(image_type)
  , image_data_(image_data)
  , image_visible_(image_visible)
  , image_sensitive_(image_sensitive)
  , priority_(priority)
  , show_now_(false)
  , active_(false)
{}

Entry::Entry(std::string const& id, std::string const& name_hint, uint32_t parent_window)
  : id_(id)
  , name_hint_(name_hint)
  , parent_window_(parent_window)
  , label_visible_(false)
  , label_sensitive_(false)
  , image_type_(0)
  , image_visible_(false)
  , image_sensitive_(false)
  , priority_(-1)
  , show_now_(false)
  , active_(false)
{}

std::string const& Entry::id() const
{
  return id_;
}

std::string const& Entry::name_hint() const
{
  return name_hint_;
}

uint32_t Entry::parent_window() const
{
  return parent_window_;
}

std::string const& Entry::label() const
{
  return label_;
}

bool Entry::image_visible() const
{
  return image_visible_;
}

bool Entry::image_sensitive() const
{
  return image_sensitive_;
}

bool Entry::label_visible() const
{
  return label_visible_;
}

bool Entry::label_sensitive() const
{
  return label_sensitive_;
}

int Entry::image_type() const
{
  return image_type_;
}

std::string const& Entry::image_data() const
{
  return image_data_;
}

int Entry::priority() const
{
  return priority_;
}

bool Entry::visible() const
{
  return ((label_visible_ && !label_.empty()) ||
          (image_type_ != 0 && image_visible_ && !image_data_.empty()));
}

void Entry::set_active(bool active)
{
  if (active_ == active)
    return;

  active_ = active;
  active_changed.emit(active);
  updated.emit();

  for (auto const& parent : parents_)
    parent->set_active(active_);
}

void Entry::set_geometry(nux::Rect const& geometry)
{
  if (geometry_ == geometry)
    return;

  geometry_ = geometry;
  geometry_changed.emit(geometry);
  updated.emit();

  for (auto const& parent : parents_)
    parent->set_geometry(geometry_);
}

void Entry::set_label(std::string const& label, bool sensitive, bool visible)
{
  if (label_ == label && sensitive == label_sensitive_ && visible == label_visible_)
    return;

  label_ = label;
  label_sensitive_ = sensitive;
  label_visible_ = visible;
  updated.emit();
}

void Entry::set_image(int type, std::string const& data, bool sensitive, bool visible)
{
  if (image_type_ == type && image_data_ == data &&
      image_sensitive_ == sensitive && image_visible_ == visible)
  {
    return;
  }

  image_type_ = type;
  image_data_ = data;
  image_sensitive_ = sensitive;
  image_visible_ = visible;
  updated.emit();
}

void Entry::set_priority(int priority)
{
  if (priority_ == priority)
    return;

  priority_ = priority;
  updated.emit();
}

bool Entry::active() const
{
  return active_;
}

nux::Rect const& Entry::geometry() const
{
  return geometry_;
}

Entry& Entry::operator=(Entry const& rhs)
{
  if (&rhs == this)
    return *this;

  id_ = rhs.id_;
  name_hint_ = rhs.name_hint_;
  parent_window_ = rhs.parent_window_;
  label_ = rhs.label_;
  label_sensitive_ = rhs.label_sensitive_;
  label_visible_ = rhs.label_visible_;
  image_type_ = rhs.image_type_;
  image_data_ = rhs.image_data_;
  image_sensitive_ = rhs.image_sensitive_;
  image_visible_ = rhs.image_visible_;
  priority_ = rhs.priority_;
  parents_ = rhs.parents_;

  updated.emit();
  return *this;
}

bool Entry::show_now() const
{
  return show_now_;
}

void Entry::set_show_now(bool show_now)
{
  if (show_now_ == show_now)
    return;

  show_now_ = show_now;
  show_now_changed.emit(show_now);
  updated.emit();

  for (auto const& parent : parents_)
    parent->set_show_now(show_now_);
}

void Entry::add_parent(Entry::Ptr const& parent)
{
  if (!parent || std::find(parents_.begin(), parents_.end(), parent) != parents_.end())
    return;

  bool was_empty = parents_.empty();
  parents_.push_back(parent);
  parent->set_geometry(geometry_);
  parent->set_show_now(was_empty ? show_now_ : (parent->show_now() || show_now_));
  parent->set_active(was_empty ? active_ : (parent->active() || active_));
}

void Entry::rm_parent(Entry::Ptr const& parent)
{
  auto it = std::find(parents_.begin(), parents_.end(), parent);

  if (it != parents_.end())
    parents_.erase(it);
}

std::vector<Entry::Ptr> const& Entry::parents() const
{
  return parents_;
}

void Entry::ShowMenu(int x, int y, unsigned button)
{
  ShowMenu(parent_window_, x, y, button);
}

void Entry::ShowMenu(unsigned int xid, int x, int y, unsigned button)
{
  on_show_menu.emit(id_, xid, x, y, button);
}

void Entry::SecondaryActivate()
{
  on_secondary_activate.emit(id_);
}

void Entry::Scroll(int delta)
{
  on_scroll.emit(id_, delta);
}

std::ostream& operator<<(std::ostream& out, Entry const& e)
{
  out << "<indicator::Entry " << e.id() << " hint: '" << e.name_hint() << "'"
      << " parent window: " << e.parent_window()
      << std::boolalpha
      << " \"" << e.label() << "\" ("
      << e.label_sensitive() << ", " << e.label_visible() << ") image ("
      << e.image_sensitive() << ", " << e.image_visible() << ") "
      << (e.active() ? "active" : "not-active") << " "
      << (e.show_now() ? "show" : "dont-show") << " >";
  return out;
}


} // namespace indicator
} // namespace unity