-
Notifications
You must be signed in to change notification settings - Fork 303
/
extprog.cc
335 lines (236 loc) · 9.73 KB
/
extprog.cc
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* This file is part of RawTherapee.
*
* Copyright (c) 2012 Oliver Duis <www.oliverduis.de>
*
* RawTherapee 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.
*
* RawTherapee 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 RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "extprog.h"
#include <cstring>
#include <iostream>
#ifdef WIN32
#include <windows.h>
#include <shlobj.h>
#endif
#include "options.h"
#include "multilangmgr.h"
Glib::ustring ExtProgAction::getFullName () const
{
return name + " [" + M(Glib::ustring::compose("EXTPROGTARGET_%1", target)) + "]";
}
bool ExtProgAction::execute (const std::vector<Glib::ustring>& fileNames) const
{
if (fileNames.empty ()) {
return false;
}
// Check if they all exists as they may not be processed yet.
for (const auto& fileName : fileNames) {
if (Glib::file_test (fileName, Glib::FILE_TEST_EXISTS)) {
continue;
}
Gtk::MessageDialog (M("MAIN_MSG_IMAGEUNPROCESSED"), true, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true).run ();
return false;
}
Glib::ustring cmdLine = "\"" + filePathEXE + "\"";
if (!preparams.empty()) {
cmdLine += " " + preparams;
}
for (const auto& fileName : fileNames) {
cmdLine += " \"" + fileName + "\"";
}
return ExtProgStore::spawnCommandAsync (cmdLine);
}
ExtProgStore* ExtProgStore::getInstance()
{
static ExtProgStore instance_;
return &instance_;
}
// Reads all profiles from the given profiles dir
void ExtProgStore::init ()
{
MyMutex::MyLock lock(mtx);
actions.clear ();
#ifdef WIN32
// Please do not add obscure little tools here, only widely used programs.
// They should also have a proper setup program and therefore a standard path.
searchProgram ("Photoshop", "Adobe\\Adobe Photoshop CS%1 (64 Bit)\\Photoshop.exe", "Adobe\\Adobe Photoshop CS%1\\Photoshop.exe", 9, false, true);
searchProgram ("Photomatix Pro", "PhotomatixPro%1\\PhotomatixPro.exe", "", 9, true, true);
searchProgram ("Paint.NET", "Paint.NET\\PaintDotNet.exe", "", 0, false, true);
searchProgram ("MS Image Composition Editor", "Microsoft Research\\Image Composite Editor\\ICE.exe", "", 0, false, true);
searchProgram ("PTGui", "PTGui\\PTGui.exe", "", 0, false, true);
searchProgram ("GeoSetter", "GeoSetter\\GeoSetter.exe", "", 0, true, true);
searchProgram ("FastStone Image Viewer", "FastStone Image Viewer\\FSViewer.exe", "", 0, true, true);
searchProgram ("FastPictureViewer", "FastPictureViewer\\FastPictureViewer.exe", "", 0, true, true);
if (!searchProgram ("Autopano Giga 3", "Kolor\\Autopano Giga 3.%1\\AutopanoGiga_x64.exe", "Kolor\\Autopano Giga 3.%1\\AutopanoGiga.exe", 15, true, true)) {
if (!searchProgram ("Autopano Pro 3", "Kolor\\Autopano Pro 3.%1\\AutopanoPro_x64.exe", "Kolor\\Autopano Pro 3.%1\\AutopanoPro.exe", 15, true, true)) {
if (!searchProgram ("Autopano Giga 2", "Kolor\\Autopano Giga 2.%1\\AutopanoGiga_x64.exe", "Kolor\\Autopano Giga 2.%1\\AutopanoGiga.exe", 6, true, true)) {
searchProgram ("Autopano Pro 2", "Kolor\\Autopano Pro 2.%1\\AutopanoPro_x64.exe", "Kolor\\Autopano Pro 2.%1\\AutopanoPro.exe", 6, true, true);
}
}
}
#endif
}
bool ExtProgStore::searchProgram (const Glib::ustring& name,
const Glib::ustring& exePath,
const Glib::ustring& exePath86,
int maxVer,
bool allowRaw,
bool allowQueueProcess)
{
#ifdef WIN32
// get_user_special_dir crashes on some Windows configurations.
static Glib::ustring progFilesDir, progFilesDirx86;
if (progFilesDir.empty ()) {
WCHAR pathW[MAX_PATH];
char pathA[MAX_PATH];
if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_PROGRAM_FILES, false)) {
if (WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0)) {
progFilesDir = pathA;
}
}
if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_PROGRAM_FILESX86, false)) {
if (WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0)) {
progFilesDirx86 = pathA;
}
}
}
ExtProgAction action;
action.name = name;
action.target = (allowRaw ? 1 : 2);
auto& filePath = action.filePathEXE;
if (maxVer > 0) {
for (auto ver = maxVer; ver >= 0; ver--) {
filePath = progFilesDir + "\\" + Glib::ustring::compose(exePath, ver);
if (Glib::file_test (filePath, Glib::FILE_TEST_EXISTS)) {
break;
}
if (!exePath86.empty ()) {
filePath = progFilesDirx86 + "\\" + Glib::ustring::compose(exePath86, ver);
if (Glib::file_test (filePath, Glib::FILE_TEST_EXISTS)) {
break;
}
}
filePath.clear ();
}
} else {
do {
filePath = progFilesDir + "\\" + exePath;
if (Glib::file_test (filePath, Glib::FILE_TEST_EXISTS)) {
break;
}
if (!exePath86.empty ()) {
filePath = progFilesDirx86 + "\\" + exePath86;
if (Glib::file_test (filePath, Glib::FILE_TEST_EXISTS)) {
break;
}
}
filePath.clear ();
} while (false);
}
if (!action.filePathEXE.empty ()) {
actions.push_back (action);
if (allowRaw && allowQueueProcess) {
action.target = 2;
actions.push_back (action);
}
return true;
}
#endif
return false;
}
bool ExtProgStore::spawnCommandAsync (const Glib::ustring& cmd)
{
try {
const auto encodedCmd = Glib::filename_from_utf8 (cmd);
Glib::spawn_command_line_async (encodedCmd.c_str ());
return true;
} catch (const Glib::Exception& exception) {
if (options.rtSettings.verbose) {
std::cerr << "Failed to execute \"" << cmd << "\": " << exception.what() << std::endl;
}
return false;
}
}
bool ExtProgStore::spawnCommandSync (const Glib::ustring& cmd)
{
auto exitStatus = -1;
try {
Glib::spawn_command_line_sync (cmd, NULL, NULL, &exitStatus);
} catch (const Glib::Exception& exception) {
if (options.rtSettings.verbose) {
std::cerr << "Failed to execute \"" << cmd << "\": " << exception.what() << std::endl;
}
}
return exitStatus == 0;
}
bool ExtProgStore::openInGimp (const Glib::ustring& fileName)
{
#if defined WIN32
auto executable = Glib::build_filename (options.gimpDir, "bin", "gimp-win-remote");
auto cmdLine = Glib::ustring::compose ("\"%1\" gimp-2.4.exe \"%2\"", executable, fileName);
auto success = spawnCommandAsync (cmdLine);
#elif defined __APPLE__
// Apps should be opened using the simplest, case-insensitive form, "open -a NameOfProgram"
// Calling the executable directly is said to often cause trouble,
// https://discuss.pixls.us/t/affinity-photo-as-external-editor-how-to/1756/18
auto cmdLine = Glib::ustring("open -a GIMP \'") + fileName + Glib::ustring("\'");
auto success = spawnCommandAsync (cmdLine);
#else
auto cmdLine = Glib::ustring("gimp \"") + fileName + Glib::ustring("\"");
auto success = spawnCommandAsync (cmdLine);
#endif
if (success) {
return true;
}
#ifdef WIN32
for (auto ver = 12; ver >= 0; --ver) {
executable = Glib::build_filename (options.gimpDir, "bin", Glib::ustring::compose (Glib::ustring("gimp-2.%1.exe"), ver));
cmdLine = Glib::ustring::compose ("\"%1\" \"%2\"", executable, fileName);
success = spawnCommandAsync (cmdLine);
if (success) {
return true;
}
}
#elif defined __APPLE__
cmdLine = Glib::ustring("open -a GIMP-dev \'") + fileName + Glib::ustring("\'");
success = ExtProgStore::spawnCommandAsync (cmdLine);
#else
cmdLine = Glib::ustring("gimp-remote \"") + fileName + Glib::ustring("\"");
success = ExtProgStore::spawnCommandAsync (cmdLine);
#endif
return success;
}
bool ExtProgStore::openInPhotoshop (const Glib::ustring& fileName)
{
#if defined WIN32
const auto executable = Glib::build_filename(options.psDir, "Photoshop.exe");
const auto cmdLine = Glib::ustring("\"") + executable + Glib::ustring("\" \"") + fileName + Glib::ustring("\"");
#elif defined __APPLE__
const auto cmdLine = Glib::ustring("open -a Photoshop \'") + fileName + Glib::ustring("\'");
#else
const auto cmdLine = Glib::ustring("\"") + Glib::build_filename(options.psDir, "Photoshop.exe") + Glib::ustring("\" \"") + fileName + Glib::ustring("\"");
#endif
return spawnCommandAsync (cmdLine);
}
bool ExtProgStore::openInCustomEditor (const Glib::ustring& fileName)
{
#if defined WIN32
const auto cmdLine = Glib::ustring("\"") + options.customEditorProg + Glib::ustring("\" \"") + fileName + Glib::ustring("\"");
#elif defined __APPLE__
const auto cmdLine = options.customEditorProg + Glib::ustring(" \"") + fileName + Glib::ustring("\"");
#else
const auto cmdLine = Glib::ustring("\"") + options.customEditorProg + Glib::ustring("\" \"") + fileName + Glib::ustring("\"");
#endif
return spawnCommandAsync (cmdLine);
}