on bd09d76 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
187 lines 5.4 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20#include "launchermodel.h"
21#include "launcheritem.h"
22#include "backend/launcherbackend.h"
24LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
26 m_backend(new LauncherBackend(this))
28 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
29 LauncherItem *item = new LauncherItem(entry,
30 m_backend->desktopFile(entry),
31 m_backend->displayName(entry),
32 m_backend->icon(entry),
33 this);
34 if (m_backend->isPinned(entry)) {
35 item->setPinned(true);
36 } else {
37 item->setRecent(true);
38 }
39 m_list.append(item);
40 }
43LauncherModel::~LauncherModel()
45 while (!m_list.empty()) {
46 m_list.takeFirst()->deleteLater();
47 }
50int LauncherModel::rowCount(const QModelIndex &parent) const
52 Q_UNUSED(parent)
53 return m_list.count();
56QVariant LauncherModel::data(const QModelIndex &index, int role) const
58 LauncherItem *item = m_list.at(index.row());
59 switch(role) {
60 case RoleAppId:
61 return item->desktopFile();
62 case RoleDesktopFile:
63 return item->desktopFile();
64 case RoleName:
65 return item->name();
66 case RoleIcon:
67 return item->icon();
68 case RolePinned:
69 return item->pinned();
70 }
72 return QVariant();
75unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
77 if (index < 0 || index >= m_list.count()) {
78 return 0;
79 }
80 return m_list.at(index);
83void LauncherModel::move(int oldIndex, int newIndex)
85 // Make sure its not moved outside the lists
86 if (newIndex < 0) {
87 newIndex = 0;
88 }
89 if (newIndex >= m_list.count()) {
90 newIndex = m_list.count()-1;
91 }
93 // Nothing to do?
94 if (oldIndex == newIndex) {
95 return;
96 }
98 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
99 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
100 // adjust the model's index by +1 in case we're moving upwards
101 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
103 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
104 m_list.move(oldIndex, newIndex);
105 endMoveRows();
107 storeAppList();
109 pin(m_list.at(newIndex)->appId());
112void LauncherModel::pin(const QString &appId, int index)
114 int currentIndex = findApplication(appId);
116 if (currentIndex >= 0) {
117 if (index == -1 || index == currentIndex) {
118 m_list.at(currentIndex)->setPinned(true);
119 QModelIndex modelIndex = this->index(currentIndex);
120 Q_EMIT dataChanged(modelIndex, modelIndex);
121 } else {
122 move(currentIndex, index);
123 }
124 } else {
125 if (index == -1) {
126 index = m_list.count();
127 }
128 beginInsertRows(QModelIndex(), index, index);
129 LauncherItem *item = new LauncherItem(appId,
130 m_backend->desktopFile(appId),
131 m_backend->displayName(appId),
132 m_backend->icon(appId));
133 item->setPinned(true);
134 m_list.insert(index, item);
135 endInsertRows();
136 }
139void LauncherModel::requestRemove(const QString &appId)
141 int index = findApplication(appId);
142 if (index < 0) {
143 return;
144 }
146 beginRemoveRows(QModelIndex(), index, index);
147 m_list.takeAt(index)->deleteLater();
148 endRemoveRows();
150 storeAppList();
153void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
155 int index = findApplication(appId);
156 if (index < 0) {
157 return;
158 }
160 QuickListModel *model = qobject_cast<QuickListModel*>(m_list.at(index)->quickList());
161 if (model) {
162 QString actionId = model->get(actionIndex).actionId();
163 m_backend->triggerQuickListAction(appId, actionId);
164 }
167void LauncherModel::storeAppList()
169 QStringList appIds;
170 Q_FOREACH(LauncherItem *item, m_list) {
171 if (item->pinned() || item->recent()) {
172 appIds << item->appId();
173 }
174 }
175 m_backend->setStoredApplications(appIds);
178int LauncherModel::findApplication(const QString &appId)
180 for (int i = 0; i < m_list.count(); ++i) {
181 LauncherItem *item = m_list.at(i);
182 if (item->appId() == appId) {
183 return i;
184 }
185 }
186 return -1;