on cbf87e3 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
142 lines 3.8 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"
24#include <QDebug>
26LauncherModel::LauncherModel(QObject *parent):
27 LauncherModelInterface(parent),
28 m_backend(new LauncherBackend(this))
30 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
31 qDebug() << "adding app" << entry;
32 LauncherItem *item = new LauncherItem(entry,
33 m_backend->desktopFile(entry),
34 m_backend->displayName(entry),
35 m_backend->icon(entry),
36 this);
37 if (m_backend->isPinned(entry)) {
38 item->setFavorite(true);
39 } else {
40 item->setRecent(true);
41 }
42 m_list.append(item);
43 }
46LauncherModel::~LauncherModel()
48 while (!m_list.empty()) {
49 m_list.takeFirst()->deleteLater();
50 }
53int LauncherModel::rowCount(const QModelIndex &parent) const
55 Q_UNUSED(parent)
56 return m_list.count();
59QVariant LauncherModel::data(const QModelIndex &index, int role) const
61 LauncherItem *item = m_list.at(index.row());
62 switch(role) {
63 case RoleDesktopFile:
64 return item->desktopFile();
65 case RoleName:
66 return item->name();
67 case RoleIcon:
68 return item->icon();
69 case RoleFavorite:
70 return item->favorite();
71 }
73 return QVariant();
76unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
78 if (index < 0 || index >= m_list.count()) {
79 return 0;
80 }
81 return m_list.at(index);
84void LauncherModel::move(int oldIndex, int newIndex)
86 // Perform the move in our list
87 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
88 m_list.move(oldIndex, newIndex);
89 endMoveRows();
91 storeAppList();
93 pin(newIndex);
96void LauncherModel::pin(int index)
98 LauncherItem *item = m_list.at(index);
99 if (!item->favorite()) {
101 item->setFavorite(true);
102 item->setRecent(false);
104 m_backend->setPinned(item->appId(), true);
106 QModelIndex modelIndex = this->index(index);
107 Q_EMIT dataChanged(modelIndex, modelIndex);
109 storeAppList();
110 }
113void LauncherModel::remove(int index)
115 beginRemoveRows(QModelIndex(), index, index);
116 m_list.takeAt(index)->deleteLater();
117 endRemoveRows();
119 storeAppList();
122QHash<int, QByteArray> LauncherModel::roleNames() const
124 QHash<int, QByteArray> roles;
125 roles.insert(RoleDesktopFile, "desktopFile");
126 roles.insert(RoleName, "name");
127 roles.insert(RoleIcon, "icon");
128 roles.insert(RoleFavorite, "favorite");
129 roles.insert(RoleRunning, "runnng");
130 return roles;
133void LauncherModel::storeAppList()
135 QStringList appIds;
136 Q_FOREACH(LauncherItem *item, m_list) {
137 if (item->favorite() || item->recent()) {
138 appIds << item->appId();
139 }
140 }
141 m_backend->setStoredApplications(appIds);