on ccaa671 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
149 lines 4.1 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michael Zanetti <michael.zanetti@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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 RoleDesktopFile:
61 return item->desktopFile();
62 case RoleName:
63 return item->name();
64 case RoleIcon:
65 return item->icon();
66 case RolePinned:
67 return item->pinned();
68 }
70 return QVariant();
73unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
75 if (index < 0 || index >= m_list.count()) {
76 return 0;
77 }
78 return m_list.at(index);
81void LauncherModel::move(int oldIndex, int newIndex)
83 // Perform the move in our list
84 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
85 m_list.move(oldIndex, newIndex);
86 endMoveRows();
88 storeAppList();
90 pin(newIndex);
93void LauncherModel::pin(int index)
95 LauncherItem *item = m_list.at(index);
96 if (!item->pinned()) {
98 item->setPinned(true);
99 item->setRecent(false);
101 m_backend->setPinned(item->appId(), true);
103 QModelIndex modelIndex = this->index(index);
104 Q_EMIT dataChanged(modelIndex, modelIndex);
106 storeAppList();
107 }
110void LauncherModel::remove(int index)
112 beginRemoveRows(QModelIndex(), index, index);
113 m_list.takeAt(index)->deleteLater();
114 endRemoveRows();
116 storeAppList();
119void LauncherModel::triggerQuickListAction(int itemIndex, int quickListIndex)
121 QString appId = m_list.at(itemIndex)->appId();
122 QuickListModel *model = qobject_cast<QuickListModel*>(m_list.at(itemIndex)->quickList());
123 if (model) {
124 QString actionId = model->get(quickListIndex).actionId();
125 m_backend->triggerQuickListAction(appId, actionId);
126 }
129QHash<int, QByteArray> LauncherModel::roleNames() const
131 QHash<int, QByteArray> roles;
132 roles.insert(RoleDesktopFile, "desktopFile");
133 roles.insert(RoleName, "name");
134 roles.insert(RoleIcon, "icon");
135 roles.insert(RolePinned, "pinned");
136 roles.insert(RoleRunning, "runnng");
137 return roles;
140void LauncherModel::storeAppList()
142 QStringList appIds;
143 Q_FOREACH(LauncherItem *item, m_list) {
144 if (item->pinned() || item->recent()) {
145 appIds << item->appId();
146 }
147 }
148 m_backend->setStoredApplications(appIds);