actions.c 32.5 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 <glib/gi18n.h>
33
#include <gtk/gtk.h>
34 35 36 37
/* Backward compatibility for gtk < 2.22.0 */
#if GTK_CHECK_VERSION(2,22,0)
#include <gdk/gdkkeysyms-compat.h>
#else
38
#include <gdk/gdkkeysyms.h>
39
#endif
40 41

#include "str_utils.h"
42
#include <glib.h>
43
#include <stdlib.h>
44
#include <string.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
45 46 47
#include <sys/types.h>
#include <unistd.h>

48
#include <arpa/nameser.h>
49
#include <netinet/in.h>
50 51
#include <resolv.h>

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

57 58
#include "actions.h"
#include "dbus/dbus.h"
59
#include "logger.h"
60
#include "contacts/calltab.h"
61 62 63 64 65
#include "contacts/searchbar.h"
#include "contacts/addrbookfactory.h"
#include "icons/icon_factory.h"
#include "imwindow.h"
#include "statusicon.h"
66
#include "unused.h"
67
#include "widget/imwidget.h"
68
#include "sliders.h"
69

70
static GHashTable * ip2ip_profile;
71

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

79
    DEBUG("sflphone_notify_voice_mail begin");
80

81
    if (g_ascii_strcasecmp(id, current_id) != 0 || account_list_get_size() == 0)
82 83
        return;

84
    // Set the number of voice messages for the current account
85 86
    current_account_set_message_number(count);
    account_t *current = account_list_get_current();
87

88
    // Update the voicemail tool button
89
    update_voicemail_status();
Emmanuel Milou's avatar
Emmanuel Milou committed
90

91
    if (current)
92
        notify_voice_mails(count, current);
Emmanuel Milou's avatar
Emmanuel Milou committed
93

94
    DEBUG("sflphone_notify_voice_mail end");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
95 96
}

97 98 99 100 101 102 103
/*
 * 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
 */

104
static gboolean is_direct_call(callable_obj_t * c)
105
{
106
    if (utf8_case_equal(c->_accountID, "empty")) {
107 108 109
        if (!g_str_has_prefix(c->_peer_number, "sip:")) {
            gchar * new_number = g_strconcat("sip:", c->_peer_number, NULL);
            g_free(c->_peer_number);
110 111
            c->_peer_number = new_number;
        }
112

113
        return TRUE;
Alexandre Savard's avatar
Alexandre Savard committed
114 115
    }

116 117
    return g_str_has_prefix(c->_peer_number, "sip:") ||
           g_str_has_prefix(c->_peer_number, "sips:");
118 119 120
}


121
void
122
status_bar_display_account()
123
{
124
    statusbar_pop_message(__MSG_ACCOUNT_DEFAULT);
125

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

Tristan Matthews's avatar
Tristan Matthews committed
129
    gchar* msg;
130
    if (acc) {
131 132
        msg = g_markup_printf_escaped("%s %s (%s)" ,
                                      _("Using account"),
133 134
                                      (gchar*) account_lookup(acc, ACCOUNT_ALIAS),
                                      (gchar*) account_lookup(acc, ACCOUNT_TYPE));
135
    } else {
136
        msg = g_markup_printf_escaped(_("No registered accounts"));
137
    }
138

139 140
    statusbar_push_message(msg, NULL,  __MSG_ACCOUNT_DEFAULT);
    g_free(msg);
141 142
}

Emmanuel Milou's avatar
Emmanuel Milou committed
143

144
void
145
sflphone_quit()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
146
{
Tristan Matthews's avatar
Tristan Matthews committed
147
    if (calllist_get_size(current_calls_tab) == 0 || main_window_ask_quit()) {
148 149
        dbus_unregister(getpid());
        dbus_clean();
Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
150
        account_list_free();
Tristan Matthews's avatar
Tristan Matthews committed
151 152 153 154 155 156
        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);
157
        gtk_main_quit();
Emmanuel Milou's avatar
Emmanuel Milou committed
158
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
159 160
}

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

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

177
void
178
sflphone_hung_up(callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
179
{
180
    DEBUG("SFLphone: Hung up");
181

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

188 189 190
    if (c->_confID) {
        g_free(c->_confID);
        c->_confID = NULL;
191 192
    }

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

199
    status_tray_icon_blink(FALSE);
200

201
    statusbar_update_clock("");
202 203
}

204
void sflphone_fill_account_list(void)
205
{
Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
206
    account_list_init();
207
    gchar **array = dbus_account_list();
208

209 210 211 212 213
    for (gchar **accountID = array; accountID && *accountID; ++accountID) {
        account_t *acc = create_account_with_ID(*accountID);
        if (acc->properties == NULL) {
            ERROR("SFLphone: Error: Could not fetch details for account %s",
                  accountID);
214
            break;
215
        }
216
        account_list_add(acc);
217
        /* Fill the actual array of credentials */
218 219
        dbus_get_credentials(acc);
        gchar * status = account_lookup(acc, REGISTRATION_STATUS);
220

221
        if (g_strcmp0(status, "REGISTERED") == 0)
222
            acc->state = ACCOUNT_STATE_REGISTERED;
223
        else if (g_strcmp0(status, "UNREGISTERED") == 0)
224
            acc->state = ACCOUNT_STATE_UNREGISTERED;
225
        else if (g_strcmp0(status, "TRYING") == 0)
226
            acc->state = ACCOUNT_STATE_TRYING;
227
        else if (g_strcmp0(status, "ERROR") == 0)
228
            acc->state = ACCOUNT_STATE_ERROR;
229
        else if (g_strcmp0(status , "ERROR_AUTH") == 0)
230
            acc->state = ACCOUNT_STATE_ERROR_AUTH;
231
        else if (g_strcmp0(status , "ERROR_NETWORK") == 0)
232
            acc->state = ACCOUNT_STATE_ERROR_NETWORK;
233
        else if (g_strcmp0(status , "ERROR_HOST") == 0)
234
            acc->state = ACCOUNT_STATE_ERROR_HOST;
235
        else if (g_strcmp0(status , "ERROR_CONF_STUN") == 0)
236
            acc->state = ACCOUNT_STATE_ERROR_CONF_STUN;
237
        else if (g_strcmp0(status , "ERROR_EXIST_STUN") == 0)
238
            acc->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
239
        else if (g_strcmp0(status, "READY") == 0)
240
            acc->state = IP2IP_PROFILE_STATUS;
241
        else
242
            acc->state = ACCOUNT_STATE_INVALID;
243

244
        gchar * code = account_lookup(acc, REGISTRATION_STATE_CODE);
245
        if (code != NULL)
246 247
            acc->protocol_state_code = atoi(code);
        acc->protocol_state_description = account_lookup(acc, REGISTRATION_STATE_DESCRIPTION);
Emmanuel Milou's avatar
Emmanuel Milou committed
248 249
    }

250 251
    g_strfreev(array);

252
    // Set the current account message number
253
    current_account_set_message_number(current_account_get_message_number());
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
254 255
}

256
gboolean sflphone_init(GError **error)
257
{
258
    if (!dbus_connect(error) || !dbus_register(getpid(), "Gtk+ Client", error))
259
        return FALSE;
260

Rafaël Carré's avatar
Rafaël Carré committed
261
    abook_init();
262

263
    // Init icons factory
264
    init_icon_factory();
265

Tristan Matthews's avatar
Tristan Matthews committed
266 267 268
    current_calls_tab = calltab_init(FALSE, CURRENT_CALLS);
    contacts_tab = calltab_init(TRUE, CONTACTS);
    history_tab = calltab_init(TRUE, HISTORY);
269

270
    codec_capabilities_load();
Tristan Matthews's avatar
Tristan Matthews committed
271
    conferencelist_init(current_calls_tab);
272

273
    // Fetch the configured accounts
274
    sflphone_fill_account_list();
275

276 277
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
278

279
    // Fetch the conference list
280
    sflphone_fill_conference_list();
281

282
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
283 284
}

285
void sflphone_fill_ip2ip_profile(void)
286 287 288 289
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

290
GHashTable *sflphone_get_ip2ip_properties(void)
291
{
292
    return ip2ip_profile;
293 294
}

295
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
296
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
297
{
Tristan Matthews's avatar
Tristan Matthews committed
298 299
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
300

301
    DEBUG("SFLphone: Hang up");
302

303
    if (selectedConf) {
304 305 306
        im_widget_update_state(IM_WIDGET(selectedConf->_im_widget), FALSE);
        dbus_hang_up_conference(selectedConf);
    } else if (selectedCall) {
307
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
308
            case CALL_STATE_DIALING:
309
                dbus_hang_up(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
310 311
                break;
            case CALL_STATE_RINGING:
312 313
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
314 315
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
316 317 318 319
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
320
            case CALL_STATE_RECORD:
321 322
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
323
                selectedCall->_state = CALL_STATE_DIALING;
324
                time(&selectedCall->_time_stop);
325

326
                im_widget_update_state(IM_WIDGET(selectedCall->_im_widget), FALSE);
327

Emmanuel Milou's avatar
Emmanuel Milou committed
328 329
                break;
            case CALL_STATE_FAILURE:
330 331
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
332
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
333 334
                break;
            case CALL_STATE_INCOMING:
335 336
                dbus_refuse(selectedCall);
                call_remove_all_errors(selectedCall);
337
                selectedCall->_state = CALL_STATE_DIALING;
338
                DEBUG("from sflphone_hang_up : ");
Emmanuel Milou's avatar
Emmanuel Milou committed
339
                break;
340
            case CALL_STATE_TRANSFER:
341 342 343
                dbus_hang_up(selectedCall);
                call_remove_all_errors(selectedCall);
                time(&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
344 345
                break;
            default:
346
                WARN("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
347 348
                break;
        }
349 350
    }

Tristan Matthews's avatar
Tristan Matthews committed
351
    calltree_update_call(history_tab, selectedCall);
352

353
    statusbar_update_clock("");
354 355 356 357 358

    // Allow screen saver to start
    guint nbcall = calllist_get_size(current_calls_tab);
    if(nbcall == 1)
        dbus_screensaver_uninhibit();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
359 360
}

361
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
362 363
sflphone_pick_up()
{
Tristan Matthews's avatar
Tristan Matthews committed
364
    callable_obj_t *selectedCall = calltab_get_selected_call(active_calltree_tab);
365

366 367 368 369 370
    // Disable screensaver if the list is empty call
    guint nbcall = calllist_get_size(current_calls_tab);
    if(nbcall == 0)
        dbus_screensaver_inhibit();

371 372 373 374
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
375

376 377
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
378
            sflphone_place_call(selectedCall);
379

380 381
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (im_window_is_visible())
382 383
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
384

385 386
            break;
        case CALL_STATE_INCOMING:
387
            selectedCall->_history_state = g_strdup(INCOMING_STRING);
Tristan Matthews's avatar
Tristan Matthews committed
388
            calltree_update_call(history_tab, selectedCall);
389

390
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
391 392 393
            if (im_window_is_visible())
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
394

395
            dbus_accept(selectedCall);
396
            break;
397
        case CALL_STATE_TRANSFER:
398 399
            dbus_transfer(selectedCall);
            time(&selectedCall->_time_stop);
Tristan Matthews's avatar
Tristan Matthews committed
400 401
            calltree_remove_call(current_calls_tab, selectedCall);
            calllist_remove_call(current_calls_tab, selectedCall->_callID);
402 403 404 405 406 407 408 409
            break;
        case CALL_STATE_CURRENT:
        case CALL_STATE_HOLD:
        case CALL_STATE_RECORD:
        case CALL_STATE_RINGING:
            sflphone_new_call();
            break;
        default:
410
            WARN("Should not happen in sflphone_pick_up()!");
411
            break;
412
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
413 414
}

415
void
416
sflphone_on_hold()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
417
{
Tristan Matthews's avatar
Tristan Matthews committed
418 419
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
420

421 422
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
423 424
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
425
                dbus_hold(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
426 427
                break;
            default:
428
                WARN("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
429 430
                break;
        }
431
    } else if (selectedConf)
432
        dbus_hold_conference(selectedConf);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
433 434
}

435
void
436
sflphone_off_hold()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
437
{
438
    DEBUG("sflphone_off_hold");
Tristan Matthews's avatar
Tristan Matthews committed
439 440
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);
441

442 443
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
444
            case CALL_STATE_HOLD:
445
                dbus_unhold(selectedCall);
Emmanuel Milou's avatar
Emmanuel Milou committed
446 447
                break;
            default:
448
                WARN("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
449 450
                break;
        }
451
    } else if (selectedConf)
452
        dbus_unhold_conference(selectedConf);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
453 454 455
}


456
void
457
sflphone_fail(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
458
{
459
    c->_state = CALL_STATE_FAILURE;
Tristan Matthews's avatar
Tristan Matthews committed
460
    calltree_update_call(current_calls_tab, c);
461
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
462 463
}

464
void
465
sflphone_busy(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
466
{
467
    c->_state = CALL_STATE_BUSY;
Tristan Matthews's avatar
Tristan Matthews committed
468
    calltree_update_call(current_calls_tab, c);
469
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
470 471
}

472
void
473
sflphone_current(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
474
{
475
    if (c->_state != CALL_STATE_HOLD)
476
        time(&c->_time_start);
477

478
    c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
479
    calltree_update_call(current_calls_tab, c);
480
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
481 482
}

483
void
484
sflphone_record(callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
485
{
486
    if (c->_state != CALL_STATE_HOLD)
487
        time(&c->_time_start);
488

489
    c->_state = CALL_STATE_RECORD;
Tristan Matthews's avatar
Tristan Matthews committed
490
    calltree_update_call(current_calls_tab, c);
491
    update_actions();
alexandresavard's avatar
alexandresavard committed
492 493
}

494
void
495
sflphone_set_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
496
{
Tristan Matthews's avatar
Tristan Matthews committed
497
    callable_obj_t * c = calltab_get_selected_call(current_calls_tab);
498 499

    if (c) {
500
        c->_state = CALL_STATE_TRANSFER;
Tristan Matthews's avatar
Tristan Matthews committed
501
        g_free(c->_trsft_to);
502
        c->_trsft_to = g_strdup("");
Tristan Matthews's avatar
Tristan Matthews committed
503
        calltree_update_call(current_calls_tab, c);
Emmanuel Milou's avatar
Emmanuel Milou committed
504
    }
505

506
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
507 508
}

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

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

521
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
522
}
Emmanuel Milou's avatar
Emmanuel Milou committed
523

524
void
525
sflphone_display_transfer_status(const gchar* message)
526
{
527
    statusbar_push_message(message , NULL, __MSG_ACCOUNT_DEFAULT);
528 529
}

530
void
531
sflphone_incoming_call(callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
532
{
533
    c->_history_state = g_strdup(MISSED_STRING);
Tristan Matthews's avatar
Tristan Matthews committed
534 535
    calllist_add_call(current_calls_tab, c);
    calltree_add_call(current_calls_tab, c, NULL);
536

537
    update_actions();
Tristan Matthews's avatar
Tristan Matthews committed
538
    calltree_display(current_calls_tab);
539 540

    // Change the status bar if we are dealing with a direct SIP call
541
    if (is_direct_call(c)) {
542 543 544 545
        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);
546
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
547
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
548

549
static void
550
process_dialing(callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
551
{
Emmanuel Milou's avatar
Emmanuel Milou committed
552
    // We stop the tone
553
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFER)
554
        dbus_start_tone(FALSE, 0);
555

556
    switch (keyval) {
557 558
        case GDK_Return:
        case GDK_KP_Enter:
559
            sflphone_place_call(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
560
            break;
561
        case GDK_Escape:
562
            sflphone_hang_up();
Emmanuel Milou's avatar
Emmanuel Milou committed
563
            break;
564
        case GDK_BackSpace: {
565 566
            gchar *num = (c->_state == CALL_STATE_TRANSFER) ? c->_trsft_to : c->_peer_number;
            size_t len = strlen(num);
567

568 569 570
            if (len) {
                len--; // delete one character
                num[len] = '\0';
Tristan Matthews's avatar
Tristan Matthews committed
571
                calltree_update_call(current_calls_tab, c);
572

573
                /* If number is now empty, hang up immediately */
574
                if (c->_state != CALL_STATE_TRANSFER && len == 0)
575
                    dbus_hang_up(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
576
            }
577

578
            break;
579
        }
580 581 582 583 584
        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
585 586
            break;
        default:
587

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

591
                if (c->_state == CALL_STATE_TRANSFER) {
592 593
                    gchar *new_trsft = g_strconcat(c->_trsft_to, key, NULL);
                    g_free(c->_trsft_to);
Tristan Matthews's avatar
Tristan Matthews committed
594
                    c->_trsft_to = new_trsft;
595
                } else {
596 597 598
                    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
599
                    c->_peer_number = new_peer_number;
Emmanuel Milou's avatar
Emmanuel Milou committed
600
                }
601

Tristan Matthews's avatar
Tristan Matthews committed
602
                calltree_update_call(current_calls_tab, c);
Emmanuel Milou's avatar
Emmanuel Milou committed
603
            }
604

Emmanuel Milou's avatar
Emmanuel Milou committed
605 606
            break;
    }
607 608
}

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

610
callable_obj_t *
areversat's avatar
areversat committed
611
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
612
{
613 614 615 616 617
    // Disable screensaver if the list is empty call
    guint nbcall = calllist_get_size(current_calls_tab);
    if(nbcall == 0)
        dbus_screensaver_inhibit();

Tristan Matthews's avatar
Tristan Matthews committed
618
    callable_obj_t *current_selected_call = calltab_get_selected_call(current_calls_tab);
619

620
    if ((current_selected_call != NULL) && (current_selected_call->_confID == NULL))
621
        sflphone_on_hold();
622

Emmanuel Milou's avatar
Emmanuel Milou committed
623
    // Play a tone when creating a new call
Tristan Matthews's avatar
Tristan Matthews committed
624
    if (calllist_get_size(current_calls_tab) == 0)
625
        dbus_start_tone(TRUE , (current_account_has_new_message()  > 0) ? TONE_WITH_MESSAGE : TONE_WITHOUT_MESSAGE) ;
Emmanuel Milou's avatar
Emmanuel Milou committed
626

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

629
    c->_history_state = g_strdup(OUTGOING_STRING);
630

Tristan Matthews's avatar
Tristan Matthews committed
631 632
    calllist_add_call(current_calls_tab, c);
    calltree_add_call(current_calls_tab, c, NULL);
633
    update_actions();
634

Emmanuel Milou's avatar
Emmanuel Milou committed
635
    return c;
636 637
}

areversat's avatar
areversat committed
638

639
void
640
sflphone_keypad(guint keyval, gchar * key)
areversat's avatar
areversat committed
641
{
Tristan Matthews's avatar
Tristan Matthews committed
642
    callable_obj_t * c = calltab_get_selected_call(current_calls_tab);