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