~3v1n0/unity/overlay-border-scale

« back to all changes in this revision

Viewing changes to plugins/unityshell/src/LauncherModel.cpp

  • Committer: Daniel van Vugt
  • Date: 2012-03-14 06:24:18 UTC
  • mfrom: (2108 unity)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: daniel.van.vugt@canonical.com-20120314062418-nprucpbr0m7qky5e
MergedĀ latestĀ lp:unity

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include "LauncherModel.h"
21
 
#include "LauncherIcon.h"
 
21
#include "AbstractLauncherIcon.h"
 
22
 
 
23
#include <UnityCore/GLibWrapper.h>
 
24
#include <UnityCore/Variant.h>
22
25
 
23
26
namespace unity
24
27
{
25
28
namespace launcher
26
29
{
27
30
 
28
 
typedef struct
 
31
struct RemoveArg
29
32
{
30
 
  LauncherIcon* icon;
 
33
  RemoveArg(AbstractLauncherIcon::Ptr const& icon_, LauncherModel* model)
 
34
    : icon(icon_), self(model)
 
35
  {}
 
36
 
 
37
  AbstractLauncherIcon::Ptr icon;
31
38
  LauncherModel* self;
32
 
} RemoveArg;
 
39
};
33
40
 
34
41
LauncherModel::LauncherModel()
35
42
{
 
43
  selection_ = 0;
36
44
}
37
45
 
38
46
LauncherModel::~LauncherModel()
39
47
{
40
 
  for (auto icon : _inner_shelf)
41
 
    icon->UnReference();
42
 
 
43
 
  for (auto icon : _inner_main)
44
 
    icon->UnReference();
45
 
}
46
 
 
47
 
bool LauncherModel::IconShouldShelf(LauncherIcon* icon)
48
 
{
49
 
  return icon->Type() == LauncherIcon::TYPE_TRASH;
50
 
}
51
 
 
52
 
bool LauncherModel::CompareIcons(LauncherIcon* first, LauncherIcon* second)
53
 
{
54
 
  if (first->Type() < second->Type())
 
48
}
 
49
 
 
50
std::string LauncherModel::GetName() const
 
51
{
 
52
  return "LauncherModel";
 
53
}
 
54
 
 
55
void LauncherModel::AddProperties(GVariantBuilder* builder)
 
56
{
 
57
  unity::variant::BuilderWrapper(builder)
 
58
  .add("selection", selection_);
 
59
}
 
60
 
 
61
unity::debug::Introspectable::IntrospectableList const& LauncherModel::GetIntrospectableChildren()
 
62
{
 
63
  introspection_results_.clear();
 
64
  
 
65
  for (auto icon : _inner)
 
66
    introspection_results_.push_back(icon.GetPointer());
 
67
  
 
68
  return introspection_results_;
 
69
}
 
70
 
 
71
bool LauncherModel::IconShouldShelf(AbstractLauncherIcon::Ptr icon) const
 
72
{
 
73
  return icon->GetIconType() == AbstractLauncherIcon::TYPE_TRASH;
 
74
}
 
75
 
 
76
bool LauncherModel::CompareIcons(AbstractLauncherIcon::Ptr first, AbstractLauncherIcon::Ptr second)
 
77
{
 
78
  if (first->GetIconType() < second->GetIconType())
55
79
    return true;
56
 
  else if (first->Type() > second->Type())
 
80
  else if (first->GetIconType() > second->GetIconType())
57
81
    return false;
58
82
 
59
83
  return first->SortPriority() < second->SortPriority();
72
96
  for (it = main_begin(); it != main_end(); it++)
73
97
  {
74
98
    _inner.push_back(*it);
75
 
    (*it)->SetSortPriority(i++);
 
99
    (*it)->SetSortPriority(i);
 
100
    ++i;
76
101
  }
77
102
 
78
103
  for (it = shelf_begin(); it != shelf_end(); it++)
79
104
  {
80
105
    _inner.push_back(*it);
81
 
    (*it)->SetSortPriority(i++);
 
106
    (*it)->SetSortPriority(i);
 
107
    ++i;
82
108
  }
83
109
 
84
 
  return !std::equal(begin(), end(), copy.begin());
 
110
  return copy.size() == _inner.size() && !std::equal(begin(), end(), copy.begin());
85
111
}
86
112
 
87
113
void
88
 
LauncherModel::AddIcon(LauncherIcon* icon)
 
114
LauncherModel::AddIcon(AbstractLauncherIcon::Ptr icon)
89
115
{
90
 
  icon->SinkReference();
91
 
 
92
116
  if (IconShouldShelf(icon))
93
 
    _inner_shelf.push_front(icon);
 
117
    _inner_shelf.push_back(icon);
94
118
  else
95
 
    _inner_main.push_front(icon);
 
119
    _inner_main.push_back(icon);
96
120
 
97
121
  Sort();
98
122
 
104
128
}
105
129
 
106
130
void
107
 
LauncherModel::RemoveIcon(LauncherIcon* icon)
 
131
LauncherModel::RemoveIcon(AbstractLauncherIcon::Ptr icon)
108
132
{
109
133
  size_t size;
110
134
 
111
 
  _inner_shelf.remove(icon);
112
 
  _inner_main.remove(icon);
 
135
  _inner_shelf.erase(std::remove(_inner_shelf.begin(), _inner_shelf.end(), icon), _inner_shelf.end());
 
136
  _inner_main.erase(std::remove(_inner_main.begin(), _inner_main.end(), icon), _inner_main.end());
113
137
 
114
138
  size = _inner.size();
115
 
  _inner.remove(icon);
 
139
  _inner.erase(std::remove(_inner.begin(), _inner.end(), icon), _inner.end());
116
140
 
117
141
  if (size != _inner.size())
118
142
  {
119
143
    icon_removed.emit(icon);
120
 
    icon->UnReference();
121
144
  }
122
145
}
123
146
 
124
147
gboolean
125
148
LauncherModel::RemoveCallback(gpointer data)
126
149
{
127
 
  RemoveArg* arg = (RemoveArg*) data;
 
150
  RemoveArg* arg = static_cast<RemoveArg*>(data);
128
151
 
129
 
  arg->self->RemoveIcon(arg->icon);
130
 
  g_free(arg);
 
152
  if (arg)
 
153
  {
 
154
    arg->self->RemoveIcon(arg->icon);
 
155
    delete arg;
 
156
  }
131
157
 
132
158
  return false;
133
159
}
134
160
 
135
161
void
136
 
LauncherModel::OnIconRemove(LauncherIcon* icon)
 
162
LauncherModel::OnIconRemove(AbstractLauncherIcon::Ptr icon)
137
163
{
138
 
  RemoveArg* arg = (RemoveArg*) g_malloc0(sizeof(RemoveArg));
139
 
  arg->icon = icon;
140
 
  arg->self = this;
141
 
 
 
164
  RemoveArg* arg = new RemoveArg(icon, this);
142
165
  g_timeout_add(1000, &LauncherModel::RemoveCallback, arg);
143
166
}
144
167
 
151
174
void
152
175
LauncherModel::Sort()
153
176
{
154
 
  _inner_shelf.sort(&LauncherModel::CompareIcons);
155
 
  _inner_main.sort(&LauncherModel::CompareIcons);
 
177
  std::stable_sort(_inner_shelf.begin(), _inner_shelf.end(), &LauncherModel::CompareIcons);
 
178
  std::stable_sort(_inner_main.begin(), _inner_main.end(), &LauncherModel::CompareIcons);
156
179
 
157
180
  if (Populate())
158
181
    order_changed.emit();
159
182
}
160
183
 
161
184
bool
162
 
LauncherModel::IconHasSister(LauncherIcon* icon)
 
185
LauncherModel::IconHasSister(AbstractLauncherIcon::Ptr icon) const
163
186
{
164
 
  iterator it;
165
 
  iterator end;
 
187
  const_iterator it;
 
188
  const_iterator end;
166
189
 
167
 
  if (icon && icon->Type() == AbstractLauncherIcon::TYPE_DEVICE)
 
190
  if (icon && icon->GetIconType() == AbstractLauncherIcon::TYPE_DEVICE)
168
191
    return true;
169
192
 
170
193
  if (IconShouldShelf(icon))
171
194
  {
172
 
    it = shelf_begin();
173
 
    end = shelf_end();
 
195
    it = _inner_shelf.begin();
 
196
    end = _inner_shelf.end();
174
197
  }
175
198
  else
176
199
  {
177
 
    it = main_begin();
178
 
    end = main_end();
 
200
    it = _inner_main.begin();
 
201
    end = _inner_main.end();
179
202
  }
180
203
 
181
204
  for (; it != end; ++it)
182
205
  {
183
 
    LauncherIcon* iter_icon = *it;
 
206
    AbstractLauncherIcon::Ptr iter_icon = *it;
184
207
    if ((iter_icon  != icon)
185
 
        && iter_icon->Type() == icon->Type())
 
208
        && iter_icon->GetIconType() == icon->GetIconType())
186
209
      return true;
187
210
  }
188
211
 
190
213
}
191
214
 
192
215
void
193
 
LauncherModel::ReorderBefore(LauncherIcon* icon, LauncherIcon* other, bool save)
 
216
LauncherModel::ReorderAfter(AbstractLauncherIcon::Ptr icon, AbstractLauncherIcon::Ptr other)
194
217
{
195
 
  if (icon == other)
 
218
  if (icon == other || icon.IsNull() || other.IsNull())
196
219
    return;
197
220
 
198
 
  LauncherModel::iterator it;
 
221
  if (icon->GetIconType() != other->GetIconType())
 
222
    return;
199
223
 
200
224
  int i = 0;
201
 
  int j = 0;
202
 
  for (it = begin(); it != end(); it++)
 
225
  for (LauncherModel::iterator it = begin(); it != end(); ++it)
203
226
  {
204
227
    if ((*it) == icon)
 
228
      continue;
 
229
 
 
230
    if ((*it) == other)
 
231
    {
 
232
      (*it)->SetSortPriority(i);
 
233
      ++i;
 
234
 
 
235
      icon->SetSortPriority(i);
 
236
      ++i;
 
237
    }
 
238
    else
 
239
    {
 
240
      (*it)->SetSortPriority(i);
 
241
      ++i;
 
242
    }
 
243
  }
 
244
 
 
245
  Sort();
 
246
}
 
247
 
 
248
void
 
249
LauncherModel::ReorderBefore(AbstractLauncherIcon::Ptr icon, AbstractLauncherIcon::Ptr other, bool save)
 
250
{
 
251
  if (icon == other || icon.IsNull() || other.IsNull())
 
252
    return;
 
253
 
 
254
  if (icon->GetIconType() != other->GetIconType())
 
255
    return;
 
256
 
 
257
  int i = 0;
 
258
  int j = 0;
 
259
  for (auto icon_it : _inner)
 
260
  {
 
261
    if (icon_it == icon)
205
262
    {
206
263
      j++;
207
264
      continue;
208
265
    }
209
266
 
210
 
    if ((*it) == other)
 
267
    if (icon_it == other)
211
268
    {
212
269
      icon->SetSortPriority(i);
213
270
      if (i != j && save)
214
 
        (*it)->SaveCenter();
 
271
        icon_it->SaveCenter();
215
272
      i++;
216
273
 
217
 
      (*it)->SetSortPriority(i);
 
274
      icon_it->SetSortPriority(i);
218
275
      if (i != j && save)
219
 
        (*it)->SaveCenter();
 
276
        icon_it->SaveCenter();
220
277
      i++;
221
278
    }
222
279
    else
223
280
    {
224
 
      (*it)->SetSortPriority(i);
 
281
      icon_it->SetSortPriority(i);
225
282
      if (i != j && save)
226
 
        (*it)->SaveCenter();
 
283
        icon_it->SaveCenter();
227
284
      i++;
228
285
    }
229
286
    j++;
233
290
}
234
291
 
235
292
void
236
 
LauncherModel::ReorderSmart(LauncherIcon* icon, LauncherIcon* other, bool save)
 
293
LauncherModel::ReorderSmart(AbstractLauncherIcon::Ptr icon, AbstractLauncherIcon::Ptr other, bool save)
237
294
{
238
 
  if (icon == other)
 
295
  if (icon == other || icon.IsNull() || other.IsNull())
239
296
    return;
240
297
 
241
 
  LauncherModel::iterator it;
 
298
  if (icon->GetIconType() != other->GetIconType())
 
299
    return;
242
300
 
243
301
  int i = 0;
244
302
  int j = 0;
245
303
  bool skipped = false;
246
 
  for (it = begin(); it != end(); it++)
 
304
  for (auto icon_it : _inner)
247
305
  {
248
 
    if ((*it) == icon)
 
306
    if (icon_it == icon)
249
307
    {
250
308
      skipped = true;
251
309
      j++;
252
310
      continue;
253
311
    }
254
312
 
255
 
    if ((*it) == other)
 
313
    if (icon_it == other)
256
314
    {
257
315
      if (!skipped)
258
316
      {
259
317
        icon->SetSortPriority(i);
260
318
        if (i != j && save)
261
 
          (*it)->SaveCenter();
 
319
          icon_it->SaveCenter();
262
320
        i++;
263
321
      }
264
322
 
265
 
      (*it)->SetSortPriority(i);
 
323
      icon_it->SetSortPriority(i);
266
324
      if (i != j && save)
267
 
        (*it)->SaveCenter();
 
325
        icon_it->SaveCenter();
268
326
      i++;
269
327
 
270
328
      if (skipped)
271
329
      {
272
330
        icon->SetSortPriority(i);
273
331
        if (i != j && save)
274
 
          (*it)->SaveCenter();
 
332
          icon_it->SaveCenter();
275
333
        i++;
276
334
      }
277
335
    }
278
336
    else
279
337
    {
280
 
      (*it)->SetSortPriority(i);
 
338
      icon_it->SetSortPriority(i);
281
339
      if (i != j && save)
282
 
        (*it)->SaveCenter();
 
340
        icon_it->SaveCenter();
283
341
      i++;
284
342
    }
285
343
    j++;
289
347
}
290
348
 
291
349
int
292
 
LauncherModel::Size()
 
350
LauncherModel::Size() const
293
351
{
294
352
  return _inner.size();
295
353
}
296
354
 
 
355
AbstractLauncherIcon::Ptr LauncherModel::Selection () const
 
356
{
 
357
  return _inner[selection_];
 
358
}
 
359
 
 
360
int LauncherModel::SelectionIndex() const
 
361
{
 
362
  return selection_;
 
363
}
 
364
 
 
365
void LauncherModel::SetSelection(int selection)
 
366
{
 
367
  int new_selection = std::min<int>(Size() - 1, std::max<int> (0, selection));
 
368
 
 
369
  if (new_selection == selection_)
 
370
    return;
 
371
  
 
372
  selection_ = new_selection;
 
373
  selection_changed.emit(Selection());
 
374
}
 
375
 
 
376
void LauncherModel::SelectNext()
 
377
{
 
378
  int temp = selection_;
 
379
 
 
380
  temp++;
 
381
  while (temp != selection_)
 
382
  {
 
383
    if (temp >= Size())
 
384
      temp = 0;
 
385
 
 
386
    if (_inner[temp]->GetQuirk(AbstractLauncherIcon::QUIRK_VISIBLE))
 
387
    {
 
388
      selection_ = temp;
 
389
      selection_changed.emit(Selection());
 
390
      break;
 
391
    }
 
392
    temp++;
 
393
  }
 
394
}
 
395
 
 
396
void LauncherModel::SelectPrevious()
 
397
{
 
398
  int temp = selection_;
 
399
 
 
400
  temp--;
 
401
  while (temp != selection_)
 
402
  {
 
403
    if (temp < 0)
 
404
      temp = Size() - 1;
 
405
 
 
406
    if (_inner[temp]->GetQuirk(AbstractLauncherIcon::QUIRK_VISIBLE))
 
407
    {
 
408
      selection_ = temp;
 
409
      selection_changed.emit(Selection());
 
410
      break;
 
411
    }
 
412
    temp--;
 
413
  }
 
414
}
 
415
 
 
416
 
297
417
/* iterators */
298
418
 
299
419
LauncherModel::iterator