@@ -21,16 +21,20 @@
2121#include "launcheritem.h"
2222#include "backend/launcherbackend.h"
2323
24+#include <unity/shell/application/ApplicationInfoInterface.h>
25+
26+using namespace unity::shell::application;
27+
2428LauncherModel::LauncherModel(QObject *parent):
2529 LauncherModelInterface(parent),
26− m_backend(new LauncherBackend(true, this))
30+ m_backend(new LauncherBackend(true, this)),
31+ m_appManager(0)
2732{
2833 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
2934 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
3035
3136 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
3237 LauncherItem *item = new LauncherItem(entry,
33− m_backend->desktopFile(entry),
3438 m_backend->displayName(entry),
3539 m_backend->icon(entry),
3640 this);
@@ -61,9 +65,7 @@ QVariant LauncherModel::data(const QModelIndex &index, int role) const
6165 LauncherItem *item = m_list.at(index.row());
6266 switch(role) {
6367 case RoleAppId:
64− return item->desktopFile();
65− case RoleDesktopFile:
66− return item->desktopFile();
68+ return item->appId();
6769 case RoleName:
6870 return item->name();
6971 case RoleIcon:
@@ -137,7 +139,6 @@ void LauncherModel::pin(const QString &appId, int index)
137139 }
138140 beginInsertRows(QModelIndex(), index, index);
139141 LauncherItem *item = new LauncherItem(appId,
140− m_backend->desktopFile(appId),
141142 m_backend->displayName(appId),
142143 m_backend->icon(appId));
143144 item->setPinned(true);
@@ -193,45 +194,21 @@ void LauncherModel::setUser(const QString &username)
193194 m_backend->setUser(username);
194195}
195196
196−void LauncherModel::applicationFocused(const QString &appId)
197+ApplicationManagerInterface *LauncherModel::applicationManager() const
197198{
198− // Unfocus all apps
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);
204− }
205− }
206−
207− if (appId.isEmpty()) {
208− return;
209− }
210−
211− // FIXME: drop this once we get real appIds from the AppManager
212− QString helper = appId.split('/').last();
213− if (helper.endsWith(".desktop")) {
214− helper.chop(8);
215− }
216−
217− int index = findApplication(helper);
218− if (index >= 0) {
219− m_list.at(index)->setFocused(true);
220− Q_EMIT dataChanged(this->index(index), this->index(index), QVector<int>() << RoleFocused);
221− } else {
222− // Add app to recent apps
223− QString desktopFile = m_backend->desktopFile(helper);
224− QString appName = m_backend->displayName(helper);
225− QString icon = m_backend->icon(helper);
226−
227− LauncherItem *item = new LauncherItem(helper, desktopFile, appName, icon);
228− item->setRecent(true);
229− item->setFocused(true);
230− beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
231− m_list.append(item);
232− endInsertRows();
199+ return m_appManager;
200+}
233201
234− // Clean up old recent apps
202+void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
203+{
204+ // Is there already another appmanager set?
205+ if (m_appManager) {
206+ // Disconnect any signals
207+ disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
208+ disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
209+ disconnect(this, SLOT(focusedAppIdChanged()));
210+
211+ // remove any recent/running apps from the launcher
235212 QList<int> recentAppIndices;
236213 for (int i = 0; i < m_list.count(); ++i) {
237214 if (m_list.at(i)->recent()) {
@@ -239,22 +216,33 @@ void LauncherModel::applicationFocused(const QString &appId)
239216 }
240217 }
241218 int run = 0;
242− while (recentAppIndices.count() > 5) {
219+ while (recentAppIndices.count() > 0) {
243220 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
244221 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
245222 endRemoveRows();
246223 recentAppIndices.takeFirst();
247224 ++run;
248225 }
249− storeAppList();
226+ }
227+
228+ m_appManager = appManager;
229+ connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
230+ connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
231+ connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
232+
233+ Q_EMIT applicationManagerChanged();
234+
235+ for (int i = 0; i < appManager->count(); ++i) {
236+ applicationAdded(QModelIndex(), i);
250237 }
251238}
252239
240+
253241void LauncherModel::storeAppList()
254242{
255243 QStringList appIds;
256244 Q_FOREACH(LauncherItem *item, m_list) {
257− if (item->pinned() || item->recent()) {
245+ if (item->pinned()) {
258246 appIds << item->appId();
259247 }
260248 }
@@ -292,3 +280,63 @@ void LauncherModel::countChanged(const QString &appId, int count)
292280 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
293281 }
294282}
283+
284+void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
285+{
286+ Q_UNUSED(parent);
287+
288+ ApplicationInfoInterface *app = m_appManager->get(row);
289+
290+ bool found = false;
291+ Q_FOREACH(LauncherItem *item, m_list) {
292+ if (app->appId() == item->appId()) {
293+ found = true;
294+ break;
295+ }
296+ }
297+ if (found) {
298+ // Shall we paint some running/recent app highlight? If yes, do it here.
299+ } else {
300+ LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
301+ item->setRecent(true);
302+ item->setFocused(app->focused());
303+
304+ beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
305+ m_list.append(item);
306+ endInsertRows();
307+ }
308+}
309+
310+void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
311+{
312+ Q_UNUSED(parent)
313+
314+ int appIndex = -1;
315+ for (int i = 0; i < m_list.count(); ++i) {
316+ if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
317+ appIndex = i;
318+ break;
319+ }
320+ }
321+
322+ if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
323+ beginRemoveRows(QModelIndex(), appIndex, appIndex);
324+ m_list.takeAt(appIndex)->deleteLater();
325+ endRemoveRows();
326+ }
327+}
328+
329+void LauncherModel::focusedAppIdChanged()
330+{
331+ QString appId = m_appManager->focusedApplicationId();
332+ for (int i = 0; i < m_list.count(); ++i) {
333+ LauncherItem *item = m_list.at(i);
334+ if (!item->focused() && item->appId() == appId) {
335+ item->setFocused(true);
336+ Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
337+ } else if (item->focused() && item->appId() != appId) {
338+ item->setFocused(false);
339+ Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
340+ }
341+ }
342+}