20 #include "launchermodel.h"
21 #include "launcheritem.h"
22 #include "gsettings.h"
23 #include "desktopfilehandler.h"
24 #include "dbusinterface.h"
26 #include <unity/shell/application/ApplicationInfoInterface.h>
28 #include <QDesktopServices>
33 LauncherModel::LauncherModel(QObject *parent):
34 LauncherModelInterface(parent),
35 m_settings(new GSettings(this)),
36 m_dbusIface(new DBusInterface(this)),
39 connect(m_dbusIface, &DBusInterface::countChanged,
this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged,
this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::refreshCalled,
this, &LauncherModel::refresh);
43 connect(m_settings, &GSettings::changed,
this, &LauncherModel::refresh);
48 LauncherModel::~LauncherModel()
50 while (!m_list.empty()) {
51 m_list.takeFirst()->deleteLater();
55 int LauncherModel::rowCount(
const QModelIndex &parent)
const
58 return m_list.count();
61 QVariant LauncherModel::data(const QModelIndex &index,
int role)
const
63 LauncherItem *item = m_list.at(index.row());
72 return item->pinned();
75 case RoleCountVisible:
76 return item->countVisible();
78 return item->progress();
80 return item->focused();
86 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const
88 if (index < 0 || index >= m_list.count()) {
91 return m_list.at(index);
94 void LauncherModel::move(
int oldIndex,
int newIndex)
100 if (newIndex >= m_list.count()) {
101 newIndex = m_list.count()-1;
105 if (oldIndex == newIndex) {
112 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
114 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
115 m_list.move(oldIndex, newIndex);
118 if (!m_list.at(newIndex)->pinned()) {
119 pin(m_list.at(newIndex)->appId());
125 void LauncherModel::pin(
const QString &appId,
int index)
127 int currentIndex = findApplication(appId);
129 if (currentIndex >= 0) {
130 if (index == -1 || index == currentIndex) {
131 m_list.at(currentIndex)->setPinned(
true);
132 QModelIndex modelIndex = this->index(currentIndex);
133 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
135 move(currentIndex, index);
141 index = m_list.count();
145 if (!desktopFile.isValid()) {
146 qWarning() <<
"Can't pin this application, there is no .destkop file available.";
150 beginInsertRows(QModelIndex(), index, index);
151 LauncherItem *item =
new LauncherItem(appId,
152 desktopFile.displayName(),
155 item->setPinned(
true);
156 m_list.insert(index, item);
163 void LauncherModel::requestRemove(
const QString &appId)
169 void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
171 int index = findApplication(appId);
176 LauncherItem *item = m_list.at(index);
177 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
179 QString actionId = model->get(actionIndex).actionId();
182 if (actionId ==
"pin_item") {
183 if (item->pinned()) {
184 requestRemove(appId);
188 }
else if (actionId ==
"launch_item") {
189 QDesktopServices::openUrl(getUrlForAppId(appId));
198 void LauncherModel::setUser(
const QString &username)
201 qWarning() << "This backend doesn't support multiple users";
204 QString LauncherModel::getUrlForAppId(const QString &appId)
const
207 if (appId.isEmpty()) {
211 if (!appId.contains(
"_")) {
212 return "application:///" + appId +
".desktop";
215 QStringList parts = appId.split(
'_');
216 QString
package = parts.value(0);
217 QString app = parts.value(1,
"first-listed-app");
218 return "appid://" +
package + "/" + app + "/current-user-version";
221 ApplicationManagerInterface *LauncherModel::applicationManager()
const
226 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
231 disconnect(
this, SLOT(applicationAdded(QModelIndex,
int)));
232 disconnect(
this, SLOT(applicationRemoved(QModelIndex,
int)));
233 disconnect(
this, SLOT(focusedAppIdChanged()));
236 QList<int> recentAppIndices;
237 for (
int i = 0; i < m_list.count(); ++i) {
238 if (m_list.at(i)->recent()) {
239 recentAppIndices << i;
243 while (recentAppIndices.count() > 0) {
244 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
245 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
247 recentAppIndices.takeFirst();
252 m_appManager = appManager;
253 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex,
int,
int)), SLOT(applicationAdded(QModelIndex,
int)));
254 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)), SLOT(applicationRemoved(QModelIndex,
int)));
255 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
257 Q_EMIT applicationManagerChanged();
259 for (
int i = 0; i < appManager->count(); ++i) {
260 applicationAdded(QModelIndex(), i);
265 void LauncherModel::storeAppList()
268 Q_FOREACH(LauncherItem *item, m_list) {
269 if (item->pinned()) {
270 appIds << item->appId();
273 m_settings->setStoredApplications(appIds);
276 void LauncherModel::unpin(
const QString &appId)
278 int index = findApplication(appId);
283 if (m_appManager->findApplication(appId)) {
284 if (m_list.at(index)->pinned()) {
285 m_list.at(index)->setPinned(
false);
286 QModelIndex modelIndex = this->index(index);
287 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
290 beginRemoveRows(QModelIndex(), index, index);
291 m_list.takeAt(index)->deleteLater();
296 int LauncherModel::findApplication(
const QString &appId)
298 for (
int i = 0; i < m_list.count(); ++i) {
299 LauncherItem *item = m_list.at(i);
300 if (item->appId() == appId) {
307 void LauncherModel::progressChanged(
const QString &appId,
int progress)
309 int idx = findApplication(appId);
311 LauncherItem *item = m_list.at(idx);
312 item->setProgress(progress);
313 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
317 void LauncherModel::countChanged(
const QString &appId,
int count)
319 int idx = findApplication(appId);
321 LauncherItem *item = m_list.at(idx);
322 item->setCount(count);
323 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
327 void LauncherModel::countVisibleChanged(
const QString &appId,
int countVisible)
329 int idx = findApplication(appId);
331 LauncherItem *item = m_list.at(idx);
332 item->setCountVisible(countVisible);
333 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
336 if (!countVisible && !item->pinned() && !item->recent()) {
337 beginRemoveRows(QModelIndex(), idx, idx);
338 m_list.takeAt(idx)->deleteLater();
344 if (countVisible && desktopFile.isValid()) {
345 LauncherItem *item =
new LauncherItem(appId,
346 desktopFile.displayName(),
348 item->setCountVisible(
true);
349 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
357 void LauncherModel::refresh()
360 QList<LauncherItem*> toBeRemoved;
361 Q_FOREACH (LauncherItem* item, m_list) {
363 if (!desktopFile.isValid()) {
366 }
else if (!m_settings->storedApplications().contains(item->appId())) {
370 int idx = m_list.indexOf(item);
371 item->setName(desktopFile.displayName());
372 item->setIcon(desktopFile.icon());
373 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
377 Q_FOREACH (LauncherItem* item, toBeRemoved) {
378 unpin(item->appId());
381 bool changed = toBeRemoved.count() > 0;
390 for (
int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
391 QString entry = m_settings->storedApplications().at(settingsIndex);
393 for (
int i = 0; i < m_list.count(); ++i) {
394 if (m_list.at(i)->appId() == entry) {
400 if (itemIndex == -1) {
404 if (!desktopFile.isValid()) {
405 qWarning() <<
"Couldn't find a .desktop file for" << entry <<
". Skipping...";
409 LauncherItem *item =
new LauncherItem(entry,
410 desktopFile.displayName(),
413 item->setPinned(
true);
414 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
415 m_list.insert(addedIndex, item);
418 }
else if (itemIndex != addedIndex) {
421 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
422 m_list.move(itemIndex, addedIndex);
437 void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
441 ApplicationInfoInterface *app = m_appManager->get(row);
443 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
447 if (app->appId() ==
"unity8-dash") {
453 Q_FOREACH(LauncherItem *item, m_list) {
454 if (app->appId() == item->appId()) {
462 LauncherItem *item =
new LauncherItem(app->appId(), app->name(), app->icon().toString(),
this);
463 item->setRecent(
true);
464 item->setFocused(app->focused());
466 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
472 void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
477 for (
int i = 0; i < m_list.count(); ++i) {
478 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
484 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
485 beginRemoveRows(QModelIndex(), appIndex, appIndex);
486 m_list.takeAt(appIndex)->deleteLater();
491 void LauncherModel::focusedAppIdChanged()
493 QString appId = m_appManager->focusedApplicationId();
494 for (
int i = 0; i < m_list.count(); ++i) {
495 LauncherItem *item = m_list.at(i);
496 if (!item->focused() && item->appId() == appId) {
497 item->setFocused(
true);
498 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
499 }
else if (item->focused() && item->appId() != appId) {
500 item->setFocused(
false);
501 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);