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 Q_FOREACH (
const QString &entry, m_settings->storedApplications()) {
45 if (!desktopFile.isValid()) {
46 qWarning() <<
"Couldn't find a .desktop file for" << entry <<
". Skipping...";
50 LauncherItem *item =
new LauncherItem(entry,
51 desktopFile.displayName(),
54 item->setPinned(
true);
59 LauncherModel::~LauncherModel()
61 while (!m_list.empty()) {
62 m_list.takeFirst()->deleteLater();
66 int LauncherModel::rowCount(
const QModelIndex &parent)
const
69 return m_list.count();
72 QVariant LauncherModel::data(const QModelIndex &index,
int role)
const
74 LauncherItem *item = m_list.at(index.row());
83 return item->pinned();
86 case RoleCountVisible:
87 return item->countVisible();
89 return item->progress();
91 return item->focused();
97 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const
99 if (index < 0 || index >= m_list.count()) {
102 return m_list.at(index);
105 void LauncherModel::move(
int oldIndex,
int newIndex)
111 if (newIndex >= m_list.count()) {
112 newIndex = m_list.count()-1;
116 if (oldIndex == newIndex) {
123 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
125 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
126 m_list.move(oldIndex, newIndex);
129 if (!m_list.at(newIndex)->pinned()) {
130 pin(m_list.at(newIndex)->appId());
136 void LauncherModel::pin(
const QString &appId,
int index)
138 int currentIndex = findApplication(appId);
140 if (currentIndex >= 0) {
141 if (index == -1 || index == currentIndex) {
142 m_list.at(currentIndex)->setPinned(
true);
143 QModelIndex modelIndex = this->index(currentIndex);
144 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
146 move(currentIndex, index);
152 index = m_list.count();
156 if (!desktopFile.isValid()) {
157 qWarning() <<
"Can't pin this application, there is no .destkop file available.";
161 beginInsertRows(QModelIndex(), index, index);
162 LauncherItem *item =
new LauncherItem(appId,
163 desktopFile.displayName(),
166 item->setPinned(
true);
167 m_list.insert(index, item);
174 void LauncherModel::requestRemove(
const QString &appId)
176 int index = findApplication(appId);
181 if (m_appManager->findApplication(appId)) {
182 m_list.at(index)->setPinned(
false);
183 QModelIndex modelIndex = this->index(index);
184 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
188 beginRemoveRows(QModelIndex(), index, index);
189 m_list.takeAt(index)->deleteLater();
195 void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
197 int index = findApplication(appId);
202 LauncherItem *item = m_list.at(index);
203 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
205 QString actionId = model->get(actionIndex).actionId();
208 if (actionId ==
"pin_item") {
209 if (item->pinned()) {
210 requestRemove(appId);
214 }
else if (actionId ==
"launch_item") {
215 QDesktopServices::openUrl(getUrlForAppId(appId));
224 void LauncherModel::setUser(
const QString &username)
227 qWarning() << "This backend doesn't support multiple users";
230 QString LauncherModel::getUrlForAppId(const QString &appId)
const
233 if (appId.isEmpty()) {
237 if (!appId.contains(
"_")) {
238 return "application:///" + appId +
".desktop";
241 QStringList parts = appId.split(
'_');
242 QString
package = parts.value(0);
243 QString app = parts.value(1,
"first-listed-app");
244 return "appid://" +
package + "/" + app + "/current-user-version";
247 ApplicationManagerInterface *LauncherModel::applicationManager()
const
252 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 disconnect(
this, SLOT(applicationAdded(QModelIndex,
int)));
258 disconnect(
this, SLOT(applicationRemoved(QModelIndex,
int)));
259 disconnect(
this, SLOT(focusedAppIdChanged()));
262 QList<int> recentAppIndices;
263 for (
int i = 0; i < m_list.count(); ++i) {
264 if (m_list.at(i)->recent()) {
265 recentAppIndices << i;
269 while (recentAppIndices.count() > 0) {
270 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
271 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
273 recentAppIndices.takeFirst();
278 m_appManager = appManager;
279 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex,
int,
int)), SLOT(applicationAdded(QModelIndex,
int)));
280 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)), SLOT(applicationRemoved(QModelIndex,
int)));
281 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
283 Q_EMIT applicationManagerChanged();
285 for (
int i = 0; i < appManager->count(); ++i) {
286 applicationAdded(QModelIndex(), i);
291 void LauncherModel::storeAppList()
294 Q_FOREACH(LauncherItem *item, m_list) {
295 if (item->pinned()) {
296 appIds << item->appId();
299 m_settings->setStoredApplications(appIds);
302 int LauncherModel::findApplication(
const QString &appId)
304 for (
int i = 0; i < m_list.count(); ++i) {
305 LauncherItem *item = m_list.at(i);
306 if (item->appId() == appId) {
313 void LauncherModel::progressChanged(
const QString &appId,
int progress)
315 int idx = findApplication(appId);
317 LauncherItem *item = m_list.at(idx);
318 item->setProgress(progress);
319 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
324 void LauncherModel::countChanged(
const QString &appId,
int count)
326 int idx = findApplication(appId);
328 LauncherItem *item = m_list.at(idx);
329 item->setCount(count);
330 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
334 void LauncherModel::countVisibleChanged(
const QString &appId,
int countVisible)
336 int idx = findApplication(appId);
338 LauncherItem *item = m_list.at(idx);
339 item->setCountVisible(countVisible);
340 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
343 if (!countVisible && !item->pinned() && !item->recent()) {
344 beginRemoveRows(QModelIndex(), idx, idx);
345 m_list.takeAt(idx)->deleteLater();
351 if (countVisible && desktopFile.isValid()) {
352 LauncherItem *item =
new LauncherItem(appId,
353 desktopFile.displayName(),
355 item->setCountVisible(
true);
356 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
363 void LauncherModel::refresh()
365 QList<LauncherItem*> toBeRemoved;
366 Q_FOREACH (LauncherItem* item, m_list) {
368 if (!desktopFile.isValid()) {
371 int idx = m_list.indexOf(item);
372 item->setName(desktopFile.displayName());
373 item->setIcon(desktopFile.icon());
374 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
378 Q_FOREACH (LauncherItem* item, toBeRemoved) {
379 requestRemove(item->appId());
383 void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
387 ApplicationInfoInterface *app = m_appManager->get(row);
389 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
393 if (app->appId() ==
"unity8-dash") {
399 Q_FOREACH(LauncherItem *item, m_list) {
400 if (app->appId() == item->appId()) {
408 LauncherItem *item =
new LauncherItem(app->appId(), app->name(), app->icon().toString(),
this);
409 item->setRecent(
true);
410 item->setFocused(app->focused());
412 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
418 void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
423 for (
int i = 0; i < m_list.count(); ++i) {
424 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
430 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
431 beginRemoveRows(QModelIndex(), appIndex, appIndex);
432 m_list.takeAt(appIndex)->deleteLater();
437 void LauncherModel::focusedAppIdChanged()
439 QString appId = m_appManager->focusedApplicationId();
440 for (
int i = 0; i < m_list.count(); ++i) {
441 LauncherItem *item = m_list.at(i);
442 if (!item->focused() && item->appId() == appId) {
443 item->setFocused(
true);
444 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
445 }
else if (item->focused() && item->appId() != appId) {
446 item->setFocused(
false);
447 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);