mirror of https://github.com/cutefishos/settings
feat: add default apps settings
parent
89666b7e98
commit
8a42ab9c53
@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (C) 2021 - 2022 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <kate@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "defaultapplications.h"
|
||||
#include "desktopproperties.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QDirIterator>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
|
||||
#include <KConfig>
|
||||
#include <KConfigGroup>
|
||||
#include <KSharedConfig>
|
||||
|
||||
DefaultApplications::DefaultApplications(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
loadApps();
|
||||
}
|
||||
|
||||
void DefaultApplications::loadApps()
|
||||
{
|
||||
QDirIterator it("/usr/share/applications", { "*.desktop" },
|
||||
QDir::NoFilter, QDirIterator::Subdirectories);
|
||||
|
||||
// Load apps
|
||||
while (it.hasNext()) {
|
||||
const auto fileName = it.next();
|
||||
if (!QFile::exists(fileName))
|
||||
continue;
|
||||
|
||||
DesktopProperties desktop(fileName, "Desktop Entry");
|
||||
QString name = desktop.value(QString("Name[%1]").arg(QLocale::system().name())).toString();
|
||||
|
||||
if (name.isEmpty())
|
||||
name = desktop.value("Name").toString();
|
||||
|
||||
AppItem item;
|
||||
item.path = fileName;
|
||||
item.name = name;
|
||||
item.icon = desktop.value("Icon").toString();
|
||||
item.mimeType = desktop.value("MimeType").toString();
|
||||
item.categories = desktop.value("Categories").toString();
|
||||
item.fileName = QFileInfo(fileName).fileName();
|
||||
|
||||
if (item.categories.contains("FileManager")
|
||||
&& item.mimeType.contains("inode/directory")) {
|
||||
m_fileManagerList.append(item);
|
||||
} else if (item.categories.contains("WebBrowser")
|
||||
&& item.mimeType.contains("x-scheme-handler/http")) {
|
||||
m_browserList.append(item);
|
||||
} else if (item.categories.contains("Email")
|
||||
&& item.mimeType.contains("x-scheme-handler/mailto")) {
|
||||
m_emailList.append(item);
|
||||
} else if (item.categories.contains("TerminalEmulator")) {
|
||||
m_terminalList.append(item);
|
||||
}
|
||||
}
|
||||
|
||||
// Load xdg config.
|
||||
QSettings mimeApps(mimeAppsListFilePath(), QSettings::IniFormat);
|
||||
mimeApps.beginGroup("Default Applications");
|
||||
|
||||
QSettings settings("cutefishos", "defaultApps");
|
||||
|
||||
QString defaultBrowser = mimeApps.value("x-scheme-handler/http").toString();
|
||||
QString defaultFM = mimeApps.value("inode/directory").toString();
|
||||
QString defaultEMail = mimeApps.value("x-scheme-handler/mailto").toString();
|
||||
QString defaultTerminal = settings.value("terminal").toString();
|
||||
|
||||
// Init indexes.
|
||||
for (int i = 0; i < m_browserList.size(); ++i) {
|
||||
if (defaultBrowser == m_browserList.at(i).fileName) {
|
||||
m_browserIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_fileManagerList.size(); ++i) {
|
||||
if (defaultFM == m_fileManagerList.at(i).fileName) {
|
||||
m_fileManagerIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_emailList.size(); ++i) {
|
||||
if (defaultEMail == m_emailList.at(i).fileName) {
|
||||
m_emailIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_terminalList.size(); ++i) {
|
||||
if (defaultTerminal == m_terminalList.at(i).fileName) {
|
||||
m_terminalIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVariantList DefaultApplications::browserList()
|
||||
{
|
||||
QVariantList list;
|
||||
|
||||
for (const AppItem &item : m_browserList) {
|
||||
QVariantMap map;
|
||||
map["name"] = item.name;
|
||||
map["icon"] = item.icon;
|
||||
map["path"] = item.path;
|
||||
list << map;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
QVariantList DefaultApplications::fileManagerList()
|
||||
{
|
||||
QVariantList list;
|
||||
|
||||
for (const AppItem &item : m_fileManagerList) {
|
||||
QVariantMap map;
|
||||
map["name"] = item.name;
|
||||
map["icon"] = item.icon;
|
||||
map["path"] = item.path;
|
||||
list << map;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
QVariantList DefaultApplications::emailList()
|
||||
{
|
||||
QVariantList list;
|
||||
|
||||
for (const AppItem &item : m_emailList) {
|
||||
QVariantMap map;
|
||||
map["name"] = item.name;
|
||||
map["icon"] = item.icon;
|
||||
map["path"] = item.path;
|
||||
list << map;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
QVariantList DefaultApplications::terminalList()
|
||||
{
|
||||
QVariantList list;
|
||||
|
||||
for (const AppItem &item : m_terminalList) {
|
||||
QVariantMap map;
|
||||
map["name"] = item.name;
|
||||
map["icon"] = item.icon;
|
||||
map["path"] = item.path;
|
||||
list << map;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
int DefaultApplications::browserIndex()
|
||||
{
|
||||
return m_browserIndex;
|
||||
}
|
||||
|
||||
int DefaultApplications::fileManagerIndex()
|
||||
{
|
||||
return m_fileManagerIndex;
|
||||
}
|
||||
|
||||
int DefaultApplications::emailIndex()
|
||||
{
|
||||
return m_emailIndex;
|
||||
}
|
||||
|
||||
int DefaultApplications::terminalIndex()
|
||||
{
|
||||
return m_terminalIndex;
|
||||
}
|
||||
|
||||
void DefaultApplications::setDefaultBrowser(int index)
|
||||
{
|
||||
if (!m_browserList.isEmpty() && m_browserList.size() < index)
|
||||
return;
|
||||
|
||||
const QString desktop = m_browserList.at(index).fileName;
|
||||
|
||||
setDefaultApp("x-scheme-handler/http", desktop);
|
||||
setDefaultApp("x-scheme-handler/https", desktop);
|
||||
}
|
||||
|
||||
void DefaultApplications::setDefaultFileManager(int index)
|
||||
{
|
||||
if (!m_fileManagerList.isEmpty() && m_fileManagerList.size() < index)
|
||||
return;
|
||||
|
||||
const QString desktop = m_fileManagerList.at(index).fileName;
|
||||
|
||||
setDefaultApp("inode/directory", desktop);
|
||||
}
|
||||
|
||||
void DefaultApplications::setDefaultEMail(int index)
|
||||
{
|
||||
if (!m_emailList.isEmpty() && m_emailList.size() < index)
|
||||
return;
|
||||
|
||||
const QString desktop = m_emailList.at(index).fileName;
|
||||
|
||||
setDefaultApp("x-scheme-handler/mailto", desktop);
|
||||
}
|
||||
|
||||
void DefaultApplications::setDefaultTerminal(int index)
|
||||
{
|
||||
if (!m_terminalList.isEmpty() && m_terminalList.size() < index)
|
||||
return;
|
||||
|
||||
const QString desktop = m_terminalList.at(index).fileName;
|
||||
|
||||
qDebug() << index << desktop;
|
||||
|
||||
QSettings settings("cutefishos", "defaultApps");
|
||||
settings.setValue("terminal", desktop);
|
||||
}
|
||||
|
||||
void DefaultApplications::setDefaultApp(const QString &mimeType, const QString &path)
|
||||
{
|
||||
KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"),
|
||||
KConfig::NoGlobals,
|
||||
QStandardPaths::GenericConfigLocation);
|
||||
KConfigGroup defaultApp(profile, "Default Applications");
|
||||
defaultApp.writeXdgListEntry(mimeType, {path});
|
||||
}
|
||||
|
||||
QString DefaultApplications::mimeAppsListFilePath() const
|
||||
{
|
||||
return QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1String("/mimeapps.list");
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2021 - 2022 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <kate@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef DEFAULTAPPLICATIONS_H
|
||||
#define DEFAULTAPPLICATIONS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class AppItem {
|
||||
public:
|
||||
QString path;
|
||||
QString fileName;
|
||||
QString name;
|
||||
QString icon;
|
||||
QString mimeType;
|
||||
QString categories;
|
||||
};
|
||||
|
||||
class DefaultApplications : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QVariantList browserList READ browserList NOTIFY loadFinished)
|
||||
Q_PROPERTY(QVariantList fileManagerList READ fileManagerList NOTIFY loadFinished)
|
||||
Q_PROPERTY(QVariantList emailList READ emailList NOTIFY loadFinished)
|
||||
Q_PROPERTY(QVariantList terminalList READ terminalList NOTIFY loadFinished)
|
||||
Q_PROPERTY(int browserIndex READ browserIndex NOTIFY loadFinished)
|
||||
Q_PROPERTY(int fileManagerIndex READ fileManagerIndex NOTIFY loadFinished)
|
||||
Q_PROPERTY(int emailIndex READ emailIndex NOTIFY loadFinished)
|
||||
Q_PROPERTY(int terminalIndex READ terminalIndex NOTIFY loadFinished)
|
||||
|
||||
public:
|
||||
explicit DefaultApplications(QObject *parent = nullptr);
|
||||
|
||||
void loadApps();
|
||||
|
||||
QVariantList browserList();
|
||||
QVariantList fileManagerList();
|
||||
QVariantList emailList();
|
||||
QVariantList terminalList();
|
||||
|
||||
int browserIndex();
|
||||
int fileManagerIndex();
|
||||
int emailIndex();
|
||||
int terminalIndex();
|
||||
|
||||
Q_INVOKABLE void setDefaultBrowser(int index);
|
||||
Q_INVOKABLE void setDefaultFileManager(int index);
|
||||
Q_INVOKABLE void setDefaultEMail(int index);
|
||||
Q_INVOKABLE void setDefaultTerminal(int index);
|
||||
|
||||
private:
|
||||
void setDefaultApp(const QString &mimeType, const QString &path);
|
||||
QString mimeAppsListFilePath() const;
|
||||
|
||||
signals:
|
||||
void loadFinished();
|
||||
|
||||
private:
|
||||
QList<AppItem> m_browserList;
|
||||
QList<AppItem> m_fileManagerList;
|
||||
QList<AppItem> m_emailList;
|
||||
QList<AppItem> m_terminalList;
|
||||
|
||||
int m_browserIndex = -1;
|
||||
int m_fileManagerIndex = -1;
|
||||
int m_emailIndex = -1;
|
||||
int m_terminalIndex = -1;
|
||||
};
|
||||
|
||||
#endif // DEFAULTAPPLICATIONS_H
|
||||
@ -0,0 +1,114 @@
|
||||
#include "desktopproperties.h"
|
||||
#include <QTextStream>
|
||||
#include <QStringList>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
DesktopProperties::DesktopProperties(const QString &fileName, const QString &group)
|
||||
{
|
||||
if (!fileName.isEmpty()) {
|
||||
load(fileName, group);
|
||||
}
|
||||
}
|
||||
|
||||
DesktopProperties::~DesktopProperties()
|
||||
{
|
||||
}
|
||||
|
||||
bool DesktopProperties::load(const QString &fileName, const QString &group)
|
||||
{
|
||||
// NOTE: This class is used for reading of property files instead of QSettings
|
||||
// class, which considers separator ';' as comment
|
||||
|
||||
// Try open file
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clear old data
|
||||
data.clear();
|
||||
|
||||
// Indicator whether group was found or not, if name of group was not
|
||||
// specified, groupFound is always true
|
||||
bool groupFound = group.isEmpty();
|
||||
|
||||
// Read propeties
|
||||
QTextStream in(&file);
|
||||
while (!in.atEnd()) {
|
||||
|
||||
// Read new line
|
||||
QString line = in.readLine();
|
||||
|
||||
// Skip empty line or line with invalid format
|
||||
if (line.trimmed().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read group
|
||||
// NOTE: symbols '[' and ']' can be found not only in group names, but
|
||||
// only group can start with '['
|
||||
if (!group.isEmpty() && line.trimmed().startsWith("[")) {
|
||||
QString tmp = line.trimmed().replace("[", "").replace("]", "");
|
||||
groupFound = group.trimmed().compare(tmp) == 0;
|
||||
}
|
||||
|
||||
// If we are in correct group and line contains assignment then read data
|
||||
int first_equal = line.indexOf('=');
|
||||
|
||||
if (groupFound && first_equal >= 0) {
|
||||
data.insert(line.left(first_equal).trimmed(), line.mid(first_equal + 1).trimmed());
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DesktopProperties::save(const QString &fileName, const QString &group)
|
||||
{
|
||||
// Try open file
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write group
|
||||
QTextStream out(&file);
|
||||
if (!group.isEmpty()) {
|
||||
out << "[" + group + "]\n";
|
||||
}
|
||||
|
||||
// Write data
|
||||
foreach (QString key, data.keys()) {
|
||||
out << key << "=" << data.value(key).toString() << "\n";
|
||||
}
|
||||
|
||||
// Exit
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DesktopProperties::contains(const QString &key) const
|
||||
{
|
||||
return data.contains(key);
|
||||
}
|
||||
|
||||
QVariant DesktopProperties::value(const QString &key, const QVariant &defaultValue)
|
||||
{
|
||||
return data.value(key, defaultValue);
|
||||
}
|
||||
|
||||
void DesktopProperties::set(const QString &key, const QVariant &value)
|
||||
{
|
||||
if (data.contains(key)) {
|
||||
data.take(key);
|
||||
}
|
||||
|
||||
data.insert(key, value);
|
||||
}
|
||||
|
||||
QStringList DesktopProperties::allKeys() const
|
||||
{
|
||||
return data.keys();
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
#ifndef DESKTOPPROPERTIES_H
|
||||
#define DESKTOPPROPERTIES_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
#include <QMap>
|
||||
|
||||
/**
|
||||
* @class DesktopProperties
|
||||
* @brief Read property files
|
||||
* @author Michal Rost
|
||||
* @date 26.1.2013
|
||||
*/
|
||||
|
||||
class DesktopProperties
|
||||
{
|
||||
public:
|
||||
DesktopProperties(const QString &fileName = "", const QString &group = "");
|
||||
~DesktopProperties();
|
||||
|
||||
QVariant value(const QString &key, const QVariant &defaultValue = QVariant());
|
||||
bool load(const QString &fileName, const QString &group = "");
|
||||
bool save(const QString &fileName, const QString &group = "");
|
||||
void set(const QString &key, const QVariant &value);
|
||||
bool contains(const QString &key) const;
|
||||
QStringList allKeys() const;
|
||||
|
||||
protected:
|
||||
QMap<QString, QVariant> data;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2021 - 2022 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <support@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import QtQuick 2.12
|
||||
import QtQuick.Controls 2.12
|
||||
import QtQuick.Layouts 1.12
|
||||
|
||||
import FishUI 1.0 as FishUI
|
||||
import Cutefish.Settings 1.0
|
||||
import "../"
|
||||
|
||||
ComboBox {
|
||||
id: control
|
||||
textRole: "name"
|
||||
}
|
||||
Loading…
Reference in New Issue