ogbrowser/src/desktopparser.cpp

67 lines
2.0 KiB
C++

#include "desktopparser.h"
#include <QDebug>
#include <QtGlobal>
#include <QDir>
DesktopParser::DesktopParser() {
_applicationPaths += qgetenv("HOME") + "./local/share/applications";
}
void DesktopParser::loadSchemes() {
qDebug() << "Loading URL schemes";
_schemes.clear();
for(const auto& appdir : _applicationPaths) {
qDebug() << "Looking in" << appdir;
QDir dir(appdir);
for(const QFileInfo &fi : dir.entryInfoList(QDir::Files)) {
//qDebug() << "File: " << fi;
QFile file(fi.absoluteFilePath());
if (file.open(QIODevice::ReadOnly)) {
QTextStream fs(&file);
QString commandString;
QString mimeTypesString;
while(!fs.atEnd()) {
QString line = fs.readLine();
QStringList parts = line.split("=");
if (!parts.length() > 1) continue;
if ( parts[0] == "Exec") {
commandString=parts[1];
}
if (parts[0] == "MimeType" ) {
mimeTypesString=parts[1];
}
}
if (!commandString.isEmpty() && !mimeTypesString.isEmpty()) {
//qDebug() << "Found MIME handler: " << mimeTypesString << "; " << commandString;
QStringList handlers = mimeTypesString.split(";");
for(const QString &handler : handlers) {
QStringList parts = handler.split("/");
if (parts.length() > 1 && parts[0] == "x-scheme-handler") {
qInfo() << "Found URL scheme handler: " << parts[1] << " => " << commandString;
_schemes[parts[1]] = commandString;
}
}
}
} else {
qWarning() << "Failed to open" << fi.absoluteFilePath();
}
}
}
}