Statistics
| Revision:

root / src / addressadd.c @ 1

History | View | Annotate | Download (12.2 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 2001 Match Grun
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 */
19

    
20
/*
21
 * Add address to address book dialog.
22
 */
23

    
24
#ifdef HAVE_CONFIG_H
25
#  include "config.h"
26
#endif
27

    
28
#include "defs.h"
29

    
30
#include <glib.h>
31
#include <gdk/gdkkeysyms.h>
32
#include <gtk/gtkwindow.h>
33
#include <gtk/gtksignal.h>
34
#include <gtk/gtkhbox.h>
35
#include <gtk/gtklabel.h>
36
#include <gtk/gtkentry.h>
37
#include <gtk/gtkhbbox.h>
38
#include <gtk/gtkbutton.h>
39

    
40
#include "intl.h"
41
#include "gtkutils.h"
42
#include "stock_pixmap.h"
43
#include "prefs_common.h"
44
#include "addressadd.h"
45
#include "addritem.h"
46
#include "addrbook.h"
47
#include "addrindex.h"
48
#include "manage_window.h"
49

    
50
typedef struct {
51
        AddressBookFile        *book;
52
        ItemFolder        *folder;
53
} FolderInfo;
54

    
55
static struct _AddressAdd_dlg {
56
        GtkWidget *window;
57
        GtkWidget *label_name;
58
        GtkWidget *label_address;
59
        GtkWidget *label_remarks;
60
        GtkWidget *tree_folder;
61
        GtkWidget *ok_btn;
62
        GtkWidget *cancel_btn;
63
        GtkWidget *statusbar;
64
        gint status_cid;
65
        FolderInfo *fiSelected;
66
} addressadd_dlg;
67

    
68
static GdkPixmap *folderXpm;
69
static GdkBitmap *folderXpmMask;
70
static GdkPixmap *bookXpm;
71
static GdkBitmap *bookXpmMask;
72

    
73
static gboolean addressadd_cancelled;
74

    
75
static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
76
{
77
        FolderInfo *fi = g_new0( FolderInfo, 1 );
78
        fi->book   = abf;
79
        fi->folder = folder;
80
        return fi;
81
}
82

    
83
static void addressadd_free_folderinfo( FolderInfo *fi ) {
84
        fi->book   = NULL;
85
        fi->folder = NULL;
86
        g_free( fi );
87
}
88

    
89
/*
90
* Edit functions.
91
*/
92
static void addressadd_status_show( gchar *msg ) {
93
        if( addressadd_dlg.statusbar != NULL ) {
94
                gtk_statusbar_pop( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid );
95
                if( msg ) {
96
                        gtk_statusbar_push( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid, msg );
97
                }
98
        }
99
}
100

    
101
static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
102
        addressadd_cancelled = TRUE;
103
        gtk_main_quit();
104
        return TRUE;
105
}
106

    
107
static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
108
        if (event && event->keyval == GDK_Escape) {
109
                addressadd_cancelled = TRUE;
110
                gtk_main_quit();
111
        }
112
        return FALSE;
113
}
114

    
115
static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
116
        addressadd_cancelled = FALSE;
117
        gtk_main_quit();
118
}
119

    
120
static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
121
        addressadd_cancelled = TRUE;
122
        gtk_main_quit();
123
}
124

    
125
static void addressadd_folder_select( GtkCTree *ctree, gint row, gint column,
126
                                        GdkEvent *event, gpointer data )
127
{
128
        addressadd_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
129
}
130

    
131
static gboolean addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
132
        if( ! event ) return FALSE;
133
        if( event->button == 1 ) {
134
                /* Handle double click */
135
                if( event->type == GDK_2BUTTON_PRESS ) {
136
                        addressadd_cancelled = FALSE;
137
                        gtk_main_quit();
138
                }
139
        }
140

    
141
        return FALSE;
142
}
143

    
144
static void addressadd_create( void ) {
145
        GtkWidget *window;
146
        GtkWidget *vbox;
147
        GtkWidget *table;
148
        GtkWidget *label;
149
        GtkWidget *label_name;
150
        GtkWidget *label_addr;
151
        GtkWidget *label_rems;
152
        GtkWidget *tree_folder;
153
        GtkWidget *vlbox;
154
        GtkWidget *tree_win;
155
        GtkWidget *hbbox;
156
        GtkWidget *hsep;
157
        GtkWidget *ok_btn;
158
        GtkWidget *cancel_btn;
159
        GtkWidget *hsbox;
160
        GtkWidget *statusbar;
161
        gint top;
162

    
163
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
164
        gtk_widget_set_size_request( window, 300, 400 );
165
        gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
166
        gtk_window_set_title( GTK_WINDOW(window), _("Add Address to Book") );
167
        gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
168
        gtk_window_set_modal( GTK_WINDOW(window), TRUE );
169
        g_signal_connect( G_OBJECT(window), "delete_event",
170
                          G_CALLBACK(addressadd_delete_event), NULL );
171
        g_signal_connect( G_OBJECT(window), "key_press_event",
172
                          G_CALLBACK(addressadd_key_pressed), NULL );
173

    
174
        vbox = gtk_vbox_new(FALSE, 8);
175
        gtk_container_add(GTK_CONTAINER(window), vbox);
176
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
177

    
178
        table = gtk_table_new(3, 2, FALSE);
179
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
180
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
181
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
182
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
183

    
184
        /* First row */
185
        top = 0;
186
        label = gtk_label_new(_("Name"));
187
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
188
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
189

    
190
        label_name = gtk_label_new("");
191
        gtk_table_attach(GTK_TABLE(table), label_name, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
192
        gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
193

    
194
        /* Second row */
195
        top = 1;
196
        label = gtk_label_new(_("Address"));
197
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
198
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
199

    
200
        label_addr = gtk_label_new("");
201
        gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
202
        gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
203

    
204
        /* Third row */
205
        top = 2;
206
        label = gtk_label_new(_("Remarks"));
207
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
208
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
209

    
210
        label_rems = gtk_label_new("");
211
        gtk_table_attach(GTK_TABLE(table), label_rems, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
212
        gtk_misc_set_alignment(GTK_MISC(label_rems), 0, 0.5);
213

    
214
        /* Address book/folder tree */
215
        vlbox = gtk_vbox_new(FALSE, 8);
216
        gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
217
        gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
218

    
219
        tree_win = gtk_scrolled_window_new( NULL, NULL );
220
        gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
221
                                        GTK_POLICY_AUTOMATIC,
222
                                        GTK_POLICY_ALWAYS );
223
        gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
224

    
225
        tree_folder = gtk_ctree_new( 1, 0 );
226
        gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
227
        gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
228
        gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
229
        gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
230
        gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
231
        gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
232
        gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
233
        gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
234

    
235
        /* Status line */
236
        hsbox = gtk_hbox_new(FALSE, 0);
237
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
238
        statusbar = gtk_statusbar_new();
239
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
240

    
241
        /* Button panel */
242
        gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
243
                                &cancel_btn, _("Cancel"), NULL, NULL);
244
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
245
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
246
        gtk_widget_grab_default(ok_btn);
247

    
248
        hsep = gtk_hseparator_new();
249
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
250

    
251
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
252
                         G_CALLBACK(addressadd_ok), NULL);
253
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
254
                         G_CALLBACK(addressadd_cancel), NULL);
255
        g_signal_connect(G_OBJECT(tree_folder), "select_row",
256
                         G_CALLBACK(addressadd_folder_select), NULL);
257
        g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
258
                         G_CALLBACK(addressadd_tree_button), NULL);
259

    
260
        gtk_widget_show_all(vbox);
261

    
262
        addressadd_dlg.window        = window;
263
        addressadd_dlg.label_name    = label_name;
264
        addressadd_dlg.label_address = label_addr;
265
        addressadd_dlg.label_remarks = label_rems;
266
        addressadd_dlg.tree_folder   = tree_folder;
267
        addressadd_dlg.ok_btn        = ok_btn;
268
        addressadd_dlg.cancel_btn    = cancel_btn;
269
        addressadd_dlg.statusbar     = statusbar;
270
        addressadd_dlg.status_cid    = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Address Add" );
271

    
272
        gtk_widget_show_all( window );
273

    
274
        stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
275
        stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
276
                          &folderXpm, &folderXpmMask );
277
}
278

    
279
static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
280
                                        FolderInfo *fiParent )
281
{
282
        GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
283
        GList *list;
284
        ItemFolder *folder;
285
        gchar *fName;
286
        gchar **name;
287
        GtkCTreeNode *node;
288
        FolderInfo *fi;
289

    
290
        list = parentFolder->listFolder;
291
        while( list ) {
292
                folder = list->data;
293
                fName = g_strdup( ADDRITEM_NAME(folder) );
294
                name = &fName;
295
                node = gtk_ctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
296
                                folderXpm, folderXpmMask, folderXpm, folderXpmMask,
297
                                FALSE, TRUE );
298
                g_free( fName );
299
                fi = addressadd_create_folderinfo( fiParent->book, folder );
300
                gtk_ctree_node_set_row_data_full( tree, node, fi,
301
                                ( GtkDestroyNotify ) addressadd_free_folderinfo );
302
                addressadd_load_folder( node, folder, fi );
303
                list = g_list_next( list );
304
        }
305
}
306

    
307
static void addressadd_load_data( AddressIndex *addrIndex ) {
308
        AddressDataSource *ds;
309
        GList *list, *nodeDS;
310
        gchar **name;
311
        gchar *dsName;
312
        ItemFolder *rootFolder;
313
        AddressBookFile *abf;
314
        FolderInfo *fi;
315
        GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
316
        GtkCTreeNode *node;
317

    
318
        gtk_clist_clear( GTK_CLIST( tree ) );
319
        list = addrindex_get_interface_list( addrIndex );
320
        while( list ) {
321
                AddressInterface *interface = list->data;
322
                if( interface->type == ADDR_IF_BOOK ) {
323
                        nodeDS = interface->listSource;
324
                        while( nodeDS ) {
325
                                ds = nodeDS->data;
326
                                dsName = g_strdup( addrindex_ds_get_name( ds ) );
327

    
328
                                /* Read address book */
329
                                if( ! addrindex_ds_get_read_flag( ds ) ) {
330
                                        addrindex_ds_read_data( ds );
331
                                }
332

    
333
                                /* Add node for address book */
334
                                abf = ds->rawDataSource;
335
                                name = &dsName;
336
                                node = gtk_ctree_insert_node( tree, NULL, NULL,
337
                                                name, FOLDER_SPACING, bookXpm,
338
                                                bookXpmMask, bookXpm, bookXpmMask,
339
                                                FALSE, TRUE );
340
                                g_free( dsName );
341

    
342
                                fi = addressadd_create_folderinfo( abf, NULL );
343
                                gtk_ctree_node_set_row_data_full( tree, node, fi,
344
                                                ( GtkDestroyNotify ) addressadd_free_folderinfo );
345

    
346
                                rootFolder = addrindex_ds_get_root_folder( ds );
347
                                addressadd_load_folder( node, rootFolder, fi );
348

    
349
                                nodeDS = g_list_next( nodeDS );
350
                        }
351
                }
352
                list = g_list_next( list );
353
        }
354
}
355

    
356
gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks ) {
357
        gboolean retVal = FALSE;
358
        ItemPerson *person = NULL;
359

    
360
        addressadd_cancelled = FALSE;
361
        if( ! addressadd_dlg.window ) addressadd_create();
362
        gtk_widget_grab_focus(addressadd_dlg.ok_btn);
363
        gtk_widget_show(addressadd_dlg.window);
364
        manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
365

    
366
        addressadd_dlg.fiSelected = NULL;
367
        addressadd_status_show( "" );
368
        addressadd_load_data( addrIndex );
369
        gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
370
        gtk_widget_show(addressadd_dlg.window);
371

    
372
        gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_name ), "" );
373
        gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
374
        gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_remarks ), "" );
375
        if( name )
376
                gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_name ), name );
377
        if( address )
378
                gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
379
        if( remarks )
380
                gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_remarks ), remarks );
381

    
382
        gtk_main();
383
        gtk_widget_hide( addressadd_dlg.window );
384

    
385
        if( ! addressadd_cancelled ) {
386
                if( addressadd_dlg.fiSelected ) {
387
                        FolderInfo *fi = addressadd_dlg.fiSelected;
388
                        person = addrbook_add_contact( fi->book, fi->folder, name, address, remarks );
389
                        if( person ) retVal = TRUE;
390
                }
391
        }
392

    
393
        gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
394

    
395
        return retVal;
396
}
397

    
398
/*
399
* End of Source.
400
*/
401