claws-mail/src/addressbook_foldersel.c
Tristan Chabredier f8e892d239 2006-08-14 [wwp] 2.4.0cvs49
* src/addressbook_foldersel.c
		fix strict ISO C90 compliance.
2006-08-14 14:31:05 +00:00

432 lines
13 KiB
C

/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 2001-2006 Match Grun and the Sylpheed-Claws team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Add address to address book dialog.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "defs.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkhbbox.h>
#include <gtk/gtkbutton.h>
#include "gtkutils.h"
#include "stock_pixmap.h"
#include "prefs_common.h"
#include "addressadd.h"
#include "addritem.h"
#include "addrbook.h"
#include "addrindex.h"
#include "manage_window.h"
typedef struct {
AddressBookFile *book;
ItemFolder *folder;
} FolderInfo;
typedef struct {
gchar **folder_path;
gboolean matched;
gint index;
GtkCTreeNode *node;
} FolderPathMatch;
static struct _AddressBookFolderSel_dlg {
GtkWidget *window;
GtkWidget *tree_folder;
GtkWidget *ok_btn;
GtkWidget *cancel_btn;
gint status_cid;
FolderInfo *fiSelected;
} addressbook_foldersel_dlg;
static GdkPixmap *folderXpm;
static GdkBitmap *folderXpmMask;
static GdkPixmap *bookXpm;
static GdkBitmap *bookXpmMask;
static gboolean addressbook_foldersel_cancelled;
static FolderInfo *addressbook_foldersel_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
{
FolderInfo *fi = g_new0( FolderInfo, 1 );
fi->book = abf;
fi->folder = folder;
return fi;
}
static void addressbook_foldersel_free_folderinfo( FolderInfo *fi ) {
fi->book = NULL;
fi->folder = NULL;
g_free( fi );
}
static gint addressbook_foldersel_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled )
{
addressbook_foldersel_cancelled = TRUE;
gtk_main_quit();
return TRUE;
}
static gboolean addressbook_foldersel_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled )
{
if ( event && event->keyval == GDK_Escape ) {
addressbook_foldersel_cancelled = TRUE;
gtk_main_quit();
}
return FALSE;
}
static void addressbook_foldersel_ok( GtkWidget *widget, gboolean *cancelled )
{
addressbook_foldersel_cancelled = FALSE;
gtk_main_quit();
}
static void addressbook_foldersel_cancel( GtkWidget *widget, gboolean *cancelled )
{
addressbook_foldersel_cancelled = TRUE;
gtk_main_quit();
}
static void addressbook_foldersel_folder_select( GtkCTree *ctree, gint row, gint column,
GdkEvent *event, gpointer data )
{
addressbook_foldersel_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
}
static gboolean addressbook_foldersel_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data )
{
if ( ! event )
return FALSE;
if ( event->button == 1 ) {
/* Handle double click */
if ( event->type == GDK_2BUTTON_PRESS ) {
addressbook_foldersel_cancelled = FALSE;
gtk_main_quit();
}
}
return FALSE;
}
static void addressbook_foldersel_size_allocate_cb(GtkWidget *widget,
GtkAllocation *allocation)
{
g_return_if_fail(allocation != NULL);
prefs_common.addressbook_folderselwin_width = allocation->width;
prefs_common.addressbook_folderselwin_height = allocation->height;
}
static void addressbook_foldersel_create( void )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *tree_folder;
GtkWidget *vlbox;
GtkWidget *tree_win;
GtkWidget *hbbox;
GtkWidget *ok_btn;
GtkWidget *cancel_btn;
static GdkGeometry geometry;
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
gtk_window_set_title( GTK_WINDOW(window), _("Select Address Book Folder") );
gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
gtk_window_set_modal( GTK_WINDOW(window), TRUE );
g_signal_connect( G_OBJECT(window), "delete_event",
G_CALLBACK(addressbook_foldersel_delete_event), NULL );
g_signal_connect( G_OBJECT(window), "key_press_event",
G_CALLBACK(addressbook_foldersel_key_pressed), NULL );
g_signal_connect(G_OBJECT(window), "size_allocate",
G_CALLBACK(addressbook_foldersel_size_allocate_cb), NULL);
vbox = gtk_vbox_new(FALSE, 8);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
/* Address book/folder tree */
vlbox = gtk_vbox_new(FALSE, 8);
gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
tree_win = gtk_scrolled_window_new( NULL, NULL );
gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC );
gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
tree_folder = gtk_ctree_new( 1, 0 );
gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Address Book" ) );
gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
/* Button panel */
gtkut_stock_button_set_create( &hbbox, &cancel_btn, GTK_STOCK_CANCEL,
&ok_btn, GTK_STOCK_OK,
NULL, NULL );
gtk_box_pack_end( GTK_BOX(vbox), hbbox, FALSE, FALSE, 0 );
gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
gtk_widget_grab_default( ok_btn );
g_signal_connect( G_OBJECT(ok_btn), "clicked",
G_CALLBACK(addressbook_foldersel_ok), NULL );
g_signal_connect( G_OBJECT(cancel_btn), "clicked",
G_CALLBACK(addressbook_foldersel_cancel), NULL );
g_signal_connect( G_OBJECT(tree_folder), "select_row",
G_CALLBACK(addressbook_foldersel_folder_select), NULL );
g_signal_connect( G_OBJECT(tree_folder), "button_press_event",
G_CALLBACK(addressbook_foldersel_tree_button), NULL );
if ( !geometry.min_height ) {
geometry.min_width = 300;
geometry.min_height = 350;
}
gtk_window_set_geometry_hints( GTK_WINDOW(window), NULL, &geometry,
GDK_HINT_MIN_SIZE );
gtk_widget_set_size_request( window, prefs_common.addressbook_folderselwin_width,
prefs_common.addressbook_folderselwin_height );
gtk_widget_show_all( vbox );
addressbook_foldersel_dlg.window = window;
addressbook_foldersel_dlg.tree_folder = tree_folder;
addressbook_foldersel_dlg.ok_btn = ok_btn;
addressbook_foldersel_dlg.cancel_btn = cancel_btn;
gtk_widget_show_all( window );
stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
&folderXpm, &folderXpmMask );
}
static void addressbook_foldersel_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
FolderInfo *fiParent, FolderPathMatch *match )
{
GtkCTree *tree = GTK_CTREE( addressbook_foldersel_dlg.tree_folder );
GList *list;
ItemFolder *folder;
gchar *fName;
gchar **name;
GtkCTreeNode *node;
FolderInfo *fi;
FolderPathMatch *nextmatch = NULL;
list = parentFolder->listFolder;
while ( list ) {
folder = list->data;
fName = g_strdup( ADDRITEM_NAME(folder) );
name = &fName;
node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
folderXpm, folderXpmMask, folderXpm, folderXpmMask,
FALSE, TRUE );
/* match folder name, match pointer will be set to NULL if next recursive call
doesn't need to match subfolder name */
if ( match != NULL &&
match->matched == FALSE ) {
if ( strcmp(match->folder_path[match->index], folder->obj.uid) == 0 ) {
/* folder name matches, prepare next subfolder match */
debug_print("matched folder name '%s'\n", fName);
match->index++;
if ( match->folder_path[match->index] == NULL ) {
/* we've matched all elements */
match->matched = TRUE;
match->node = node;
debug_print("book/folder path matched!\n");
} else {
/* keep on matching */
nextmatch = match;
}
}
}
g_free( fName );
fi = addressbook_foldersel_create_folderinfo( fiParent->book, folder );
gtk_ctree_node_set_row_data_full( tree, node, fi,
( GtkDestroyNotify ) addressbook_foldersel_free_folderinfo );
addressbook_foldersel_load_folder( node, folder, fi, nextmatch );
list = g_list_next( list );
}
}
static void addressbook_foldersel_load_data( AddressIndex *addrIndex, gchar *path, FolderPathMatch* match )
{
AddressDataSource *ds;
GList *list, *nodeDS;
gchar **name;
gchar *dsName;
ItemFolder *rootFolder;
AddressBookFile *abf;
FolderInfo *fi;
GtkCTree *tree = GTK_CTREE( addressbook_foldersel_dlg.tree_folder );
GtkCTreeNode *node;
FolderPathMatch *nextmatch;
gtk_clist_clear( GTK_CLIST( tree ) );
list = addrindex_get_interface_list( addrIndex );
while ( list ) {
AddressInterface *interface = list->data;
if ( interface->type == ADDR_IF_BOOK ) {
nodeDS = interface->listSource;
while ( nodeDS ) {
ds = nodeDS->data;
dsName = g_strdup( addrindex_ds_get_name( ds ) );
/* Read address book */
if( ! addrindex_ds_get_read_flag( ds ) ) {
addrindex_ds_read_data( ds );
}
/* Add node for address book */
abf = ds->rawDataSource;
name = &dsName;
node = gtk_sctree_insert_node( tree, NULL, NULL,
name, FOLDER_SPACING, bookXpm,
bookXpmMask, bookXpm, bookXpmMask,
FALSE, TRUE );
g_free( dsName );
/* try to match subfolders if this book is the right book
(and if there's smth to match, and not yet matched) */
nextmatch = NULL;
if ( match->folder_path != NULL &&
match->matched == FALSE &&
match->folder_path[0] != NULL &&
strcmp(match->folder_path[0], abf->fileName) == 0 ) {
debug_print("matched book name '%s'\n", abf->fileName);
match->index = 1;
if ( match->folder_path[match->index] == NULL ) {
/* we've matched all elements */
match->matched = TRUE;
match->node = node;
debug_print("book path matched!\n");
} else {
/* keep on matching */
nextmatch = match;
}
}
fi = addressbook_foldersel_create_folderinfo( abf, NULL );
gtk_ctree_node_set_row_data_full( tree, node, fi,
( GtkDestroyNotify ) addressbook_foldersel_free_folderinfo );
rootFolder = addrindex_ds_get_root_folder( ds );
addressbook_foldersel_load_folder( node, rootFolder, fi, nextmatch );
nodeDS = g_list_next( nodeDS );
}
}
list = g_list_next( list );
}
}
gboolean addressbook_foldersel_selection( AddressIndex *addrIndex,
AddressBookFile **book, ItemFolder **folder, gchar* path)
{
FolderPathMatch folder_path_match = { NULL, FALSE, 0, NULL };
gboolean retVal = FALSE;
addressbook_foldersel_cancelled = FALSE;
if ( ! addressbook_foldersel_dlg.window )
addressbook_foldersel_create();
gtk_widget_grab_focus(addressbook_foldersel_dlg.ok_btn);
gtk_widget_show(addressbook_foldersel_dlg.window);
manage_window_set_transient(GTK_WINDOW(addressbook_foldersel_dlg.window));
addressbook_foldersel_dlg.fiSelected = NULL;
/* split the folder path we've received, we'll try to match this path, subpath by
subpath against the book/folder structure in order to select the folder that
corresponds to what we received */
if ( path != NULL ) {
if ( strcasecmp(path, _("Any")) == 0 || *path == '\0' )
/* consider "Any" and "" as valid addressbook root */
folder_path_match.matched = TRUE;
else
folder_path_match.folder_path = g_strsplit( path, "/", 256 );
}
addressbook_foldersel_load_data( addrIndex, path, &folder_path_match );
if ( folder_path_match.folder_path != NULL && folder_path_match.matched == FALSE)
g_warning("addressbook_foldersel_load_data: couldn't match book/folder path '%s'\n", path);
g_strfreev( folder_path_match.folder_path );
if ( folder_path_match.node != NULL)
gtk_ctree_select( GTK_CTREE( addressbook_foldersel_dlg.tree_folder ),
GTK_CTREE_NODE( folder_path_match.node ) );
else
gtk_clist_select_row( GTK_CLIST( addressbook_foldersel_dlg.tree_folder ), 0, 0 );
gtk_widget_show(addressbook_foldersel_dlg.window);
gtk_main();
gtk_widget_hide( addressbook_foldersel_dlg.window );
if ( ! addressbook_foldersel_cancelled ) {
*book = NULL;
*folder = NULL;
if ( addressbook_foldersel_dlg.fiSelected ) {
*book = addressbook_foldersel_dlg.fiSelected->book;
*folder = addressbook_foldersel_dlg.fiSelected->folder;
retVal = TRUE;
}
}
gtk_clist_clear( GTK_CLIST( addressbook_foldersel_dlg.tree_folder ) );
return retVal;
}
/*
* End of Source.
*/