-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathapplication.cpp
182 lines (151 loc) · 5.06 KB
/
application.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "application.h"
#include "dbusinterface.h"
#include "window.h"
#include "desktop/desktop.h"
#include "thumbnailer/thumbnailprovider.h"
#include "filemanageradaptor.h"
#include <QCommandLineParser>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QDBusConnection>
#include <QPixmapCache>
#include <QTranslator>
#include <QFileInfo>
#include <QIcon>
#include <QDir>
// KIO
#include <KIO/CopyJob>
#include <KIO/Job>
#include <KIO/PreviewJob>
#include <KIO/DeleteJob>
#include <KIO/DropJob>
#include <KIO/FileUndoManager>
#include <KIO/JobUiDelegate>
#include <KIO/Paste>
#include <KIO/PasteJob>
#include <KIO/RestoreJob>
Application::Application(int& argc, char** argv)
: QApplication(argc, argv)
, m_instance(false)
{
if (QDBusConnection::sessionBus().registerService("com.cutefish.FileManager")) {
setOrganizationName("cutefishos");
setWindowIcon(QIcon::fromTheme("file-manager"));
new FileManagerAdaptor(this);
new DBusInterface;
QDBusConnection::sessionBus().registerObject("/FileManager", this);
// Translations
QLocale locale;
QString qmFilePath = QString("%1/%2.qm").arg("/usr/share/cutefish-filemanager/translations/").arg(locale.name());
if (QFile::exists(qmFilePath)) {
QTranslator *translator = new QTranslator(this);
if (translator->load(qmFilePath)) {
installTranslator(translator);
} else {
translator->deleteLater();
}
}
m_instance = true;
}
}
int Application::run()
{
if (!parseCommandLineArgs())
return 0;
return QApplication::exec();
}
void Application::openFiles(const QStringList &paths)
{
for (const QString &path : paths) {
openWindow(path);
}
}
void Application::moveToTrash(const QStringList &paths)
{
if (paths.isEmpty())
return;
QList<QUrl> urls;
for (const QString &path : paths) {
urls.append(QUrl::fromLocalFile(path));
}
KIO::Job *job = KIO::trash(urls);
job->uiDelegate()->setAutoErrorHandlingEnabled(true);
KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, urls, QUrl(QStringLiteral("trash:/")), job);
}
void Application::emptyTrash()
{
Window *w = new Window;
w->load(QUrl("qrc:/qml/Dialogs/EmptyTrashDialog.qml"));
}
void Application::openWindow(const QString &path)
{
Window *w = new Window;
w->rootContext()->setContextProperty("arg", path);
w->addImageProvider("thumbnailer", new ThumbnailProvider());
w->load(QUrl("qrc:/qml/main.qml"));
}
QStringList Application::formatUriList(const QStringList &list)
{
QStringList val;
for (const QString &path : list) {
val.append(path == "." ? QDir::currentPath() : path);
}
if (val.isEmpty()) {
val.append(QDir::currentPath());
}
return val;
}
bool Application::parseCommandLineArgs()
{
QCommandLineParser parser;
parser.setApplicationDescription(QStringLiteral("File Manager"));
parser.addHelpOption();
parser.addPositionalArgument("files", "Files", "[FILE1, FILE2,...]");
QCommandLineOption desktopOption(QStringList() << "d" << "desktop" << "Desktop Mode");
parser.addOption(desktopOption);
QCommandLineOption emptyTrashOption(QStringList() << "e" << "empty-trash" << "Empty Trash");
parser.addOption(emptyTrashOption);
QCommandLineOption moveToTrashOption(QStringList() << "mtr" << "move-to-trash" << "Move To Trash");
parser.addOption(moveToTrashOption);
parser.process(arguments());
if (m_instance) {
QPixmapCache::setCacheLimit(2048);
if (parser.isSet(desktopOption)) {
Desktop desktop;
} else {
openFiles(formatUriList(parser.positionalArguments()));
}
} else {
QDBusInterface iface("com.cutefish.FileManager",
"/FileManager",
"com.cutefish.FileManager",
QDBusConnection::sessionBus(), this);
if (parser.isSet(emptyTrashOption)) {
// Empty Dialog
iface.call("emptyTrash");
} else if (parser.isSet(moveToTrashOption)) {
iface.call("moveToTrash", parser.positionalArguments());
} else {
iface.call("openFiles", formatUriList(parser.positionalArguments()));
}
}
return m_instance;
}