
QFile *file = new QFile;
file->setFileName("/usr/share/applications/mimeinfo.cache");
bool ok = file->open(QIODevice::ReadOnly);
if (ok) {
QTextStream TS(file);
QString s = TS.readAll();
file->close();
QStringList SL = s.split("\n");
SL = SL.filter(MIME + "=");
QMenu *menu_openwith = new QMenu(this);
action_openwith->setMenu(menu_openwith);
for (int i=0; i<SL.length(); i++) {
s = SL.at(i);
//qDebug() << s;
s = s.mid(s.indexOf("=")+1);
//qDebug() << s;
QStringList SL1 = s.split(";");
SL1.removeAll("");
for (int j=0; j<SL1.length(); j++) {
QString desktop = "/usr/share/applications/" + SL1.at(j);
QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
sExec = sExec.left(sExec.indexOf(" "));
QString sName = readSettings(desktop, "Desktop Entry", "Name");
QString sIcon = readSettings(desktop, "Desktop Entry", "Icon");
QAction *action = new QAction(menu_openwith);
action->setText(sName);
QIcon icon;
if (sIcon == "")
sIcon = "applications-system-symbolic";
if (QFileInfo(sIcon).isFile()) {
icon = QIcon(sIcon);
} else {
icon = QIcon::fromTheme(sIcon);
}
action->setIcon(icon);
connect(action, &QAction::triggered, [=](){
QProcess *process = new QProcess;
process->setWorkingDirectory(path);
qDebug() << sExec;
process->setProgram(sExec);
process->setArguments(QStringList() << filepath);
bool b = process->startDetached();
qDebug() << b;
});
menu_openwith->addAction(action);
}
}
}