actions.c 33 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 44
#include <sys/types.h>
#include <unistd.h>

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

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

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

66
static GHashTable * ip2ip_profile;
67

68
void
69
sflphone_notify_voice_mail(const gchar* accountID , guint count)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
70
{
71
    // We want to notify only the current account; ie the first in the list
72 73
    gchar *id = g_strdup(accountID);
    const gchar * const current_id = account_list_get_current_id();
74

75
    DEBUG("sflphone_notify_voice_mail begin");
76

77
    if (g_ascii_strcasecmp(id, current_id) != 0 || account_list_get_size() == 0)
78 79
        return;

80
    // Set the number of voice messages for the current account
81 82
    current_account_set_message_number(count);
    account_t *current = account_list_get_current();
83

84
    // Update the voicemail tool button
85
    update_voicemail_status();
Emmanuel Milou's avatar
Emmanuel Milou committed
86

87
    if (current)
88
        notify_voice_mails(count, current);
Emmanuel Milou's avatar
Emmanuel Milou committed
89

90
    DEBUG("sflphone_notify_voice_mail end");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
91 92
}

93 94 95 96 97 98 99
/*
 * 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
 */

100
static gboolean is_direct_call(callable_obj_t * c)
101
{
102 103 104 105
    if (g_strcasecmp(c->_accountID, "empty") == 0) {
        if (!g_str_has_prefix(c->_peer_number, "sip:")) {
            gchar * new_number = g_strconcat("sip:", c->_peer_number, NULL);
            g_free(c->_peer_number);
106 107
            c->_peer_number = new_number;
        }
108

109
        return TRUE;
Alexandre Savard's avatar
Alexandre Savard committed
110 111
    }

112 113
    return g_str_has_prefix(c->_peer_number, "sip:") ||
           g_str_has_prefix(c->_peer_number, "sips:");
114 115 116
}


117
void
118
status_bar_display_account()
119
{
120
    gchar* msg;
121

122
    statusbar_pop_message(__MSG_ACCOUNT_DEFAULT);
123

124 125
    account_t *acc = account_list_get_current();
    status_tray_icon_online(acc != NULL);
126 127

    if (acc) {
128 129
        msg = g_markup_printf_escaped("%s %s (%s)" ,
                                      _("Using account"),
130 131
                                      (gchar*) g_hash_table_lookup(acc->properties, ACCOUNT_ALIAS),
                                      (gchar*) g_hash_table_lookup(acc->properties, ACCOUNT_TYPE));
132
    } else {
133
        msg = g_markup_printf_escaped(_("No registered accounts"));
134
    }
135

136 137
    statusbar_push_message(msg, NULL,  __MSG_ACCOUNT_DEFAULT);
    g_free(msg);
138 139
}

Emmanuel Milou's avatar
Emmanuel Milou committed
140

141
void
142
sflphone_quit()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
143
{
Tristan Matthews's avatar
Tristan Matthews committed
144
    if (calllist_get_size(current_calls_tab) == 0 || main_window_ask_quit()) {
145 146
        dbus_unregister(getpid());
        dbus_clean();
Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
147
        account_list_free();
Tristan Matthews's avatar
Tristan Matthews committed
148 149 150 151 152 153
        calllist_clean(current_calls_tab);
        calllist_clean(contacts_tab);
        calllist_clean(history_tab);
        gtk_tree_store_clear(history_tab->store);
        gtk_tree_store_clear(current_calls_tab->store);
        gtk_tree_store_clear(contacts_tab->store);
154
        gtk_main_quit();
Emmanuel Milou's avatar
Emmanuel Milou committed
155
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
156 157
}

158
void
159
sflphone_hold(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
160
{
161
    c->_state = CALL_STATE_HOLD;
Tristan Matthews's avatar
Tristan Matthews committed
162
    calltree_update_call(current_calls_tab, c);
163
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
164 165
}

166
void
167
sflphone_ringing(callable_obj_t * c)
168
{
169
    c->_state = CALL_STATE_RINGING;
Tristan Matthews's avatar
Tristan Matthews committed
170
    calltree_update_call(current_calls_tab, c);
171
    update_actions();
172
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
173

174
void
175
sflphone_hung_up(callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
176
{
177
    DEBUG("SFLphone: Hung up");
178

Tristan Matthews's avatar
Tristan Matthews committed
179 180
    calllist_remove_call(current_calls_tab, c->_callID);
    calltree_remove_call(current_calls_tab, c);
181
    c->_state = CALL_STATE_DIALING;
182
    call_remove_all_errors(c);
183
    update_actions();
184

185 186 187
    if (c->_confID) {
        g_free(c->_confID);
        c->_confID = NULL;
188 189
    }

190
    // test whether the widget contains text, if not remove it
191 192
    if ((im_window_get_nb_tabs() > 1) && c->_im_widget && !(IM_WIDGET(c->_im_widget)->containText))
        im_window_remove_tab(c->_im_widget);
193
    else
194
        im_widget_update_state(IM_WIDGET(c->_im_widget), FALSE);
195

196
    status_tray_icon_blink(FALSE);
197

198
    statusbar_update_clock("");
199 200
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
201
/** Internal to actions: Fill account list */
202
void sflphone_fill_account_list(void)
203
{
204
    int count = current_account_get_message_number();
205

Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
206 207
    account_list_free();
    account_list_init();
208

209
    gchar **array = dbus_account_list();
210

211
    if (array) {
212
        for (gchar **accountID = array; accountID && *accountID; accountID++) {
213
            account_t * a = g_new0(account_t, 1);
214
            a->accountID = g_strdup(*accountID);
215
            a->credential_information = NULL;
216
            account_list_add(a);
Emmanuel Milou's avatar
Emmanuel Milou committed
217
        }
218

219
        g_strfreev(array);
220
    }
221

222
    for (unsigned i = 0; i < account_list_get_size(); i++) {
223 224 225
        account_t  * a = account_list_get_nth(i);

        if (a == NULL) {
226
            ERROR("SFLphone: Error: Could not find account %d in list", i);
227
            break;
228
        }
229

230 231
        GHashTable * details = (GHashTable *) dbus_get_account_details(a->accountID);

232 233
        if (details == NULL) {
            ERROR("SFLphone: Error: Could not fetch detais for account %s", a->accountID);
234
            break;
235
        }
236

237
        a->properties = details;
238 239

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

242
        gchar * status = g_hash_table_lookup(details, REGISTRATION_STATUS);
243

244
        if (g_strcmp0(status, "REGISTERED") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
245
            a->state = ACCOUNT_STATE_REGISTERED;
246
        else if (g_strcmp0(status, "UNREGISTERED") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
247
            a->state = ACCOUNT_STATE_UNREGISTERED;
248
        else if (g_strcmp0(status, "TRYING") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
249
            a->state = ACCOUNT_STATE_TRYING;
250
        else if (g_strcmp0(status, "ERROR") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
251
            a->state = ACCOUNT_STATE_ERROR;
252
        else if (g_strcmp0(status , "ERROR_AUTH") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
253
            a->state = ACCOUNT_STATE_ERROR_AUTH;
254
        else if (g_strcmp0(status , "ERROR_NETWORK") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
255
            a->state = ACCOUNT_STATE_ERROR_NETWORK;
256
        else if (g_strcmp0(status , "ERROR_HOST") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
257
            a->state = ACCOUNT_STATE_ERROR_HOST;
258
        else if (g_strcmp0(status , "ERROR_CONF_STUN") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
259
            a->state = ACCOUNT_STATE_ERROR_CONF_STUN;
260
        else if (g_strcmp0(status , "ERROR_EXIST_STUN") == 0)
Emmanuel Milou's avatar
Emmanuel Milou committed
261
            a->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
262
        else if (g_strcmp0(status, "READY") == 0)
263
            a->state = IP2IP_PROFILE_STATUS;
264
        else
Emmanuel Milou's avatar
Emmanuel Milou committed
265 266
            a->state = ACCOUNT_STATE_INVALID;

267 268
        gchar * code = g_hash_table_lookup(details, REGISTRATION_STATE_CODE);

269
        if (code != NULL)
270
            a->protocol_state_code = atoi(code);
271

272 273
        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
274 275
    }

276
    // Set the current account message number
277
    current_account_set_message_number(count);
278

279
    sflphone_fill_codec_list();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
280 281
}

282
gboolean sflphone_init(GError **error)
283
{
284
    if (!dbus_connect(error) || !dbus_register(getpid(), "Gtk+ Client", error))
285
        return FALSE;
286

Rafaël Carré's avatar
Rafaël Carré committed
287
    abook_init();
288

289
    // Init icons factory
290
    init_icon_factory();
291

Tristan Matthews's avatar
Tristan Matthews committed
292 293 294
    current_calls_tab = calltab_init(FALSE, CURRENT_CALLS);
    contacts_tab = calltab_init(TRUE, CONTACTS);
    history_tab = calltab_init(TRUE, HISTORY);
295

296 297
    account_list_init();
    codec_capabilities_load();
Tristan Matthews's avatar
Tristan Matthews committed
298
    conferencelist_init(current_calls_tab);
299

300
    // Fetch the configured accounts
301
    sflphone_fill_account_list();
302

303 304
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
305

306
    // Fetch the conference list
307
    sflphone_fill_conference_list();
308

309
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
310 311
}

312
void sflphone_fill_ip2ip_profile(void)
313 314 315 316
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

317
GHashTable *sflphone_get_ip2ip_properties(void)
318
{
319
    return ip2ip_profile;
320 321
}

322
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
323
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
324
{
Tristan Matthews's avatar
Tristan Matthews committed
325 326
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
327

328
    DEBUG("SFLphone: Hang up");
329

330
    if (selectedConf) {
331 332 333
        im_widget_update_state(IM_WIDGET(selectedConf->_im_widget), FALSE);
        dbus_hang_up_conference(selectedConf);
    } else if (selectedCall) {
334
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
335
            case CALL_STATE_DIALING:
336
                dbus_hang_up(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
337 338
                break;
            case CALL_STATE_RINGING:
339 340
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
341 342
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
343 344 345 346
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
347
            case CALL_STATE_RECORD:
348 349
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
350
                selectedCall->_state = CALL_STATE_DIALING;
351
                time(&selectedCall->_time_stop);
352

353
                im_widget_update_state(IM_WIDGET(selectedCall->_im_widget), FALSE);
354

Emmanuel Milou's avatar
Emmanuel Milou committed
355 356
                break;
            case CALL_STATE_FAILURE:
357 358
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
359
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
360 361
                break;
            case CALL_STATE_INCOMING:
362 363
                dbus_refuse(selectedCall);
                call_remove_all_errors(selectedCall);
364
                selectedCall->_state = CALL_STATE_DIALING;
365
                DEBUG("from sflphone_hang_up : ");
Emmanuel Milou's avatar
Emmanuel Milou committed
366
                break;
367
            case CALL_STATE_TRANSFER:
368 369 370
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
                time(&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
371 372
                break;
            default:
373
                WARN("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
374 375
                break;
        }
376 377
    }

Tristan Matthews's avatar
Tristan Matthews committed
378
    calltree_update_call(history_tab, selectedCall);
379

380
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
381 382
}

383
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
384 385
sflphone_pick_up()
{
Tristan Matthews's avatar
Tristan Matthews committed
386
    callable_obj_t *selectedCall = calltab_get_selected_call(active_calltree_tab);
387

388 389 390 391
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
392

393 394
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
395
            sflphone_place_call(selectedCall);
396

397 398
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (im_window_is_visible())
399 400
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
401

402 403
            break;
        case CALL_STATE_INCOMING:
404
            selectedCall->_history_state = g_strdup(INCOMING_STRING);
Tristan Matthews's avatar
Tristan Matthews committed
405
            calltree_update_call(history_tab, selectedCall);
406

407
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
408 409 410
            if (im_window_is_visible())
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
411

412
            dbus_accept(selectedCall);
413
            break;
414
        case CALL_STATE_TRANSFER:
415 416
            dbus_transfer(selectedCall);
            time(&selectedCall->_time_stop);
Tristan Matthews's avatar
Tristan Matthews committed
417 418
            calltree_remove_call(current_calls_tab, selectedCall);
            calllist_remove_call(current_calls_tab, selectedCall->_callID);
419 420 421 422 423 424 425 426
            break;
        case CALL_STATE_CURRENT:
        case CALL_STATE_HOLD:
        case CALL_STATE_RECORD:
        case CALL_STATE_RINGING:
            sflphone_new_call();
            break;
        default:
427
            WARN("Should not happen in sflphone_pick_up()!");
428
            break;
429
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
430 431
}

432
void
433
sflphone_on_hold()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
434
{
Tristan Matthews's avatar
Tristan Matthews committed
435 436
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
437

438 439
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
440 441
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
442
                dbus_hold(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
443 444
                break;
            default:
445
                WARN("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
446 447
                break;
        }
448
    } else if (selectedConf)
449
        dbus_hold_conference(selectedConf);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
450 451
}

452
void
453
sflphone_off_hold()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
454
{
455
    DEBUG("sflphone_off_hold");
Tristan Matthews's avatar
Tristan Matthews committed
456 457
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
458

459 460
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
461
            case CALL_STATE_HOLD:
462
                dbus_unhold(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
463 464
                break;
            default:
465
                WARN("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
466 467
                break;
        }
468
    } else if (selectedConf)
469
        dbus_unhold_conference(selectedConf);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
470 471 472
}


473
void
474
sflphone_fail(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
475
{
476
    c->_state = CALL_STATE_FAILURE;
Tristan Matthews's avatar
Tristan Matthews committed
477
    calltree_update_call(current_calls_tab, c);
478
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
479 480
}

481
void
482
sflphone_busy(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
483
{
484
    c->_state = CALL_STATE_BUSY;
Tristan Matthews's avatar
Tristan Matthews committed
485
    calltree_update_call(current_calls_tab, c);
486
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
487 488
}

489
void
490
sflphone_current(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
491
{
492
    if (c->_state != CALL_STATE_HOLD)
493
        time(&c->_time_start);
494

495
    c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
496
    calltree_update_call(current_calls_tab, c);
497
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
498 499
}

500
void
501
sflphone_record(callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
502
{
503
    if (c->_state != CALL_STATE_HOLD)
504
        time(&c->_time_start);
505

506
    c->_state = CALL_STATE_RECORD;
Tristan Matthews's avatar
Tristan Matthews committed
507
    calltree_update_call(current_calls_tab, c);
508
    update_actions();
alexandresavard's avatar
alexandresavard committed
509 510
}

511
void
512
sflphone_set_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
513
{
Tristan Matthews's avatar
Tristan Matthews committed
514
    callable_obj_t * c = calltab_get_selected_call(current_calls_tab);
515 516

    if (c) {
517
        c->_state = CALL_STATE_TRANSFER;
Tristan Matthews's avatar
Tristan Matthews committed
518
        g_free(c->_trsft_to);
519
        c->_trsft_to = g_strdup("");
Tristan Matthews's avatar
Tristan Matthews committed
520
        calltree_update_call(current_calls_tab, c);
Emmanuel Milou's avatar
Emmanuel Milou committed
521
    }
522

523
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
524 525
}

526
void
527
sflphone_unset_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
528
{
Tristan Matthews's avatar
Tristan Matthews committed
529
    callable_obj_t * c = calltab_get_selected_call(current_calls_tab);
530 531

    if (c) {
532
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
533
        g_free(c->_trsft_to);
534
        c->_trsft_to = g_strdup("");
Tristan Matthews's avatar
Tristan Matthews committed
535
        calltree_update_call(current_calls_tab, c);
Emmanuel Milou's avatar
Emmanuel Milou committed
536
    }
537

538
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
539
}
Emmanuel Milou's avatar
Emmanuel Milou committed
540

541
void
542
sflphone_display_transfer_status(const gchar* message)
543
{
544
    statusbar_push_message(message , NULL, __MSG_ACCOUNT_DEFAULT);
545 546
}

547
void
548
sflphone_incoming_call(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
549
{
550
    c->_history_state = g_strdup(MISSED_STRING);
Tristan Matthews's avatar
Tristan Matthews committed
551 552
    calllist_add_call(current_calls_tab, c);
    calltree_add_call(current_calls_tab, c, NULL);
553

554
    update_actions();
Tristan Matthews's avatar
Tristan Matthews committed
555
    calltree_display(current_calls_tab);
556 557

    // Change the status bar if we are dealing with a direct SIP call
558
    if (is_direct_call(c)) {
559 560 561 562
        gchar *msg = g_markup_printf_escaped(_("Direct SIP call"));
        statusbar_pop_message(__MSG_ACCOUNT_DEFAULT);
        statusbar_push_message(msg , NULL, __MSG_ACCOUNT_DEFAULT);
        g_free(msg);
563
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
564
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
565

566
static void
567
process_dialing(callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
568
{
Emmanuel Milou's avatar
Emmanuel Milou committed
569
    // We stop the tone
570
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFER)
571
        dbus_start_tone(FALSE, 0);
572

573
    switch (keyval) {
574 575
        case GDK_Return:
        case GDK_KP_Enter:
576
            sflphone_place_call(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
577
            break;
578
        case GDK_Escape:
579
            sflphone_hang_up();
Emmanuel Milou's avatar
Emmanuel Milou committed
580
            break;
581
        case GDK_BackSpace: {
582 583
            gchar *num = (c->_state == CALL_STATE_TRANSFER) ? c->_trsft_to : c->_peer_number;
            size_t len = strlen(num);
584

585 586 587
            if (len) {
                len--; // delete one character
                num[len] = '\0';
Tristan Matthews's avatar
Tristan Matthews committed
588
                calltree_update_call(current_calls_tab, c);
589

590
                /* If number is now empty, hang up immediately */
591
                if (c->_state != CALL_STATE_TRANSFER && len == 0)
592
                    dbus_hang_up(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
593
            }
594

595
            break;
596
        }
597 598 599 600 601
        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
602 603
            break;
        default:
604

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

608
                if (c->_state == CALL_STATE_TRANSFER) {
609 610
                    gchar *new_trsft = g_strconcat(c->_trsft_to, key, NULL);
                    g_free(c->_trsft_to);
Tristan Matthews's avatar
Tristan Matthews committed
611
                    c->_trsft_to = new_trsft;
612
                } else {
613 614 615
                    dbus_play_dtmf(key);
                    gchar *new_peer_number = g_strconcat(c->_peer_number, key, NULL);
                    g_free(c->_peer_number);
Tristan Matthews's avatar
Tristan Matthews committed
616
                    c->_peer_number = new_peer_number;
Emmanuel Milou's avatar
Emmanuel Milou committed
617
                }
618

Tristan Matthews's avatar
Tristan Matthews committed
619
                calltree_update_call(current_calls_tab, c);
Emmanuel Milou's avatar
Emmanuel Milou committed
620
            }
621

Emmanuel Milou's avatar
Emmanuel Milou committed
622 623
            break;
    }
624 625
}

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

627
callable_obj_t *
areversat's avatar
areversat committed
628
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
629
{
Tristan Matthews's avatar
Tristan Matthews committed
630
    callable_obj_t *current_selected_call = calltab_get_selected_call(current_calls_tab);
631

632
    if ((current_selected_call != NULL) && (current_selected_call->_confID == NULL))
633
        sflphone_on_hold();
634

Emmanuel Milou's avatar
Emmanuel Milou committed
635
    // Play a tone when creating a new call
Tristan Matthews's avatar
Tristan Matthews committed
636
    if (calllist_get_size(current_calls_tab) == 0)