Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (157 loc) · 7.12 KB
/
Copy pathmain.cpp
File metadata and controls
174 lines (157 loc) · 7.12 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
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
/*
* Copyright (C) 2013-2015 Ofer Kashayov <oferkv@live.com>
* This file is part of Phototonic Image Viewer.
*
* Phototonic 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
* (at your option) any later version.
*
* Phototonic 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 Phototonic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Phototonic.h"
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QFile>
#include <QFileSystemWatcher>
#include <QLibraryInfo>
#include <QTranslator>
#include <signal.h>
static QString singletonPath = QDir::tempPath() + QDir::separator() + ".singleton.Phototonic.";
#ifndef Q_OS_WIN
static void sighandler(int signum) {
switch(signum) {
case SIGABRT:
case SIGINT:
case SIGSEGV:
case SIGTERM:
QFile::remove(singletonPath);
break;
default:
break;
}
raise(signum);
}
#endif
int main(int argc, char *argv[]) {
QApplication QApp(argc, argv);
QLocale locale = QLocale::system();
QCoreApplication::setApplicationVersion(VERSION);
QCommandLineParser parser;
parser.setApplicationDescription(VERSION " image viewer.");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument(QCoreApplication::translate("main", "files or directory"),
QCoreApplication::translate("main", "files or directory to open, \"-\" to read them from stdin."),
QCoreApplication::translate("main", "[FILE...] | [DIRECTORY] | [-]"));
QCommandLineOption langOption(QStringList() << "l" << "lang",
QCoreApplication::translate("main", "start with a specific translation"),
QCoreApplication::translate("main", "language"));
parser.addOption(langOption);
QCommandLineOption targetDirectoryOption(QStringList() << "o" << "output-directory",
QCoreApplication::translate("main", "Copy all modified images into <directory>."),
QCoreApplication::translate("main", "directory"));
parser.addOption(targetDirectoryOption);
QCommandLineOption singletonOption(QStringList() << "s" << "single",
QCoreApplication::translate("main", "Run a single instance of Phototonic or open files in such already running."));
parser.addOption(singletonOption);
QCommandLineOption filterOption(QStringList() << "f" << "filter",
QCoreApplication::translate("main", "Apply <filter> to the directory and print matches."),
QCoreApplication::translate("main", "filter"));
parser.addOption(filterOption);
parser.process(QApp);
Phototonic *futuretonic = nullptr;
bool isSingleton = false;
if (parser.isSet(singletonOption)) {
QStringList singletons = QDir::temp().entryList(QStringList() << ".singleton.Phototonic.*", QDir::Files|QDir::Hidden|QDir::Writable);
#ifndef Q_OS_WIN
for (QStringList::iterator it = singletons.begin(); it != singletons.end();) {
bool ok;
qint64 pid = it->section('.', -1).toLongLong(&ok, 10);
if (!ok || kill(pid, 0)) {
qWarning() << "removing stale" << *it;
QFile::remove(QDir::tempPath() + QDir::separator() + *it);
it = singletons.erase(it);
} else {
++it;
}
}
#endif
if (!singletons.isEmpty()) { // slave mode
QFile singleton(QDir::tempPath() + QDir::separator() + singletons.first());
if (singleton.open(QIODevice::WriteOnly | QIODevice::Text)) {
QStringList args = parser.positionalArguments();
singleton.write(args.size() ? args.at(0).toUtf8() : "activate");
singleton.close();
return 0;
}
return 1;
}
singletonPath += QString::number(QCoreApplication::applicationPid());
QFile singleton(singletonPath);
if (singleton.open(QIODevice::WriteOnly)) { // master mode
singleton.close(); // created file
isSingleton = true;
QFileSystemWatcher *watcher = new QFileSystemWatcher(QStringList() << singletonPath, &QApp);
QObject::connect(watcher, &QFileSystemWatcher::fileChanged, [=,&futuretonic]() {
watcher->blockSignals(true);
QFile singleton(singletonPath);
QString newPath;
if (singleton.open(QIODevice::ReadOnly | QIODevice::Text)) {
newPath = QString::fromUtf8(singleton.readLine(4096));
singleton.close();
}
singleton.resize(0);
if (newPath == "activate")
futuretonic->activateWindow();
else if (!newPath.isEmpty())
futuretonic->setCurrentFileOrDirectory(newPath);
watcher->blockSignals(false);
});
#ifndef Q_OS_WIN
struct sigaction sa;
sa.sa_handler = sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART|SA_RESETHAND;
for (int signum : {SIGABRT,SIGINT,SIGSEGV,SIGTERM}) {
if (sigaction(signum, &sa, NULL) == -1)
qDebug() << "no signal handling for" << signum;
}
#endif
} else {
qDebug() << "MEH, cannot read or write singleton file";
}
}
if (parser.isSet(langOption))
locale = QLocale(parser.value(langOption));
QTranslator qTranslator;
if (qTranslator.load(locale, "qt", "_", QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
QApp.installTranslator(&qTranslator);
QTranslator qTranslatorPhototonic;
if (qTranslatorPhototonic.load(locale, "phototonic", "_", QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
QApp.installTranslator(&qTranslatorPhototonic);
} else if (qTranslatorPhototonic.load(locale, "phototonic", "_", ":/translations")) {
QApp.installTranslator(&qTranslatorPhototonic);
} else {
qWarning("No translation module found for your locale.\nPlace a phototonic_%s.tm or phototonic_%s.tm module into %s",
qUtf8Printable(locale.bcp47Name()),
qUtf8Printable(locale.name()),
qUtf8Printable(QLibraryInfo::path(QLibraryInfo::TranslationsPath)));
}
Phototonic phototonic(parser.positionalArguments(), 0, parser.value(filterOption));
futuretonic = &phototonic;
if (parser.isSet(targetDirectoryOption))
phototonic.setSaveDirectory(parser.value(targetDirectoryOption));
phototonic.show();
int result = QApp.exec();
if (isSingleton)
QFile::remove(singletonPath);
return result;
}