-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathufraw_saver.c
154 lines (150 loc) · 5.74 KB
/
ufraw_saver.c
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
/*
* UFRaw - Unidentified Flying Raw converter for digital camera images
*
* ufraw_saver.c - The GUI file saver.
* Copyright 2004-2016 by Udi Fuchs
*
* 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 2 of the License, or
* (at your option) any later version.
*/
#include "ufraw.h"
#include "uf_gtk.h"
#include <errno.h>
#include <string.h>
#include <glib/gi18n.h>
static int ufraw_overwrite_dialog(char *filename, GtkWidget *widget)
{
char message[max_path];
int response;
GtkWidget *dialog = gtk_dialog_new_with_buttons(_("File exists"),
GTK_WINDOW(gtk_widget_get_toplevel(widget)),
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
GTK_STOCK_NO, GTK_RESPONSE_NO,
GTK_STOCK_YES, GTK_RESPONSE_YES, NULL);
char *utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
if (utf8 == NULL) utf8 = g_strdup("Unknown file name");
snprintf(message, max_path,
_("File '%s' already exists.\nOverwrite?"), utf8);
g_free(utf8);
GtkWidget *label = gtk_label_new(message);
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
gtk_widget_show_all(dialog);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return (response == GTK_RESPONSE_YES);
}
long ufraw_save_now(ufraw_data *uf, void *widget)
{
if (!uf->conf->overwrite && uf->conf->createID != only_id
&& g_file_test(uf->conf->outputFilename, G_FILE_TEST_EXISTS)) {
if (!ufraw_overwrite_dialog(uf->conf->outputFilename, widget))
return UFRAW_ERROR;
}
int status = ufraw_write_image(uf);
if (status == UFRAW_ERROR) {
ufraw_message(status, ufraw_get_message(uf));
return UFRAW_ERROR;
}
if (status == UFRAW_WARNING) {
ufraw_message(status, ufraw_get_message(uf));
}
if (ufraw_get_message(uf) != NULL)
ufraw_message(UFRAW_SET_LOG, ufraw_get_message(uf));
return UFRAW_SUCCESS;
}
long ufraw_send_to_gimp(ufraw_data *uf)
{
char *basename = g_path_get_basename(uf->conf->inputFilename);
char *template = g_strconcat(basename, "_XXXXXX", NULL);
g_free(basename);
char *confFilename = NULL;
GError *err = NULL;
int fd = g_file_open_tmp(template, &confFilename, &err);
g_free(template);
if (fd == -1) {
g_free(confFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error creating temporary file."), err->message);
g_error_free(err);
return UFRAW_ERROR;
}
FILE *out = fdopen(fd, "w");
if (out == NULL) {
g_free(confFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error creating temporary file."), g_strerror(errno));
return UFRAW_ERROR;
}
char *buffer;
int saveCreateID = uf->conf->createID;
uf->conf->createID = send_id;
conf_save(uf->conf, confFilename, &buffer);
uf->conf->createID = saveCreateID;
if (fwrite(buffer, strlen(buffer), 1, out) != 1) {
g_free(buffer);
g_free(confFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error creating temporary file."), g_strerror(errno));
return UFRAW_ERROR;
}
g_free(buffer);
if (fclose(out) != 0) {
g_free(confFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error creating temporary file."), g_strerror(errno));
return UFRAW_ERROR;
}
char *fullConfFilename = g_strconcat(confFilename, ".ufraw", NULL);
if (g_rename(confFilename, fullConfFilename) == -1) {
g_free(confFilename);
g_free(fullConfFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error creating temporary file."), g_strerror(errno));
return UFRAW_ERROR;
}
g_free(confFilename);
char *commandLine = g_strdup_printf("%s \"%s\"",
uf->conf->remoteGimpCommand, fullConfFilename);
/* gimp-remote starts the gimp in a fork().
* Therefore we must call it asynchronously. */
if (!g_spawn_command_line_async(commandLine, &err)) {
g_free(commandLine);
#ifdef _WIN32
if (strcmp(uf->conf->remoteGimpCommand,
conf_default.remoteGimpCommand) == 0) {
/* If the user didn't play with the remoteGimpCommand,
* try to run Gimp-2.6 instead of Gimp-2.8 */
g_strlcpy(uf->conf->remoteGimpCommand, "gimp-2.6.exe", max_path);
commandLine = g_strdup_printf("%s \"%s\"",
uf->conf->remoteGimpCommand, fullConfFilename);
g_error_free(err);
err = NULL;
/* gimp-remote starts the gimp in a fork().
* Therefore we must call it asynchronously. */
if (!g_spawn_command_line_async(commandLine, &err)) {
g_free(commandLine);
g_free(fullConfFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error activating Gimp."), err->message);
g_error_free(err);
return UFRAW_ERROR;
}
} else
#endif
{
g_free(fullConfFilename);
ufraw_message(UFRAW_ERROR, "%s\n%s",
_("Error activating Gimp."), err->message);
g_error_free(err);
return UFRAW_ERROR;
}
}
g_free(fullConfFilename);
g_free(commandLine);
// Sleep for 0.2 seconds, giving time for gimp-remote to do the X query.
g_usleep(200 * 1000);
return UFRAW_SUCCESS;
}