File: desktopfileparse.cpp

package info (click to toggle)
flameshot 0.6.0-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,576 kB
  • sloc: cpp: 9,204; sh: 196; xml: 95; makefile: 10
file content (152 lines) | stat: -rw-r--r-- 5,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors
//
// This file is part of Flameshot.
//
//     Flameshot 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
//     (at your option) any later version.
//
//     Flameshot 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 Flameshot.  If not, see <http://www.gnu.org/licenses/>.

#include "desktopfileparse.h"
#include <QFile>
#include <QDir>
#include <QString>
#include <QTextStream>
#include <QLocale>

DesktopFileParser::DesktopFileParser() {
    QString locale = QLocale().name();
    QString localeShort = QLocale().name().left(2);
    m_localeName = QStringLiteral("Name[%1]").arg(locale);
    m_localeDescription = QStringLiteral("Comment[%1]").arg(locale);
    m_localeNameShort = QStringLiteral("Name[%1]").arg(localeShort);
    m_localeDescriptionShort = QStringLiteral("Comment[%1]")
            .arg(localeShort);
    m_defaultIcon = QIcon::fromTheme(QStringLiteral("application-x-executable"));
}

DesktopAppData DesktopFileParser::parseDesktopFile(
        const QString &fileName, bool &ok) const
{
    DesktopAppData res;
    ok = true;
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        ok = false;
        return res;
    }
    bool nameLocaleSet = false;
    bool descriptionLocaleSet = false;
    bool isApplication = false;
    QTextStream in(&file);
    // enter the desktop entry definition
    while (!in.atEnd() && in.readLine() != QLatin1String("[Desktop Entry]")) {
    }
    // start parsing
    while (!in.atEnd()) {
        QString line = in.readLine();
        if (line.startsWith(QLatin1String("Icon"))) {
            res.icon = QIcon::fromTheme(
                      line.mid(line.indexOf(QLatin1String("="))+1).trimmed(),
                        m_defaultIcon);
        }
        else if (!nameLocaleSet && line.startsWith(QLatin1String("Name"))) {
            if (line.startsWith(m_localeName) ||
                    line.startsWith(m_localeNameShort))
            {
                res.name = line.mid(line.indexOf(QLatin1String("="))+1).trimmed();
                nameLocaleSet = true;
            } else if (line.startsWith(QLatin1String("Name="))) {
                res.name = line.mid(line.indexOf(QLatin1String("="))+1).trimmed();
            }
        }
        else if (!descriptionLocaleSet && line.startsWith(QLatin1String("Comment"))) {
            if (line.startsWith(m_localeDescription) ||
                    line.startsWith(m_localeDescriptionShort))
            {
                res.description = line.mid(line.indexOf(QLatin1String("="))+1).trimmed();
                descriptionLocaleSet = true;
            } else if (line.startsWith(QLatin1String("Comment="))) {
                res.description = line.mid(line.indexOf(QLatin1String("="))+1).trimmed();
            }
        }
        else if (line.startsWith(QLatin1String("Exec"))) {
            if (line.contains(QLatin1String("%"))) {
                res.exec = line.mid(line.indexOf(QLatin1String("="))+1)
                        .trimmed();
            } else {
                ok = false;
                break;
            }
        }
        else if (line.startsWith(QLatin1String("Type"))) {
            if (line.contains(QLatin1String("Application"))) {
                isApplication = true;
            }
        }
        else if (line.startsWith(QLatin1String("Categories"))) {
            res.categories = line.mid(line.indexOf(QLatin1String("="))+1).split(QStringLiteral(";"));
        }
        else if (line == QLatin1String("NoDisplay=true")) {
            ok = false;
            break;
        }
        else if (line == QLatin1String("Terminal=true")) {
            res.showInTerminal = true;
        }
        // ignore the other entries
        else if (line.startsWith(QLatin1String("["))) {
            break;
        }
    }
    file.close();
    if (res.exec.isEmpty() || res.name.isEmpty() || !isApplication) {
        ok = false;
    }
    return res;
}

int DesktopFileParser::processDirectory(const QDir &dir) {
    QStringList entries = dir.entryList(QDir::NoDotAndDotDot | QDir::Files);
    bool ok;
    int length = m_appList.length();
    for (QString file: entries){
        DesktopAppData app = parseDesktopFile(dir.absoluteFilePath(file), ok);
        if (ok) {
            m_appList.append(app);
        }
    }
    return m_appList.length() - length;
}

QVector<DesktopAppData> DesktopFileParser::getAppsByCategory(const QString &category) {
    QVector<DesktopAppData> res;
    for (const DesktopAppData &app : m_appList) {
        if (app.categories.contains(category)) {
            res.append(app);
        }
    }
    return res;
}

QMap<QString, QVector<DesktopAppData>> DesktopFileParser::getAppsByCategory(
        const QStringList &categories)
{
    QMap<QString, QVector<DesktopAppData>> res;
    for (const DesktopAppData &app : m_appList) {
        for (const QString &category: categories) {
            if (app.categories.contains(category)) {
                res[category].append(app);
            }
        }
    }
    return res;
}