-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathufraw_delete.c
119 lines (110 loc) · 4.56 KB
/
ufraw_delete.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
/*
* UFRaw - Unidentified Flying Raw converter for digital camera images
*
* ufraw_delete.c - The GUI for choosing what files to delete.
* 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 <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
// Response can be any unique positive integer
#define UFRAW_RESPONSE_DELETE_SELECTED 1
#define UFRAW_RESPONSE_DELETE_ALL 2
long ufraw_delete(void *widget, ufraw_data *uf)
{
if (!g_file_test(uf->filename, G_FILE_TEST_EXISTS)) {
char *ufile = g_filename_display_name(uf->filename);
ufraw_message(UFRAW_ERROR, _("Raw file '%s' missing."), ufile);
g_free(ufile);
return UFRAW_ERROR;
}
GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new_with_buttons(
_("Delete raw file"), GTK_WINDOW(gtk_widget_get_toplevel(widget)),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
_("_Delete selected"), UFRAW_RESPONSE_DELETE_SELECTED,
_("Delete _All"), UFRAW_RESPONSE_DELETE_ALL, NULL));
gtk_dialog_set_default_response(dialog, GTK_RESPONSE_CANCEL);
GtkBox *box = GTK_BOX(dialog->vbox);
GtkWidget *label = gtk_label_new(_("Select files to delete"));
gtk_box_pack_start(box, label, FALSE, FALSE, 0);
char *path = g_path_get_dirname(uf->filename);
GDir *dir = g_dir_open(path, 0, NULL);
if (dir == NULL) {
char *upath = g_filename_display_name(path);
ufraw_message(UFRAW_ERROR, _("Error reading directory '%s'."), upath);
g_free(upath);
g_free(path);
return UFRAW_ERROR;
}
g_free(path);
char *dirname = g_path_get_dirname(uf->filename);
char *base = g_path_get_basename(uf->filename);
if (strcmp(base, uf->filename) == 0) {
// Handle empty dirname
g_free(dirname);
dirname = g_strdup("");
}
char *rawBase = uf_file_set_type(base, ".");
int rawBaseLen = strlen(rawBase);
GList *buttonList = NULL;
GList *fileList = NULL;
// Check button for raw file
char *ufile = g_filename_display_name(uf->filename);
GtkWidget *button = gtk_check_button_new_with_label(ufile);
buttonList = g_list_append(buttonList, button);
g_free(ufile);
gtk_box_pack_start(box, button, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
fileList = g_list_append(fileList, g_strdup(uf->filename));
// Check buttons for associated files
const char *file;
while ((file = g_dir_read_name(dir)) != NULL) {
if (strncmp(file, rawBase, rawBaseLen) == 0 &&
strcmp(file, base) != 0) {
char *filename = g_build_filename(dirname, file, NULL);
char *ufile = g_filename_display_name(filename);
button = gtk_check_button_new_with_label(ufile);
buttonList = g_list_append(buttonList, button);
g_free(ufile);
gtk_box_pack_start(box, button, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
fileList = g_list_append(fileList, filename);
}
}
g_dir_close(dir);
gtk_widget_show_all(GTK_WIDGET(dialog));
long response = gtk_dialog_run(dialog);
long status = UFRAW_CANCEL;
/* Delete files as needed */
while (fileList != NULL) {
if (response == UFRAW_RESPONSE_DELETE_ALL ||
(response == UFRAW_RESPONSE_DELETE_SELECTED &&
gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(buttonList->data)))) {
if (g_unlink(fileList->data) != 0) {
char *ufile = g_filename_display_name(fileList->data);
ufraw_message(UFRAW_ERROR, _("Error deleting '%s'"), ufile);
g_free(ufile);
} else if (strcmp(fileList->data, uf->filename) == 0) {
/* Success means deleting the raw file */
status = UFRAW_SUCCESS;
}
}
g_free(fileList->data);
fileList = g_list_next(fileList);
buttonList = g_list_next(buttonList);
}
g_list_free(fileList);
g_list_free(buttonList);
g_free(base);
g_free(dirname);
gtk_widget_destroy(GTK_WIDGET(dialog));
return status;
}