-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystemmodel.cpp
42 lines (33 loc) · 1.17 KB
/
filesystemmodel.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
#include "filesystemmodel.h"
#include <QMimeDatabase>
#include <QStandardPaths>
FileSystemModel::FileSystemModel(QObject *parent) : QFileSystemModel(parent)
{
setRootPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
}
int FileSystemModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QString FileSystemModel::readFile(const QString &filePath)
{
if (filePath.isEmpty())
return {};
QFile file(filePath);
if (file.size() >= 2'000'000)
return tr("File size is too big.\nYou can read files up to %1 MB.").arg(2);
static const QMimeDatabase db;
const QMimeType mime = db.mimeTypeForFile(QFileInfo(file));
const auto mimeTypesForFile = mime.parentMimeTypes();
for (const auto &m : mimeTypesForFile) {
if (m.contains("text", Qt::CaseInsensitive)|| mime.comment().contains("text", Qt::CaseInsensitive)) {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return tr("Error opening the File!");
}
QTextStream stream(&file);
return stream.readAll();
}
}
return tr("Filetype not supported!");
}