on 2fd0bb0 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
105 lines 3.0 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"
23#include "backend/applicationlistmodel.h"
25#include <QDebug>
27LauncherModel::LauncherModel(QObject *parent):
28 LauncherModelInterface(parent),
29 m_backend(new LauncherBackend(this))
31 Q_FOREACH (const QString &entry, m_backend->favoriteApplications()) {
32 m_list.append(new LauncherItem(entry, m_backend->displayName(entry), m_backend->icon(entry), this));
33 }
36LauncherModel::~LauncherModel()
38 while (!m_list.empty()) {
39 m_list.takeFirst()->deleteLater();
40 }
43int LauncherModel::rowCount(const QModelIndex &parent) const
45 Q_UNUSED(parent)
46 return m_list.count();
49QVariant LauncherModel::data(const QModelIndex &index, int role) const
51 LauncherItem *item = m_list.at(index.row());
52 switch(role) {
53 case RoleDesktopFile:
54 return item->desktopFile();
55 case RoleName:
56 return item->name();
57 case RoleIcon:
58 return item->icon();
59 case RoleFavorite:
60 return item->favorite();
61 }
63 return QVariant();
66unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
68 if (index < 0 || index >= m_list.count()) {
69 return 0;
70 }
71 return m_list.at(index);
74void LauncherModel::move(int oldIndex, int newIndex)
76 // Perform the move in our list
77 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
78 m_list.move(oldIndex, newIndex);
79 endMoveRows();
81 // Mark moved app as pinned
82 LauncherItem *movedItem = m_list.at(newIndex);
83 if (!m_backend->isPinned(movedItem->desktopFile()) {
84 m_backend->setPinned(m_list.at(newIndex)->desktopFile(), true);
85 emit dataChanged(QModelIndex(), newIndex, newIndex);
86 }
88 // Store new order
89 QStringList appIds;
90 Q_FOREACH(LauncherItem *item, m_list) {
91 appIds << item->desktopFile();
92 }
93 m_backend->setFavoriteApplications(appIds);
96QHash<int, QByteArray> LauncherModel::roleNames() const
98 QHash<int, QByteArray> roles;
99 roles.insert(RoleDesktopFile, "desktopFile");
100 roles.insert(RoleName, "name");
101 roles.insert(RoleIcon, "icon");
102 roles.insert(RoleFavorite, "favorite");
103 roles.insert(RoleRunning, "runnng");
104 return roles;