@@ -21,16 +21,22 @@
21#include "launcheritem.h"
21#include "launcheritem.h"
22#include "backend/launcherbackend.h"
22#include "backend/launcherbackend.h"
24+#include <unity/shell/application/ApplicationInfoInterface.h>
28+using namespace unity::shell::application;
24LauncherModel::LauncherModel(QObject *parent):
30LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
31 LauncherModelInterface(parent),
26− m_backend(new LauncherBackend(true, this))
32+ m_backend(new LauncherBackend(true, this)),
28 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
35 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
29 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
36 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
31 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
38 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
32 LauncherItem *item = new LauncherItem(entry,
39 LauncherItem *item = new LauncherItem(entry,
33− m_backend->desktopFile(entry),
34 m_backend->displayName(entry),
40 m_backend->displayName(entry),
35 m_backend->icon(entry),
41 m_backend->icon(entry),
@@ -61,9 +67,7 @@ QVariant LauncherModel::data(const QModelIndex &index, int role) const
61 LauncherItem *item = m_list.at(index.row());
67 LauncherItem *item = m_list.at(index.row());
62 switch(role) {
68 switch(role) {
63 case RoleAppId:
69 case RoleAppId:
64− return item->desktopFile();
70+ return item->appId();
65− case RoleDesktopFile:
66− return item->desktopFile();
67 case RoleName:
71 case RoleName:
68 return item->name();
72 return item->name();
69 case RoleIcon:
73 case RoleIcon:
@@ -137,7 +141,6 @@ void LauncherModel::pin(const QString &appId, int index)
138 beginInsertRows(QModelIndex(), index, index);
142 beginInsertRows(QModelIndex(), index, index);
139 LauncherItem *item = new LauncherItem(appId,
143 LauncherItem *item = new LauncherItem(appId,
140− m_backend->desktopFile(appId),
141 m_backend->displayName(appId),
144 m_backend->displayName(appId),
142 m_backend->icon(appId));
145 m_backend->icon(appId));
143 item->setPinned(true);
146 item->setPinned(true);
@@ -193,45 +196,23 @@ void LauncherModel::setUser(const QString &username)
193 m_backend->setUser(username);
196 m_backend->setUser(username);
196−void LauncherModel::applicationFocused(const QString &appId)
199+ApplicationManagerInterface *LauncherModel::applicationManager() const
199− for(int i = 0; i < m_list.count(); ++i) {
200− LauncherItem *item = m_list.at(i);
201− if (item->focused()) {
202− item->setFocused(false);
203− Q_EMIT dataChanged(this->index(i), this->index(i), QVector<int>() << RoleFocused);
207− if (appId.isEmpty()) {
208− return;
201+ return m_appManager;
211− // FIXME: drop this once we get real appIds from the AppManager
204+void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
212− QString helper = appId.split('/').last();
205+{
213− if (helper.endsWith(".desktop")) {
206+ qDebug() << "setting appmanager" << appManager;
217− int index = findApplication(helper);
208+ // Is there already another appmanager set?
218− if (index >= 0) {
209+ if (m_appManager) {
219− m_list.at(index)->setFocused(true);
210+ // Disconnect any signals
220− Q_EMIT dataChanged(this->index(index), this->index(index), QVector<int>() << RoleFocused);
211+ disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
221− } else {
212+ disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
222− // Add app to recent apps
213+ disconnect(this, SLOT(focusedAppIdChanged()));
223− QString desktopFile = m_backend->desktopFile(helper);
224− QString appName = m_backend->displayName(helper);
225− QString icon = m_backend->icon(helper);
227− LauncherItem *item = new LauncherItem(helper, desktopFile, appName, icon);
215+ // remove any recent/running apps from the launcher
228− item->setRecent(true);
229− item->setFocused(true);
230− beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
231− m_list.append(item);
234− // Clean up old recent apps
235 QList<int> recentAppIndices;
216 QList<int> recentAppIndices;
236 for (int i = 0; i < m_list.count(); ++i) {
217 for (int i = 0; i < m_list.count(); ++i) {
237 if (m_list.at(i)->recent()) {
218 if (m_list.at(i)->recent()) {
@@ -239,22 +220,33 @@ void LauncherModel::applicationFocused(const QString &appId)
241 int run = 0;
222 int run = 0;
242− while (recentAppIndices.count() > 5) {
223+ while (recentAppIndices.count() > 0) {
243 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
224 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
244 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
225 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
245 endRemoveRows();
226 endRemoveRows();
246 recentAppIndices.takeFirst();
227 recentAppIndices.takeFirst();
249− storeAppList();
230+ }
232+ m_appManager = appManager;
233+ connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
234+ connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
235+ connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
237+ Q_EMIT applicationManagerChanged();
239+ for (int i = 0; i < appManager->count(); ++i) {
240+ applicationAdded(QModelIndex(), i);
253void LauncherModel::storeAppList()
245void LauncherModel::storeAppList()
255 QStringList appIds;
247 QStringList appIds;
256 Q_FOREACH(LauncherItem *item, m_list) {
248 Q_FOREACH(LauncherItem *item, m_list) {
257− if (item->pinned() || item->recent()) {
249+ if (item->pinned()) {
258 appIds << item->appId();
250 appIds << item->appId();
@@ -292,3 +284,64 @@ void LauncherModel::countChanged(const QString &appId, int count)
292 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
284 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
288+void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
292+ ApplicationInfoInterface *app = m_appManager->get(row);
295+ Q_FOREACH(LauncherItem *item, m_list) {
296+ if (app->appId() == item->appId()) {
302+ // Shall we paint some running/recent app highlight? If yes, do it here.
304+ qDebug() << "Adding app icon" << app->icon();
305+ LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
306+ item->setRecent(true);
307+ item->setFocused(app->focused());
309+ beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
310+ m_list.append(item);
315+void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
320+ for (int i = 0; i < m_list.count(); ++i) {
321+ if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
327+ if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
328+ beginRemoveRows(QModelIndex(), appIndex, appIndex);
329+ m_list.takeAt(appIndex)->deleteLater();
334+void LauncherModel::focusedAppIdChanged()
336+ QString appId = m_appManager->focusedApplicationId();
337+ for (int i = 0; i < m_list.count(); ++i) {
338+ LauncherItem *item = m_list.at(i);
339+ if (!item->focused() && item->appId() == appId) {
340+ item->setFocused(true);
341+ Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
342+ } else if (item->focused() && item->appId() != appId) {
343+ item->setFocused(false);
344+ Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);