This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Launcher_Linux.cpp
128 lines (78 loc) · 2.64 KB
/
Launcher_Linux.cpp
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
// Copyright (C) 2012-2015, Ali Baharev
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#ifdef __linux__
#include <QDebug>
#include <QMimeDatabase>
#include <QProcess>
#include <QString>
#include "Launcher.hpp"
#include "ErrorMsg.hpp"
bool openWithDefaultApp(const QString& file) {
QString command("xdg-open "+file);
bool success = QProcess::startDetached(command);
if (!success) {
showErrorMsg("failed to execute \""+command+"\"");
}
return success;
}
bool openWithAppAssoc(const QString& nativeFileName, const QString& extension) {
qDebug() << "Extension" << extension << "ignored, opening with xdg-open instead";
return openWithDefaultApp(nativeFileName);
}
bool openWithShortAppname(const QString& filename, const QString& appname, const QString& flag) {
QStringList args;
if (!flag.isEmpty()) {
args.append(flag);
}
args.append(filename);
qDebug() << "Attempting to execute" << appname << args;
return QProcess::startDetached(appname, args);
}
void openDirectoryWithFileManager(const QString& directory) {
openWithDefaultApp(directory);
}
QString getMimeTypeForExtension(const QString& extension) {
QString file(QString::fromWCharArray(L"dummy") + extension);
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile(file, QMimeDatabase::MatchExtension);
QString mime = mimeType.isDefault() ? QString() : mimeType.name();
if (!mime.isEmpty()) {
qDebug() << "Mime type for " << extension << " extension "
<< "is " << mime;
}
else {
qDebug() << "Only default mime type is found for " << extension;
}
return mime;
}
bool associatedAppExists(const QString& mime) {
if (mime.isEmpty()) {
return false;
}
QProcess p;
QStringList args;
args.append("query");
args.append("default");
args.append(mime);
p.start("xdg-mime", args );
bool success = p.waitForFinished(3000);
QString defaultApp = success ? QString(p.readAllStandardOutput()) : QString();
if (!success) {
qDebug() << "The xdg-mime query timed out";
}
else if (defaultApp.isEmpty()) {
qDebug() << "Apparently no associated app for " << mime;
}
else {
qDebug() << "Associated app is " << defaultApp;
}
return !defaultApp.isEmpty();
}
bool hasAssociatedApp(const wchar_t* ext) {
QString extension = QString::fromWCharArray(ext);
qDebug() << "Looking for the associated app for extension " << extension;
QString mime = getMimeTypeForExtension(extension);
return associatedAppExists(mime);
}
#endif