on 44eced4 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
239 lines 7.0 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"
24#include <QDebug>
26LauncherModel::LauncherModel(QObject *parent):
27 LauncherModelInterface(parent),
28 m_backend(new LauncherBackend(true, this))
30 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
31 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
33 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
34 LauncherItem *item = new LauncherItem(entry,
35 m_backend->desktopFile(entry),
36 m_backend->displayName(entry),
37 m_backend->icon(entry),
38 this);
39 qDebug() << "**************" << "got icon" << m_backend->icon(entry);
40 if (m_backend->isPinned(entry)) {
41 item->setPinned(true);
42 } else {
43 item->setRecent(true);
44 }
45 m_list.append(item);
46 }
47 qDebug() << "ctor done";
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
54 }
57int LauncherModel::rowCount(const QModelIndex &parent) const
59 Q_UNUSED(parent)
60 return m_list.count();
63QVariant LauncherModel::data(const QModelIndex &index, int role) const
65 LauncherItem *item = m_list.at(index.row());
66 switch(role) {
67 case RoleAppId:
68 return item->desktopFile();
69 case RoleDesktopFile:
70 return item->desktopFile();
71 case RoleName:
72 return item->name();
73 case RoleIcon:
74 return item->icon();
75 case RolePinned:
76 return item->pinned();
77 case RoleCount:
78 return item->count();
79 case RoleProgress:
80 return item->progress();
81 }
83 return QVariant();
86unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
88 if (index < 0 || index >= m_list.count()) {
89 return 0;
90 }
91 return m_list.at(index);
94void LauncherModel::move(int oldIndex, int newIndex)
96 // Make sure its not moved outside the lists
97 if (newIndex < 0) {
98 newIndex = 0;
99 }
100 if (newIndex >= m_list.count()) {
101 newIndex = m_list.count()-1;
102 }
104 // Nothing to do?
105 if (oldIndex == newIndex) {
106 return;
107 }
109 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
110 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
111 // adjust the model's index by +1 in case we're moving upwards
112 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
114 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
115 m_list.move(oldIndex, newIndex);
116 endMoveRows();
118 storeAppList();
120 pin(m_list.at(newIndex)->appId());
123void LauncherModel::pin(const QString &appId, int index)
125 int currentIndex = findApplication(appId);
127 if (currentIndex >= 0) {
128 if (index == -1 || index == currentIndex) {
129 m_list.at(currentIndex)->setPinned(true);
130 m_backend->setPinned(appId, true);
131 QModelIndex modelIndex = this->index(currentIndex);
132 Q_EMIT dataChanged(modelIndex, modelIndex);
133 } else {
134 move(currentIndex, index);
135 }
136 } else {
137 if (index == -1) {
138 index = m_list.count();
139 }
140 beginInsertRows(QModelIndex(), index, index);
141 LauncherItem *item = new LauncherItem(appId,
142 m_backend->desktopFile(appId),
143 m_backend->displayName(appId),
144 m_backend->icon(appId));
145 item->setPinned(true);
146 m_backend->setPinned(appId, true);
147 m_list.insert(index, item);
148 endInsertRows();
149 }
152void LauncherModel::requestRemove(const QString &appId)
154 int index = findApplication(appId);
155 if (index < 0) {
156 return;
157 }
159 beginRemoveRows(QModelIndex(), index, index);
160 m_list.takeAt(index)->deleteLater();
161 endRemoveRows();
163 storeAppList();
166void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
168 int index = findApplication(appId);
169 if (index < 0) {
170 return;
171 }
173 LauncherItem *item = m_list.at(index);
174 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
175 if (model) {
176 QString actionId = model->get(actionIndex).actionId();
178 // Check if this is one of the launcher actions we handle ourselves
179 if (actionId == "pin_item") {
180 if (item->pinned()) {
181 requestRemove(appId);
182 } else {
183 pin(appId);
184 }
186 // Nope, we don't know this action, let the backend forward it to the application
187 } else {
188 m_backend->triggerQuickListAction(appId, actionId);
189 }
190 }
193void LauncherModel::setUser(const QString &username)
195 m_backend->setUser(username);
198void LauncherModel::storeAppList()
200 QStringList appIds;
201 Q_FOREACH(LauncherItem *item, m_list) {
202 if (item->pinned() || item->recent()) {
203 appIds << item->appId();
204 }
205 }
206 m_backend->setStoredApplications(appIds);
209int LauncherModel::findApplication(const QString &appId)
211 for (int i = 0; i < m_list.count(); ++i) {
212 LauncherItem *item = m_list.at(i);
213 if (item->appId() == appId) {
214 return i;
215 }
216 }
217 return -1;
220void LauncherModel::progressChanged(const QString &appId, int progress)
222 int idx = findApplication(appId);
223 if (idx >= 0) {
224 LauncherItem *item = m_list.at(idx);
225 item->setProgress(progress);
226 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
227 }
231void LauncherModel::countChanged(const QString &appId, int count)
233 int idx = findApplication(appId);
234 if (idx >= 0) {
235 LauncherItem *item = m_list.at(idx);
236 item->setCount(count);
237 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
238 }