aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/exchange-operations/exchange-contacts.c
blob: 6a5cb754ce027456eca68eea8b619a7b409d8f19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Praveen Kumar <kpraveen@novell.com>
 * Copyright (C) 2005 Novell, Inc.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <string.h>
#include <gtk/gtk.h>
#include <e-util/e-config.h>
#include <calendar/gui/e-cal-config.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-url.h>
#include <e-folder.h>
#include <exchange-account.h>

#include <libebook/e-book.h>
#include <libecal/e-cal.h>
#include <addressbook/gui/widgets/eab-config.h>

#include "mail/em-account-editor.h"
#include "mail/em-config.h"
#include "e-util/e-account.h"
#include "e-util/e-error.h"

#include "exchange-operations.h"
#include "exchange-folder-size-display.h"

enum {
    CONTACTSNAME_COL,
    CONTACTSRURI_COL,
    NUM_COLS
};

gboolean contacts_src_exists = FALSE;
gchar *contacts_old_src_uri = NULL;


GPtrArray *e_exchange_contacts_get_contacts (void);
void e_exchange_contacts_pcontacts_on_change (GtkTreeView *treeview, ESource *source);
GtkWidget *e_exchange_contacts_pcontacts (EPlugin *epl, EConfigHookItemFactoryData *data);
gboolean e_exchange_contacts_check (EPlugin *epl, EConfigHookPageCheckData *data);
void e_exchange_contacts_commit (EPlugin *epl, EConfigTarget *target);

/* FIXME: Reconsider the prototype of this function */
GPtrArray *
e_exchange_contacts_get_contacts (void) 
{
    ExchangeAccount *account;
    GPtrArray *folder_array;
    GPtrArray *contacts_list;
    EFolder *folder;

    int i, prefix_len;
    gchar *uri_prefix, *ruri;

    account = exchange_operations_get_exchange_account ();

    uri_prefix = g_strconcat ("exchange://", account->account_filename, "/", NULL);
    prefix_len = strlen (uri_prefix);

    contacts_list = g_ptr_array_new ();
    exchange_account_rescan_tree (account);
    folder_array = exchange_account_get_folders (account);

    for (i=0; i<folder_array->len; ++i) {
        gchar *type, *tmp;
        folder = g_ptr_array_index (folder_array, i);
        type = (gchar*) e_folder_get_type_string (folder);
        if (!strcmp (type, "contacts")) {
            tmp = (gchar*) e_folder_get_physical_uri (folder);
            if (g_str_has_prefix (tmp, uri_prefix)) {
                ruri = g_strdup (tmp+prefix_len); /* ATTN: Should not free this explicitly */
                g_ptr_array_add (contacts_list, (gpointer)ruri);
            }
        }
    }

    g_free (uri_prefix);
    return contacts_list;
}

void
e_exchange_contacts_pcontacts_on_change (GtkTreeView *treeview, ESource *source)
{
    GtkTreeSelection *selection;
    GtkTreeModel     *model;
    GtkTreeIter       iter;
    ExchangeAccount *account;
    gchar *es_ruri;
    
    account = exchange_operations_get_exchange_account ();

    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    gtk_tree_selection_get_selected(selection, &model, &iter);
    gchar *ruri;
    
    gtk_tree_model_get (model, &iter, CONTACTSRURI_COL, &ruri, -1);
    es_ruri = g_strconcat (account->account_filename, "/", ruri, NULL);
    e_source_set_relative_uri (source, es_ruri);

    g_free (ruri);
    g_free (es_ruri);
} 

GtkWidget *
e_exchange_contacts_pcontacts (EPlugin *epl, EConfigHookItemFactoryData *data)
{
    static GtkWidget *lbl_pcontacts, *scrw_pcontacts, *tv_pcontacts, *vb_pcontacts, *lbl_size, *lbl_size_val, *hbx_size;
    GtkTreeStore *ts_pcontacts;
    GtkCellRenderer *cr_contacts;
    GtkTreeViewColumn *tvc_contacts;
    GtkListStore *model;
    GPtrArray *conlist;
    gchar *ruri, *account_name, *uri_text;
    ExchangeAccount *account;

    int i;
    char *folder_size, *abook_name;
    const char *rel_uri;
    const char *uid;

    EABConfigTargetSource *t = (EABConfigTargetSource *) data->target;
    ESource *source = t->source;

    if (data->old) {
        gtk_widget_destroy (vb_pcontacts);
    }

        uri_text = e_source_get_uri (source);
    if (uri_text && strncmp (uri_text, "exchange", 8)) {
        g_free (uri_text);      
        return NULL;
    }

    g_free (uri_text);

    rel_uri = e_source_peek_relative_uri (source);
    uid = e_source_peek_uid (source);
    if (rel_uri && uid && (strcmp (rel_uri, uid))) {
        contacts_src_exists = TRUE;
        g_free (contacts_old_src_uri);
        contacts_old_src_uri = g_strdup (rel_uri);
    }
    else {
        contacts_src_exists = FALSE;
        e_source_set_relative_uri (source, ""); /* FIXME: Nasty hack */
    }

    account = exchange_operations_get_exchange_account ();
    account_name = account->account_name;
    hbx_size = NULL;
    if (contacts_src_exists) {
        abook_name = e_source_peek_name (source);
        model = exchange_account_folder_size_get_model (account);
        if (model)
            folder_size = g_strdup_printf ("%s KB", exchange_folder_size_get_val (model, abook_name));
        else
            folder_size = g_strdup_printf ("0 KB");

        /* FIXME: Take care of i18n */
        lbl_size = gtk_label_new_with_mnemonic (_("Size:"));
        lbl_size_val = gtk_label_new_with_mnemonic (_(folder_size));
        hbx_size = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start (hbx_size, lbl_size, FALSE, TRUE, 0);
        gtk_box_pack_start (hbx_size, lbl_size_val, FALSE, TRUE, 10);
        gtk_widget_show (lbl_size);
        gtk_widget_show (lbl_size_val);
        gtk_misc_set_alignment (GTK_MISC (lbl_size), 0.0, 0.5);
        gtk_misc_set_alignment (GTK_MISC (lbl_size_val), 0.0, 0.5);
        g_free (folder_size);
    }
    vb_pcontacts = gtk_vbox_new (FALSE, 6);
    gtk_container_add (GTK_CONTAINER (data->parent), vb_pcontacts);

    if (hbx_size)
        gtk_box_pack_start (GTK_BOX (vb_pcontacts), hbx_size, FALSE, FALSE, 0);

    lbl_pcontacts = gtk_label_new_with_mnemonic (_("_Location:"));
    gtk_widget_show (lbl_pcontacts);
    gtk_misc_set_alignment (GTK_MISC (lbl_pcontacts), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (vb_pcontacts), lbl_pcontacts, FALSE, FALSE, 0);
  
    ts_pcontacts = gtk_tree_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING);

    conlist = e_exchange_contacts_get_contacts ();

    for (i=0; i<conlist->len; ++i) {
        ruri = g_ptr_array_index (conlist, i);
        exchange_operations_cta_add_node_to_tree (ts_pcontacts, NULL, ruri);        
    }

    cr_contacts = gtk_cell_renderer_text_new ();
    tvc_contacts = gtk_tree_view_column_new_with_attributes (account_name, cr_contacts, "text", CONTACTSNAME_COL, NULL);
    tv_pcontacts = gtk_tree_view_new_with_model (GTK_TREE_MODEL (ts_pcontacts));
    gtk_tree_view_append_column (GTK_TREE_VIEW (tv_pcontacts), tvc_contacts);
    g_object_set (tv_pcontacts,"expander-column", tvc_contacts, "headers-visible", TRUE, NULL);
    gtk_tree_view_expand_all (GTK_TREE_VIEW (tv_pcontacts));
    
    scrw_pcontacts = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrw_pcontacts), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrw_pcontacts), GTK_SHADOW_IN);
    g_object_set (scrw_pcontacts, "height-request", 150, NULL);
    gtk_container_add (GTK_CONTAINER (scrw_pcontacts), tv_pcontacts);
    gtk_label_set_mnemonic_widget (GTK_LABEL (lbl_pcontacts), tv_pcontacts);
    g_signal_connect (G_OBJECT (tv_pcontacts), "cursor-changed", G_CALLBACK (e_exchange_contacts_pcontacts_on_change), t->source);
    gtk_widget_show_all (scrw_pcontacts);

    gtk_box_pack_start (GTK_BOX (vb_pcontacts), scrw_pcontacts, FALSE, FALSE, 0);
    gtk_widget_show_all (vb_pcontacts);
    
    if (contacts_src_exists) {
        gchar *uri_prefix, *sruri, *tmpruri;
        int prefix_len;
        GtkTreeSelection *selection;

        tmpruri = rel_uri;
        uri_prefix = g_strconcat (account->account_filename, "/", NULL);
        prefix_len = strlen (uri_prefix);
        
        if (g_str_has_prefix (tmpruri, uri_prefix)) {
            sruri = g_strdup (tmpruri+prefix_len);
        }
        else {
            sruri = NULL;
        }
        
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv_pcontacts));
        exchange_operations_cta_select_node_from_tree (ts_pcontacts, 
                                   NULL, 
                                   sruri, 
                                   sruri, 
                                   selection);
        gtk_widget_set_sensitive (tv_pcontacts, FALSE);     
        
        g_free (uri_prefix);
        g_free (sruri);
    }
    
    g_ptr_array_free (conlist, TRUE);  
    return vb_pcontacts;
}

gboolean 
e_exchange_contacts_check (EPlugin *epl, EConfigHookPageCheckData *data) 
{
    /* FIXME - check pageid */
    EABConfigTargetSource *t = (EABConfigTargetSource *) data->target;
    ESourceGroup *group;
    const char *base_uri;
    const char *rel_uri;

    rel_uri = e_source_peek_relative_uri (t->source);
    group = e_source_peek_group (t->source);
    base_uri = e_source_group_peek_base_uri (group);
    if (base_uri && !strncmp (base_uri, "exchange", 8)) {
        if (rel_uri && !strlen (rel_uri)) {
            return FALSE;
        }
    }

    return TRUE;
}

void 
e_exchange_contacts_commit (EPlugin *epl, EConfigTarget *target)
{
    EABConfigTargetSource *t = (EABConfigTargetSource *) target;
    ESource *source = t->source;
    gchar *uri_text, *gname, *gruri, *ruri, *path, *path_prefix, *oldpath=NULL;
    int prefix_len;
    ExchangeAccount *account;
    ExchangeAccountFolderResult result;
        
    uri_text = e_source_get_uri (source);
    if (uri_text && strncmp (uri_text, "exchange", 8)) {
        g_free (uri_text);
        return ;
    }   
    g_free (uri_text);

    account = exchange_operations_get_exchange_account ();
    path_prefix = g_strconcat (account->account_filename, "/", NULL);
    prefix_len = strlen (path_prefix);
    g_free (path_prefix);

    gname = (gchar*) e_source_peek_name (source);
    gruri = (gchar*) e_source_peek_relative_uri (source);
    if (contacts_src_exists) {
        gchar *tmpruri, *tmpdelimit;
        tmpruri = g_strdup (gruri);
        tmpdelimit = g_strrstr (tmpruri, "/");
        tmpdelimit[0] = '\0';
        ruri = g_strconcat (tmpruri, "/", gname, NULL);
        g_free (tmpruri);
    }
    else {
        ruri = g_strconcat (gruri, "/", gname, NULL);
    }
    e_source_set_relative_uri (source, ruri);

    path = g_strdup_printf ("/%s", ruri+prefix_len);
    
    if (!contacts_src_exists) {
        /* Create the new folder */
        result = exchange_account_create_folder (account, path, "contacts");
    }
    else if (strcmp (gruri, contacts_old_src_uri)) {
        /* Rename the folder */
        oldpath = g_strdup_printf ("/%s", contacts_old_src_uri+prefix_len);
        result = exchange_account_xfer_folder (account, oldpath, path, TRUE);
        exchange_operations_update_child_esources (source, 
                               contacts_old_src_uri, 
                               ruri);
    }
    else {
        /* Nothing happened specific to exchange; just return */
        return;
    }

    switch (result) {
    case EXCHANGE_ACCOUNT_FOLDER_ALREADY_EXISTS:
        e_error_run (NULL, ERROR_DOMAIN ":folder-exists-error", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST:
        e_error_run (NULL, ERROR_DOMAIN ":folder-doesnt-exist-error", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_UNKNOWN_TYPE:
        e_error_run (NULL, ERROR_DOMAIN ":folder-unknown-type", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_PERMISSION_DENIED:
        e_error_run (NULL, ERROR_DOMAIN ":folder-perm-error", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_OFFLINE:
        e_error_run (NULL, ERROR_DOMAIN ":folder-offline-error", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_UNSUPPORTED_OPERATION:
        e_error_run (NULL, ERROR_DOMAIN ":folder-unsupported-error", NULL);
        break;
    case EXCHANGE_ACCOUNT_FOLDER_GENERIC_ERROR:     
        e_error_run (NULL, ERROR_DOMAIN ":folder-generic-error", NULL);
        break;
    }
    g_free (ruri);
    g_free (path);
    g_free (oldpath);
    g_free (contacts_old_src_uri);
    contacts_old_src_uri = NULL;
}