2005-07-15 [colin] 1.9.12cvs60
[claws.git] / src / addressbook.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkscrolledwindow.h>
33 #include <gtk/gtkhpaned.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkctree.h>
38 #include <gtk/gtkclist.h>
39 #include <gtk/gtktable.h>
40 #include <gtk/gtkhbbox.h>
41 #include <gtk/gtkbutton.h>
42 #include <gtk/gtkmenu.h>
43 #include <gtk/gtkmenuitem.h>
44 #include <gtk/gtkitemfactory.h>
45 #include <string.h>
46 #include <setjmp.h>
47
48 #include "main.h"
49 #include "addressbook.h"
50 #include "manage_window.h"
51 #include "prefs_common.h"
52 #include "alertpanel.h"
53 #include "inputdialog.h"
54 #include "menu.h"
55 #include "stock_pixmap.h"
56 #include "xml.h"
57 #include "prefs_gtk.h"
58 #include "procmime.h"
59 #include "utils.h"
60 #include "gtkutils.h"
61 #include "codeconv.h"
62 #include "about.h"
63 #include "addr_compl.h"
64
65 #include "mgutils.h"
66 #include "addressitem.h"
67 #include "addritem.h"
68 #include "addrcache.h"
69 #include "addrbook.h"
70 #include "addrindex.h"
71 #include "addressadd.h"
72 #include "vcard.h"
73 #include "editvcard.h"
74 #include "editgroup.h"
75 #include "editaddress.h"
76 #include "editbook.h"
77 #include "importldif.h"
78 #include "importmutt.h"
79 #include "importpine.h"
80
81 #ifdef USE_JPILOT
82 #include "jpilot.h"
83 #include "editjpilot.h"
84 #endif
85
86 #ifdef USE_LDAP
87 #include <pthread.h>
88 #include "ldapserver.h"
89 #include "editldap.h"
90
91 #define ADDRESSBOOK_LDAP_BUSYMSG "Busy"
92 #endif
93
94 #include "addrquery.h"
95 #include "addrselect.h"
96 #include "addrclip.h"
97 #include "addrgather.h"
98 #include "adbookbase.h"
99 #include "exphtmldlg.h"
100 #include "expldifdlg.h"
101 #include "browseldap.h"
102
103 typedef enum
104 {
105         COL_NAME        = 0,
106         COL_ADDRESS     = 1,
107         COL_REMARKS     = 2
108 } AddressBookColumnPos;
109
110 #define N_COLS  3
111 #define COL_NAME_WIDTH          164
112 #define COL_ADDRESS_WIDTH       156
113
114 #define COL_FOLDER_WIDTH        170
115 #define ADDRESSBOOK_WIDTH       640
116 #define ADDRESSBOOK_HEIGHT      360
117
118 #define ADDRESSBOOK_MSGBUF_SIZE 2048
119
120 static GdkPixmap *folderxpm;
121 static GdkBitmap *folderxpmmask;
122 static GdkPixmap *folderopenxpm;
123 static GdkBitmap *folderopenxpmmask;
124 static GdkPixmap *groupxpm;
125 static GdkBitmap *groupxpmmask;
126 static GdkPixmap *interfacexpm;
127 static GdkBitmap *interfacexpmmask;
128 static GdkPixmap *bookxpm;
129 static GdkBitmap *bookxpmmask;
130 static GdkPixmap *addressxpm;
131 static GdkBitmap *addressxpmmask;
132 static GdkPixmap *vcardxpm;
133 static GdkBitmap *vcardxpmmask;
134 static GdkPixmap *jpilotxpm;
135 static GdkBitmap *jpilotxpmmask;
136 static GdkPixmap *categoryxpm;
137 static GdkBitmap *categoryxpmmask;
138 static GdkPixmap *ldapxpm;
139 static GdkBitmap *ldapxpmmask;
140 static GdkPixmap *addrsearchxpm;
141 static GdkPixmap *addrsearchxpmmask;
142
143 /* Message buffer */
144 static gchar addressbook_msgbuf[ ADDRESSBOOK_MSGBUF_SIZE ];
145
146 /* Address list selection */
147 static AddrSelectList *_addressSelect_ = NULL;
148 static AddressClipboard *_clipBoard_ = NULL;
149
150 /* Address index file and interfaces */
151 static AddressIndex *_addressIndex_ = NULL;
152 static GList *_addressInterfaceList_ = NULL;
153 static GList *_addressIFaceSelection_ = NULL;
154 #define ADDRESSBOOK_IFACE_SELECTION "1/y,3/y,4/y,2/n"
155
156 static AddressBook_win addrbook;
157
158 static GHashTable *_addressBookTypeHash_ = NULL;
159 static GList *_addressBookTypeList_ = NULL;
160
161 static void addressbook_create                  (void);
162 static gint addressbook_close                   (void);
163 static void addressbook_button_set_sensitive    (void);
164
165 /* callback functions */
166 static void addressbook_del_clicked             (GtkButton      *button,
167                                                  gpointer        data);
168 static void addressbook_reg_clicked             (GtkButton      *button,
169                                                  gpointer        data);
170 static void addressbook_to_clicked              (GtkButton      *button,
171                                                  gpointer        data);
172 static void addressbook_lup_clicked             (GtkButton      *button,
173                                                  gpointer       data);
174
175 static gboolean addressbook_tree_selected       (GtkCTree       *ctree,
176                                                  GtkCTreeNode   *node,
177                                                  gint            column,
178                                                  gpointer        data);
179 static void addressbook_select_row_tree         (GtkCTree       *ctree,
180                                                  GtkCTreeNode   *node,
181                                                  gint            column,
182                                                  gpointer        data);
183 static void addressbook_list_row_selected       (GtkCTree       *clist,
184                                                  GtkCTreeNode   *node,
185                                                  gint            column,
186                                                  gpointer        data);
187 static void addressbook_list_row_unselected     (GtkCTree       *clist,
188                                                  GtkCTreeNode   *node,
189                                                  gint            column,
190                                                  gpointer        data);
191 static void addressbook_person_expand_node      (GtkCTree       *ctree,
192                                                  GList          *node,
193                                                  gpointer       *data );
194 static void addressbook_person_collapse_node    (GtkCTree       *ctree,
195                                                  GList          *node,
196                                                  gpointer       *data );
197 static void addressbook_entry_gotfocus          (GtkWidget      *widget);
198
199 #if 0
200 static void addressbook_entry_changed           (GtkWidget      *widget);
201 #endif
202
203 static gboolean addressbook_list_button_pressed (GtkWidget      *widget,
204                                                  GdkEventButton *event,
205                                                  gpointer        data);
206 static gboolean addressbook_list_button_released(GtkWidget      *widget,
207                                                  GdkEventButton *event,
208                                                  gpointer        data);
209 static gboolean addressbook_tree_button_pressed (GtkWidget      *ctree,
210                                                  GdkEventButton *event,
211                                                  gpointer        data);
212 static gboolean addressbook_tree_button_released(GtkWidget      *ctree,
213                                                  GdkEventButton *event,
214                                                  gpointer        data);
215 static void addressbook_popup_close             (GtkMenuShell   *menu_shell,
216                                                  gpointer        data);
217
218 static void addressbook_new_folder_cb           (gpointer        data,
219                                                  guint           action,
220                                                  GtkWidget      *widget);
221 static void addressbook_new_group_cb            (gpointer        data,
222                                                  guint           action,
223                                                  GtkWidget      *widget);
224 static void addressbook_treenode_edit_cb        (gpointer        data,
225                                                  guint           action,
226                                                  GtkWidget      *widget);
227 static void addressbook_treenode_delete_cb      (gpointer        data,
228                                                  guint           action,
229                                                  GtkWidget      *widget);
230
231 static void addressbook_change_node_name        (GtkCTreeNode   *node,
232                                                  const gchar    *name);
233
234 static void addressbook_new_address_cb          (gpointer        data,
235                                                  guint           action,
236                                                  GtkWidget      *widget);
237 static void addressbook_edit_address_cb         (gpointer        data,
238                                                  guint           action,
239                                                  GtkWidget      *widget);
240 static void addressbook_delete_address_cb       (gpointer        data,
241                                                  guint           action,
242                                                  GtkWidget      *widget);
243
244 static void close_cb                            (gpointer        data,
245                                                  guint           action,
246                                                  GtkWidget      *widget);
247 static void addressbook_file_save_cb            (gpointer        data,
248                                                  guint           action,
249                                                  GtkWidget      *widget);
250
251 /* Data source edit stuff */
252 static void addressbook_new_book_cb             (gpointer        data,
253                                                  guint           action,
254                                                  GtkWidget      *widget);
255 static void addressbook_new_vcard_cb            (gpointer        data,
256                                                  guint           action,
257                                                  GtkWidget      *widget);
258
259 #ifdef USE_JPILOT
260 static void addressbook_new_jpilot_cb           (gpointer        data,
261                                                  guint           action,
262                                                  GtkWidget      *widget);
263 #endif
264
265 #ifdef USE_LDAP
266 static void addressbook_new_ldap_cb             (gpointer        data,
267                                                  guint           action,
268                                                  GtkWidget      *widget);
269 #endif
270
271 static void addressbook_set_clist               (AddressObject  *obj);
272
273 static void addressbook_load_tree               (void);
274 void addressbook_read_file                      (void);
275
276 static GtkCTreeNode *addressbook_add_object     (GtkCTreeNode   *node,
277                                                  AddressObject  *obj);
278 static void addressbook_treenode_remove_item    ( void );
279
280 static AddressDataSource *addressbook_find_datasource
281                                                 (GtkCTreeNode   *node );
282
283 static AddressBookFile *addressbook_get_book_file(void);
284
285 static GtkCTreeNode *addressbook_node_add_folder
286                                                 (GtkCTreeNode   *node,
287                                                 AddressDataSource *ds,
288                                                 ItemFolder      *itemFolder,
289                                                 AddressObjectType otype);
290 static GtkCTreeNode *addressbook_node_add_group (GtkCTreeNode   *node,
291                                                 AddressDataSource *ds,
292                                                 ItemGroup       *itemGroup);
293 static void addressbook_tree_remove_children    (GtkCTree       *ctree,
294                                                 GtkCTreeNode    *parent);
295 static void addressbook_move_nodes_up           (GtkCTree       *ctree,
296                                                 GtkCTreeNode    *node);
297 static GtkCTreeNode *addressbook_find_group_node (GtkCTreeNode  *parent,
298                                                    ItemGroup    *group);
299 static gboolean key_pressed                     (GtkWidget      *widget,
300                                                  GdkEventKey    *event,
301                                                  gpointer        data);
302 static gint addressbook_treenode_compare_func   (GtkCList       *clist,
303                                                  gconstpointer   ptr1,
304                                                  gconstpointer   ptr2);
305 static gint addressbook_list_compare_func       (GtkCList       *clist,
306                                                  gconstpointer   ptr1,
307                                                  gconstpointer   ptr2);
308 static void addressbook_folder_load_one_person  (GtkCTree *clist, 
309                                                  ItemPerson *person,  
310                                                  AddressTypeControlItem *atci, 
311                                                  AddressTypeControlItem *atciMail);
312 static void addressbook_folder_refresh_one_person(GtkCTree *clist, 
313                                                   ItemPerson *person);
314 static void addressbook_folder_remove_one_person(GtkCTree *clist, 
315                                                  ItemPerson *person);
316 static void addressbook_folder_remove_node      (GtkCTree *clist, 
317                                                  GtkCTreeNode *node);
318
319 #ifdef USE_LDAP
320 static void addressbook_ldap_show_message       ( LdapServer *server );
321 #endif
322
323 /* LUT's and IF stuff */
324 static void addressbook_free_treenode           ( gpointer data );
325 AddressTypeControlItem *addrbookctl_lookup      (gint            ot);
326 AddressTypeControlItem *addrbookctl_lookup_iface(AddressIfType   ifType);
327
328 void addrbookctl_build_map                      (GtkWidget      *window);
329 void addrbookctl_build_iflist                   (void);
330 AdapterInterface *addrbookctl_find_interface    (AddressIfType   ifType);
331 void addrbookctl_build_ifselect                 (void);
332
333 static void addrbookctl_free_interface          (AdapterInterface *adapter);
334 static void addrbookctl_free_datasource         (AdapterDSource   *adapter);
335 static void addrbookctl_free_folder             (AdapterFolder    *adapter);
336 static void addrbookctl_free_group              (AdapterGroup     *adapter);
337
338 static void addressbook_list_select_clear       ( void );
339 static void addressbook_list_select_add         ( AddrItemObject    *aio,
340                                                   AddressDataSource *ds );
341 static void addressbook_list_select_remove      ( AddrItemObject    *aio );
342
343 static void addressbook_import_ldif_cb          ( void );
344 static void addressbook_import_mutt_cb          ( void );
345 static void addressbook_import_pine_cb          ( void );
346 static void addressbook_export_html_cb          ( void );
347 static void addressbook_export_ldif_cb          ( void );
348 static void addressbook_clip_cut_cb             ( void );
349 static void addressbook_clip_copy_cb            ( void );
350 static void addressbook_clip_paste_cb           ( void );
351 static void addressbook_clip_paste_address_cb   ( void );
352 static void addressbook_treenode_cut_cb         ( void );
353 static void addressbook_treenode_copy_cb        ( void );
354 static void addressbook_treenode_paste_cb       ( void );
355
356 static void addressbook_mail_to_cb              ( void );
357
358 #ifdef USE_LDAP
359 static void addressbook_browse_entry_cb         ( void );
360 #endif
361
362 static void addressbook_start_drag(GtkWidget *widget, gint button, 
363                                    GdkEvent *event,
364                                    void *data);
365 static void addressbook_drag_data_get(GtkWidget        *widget,
366                                      GdkDragContext   *drag_context,
367                                      GtkSelectionData *selection_data,
368                                      guint             info,
369                                      guint             time,
370                                      void             *data);
371 static gboolean addressbook_drag_motion_cb(GtkWidget      *widget,
372                                           GdkDragContext *context,
373                                           gint            x,
374                                           gint            y,
375                                           guint           time,
376                                           void           *data);
377 static void addressbook_drag_leave_cb(GtkWidget      *widget,
378                                      GdkDragContext *context,
379                                      guint           time,
380                                      void           *data);
381 static void addressbook_drag_received_cb(GtkWidget        *widget,
382                                         GdkDragContext   *drag_context,
383                                         gint              x,
384                                         gint              y,
385                                         GtkSelectionData *data,
386                                         guint             info,
387                                         guint             time,
388                                         void             *pdata);
389
390 static GtkTargetEntry addressbook_drag_types[] =
391 {
392         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
393 };
394
395 static GtkTargetList *addressbook_target_list = NULL;
396
397
398 static GtkItemFactoryEntry addressbook_entries[] =
399 {
400         {N_("/_File"),                  NULL,           NULL, 0, "<Branch>"},
401         {N_("/_File/New _Book"),        "<alt>B",       addressbook_new_book_cb,        0, NULL},
402         {N_("/_File/New _vCard"),       "<alt>D",       addressbook_new_vcard_cb,       0, NULL},
403 #ifdef USE_JPILOT
404         {N_("/_File/New _JPilot"),      "<alt>J",       addressbook_new_jpilot_cb,      0, NULL},
405 #endif
406 #ifdef USE_LDAP
407         {N_("/_File/New _Server"),      "<alt>S",       addressbook_new_ldap_cb,        0, NULL},
408 #endif
409         {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
410         {N_("/_File/_Edit"),            NULL,           addressbook_treenode_edit_cb,   0, NULL},
411         {N_("/_File/_Delete"),          NULL,           addressbook_treenode_delete_cb, 0, NULL},
412         {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
413         {N_("/_File/_Save"),            "<alt>S",       addressbook_file_save_cb,       0, NULL},
414         {N_("/_File/_Close"),           "<alt>W",       close_cb,                       0, NULL},
415         {N_("/_Edit"),                  NULL,           NULL, 0, "<Branch>"},
416         {N_("/_Edit/C_ut"),             "<ctl>X",       addressbook_clip_cut_cb,        0, NULL},
417         {N_("/_Edit/_Copy"),            "<ctl>C",       addressbook_clip_copy_cb,       0, NULL},
418         {N_("/_Edit/_Paste"),           "<ctl>V",       addressbook_clip_paste_cb,      0, NULL},
419         {N_("/_Edit/---"),              NULL,           NULL, 0, "<Separator>"},
420         {N_("/_Edit/Pa_ste Address"),   NULL,           addressbook_clip_paste_address_cb, 0, NULL},
421         {N_("/_Address"),               NULL,           NULL, 0, "<Branch>"},
422         {N_("/_Address/New _Address"),  "<alt>N",       addressbook_new_address_cb,     0, NULL},
423         {N_("/_Address/New _Group"),    "<alt>G",       addressbook_new_group_cb,       0, NULL},
424         {N_("/_Address/New _Folder"),   "<alt>R",       addressbook_new_folder_cb,      0, NULL},
425         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>"},
426         {N_("/_Address/_Edit"),         "<alt>Return",  addressbook_edit_address_cb,    0, NULL},
427         {N_("/_Address/_Delete"),       NULL,           addressbook_delete_address_cb,  0, NULL},
428         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>"},
429         {N_("/_Address/_Mail To"),      NULL,           addressbook_mail_to_cb,         0, NULL},
430         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>"},
431         {N_("/_Tools/Import _LDIF file..."), NULL,      addressbook_import_ldif_cb,     0, NULL},
432         {N_("/_Tools/Import M_utt file..."), NULL,      addressbook_import_mutt_cb,     0, NULL},
433         {N_("/_Tools/Import _Pine file..."), NULL,      addressbook_import_pine_cb,     0, NULL},
434         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>"},
435         {N_("/_Tools/Export _HTML..."), NULL,           addressbook_export_html_cb,     0, NULL},
436         {N_("/_Tools/Export LDI_F..."), NULL,           addressbook_export_ldif_cb,     0, NULL},
437         {N_("/_Help"),                  NULL,           NULL, 0, "<LastBranch>"},
438         {N_("/_Help/_About"),           NULL,           about_show, 0, NULL}
439 };
440
441 static GtkItemFactoryEntry addressbook_tree_popup_entries[] =
442 {
443         {N_("/_Edit"),          NULL, addressbook_treenode_edit_cb,   0, NULL},
444         {N_("/_Delete"),        NULL, addressbook_treenode_delete_cb, 0, NULL},
445         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
446         {N_("/New _Address"),   NULL, addressbook_new_address_cb,     0, NULL},
447         {N_("/New _Group"),     NULL, addressbook_new_group_cb,       0, NULL},
448         {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,      0, NULL},
449         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
450         {N_("/C_ut"),           NULL, addressbook_treenode_cut_cb,    0, NULL},
451         {N_("/_Copy"),          NULL, addressbook_treenode_copy_cb,   0, NULL},
452         {N_("/_Paste"),         NULL, addressbook_treenode_paste_cb,  0, NULL}
453 };
454
455 static GtkItemFactoryEntry addressbook_list_popup_entries[] =
456 {
457         {N_("/_Edit"),          NULL, addressbook_edit_address_cb,   0, NULL},
458         {N_("/_Delete"),        NULL, addressbook_delete_address_cb, 0, NULL},
459         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
460         {N_("/New _Address"),   NULL, addressbook_new_address_cb,    0, NULL},
461         {N_("/New _Group"),     NULL, addressbook_new_group_cb,      0, NULL},
462         {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,     0, NULL},
463         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
464         {N_("/C_ut"),           NULL, addressbook_clip_cut_cb,       0, NULL},
465         {N_("/_Copy"),          NULL, addressbook_clip_copy_cb,      0, NULL},
466         {N_("/_Paste"),         NULL, addressbook_clip_paste_cb,     0, NULL},
467         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
468         {N_("/Pa_ste Address"), NULL, addressbook_clip_paste_address_cb, 0, NULL},
469         {N_("/_Mail To"),       NULL, addressbook_mail_to_cb,            0, NULL},
470 #ifdef USE_LDAP
471         {N_("/_Browse Entry"),  NULL, addressbook_browse_entry_cb,       0, NULL},
472 #endif  
473 };
474
475 /**
476  * Structure of error message table.
477  */
478 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
479 struct _ErrMsgTableEntry {
480         gint    code;
481         gchar   *description;
482 };
483
484 static gchar *_errMsgUnknown_ = N_( "Unknown" );
485
486 /**
487  * Lookup table of error messages for general errors. Note that a NULL
488  * description signifies the end of the table.
489  */
490 static ErrMsgTableEntry _lutErrorsGeneral_[] = {
491         { MGU_SUCCESS,          N_("Success") },
492         { MGU_BAD_ARGS,         N_("Bad arguments") },
493         { MGU_NO_FILE,          N_("File not specified") },
494         { MGU_OPEN_FILE,        N_("Error opening file") },
495         { MGU_ERROR_READ,       N_("Error reading file") },
496         { MGU_EOF,              N_("End of file encountered") },
497         { MGU_OO_MEMORY,        N_("Error allocating memory") },
498         { MGU_BAD_FORMAT,       N_("Bad file format") },
499         { MGU_ERROR_WRITE,      N_("Error writing to file") },
500         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
501         { MGU_NO_PATH,          N_("No path specified") },
502         { 0,                    NULL }
503 };
504
505 #ifdef USE_LDAP
506 /**
507  * Lookup table of error messages for LDAP errors.
508  */
509 static ErrMsgTableEntry _lutErrorsLDAP_[] = {
510         { LDAPRC_SUCCESS,       N_("Success") },
511         { LDAPRC_CONNECT,       N_("Error connecting to LDAP server") },
512         { LDAPRC_INIT,          N_("Error initializing LDAP") },
513         { LDAPRC_BIND,          N_("Error binding to LDAP server") },
514         { LDAPRC_SEARCH,        N_("Error searching LDAP database") },
515         { LDAPRC_TIMEOUT,       N_("Timeout performing LDAP operation") },
516         { LDAPRC_CRITERIA,      N_("Error in LDAP search criteria") },
517         { LDAPRC_NOENTRIES,     N_("No LDAP entries found for search criteria") },
518         { LDAPRC_STOP_FLAG,     N_("LDAP search terminated on request") },
519         { LDAPRC_TLS,           N_("Error starting TLS connection") },
520         { 0,                    NULL }
521 };
522 #endif
523
524 /**
525  * Lookup message for specified error code.
526  * \param lut  Lookup table.
527  * \param code Code to lookup.
528  * \return Description associated to code.
529  */
530 static gchar *addressbook_err2string( ErrMsgTableEntry lut[], gint code ) {
531         gchar *desc = NULL;
532         ErrMsgTableEntry entry;
533         gint i;
534
535         for( i = 0; ; i++ ) {
536                 entry = lut[ i ];
537                 if( entry.description == NULL ) break;
538                 if( entry.code == code ) {
539                         desc = entry.description;
540                         break;
541                 }
542         }
543         if( ! desc ) {
544                 desc = _errMsgUnknown_;
545         }
546         return desc;
547 }
548
549 void addressbook_open(Compose *target)
550 {
551         /* Initialize all static members */
552         if( _clipBoard_ == NULL ) {
553                 _clipBoard_ = addrclip_create();
554         }
555         if( _addressIndex_ != NULL ) {
556                 addrclip_set_index( _clipBoard_, _addressIndex_ );
557         }
558         if( _addressSelect_ == NULL ) {
559                 _addressSelect_ = addrselect_list_create();
560         }
561         if (!addrbook.window) {
562                 addressbook_read_file();
563                 addressbook_create();
564                 addressbook_load_tree();
565                 gtk_ctree_select(GTK_CTREE(addrbook.ctree),
566                                  GTK_CTREE_NODE(GTK_CLIST(addrbook.ctree)->row_list));
567         }
568         else {
569                 gtk_widget_hide(addrbook.window);
570         }
571
572         gtk_widget_show_all(addrbook.window);
573         addressbook_set_target_compose(target);
574 }
575
576 /**
577  * Destroy addressbook.
578  */
579 void addressbook_destroy( void ) {
580         /* Free up address stuff */
581         if( _addressSelect_ != NULL ) {
582                 addrselect_list_free( _addressSelect_ );
583         }
584         if( _clipBoard_ != NULL ) {
585                 addrclip_free( _clipBoard_ );
586         }
587         if( _addressIndex_ != NULL ) {
588                 addrindex_free_index( _addressIndex_ );
589                 addrindex_teardown();
590         }
591         _addressSelect_ = NULL;
592         _clipBoard_ = NULL;
593         _addressIndex_ = NULL;
594 }
595
596 void addressbook_set_target_compose(Compose *target)
597 {
598         addrbook.target_compose = target;
599         addressbook_button_set_sensitive();
600 }
601
602 Compose *addressbook_get_target_compose(void)
603 {
604         return addrbook.target_compose;
605 }
606
607 /**
608  * Refresh addressbook and save to file(s).
609  */
610 void addressbook_refresh( void )
611 {
612         if (addrbook.window) {
613                 if (addrbook.treeSelected) {
614                         gtk_ctree_select(GTK_CTREE(addrbook.ctree),
615                                          addrbook.treeSelected);
616                 }
617         }
618         addressbook_export_to_file();
619 }
620
621 /*
622 * Create the address book widgets. The address book contains two CTree widgets: the
623 * address index tree on the left and the address list on the right.
624 *
625 * The address index tree displays a hierarchy of interfaces and groups. Each node in
626 * this tree is linked to an address Adapter. Adapters have been created for interfaces,
627 * data sources and folder objects.
628 *
629 * The address list displays group, person and email objects. These items are linked
630 * directly to ItemGroup, ItemPerson and ItemEMail objects inside the address book data
631 * sources.
632 *
633 * In the tradition of MVC architecture, the data stores have been separated from the
634 * GUI components. The addrindex.c file provides the interface to all data stores.
635 */
636 static void addressbook_create(void)
637 {
638         GtkWidget *window;
639         GtkWidget *vbox;
640         GtkWidget *menubar;
641         GtkWidget *vbox2;
642         GtkWidget *ctree_swin;
643         GtkWidget *ctree;
644         GtkWidget *clist_vbox;
645         GtkWidget *clist_swin;
646         GtkWidget *clist;
647         GtkWidget *paned;
648         GtkWidget *hbox;
649         GtkWidget *label;
650         GtkWidget *entry;
651         GtkWidget *statusbar;
652         GtkWidget *hbbox;
653         GtkWidget *hsbox;
654         GtkWidget *del_btn;
655         GtkWidget *reg_btn;
656         GtkWidget *lup_btn;
657         GtkWidget *to_btn;
658         GtkWidget *cc_btn;
659         GtkWidget *bcc_btn;
660         GtkWidget *tree_popup;
661         GtkWidget *list_popup;
662         GtkItemFactory *tree_factory;
663         GtkItemFactory *list_factory;
664         GtkItemFactory *menu_factory;
665         gint n_entries;
666         GList *nodeIf;
667
668         gchar *titles[N_COLS];
669         gchar *dummy_titles[1];
670         gchar *text;
671         gint i;
672
673         debug_print("Creating addressbook window...\n");
674
675         titles[COL_NAME]    = _("Name");
676         titles[COL_ADDRESS] = _("E-Mail address");
677         titles[COL_REMARKS] = _("Remarks");
678         dummy_titles[0]     = "";
679
680         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
681         gtk_window_set_title(GTK_WINDOW(window), _("Address book"));
682         gtk_widget_set_size_request(window, ADDRESSBOOK_WIDTH, ADDRESSBOOK_HEIGHT);
683         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
684         gtk_widget_realize(window);
685
686         g_signal_connect(G_OBJECT(window), "delete_event",
687                          G_CALLBACK(addressbook_close), NULL);
688         g_signal_connect(G_OBJECT(window), "key_press_event",
689                          G_CALLBACK(key_pressed), NULL);
690         MANAGE_WINDOW_SIGNALS_CONNECT(window);
691
692         vbox = gtk_vbox_new(FALSE, 0);
693         gtk_container_add(GTK_CONTAINER(window), vbox);
694
695         n_entries = sizeof(addressbook_entries) /
696                 sizeof(addressbook_entries[0]);
697         menubar = menubar_create(window, addressbook_entries, n_entries,
698                                  "<AddressBook>", NULL);
699         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
700         menu_factory = gtk_item_factory_from_widget(menubar);
701
702         vbox2 = gtk_vbox_new(FALSE, 4);
703         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
704         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
705
706         ctree_swin = gtk_scrolled_window_new(NULL, NULL);
707         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ctree_swin),
708                                        GTK_POLICY_AUTOMATIC,
709                                        GTK_POLICY_AUTOMATIC);
710         gtk_widget_set_size_request(ctree_swin, COL_FOLDER_WIDTH + 40, -1);
711
712         /* Address index */
713         ctree = gtk_sctree_new_with_titles(1, 0, dummy_titles);
714         gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
715         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
716         gtk_clist_set_column_width(GTK_CLIST(ctree), 0, COL_FOLDER_WIDTH);
717         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
718         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
719                                      GTK_CTREE_EXPANDER_SQUARE);
720         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
721         gtk_clist_set_compare_func(GTK_CLIST(ctree),
722                                    addressbook_treenode_compare_func);
723
724         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
725                          G_CALLBACK(addressbook_tree_selected), NULL);
726         g_signal_connect(G_OBJECT(ctree), "button_press_event",
727                          G_CALLBACK(addressbook_tree_button_pressed),
728                          NULL);
729         g_signal_connect(G_OBJECT(ctree), "button_release_event",
730                          G_CALLBACK(addressbook_tree_button_released),
731                          NULL);
732         /* TEMPORARY */
733         g_signal_connect(G_OBJECT(ctree), "select_row",
734                          G_CALLBACK(addressbook_select_row_tree), NULL);
735
736         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
737                           addressbook_drag_types, 1,
738                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
739         g_signal_connect(G_OBJECT(ctree), "drag_motion",
740                          G_CALLBACK(addressbook_drag_motion_cb),
741                          ctree);
742         g_signal_connect(G_OBJECT(ctree), "drag_leave",
743                          G_CALLBACK(addressbook_drag_leave_cb),
744                          ctree);
745         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
746                          G_CALLBACK(addressbook_drag_received_cb),
747                          ctree);
748
749         clist_vbox = gtk_vbox_new(FALSE, 4);
750
751         clist_swin = gtk_scrolled_window_new(NULL, NULL);
752         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
753                                        GTK_POLICY_AUTOMATIC,
754                                        GTK_POLICY_AUTOMATIC);
755         gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
756
757         /* Address list */
758         clist = gtk_sctree_new_with_titles(N_COLS, 0, titles);
759         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
760         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
761         gtk_ctree_set_line_style(GTK_CTREE(clist), GTK_CTREE_LINES_NONE);
762         gtk_ctree_set_expander_style(GTK_CTREE(clist), GTK_CTREE_EXPANDER_SQUARE);
763         gtk_ctree_set_indent(GTK_CTREE(clist), CTREE_INDENT);
764         gtk_clist_set_column_width(GTK_CLIST(clist), COL_NAME,
765                                    COL_NAME_WIDTH);
766         gtk_clist_set_column_width(GTK_CLIST(clist), COL_ADDRESS,
767                                    COL_ADDRESS_WIDTH);
768         gtk_clist_set_compare_func(GTK_CLIST(clist),
769                                    addressbook_list_compare_func);
770
771         for (i = 0; i < N_COLS; i++)
772                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
773                                        GTK_CAN_FOCUS);
774
775         g_signal_connect(G_OBJECT(clist), "tree_select_row",
776                          G_CALLBACK(addressbook_list_row_selected), NULL);
777         g_signal_connect(G_OBJECT(clist), "tree_unselect_row",
778                          G_CALLBACK(addressbook_list_row_unselected), NULL);
779         g_signal_connect(G_OBJECT(clist), "button_press_event",
780                          G_CALLBACK(addressbook_list_button_pressed),
781                          NULL);
782         g_signal_connect(G_OBJECT(clist), "button_release_event",
783                          G_CALLBACK(addressbook_list_button_released),
784                          NULL);
785         g_signal_connect(G_OBJECT(clist), "tree_expand",
786                          G_CALLBACK(addressbook_person_expand_node), NULL );
787         g_signal_connect(G_OBJECT(clist), "tree_collapse",
788                          G_CALLBACK(addressbook_person_collapse_node), NULL );
789         g_signal_connect(G_OBJECT(clist), "start_drag",
790                          G_CALLBACK(addressbook_start_drag), NULL);
791         g_signal_connect(G_OBJECT(clist), "drag_data_get",
792                          G_CALLBACK(addressbook_drag_data_get), NULL);  
793         hbox = gtk_hbox_new(FALSE, 4);
794         gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
795
796         label = gtk_label_new(_("Name:"));
797         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
798
799         entry = gtk_entry_new();
800         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
801
802         address_completion_register_entry(GTK_ENTRY(entry));
803         g_signal_connect(G_OBJECT(entry), "focus_in_event",
804                          G_CALLBACK(addressbook_entry_gotfocus), NULL);
805
806 #if 0
807         g_signal_connect(G_OBJECT(entry), "changed",
808                          G_CALLBACK(addressbook_entry_changed), NULL);
809 #endif
810
811         paned = gtk_hpaned_new();
812         gtk_box_pack_start(GTK_BOX(vbox2), paned, TRUE, TRUE, 0);
813         gtk_paned_add1(GTK_PANED(paned), ctree_swin);
814         gtk_paned_add2(GTK_PANED(paned), clist_vbox);
815
816         /* Status bar */
817         hsbox = gtk_hbox_new(FALSE, 0);
818         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
819         statusbar = gtk_statusbar_new();
820         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
821
822         /* Button panel */
823         hbbox = gtk_hbutton_box_new();
824         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
825         gtk_box_set_spacing(GTK_BOX(hbbox), 2);
826         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
827
828         del_btn = gtk_button_new_with_label(_("Delete"));
829         GTK_WIDGET_SET_FLAGS(del_btn, GTK_CAN_DEFAULT);
830         gtk_box_pack_start(GTK_BOX(hbbox), del_btn, TRUE, TRUE, 0);
831         reg_btn = gtk_button_new_with_label(_("Add"));
832         GTK_WIDGET_SET_FLAGS(reg_btn, GTK_CAN_DEFAULT);
833         gtk_box_pack_start(GTK_BOX(hbbox), reg_btn, TRUE, TRUE, 0);
834         lup_btn = gtk_button_new_with_label(_("Lookup"));
835         GTK_WIDGET_SET_FLAGS(lup_btn, GTK_CAN_DEFAULT);
836         gtk_box_pack_start(GTK_BOX(hbbox), lup_btn, TRUE, TRUE, 0);
837
838         g_signal_connect(G_OBJECT(del_btn), "clicked",
839                          G_CALLBACK(addressbook_del_clicked), NULL);
840         g_signal_connect(G_OBJECT(reg_btn), "clicked",
841                          G_CALLBACK(addressbook_reg_clicked), NULL);
842         g_signal_connect(G_OBJECT(lup_btn), "clicked",
843                          G_CALLBACK(addressbook_lup_clicked), NULL);
844
845         to_btn = gtk_button_new_with_label
846                 (prefs_common.trans_hdr ? _("To:") : "To:");
847         GTK_WIDGET_SET_FLAGS(to_btn, GTK_CAN_DEFAULT);
848         gtk_box_pack_start(GTK_BOX(hbbox), to_btn, TRUE, TRUE, 0);
849         cc_btn = gtk_button_new_with_label
850                 (prefs_common.trans_hdr ? _("Cc:") : "Cc:");
851         GTK_WIDGET_SET_FLAGS(cc_btn, GTK_CAN_DEFAULT);
852         gtk_box_pack_start(GTK_BOX(hbbox), cc_btn, TRUE, TRUE, 0);
853         bcc_btn = gtk_button_new_with_label
854                 (prefs_common.trans_hdr ? _("Bcc:") : "Bcc:");
855         GTK_WIDGET_SET_FLAGS(bcc_btn, GTK_CAN_DEFAULT);
856         gtk_box_pack_start(GTK_BOX(hbbox), bcc_btn, TRUE, TRUE, 0);
857
858         g_signal_connect(G_OBJECT(to_btn), "clicked",
859                          G_CALLBACK(addressbook_to_clicked),
860                          GINT_TO_POINTER(COMPOSE_TO));
861         g_signal_connect(G_OBJECT(cc_btn), "clicked",
862                          G_CALLBACK(addressbook_to_clicked),
863                          GINT_TO_POINTER(COMPOSE_CC));
864         g_signal_connect(G_OBJECT(bcc_btn), "clicked",
865                          G_CALLBACK(addressbook_to_clicked),
866                          GINT_TO_POINTER(COMPOSE_BCC));
867
868         /* Build icons for interface */
869         stock_pixmap_gdk( window, STOCK_PIXMAP_INTERFACE,
870                           &interfacexpm, &interfacexpmmask );
871
872         /* Build control tables */
873         addrbookctl_build_map(window);
874         addrbookctl_build_iflist();
875         addrbookctl_build_ifselect();
876
877         /* Add each interface into the tree as a root level folder */
878         nodeIf = _addressInterfaceList_;
879         while( nodeIf ) {
880                 AdapterInterface *adapter = nodeIf->data;
881                 AddressInterface *iface = adapter->interface;
882                 nodeIf = g_list_next(nodeIf);
883
884                 if(iface->useInterface) {
885                         AddressTypeControlItem *atci = adapter->atci;
886                         text = atci->displayName;
887                         adapter->treeNode =
888                                 gtk_ctree_insert_node( GTK_CTREE(ctree),
889                                         NULL, NULL, &text, FOLDER_SPACING,
890                                         interfacexpm, interfacexpmmask,
891                                         interfacexpm, interfacexpmmask,
892                                         FALSE, FALSE );
893                         menu_set_sensitive( menu_factory, atci->menuCommand, adapter->haveLibrary );
894                         gtk_ctree_node_set_row_data_full(
895                                 GTK_CTREE(ctree), adapter->treeNode, adapter,
896                                 addressbook_free_treenode );
897                 }
898         }
899
900         /* Popup menu */
901         n_entries = sizeof(addressbook_tree_popup_entries) /
902                 sizeof(addressbook_tree_popup_entries[0]);
903         tree_popup = menu_create_items(addressbook_tree_popup_entries,
904                                        n_entries,
905                                        "<AddressBookTree>", &tree_factory,
906                                        NULL);
907         g_signal_connect(G_OBJECT(tree_popup), "selection_done",
908                          G_CALLBACK(addressbook_popup_close), NULL);
909         n_entries = sizeof(addressbook_list_popup_entries) /
910                 sizeof(addressbook_list_popup_entries[0]);
911         list_popup = menu_create_items(addressbook_list_popup_entries,
912                                        n_entries,
913                                        "<AddressBookList>", &list_factory,
914                                        NULL);
915
916         addrbook.window  = window;
917         addrbook.menubar = menubar;
918         addrbook.ctree   = ctree;
919         addrbook.ctree_swin
920                          = ctree_swin;
921         addrbook.clist   = clist;
922         addrbook.entry   = entry;
923         addrbook.statusbar = statusbar;
924         addrbook.status_cid = gtk_statusbar_get_context_id(
925                         GTK_STATUSBAR(statusbar), "Addressbook Window" );
926
927         addrbook.del_btn = del_btn;
928         addrbook.reg_btn = reg_btn;
929         addrbook.lup_btn = lup_btn;
930         addrbook.to_btn  = to_btn;
931         addrbook.cc_btn  = cc_btn;
932         addrbook.bcc_btn = bcc_btn;
933
934         addrbook.tree_popup   = tree_popup;
935         addrbook.list_popup   = list_popup;
936         addrbook.tree_factory = tree_factory;
937         addrbook.list_factory = list_factory;
938         addrbook.menu_factory = menu_factory;
939
940         addrbook.listSelected = NULL;
941         address_completion_start(window);
942         gtk_widget_show_all(window);
943         gtk_widget_set_sensitive(addrbook.lup_btn, FALSE);
944
945 }
946
947 /**
948  * Close address book window and save to file(s).
949  */
950 static gint addressbook_close( void ) {
951         gtk_widget_hide(addrbook.window);
952         addressbook_export_to_file();
953         return TRUE;
954 }
955
956 /**
957  * Display message in status line.
958  * \param msg Message to display.
959  */
960 static void addressbook_status_show( gchar *msg ) {
961         if( addrbook.statusbar != NULL ) {
962                 gtk_statusbar_pop(
963                         GTK_STATUSBAR(addrbook.statusbar),
964                         addrbook.status_cid );
965                 if( msg ) {
966                         gtk_statusbar_push(
967                                 GTK_STATUSBAR(addrbook.statusbar),
968                                 addrbook.status_cid, msg );
969                 }
970         }
971 }
972
973 static void addressbook_ds_status_message( AddressDataSource *ds, gchar *msg ) {
974         *addressbook_msgbuf = '\0';
975         if( ds ) {
976                 gchar *name;
977
978                 name = addrindex_ds_get_name( ds );
979                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
980                             "%s: %s", name, msg );
981         }
982         else {
983                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
984                             "%s", msg );
985         }
986         addressbook_status_show( addressbook_msgbuf );
987 }
988
989 static void addressbook_ds_show_message( AddressDataSource *ds ) {
990         gint retVal;
991         gchar *name;
992         gchar *desc;
993         *addressbook_msgbuf = '\0';
994         if( ds ) {
995                 name = addrindex_ds_get_name( ds );
996                 retVal = addrindex_ds_get_status_code( ds );
997                 if( retVal == MGU_SUCCESS ) {
998                         g_snprintf( addressbook_msgbuf,
999                                     sizeof(addressbook_msgbuf), "%s", name );
1000                 }
1001                 else {
1002                         desc = addressbook_err2string( _lutErrorsGeneral_, retVal );
1003                         g_snprintf( addressbook_msgbuf, 
1004                             sizeof(addressbook_msgbuf), "%s: %s", name, desc );
1005                 }
1006         }
1007         addressbook_status_show( addressbook_msgbuf );
1008 }
1009
1010 static void addressbook_button_set_sensitive(void)
1011 {
1012         gboolean to_sens  = FALSE;
1013         gboolean cc_sens  = FALSE;
1014         gboolean bcc_sens = FALSE;
1015
1016         if (!addrbook.window) return;
1017
1018         if (addrbook.target_compose) {
1019                 to_sens = TRUE;
1020                 cc_sens = TRUE;
1021 #ifndef CLAWS           
1022                 if (addrbook.target_compose->use_bcc)
1023 #endif                  
1024                         bcc_sens = TRUE;
1025         }
1026
1027         gtk_widget_set_sensitive(addrbook.to_btn, to_sens);
1028         gtk_widget_set_sensitive(addrbook.cc_btn, cc_sens);
1029         gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
1030 }
1031
1032 /*
1033 * Delete one or more objects from address list.
1034 */
1035 static void addressbook_del_clicked(GtkButton *button, gpointer data)
1036 {
1037         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1038         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1039         AddressObject *pobj;
1040         AdapterDSource *ads = NULL;
1041         GtkCTreeNode *nodeList;
1042         gboolean procFlag;
1043         AlertValue aval;
1044         AddressBookFile *abf = NULL;
1045         AddressDataSource *ds = NULL;
1046         AddressInterface *iface;
1047         AddrItemObject *aio;
1048         AddrSelectItem *item;
1049         GList *list, *node;
1050         gboolean refreshList = FALSE;
1051         
1052         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
1053         g_return_if_fail(pobj != NULL);
1054
1055         /* Test whether anything selected for deletion */
1056         nodeList = addrbook.listSelected;
1057         aio = gtk_ctree_node_get_row_data( clist, nodeList );
1058         if( aio == NULL) return;
1059         ds = addressbook_find_datasource( addrbook.treeSelected );
1060         if( ds == NULL ) return;
1061
1062         /* Test for read only */
1063         iface = ds->interface;
1064         if( iface->readOnly ) {
1065                 alertpanel( _("Delete address(es)"),
1066                         _("This address data is readonly and cannot be deleted."),
1067                         GTK_STOCK_CLOSE, NULL, NULL );
1068                 return;
1069         }
1070
1071         /* Test whether Ok to proceed */
1072         procFlag = FALSE;
1073         if( pobj->type == ADDR_DATASOURCE ) {
1074                 ads = ADAPTER_DSOURCE(pobj);
1075                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
1076         }
1077         else if( pobj->type == ADDR_ITEM_FOLDER ) {
1078                 procFlag = TRUE;
1079         }
1080         else if( pobj->type == ADDR_ITEM_GROUP ) {
1081                 procFlag = TRUE;
1082         }
1083         if( ! procFlag ) return;
1084         abf = ds->rawDataSource;
1085         if( abf == NULL ) return;
1086
1087         /* Confirm deletion */
1088         aval = alertpanel( _("Delete address(es)"),
1089                         _("Really delete the address(es)?"),
1090                         GTK_STOCK_YES, GTK_STOCK_NO, NULL );
1091         if( aval != G_ALERTDEFAULT ) return;
1092
1093         /* Process deletions */
1094         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1095                 /* Items inside folders */
1096                 list = addrselect_get_list( _addressSelect_ );
1097                 node = list;
1098                 while( node ) {
1099                         item = node->data;
1100                         node = g_list_next( node );
1101                         aio = ( AddrItemObject * ) item->addressItem;
1102                         if( aio->type == ADDR_ITEM_GROUP ) {
1103                                 ItemGroup *item = ( ItemGroup * ) aio;
1104                                 GtkCTreeNode *nd = NULL;
1105
1106                                 nd = addressbook_find_group_node( addrbook.opened, item );
1107                                 item = addrbook_remove_group( abf, item );
1108                                 if( item ) {
1109                                         addritem_free_item_group( item );
1110                                 }
1111                                 /* Remove group from parent node */
1112                                 gtk_ctree_remove_node( ctree, nd );
1113                                 refreshList = TRUE;
1114                         }
1115                         else if( aio->type == ADDR_ITEM_PERSON ) {
1116                                 ItemPerson *item = ( ItemPerson * ) aio;
1117                                 addressbook_folder_remove_one_person( clist, item );
1118                                 if (pobj->type == ADDR_ITEM_FOLDER)
1119                                         addritem_folder_remove_person((ItemFolder *)pobj, item);
1120                                 item = addrbook_remove_person( abf, item );
1121                                 if( item ) {
1122                                         addritem_free_item_person( item );
1123                                 }
1124                         }
1125                         else if( aio->type == ADDR_ITEM_EMAIL ) {
1126                                 ItemEMail *item = ( ItemEMail * ) aio;
1127                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1128                                 item = addrbook_person_remove_email( abf, person, item );
1129                                 if( item ) {
1130                                         addritem_free_item_email( item );
1131                                 }
1132                                 addressbook_folder_refresh_one_person( clist, person );
1133                         }
1134                 }
1135                 g_list_free( list );
1136                 addressbook_list_select_clear();
1137                 if( refreshList ) 
1138                         gtk_ctree_select( ctree, addrbook.opened);
1139                 addrbook_set_dirty(abf, TRUE);
1140                 addressbook_export_to_file();
1141                 return;
1142         }
1143         else if( pobj->type == ADDR_ITEM_GROUP ) {
1144                 /* Items inside groups */
1145                 list = addrselect_get_list( _addressSelect_ );
1146                 node = list;
1147                 while( node ) {
1148                         item = node->data;
1149                         node = g_list_next( node );
1150                         aio = ( AddrItemObject * ) item->addressItem;
1151                         if( aio->type == ADDR_ITEM_EMAIL ) {
1152                                 ItemEMail *item = ( ItemEMail * ) aio;
1153                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1154                                 item = addrbook_person_remove_email( abf, person, item );
1155                                 if( item ) {
1156                                         addritem_free_item_email( item );
1157                                 }
1158                         }
1159                 }
1160                 g_list_free( list );
1161                 addressbook_list_select_clear();
1162                 gtk_ctree_select( ctree, addrbook.opened);
1163                 addrbook_set_dirty(abf, TRUE);
1164                 addressbook_export_to_file();
1165                 return;
1166         }
1167
1168         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
1169         gtk_ctree_remove_node( clist, nodeList );
1170
1171 }
1172
1173 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1174 {
1175         addressbook_new_address_cb( NULL, 0, NULL );
1176 }
1177
1178 gchar *addressbook_format_address( AddrItemObject * aio ) {
1179         gchar *buf = NULL;
1180         gchar *name = NULL;
1181         gchar *address = NULL;
1182
1183         if( aio->type == ADDR_ITEM_EMAIL ) {
1184                 ItemPerson *person = NULL;
1185                 ItemEMail *email = ( ItemEMail * ) aio;
1186
1187                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1188                 if( email->address ) {
1189                         if( ADDRITEM_NAME(email) ) {
1190                                 name = ADDRITEM_NAME(email);
1191                                 if( *name == '\0' ) {
1192                                         name = ADDRITEM_NAME(person);
1193                                 }
1194                         }
1195                         else if( ADDRITEM_NAME(person) ) {
1196                                 name = ADDRITEM_NAME(person);
1197                         }
1198                         else {
1199                                 buf = g_strdup( email->address );
1200                         }
1201                         address = email->address;
1202                 }
1203         }
1204         else if( aio->type == ADDR_ITEM_PERSON ) {
1205                 ItemPerson *person = ( ItemPerson * ) aio;
1206                 GList *node = person->listEMail;
1207
1208                 name = ADDRITEM_NAME(person);
1209                 if( node ) {
1210                         ItemEMail *email = ( ItemEMail * ) node->data;
1211                         address = email->address;
1212                 }
1213         }
1214         if( address ) {
1215                 if( name && name[0] != '\0' ) {
1216                         if( strchr_with_skip_quote( name, '"', ',' ) )
1217                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1218                         else
1219                                 buf = g_strdup_printf( "%s <%s>", name, address );
1220                 }
1221                 else {
1222                         buf = g_strdup( address );
1223                 }
1224         }
1225
1226         return buf;
1227 }
1228
1229 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1230 {
1231         GList *list, *node;
1232         Compose *compose;
1233         AddrSelectItem *item;
1234         AddrItemObject *aio;
1235         gchar *addr;
1236
1237         compose = addrbook.target_compose;
1238         if( ! compose ) return;
1239
1240         /* Nothing selected, but maybe there is something in text entry */
1241         addr = (char *)gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1242         if ( addr ) {
1243                 compose_entry_append(
1244                         compose, addr, (ComposeEntryType)data );
1245         }
1246
1247         /* Select from address list */
1248         list = addrselect_get_list( _addressSelect_ );
1249         node = list;
1250         while( node ) {
1251                 item = node->data;
1252                 node = g_list_next( node );
1253                 aio = item->addressItem;
1254                 if( aio->type == ADDR_ITEM_PERSON ||
1255                     aio->type == ADDR_ITEM_EMAIL ) {
1256                         addr = addressbook_format_address( aio );
1257                         compose_entry_append(
1258                                 compose, addr, (ComposeEntryType) data );
1259                         g_free( addr );
1260                 }
1261                 else if( aio->type == ADDR_ITEM_GROUP ) {
1262                         ItemGroup *group = ( ItemGroup * ) aio;
1263                         GList *nodeMail = group->listEMail;
1264                         while( nodeMail ) {
1265                                 ItemEMail *email = nodeMail->data;
1266
1267                                 addr = addressbook_format_address(
1268                                                 ( AddrItemObject * ) email );
1269                                 compose_entry_append(
1270                                         compose, addr, (ComposeEntryType) data );
1271                                 g_free( addr );
1272                                 nodeMail = g_list_next( nodeMail );
1273                         }
1274                 }
1275         }
1276         g_list_free( list );
1277 }
1278
1279 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1280         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",   sensitive );
1281         menu_set_sensitive( addrbook.menu_factory, "/File/Delete", sensitive );
1282
1283         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",    sensitive );
1284         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",   sensitive );
1285         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",  sensitive );
1286         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address",  sensitive );
1287
1288         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1289         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1290         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  sensitive );
1291         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To",     sensitive );
1292         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1293         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1294 }
1295
1296 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1297         gboolean canEdit = FALSE;
1298         gboolean canAdd = FALSE;
1299         gboolean canEditTr = TRUE;
1300         gboolean editAddress = FALSE;
1301         gboolean canExport = TRUE;
1302         AddressTypeControlItem *atci = NULL;
1303         AddressDataSource *ds = NULL;
1304         AddressInterface *iface = NULL;
1305
1306         if( obj == NULL ) return;
1307         if( obj->type == ADDR_INTERFACE ) {
1308                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1309                 iface = adapter->interface;
1310                 if( iface ) {
1311                         if( iface->haveLibrary ) {
1312                                 /* Enable appropriate File / New command */
1313                                 atci = adapter->atci;
1314                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1315                         }
1316                 }
1317                 canEditTr = canExport = FALSE;
1318         }
1319         else if( obj->type == ADDR_DATASOURCE ) {
1320                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1321                 ds = ads->dataSource;
1322                 iface = ds->interface;
1323                 if( ! iface->readOnly ) {
1324                         canAdd = canEdit = editAddress = TRUE;
1325                 }
1326                 if( ! iface->haveLibrary ) {
1327                         canAdd = canEdit = editAddress = canExport = FALSE;
1328                 }
1329         }
1330         else if( obj->type == ADDR_ITEM_FOLDER ) {
1331                 ds = addressbook_find_datasource( addrbook.treeSelected );
1332                 if( ds ) {
1333                         iface = ds->interface;
1334                         if( iface->readOnly ) {
1335                                 canEditTr = FALSE;
1336                         }
1337                         else {
1338                                 canAdd = editAddress = TRUE;
1339                         }
1340                 }
1341         }
1342         else if( obj->type == ADDR_ITEM_GROUP ) {
1343                 ds = addressbook_find_datasource( addrbook.treeSelected );
1344                 if( ds ) {
1345                         iface = ds->interface;
1346                         if( ! iface->readOnly ) {
1347                                 editAddress = TRUE;
1348                         }
1349                 }
1350         }
1351
1352         if( addrbook.listSelected == NULL ) canEdit = FALSE;
1353
1354         /* Enable add */
1355         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1356         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1357         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  canAdd );
1358         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1359
1360         /* Enable edit */
1361         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1362         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canEdit );
1363         gtk_widget_set_sensitive( addrbook.del_btn, canEdit );
1364
1365         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",      canEditTr );
1366         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",    canEditTr );
1367
1368         /* Export data */
1369         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1370         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export LDIF...", canExport );
1371 }
1372
1373 /**
1374  * Address book tree callback function that responds to selection of tree
1375  * items.
1376  *
1377  * \param ctree  Tree widget.
1378  * \param node   Node that was selected.
1379  * \param column Column number where selected occurred.
1380  * \param data   Pointer to user data.
1381  */
1382 static gboolean addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1383                                       gint column, gpointer data)
1384 {
1385         AddressObject *obj = NULL;
1386         AdapterDSource *ads = NULL;
1387         AddressDataSource *ds = NULL;
1388         ItemFolder *rootFolder = NULL;
1389         AddressObjectType aot;
1390
1391         addrbook.treeSelected = node;
1392         addrbook.listSelected = NULL;
1393         addressbook_status_show( "" );
1394         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1395
1396         if( addrbook.clist ) gtk_clist_clear( GTK_CLIST(addrbook.clist) );
1397         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1398         if( obj == NULL ) return FALSE;
1399
1400         addrbook.opened = node;
1401
1402         if( obj->type == ADDR_DATASOURCE ) {
1403                 /* Read from file */
1404                 static gboolean tVal = TRUE;
1405
1406                 ads = ADAPTER_DSOURCE(obj);
1407                 if( ads == NULL ) return FALSE;
1408                 ds = ads->dataSource;
1409                 if( ds == NULL ) return FALSE;          
1410
1411                 if( addrindex_ds_get_modify_flag( ds ) ) {
1412                         addrindex_ds_read_data( ds );
1413                 }
1414
1415                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1416                         addrindex_ds_read_data( ds );
1417                 }
1418                 addressbook_ds_show_message( ds );
1419
1420                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1421                         /* Remove existing folders and groups */
1422                         gtk_clist_freeze( GTK_CLIST(ctree) );
1423                         addressbook_tree_remove_children( ctree, node );
1424                         gtk_clist_thaw( GTK_CLIST(ctree) );
1425
1426                         /* Load folders into the tree */
1427                         rootFolder = addrindex_ds_get_root_folder( ds );
1428                         if( ds->type == ADDR_IF_JPILOT ) {
1429                                 aot = ADDR_CATEGORY;
1430                         }
1431                         else if( ds->type == ADDR_IF_LDAP ) {
1432                                 aot = ADDR_LDAP_QUERY;
1433                         }
1434                         else {
1435                                 aot = ADDR_ITEM_FOLDER;
1436                         }
1437                         addressbook_node_add_folder( node, ds, rootFolder, aot );
1438                         addrindex_ds_set_access_flag( ds, &tVal );
1439                         gtk_ctree_expand( ctree, node );
1440                 }
1441         }
1442
1443         /* Update address list */
1444         addressbook_set_clist( obj );
1445
1446         /* Setup main menu selections */
1447         addressbook_menubar_set_sensitive( FALSE );
1448         addressbook_menuitem_set_sensitive( obj, node );
1449
1450         addressbook_list_select_clear();
1451         
1452         return FALSE;
1453 }
1454
1455 /**
1456  * Setup address list popup menu items. Items are enabled or disabled as
1457  * required.
1458  */
1459 static void addressbook_list_menu_setup( void ) {
1460         GtkCTree *clist = NULL;
1461         AddressObject *pobj = NULL;
1462         AddressObject *obj = NULL;
1463         AdapterDSource *ads = NULL;
1464         AddressInterface *iface = NULL;
1465         AddressDataSource *ds = NULL;
1466         gboolean canEdit = FALSE;
1467         gboolean canDelete = FALSE;
1468         gboolean canCut = FALSE;
1469         gboolean canCopy = FALSE;
1470         gboolean canPaste = FALSE;
1471         gboolean canBrowse = FALSE;
1472
1473         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1474         if( pobj == NULL ) return;
1475
1476         clist = GTK_CTREE(addrbook.clist);
1477         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1478         if( obj == NULL ) canEdit = FALSE;
1479
1480         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1481
1482         if( pobj->type == ADDR_DATASOURCE ) {
1483                 /* Parent object is a data source */
1484                 ads = ADAPTER_DSOURCE(pobj);
1485                 ds = ads->dataSource;
1486                 iface = ds->interface;
1487                 if( ! iface->readOnly ) {
1488                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1489                         menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1490                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1491                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1492                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1493                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1494                         if( obj ) canEdit = TRUE;
1495                 }
1496         }
1497         else if( pobj->type != ADDR_INTERFACE ) {
1498                 /* Parent object is not an interface */
1499                 ds = addressbook_find_datasource( addrbook.treeSelected );
1500                 iface = ds->interface;
1501                 if( ! iface->readOnly ) {
1502                         /* Folder or group */
1503                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1504                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1505                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1506                                 if( obj ) canEdit = TRUE;
1507                         }
1508                         /* Folder */
1509                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1510                                 menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1511                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1512                                 if( obj ) canEdit = TRUE;
1513                         }
1514                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1515                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1516                 }
1517                 if( iface->type == ADDR_IF_LDAP ) {
1518                         if( obj ) canBrowse = TRUE;
1519                 }
1520         }
1521         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1522
1523         canDelete = canEdit;
1524
1525         /* Disable edit or browse if more than one row selected */
1526         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) {
1527                 canEdit = FALSE;
1528                 canBrowse = FALSE;
1529         }
1530
1531         /* Now go finalize menu items */
1532         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1533         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1534
1535         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1536         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1537         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1538         menu_set_sensitive( addrbook.list_factory, "/Paste Address", canPaste );
1539
1540         menu_set_sensitive( addrbook.list_factory, "/Mail To",       canCopy );
1541
1542         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
1543         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
1544         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
1545         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
1546
1547         menu_set_sensitive( addrbook.tree_factory, "/Cut",             canCut );
1548         menu_set_sensitive( addrbook.tree_factory, "/Copy",            canCopy );
1549         menu_set_sensitive( addrbook.tree_factory, "/Paste",           canPaste );
1550
1551         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",    canEdit );
1552         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete",  canDelete );
1553         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To", canCopy );
1554
1555         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1556
1557 #ifdef USE_LDAP
1558         menu_set_sensitive( addrbook.list_factory, "/Browse Entry",    canBrowse );
1559 #endif
1560 }
1561
1562 static void addressbook_select_row_tree (GtkCTree       *ctree,
1563                                          GtkCTreeNode   *node,
1564                                          gint            column,
1565                                          gpointer        data)
1566 {
1567 }
1568
1569 /**
1570  * Add list of items into tree node below specified tree node.
1571  * \param treeNode  Tree node.
1572  * \param ds        Data source.
1573  * \param listItems List of items.
1574  */
1575 static void addressbook_treenode_add_list(
1576         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
1577 {
1578         GList *node;
1579
1580         node = listItems;
1581         while( node ) {
1582                 AddrItemObject *aio;
1583                 GtkCTreeNode *nn;
1584
1585                 aio = node->data;
1586                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
1587                         ItemGroup *group;
1588
1589                         group = ( ItemGroup * ) aio;
1590                         nn = addressbook_node_add_group( treeNode, ds, group );
1591                 }
1592                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
1593                         ItemFolder *folder;
1594
1595                         folder = ( ItemFolder * ) aio;
1596                         nn = addressbook_node_add_folder(
1597                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
1598                 }
1599                 node = g_list_next( node );
1600         }
1601 }
1602
1603 /**
1604  * Cut from address list widget.
1605  */
1606 static void addressbook_clip_cut_cb( void ) {
1607         _clipBoard_->cutFlag = TRUE;
1608         addrclip_clear( _clipBoard_ );
1609         addrclip_add( _clipBoard_, _addressSelect_ );
1610         /* addrclip_list_show( _clipBoard_, stdout ); */
1611 }
1612
1613 /**
1614  * Copy from address list widget.
1615  */
1616 static void addressbook_clip_copy_cb( void ) {
1617         _clipBoard_->cutFlag = FALSE;
1618         addrclip_clear( _clipBoard_ );
1619         addrclip_add( _clipBoard_, _addressSelect_ );
1620         /* addrclip_list_show( _clipBoard_, stdout ); */
1621 }
1622
1623 /**
1624  * Paste clipboard into address list widget.
1625  */
1626 static void addressbook_clip_paste_cb( void ) {
1627         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1628         AddressObject *pobj = NULL;
1629         AddressDataSource *ds = NULL;
1630         AddressBookFile *abf = NULL;
1631         ItemFolder *folder = NULL;
1632         GList *folderGroup = NULL;
1633
1634         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1635         if( ds == NULL ) return;
1636         if( addrindex_ds_get_readonly( ds ) ) {
1637                 addressbook_ds_status_message(
1638                         ds, _( "Cannot paste. Target address book is readonly." ) );
1639                 return;
1640         }
1641
1642         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1643         if( pobj ) {
1644                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1645                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1646                 }
1647                 else if( pobj->type == ADDR_ITEM_GROUP ) {
1648                         addressbook_ds_status_message(
1649                                 ds, _( "Cannot paste into an address group." ) );
1650                         return;
1651                 }
1652         }
1653
1654         /* Get an address book */
1655         abf = addressbook_get_book_file();
1656         if( abf == NULL ) return;
1657
1658         if( _clipBoard_->cutFlag ) {
1659                 /* Paste/Cut */
1660                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
1661
1662                 /* Remove all groups and folders in clipboard from tree node */
1663                 addressbook_treenode_remove_item();
1664
1665                 /* Remove all "cut" items */
1666                 addrclip_delete_item( _clipBoard_ );
1667
1668                 /* Clear clipboard - cut items??? */
1669                 addrclip_clear( _clipBoard_ );
1670         }
1671         else {
1672                 /* Paste/Copy */
1673                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
1674         }
1675
1676         /* addrclip_list_show( _clipBoard_, stdout ); */
1677         if( folderGroup ) {
1678                 /* Update tree by inserting node for each folder or group */
1679                 addressbook_treenode_add_list(
1680                         addrbook.treeSelected, ds, folderGroup );
1681                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1682                 g_list_free( folderGroup );
1683                 folderGroup = NULL;
1684         }
1685
1686         /* Display items pasted */
1687         gtk_ctree_select( ctree, addrbook.opened );
1688
1689 }
1690
1691 /**
1692  * Paste clipboard email addresses only into address list widget.
1693  */
1694 static void addressbook_clip_paste_address_cb( void ) {
1695         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1696         GtkCTree *ctree;
1697         AddressObject *pobj = NULL;
1698         AddressDataSource *ds = NULL;
1699         AddressBookFile *abf = NULL;
1700         ItemFolder *folder = NULL;
1701         AddrItemObject *aio;
1702         gint cnt;
1703
1704         if( addrbook.listSelected == NULL ) return;
1705
1706         ctree = GTK_CTREE( addrbook.ctree );
1707         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1708         if( ds == NULL ) return;
1709         if( addrindex_ds_get_readonly( ds ) ) {
1710                 addressbook_ds_status_message(
1711                         ds, _( "Cannot paste. Target address book is readonly." ) );
1712                 return;
1713         }
1714
1715         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1716         if( pobj ) {
1717                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1718                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1719                 }
1720         }
1721
1722         abf = addressbook_get_book_file();
1723         if( abf == NULL ) return;
1724
1725         cnt = 0;
1726         aio = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1727         if( aio->type == ADDR_ITEM_PERSON ) {
1728                 ItemPerson *person;
1729
1730                 person = ( ItemPerson * ) aio;
1731                 if( _clipBoard_->cutFlag ) {
1732                         /* Paste/Cut */
1733                         cnt = addrclip_paste_person_cut( _clipBoard_, abf, person );
1734
1735                         /* Remove all "cut" items */
1736                         addrclip_delete_address( _clipBoard_ );
1737
1738                         /* Clear clipboard */
1739                         addrclip_clear( _clipBoard_ );
1740                 }
1741                 else {
1742                         /* Paste/Copy */
1743                         cnt = addrclip_paste_person_copy( _clipBoard_, abf, person );
1744                 }
1745                 if( cnt > 0 ) {
1746                         addritem_person_set_opened( person, TRUE );
1747                 }
1748         }
1749
1750         /* Display items pasted */
1751         if( cnt > 0 ) {
1752                 gtk_ctree_select( ctree, addrbook.opened );
1753         }
1754 }
1755
1756 /**
1757  * Add current treenode object to clipboard. Note that widget only allows
1758  * one entry from the tree list to be selected.
1759  */
1760 static void addressbook_treenode_to_clipboard( void ) {
1761         AddressObject *obj = NULL;
1762         AddressDataSource *ds = NULL;
1763         AddrSelectItem *item;
1764         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1765         GtkCTreeNode *node;
1766
1767         node = addrbook.treeSelected;
1768         if( node == NULL ) return;
1769         obj = gtk_ctree_node_get_row_data( ctree, node );
1770         if( obj == NULL ) return;
1771
1772         ds = addressbook_find_datasource( node );
1773         if( ds == NULL ) return;
1774
1775         item = NULL;
1776         if( obj->type == ADDR_ITEM_FOLDER ) {
1777                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
1778                 ItemFolder *folder = adapter->itemFolder;
1779
1780                 item = addrselect_create_node( obj );
1781                 item->uid = g_strdup( ADDRITEM_ID(folder) );
1782         }
1783         else if( obj->type == ADDR_ITEM_GROUP ) {
1784                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
1785                 ItemGroup *group = adapter->itemGroup;
1786
1787                 item = addrselect_create_node( obj );
1788                 item->uid = g_strdup( ADDRITEM_ID(group) );
1789         }
1790         else if( obj->type == ADDR_DATASOURCE ) {
1791                 /* Data source */
1792                 item = addrselect_create_node( obj );
1793                 item->uid = NULL;
1794         }
1795
1796         if( item ) {
1797                 /* Clear existing list and add item into list */
1798                 gchar *cacheID;
1799
1800                 addressbook_list_select_clear();
1801                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1802                 addrselect_list_add( _addressSelect_, item, cacheID );
1803                 g_free( cacheID );
1804         }
1805 }
1806
1807 /**
1808  * Cut from tree widget.
1809  */
1810 static void addressbook_treenode_cut_cb( void ) {
1811         _clipBoard_->cutFlag = TRUE;
1812         addressbook_treenode_to_clipboard();
1813         addrclip_clear( _clipBoard_ );
1814         addrclip_add( _clipBoard_, _addressSelect_ );
1815         /* addrclip_list_show( _clipBoard_, stdout ); */
1816 }
1817
1818 /**
1819  * Copy from tree widget.
1820  */
1821 static void addressbook_treenode_copy_cb( void ) {
1822         _clipBoard_->cutFlag = FALSE;
1823         addressbook_treenode_to_clipboard();
1824         addrclip_clear( _clipBoard_ );
1825         addrclip_add( _clipBoard_, _addressSelect_ );
1826         /* addrclip_list_show( _clipBoard_, stdout ); */
1827 }
1828
1829 /**
1830  * Paste clipboard into address tree widget.
1831  */
1832 static void addressbook_treenode_paste_cb( void ) {
1833         addressbook_clip_paste_cb();
1834 }
1835
1836 /**
1837  * Clear selected entries in clipboard.
1838  */
1839 static void addressbook_list_select_clear( void ) {
1840         addrselect_list_clear( _addressSelect_ );
1841 }
1842
1843 /**
1844  * Add specified address item to selected address list.
1845  * \param aio Address item object.
1846  * \param ds  Datasource.
1847  */
1848 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
1849         gchar *cacheID;
1850
1851         if( ds == NULL ) return;
1852         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1853         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
1854         g_free( cacheID );
1855 }
1856
1857 /**
1858  * Remove specified address item from selected address list.
1859  * \param aio Address item object.
1860  */
1861 static void addressbook_list_select_remove( AddrItemObject *aio ) {
1862         addrselect_list_remove( _addressSelect_, aio );
1863 }
1864
1865 /**
1866  * Invoke EMail compose window with addresses in selected address list.
1867  */
1868 static void addressbook_mail_to_cb( void ) {
1869         GList *listAddress;
1870
1871         if( ! addrselect_test_empty( _addressSelect_ ) ) {
1872                 listAddress = addrselect_build_list( _addressSelect_ );
1873                 compose_new_with_list( NULL, listAddress );
1874                 mgu_free_dlist( listAddress );
1875                 listAddress = NULL;
1876         }
1877 }
1878
1879 static void addressbook_list_row_selected( GtkCTree *clist,
1880                                            GtkCTreeNode *node,
1881                                            gint column,
1882                                            gpointer data )
1883 {
1884         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
1885         AddrItemObject *aio = NULL;
1886         AddressObject *pobj = NULL;
1887         AdapterDSource *ads = NULL;
1888         AddressDataSource *ds = NULL;
1889
1890         gtk_entry_set_text( entry, "" );
1891         addrbook.listSelected = node;
1892
1893         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1894         if( pobj == NULL ) return;
1895
1896         if( pobj->type == ADDR_DATASOURCE ) {
1897                 ads = ADAPTER_DSOURCE(pobj);
1898                 ds = ads->dataSource;
1899         }
1900         else if( pobj->type != ADDR_INTERFACE ) {
1901                 ds = addressbook_find_datasource( addrbook.treeSelected );
1902         }
1903
1904         aio = gtk_ctree_node_get_row_data( clist, node );
1905         if( aio ) {
1906                 printf( "list select: %d : '%s'\n", aio->type, aio->name );
1907                 addressbook_list_select_add( aio, ds );
1908         }
1909
1910         addressbook_list_menu_setup();
1911 }
1912
1913 static void addressbook_list_row_unselected( GtkCTree *ctree,
1914                                              GtkCTreeNode *node,
1915                                              gint column,
1916                                              gpointer data )
1917 {
1918         AddrItemObject *aio;
1919
1920         aio = gtk_ctree_node_get_row_data( ctree, node );
1921         if( aio != NULL ) {
1922                 /* printf( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
1923                 addressbook_list_select_remove( aio );
1924         }
1925 }
1926
1927 static void addressbook_entry_gotfocus( GtkWidget *widget ) {
1928         gtk_editable_select_region( GTK_EDITABLE(addrbook.entry), 0, -1 );
1929 }
1930
1931 /* from gdkevents.c */
1932 #define DOUBLE_CLICK_TIME 250
1933
1934 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
1935                                                 GdkEventButton *event,
1936                                                 gpointer data)
1937 {
1938         static guint32 lasttime = 0;
1939         if( ! event ) return FALSE;
1940
1941         addressbook_list_menu_setup();
1942
1943         if( event->button == 3 ) {
1944                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
1945                        event->button, event->time );
1946         } else if (event->button == 1) {
1947                 if (event->time - lasttime < DOUBLE_CLICK_TIME) {
1948                         if (prefs_common.add_address_by_click &&
1949                             addrbook.target_compose)
1950                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
1951                         else
1952                                 addressbook_edit_address_cb(NULL, 0, NULL);
1953
1954                         lasttime = 0;
1955                 } else
1956                         lasttime = event->time;
1957         }
1958
1959         return FALSE;
1960 }
1961
1962 static gboolean addressbook_list_button_released(GtkWidget *widget,
1963                                                  GdkEventButton *event,
1964                                                  gpointer data)
1965 {
1966         return FALSE;
1967 }
1968
1969 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
1970                                                 GdkEventButton *event,
1971                                                 gpointer data)
1972 {
1973         GtkCList *clist = GTK_CLIST(ctree);
1974         gint row, column;
1975         AddressObject *obj = NULL;
1976         AdapterDSource *ads = NULL;
1977         AddressInterface *iface = NULL;
1978         AddressDataSource *ds = NULL;
1979         gboolean canEdit = FALSE;
1980         gboolean canDelete = FALSE;
1981         gboolean canCut = FALSE;
1982         gboolean canCopy = FALSE;
1983         gboolean canPaste = FALSE;
1984         gboolean canTreeCut = FALSE;
1985         gboolean canTreeCopy = FALSE;
1986         gboolean canTreePaste = FALSE;
1987         gboolean canLookup = FALSE;
1988
1989         if( ! event ) return FALSE;
1990         addressbook_menubar_set_sensitive( FALSE );
1991
1992         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
1993                 gtk_clist_select_row( clist, row, column );
1994                 gtkut_clist_set_focus_row(clist, row);
1995                 obj = gtk_clist_get_row_data( clist, row );
1996         }
1997
1998         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
1999         gtk_widget_set_sensitive( addrbook.lup_btn, FALSE );
2000
2001         if( obj == NULL ) return FALSE;
2002
2003         if( ! addrclip_is_empty( _clipBoard_ ) ) {
2004                 canTreePaste = TRUE;
2005         }
2006
2007         if (obj->type == ADDR_DATASOURCE) {
2008                 ads = ADAPTER_DSOURCE(obj);
2009                 ds = ads->dataSource;
2010                 iface = ds->interface;
2011                 canEdit = TRUE;
2012                 canDelete = TRUE;
2013                 if( iface->readOnly ) {
2014                         canTreePaste = FALSE;
2015                 }
2016                 else {
2017                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
2018                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2019                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2020                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2021                 }
2022                 canTreeCopy = TRUE;
2023                 if( iface->externalQuery ) canLookup = TRUE;
2024         }
2025         else if (obj->type == ADDR_ITEM_FOLDER) {
2026                 ds = addressbook_find_datasource( addrbook.treeSelected );
2027                 iface = ds->interface;
2028                 if( iface->readOnly ) {
2029                         canTreePaste = FALSE;
2030                 }
2031                 else {
2032                         canEdit = TRUE;
2033                         canDelete = TRUE;
2034                         canTreeCut = TRUE;
2035                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
2036                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2037                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2038                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2039                 }
2040                 canTreeCopy = TRUE;
2041                 iface = ds->interface;
2042                 if( iface->externalQuery ) {
2043                         /* Enable deletion of LDAP folder */
2044                         canLookup = TRUE;
2045                         canDelete = TRUE;
2046                 }
2047         }
2048         else if (obj->type == ADDR_ITEM_GROUP) {
2049                 ds = addressbook_find_datasource( addrbook.treeSelected );
2050                 iface = ds->interface;
2051                 if( ! iface->readOnly ) {
2052                         canEdit = TRUE;
2053                         canDelete = TRUE;
2054                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
2055                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2056                 }
2057         }
2058         else if (obj->type == ADDR_INTERFACE) {
2059                 canTreePaste = FALSE;
2060         }
2061
2062         if( canEdit ) {
2063                 if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
2064         }
2065         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
2066         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
2067
2068         /* Enable edit */
2069         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
2070         menu_set_sensitive( addrbook.tree_factory, "/Delete", canDelete );
2071         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
2072         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
2073         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
2074
2075         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",          canEdit );
2076         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",        canEdit );
2077         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
2078         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
2079         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
2080         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
2081
2082         gtk_widget_set_sensitive( addrbook.lup_btn, canLookup );
2083
2084         if( event->button == 3 ) {
2085                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2086                                event->button, event->time);
2087         }
2088
2089         return FALSE;
2090 }
2091
2092 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2093                                                  GdkEventButton *event,
2094                                                  gpointer data)
2095 {
2096         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
2097         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
2098         return FALSE;
2099 }
2100
2101 static void addressbook_popup_close(GtkMenuShell *menu_shell, gpointer data)
2102 {
2103         if (!addrbook.opened) return;
2104
2105         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
2106         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree),
2107                                   addrbook.opened);
2108 }
2109
2110 static void addressbook_new_folder_cb(gpointer data, guint action,
2111                                       GtkWidget *widget)
2112 {
2113         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2114         AddressObject *obj = NULL;
2115         AddressDataSource *ds = NULL;
2116         AddressBookFile *abf = NULL;
2117         ItemFolder *parentFolder = NULL;
2118         ItemFolder *folder = NULL;
2119
2120         if( ! addrbook.treeSelected ) return;
2121         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2122         if( obj == NULL ) return;
2123         ds = addressbook_find_datasource( addrbook.treeSelected );
2124         if( ds == NULL ) return;
2125
2126         if( obj->type == ADDR_DATASOURCE ) {
2127                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2128         }
2129         else if( obj->type == ADDR_ITEM_FOLDER ) {
2130                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2131         }
2132         else {
2133                 return;
2134         }
2135
2136         abf = ds->rawDataSource;
2137         if( abf == NULL ) return;
2138         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2139         if( folder ) {
2140                 GtkCTreeNode *nn;
2141                 nn = addressbook_node_add_folder(
2142                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2143                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2144                 if( addrbook.treeSelected == addrbook.opened )
2145                         addressbook_set_clist(obj);
2146         }
2147
2148 }
2149
2150 static void addressbook_new_group_cb(gpointer data, guint action,
2151                                      GtkWidget *widget)
2152 {
2153         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2154         AddressObject *obj = NULL;
2155         AddressDataSource *ds = NULL;
2156         AddressBookFile *abf = NULL;
2157         ItemFolder *parentFolder = NULL;
2158         ItemGroup *group = NULL;
2159
2160         if( ! addrbook.treeSelected ) return;
2161         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
2162         if( obj == NULL ) return;
2163         ds = addressbook_find_datasource( addrbook.treeSelected );
2164         if( ds == NULL ) return;
2165
2166         if( obj->type == ADDR_DATASOURCE ) {
2167                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2168         }
2169         else if( obj->type == ADDR_ITEM_FOLDER ) {
2170                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2171         }
2172         else {
2173                 return;
2174         }
2175
2176         abf = ds->rawDataSource;
2177         if( abf == NULL ) return;
2178         group = addressbook_edit_group( abf, parentFolder, NULL );
2179         if( group ) {
2180                 GtkCTreeNode *nn;
2181                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2182                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2183                 if( addrbook.treeSelected == addrbook.opened )
2184                         addressbook_set_clist(obj);
2185         }
2186
2187 }
2188
2189 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2190 {
2191         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2192         gchar *text[1];
2193         guint8 spacing;
2194         GdkPixmap *pix_cl, *pix_op;
2195         GdkBitmap *mask_cl, *mask_op;
2196         gboolean is_leaf, expanded;
2197
2198         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2199                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2200                                 &is_leaf, &expanded);
2201         gtk_ctree_set_node_info(ctree, node, name, spacing,
2202                                 pix_cl, mask_cl, pix_op, mask_op,
2203                                 is_leaf, expanded);
2204 }
2205
2206 /**
2207  * Edit data source.
2208  * \param obj  Address object to edit.
2209  * \param node Node in tree.
2210  * \return New name of data source.
2211  */
2212 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2213         gchar *newName = NULL;
2214         AddressDataSource *ds = NULL;
2215         AddressInterface *iface = NULL;
2216         AdapterDSource *ads = NULL;
2217
2218         ds = addressbook_find_datasource( node );
2219         if( ds == NULL ) return NULL;
2220         iface = ds->interface;
2221         if( ! iface->haveLibrary ) return NULL;
2222
2223         /* Read data from data source */
2224         if( addrindex_ds_get_modify_flag( ds ) ) {
2225                 addrindex_ds_read_data( ds );
2226         }
2227
2228         if( ! addrindex_ds_get_read_flag( ds ) ) {
2229                 addrindex_ds_read_data( ds );
2230         }
2231
2232         /* Handle edit */
2233         ads = ADAPTER_DSOURCE(obj);
2234         if( ads->subType == ADDR_BOOK ) {
2235                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2236         }
2237         else if( ads->subType == ADDR_VCARD ) {
2238                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2239         }
2240 #ifdef USE_JPILOT
2241         else if( ads->subType == ADDR_JPILOT ) {
2242                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2243         }
2244 #endif
2245 #ifdef USE_LDAP
2246         else if( ads->subType == ADDR_LDAP ) {
2247                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2248         }
2249 #endif
2250         else {
2251                 return NULL;
2252         }
2253         newName = obj->name;
2254         return newName;
2255 }
2256
2257 /*
2258 * Edit an object that is in the address tree area.
2259 */
2260 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2261                                        GtkWidget *widget)
2262 {
2263         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2264         AddressObject *obj;
2265         AddressDataSource *ds = NULL;
2266         AddressBookFile *abf = NULL;
2267         GtkCTreeNode *node = NULL, *parentNode = NULL;
2268         gchar *name = NULL;
2269
2270         if( ! addrbook.treeSelected ) return;
2271         node = addrbook.treeSelected;
2272         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2273         obj = gtk_ctree_node_get_row_data( ctree, node );
2274         if( obj == NULL ) return;
2275         parentNode = GTK_CTREE_ROW(node)->parent;
2276
2277         ds = addressbook_find_datasource( node );
2278         if( ds == NULL ) return;
2279
2280         if( obj->type == ADDR_DATASOURCE ) {
2281                 name = addressbook_edit_datasource( obj, node );
2282                 if( name == NULL ) return;
2283         }
2284         else {
2285                 abf = ds->rawDataSource;
2286                 if( abf == NULL ) return;
2287                 if( obj->type == ADDR_ITEM_FOLDER ) {
2288                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2289                         ItemFolder *item = adapter->itemFolder;
2290                         ItemFolder *parentFolder = NULL;
2291                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2292                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2293                         name = ADDRITEM_NAME(item);
2294                 }
2295                 else if( obj->type == ADDR_ITEM_GROUP ) {
2296                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2297                         ItemGroup *item = adapter->itemGroup;
2298                         ItemFolder *parentFolder = NULL;
2299                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2300                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2301                         name = ADDRITEM_NAME(item);
2302                 }
2303         }
2304         if( name && parentNode ) {
2305                 /* Update node in tree view */
2306                 addressbook_change_node_name( node, name );
2307                 gtk_sctree_sort_node(ctree, parentNode);
2308                 gtk_ctree_expand( ctree, node );
2309                 gtk_ctree_select( ctree, node );
2310         }
2311 }
2312
2313 typedef enum {
2314         ADDRTREE_DEL_NONE,
2315         ADDRTREE_DEL_DATA,
2316         ADDRTREE_DEL_FOLDER_ONLY,
2317         ADDRTREE_DEL_FOLDER_ADDR
2318 } TreeItemDelType ;
2319
2320 /**
2321  * Delete an item from the tree widget.
2322  * \param data   Data passed in.
2323  * \param action Action.
2324  * \param widget Widget issuing callback.
2325  */
2326 static void addressbook_treenode_delete_cb(
2327                 gpointer data, guint action, GtkWidget *widget )
2328 {
2329         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2330         GtkCTreeNode *node = NULL;
2331         AddressObject *obj;
2332         gchar *message;
2333         AlertValue aval;
2334         AddrBookBase *adbase;
2335         AddressCache *cache;
2336         AdapterDSource *ads = NULL;
2337         AddressInterface *iface = NULL;
2338         AddressDataSource *ds = NULL;
2339         gboolean remFlag = FALSE;
2340         TreeItemDelType delType;
2341
2342         if( ! addrbook.treeSelected ) return;
2343         node = addrbook.treeSelected;
2344         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2345
2346         obj = gtk_ctree_node_get_row_data( ctree, node );
2347         g_return_if_fail(obj != NULL);
2348
2349         if( obj->type == ADDR_DATASOURCE ) {
2350                 ads = ADAPTER_DSOURCE(obj);
2351                 if( ads == NULL ) return;
2352                 ds = ads->dataSource;
2353                 if( ds == NULL ) return;
2354         }
2355         else {
2356                 /* Must be folder or something else */
2357                 ds = addressbook_find_datasource( node );
2358                 if( ds == NULL ) return;
2359
2360                 /* Only allow deletion from non-readOnly */
2361                 iface = ds->interface;
2362                 if( iface->readOnly ) {
2363                         /* Allow deletion of query results */
2364                         if( ! iface->externalQuery ) return;
2365                 }
2366         }
2367
2368         /* Confirm deletion */
2369         delType = ADDRTREE_DEL_NONE;
2370         if( obj->type == ADDR_ITEM_FOLDER ) {
2371                 if( iface->externalQuery ) {
2372                         message = g_strdup_printf( _(
2373                                 "Do you want to delete the query " \
2374                                 "results and addresses in `%s' ?" ),
2375                                 obj->name );
2376                         aval = alertpanel( _("Delete"), message,
2377                                 GTK_STOCK_YES, GTK_STOCK_NO, NULL );
2378                         g_free(message);
2379                         if( aval == G_ALERTDEFAULT ) {
2380                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2381                         }
2382                 }
2383                 else {
2384                         message = g_strdup_printf
2385                                 ( _( "Do you want to delete the folder AND all addresses in `%s' ?\n"
2386                                      "If deleting the folder only, addresses will be moved into parent folder." ),
2387                                  obj->name );
2388                         aval = alertpanel( _("Delete folder"), message,
2389                                 _("_Folder only"), _("Folder and _addresses"),
2390                                 GTK_STOCK_CANCEL );
2391                         g_free(message);
2392                         if( aval == G_ALERTDEFAULT ) {
2393                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2394                         }
2395                         else if( aval == G_ALERTALTERNATE ) {
2396                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2397                         }
2398                 }
2399         }
2400         else {
2401                 message = g_strdup_printf(_("Really delete `%s' ?"), obj->name);
2402                 aval = alertpanel(_("Delete"), message, GTK_STOCK_YES, GTK_STOCK_NO, NULL);
2403                 g_free(message);
2404                 if( aval == G_ALERTDEFAULT ) delType = ADDRTREE_DEL_DATA;
2405         }
2406         if( delType == ADDRTREE_DEL_NONE ) return;
2407
2408         /* Proceed with deletion */
2409         if( obj->type == ADDR_DATASOURCE ) {
2410                 /* Remove node from tree */
2411                 gtk_ctree_remove_node( ctree, node );
2412         
2413                 /* Remove data source. */
2414                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2415                         addrindex_free_datasource( ds );
2416                 }
2417                 return;
2418         }
2419
2420         /* Get reference to cache */
2421         adbase = ( AddrBookBase * ) ds->rawDataSource;
2422         if( adbase == NULL ) return;
2423         cache = adbase->addressCache;
2424
2425         /* Remove query results folder */
2426         if( iface->externalQuery ) {
2427                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2428                 ItemFolder *folder = adapter->itemFolder;
2429
2430                 adapter->itemFolder = NULL;
2431                 /*
2432                 printf( "remove folder for ::%s::\n", obj->name );
2433                 printf( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
2434                 printf( "-------------- remove results\n" );
2435                 */
2436                 addrindex_remove_results( ds, folder );
2437                 /* printf( "-------------- remove node\n" ); */
2438                 gtk_ctree_remove_node( ctree, node );
2439                 return;
2440         }
2441
2442         /* Code below is valid for regular address book deletion */
2443         if( obj->type == ADDR_ITEM_FOLDER ) {
2444                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2445                 ItemFolder *item = adapter->itemFolder;
2446
2447                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
2448                         /* Remove folder only */
2449                         item = addrcache_remove_folder( cache, item );
2450                         if( item ) {
2451                                 addritem_free_item_folder( item );
2452                                 addressbook_move_nodes_up( ctree, node );
2453                                 remFlag = TRUE;
2454                         }
2455                 }
2456                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
2457                         /* Remove folder and addresses */
2458                         item = addrcache_remove_folder_delete( cache, item );
2459                         if( item ) {
2460                                 addritem_free_item_folder( item );
2461                                 remFlag = TRUE;
2462                         }
2463                 }
2464         }
2465         else if( obj->type == ADDR_ITEM_GROUP ) {
2466                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2467                 ItemGroup *item = adapter->itemGroup;
2468
2469                 item = addrcache_remove_group( cache, item );
2470                 if( item ) {
2471                         addritem_free_item_group( item );
2472                         remFlag = TRUE;
2473                 }
2474         }
2475
2476         if( remFlag ) {
2477                 /* Remove node. */
2478                 gtk_ctree_remove_node(ctree, node );
2479         }
2480 }
2481
2482 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2483         AddressObject *pobj = NULL;
2484         AddressDataSource *ds = NULL;
2485         AddressBookFile *abf = NULL;
2486
2487         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
2488         if( pobj == NULL ) return;
2489         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2490         if( ds == NULL ) return;
2491
2492         abf = ds->rawDataSource;
2493         if( abf == NULL ) return;
2494
2495         if( pobj->type == ADDR_DATASOURCE ) {
2496                 if( ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ) {
2497                         /* New address */
2498                         ItemPerson *person = addressbook_edit_person( abf, NULL, NULL, FALSE );
2499                         if( person && addrbook.treeSelected == addrbook.opened ) {
2500                                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2501                                 addressbook_folder_refresh_one_person(
2502                                         GTK_CTREE(addrbook.clist), person );
2503                         }
2504                 }
2505         }
2506         else if( pobj->type == ADDR_ITEM_FOLDER ) {
2507                 /* New address */
2508                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
2509                 ItemPerson *person = addressbook_edit_person( abf, folder, NULL, FALSE );
2510                 if( person ) {
2511                         if (addrbook.treeSelected == addrbook.opened) {
2512                                 gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2513                         }
2514                 }
2515         }
2516         else if( pobj->type == ADDR_ITEM_GROUP ) {
2517                 /* New address in group */
2518                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
2519                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
2520                 if (addrbook.treeSelected == addrbook.opened) {
2521                         /* Change node name in tree. */
2522                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
2523                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2524                 }
2525         }
2526 }
2527
2528 /**
2529  * Search for specified child group node in address index tree.
2530  * \param parent Parent node.
2531  * \param group  Group to find.
2532  */
2533 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
2534         GtkCTreeNode *node = NULL;
2535         GtkCTreeRow *currRow;
2536
2537         currRow = GTK_CTREE_ROW( parent );
2538         if( currRow ) {
2539                 node = currRow->children;
2540                 while( node ) {
2541                         AddressObject *obj;
2542
2543                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2544                         if( obj->type == ADDR_ITEM_GROUP ) {
2545                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
2546                                 if( g == group ) return node;
2547                         }
2548                         currRow = GTK_CTREE_ROW(node);
2549                         node = currRow->sibling;
2550                 }
2551         }
2552         return NULL;
2553 }
2554
2555 static AddressBookFile *addressbook_get_book_file() {
2556         AddressBookFile *abf = NULL;
2557         AddressDataSource *ds = NULL;
2558
2559         ds = addressbook_find_datasource( addrbook.treeSelected );
2560         if( ds == NULL ) return NULL;
2561         if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
2562         return abf;
2563 }
2564
2565 static AddressBookFile *addressbook_get_book_file_for_node(GtkCTreeNode *node) {
2566         AddressBookFile *abf = NULL;
2567         AddressDataSource *ds = NULL;
2568
2569         ds = addressbook_find_datasource( node );
2570         if( ds == NULL ) return NULL;
2571         if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
2572         return abf;
2573 }
2574
2575 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
2576         GtkCTreeNode *node;
2577         GtkCTreeRow *row;
2578
2579         /* Remove existing folders and groups */
2580         row = GTK_CTREE_ROW( parent );
2581         if( row ) {
2582                 while( (node = row->children) ) {
2583                         gtk_ctree_remove_node( ctree, node );
2584                 }
2585         }
2586 }
2587
2588 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
2589         GtkCTreeNode *parent, *child;
2590         GtkCTreeRow *currRow;
2591         currRow = GTK_CTREE_ROW( node );
2592         if( currRow ) {
2593                 parent = currRow->parent;
2594                 while( (child = currRow->children) ) {
2595                         gtk_ctree_move( ctree, child, parent, node );
2596                 }
2597                 gtk_sctree_sort_node( ctree, parent );
2598         }
2599 }
2600
2601 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2602         GtkCTree *clist = GTK_CTREE(addrbook.clist);
2603         GtkCTree *ctree;
2604         AddressObject *obj = NULL, *pobj = NULL;
2605         AddressDataSource *ds = NULL;
2606         GtkCTreeNode *node = NULL, *parentNode = NULL;
2607         gchar *name = NULL;
2608         AddressBookFile *abf = NULL;
2609
2610         if( addrbook.listSelected == NULL ) return;
2611         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2612         g_return_if_fail(obj != NULL);
2613
2614         ctree = GTK_CTREE( addrbook.ctree );
2615         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2616         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
2617
2618         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2619         if( ds == NULL ) return;
2620
2621         abf = addressbook_get_book_file();
2622         if( abf == NULL ) return;
2623         if( obj->type == ADDR_ITEM_EMAIL ) {
2624                 ItemEMail *email = ( ItemEMail * ) obj;
2625                 if( email == NULL ) return;
2626                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
2627                         /* Edit parent group */
2628                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
2629                         ItemGroup *itemGrp = adapter->itemGroup;
2630                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2631                         name = ADDRITEM_NAME(itemGrp);
2632                         node = addrbook.treeSelected;
2633                         parentNode = GTK_CTREE_ROW(node)->parent;
2634                 }
2635                 else {
2636                         /* Edit person - email page */
2637                         ItemPerson *person;
2638                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2639                         if( addressbook_edit_person( abf, NULL, person, TRUE ) == NULL ) return;
2640                         addressbook_folder_refresh_one_person( clist, person );
2641                         invalidate_address_completion();
2642                         return;
2643                 }
2644         }
2645         else if( obj->type == ADDR_ITEM_PERSON ) {
2646                 /* Edit person - basic page */
2647                 ItemPerson *person = ( ItemPerson * ) obj;
2648                 if( addressbook_edit_person( abf, NULL, person, FALSE ) == NULL ) return;
2649                 invalidate_address_completion();
2650                 addressbook_folder_refresh_one_person( clist, person );
2651                 return;
2652         }
2653         else if( obj->type == ADDR_ITEM_GROUP ) {
2654                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
2655                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2656                 parentNode = addrbook.treeSelected;
2657                 node = addressbook_find_group_node( parentNode, itemGrp );
2658                 name = ADDRITEM_NAME(itemGrp);
2659         }
2660         else {
2661                 return;
2662         }
2663
2664         /* Update tree node with node name */
2665         if( node == NULL ) return;
2666         addressbook_change_node_name( node, name );
2667         gtk_sctree_sort_node( ctree, parentNode );
2668         gtk_ctree_select( ctree, addrbook.opened ); 
2669 }
2670
2671 static void addressbook_delete_address_cb(gpointer data, guint action,
2672                                           GtkWidget *widget)
2673 {
2674         addressbook_del_clicked(NULL, NULL);
2675 }
2676
2677 static void close_cb(gpointer data, guint action, GtkWidget *widget)
2678 {
2679         addressbook_close();
2680 }
2681
2682 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
2683         addressbook_export_to_file();
2684 }
2685
2686 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2687         if( node ) {
2688                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2689                 if( person ) addritem_person_set_opened( person, TRUE );
2690         }
2691 }
2692
2693 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2694         if( node ) {
2695                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2696                 if( person ) addritem_person_set_opened( person, FALSE );
2697         }
2698 }
2699
2700 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
2701         gchar *str = NULL;
2702         gchar *eMailAlias = ADDRITEM_NAME(email);
2703         if( eMailAlias && *eMailAlias != '\0' ) {
2704                 if( person ) {
2705                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
2706                 }
2707                 else {
2708                         str = g_strdup( eMailAlias );
2709                 }
2710         }
2711         return str;
2712 }
2713
2714 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
2715         GList *items = itemGroup->listEMail;
2716         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2717         for( ; items != NULL; items = g_list_next( items ) ) {
2718                 GtkCTreeNode *nodeEMail = NULL;
2719                 gchar *text[N_COLS];
2720                 ItemEMail *email = items->data;
2721                 ItemPerson *person;
2722                 gchar *str = NULL;
2723
2724                 if( ! email ) continue;
2725
2726                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2727                 str = addressbook_format_item_clist( person, email );
2728                 if( str ) {
2729                         text[COL_NAME] = str;
2730                 }
2731                 else {
2732                         text[COL_NAME] = ADDRITEM_NAME(person);
2733                 }
2734                 text[COL_ADDRESS] = email->address;
2735                 text[COL_REMARKS] = email->remarks;
2736                 nodeEMail = gtk_ctree_insert_node(
2737                                 clist, NULL, NULL,
2738                                 text, FOLDER_SPACING,
2739                                 atci->iconXpm, atci->maskXpm,
2740                                 atci->iconXpmOpen, atci->maskXpmOpen,
2741                                 FALSE, FALSE );
2742                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
2743                 g_free( str );
2744                 str = NULL;
2745         }
2746 }
2747
2748 static void addressbook_folder_load_one_person(
2749                 GtkCTree *clist, ItemPerson *person,
2750                 AddressTypeControlItem *atci,
2751                 AddressTypeControlItem *atciMail )
2752 {
2753         GtkCTreeNode *nodePerson = NULL;
2754         GtkCTreeNode *nodeEMail = NULL;
2755         gchar *text[N_COLS];
2756         gboolean flgFirst = TRUE, haveAddr = FALSE;
2757         GList *node;
2758
2759         if( person == NULL ) return;
2760
2761         text[COL_NAME] = NULL;
2762         node = person->listEMail;
2763         while( node ) {
2764                 ItemEMail *email = node->data;
2765                 gchar *eMailAddr = NULL;
2766                 node = g_list_next( node );
2767
2768                 text[COL_ADDRESS] = email->address;
2769                 text[COL_REMARKS] = email->remarks;
2770                 eMailAddr = ADDRITEM_NAME(email);
2771                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
2772                 if( flgFirst ) {
2773                         /* First email belongs with person */
2774                         gchar *str = addressbook_format_item_clist( person, email );
2775                         if( str ) {
2776                                 text[COL_NAME] = str;
2777                         }
2778                         else {
2779                                 text[COL_NAME] = ADDRITEM_NAME(person);
2780                         }
2781                         nodePerson = gtk_ctree_insert_node(
2782                                         clist, NULL, NULL,
2783                                         text, FOLDER_SPACING,
2784                                         atci->iconXpm, atci->maskXpm,
2785                                         atci->iconXpmOpen, atci->maskXpmOpen,
2786                                         FALSE, person->isOpened );
2787                         g_free( str );
2788                         str = NULL;
2789                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
2790                 }
2791                 else {
2792                         /* Subsequent email is a child node of person */
2793                         text[COL_NAME] = ADDRITEM_NAME(email);
2794                         nodeEMail = gtk_ctree_insert_node(
2795                                         clist, nodePerson, NULL,
2796                                         text, FOLDER_SPACING,
2797                                         atciMail->iconXpm, atciMail->maskXpm,
2798                                         atciMail->iconXpmOpen, atciMail->maskXpmOpen,
2799                                         FALSE, TRUE );
2800                         gtk_ctree_node_set_row_data(clist, nodeEMail, email );
2801                 }
2802                 flgFirst = FALSE;
2803                 haveAddr = TRUE;
2804         }
2805         if( ! haveAddr ) {
2806                 /* Have name without EMail */
2807                 text[COL_NAME] = ADDRITEM_NAME(person);
2808                 text[COL_ADDRESS] = NULL;
2809                 text[COL_REMARKS] = NULL;
2810                 nodePerson = gtk_ctree_insert_node(
2811                                 clist, NULL, NULL,
2812                                 text, FOLDER_SPACING,
2813                                 atci->iconXpm, atci->maskXpm,
2814                                 atci->iconXpmOpen, atci->maskXpmOpen,
2815                                 FALSE, person->isOpened );
2816                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
2817         }
2818         return;
2819 }
2820
2821 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
2822         GList *items;
2823         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
2824         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2825
2826         if( atci == NULL ) return;
2827         if( atciMail == NULL ) return;
2828
2829         /* Load email addresses */
2830         items = addritem_folder_get_person_list( itemFolder );
2831         for( ; items != NULL; items = g_list_next( items ) ) {
2832                 addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
2833         }
2834         /* Free up the list */
2835         mgu_clear_list( items );
2836         g_list_free( items );
2837 }
2838
2839 static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node ) { 
2840         addrbook.listSelected = NULL;
2841         gtk_ctree_remove_node( clist, node );
2842         addressbook_menubar_set_sensitive( FALSE );
2843         addressbook_menuitem_set_sensitive(
2844                 gtk_ctree_node_get_row_data(
2845                         GTK_CTREE(clist), addrbook.treeSelected ),
2846                 addrbook.treeSelected );
2847 }
2848
2849 static void addressbook_folder_refresh_one_person( GtkCTree *clist, ItemPerson *person ) {
2850         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
2851         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2852         GtkCTreeNode *node;
2853         if( atci == NULL ) return;
2854         if( atciMail == NULL ) return;
2855         if( person == NULL ) return;
2856         /* unload the person */
2857         
2858         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2859         if( node )
2860                 addressbook_folder_remove_node( clist, node );
2861         addressbook_folder_load_one_person( clist, person, atci, atciMail );
2862         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2863         if( node ) {
2864                 gtk_ctree_select( clist, node );
2865                 if (!gtk_ctree_node_is_visible( clist, node ) ) 
2866                         gtk_ctree_node_moveto( clist, node, 0, 0, 0 );
2867         }
2868 }
2869
2870 static void addressbook_folder_remove_one_person( GtkCTree *clist, ItemPerson *person ) {
2871         GtkCTreeNode *node;
2872         gint row;
2873         
2874         if( person == NULL ) return;
2875         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2876         row  = gtk_clist_find_row_from_data( GTK_CLIST(clist), person );
2877         if( node ) {
2878                 addressbook_folder_remove_node( clist, node );
2879         }
2880 }
2881
2882 static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
2883         GList *items;
2884         AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
2885
2886         /* Load any groups */
2887         if( ! atci ) return;
2888         items = addritem_folder_get_group_list( itemFolder );
2889         for( ; items != NULL; items = g_list_next( items ) ) {
2890                 GtkCTreeNode *nodeGroup = NULL;
2891                 gchar *text[N_COLS];
2892                 ItemGroup *group = items->data;
2893                 if( group == NULL ) continue;
2894                 text[COL_NAME] = ADDRITEM_NAME(group);
2895                 text[COL_ADDRESS] = NULL;
2896                 text[COL_REMARKS] = NULL;
2897                 nodeGroup = gtk_ctree_insert_node(clist, NULL, NULL,
2898                                       text, FOLDER_SPACING,
2899                                       atci->iconXpm, atci->maskXpm,
2900                                       atci->iconXpmOpen, atci->maskXpmOpen,
2901                                       FALSE, FALSE);
2902                 gtk_ctree_node_set_row_data(clist, nodeGroup, group );
2903                 gtk_sctree_sort_node(clist, NULL);
2904         }
2905         /* Free up the list */
2906         mgu_clear_list( items );
2907         g_list_free( items );
2908 }
2909
2910 /**
2911  * Search ctree widget callback function.
2912  * \param  pA Pointer to node.
2913  * \param  pB Pointer to data item being sought.
2914  * \return Zero (0) if group found.
2915  */
2916 static int addressbook_treenode_find_group_cb( gconstpointer pA, gconstpointer pB ) {
2917         AddressObject *aoA;
2918
2919         aoA = ( AddressObject * ) pA;
2920         if( aoA->type == ADDR_ITEM_GROUP ) {
2921                 ItemGroup *group, *grp;
2922
2923                 grp = ADAPTER_GROUP(aoA)->itemGroup;
2924                 group = ( ItemGroup * ) pB;
2925                 if( grp == group ) return 0;    /* Found group */
2926         }
2927         return 1;
2928 }
2929
2930 /**
2931  * Search ctree widget callback function.
2932  * \param  pA Pointer to node.
2933  * \param  pB Pointer to data item being sought.
2934  * \return Zero (0) if folder found.
2935  */
2936 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2937         AddressObject *aoA;
2938
2939         aoA = ( AddressObject * ) pA;
2940         if( aoA->type == ADDR_ITEM_FOLDER ) {
2941                 ItemFolder *folder, *fld;
2942
2943                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
2944                 folder = ( ItemFolder * ) pB;
2945                 if( fld == folder ) return 0;   /* Found folder */
2946         }
2947         return 1;
2948 }
2949
2950 /*
2951 * Remove folder and group nodes from tree widget for items contained ("cut")
2952 * in clipboard.
2953 */
2954 static void addressbook_treenode_remove_item( void ) {
2955         GList *node;
2956         AddrSelectItem *cutItem;
2957         AddressCache *cache;
2958         AddrItemObject *aio;
2959         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2960         GtkCTreeNode *tn;
2961
2962         node = _clipBoard_->objectList;
2963         while( node ) {
2964                 cutItem = node->data;
2965                 node = g_list_next( node );
2966                 cache = addrindex_get_cache(
2967                         _clipBoard_->addressIndex, cutItem->cacheID );
2968                 if( cache == NULL ) continue;
2969                 aio = addrcache_get_object( cache, cutItem->uid );
2970                 if( aio ) {
2971                         tn = NULL;
2972                         if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
2973                                 ItemFolder *folder;
2974
2975                                 folder = ( ItemFolder * ) aio;
2976                                 tn = gtk_ctree_find_by_row_data_custom(
2977                                         ctree, NULL, folder,
2978                                         addressbook_treenode_find_folder_cb );
2979                         }
2980                         else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
2981                                 ItemGroup *group;
2982
2983                                 group = ( ItemGroup * ) aio;
2984                                 tn = gtk_ctree_find_by_row_data_custom(
2985                                         ctree, NULL, group,
2986                                         addressbook_treenode_find_group_cb );
2987                         }
2988
2989                         if( tn ) {
2990                                 /* Free up adapter and remove node. */
2991                                 gtk_ctree_remove_node( ctree, tn );
2992                         }
2993                 }
2994         }
2995 }
2996
2997 /**
2998  * Find parent datasource for specified tree node.
2999  * \param  node Node to test.
3000  * \return Data source, or NULL if not found.
3001  */
3002 static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
3003         AddressDataSource *ds = NULL;
3004         AddressObject *ao;
3005
3006         g_return_val_if_fail(addrbook.ctree != NULL, NULL);
3007
3008         while( node ) {
3009                 if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
3010                 ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3011                 if( ao ) {
3012                         /* printf( "ao->type = %d\n", ao->type ); */
3013                         if( ao->type == ADDR_DATASOURCE ) {
3014                                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
3015                                 /* printf( "found it\n" ); */
3016                                 ds = ads->dataSource;
3017                                 break;
3018                         }
3019                 }
3020                 node = GTK_CTREE_ROW(node)->parent;
3021         }
3022         return ds;
3023 }
3024
3025 /**
3026  * Load address list widget with children of specified object.
3027  * \param obj Parent object to be loaded.
3028  */
3029 static void addressbook_set_clist( AddressObject *obj ) {
3030         GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
3031         GtkCList *clist = GTK_CLIST(addrbook.clist);
3032         AddressDataSource *ds = NULL;
3033         AdapterDSource *ads = NULL;
3034
3035         if( obj == NULL ) {
3036                 gtk_clist_clear(clist);
3037                 return;
3038         }
3039
3040         if( obj->type == ADDR_INTERFACE ) {
3041                 /* printf( "set_clist: loading datasource...\n" ); */
3042                 /* addressbook_node_load_datasource( GTK_CTREE(clist), obj ); */
3043                 return;
3044         }
3045
3046         gtk_clist_freeze(clist);
3047         gtk_clist_clear(clist);
3048
3049         if( obj->type == ADDR_DATASOURCE ) {
3050                 ads = ADAPTER_DSOURCE(obj);
3051                 ds = ADAPTER_DSOURCE(obj)->dataSource;
3052                 if( ds ) {
3053                         /* Load root folder */
3054                         ItemFolder *rootFolder = NULL;
3055                         rootFolder = addrindex_ds_get_root_folder( ds );
3056                         addressbook_folder_load_person(
3057                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3058                         addressbook_folder_load_group(
3059                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3060                 }
3061         }
3062         else {
3063                 if( obj->type == ADDR_ITEM_GROUP ) {
3064                         /* Load groups */
3065                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
3066                         addressbook_load_group( ctreelist, itemGroup );
3067                 }
3068                 else if( obj->type == ADDR_ITEM_FOLDER ) {
3069                         /* Load folders */
3070                         ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
3071                         addressbook_folder_load_person( ctreelist, itemFolder );
3072                         addressbook_folder_load_group( ctreelist, itemFolder );
3073                 }
3074         }
3075         gtk_sctree_sort_node(GTK_CTREE(clist), NULL);
3076         gtk_clist_thaw(clist);
3077 }
3078
3079 /**
3080  * Call back function to free adaptor. Call back is setup by function
3081  * gtk_ctree_node_set_row_data_full() when node is populated. This function is
3082  * called when the address book tree widget node is removed by calling
3083  * function gtk_ctree_remove_node().
3084  * 
3085  * \param data Tree node's row data.
3086  */
3087 static void addressbook_free_treenode( gpointer data ) {
3088         AddressObject *ao;
3089
3090         ao = ( AddressObject * ) data;
3091         if( ao == NULL ) return;
3092         if( ao->type == ADDR_INTERFACE ) {
3093                 AdapterInterface *ai = ADAPTER_INTERFACE(ao);
3094                 addrbookctl_free_interface( ai );
3095         }
3096         else if( ao->type == ADDR_DATASOURCE ) {
3097                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
3098                 addrbookctl_free_datasource( ads );
3099         }
3100         else if( ao->type == ADDR_ITEM_FOLDER ) {
3101                 AdapterFolder *af = ADAPTER_FOLDER(ao);
3102                 addrbookctl_free_folder( af );
3103         }
3104         else if( ao->type == ADDR_ITEM_GROUP ) {
3105                 AdapterGroup *ag = ADAPTER_GROUP(ao);
3106                 addrbookctl_free_group( ag );
3107         }
3108 }
3109
3110 /*
3111 * Create new adaptor for specified data source.
3112 */
3113 AdapterDSource *addressbook_create_ds_adapter( AddressDataSource *ds,
3114                                 AddressObjectType otype, gchar *name )
3115 {
3116         AdapterDSource *adapter = g_new0( AdapterDSource, 1 );
3117         ADDRESS_OBJECT(adapter)->type = ADDR_DATASOURCE;
3118         ADDRESS_OBJECT_NAME(adapter) = g_strdup( name );
3119         adapter->dataSource = ds;
3120         adapter->subType = otype;
3121         return adapter;
3122 }
3123
3124 void addressbook_ads_set_name( AdapterDSource *adapter, gchar *value ) {
3125         ADDRESS_OBJECT_NAME(adapter) =
3126                 mgu_replace_string( ADDRESS_OBJECT_NAME(adapter), value );
3127 }
3128
3129 /*
3130  * Load tree from address index with the initial data.
3131  */
3132 static void addressbook_load_tree( void ) {
3133         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
3134         GList *nodeIf, *nodeDS;
3135         AdapterInterface *adapter;
3136         AddressInterface *iface;
3137         AddressTypeControlItem *atci;
3138         AddressDataSource *ds;
3139         AdapterDSource *ads;
3140         GtkCTreeNode *node, *newNode;
3141         gchar *name;
3142
3143         nodeIf = _addressInterfaceList_;
3144         while( nodeIf ) {
3145                 adapter = nodeIf->data;
3146                 node = adapter->treeNode;
3147                 iface = adapter->interface;
3148                 atci = adapter->atci;
3149                 if( iface ) {
3150                         if( iface->useInterface ) {
3151                                 /* Load data sources below interface node */
3152                                 nodeDS = iface->listSource;
3153                                 while( nodeDS ) {
3154                                         ds = nodeDS->data;
3155                                         newNode = NULL;
3156                                         name = addrindex_ds_get_name( ds );
3157                                         ads = addressbook_create_ds_adapter(
3158                                                         ds, atci->objectType, name );
3159                                         newNode = addressbook_add_object(
3160                                                         node, ADDRESS_OBJECT(ads) );
3161                                         nodeDS = g_list_next( nodeDS );
3162                                 }
3163                                 gtk_ctree_expand( ctree, node );
3164                         }
3165                 }
3166                 nodeIf = g_list_next( nodeIf );
3167         }
3168 }
3169
3170 /*
3171  * Convert the old address book to new format.
3172  */
3173 static gboolean addressbook_convert( AddressIndex *addrIndex ) {
3174         gboolean retVal = FALSE;
3175         gboolean errFlag = TRUE;
3176         gchar *msg = NULL;
3177
3178         /* Read old address book, performing conversion */
3179         debug_print( "Reading and converting old address book...\n" );
3180         addrindex_set_file_name( addrIndex, ADDRESSBOOK_OLD_FILE );
3181         addrindex_read_data( addrIndex );
3182         if( addrIndex->retVal == MGU_NO_FILE ) {
3183                 /* We do not have a file - new user */
3184                 debug_print( "New user... create new books...\n" );
3185                 addrindex_create_new_books( addrIndex );
3186                 if( addrIndex->retVal == MGU_SUCCESS ) {
3187                         /* Save index file */
3188                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
3189                         addrindex_save_data( addrIndex );
3190                         if( addrIndex->retVal == MGU_SUCCESS ) {
3191                                 retVal = TRUE;
3192                                 errFlag = FALSE;
3193                         }
3194                         else {
3195                                 msg = _( "New user, could not save index file." );
3196                         }
3197                 }
3198                 else {
3199                         msg = _( "New user, could not save address book files." );
3200                 }
3201         }
3202         else {
3203                 /* We have an old file */
3204                 if( addrIndex->wasConverted ) {
3205                         /* Converted successfully - save address index */
3206                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
3207                         addrindex_save_data( addrIndex );
3208                         if( addrIndex->retVal == MGU_SUCCESS ) {
3209                                 msg = _( "Old address book converted successfully." );
3210                                 retVal = TRUE;
3211                                 errFlag = FALSE;
3212                         }
3213                         else {
3214                                 msg = _("Old address book converted,\n"
3215                                         "could not save new address index file" );
3216                         }
3217                 }
3218                 else {
3219                         /* File conversion failed - just create new books */
3220                         debug_print( "File conversion failed... just create new books...\n" );
3221                         addrindex_create_new_books( addrIndex );
3222                         if( addrIndex->retVal == MGU_SUCCESS ) {
3223                                 /* Save index */
3224                                 addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
3225                                 addrindex_save_data( addrIndex );
3226                                 if( addrIndex->retVal == MGU_SUCCESS ) {
3227                                         msg = _("Could not convert address book,\n"
3228                                                 "but created empty new address book files." );
3229                                         retVal = TRUE;
3230                                         errFlag = FALSE;
3231                                 }
3232                                 else {
3233                                         msg = _("Could not convert address book,\n"
3234                                                 "could not create new address book files." );
3235                                 }
3236                         }
3237                         else {
3238                                 msg = _("Could not convert address book\n"
3239                                         "and could not create new address book files." );
3240                         }
3241                 }
3242         }
3243         if( errFlag ) {
3244                 debug_print( "Error\n%s\n", msg );
3245                 alertpanel_with_type( _( "Addressbook conversion error" ), msg, GTK_STOCK_CLOSE, 
3246                                       NULL, NULL, NULL, ALERT_ERROR );
3247         }
3248         else if( msg ) {
3249                 debug_print( "Warning\n%s\n", msg );
3250                 alertpanel_with_type( _( "Addressbook conversion" ), msg, GTK_STOCK_CLOSE, 
3251                                       NULL, NULL, NULL, ALERT_WARNING );
3252         }
3253
3254         return retVal;
3255 }
3256
3257 void addressbook_read_file( void ) {
3258         AddressIndex *addrIndex = NULL;
3259
3260         debug_print( "Reading address index...\n" );
3261         if( _addressIndex_ ) {
3262                 debug_print( "address book already read!!!\n" );
3263                 return;
3264         }
3265
3266         addrIndex = addrindex_create_index();
3267         addrindex_initialize();
3268
3269         /* Use new address book index. */
3270         addrindex_set_file_path( addrIndex, get_rc_dir() );
3271         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
3272         addrindex_read_data( addrIndex );
3273         if( addrIndex->retVal == MGU_NO_FILE ) {
3274                 /* Conversion required */
3275                 debug_print( "Converting...\n" );
3276                 if( addressbook_convert( addrIndex ) ) {
3277                         _addressIndex_ = addrIndex;
3278                 }
3279         }