2014-10-28 12:27:54 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2,
|
|
|
|
|
* or (at your option) any later version, as published by the Free
|
|
|
|
|
* Software Foundation
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Self
|
|
|
|
|
#include "applicationlistmodel.h"
|
|
|
|
|
|
|
|
|
|
// Qt
|
|
|
|
|
#include <QByteArray>
|
|
|
|
|
#include <QModelIndex>
|
2014-11-05 21:02:40 +00:00
|
|
|
#include <QProcess>
|
2014-10-28 12:27:54 +00:00
|
|
|
|
|
|
|
|
// KDE
|
|
|
|
|
#include <KPluginInfo>
|
|
|
|
|
#include <KService>
|
|
|
|
|
#include <KServiceGroup>
|
|
|
|
|
#include <KServiceTypeTrader>
|
2015-06-16 03:08:12 +00:00
|
|
|
#include <KSharedConfig>
|
2015-03-18 13:57:54 +00:00
|
|
|
#include <KSycoca>
|
2014-10-28 12:27:54 +00:00
|
|
|
#include <KSycocaEntry>
|
2015-06-20 23:53:27 +00:00
|
|
|
#include <KShell>
|
2015-06-18 23:58:11 +00:00
|
|
|
#include <KIOWidgets/KRun>
|
2014-10-28 12:27:54 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
|
|
|
|
|
ApplicationListModel::ApplicationListModel(HomeScreen *parent)
|
2019-09-17 16:14:23 +00:00
|
|
|
: QSortFilterProxyModel(parent),
|
2019-09-04 16:39:31 +00:00
|
|
|
m_homeScreen(parent)
|
2014-10-28 12:27:54 +00:00
|
|
|
{
|
2019-09-16 17:13:52 +00:00
|
|
|
loadSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ApplicationListModel::~ApplicationListModel()
|
|
|
|
|
= default;
|
|
|
|
|
|
|
|
|
|
void ApplicationListModel::loadSettings()
|
|
|
|
|
{
|
2019-09-04 16:39:31 +00:00
|
|
|
m_favorites = m_homeScreen->config().readEntry("Favorites", QStringList());
|
|
|
|
|
m_desktopItems = m_homeScreen->config().readEntry("DesktopItems", QStringList()).toSet();
|
|
|
|
|
m_appOrder = m_homeScreen->config().readEntry("AppOrder", QStringList());
|
|
|
|
|
m_maxFavoriteCount = m_homeScreen->config().readEntry("MaxFavoriteCount", 5);
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (auto app : m_appOrder) {
|
|
|
|
|
m_appPositions[app] = i;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
2014-10-28 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> ApplicationListModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
QHash<int, QByteArray> roleNames;
|
2019-09-17 16:14:23 +00:00
|
|
|
if (sourceModel()) {
|
|
|
|
|
roleNames = sourceModel()->roleNames();
|
2015-03-18 13:57:54 +00:00
|
|
|
}
|
2019-09-17 16:14:23 +00:00
|
|
|
|
|
|
|
|
roleNames[SortKeyRole] = "SortKeyRole";
|
|
|
|
|
roleNames[ApplicationLocationRole] = "ApplicationLocationRole";
|
2015-03-18 13:57:54 +00:00
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
const_cast<ApplicationListModel *>(this)->m_urlRole = roleNames.key("url");
|
2015-03-18 13:57:54 +00:00
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
return roleNames;
|
2015-03-18 13:34:05 +00:00
|
|
|
}
|
2014-10-28 12:27:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
QVariant ApplicationListModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid()) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (role) {
|
2019-09-17 16:14:23 +00:00
|
|
|
case SortKeyRole: {
|
|
|
|
|
const QString url = QSortFilterProxyModel::data(index, m_urlRole).toString();
|
|
|
|
|
if (m_appOrder.contains(url)) {
|
|
|
|
|
return QString::number(m_appOrder.indexOf(url)) + QStringLiteral("_") + QSortFilterProxyModel::data(index, Qt::DisplayRole).toString();
|
|
|
|
|
} else {
|
|
|
|
|
return QStringLiteral("z_") + QSortFilterProxyModel::data(index, Qt::DisplayRole).toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case ApplicationLocationRole: {
|
|
|
|
|
const QString url = QSortFilterProxyModel::data(index, m_urlRole).toString();
|
|
|
|
|
if (m_favorites.contains(url)) {
|
|
|
|
|
return Favorites;
|
|
|
|
|
} else if (m_desktopItems.contains(url)) {
|
|
|
|
|
return Desktop;
|
|
|
|
|
} else {
|
|
|
|
|
return Grid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-28 12:27:54 +00:00
|
|
|
|
|
|
|
|
default:
|
2019-09-17 16:14:23 +00:00
|
|
|
return QSortFilterProxyModel::data(index, role);
|
2014-10-28 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
//TODO: remove?
|
2015-04-21 12:14:33 +00:00
|
|
|
Qt::ItemFlags ApplicationListModel::flags(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
2018-12-31 03:13:55 +00:00
|
|
|
return nullptr;
|
2019-09-17 16:14:23 +00:00
|
|
|
return Qt::ItemIsDragEnabled|QSortFilterProxyModel::flags(index);
|
2015-04-21 12:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
void ApplicationListModel::setLocation(int row, LauncherLocation location)
|
|
|
|
|
{
|
2019-09-17 16:14:23 +00:00
|
|
|
if (row < 0 || row >= rowCount()) {
|
2019-09-04 16:39:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
const QString url = data(index(row, 0), m_urlRole).toString();
|
|
|
|
|
|
|
|
|
|
if (url.isEmpty()) {
|
2019-09-04 16:39:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
if (location == Favorites && !m_favorites.contains(url)) {
|
|
|
|
|
qWarning()<<"favoriting"<<row;
|
2019-09-04 16:39:31 +00:00
|
|
|
// Deny favorites when full
|
|
|
|
|
if (row >= m_maxFavoriteCount || m_favorites.count() >= m_maxFavoriteCount) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
m_favorites.insert(row, url);
|
2019-09-04 16:39:31 +00:00
|
|
|
|
|
|
|
|
m_homeScreen->config().writeEntry("Favorites", m_favorites);
|
|
|
|
|
emit favoriteCountChanged();
|
|
|
|
|
|
|
|
|
|
// Out of favorites
|
2019-09-17 16:14:23 +00:00
|
|
|
} else if (m_favorites.contains(url)) {
|
|
|
|
|
m_favorites.removeAll(url);
|
2019-09-04 16:39:31 +00:00
|
|
|
m_homeScreen->config().writeEntry("Favorites", m_favorites);
|
|
|
|
|
emit favoriteCountChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In Desktop
|
2019-09-17 16:14:23 +00:00
|
|
|
if (location == Desktop && m_desktopItems.contains(url)) {
|
|
|
|
|
m_desktopItems.insert(url);
|
2019-09-04 16:39:31 +00:00
|
|
|
m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.toList());
|
|
|
|
|
|
|
|
|
|
// Out of Desktop
|
2019-09-17 16:14:23 +00:00
|
|
|
} else if (m_desktopItems.contains(url)) {
|
|
|
|
|
m_desktopItems.remove(url);
|
2019-09-04 16:39:31 +00:00
|
|
|
m_homeScreen->config().writeEntry("DesktopItems", m_desktopItems.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit m_homeScreen->configNeedsSaving();
|
|
|
|
|
emit dataChanged(index(row, 0), index(row, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ApplicationListModel::moveItem(int row, int destination)
|
2015-03-05 12:37:39 +00:00
|
|
|
{
|
2019-09-17 16:14:23 +00:00
|
|
|
if (row < 0 || destination < 0 || row >= rowCount() ||
|
|
|
|
|
destination >= rowCount() || row == destination) {
|
2015-03-05 12:37:39 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (destination > row) {
|
|
|
|
|
++destination;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
const QString url = data(index(row, 0), m_urlRole).toString();
|
2019-09-04 16:39:31 +00:00
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
if (url.isEmpty()) {
|
|
|
|
|
return;
|
2015-04-10 22:23:49 +00:00
|
|
|
}
|
2015-03-18 13:34:05 +00:00
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
if (m_appOrder.length() < qMax(row, destination)) {
|
|
|
|
|
for (int i = m_appOrder.length(); i <= qMax(row, destination); ++i) {
|
|
|
|
|
m_appOrder << data(index(i, 0), m_urlRole).toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (destination > row) {
|
|
|
|
|
m_appOrder.insert(destination, url);
|
|
|
|
|
m_appOrder.takeAt(row);
|
2015-03-18 13:34:05 +00:00
|
|
|
|
2019-09-17 16:14:23 +00:00
|
|
|
} else {
|
|
|
|
|
m_appOrder.takeAt(row);
|
|
|
|
|
m_appOrder.insert(destination, url);
|
2015-03-18 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
m_homeScreen->config().writeEntry("AppOrder", m_appOrder);
|
2015-03-05 12:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ApplicationListModel::runApplication(const QString &storageId)
|
|
|
|
|
{
|
2014-10-28 12:27:54 +00:00
|
|
|
if (storageId.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KService::Ptr service = KService::serviceByStorageId(storageId);
|
2014-11-05 21:02:40 +00:00
|
|
|
|
2018-12-31 03:13:55 +00:00
|
|
|
KRun::runService(*service, QList<QUrl>(), nullptr);
|
2015-06-18 23:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
int ApplicationListModel::maxFavoriteCount() const
|
2015-03-18 13:34:05 +00:00
|
|
|
{
|
2019-09-04 16:39:31 +00:00
|
|
|
return m_maxFavoriteCount;
|
2015-03-18 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
void ApplicationListModel::setMaxFavoriteCount(int count)
|
2015-03-18 13:34:05 +00:00
|
|
|
{
|
2019-09-04 16:39:31 +00:00
|
|
|
if (m_maxFavoriteCount == count) {
|
2015-03-18 13:34:05 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 16:39:31 +00:00
|
|
|
if (m_maxFavoriteCount > count) {
|
|
|
|
|
while (m_favorites.size() > count && m_favorites.count() > 0) {
|
|
|
|
|
m_favorites.pop_back();
|
|
|
|
|
}
|
|
|
|
|
emit favoriteCountChanged();
|
2019-09-17 16:14:23 +00:00
|
|
|
/*TODO
|
2019-09-04 16:39:31 +00:00
|
|
|
int i = 0;
|
|
|
|
|
for (auto &app : m_applicationList) {
|
|
|
|
|
if (i >= count && app.location == Favorites) {
|
|
|
|
|
app.location = Grid;
|
|
|
|
|
emit dataChanged(index(i, 0), index(i, 0));
|
|
|
|
|
}
|
|
|
|
|
++i;
|
2019-09-17 16:14:23 +00:00
|
|
|
}*/
|
2015-03-18 13:34:05 +00:00
|
|
|
}
|
2019-09-04 16:39:31 +00:00
|
|
|
|
|
|
|
|
m_maxFavoriteCount = count;
|
|
|
|
|
m_homeScreen->config().writeEntry("MaxFavoriteCount", m_maxFavoriteCount);
|
|
|
|
|
|
|
|
|
|
emit maxFavoriteCountChanged();
|
2015-03-18 13:34:05 +00:00
|
|
|
}
|
2019-09-04 16:39:31 +00:00
|
|
|
|
|
|
|
|
#include "moc_applicationlistmodel.cpp"
|
|
|
|
|
|