actions.c 40.4 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
4
 *  Author: Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
Emmanuel Milou's avatar
Emmanuel Milou committed
5
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
6
7
 *  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
8
 *  the Free Software Foundation; either version 3 of the License, or
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
9
 *  (at your option) any later version.
Emmanuel Milou's avatar
Emmanuel Milou committed
10
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
11
12
13
14
 *  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.
Emmanuel Milou's avatar
Emmanuel Milou committed
15
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
16
17
18
 *  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.
19
20
21
22
23
24
25
26
27
28
29
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
30
 */
31

32
#include <gtk/gtk.h>
33
34
35
36
/* Backward compatibility for gtk < 2.22.0 */
#if GTK_CHECK_VERSION(2,22,0)
#include <gdk/gdkkeysyms-compat.h>
#else
37
#include <gdk/gdkkeysyms.h>
38
#endif
39
40
#include <glib/gprintf.h>
#include <stdlib.h>
41
#include <string.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
42
43
#include <sys/types.h>
#include <unistd.h>
44
#include <assert.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
45

46
#include <arpa/nameser.h>
47
#include <netinet/in.h>
48
49
#include <resolv.h>

50
#include <linux/if.h>
51
52
53
54
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

55
56
57
58
59
60
61
62
63
#include "actions.h"
#include "dbus/dbus.h"
#include "contacts/searchbar.h"
#include "contacts/addrbookfactory.h"
#include "icons/icon_factory.h"
#include "imwindow.h"
#include "statusicon.h"
#include "widget/imwidget.h"

64

65
static GHashTable * ip2ip_profile;
66

67
static gchar ** sflphone_order_history_hash_table(GHashTable *result)
68
69
{
    GHashTableIter iter;
70
71
    gint size = 0;
    gchar **ordered_list = NULL;
72

73
    assert(result);
74
    while (g_hash_table_size (result)) {
75
        gpointer key, key_to_min, value;
76
77
78
79

        // find lowest timestamp in map
        g_hash_table_iter_init (&iter, result);

80
        gint min_timestamp = G_MAXINT;
81
        while (g_hash_table_iter_next (&iter, &key, &value))  {
82
83
            gint timestamp = atoi ( (gchar*) key);
            if (timestamp < min_timestamp) {
84
85
86
87
88
                min_timestamp = timestamp;
                key_to_min = key;
            }
        }

Tristan Matthews's avatar
Tristan Matthews committed
89
90
91
92
93
94
95
96
97
        if (g_hash_table_lookup_extended(result, key_to_min, &key, &value)) {
            GSList *llist = (GSList *)value;
            while (llist) {
                ordered_list = (void *) g_realloc(ordered_list, (size + 1) * sizeof (void *));
                *(ordered_list + size) = g_strdup((gchar *)llist->data);
                size++;
                llist = g_slist_next(llist);
            }
            g_hash_table_remove(result, key_to_min);
98
99
100
        }
    }

101
102
    ordered_list = (void *) g_realloc(ordered_list, (size + 1) * sizeof(void *));
    *(ordered_list + size) = NULL;
103

104
    return ordered_list;
105
106
}

107
void
108
sflphone_notify_voice_mail (const gchar* accountID , guint count)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
109
{
110
    // We want to notify only the current account; ie the first in the list
111
112
    gchar *id = g_strdup (accountID);
    const gchar * const current_id = account_list_get_current_id ();
113

Julien Bonjean's avatar
Julien Bonjean committed
114
    DEBUG ("sflphone_notify_voice_mail begin");
115

116
    if (g_ascii_strcasecmp (id, current_id) != 0 || account_list_get_size() == 0)
117
118
        return;

119
120
    // Set the number of voice messages for the current account
    current_account_set_message_number (count);
121
    account_t *current = account_list_get_current ();
122

123
124
    // Update the voicemail tool button
    update_voicemail_status ();
Emmanuel Milou's avatar
Emmanuel Milou committed
125

126
127
    if (current)
        notify_voice_mails (count, current);
Emmanuel Milou's avatar
Emmanuel Milou committed
128

Julien Bonjean's avatar
Julien Bonjean committed
129
    DEBUG ("sflphone_notify_voice_mail end");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
130
131
}

132
133
134
135
136
137
138
/*
 * Place a call with the current account.
 * If there is no default account selected, place a call with the first
 * registered account of the account list
 * Else, check if it an IP call. if not, popup an error message
 */

139
140
static gboolean _is_direct_call (callable_obj_t * c)
{
141
    if (g_strcasecmp (c->_accountID, "empty") == 0) {
142
143
144
        if (!g_str_has_prefix (c->_peer_number, "sip:")) {
            gchar * new_number = g_strconcat ("sip:", c->_peer_number, NULL);
            g_free (c->_peer_number);
145
146
            c->_peer_number = new_number;
        }
147

148
        return TRUE;
Alexandre Savard's avatar
Alexandre Savard committed
149
150
    }

151
152
    return g_str_has_prefix (c->_peer_number, "sip:") ||
           g_str_has_prefix (c->_peer_number, "sips:");
153
154
155
}


156
void
157
status_bar_display_account ()
158
{
159
    gchar* msg;
160

161
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
162

163
164
    account_t *acc = account_list_get_current ();
    status_tray_icon_online (acc != NULL);
165
166
167
168
169
170
171
172

    if (acc) {
        msg = g_markup_printf_escaped ("%s %s (%s)" ,
                                       _ ("Using account"),
                                       (gchar*) g_hash_table_lookup (acc->properties , ACCOUNT_ALIAS),
                                       (gchar*) g_hash_table_lookup (acc->properties , ACCOUNT_TYPE));
    } else {
        msg = g_markup_printf_escaped (_ ("No registered accounts"));
173
    }
174

Julien Bonjean's avatar
Julien Bonjean committed
175
    statusbar_push_message (msg, NULL,  __MSG_ACCOUNT_DEFAULT);
176
    g_free (msg);
177
178
}

Emmanuel Milou's avatar
Emmanuel Milou committed
179

180
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
181
182
sflphone_quit ()
{
183
    if (calllist_get_size(current_calls) == 0 || main_window_ask_quit()) {
184
        // Save the history
185
186
        sflphone_save_history ();

187
        dbus_unregister (getpid());
Emmanuel Milou's avatar
Emmanuel Milou committed
188
        dbus_clean ();
Vivien Didelot's avatar
Vivien Didelot committed
189
190
191
        calllist_clean (current_calls);
        calllist_clean (contacts);
        calllist_clean (history);
Emmanuel Milou's avatar
Emmanuel Milou committed
192
193
        gtk_main_quit ();
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
194
195
}

196
197
void
sflphone_hold (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
198
{
199
    c->_state = CALL_STATE_HOLD;
200
    calltree_update_call (current_calls, c, NULL);
201
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
202
203
}

204
205
void
sflphone_ringing (callable_obj_t * c)
206
{
207
    c->_state = CALL_STATE_RINGING;
208
    calltree_update_call (current_calls, c, NULL);
209
    update_actions();
210
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
211

212
213
void
sflphone_hung_up (callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
214
{
Julien Bonjean's avatar
Julien Bonjean committed
215
    DEBUG ("SFLphone: Hung up");
216

217
    calllist_remove_call (current_calls, c->_callID);
218
    calltree_remove_call (current_calls, c, NULL);
219
    c->_state = CALL_STATE_DIALING;
220
    call_remove_all_errors (c);
221
    update_actions();
222

223
224
225
226
227
    if(c->_confID) {
	g_free(c->_confID);
	c->_confID = NULL;
    }

228
229
230
231
232
    // test wether the widget contain text, if not remove it
    if ( (im_window_get_nb_tabs() > 1) && c->_im_widget && ! (IM_WIDGET (c->_im_widget)->containText))
        im_window_remove_tab (c->_im_widget);
    else
        im_widget_update_state (IM_WIDGET (c->_im_widget), FALSE);
233

234
#if GTK_CHECK_VERSION(2,10,0)
235
    status_tray_icon_blink (FALSE);
236
#endif
237

238
    statusbar_update_clock("");
239
240
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
241
/** Internal to actions: Fill account list */
242
243
void sflphone_fill_account_list (void)
{
244
245
246
247

    gchar** array;
    gchar** accountID;
    unsigned int i;
248
    int count;
249

250
    DEBUG ("SFLphone: Fill account list");
251

252
    count = current_account_get_message_number ();
253

Emmanuel Milou's avatar
Emmanuel Milou committed
254
    account_list_clear ();
255

256
257
258
    array = (gchar **) dbus_account_list();

    if (array) {
Julien Bonjean's avatar
Julien Bonjean committed
259

260
261
262
        for (accountID = array; *accountID; accountID++) {
            account_t * a = g_new0 (account_t,1);
            a->accountID = g_strdup (*accountID);
263
            a->credential_information = NULL;
264
            account_list_add (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
265
        }
266

Emmanuel Milou's avatar
Emmanuel Milou committed
267
        g_strfreev (array);
268
    }
269

270
    for (i = 0; i < account_list_get_size(); i++) {
Emmanuel Milou's avatar
Emmanuel Milou committed
271
        account_t  * a = account_list_get_nth (i);
272
273
        if(a == NULL) {
            ERROR("SFLphone: Error: Could not find account %d in list", i);
274
            break;
275
        }
276

277
278
279
280
281
        GHashTable * details = (GHashTable *) dbus_get_account_details (a->accountID);
        if (details == NULL) {
            ERROR("SFLphone: Error: Could not fetch detais for account %s", a->accountID);
	    break;
        }
282

283
        a->properties = details;
284
285

        /* Fill the actual array of credentials */
286
        dbus_get_credentials (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
287

288
289
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

290
        if (g_strcmp0 (status, "REGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
291
            a->state = ACCOUNT_STATE_REGISTERED;
292
        } else if (g_strcmp0 (status, "UNREGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
293
            a->state = ACCOUNT_STATE_UNREGISTERED;
294
        } else if (g_strcmp0 (status, "TRYING") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
295
            a->state = ACCOUNT_STATE_TRYING;
296
        } else if (g_strcmp0 (status, "ERROR") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
297
            a->state = ACCOUNT_STATE_ERROR;
298
        } else if (g_strcmp0 (status , "ERROR_AUTH") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
299
            a->state = ACCOUNT_STATE_ERROR_AUTH;
300
        } else if (g_strcmp0 (status , "ERROR_NETWORK") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
301
            a->state = ACCOUNT_STATE_ERROR_NETWORK;
302
        } else if (g_strcmp0 (status , "ERROR_HOST") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
303
            a->state = ACCOUNT_STATE_ERROR_HOST;
304
        } else if (g_strcmp0 (status , "ERROR_CONF_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
305
            a->state = ACCOUNT_STATE_ERROR_CONF_STUN;
306
        } else if (g_strcmp0 (status , "ERROR_EXIST_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
307
            a->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
308
        } else if (g_strcmp0 (status, "READY") == 0) {
309
310
            a->state = IP2IP_PROFILE_STATUS;
        } else {
Emmanuel Milou's avatar
Emmanuel Milou committed
311
312
313
            a->state = ACCOUNT_STATE_INVALID;
        }

314
        gchar * code = NULL;
315
316
        code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);

317
        if (code != NULL) {
318
            a->protocol_state_code = atoi (code);
319
        }
320
321
322

        g_free (a->protocol_state_description);
        a->protocol_state_description = g_hash_table_lookup (details, REGISTRATION_STATE_DESCRIPTION);
Emmanuel Milou's avatar
Emmanuel Milou committed
323
324
    }

325
326
    // Set the current account message number
    current_account_set_message_number (count);
327

328
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
329
330
}

331
gboolean sflphone_init (GError **error)
332
{
333
    if (!dbus_connect (error) || !dbus_register (getpid (), "Gtk+ Client", error))
334
        return FALSE;
335

Rafaël Carré's avatar
Rafaël Carré committed
336
    abook_init();
337

338
339
    // Init icons factory
    init_icon_factory ();
340

341
342
343
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
344

345
346
    account_list_init ();
    codec_capabilities_load ();
347
348
    conferencelist_init (current_calls);
    conferencelist_init (history);
349

350
351
    // Fetch the configured accounts
    sflphone_fill_account_list ();
352

353
354
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
355

356
357
    // Fetch the conference list
    // sflphone_fill_conference_list();
358

359
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
360
361
}

362
void sflphone_fill_ip2ip_profile (void)
363
364
365
366
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

367
GHashTable *sflphone_get_ip2ip_properties(void)
368
{
369
    return ip2ip_profile;
370
371
}

372
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
373
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
374
{
375
376
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
377

Julien Bonjean's avatar
Julien Bonjean committed
378
    DEBUG ("SFLphone: Hang up");
379

380
381
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
382
383
384
385
386
            case CALL_STATE_DIALING:
                dbus_hang_up (selectedCall);
                break;
            case CALL_STATE_RINGING:
                dbus_hang_up (selectedCall);
387
                call_remove_all_errors (selectedCall);
388
389
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
390
391
392
393
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
394
            case CALL_STATE_RECORD:
Emmanuel Milou's avatar
Emmanuel Milou committed
395
                dbus_hang_up (selectedCall);
396
                call_remove_all_errors (selectedCall);
397
                selectedCall->_state = CALL_STATE_DIALING;
398
                time (&selectedCall->_time_stop);
399
400
401
402
403

                //if ( (im_window_get_nb_tabs() > 1) && selectedCall->_im_widget &&
                //        ! (IM_WIDGET (selectedCall->_im_widget)->containText))
                //    im_window_remove_tab (selectedCall->_im_widget);
                //else
404
                im_widget_update_state (IM_WIDGET (selectedCall->_im_widget), FALSE);
405

Emmanuel Milou's avatar
Emmanuel Milou committed
406
407
408
                break;
            case CALL_STATE_FAILURE:
                dbus_hang_up (selectedCall);
409
                call_remove_all_errors (selectedCall);
410
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
411
412
413
                break;
            case CALL_STATE_INCOMING:
                dbus_refuse (selectedCall);
414
                call_remove_all_errors (selectedCall);
415
                selectedCall->_state = CALL_STATE_DIALING;
416
                DEBUG ("from sflphone_hang_up : ");
Emmanuel Milou's avatar
Emmanuel Milou committed
417
                break;
418
            case CALL_STATE_TRANSFER:
Emmanuel Milou's avatar
Emmanuel Milou committed
419
                dbus_hang_up (selectedCall);
420
                call_remove_all_errors (selectedCall);
421
                time (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
422
423
                break;
            default:
424
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
425
426
                break;
        }
427
    } else if (selectedConf) {
428
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
429
        dbus_hang_up_conference (selectedConf);
430
431
    }

432
    calltree_update_call (history, selectedCall, NULL);
433

434
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
435
436
}

437
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
438
439
sflphone_pick_up()
{
440
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
441

442
443
    DEBUG("SFLphone: Pick up");

444
445
446
447
448
449
450
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
451

452
453
454
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (im_window_is_visible())
                im_widget_display ( (IMWidget **) (&selectedCall->_im_widget), NULL, selectedCall->_callID, NULL);
455

456
457
458
459
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
460

461
462
463
464
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (selectedCall->_im_widget && im_window_is_visible()) {
                im_widget_display ( (IMWidget **) (&selectedCall->_im_widget), NULL, selectedCall->_callID, NULL);
            }
465

466
467
            dbus_accept (selectedCall);
            break;
468
469
        case CALL_STATE_TRANSFER:
            dbus_transfer (selectedCall);
470
471
472
473
474
475
476
477
478
479
480
481
482
            time (&selectedCall->_time_stop);
            calltree_remove_call(current_calls, selectedCall, NULL);
            calllist_remove_call(current_calls, selectedCall->_callID);
            break;
        case CALL_STATE_CURRENT:
        case CALL_STATE_HOLD:
        case CALL_STATE_RECORD:
        case CALL_STATE_RINGING:
            sflphone_new_call();
            break;
        default:
            WARN ("Should not happen in sflphone_pick_up()!");
            break;
483
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
484
485
}

486
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
487
488
sflphone_on_hold ()
{
489
490
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
491

492
493
494
495
    DEBUG ("sflphone_on_hold");

    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
496
497
498
499
500
501
502
            case CALL_STATE_CURRENT:
                dbus_hold (selectedCall);
                break;
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
503
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
504
505
                break;
        }
506
507
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
508
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
509
510
}

511
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
512
513
sflphone_off_hold ()
{
514
515
516
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
517

518
519
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
520
521
522
523
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
524
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
525
526
                break;
        }
527
528
    } else if (selectedConf) {

Julien Bonjean's avatar
Julien Bonjean committed
529

530
        dbus_unhold_conference (selectedConf);
531
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
532
533
534
}


535
536
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
537
{
538
    c->_state = CALL_STATE_FAILURE;
539
    calltree_update_call (current_calls, c, NULL);
540
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
541
542
}

543
544
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
545
{
546
    c->_state = CALL_STATE_BUSY;
547
    calltree_update_call (current_calls, c, NULL);
548
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
549
550
}

551
552
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
553
{
554

555
    if (c->_state != CALL_STATE_HOLD)
556
        time (&c->_time_start);
557

558
    c->_state = CALL_STATE_CURRENT;
559
    calltree_update_call (current_calls, c, NULL);
560
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
561
562
}

563
564
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
565
{
566
    if (c->_state != CALL_STATE_HOLD)
567
        time (&c->_time_start);
568

569
    c->_state = CALL_STATE_RECORD;
570
    calltree_update_call (current_calls, c, NULL);
571
    update_actions();
alexandresavard's avatar
alexandresavard committed
572
573
}

574
void
575
sflphone_set_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
576
{
577
578
579
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
580
        c->_state = CALL_STATE_TRANSFER;
Tristan Matthews's avatar
Tristan Matthews committed
581
        g_free(c->_trsft_to);
582
583
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
584
    }
585

586
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
587
588
}

589
void
590
sflphone_unset_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
591
{
592
593
594
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
595
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
596
        g_free(c->_trsft_to);
597
598
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
599
    }
600

601
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
602
}
Emmanuel Milou's avatar
Emmanuel Milou committed
603

604
605
void
sflphone_display_transfer_status (const gchar* message)
606
{
Julien Bonjean's avatar
Julien Bonjean committed
607
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
608
609
}

610
void
611
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
612
{
613
    c->_history_state = MISSED;
614
    calllist_add_call (current_calls, c);
615
    calltree_add_call (current_calls, c, NULL);
616

617
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
618
    calltree_display (current_calls);
619
620
621

    // Change the status bar if we are dealing with a direct SIP call
    if (_is_direct_call (c)) {
622
        gchar *msg = g_markup_printf_escaped (_ ("Direct SIP call"));
623
        statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
Julien Bonjean's avatar
Julien Bonjean committed
624
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
625
626
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
627
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
628

629
630
631
632
633
634
635
636
637
638
639
/* Truncates last char from dynamically allocated string */
static void truncate_last_char(gchar **str)
{
    if (strlen(*str) > 0) {
        gchar *tmp = *str;
        tmp = g_strndup(*str, strlen(*str) - 1);
        g_free(*str);
        *str = tmp;
    }
}

640
void
641
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
642
{
Emmanuel Milou's avatar
Emmanuel Milou committed
643
    // We stop the tone
644
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFER)
645
        dbus_start_tone (FALSE, 0);
646

647
    switch (keyval) {
648
649
        case GDK_Return:
        case GDK_KP_Enter:
650
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
651
            break;
652
        case GDK_Escape:
Rafaël Carré's avatar
Rafaël Carré committed
653
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
654
            break;
655
        case GDK_BackSpace:
656
            if (c->_state == CALL_STATE_TRANSFER) {
657
                truncate_last_char(&c->_trsft_to);
658
                calltree_update_call (current_calls, c, NULL);
659
            } else {
660
661
662
663
664
                truncate_last_char(&c->_peer_number);
                calltree_update_call (current_calls, c, NULL);
                /* If number is now empty, hang up immediately */
                if (strlen(c->_peer_number) == 0)
                    dbus_hang_up(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
665
            }
666
667

            break;
668
669
670
671
672
        case GDK_Tab:
        case GDK_Alt_L:
        case GDK_Control_L:
        case GDK_Super_L:
        case GDK_Caps_Lock:
Emmanuel Milou's avatar
Emmanuel Milou committed
673
674
            break;
        default:
675

676
            if (keyval < 127 /* ascii */ ||
677
               (keyval >= GDK_Mode_switch && keyval <= GDK_KP_9) /* num keypad */) {
Emmanuel Milou's avatar
Emmanuel Milou committed
678

679
                if (c->_state == CALL_STATE_TRANSFER) {
Tristan Matthews's avatar
Tristan Matthews committed
680
681
682
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
683
684
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
685
686
687
                    gchar *new_peer_number = g_strconcat (c->_peer_number, key, NULL);
                    g_free (c->_peer_number);
                    c->_peer_number = new_peer_number;
Emmanuel Milou's avatar
Emmanuel Milou committed
688
                }
689
690

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
691
            }
692

Emmanuel Milou's avatar
Emmanuel Milou committed
693
694
            break;
    }
695
696
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
697

698
callable_obj_t *
areversat's avatar
areversat committed
699
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
700
{
Tristan Matthews's avatar
Tristan Matthews committed
701
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
702

703
704
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
705

Emmanuel Milou's avatar
Emmanuel Milou committed
706
    // Play a tone when creating a new call
707
708
    if (calllist_get_size (current_calls) == 0)
        dbus_start_tone (TRUE , (current_account_has_new_message ()  > 0) ? TONE_WITH_MESSAGE : TONE_WITHOUT_MESSAGE) ;
Emmanuel Milou's avatar
Emmanuel Milou committed
709

710
    callable_obj_t *c = create_new_call (CALL, CALL_STATE_DIALING, "", "", "", "");
Emmanuel Milou's avatar
Emmanuel Milou committed
711

712
713
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
714
    calllist_add_call (current_calls, c);
715
    calltree_add_call (current_calls, c, NULL);
716
    update_actions();
717

Emmanuel Milou's avatar
Emmanuel Milou committed
718
    return c;
719
720
}

areversat's avatar
areversat committed
721

722
723
void
sflphone_keypad (guint keyval, gchar * key)
areversat's avatar
areversat committed
724
{
725
726
727
728
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if ( (active_calltree != current_calls) || (active_calltree == current_calls && !c)) {
        switch (keyval) {
729
730
731
            case GDK_Return:
            case GDK_KP_Enter:
            case GDK_Escape:
Emmanuel Milou's avatar
Emmanuel Milou committed
732
733
                break;
            default:
Julien Bonjean's avatar
Julien Bonjean committed
734
                calltree_display (current_calls);
735
                process_dialing (sflphone_new_call(), keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
736
737
                break;
        }
738
739
    } else if (c) {
        switch (c->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
740
            case CALL_STATE_DIALING: // Currently dialing => edit number
741
                process_dialing (c, keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
742
743
744
                break;
            case CALL_STATE_RECORD:
            case CALL_STATE_CURRENT:
745
746

                switch (keyval) {
747
                    case GDK_Escape:
748
                        dbus_hang_up (c);
749
                        time (&c->_time_stop);
750
                        calltree_update_call (history, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
751
752
753
                        break;
                    default:
                        // To play the dtmf when calling mail box for instance
754
                        dbus_play_dtmf (key);
Emmanuel Milou's avatar
Emmanuel Milou committed
755
756
                        break;
                }
757

Emmanuel Milou's avatar
Emmanuel Milou committed
758
759
                break;
            case CALL_STATE_INCOMING:
760
761

                switch (keyval) {
762
763
                    case GDK_Return:
                    case GDK_KP_Enter:
764
                        c->_history_state = INCOMING;
765