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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-contact-editor.h
* Copyright (C) 2000 Ximian, Inc.
* Author: Chris Lahey <clahey@ximian.com>
*
* This library 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.
*
* 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 library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include "e-contact-save-as.h"
#include <unistd.h>
#include <fcntl.h>
#include <gtk/gtkfilesel.h>
#include <gtk/gtksignal.h>
#include <gtk/gtk.h>
#include <libgnomeui/gnome-dialog.h>
#include <gal/util/e-util.h>
#include <libgnome/gnome-i18n.h>
#include <errno.h>
#include <string.h>
#include <libgnomeui/gnome-messagebox.h>
#include <libgnomeui/gnome-stock.h>
static int file_exists(GtkFileSelection *filesel, const char *filename);
typedef struct {
GtkFileSelection *filesel;
char *vcard;
} SaveAsInfo;
static void
save_it(GtkWidget *widget, SaveAsInfo *info)
{
gint error = 0;
gint response = 0;
const char *filename = gtk_file_selection_get_filename (info->filesel);
error = e_write_file (filename, info->vcard, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC);
if (error == EEXIST) {
response = file_exists(info->filesel, filename);
switch (response) {
case 0 : /* Overwrite */
e_write_file(filename, info->vcard, O_WRONLY | O_CREAT | O_TRUNC);
break;
case 1 : /* cancel */
return;
}
} else if (error != 0) {
GtkWidget *dialog;
char *str;
str = g_strdup_printf ("Error saving %s: %s", filename, strerror(errno));
dialog = gnome_message_box_new (str, GNOME_MESSAGE_BOX_ERROR, GNOME_STOCK_BUTTON_OK, NULL);
g_free (str);
gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (info->filesel));
gtk_widget_show (dialog);
return;
}
g_free (info->vcard);
gtk_widget_destroy(GTK_WIDGET(info->filesel));
g_free(info);
}
static void
close_it(GtkWidget *widget, SaveAsInfo *info)
{
g_free (info->vcard);
gtk_widget_destroy (GTK_WIDGET (info->filesel));
g_free (info);
}
static void
delete_it(GtkWidget *widget, SaveAsInfo *info)
{
g_free (info->vcard);
g_free (info);
}
static char *
make_safe_filename (const char *prefix, char *name)
{
char *safe, *p;
if (!name) {
/* This is a filename. Translators take note. */
name = _("card.vcf");
}
p = strrchr (name, '/');
if (p)
safe = g_strdup_printf ("%s%s%s", prefix, p, ".vcf");
else
safe = g_strdup_printf ("%s/%s%s", prefix, name, ".vcf");
p = strrchr (safe, '/') + 1;
if (p)
e_filename_make_safe (p);
return safe;
}
void
e_contact_save_as(char *title, ECard *card)
{
GtkFileSelection *filesel;
char *file;
char *name;
SaveAsInfo *info = g_new(SaveAsInfo, 1);
filesel = GTK_FILE_SELECTION(gtk_file_selection_new(title));
gtk_object_get (GTK_OBJECT (card),
"file_as", &name,
NULL);
file = make_safe_filename (g_get_home_dir(), name);
gtk_file_selection_set_filename (filesel, file);
g_free (file);
info->filesel = filesel;
info->vcard = e_card_get_vcard(card);
gtk_signal_connect(GTK_OBJECT(filesel->ok_button), "clicked",
save_it, info);
gtk_signal_connect(GTK_OBJECT(filesel->cancel_button), "clicked",
close_it, info);
gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
delete_it, info);
gtk_widget_show(GTK_WIDGET(filesel));
}
void
e_contact_list_save_as(char *title, GList *list)
{
GtkFileSelection *filesel;
SaveAsInfo *info = g_new(SaveAsInfo, 1);
filesel = GTK_FILE_SELECTION(gtk_file_selection_new(title));
/* This is a filename. Translators take note. */
if (list && list->data && list->next == NULL) {
char *name, *file;
gtk_object_get (GTK_OBJECT (list->data),
"file_as", &name,
NULL);
file = make_safe_filename (g_get_home_dir(), name);
gtk_file_selection_set_filename (filesel, file);
g_free (file);
} else {
char *file;
file = make_safe_filename (g_get_home_dir(), _("list"));
gtk_file_selection_set_filename (filesel, file);
g_free (file);
}
info->filesel = filesel;
info->vcard = e_card_list_get_vcard (list);
gtk_signal_connect(GTK_OBJECT(filesel->ok_button), "clicked",
save_it, info);
gtk_signal_connect(GTK_OBJECT(filesel->cancel_button), "clicked",
close_it, info);
gtk_signal_connect(GTK_OBJECT(filesel), "delete_event",
delete_it, info);
gtk_widget_show(GTK_WIDGET(filesel));
}
static int
file_exists(GtkFileSelection *filesel, const char *filename)
{
GnomeDialog *dialog = NULL;
GtkWidget *label;
GladeXML *gui = NULL;
int result = 0;
char *string;
gui = glade_xml_new (EVOLUTION_GLADEDIR "/file-exists.glade", NULL);
dialog = GNOME_DIALOG(glade_xml_get_widget(gui, "dialog-exists"));
label = glade_xml_get_widget (gui, "label-exists");
if (GTK_IS_LABEL (label)) {
string = g_strdup_printf (_("%s already exists\nDo you want to overwrite it?"), filename);
gtk_label_set_text (GTK_LABEL (label), string);
g_free (string);
}
gnome_dialog_set_parent(dialog, GTK_WINDOW(filesel));
gtk_widget_show (GTK_WIDGET (dialog));
result = gnome_dialog_run_and_close(dialog);
g_free(gui);
return result;
}
|