~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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011 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: Nick Dedekind <nick.dedeknd@canoncial.com>
 */

#include "Scopes.h"
#include "ConnectionManager.h"
#include <stdexcept>

namespace unity
{
namespace dash
{
DECLARE_LOGGER(logger, "unity.dash.scopes");

class Scopes::Impl
{
public:
  Impl(Scopes* owner, ScopesReader::Ptr scopes_reader);

  void LoadScopes();
  void InsertScope(ScopeData::Ptr const& data, unsigned index);
  void RemoveScope(std::string const& scope);

  ScopeList const& GetScopes() const;
  Scope::Ptr GetScope(std::string const& scope_id, int* index = nullptr) const;
  Scope::Ptr GetScopeAtIndex(std::size_t index) const;
  Scope::Ptr GetScopeForShortcut(std::string const& scope_shortcut) const;

  Scopes* owner_;
  ScopesReader::Ptr scopes_reader_;
  ScopeList scopes_;
  connection::Wrapper scope_changed_connection_;

  std::size_t get_count() const { return scopes_.size(); }

  void UpdateScopes(ScopeDataList const& scopes_list);
};

Scopes::Impl::Impl(Scopes* owner, ScopesReader::Ptr scopes_reader)
: owner_(owner)
, scopes_reader_(scopes_reader)
{
}

void Scopes::Impl::UpdateScopes(ScopeDataList const& scopes_list)
{
  // insert new.
  int index = 0;
  for (ScopeData::Ptr const& scope_data: scopes_list)  
  {
    InsertScope(scope_data, index++);
  }

  // remove old.
  std::vector<std::string> remove_scopes;
  for (Scope::Ptr const& scope: scopes_)
  {
    auto scope_data_position = std::find_if(scopes_list.begin(),
                                            scopes_list.end(),
                                            [scope](ScopeData::Ptr const& data) { return data->id() == scope->id(); });
    if (scope_data_position == scopes_list.end())
    {
      remove_scopes.push_back(scope->id());
    }
  }
  for (std::string const& scope: remove_scopes)
  {
    RemoveScope(scope);
  }
}

void Scopes::Impl::LoadScopes()
{
  if (!scopes_reader_)
    return;

  scope_changed_connection_ = scopes_reader_->scopes_changed.connect(sigc::mem_fun(this, &Impl::UpdateScopes));
  UpdateScopes(scopes_reader_->GetScopesData());
}

void Scopes::Impl::InsertScope(ScopeData::Ptr const& scope_data, unsigned index)
{
  if (!scope_data)
    return;

  index = std::min(index, static_cast<unsigned>(scopes_.size()));

  auto start = scopes_.begin();
  auto scope_position = std::find_if(scopes_.begin(),
                                     scopes_.end(),
                                     [scope_data](Scope::Ptr const& value) { return value->id() == scope_data->id(); });
  if (scope_position == scopes_.end())
  {
    Scope::Ptr scope(owner_->CreateScope(scope_data));
    if (scope)
    {
      scopes_.insert(start+index, scope);

      owner_->scope_added.emit(scope, index);
    }
  }
  else
  {
    Scope::Ptr scope = *scope_position;
    if (scope_position > (start + index))
    {
      // move before
      scopes_.erase(scope_position);
      scopes_.insert(start + index, scope);

      owner_->scopes_reordered.emit(GetScopes());
    }
    else if (index > 0 &&
             scope_position < (start + index) &&
             scopes_.size() > index)
    {
      // move after
      scopes_.erase(scope_position);

      scopes_.insert(start + index, scope);

      owner_->scopes_reordered.emit(GetScopes());
    }
  }
}

void Scopes::Impl::RemoveScope(std::string const& scope_id)
{
  auto scope_position = std::find_if(scopes_.begin(),
                                     scopes_.end(),
                                     [scope_id](Scope::Ptr const& value) { return value->id() == scope_id; });

  if (scope_position != scopes_.end())
  {
    Scope::Ptr scope = *scope_position;
    scopes_.erase(scope_position);

    owner_->scope_removed.emit(scope);
  }
}

Scopes::ScopeList const& Scopes::Impl::GetScopes() const
{
  return scopes_;
}

Scope::Ptr Scopes::Impl::GetScope(std::string const& scope_id, int* index) const
{
  int tmp_index = 0;
  for (auto scope: scopes_)
  {
    if (scope->id == scope_id)
    {
      if (index)
        *index = tmp_index;
      return scope;
    }
    tmp_index++;
  }
  if (index)
    *index = -1;
  return Scope::Ptr();
}

Scope::Ptr Scopes::Impl::GetScopeAtIndex(std::size_t index) const
{
  try
  {
    return scopes_.at(index);
  }
  catch (std::out_of_range& error)
  {
    LOG_WARN(logger) << error.what();
  }
  return Scope::Ptr();
}

Scope::Ptr Scopes::Impl::GetScopeForShortcut(std::string const& scope_shortcut) const
{
  for (auto scope: scopes_)
  {
    if (scope->shortcut == scope_shortcut)
    {
      return scope;
    }
  }

  return Scope::Ptr();
}

Scopes::Scopes(ScopesReader::Ptr scopes_reader)
: pimpl(new Impl(this, scopes_reader))
{
  count.SetGetterFunction(sigc::mem_fun(pimpl.get(), &Impl::get_count));
}

Scopes::~Scopes()
{
}

void Scopes::LoadScopes()
{
  pimpl->LoadScopes();
}

Scopes::ScopeList const& Scopes::GetScopes() const
{
  return pimpl->GetScopes();
}

Scope::Ptr Scopes::GetScope(std::string const& scope_id, int* position) const
{
  return pimpl->GetScope(scope_id, position);
}

Scope::Ptr Scopes::GetScopeAtIndex(std::size_t index) const
{
  return pimpl->GetScopeAtIndex(index);
}

Scope::Ptr Scopes::GetScopeForShortcut(std::string const& scope_shortcut) const
{
  return pimpl->GetScopeForShortcut(scope_shortcut);
}

void Scopes::AppendScope(std::string const& scope_id)
{
  InsertScope(scope_id, pimpl->scopes_.size());
}

void Scopes::InsertScope(std::string const& scope_id, unsigned index)
{
  if (pimpl->scopes_reader_)
  {
    ScopeData::Ptr scope_data(pimpl->scopes_reader_->GetScopeDataById(scope_id));
    if (scope_data)
    {
      pimpl->InsertScope(scope_data, index);
      return;
    }
  }

  // Fallback - manually create and insert the scope.
  glib::Error error;
  ScopeData::Ptr scope_data(ScopeData::ReadProtocolDataForId(scope_id, error));
  scope_data->visible = false;
  if (scope_data && !error)
  {
    pimpl->InsertScope(scope_data, index);
  }
}

void Scopes::RemoveScope(std::string const& scope_id)
{
  pimpl->RemoveScope(scope_id);
}

Scope::Ptr Scopes::CreateScope(ScopeData::Ptr const& scope_data)
{
  if (!scope_data)
    return Scope::Ptr();

  LOG_DEBUG(logger) << "Creating scope: " << scope_data->id() << " (" << scope_data->dbus_path()  << " @ " << scope_data->dbus_name() << ")";
  Scope::Ptr scope(new Scope(scope_data));
  scope->Init();
  return scope;
}


} // namespace dash
} // namespace unity