on cedbc3f help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
170 lines 4.8 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 // Perform the move in our list
86 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
87 m_list.move(oldIndex, newIndex);
88 endMoveRows();
90 storeAppList();
92 pin(m_list.at(newIndex)->appId());
95void LauncherModel::pin(const QString &appId, int index)
97 int currentIndex = findApplication(appId);
99 if (currentIndex >= 0) {
100 if (index == -1 || index == currentIndex) {
101 m_list.at(currentIndex)->setPinned(true);
102 QModelIndex modelIndex = this->index(currentIndex);
103 Q_EMIT dataChanged(modelIndex, modelIndex);
104 } else {
105 move(currentIndex, index);
106 }
107 } else {
108 if (index == -1) {
109 index = m_list.count();
110 }
111 beginInsertRows(QModelIndex(), index, index);
112 LauncherItem *item = new LauncherItem(appId,
113 m_backend->desktopFile(appId),
114 m_backend->displayName(appId),
115 m_backend->icon(appId));
116 item->setPinned(true);
117 m_list.insert(index, item);
118 endInsertRows();
119 }
122void LauncherModel::requestRemove(const QString &appId)
124 int index = findApplication(appId);
125 if (index < 0) {
126 return;
127 }
129 beginRemoveRows(QModelIndex(), index, index);
130 m_list.takeAt(index)->deleteLater();
131 endRemoveRows();
133 storeAppList();
136void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
138 int index = findApplication(appId);
139 if (index < 0) {
140 return;
141 }
143 QuickListModel *model = qobject_cast<QuickListModel*>(m_list.at(index)->quickList());
144 if (model) {
145 QString actionId = model->get(actionIndex).actionId();
146 m_backend->triggerQuickListAction(appId, actionId);
147 }
150void LauncherModel::storeAppList()
152 QStringList appIds;
153 Q_FOREACH(LauncherItem *item, m_list) {
154 if (item->pinned() || item->recent()) {
155 appIds << item->appId();
156 }
157 }
158 m_backend->setStoredApplications(appIds);
161int LauncherModel::findApplication(const QString &appId)
163 for (int i = 0; i < m_list.count(); ++i) {
164 LauncherItem *item = m_list.at(i);
165 if (item->appId() == appId) {
166 return i;
167 }
168 }
169 return -1;