Statistics
| Revision:

root / src / editvcard.c @ 2080

History | View | Annotate | Download (9 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 2001 Match Grun
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
/*
21 1 hiro
 * Edit vCard address book data.
22 1 hiro
 */
23 1 hiro
24 1 hiro
#ifdef HAVE_CONFIG_H
25 1 hiro
#  include "config.h"
26 1 hiro
#endif
27 1 hiro
28 1 hiro
#include "defs.h"
29 1 hiro
30 1 hiro
#include <glib.h>
31 92 hiro
#include <glib/gi18n.h>
32 1 hiro
#include <gdk/gdkkeysyms.h>
33 1 hiro
#include <gtk/gtkwindow.h>
34 1 hiro
#include <gtk/gtksignal.h>
35 237 hiro
#include <gtk/gtkvbox.h>
36 1 hiro
#include <gtk/gtklabel.h>
37 1 hiro
#include <gtk/gtkentry.h>
38 1 hiro
#include <gtk/gtktable.h>
39 1 hiro
#include <gtk/gtkbutton.h>
40 237 hiro
#include <gtk/gtkstatusbar.h>
41 237 hiro
#include <gtk/gtkhseparator.h>
42 237 hiro
#include <gtk/gtkstock.h>
43 1 hiro
44 1 hiro
#include "addressbook.h"
45 1 hiro
#include "prefs_common.h"
46 1 hiro
#include "addressitem.h"
47 1 hiro
#include "vcard.h"
48 1 hiro
#include "mgutils.h"
49 411 hiro
#include "filesel.h"
50 1 hiro
#include "gtkutils.h"
51 411 hiro
#include "codeconv.h"
52 1 hiro
#include "manage_window.h"
53 1 hiro
54 1 hiro
#define ADDRESSBOOK_GUESS_VCARD  "MyGnomeCard"
55 1 hiro
56 1 hiro
static struct _VCardEdit {
57 1 hiro
        GtkWidget *window;
58 1 hiro
        GtkWidget *name_entry;
59 1 hiro
        GtkWidget *file_entry;
60 405 hiro
        GtkWidget *hbbox;
61 1 hiro
        GtkWidget *ok_btn;
62 1 hiro
        GtkWidget *cancel_btn;
63 1 hiro
        GtkWidget *statusbar;
64 1 hiro
        gint status_cid;
65 1 hiro
} vcardedit;
66 1 hiro
67 1 hiro
/*
68 1 hiro
* Edit functions.
69 1 hiro
*/
70 1 hiro
void edit_vcard_status_show( gchar *msg ) {
71 1 hiro
        if( vcardedit.statusbar != NULL ) {
72 1 hiro
                gtk_statusbar_pop( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid );
73 1 hiro
                if( msg ) {
74 1 hiro
                        gtk_statusbar_push( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid, msg );
75 1 hiro
                }
76 1 hiro
        }
77 1 hiro
}
78 1 hiro
79 1 hiro
static void edit_vcard_ok( GtkWidget *widget, gboolean *cancelled ) {
80 1 hiro
        *cancelled = FALSE;
81 1 hiro
        gtk_main_quit();
82 1 hiro
}
83 1 hiro
84 1 hiro
static void edit_vcard_cancel( GtkWidget *widget, gboolean *cancelled ) {
85 1 hiro
        *cancelled = TRUE;
86 1 hiro
        gtk_main_quit();
87 1 hiro
}
88 1 hiro
89 1 hiro
static void edit_vcard_file_check( void ) {
90 1 hiro
        gint t;
91 1 hiro
        gchar *sFile;
92 411 hiro
        gchar *sFSFile;
93 1 hiro
        gchar *sMsg;
94 1 hiro
95 1 hiro
        sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
96 411 hiro
        sFSFile = conv_filename_from_utf8( sFile );
97 411 hiro
        t = vcard_test_read_file( sFSFile );
98 411 hiro
        g_free( sFSFile );
99 1 hiro
        g_free( sFile );
100 1 hiro
        if( t == MGU_SUCCESS ) {
101 1 hiro
                sMsg = "";
102 1 hiro
        }
103 1 hiro
        else if( t == MGU_BAD_FORMAT ) {
104 1 hiro
                sMsg = _("File does not appear to be vCard format.");
105 1 hiro
        }
106 1 hiro
        else {
107 1 hiro
                sMsg = _("Could not read file.");
108 1 hiro
        }
109 1 hiro
        edit_vcard_status_show( sMsg );
110 1 hiro
}
111 1 hiro
112 1 hiro
static void edit_vcard_file_select( void ) {
113 1 hiro
        gchar *sFile;
114 411 hiro
        gchar *sUTF8File;
115 1 hiro
116 411 hiro
        sFile = filesel_select_file( _("Select vCard File"), NULL, GTK_FILE_CHOOSER_ACTION_OPEN );
117 1 hiro
118 411 hiro
        if( sFile ) {
119 411 hiro
                sUTF8File = conv_filename_to_utf8( sFile );
120 411 hiro
                gtk_entry_set_text( GTK_ENTRY(vcardedit.file_entry), sUTF8File );
121 411 hiro
                g_free( sUTF8File );
122 411 hiro
                g_free( sFile );
123 411 hiro
                edit_vcard_file_check();
124 411 hiro
        }
125 1 hiro
}
126 1 hiro
127 1 hiro
static gint edit_vcard_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
128 1 hiro
        *cancelled = TRUE;
129 1 hiro
        gtk_main_quit();
130 1 hiro
        return TRUE;
131 1 hiro
}
132 1 hiro
133 1 hiro
static gboolean edit_vcard_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
134 1 hiro
        if (event && event->keyval == GDK_Escape) {
135 1 hiro
                *cancelled = TRUE;
136 1 hiro
                gtk_main_quit();
137 1 hiro
        }
138 1 hiro
        return FALSE;
139 1 hiro
}
140 1 hiro
141 1 hiro
static void addressbook_edit_vcard_create( gboolean *cancelled ) {
142 1 hiro
        GtkWidget *window;
143 1 hiro
        GtkWidget *vbox;
144 1 hiro
        GtkWidget *table;
145 1 hiro
        GtkWidget *label;
146 1 hiro
        GtkWidget *name_entry;
147 1 hiro
        GtkWidget *file_entry;
148 1 hiro
        GtkWidget *hbbox;
149 1 hiro
        GtkWidget *hsep;
150 1 hiro
        GtkWidget *ok_btn;
151 1 hiro
        GtkWidget *cancel_btn;
152 1 hiro
        GtkWidget *check_btn;
153 1 hiro
        GtkWidget *file_btn;
154 1 hiro
        GtkWidget *statusbar;
155 1 hiro
        GtkWidget *hsbox;
156 1 hiro
        gint top;
157 1 hiro
158 1 hiro
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
159 1 hiro
        gtk_widget_set_size_request(window, 450, -1);
160 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
161 1 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Edit vCard Entry"));
162 1 hiro
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
163 1 hiro
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
164 1 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
165 1 hiro
                         G_CALLBACK(edit_vcard_delete_event),
166 1 hiro
                         cancelled);
167 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
168 1 hiro
                         G_CALLBACK(edit_vcard_key_pressed),
169 1 hiro
                         cancelled);
170 1 hiro
171 1 hiro
        vbox = gtk_vbox_new(FALSE, 8);
172 1 hiro
        gtk_container_add(GTK_CONTAINER(window), vbox);
173 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
174 1 hiro
175 1 hiro
        table = gtk_table_new(2, 3, FALSE);
176 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
177 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
178 1 hiro
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
179 1 hiro
        gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
180 1 hiro
181 1 hiro
        /* First row */
182 1 hiro
        top = 0;
183 1 hiro
        label = gtk_label_new(_("Name"));
184 1 hiro
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186 1 hiro
187 1 hiro
        name_entry = gtk_entry_new();
188 1 hiro
        gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
189 1 hiro
190 1 hiro
        check_btn = gtk_button_new_with_label( _(" Check File "));
191 1 hiro
        gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
192 1 hiro
193 1 hiro
        /* Second row */
194 1 hiro
        top = 1;
195 1 hiro
        label = gtk_label_new(_("File"));
196 1 hiro
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
197 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
198 1 hiro
199 1 hiro
        file_entry = gtk_entry_new();
200 1 hiro
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
201 1 hiro
202 1 hiro
        file_btn = gtk_button_new_with_label( _(" ... "));
203 1 hiro
        gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
204 1 hiro
205 1 hiro
        /* Status line */
206 1 hiro
        hsbox = gtk_hbox_new(FALSE, 0);
207 1040 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, 0);
208 1 hiro
        statusbar = gtk_statusbar_new();
209 1040 hiro
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, 0);
210 1 hiro
211 1 hiro
        /* Button panel */
212 31 hiro
        gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
213 31 hiro
                                      &cancel_btn, GTK_STOCK_CANCEL,
214 31 hiro
                                      NULL, NULL);
215 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
216 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
217 1 hiro
        gtk_widget_grab_default(ok_btn);
218 1 hiro
219 1 hiro
        hsep = gtk_hseparator_new();
220 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
221 1 hiro
222 1 hiro
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
223 1 hiro
                         G_CALLBACK(edit_vcard_ok), cancelled);
224 1 hiro
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
225 1 hiro
                         G_CALLBACK(edit_vcard_cancel), cancelled);
226 1 hiro
        g_signal_connect(G_OBJECT(file_btn), "clicked",
227 1 hiro
                         G_CALLBACK(edit_vcard_file_select), NULL);
228 1 hiro
        g_signal_connect(G_OBJECT(check_btn), "clicked",
229 1 hiro
                         G_CALLBACK(edit_vcard_file_check), NULL);
230 1 hiro
231 1 hiro
        gtk_widget_show_all(vbox);
232 1 hiro
233 1 hiro
        vcardedit.window     = window;
234 1 hiro
        vcardedit.name_entry = name_entry;
235 1 hiro
        vcardedit.file_entry = file_entry;
236 405 hiro
        vcardedit.hbbox      = hbbox;
237 1 hiro
        vcardedit.ok_btn     = ok_btn;
238 1 hiro
        vcardedit.cancel_btn = cancel_btn;
239 1 hiro
        vcardedit.statusbar  = statusbar;
240 1 hiro
        vcardedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit vCard Dialog" );
241 1 hiro
}
242 1 hiro
243 1 hiro
AdapterDSource *addressbook_edit_vcard( AddressIndex *addrIndex, AdapterDSource *ads ) {
244 1 hiro
        static gboolean cancelled;
245 1 hiro
        gchar *sName;
246 1 hiro
        gchar *sFile;
247 411 hiro
        gchar *sFSFile;
248 1 hiro
        AddressDataSource *ds = NULL;
249 1 hiro
        VCardFile *vcf = NULL;
250 1 hiro
        gboolean fin;
251 1 hiro
252 1 hiro
        if( ! vcardedit.window )
253 1 hiro
                addressbook_edit_vcard_create(&cancelled);
254 405 hiro
        gtkut_box_set_reverse_order(GTK_BOX(vcardedit.hbbox),
255 405 hiro
                                    !prefs_common.comply_gnome_hig);
256 1 hiro
        gtk_widget_grab_focus(vcardedit.ok_btn);
257 1 hiro
        gtk_widget_grab_focus(vcardedit.name_entry);
258 1 hiro
        gtk_widget_show(vcardedit.window);
259 1 hiro
        manage_window_set_transient(GTK_WINDOW(vcardedit.window));
260 1 hiro
261 1 hiro
        edit_vcard_status_show( "" );
262 1 hiro
        if( ads ) {
263 1 hiro
                ds = ads->dataSource;
264 1 hiro
                vcf = ds->rawDataSource;
265 1 hiro
                if (vcf->name)
266 1 hiro
                        gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), vcf->name);
267 1 hiro
                if (vcf->path)
268 1 hiro
                        gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcf->path);
269 1 hiro
                gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Edit vCard Entry"));
270 1 hiro
        }
271 1 hiro
        else {
272 1 hiro
                gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), ADDRESSBOOK_GUESS_VCARD );
273 1 hiro
                gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcard_find_gnomecard() );
274 1 hiro
                gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Add New vCard Entry"));
275 1 hiro
        }
276 1 hiro
277 1 hiro
        gtk_main();
278 1 hiro
        gtk_widget_hide(vcardedit.window);
279 1 hiro
        if (cancelled == TRUE) return NULL;
280 1 hiro
281 1 hiro
        fin = FALSE;
282 1 hiro
        sName = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.name_entry), 0, -1 );
283 1 hiro
        sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
284 411 hiro
        sFSFile = conv_filename_from_utf8( sFile );
285 1 hiro
        if( *sName == '\0' ) fin = TRUE;
286 1 hiro
        if( *sFile == '\0' ) fin = TRUE;
287 411 hiro
        if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
288 1 hiro
289 1 hiro
        if( ! fin ) {
290 1 hiro
                if( ! ads ) {
291 1 hiro
                        vcf = vcard_create();
292 1 hiro
                        ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_VCARD, vcf );
293 1 hiro
                        ads = addressbook_create_ds_adapter( ds, ADDR_VCARD, NULL );
294 1 hiro
                }
295 1 hiro
                addressbook_ads_set_name( ads, sName );
296 1 hiro
                vcard_set_name( vcf, sName );
297 411 hiro
                vcard_set_file( vcf, sFSFile );
298 1 hiro
        }
299 411 hiro
        g_free( sFSFile );
300 1 hiro
        g_free( sFile );
301 411 hiro
        g_free( sName );
302 1 hiro
303 1 hiro
        return ads;
304 1 hiro
}
305 1 hiro
306 1 hiro
/*
307 1 hiro
* End of Source.
308 1 hiro
*/