~om26er/ubuntu/oneiric/unity/sru-778256

« back to all changes in this revision

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

  • Committer: Didier Roche
  • Date: 2011-07-21 16:17:59 UTC
  • mfrom: (55.813.3 upstream)
  • Revision ID: didier.roche@canonical.com-20110721161759-osmh94x428t2bf2b
* New upstream release.
* debian/control:
  - build-dep on libnotify-dev
  - bump libnux-1.0-dev dep for ABI break

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include "LauncherModel.h"
21
21
#include "LauncherIcon.h"
22
 
#include "Launcher.h"
23
22
 
24
23
typedef struct
25
24
{
26
 
  LauncherIcon *icon;
27
 
  LauncherModel *self;
 
25
  LauncherIcon* icon;
 
26
  LauncherModel* self;
28
27
} RemoveArg;
29
28
 
30
29
LauncherModel::LauncherModel()
33
32
 
34
33
LauncherModel::~LauncherModel()
35
34
{
36
 
  for (iterator it = _inner_shelf.begin (); it != _inner_shelf.end (); ++it)
37
 
    reinterpret_cast<LauncherIcon*> (*it)->UnReference ();
38
 
  _inner_shelf.clear ();
39
 
 
40
 
  for (iterator it = _inner_main.begin (); it != _inner_main.end (); ++it)
41
 
    reinterpret_cast<LauncherIcon*> (*it)->UnReference ();
42
 
  _inner_main.clear ();
43
 
 
44
 
  _inner.clear ();
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 ())
 
35
  for (iterator it = _inner_shelf.begin(); it != _inner_shelf.end(); ++it)
 
36
    reinterpret_cast<LauncherIcon*>(*it)->UnReference();
 
37
  _inner_shelf.clear();
 
38
 
 
39
  for (iterator it = _inner_main.begin(); it != _inner_main.end(); ++it)
 
40
    reinterpret_cast<LauncherIcon*>(*it)->UnReference();
 
41
  _inner_main.clear();
 
42
 
 
43
  _inner.clear();
 
44
}
 
45
 
 
46
bool LauncherModel::IconShouldShelf(LauncherIcon* icon)
 
47
{
 
48
  return icon->Type() == LauncherIcon::TYPE_TRASH;
 
49
}
 
50
 
 
51
bool LauncherModel::CompareIcons(LauncherIcon* first, LauncherIcon* second)
 
52
{
 
53
  if (first->Type() < second->Type())
55
54
    return true;
56
 
  else if (first->Type () > second->Type ())
 
55
  else if (first->Type() > second->Type())
57
56
    return false;
58
 
    
59
 
  return first->SortPriority () < second->SortPriority ();
 
57
 
 
58
  return first->SortPriority() < second->SortPriority();
60
59
}
61
60
 
62
61
bool
63
 
LauncherModel::Populate ()
 
62
LauncherModel::Populate()
64
63
{
65
64
  Base copy = _inner;
66
 
  
67
 
  _inner.clear ();
68
 
  
 
65
 
 
66
  _inner.clear();
 
67
 
69
68
  iterator it, it2;
70
 
  
 
69
 
71
70
  int i = 0;
72
 
  for (it = main_begin (); it != main_end (); it++)
73
 
  {
74
 
    _inner.push_back (*it);
75
 
    (*it)->SetSortPriority (i++);
76
 
  }
77
 
  
78
 
  for (it = shelf_begin (); it != shelf_end (); it++)
79
 
  {
80
 
    _inner.push_back (*it);
81
 
    (*it)->SetSortPriority (i++);
82
 
  }
83
 
  
84
 
  return !std::equal (begin (), end (), copy.begin ());
 
71
  for (it = main_begin(); it != main_end(); it++)
 
72
  {
 
73
    _inner.push_back(*it);
 
74
    (*it)->SetSortPriority(i++);
 
75
  }
 
76
 
 
77
  for (it = shelf_begin(); it != shelf_end(); it++)
 
78
  {
 
79
    _inner.push_back(*it);
 
80
    (*it)->SetSortPriority(i++);
 
81
  }
 
82
 
 
83
  return !std::equal(begin(), end(), copy.begin());
85
84
}
86
85
 
87
 
void 
88
 
LauncherModel::AddIcon (LauncherIcon *icon)
 
86
void
 
87
LauncherModel::AddIcon(LauncherIcon* icon)
89
88
{
90
 
  icon->SinkReference ();
91
 
  
92
 
  if (IconShouldShelf (icon))
93
 
    _inner_shelf.push_front (icon);
 
89
  icon->SinkReference();
 
90
 
 
91
  if (IconShouldShelf(icon))
 
92
    _inner_shelf.push_front(icon);
94
93
  else
95
 
    _inner_main.push_front (icon);
96
 
  
97
 
  Sort ();
98
 
  
99
 
  icon_added.emit (icon);
100
 
 
101
 
  if (icon->on_icon_removed_connection.connected ())
102
 
    icon->on_icon_removed_connection.disconnect ();
103
 
  icon->on_icon_removed_connection = (sigc::connection) icon->remove.connect (sigc::mem_fun (this, &LauncherModel::OnIconRemove));
 
94
    _inner_main.push_front(icon);
 
95
 
 
96
  Sort();
 
97
 
 
98
  icon_added.emit(icon);
 
99
 
 
100
  if (icon->on_icon_removed_connection.connected())
 
101
    icon->on_icon_removed_connection.disconnect();
 
102
  icon->on_icon_removed_connection = icon->remove.connect(sigc::mem_fun(this, &LauncherModel::OnIconRemove));
104
103
}
105
104
 
106
105
void
107
 
LauncherModel::RemoveIcon (LauncherIcon *icon)
 
106
LauncherModel::RemoveIcon(LauncherIcon* icon)
108
107
{
109
108
  size_t size;
110
 
  
111
 
  _inner_shelf.remove (icon);
112
 
  _inner_main.remove (icon);
113
 
  
114
 
  size = _inner.size ();
115
 
  _inner.remove (icon);
116
 
 
117
 
  if (size != _inner.size ())
 
109
 
 
110
  _inner_shelf.remove(icon);
 
111
  _inner_main.remove(icon);
 
112
 
 
113
  size = _inner.size();
 
114
  _inner.remove(icon);
 
115
 
 
116
  if (size != _inner.size())
118
117
  {
119
 
    icon_removed.emit (icon);
120
 
    icon->UnReference ();
 
118
    icon_removed.emit(icon);
 
119
    icon->UnReference();
121
120
  }
122
121
}
123
122
 
124
123
gboolean
125
 
LauncherModel::RemoveCallback (gpointer data)
 
124
LauncherModel::RemoveCallback(gpointer data)
126
125
{
127
 
  RemoveArg *arg = (RemoveArg*) data;
128
 
 
129
 
  arg->self->RemoveIcon (arg->icon);
130
 
  g_free (arg);
131
 
  
 
126
  RemoveArg* arg = (RemoveArg*) data;
 
127
 
 
128
  arg->self->RemoveIcon(arg->icon);
 
129
  g_free(arg);
 
130
 
132
131
  return false;
133
132
}
134
133
 
135
 
void 
136
 
LauncherModel::OnIconRemove (LauncherIcon *icon)
 
134
void
 
135
LauncherModel::OnIconRemove(LauncherIcon* icon)
137
136
{
138
 
  RemoveArg *arg = (RemoveArg*) g_malloc0 (sizeof (RemoveArg));
 
137
  RemoveArg* arg = (RemoveArg*) g_malloc0(sizeof(RemoveArg));
139
138
  arg->icon = icon;
140
139
  arg->self = this;
141
 
  
142
 
  g_timeout_add (1000, &LauncherModel::RemoveCallback, arg);
 
140
 
 
141
  g_timeout_add(1000, &LauncherModel::RemoveCallback, arg);
143
142
}
144
143
 
145
 
void 
146
 
LauncherModel::Sort ()
 
144
void
 
145
LauncherModel::Sort()
147
146
{
148
 
  _inner_shelf.sort (&LauncherModel::CompareIcons);
149
 
  _inner_main.sort (&LauncherModel::CompareIcons);
150
 
  
151
 
  if (Populate ())
152
 
    order_changed.emit ();
 
147
  _inner_shelf.sort(&LauncherModel::CompareIcons);
 
148
  _inner_main.sort(&LauncherModel::CompareIcons);
 
149
 
 
150
  if (Populate())
 
151
    order_changed.emit();
153
152
}
154
153
 
155
154
bool
156
 
LauncherModel::IconHasSister (LauncherIcon *icon)
 
155
LauncherModel::IconHasSister(LauncherIcon* icon)
157
156
{
158
 
  iterator (LauncherModel::*begin_it)(void);
159
 
  iterator (LauncherModel::*end_it)(void);
 
157
  iterator(LauncherModel::*begin_it)(void);
 
158
  iterator(LauncherModel::*end_it)(void);
160
159
  iterator it;
161
 
  
162
 
  if (IconShouldShelf (icon))
 
160
 
 
161
  if (IconShouldShelf(icon))
163
162
  {
164
163
    begin_it = &LauncherModel::shelf_begin;
165
164
    end_it = &LauncherModel::shelf_end;
169
168
    begin_it = &LauncherModel::main_begin;
170
169
    end_it = &LauncherModel::main_end;
171
170
  }
172
 
  
173
 
  for (it = (this->*begin_it) (); it != (this->*end_it) (); it++)
 
171
 
 
172
  for (it = (this->*begin_it)(); it != (this->*end_it)(); it++)
174
173
  {
175
174
    if ((*it  != icon)
176
 
        && (*it)->Type () == icon->Type ())
 
175
        && (*it)->Type() == icon->Type())
177
176
      return true;
178
177
  }
179
 
  
 
178
 
180
179
  return false;
181
180
}
182
181
 
183
182
void
184
 
LauncherModel::ReorderBefore (LauncherIcon *icon, LauncherIcon *other, bool save)
 
183
LauncherModel::ReorderBefore(LauncherIcon* icon, LauncherIcon* other, bool save)
185
184
{
186
185
  if (icon == other)
187
186
    return;
188
187
 
189
188
  LauncherModel::iterator it;
190
 
  
 
189
 
191
190
  int i = 0;
192
191
  int j = 0;
193
 
  for (it = begin (); it != end (); it++)
 
192
  for (it = begin(); it != end(); it++)
194
193
  {
195
194
    if ((*it) == icon)
196
195
    {
197
196
      j++;
198
197
      continue;
199
198
    }
200
 
    
 
199
 
201
200
    if ((*it) == other)
202
201
    {
203
 
      icon->SetSortPriority (i);
204
 
      if (i != j && save) (*it)->SaveCenter ();
 
202
      icon->SetSortPriority(i);
 
203
      if (i != j && save)
 
204
        (*it)->SaveCenter();
205
205
      i++;
206
 
      
207
 
      (*it)->SetSortPriority (i);
208
 
      if (i != j && save) (*it)->SaveCenter ();
 
206
 
 
207
      (*it)->SetSortPriority(i);
 
208
      if (i != j && save)
 
209
        (*it)->SaveCenter();
209
210
      i++;
210
211
    }
211
212
    else
212
213
    {
213
 
      (*it)->SetSortPriority (i);
214
 
      if (i != j && save) (*it)->SaveCenter ();
 
214
      (*it)->SetSortPriority(i);
 
215
      if (i != j && save)
 
216
        (*it)->SaveCenter();
215
217
      i++;
216
218
    }
217
219
    j++;
218
220
  }
219
 
  
220
 
  Sort ();
 
221
 
 
222
  Sort();
221
223
}
222
224
 
223
225
void
224
 
LauncherModel::ReorderSmart (LauncherIcon *icon, LauncherIcon *other, bool save)
 
226
LauncherModel::ReorderSmart(LauncherIcon* icon, LauncherIcon* other, bool save)
225
227
{
226
228
  if (icon == other)
227
229
    return;
228
230
 
229
231
  LauncherModel::iterator it;
230
 
  
 
232
 
231
233
  int i = 0;
232
234
  int j = 0;
233
235
  bool skipped = false;
234
 
  for (it = begin (); it != end (); it++)
 
236
  for (it = begin(); it != end(); it++)
235
237
  {
236
238
    if ((*it) == icon)
237
239
    {
239
241
      j++;
240
242
      continue;
241
243
    }
242
 
    
 
244
 
243
245
    if ((*it) == other)
244
246
    {
245
247
      if (!skipped)
246
248
      {
247
 
        icon->SetSortPriority (i);
248
 
        if (i != j && save) (*it)->SaveCenter ();
 
249
        icon->SetSortPriority(i);
 
250
        if (i != j && save)
 
251
          (*it)->SaveCenter();
249
252
        i++;
250
253
      }
251
 
      
252
 
      (*it)->SetSortPriority (i);
253
 
      if (i != j && save) (*it)->SaveCenter ();
 
254
 
 
255
      (*it)->SetSortPriority(i);
 
256
      if (i != j && save)
 
257
        (*it)->SaveCenter();
254
258
      i++;
255
 
      
 
259
 
256
260
      if (skipped)
257
261
      {
258
 
        icon->SetSortPriority (i);
259
 
        if (i != j && save) (*it)->SaveCenter ();
 
262
        icon->SetSortPriority(i);
 
263
        if (i != j && save)
 
264
          (*it)->SaveCenter();
260
265
        i++;
261
266
      }
262
267
    }
263
268
    else
264
269
    {
265
 
      (*it)->SetSortPriority (i);
266
 
      if (i != j && save) (*it)->SaveCenter ();
 
270
      (*it)->SetSortPriority(i);
 
271
      if (i != j && save)
 
272
        (*it)->SaveCenter();
267
273
      i++;
268
274
    }
269
275
    j++;
270
276
  }
271
 
  
272
 
  Sort ();
 
277
 
 
278
  Sort();
273
279
}
274
280
 
275
281
int
276
 
LauncherModel::Size ()
 
282
LauncherModel::Size()
277
283
{
278
 
  return _inner.size ();
 
284
  return _inner.size();
279
285
}
280
286
 
281
287
/* iterators */
282
 
    
283
 
LauncherModel::iterator 
284
 
LauncherModel::begin ()
285
 
{
286
 
  return _inner.begin ();
287
 
}
288
 
 
289
 
LauncherModel::iterator 
290
 
LauncherModel::end ()
291
 
{
292
 
  return _inner.end ();
293
 
}
294
 
 
295
 
LauncherModel::iterator 
296
 
LauncherModel::at (int index)
 
288
 
 
289
LauncherModel::iterator
 
290
LauncherModel::begin()
 
291
{
 
292
  return _inner.begin();
 
293
}
 
294
 
 
295
LauncherModel::iterator
 
296
LauncherModel::end()
 
297
{
 
298
  return _inner.end();
 
299
}
 
300
 
 
301
LauncherModel::iterator
 
302
LauncherModel::at(int index)
297
303
{
298
304
  LauncherModel::iterator it;
299
305
  int i;
300
306
 
301
307
  // start currently selected icon
302
 
  for (it = _inner.begin (), i = 0; it != _inner.end (); it++, i++)
 
308
  for (it = _inner.begin(), i = 0; it != _inner.end(); it++, i++)
303
309
  {
304
310
    if (i == index)
305
311
      return it;
306
312
  }
307
 
      
 
313
 
308
314
  return (LauncherModel::iterator)NULL;
309
315
}
310
 
      
311
 
LauncherModel::reverse_iterator 
312
 
LauncherModel::rbegin ()
313
 
{
314
 
  return _inner.rbegin ();
315
 
}
316
 
 
317
 
LauncherModel::reverse_iterator 
318
 
LauncherModel::rend ()
319
 
{
320
 
  return _inner.rend ();
321
 
}
322
 
 
323
 
LauncherModel::iterator 
324
 
LauncherModel::main_begin ()
325
 
{
326
 
  return _inner_main.begin ();
327
 
}
328
 
 
329
 
LauncherModel::iterator 
330
 
LauncherModel::main_end ()
331
 
{
332
 
  return _inner_main.end ();
333
 
}
334
 
 
335
 
LauncherModel::reverse_iterator 
336
 
LauncherModel::main_rbegin ()
337
 
{
338
 
  return _inner_main.rbegin ();
339
 
}
340
 
 
341
 
LauncherModel::reverse_iterator 
342
 
LauncherModel::main_rend ()
343
 
{
344
 
  return _inner_main.rend ();
345
 
}
346
 
 
347
 
LauncherModel::iterator 
348
 
LauncherModel::shelf_begin ()
349
 
{
350
 
  return _inner_shelf.begin ();
351
 
}
352
 
 
353
 
LauncherModel::iterator 
354
 
LauncherModel::shelf_end ()
355
 
{
356
 
  return _inner_shelf.end ();
357
 
}
358
 
 
359
 
LauncherModel::reverse_iterator 
360
 
LauncherModel::shelf_rbegin ()
361
 
{
362
 
  return _inner_shelf.rbegin ();
363
 
}
364
 
 
365
 
LauncherModel::reverse_iterator 
366
 
LauncherModel::shelf_rend ()
367
 
{
368
 
  return _inner_shelf.rend ();
 
316
 
 
317
LauncherModel::reverse_iterator
 
318
LauncherModel::rbegin()
 
319
{
 
320
  return _inner.rbegin();
 
321
}
 
322
 
 
323
LauncherModel::reverse_iterator
 
324
LauncherModel::rend()
 
325
{
 
326
  return _inner.rend();
 
327
}
 
328
 
 
329
LauncherModel::iterator
 
330
LauncherModel::main_begin()
 
331
{
 
332
  return _inner_main.begin();
 
333
}
 
334
 
 
335
LauncherModel::iterator
 
336
LauncherModel::main_end()
 
337
{
 
338
  return _inner_main.end();
 
339
}
 
340
 
 
341
LauncherModel::reverse_iterator
 
342
LauncherModel::main_rbegin()
 
343
{
 
344
  return _inner_main.rbegin();
 
345
}
 
346
 
 
347
LauncherModel::reverse_iterator
 
348
LauncherModel::main_rend()
 
349
{
 
350
  return _inner_main.rend();
 
351
}
 
352
 
 
353
LauncherModel::iterator
 
354
LauncherModel::shelf_begin()
 
355
{
 
356
  return _inner_shelf.begin();
 
357
}
 
358
 
 
359
LauncherModel::iterator
 
360
LauncherModel::shelf_end()
 
361
{
 
362
  return _inner_shelf.end();
 
363
}
 
364
 
 
365
LauncherModel::reverse_iterator
 
366
LauncherModel::shelf_rbegin()
 
367
{
 
368
  return _inner_shelf.rbegin();
 
369
}
 
370
 
 
371
LauncherModel::reverse_iterator
 
372
LauncherModel::shelf_rend()
 
373
{
 
374
  return _inner_shelf.rend();
369
375
}