File indexing completed on 2024-05-12 04:58:27

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "desktopfile.h"
0019 
0020 #include <QSettings>
0021 #include <QStandardPaths>
0022 #include <QLocale>
0023 
0024 DesktopFile::DesktopFile()
0025 = default;
0026 
0027 DesktopFile::DesktopFile(const QString &fileName)
0028 {
0029     m_settings.reset(new QSettings(fileName, QSettings::IniFormat));
0030     m_settings->beginGroup(QSL("Desktop Entry"));
0031 }
0032 
0033 QString DesktopFile::fileName() const
0034 {
0035     return m_settings ? m_settings->fileName() : QString();
0036 }
0037 
0038 QString DesktopFile::name() const
0039 {
0040     return value(QSL("Name"), true).toString();
0041 }
0042 
0043 QString DesktopFile::comment() const
0044 {
0045     return value(QSL("Comment"), true).toString();
0046 }
0047 
0048 QString DesktopFile::type() const
0049 {
0050     return value(QSL("Type")).toString();
0051 }
0052 
0053 QString DesktopFile::icon() const
0054 {
0055     return value(QSL("Icon")).toString();
0056 }
0057 
0058 QVariant DesktopFile::value(const QString &key, bool localized) const
0059 {
0060     if (!m_settings) {
0061         return {};
0062     }
0063     if (localized) {
0064         const QLocale locale = QLocale::system();
0065         QString localeKey = QSL("%1[%2]").arg(key, locale.name());
0066         if (m_settings->contains(localeKey)) {
0067             return m_settings->value(localeKey);
0068         }
0069         localeKey = QSL("%1[%2]").arg(key, locale.bcp47Name());
0070         if (m_settings->contains(localeKey)) {
0071             return m_settings->value(localeKey);
0072         }
0073         const int i = locale.name().indexOf(QLatin1Char('_'));
0074         if (i > 0) {
0075             localeKey = QSL("%1[%2]").arg(key, locale.name().left(i));
0076             if (m_settings->contains(localeKey)) {
0077                 return m_settings->value(localeKey);
0078             }
0079         }
0080     }
0081     return m_settings->value(key);
0082 }
0083 
0084 bool DesktopFile::tryExec() const
0085 {
0086     if (!m_settings) {
0087         return false;
0088     }
0089 
0090     const QString exec = m_settings->value(QSL("TryExec")).toString();
0091     return exec.isEmpty() || !QStandardPaths::findExecutable(exec).isEmpty();
0092 }