root/src/systemutils.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
#include "systemutils.h"

#ifdef __unix__
#include <sys/resource.h>
#elif defined(windows)
#endif

#include <stdlib.h>
#include <stdio.h>


SystemUtils::SystemUtils()
{
}

QString SystemUtils::fileMime(const QString filepath) {
    // for some fucking reason mimetype started returning application/x-extension-html for .html
    if (filepath.endsWith(".html")) {
        return "text/html";
    }
#ifdef __linux__
    char mimeBuffer[128]; // shit what the fuck
    FILE* fp = popen(QString("xdg-mime query filetype %1").arg(filepath).toStdString().data(), "r");
    if (fp == NULL) {
        return QString();
    }
    while (fgets(mimeBuffer, sizeof(mimeBuffer)-1, fp) != NULL) {
        //printf("mime: %s\n", mimeBuffer);
        removeNewline(mimeBuffer);
        return mimeBuffer;
    }
    removeNewline(mimeBuffer);
    return mimeBuffer;
#endif
    return QString();
}

void SystemUtils::removeNewline(char* buffer) {
    int length = strlen(buffer);
    while (buffer[length-1] == '\n') {
        buffer[length-1] = '\0';
        length = strlen(buffer);
    }
}

bool SystemUtils::commandExists(QString command) {
#ifdef __unix__
    return 0 == system(QString("command -v %1 > /dev/null").arg(command).toUtf8());
#endif
    return false;
}

int SystemUtils::setProcessPriority(QProcess &process, int nice) {
#ifdef __unix__
    return setpriority(PRIO_PROCESS, process.pid(), nice);
#else
    return 0;
#endif
}