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