void LauncherModel::pin(const QString &appId) { if (!m_includePinnedApps) return; Application *found = Q_NULLPTR; int pinAtIndex = 0; Q_FOREACH (Application *item, m_list) { if (item->isPinned()) pinAtIndex++; if (item->appId() != appId) continue; found = item; break; } if (!found) return; Q_ASSERT(!found->isPinned()); found->setPinned(true); int foundIndex = m_list.indexOf(found); if (foundIndex != pinAtIndex) { moveRows(foundIndex, 1, pinAtIndex); } else { QModelIndex modelIndex = index(foundIndex); Q_EMIT dataChanged(modelIndex, modelIndex); } pinLauncher(appId, true); }
void LauncherModel::unpin(const QString &appId) { if (!m_includePinnedApps) return; Application *found = Q_NULLPTR; Q_FOREACH (Application *item, m_list) { if (!item->isPinned()) break; if (item->appId() != appId) continue; found = item; } if (!found) return; Q_ASSERT(found->isPinned()); int i = m_list.indexOf(found); // Remove the item when unpinned and not running if (found->isRunning()) { found->setPinned(false); moveRows(i, 1, m_list.size() - 1); } else { beginRemoveRows(QModelIndex(), i, i); m_list.takeAt(i)->deleteLater(); endRemoveRows(); } pinLauncher(appId, false); }
Application *LauncherModel::addApplication(const QString &appId, bool pinned) { auto app = new Application(appId, pinned, this); if (pinned && !app->isValid()) { pinLauncher(appId, false); return nullptr; } QObject::connect(app, &Application::launched, [=]() { QModelIndex modelIndex = index(indexFromAppId(appId)); emit dataChanged(modelIndex, modelIndex); QTimer::singleShot(5000, [=]() { if (app->isStarting()) { qDebug() << "Application failed to start!" << appId; auto i = indexFromAppId(appId); if (app->isPinned()) { QModelIndex modelIndex = index(i); app->setState(Application::NotRunning); emit dataChanged(modelIndex, modelIndex); } else { beginRemoveRows(QModelIndex(), i, i); m_list.takeAt(i)->deleteLater(); endRemoveRows(); } } else { qDebug() << "Application is now running" << appId; } }); }); m_list.append(app); return app; }
void Task::fixMenu(QMenu *menu, Task *task) { if (!menu || menu->actions().count() < 3) { return; } const bool group = (task?(task->taskType() == GroupType):false); menu->actions().at(menu->actions().count() - 4)->setVisible(false); if (menu->actions().count() > 7) { menu->actions().at(menu->actions().count() - 5)->setVisible(false); } const QString url = (task?task->launcherUrl().pathOrUrl():QString()); if (!task || (!url.isEmpty() && m_applet->arrangement().contains(url)) || (group && m_applet->groupManager()->groupingStrategy() != TaskManager::GroupManager::ProgramGrouping)) { return; } QMenu *moreMenu = menu->actions().at(qMax(0, (menu->actions().count() - 3)))->menu(); if (!moreMenu) { return; } moreMenu->addSeparator(); QAction *launcherAction = moreMenu->addAction(KIcon("object-locked"), i18n("Pin This Task"), this, SLOT(pinLauncher())); launcherAction->setData(url); }