actions.c 32.7 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 82
    if (g_ascii_strcasecmp(id, current_id) != 0 ||
        account_list_get_size() == 0)
83 84
        return;

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

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

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

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

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

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

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

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


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

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

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

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

Emmanuel Milou's avatar
Emmanuel Milou committed
144

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

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

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

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

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

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

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

200
    status_tray_icon_blink(FALSE);
201

202
    statusbar_update_clock("");
203 204
}

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

210 211 212 213 214
    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);
215
            break;
216
        }
217
        account_list_add(acc);
218
        /* Fill the actual array of credentials */
219 220
        dbus_get_credentials(acc);
        gchar * status = account_lookup(acc, REGISTRATION_STATUS);
221

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

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

251 252
    g_strfreev(array);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // 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
360 361
}

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

611
callable_obj_t *
areversat's avatar
areversat committed
612
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
613
{
614 615 616 617 618
    // 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
619
    callable_obj_t *current_selected_call = calltab_get_selected_call(current_calls_tab);
620

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

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

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

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

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

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

areversat's avatar
areversat committed
639

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

Tristan Matthews's avatar
Tristan Matthews committed
645
    if ((active_calltree_tab != current_calls_tab) || (active_calltree_tab == current_calls_tab && !c)) {
646
        switch (keyval) {
647 648 649
            case GDK_Return:
            case GDK_KP_Enter:
            case GDK_Escape:
650
            case GDK_BackSpace:
Emmanuel Milou's avatar
Emmanuel Milou committed
651 652
                break;
            default:
Tristan Matthews's avatar
Tristan Matthews committed
653
                calltree_display(current_calls_tab);
654
                process_dialing(sflphone_new_call(), keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
655 656
                break;
        }
657 658
    } else if (c) {
        switch (c->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
659
            case CALL_STATE_DIALING: // Currently dialing => edit number
660
                process_dialing(c, keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
661 662 663
                break;
            case CALL_STATE_RECORD:
            case CALL_STATE_CURRENT:
664 665

                switch (keyval) {
666
                    case GDK_Escape:
667 668
                        dbus_hang_up(c);
                        time(&c->_time_stop);