on cff11ab help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
129 lines 3.4 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, m_backend->displayName(entry), m_backend->icon(entry), this);
33 if (m_backend->isPinned(entry)) {
34 item->setFavorite(true);
35 } else {
36 item->setRecent(true);
37 }
38 m_list.append(item);
39 }
42LauncherModel::~LauncherModel()
44 while (!m_list.empty()) {
45 m_list.takeFirst()->deleteLater();
46 }
49int LauncherModel::rowCount(const QModelIndex &parent) const
51 Q_UNUSED(parent)
52 return m_list.count();
55QVariant LauncherModel::data(const QModelIndex &index, int role) const
57 LauncherItem *item = m_list.at(index.row());
58 switch(role) {
59 case RoleDesktopFile:
60 return item->desktopFile();
61 case RoleName:
62 return item->name();
63 case RoleIcon:
64 return item->icon();
65 case RoleFavorite:
66 return item->favorite();
67 }
69 return QVariant();
72unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
74 if (index < 0 || index >= m_list.count()) {
75 return 0;
76 }
77 return m_list.at(index);
80void LauncherModel::move(int oldIndex, int newIndex)
82 // Perform the move in our list
83 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
84 m_list.move(oldIndex, newIndex);
85 endMoveRows();
87 storeAppList();
89 pin(newIndex);
92void LauncherModel::pin(int index)
94 LauncherItem *item = m_list.at(index);
95 if (!item->favorite()) {
97 item->setFavorite(true);
98 item->setRecent(false);
100 m_backend->setPinned(item->desktopFile(), true);
102 QModelIndex modelIndex = this->index(index);
103 Q_EMIT dataChanged(modelIndex, modelIndex);
105 storeAppList();
106 }
109QHash<int, QByteArray> LauncherModel::roleNames() const
111 QHash<int, QByteArray> roles;
112 roles.insert(RoleDesktopFile, "desktopFile");
113 roles.insert(RoleName, "name");
114 roles.insert(RoleIcon, "icon");
115 roles.insert(RoleFavorite, "favorite");
116 roles.insert(RoleRunning, "runnng");
117 return roles;
120void LauncherModel::storeAppList()
122 QStringList appIds;
123 Q_FOREACH(LauncherItem *item, m_list) {
124 if (item->favorite() || item->recent()) {
125 appIds << item->desktopFile();
126 }
127 }
128 m_backend->setStoredApplications(appIds);