addressbook-config.c 7.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  Copyright (C) 2009 Savoir-Faire Linux inc.
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *  
 *  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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "addressbook-config.h"

22 23
AddressBook_Config *addressbook_config;

24 25 26 27 28 29 30 31 32 33 34 35 36 37
void addressbook_load_parameters (AddressBook_Config **settings) {

    GHashTable *_params = NULL;
    AddressBook_Config *_settings;

    // Allocate a struct
    _settings = g_new0 (AddressBook_Config, 1);
    
    // Fetch the settings from D-Bus
    _params = (GHashTable*) dbus_get_addressbook_settings ();

    if (_params == NULL) {
        _settings->max_results = 30;
        _settings->display_contact_photo = 0;
38 39 40
        _settings->search_phone_business = 1;
        _settings->search_phone_home = 1;
        _settings->search_phone_mobile = 1;
41 42
    }
    else {
43 44 45 46 47
        _settings->max_results =  (guint)(g_hash_table_lookup (_params, ADDRESSBOOK_MAX_RESULTS));
        _settings->display_contact_photo = (guint) (g_hash_table_lookup (_params, ADDRESSBOOK_DISPLAY_CONTACT_PHOTO));
        _settings->search_phone_business = (guint) (g_hash_table_lookup (_params, ADDRESSBOOK_DISPLAY_PHONE_BUSINESS));
        _settings->search_phone_home = (guint) (g_hash_table_lookup (_params, ADDRESSBOOK_DISPLAY_PHONE_HOME));
        _settings->search_phone_mobile = (guint) (g_hash_table_lookup (_params, ADDRESSBOOK_DISPLAY_PHONE_MOBILE));
48 49 50 51 52
    }
    
    *settings = _settings;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void addressbook_save_parameters (void) {

    GHashTable *params = NULL;
    
    params = g_hash_table_new (NULL, g_str_equal);
    g_hash_table_replace (params, (gpointer)ADDRESSBOOK_MAX_RESULTS, (gpointer)addressbook_config->max_results);
    g_hash_table_replace (params, (gpointer)ADDRESSBOOK_DISPLAY_CONTACT_PHOTO, (gpointer)addressbook_config->display_contact_photo);
    g_hash_table_replace (params, (gpointer)ADDRESSBOOK_DISPLAY_PHONE_BUSINESS, (gpointer)addressbook_config->search_phone_business);
    g_hash_table_replace (params, (gpointer)ADDRESSBOOK_DISPLAY_PHONE_HOME, (gpointer)addressbook_config->search_phone_home);
    g_hash_table_replace (params, (gpointer)ADDRESSBOOK_DISPLAY_PHONE_MOBILE, (gpointer)addressbook_config->search_phone_mobile);

    dbus_set_addressbook_settings (params);

    // Decrement the reference count
    g_hash_table_unref (params);
}

70 71
static void max_results_cb (GtkRange* scale, gpointer user_data) {

72
    addressbook_config->max_results = (guint) gtk_range_get_value (GTK_RANGE (scale));
73 74
}

75
static void display_contact_photo_cb (GtkWidget *widget) {
76

77
    addressbook_config->display_contact_photo = (guint) gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
78 79
}

80
static void search_phone_business_cb (GtkWidget *widget) {
81

82
    addressbook_config->search_phone_business = (guint) gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
83 84
}

85
static void search_phone_home_cb (GtkWidget *widget) {
86

87
    addressbook_config->search_phone_home = (guint) gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
88 89
}

90
static void search_phone_mobile_cb (GtkWidget *widget) {
91

92
    addressbook_config->search_phone_mobile = (guint) gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
93
}
94 95 96

GtkWidget* create_addressbook_settings () {
    
97
    GtkWidget *ret, *result_frame, *box, *value, *label, *photo, *item;
98 99

    // Load the user value
100
    addressbook_load_parameters (&addressbook_config);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    ret = gtk_vbox_new(FALSE, 10);
    gtk_container_set_border_width(GTK_CONTAINER(ret), 10);

    result_frame = gtk_frame_new(_("Search Parameters"));
    gtk_box_pack_start(GTK_BOX(ret), result_frame, FALSE, FALSE, 0);
    gtk_widget_show (result_frame);

    box = gtk_vbox_new( FALSE , 1);
    gtk_widget_show (box);
    gtk_container_add (GTK_CONTAINER(result_frame) , box);

    // SCALE BUTTON - NUMBER OF RESULTS
    label = gtk_label_new (_("Maximum result number for a request: "));
    gtk_box_pack_start (GTK_BOX(box) , label , FALSE , FALSE , 1);
    value = gtk_hscale_new_with_range (0.0 , 50.0 , 5.0);
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), value);
    gtk_scale_set_digits (GTK_SCALE(value) , 0);
    gtk_scale_set_value_pos (GTK_SCALE(value) , GTK_POS_RIGHT); 
120
    gtk_range_set_value (GTK_RANGE( value ) , addressbook_config->max_results);
121
    gtk_box_pack_start (GTK_BOX(box) , value , TRUE , TRUE , 0);
122
    g_signal_connect (G_OBJECT (value) , "value-changed" , G_CALLBACK(max_results_cb), NULL );
123 124 125

    // PHOTO DISPLAY
    photo = gtk_check_button_new_with_mnemonic( _("_Display contact photo if available"));
126 127
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(photo), addressbook_config->display_contact_photo);
    g_signal_connect (G_OBJECT(photo) , "clicked" , G_CALLBACK (display_contact_photo_cb), NULL);
128 129
    gtk_box_pack_start (GTK_BOX(box) , photo , TRUE , TRUE , 1);
     
130 131 132 133
    label = gtk_label_new (_("Search for and display: "));
    gtk_box_pack_start (GTK_BOX(box) , label , FALSE , FALSE , 1);
    
    item = gtk_check_button_new_with_mnemonic( _("_Business phone"));
134 135
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(item), addressbook_config->search_phone_business);
    g_signal_connect (G_OBJECT(item) , "clicked" , G_CALLBACK (search_phone_business_cb) , NULL);
136 137 138
    gtk_box_pack_start (GTK_BOX(box) , item , TRUE , TRUE , 1);
     
    item = gtk_check_button_new_with_mnemonic( _("_Home phone"));
139 140
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(item), addressbook_config->search_phone_home);
    g_signal_connect (G_OBJECT(item) , "clicked" , G_CALLBACK (search_phone_home_cb) , NULL);
141 142 143
    gtk_box_pack_start (GTK_BOX(box) , item , TRUE , TRUE , 1);
     
    item = gtk_check_button_new_with_mnemonic( _("_Mobile phone"));
144 145
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(item), addressbook_config->search_phone_mobile);
    g_signal_connect (G_OBJECT(item) , "clicked" , G_CALLBACK (search_phone_mobile_cb) , NULL);
146 147
    gtk_box_pack_start (GTK_BOX(box) , item , TRUE , TRUE , 1);
     
148 149 150 151 152
    gtk_widget_show_all(ret);

    return ret;

}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

gboolean addressbook_display (AddressBook_Config *settings, const gchar *field) {

    gboolean display = FALSE;

    if (g_strcasecmp (field, ADDRESSBOOK_DISPLAY_CONTACT_PHOTO) == 0)
        display = (settings->display_contact_photo == 1)? TRUE : FALSE;

    else if (g_strcasecmp (field, ADDRESSBOOK_DISPLAY_PHONE_BUSINESS) == 0)
        display = (settings->search_phone_business == 1)? TRUE : FALSE;

    else if (g_strcasecmp (field, ADDRESSBOOK_DISPLAY_PHONE_HOME) == 0)
        display = (settings->search_phone_home == 1)? TRUE : FALSE;

    else if (g_strcasecmp (field, ADDRESSBOOK_DISPLAY_PHONE_MOBILE) == 0)
        display = (settings->search_phone_mobile == 1)? TRUE : FALSE;

    else
        display = FALSE;

    return display;
}