-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilebackend.cpp
More file actions
119 lines (111 loc) · 2.89 KB
/
Copy pathfilebackend.cpp
File metadata and controls
119 lines (111 loc) · 2.89 KB
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
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QUrl>
#include <QProcess>
#include "filebackend.h"
void FileBackend::setArguments(const QStringList& arguments)
{
status = Status::Loading;
emit statusChanged();
this->files.clear();
if (!arguments.empty()) {
QFileInfo info(arguments.at(0));
reader.setFileName(info.absoluteFilePath());
if (info.exists() && info.isFile() && reader.canRead()) {
this->initFromSingleFile(info);
}
}
status = (this->getCurrentFile() != nullptr) ? Status::Ready : Status::Empty;
emit statusChanged();
emit currentFileChanged();
}
bool FileBackend::setArgumentsFromQml(const QVariantList& arguments)
{
QStringList list;
for (auto &item : arguments) {
QUrl url(item.toString());
if (url.isLocalFile()) {
list.append(url.toLocalFile());
}
}
if (!list.empty()) {
this->setArguments(list);
return true;
}
return false;
}
void FileBackend::initFromSingleFile(const QFileInfo &file)
{
bool foundCurrent = false;
for (const QFileInfo& entry : file.absoluteDir().entryInfoList(QDir::NoFilter, QDir::Name | QDir::LocaleAware)) {
if (!entry.isFile()) {
continue;
}
reader.setFileName(entry.absoluteFilePath());
if (!reader.canRead()) {
// Not an image or file type not supported
continue;
}
files.push_back(std::unique_ptr<File>(new File(entry.absoluteFilePath())));
if (!foundCurrent && entry == file) {
foundCurrent = true;
auto last = this->files.end();
last--;
this->currentFile = last;
}
}
if (!foundCurrent) {
this->currentFile = this->files.begin();
}
}
File* FileBackend::getCurrentFile() const
{
if (!files.empty()) {
(*this->currentFile)->loadMetadata();
return this->currentFile->get();
}
return nullptr;
}
void FileBackend::next()
{
if (files.size() > 1) {
this->currentFile++;
if (this->currentFile == this->files.end()) {
this->currentFile = this->files.begin();
}
emit currentFileChanged();
}
}
void FileBackend::prev()
{
if (files.size() > 1) {
if (this->currentFile == this->files.begin()) {
this->currentFile = this->files.end();
}
this->currentFile--;
emit currentFileChanged();
}
}
FileBackend::Status FileBackend::getStatus() const
{
return status;
}
void FileBackend::showInFinder() const
{
auto file = getCurrentFile();
if (file == nullptr) {
return;
}
// Code taken from Qt Creator project
// https://github.com/qtproject/qt-creator/blob/397e7f48437dc57e6333c3a358ad24d3e891920d/src/plugins/coreplugin/fileutils.cpp#L68
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(file->getPath());
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e")
<< QLatin1String("tell application \"Finder\" to activate");
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
}