actions.c 41.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=NULL;
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 141 142 143 144
static gboolean _is_direct_call (callable_obj_t * c)
{
    if (g_strcasecmp (c->_accountID, EMPTY_ENTRY) == 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);
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 336

    abookfactory_init_factory();
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 417
                DEBUG ("from sflphone_hang_up : ");
                stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
418 419 420
                break;
            case CALL_STATE_TRANSFERT:
                dbus_hang_up (selectedCall);
421
                call_remove_all_errors (selectedCall);
422
                time (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
423 424
                break;
            default:
425
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
426 427
                break;
        }
428
    } else if (selectedConf) {
429
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
430
        dbus_hang_up_conference (selectedConf);
431 432
    }

433
    calltree_update_call (history, selectedCall, NULL);
434

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

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

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

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

453 454 455
            // 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);
456

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

462 463 464 465
            // 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);
            }
466

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
            dbus_accept (selectedCall);
            stop_notification();
            break;
        case CALL_STATE_TRANSFERT:
            dbus_transfert (selectedCall);
            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;
485
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
486 487
}

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

494 495 496 497
    DEBUG ("sflphone_on_hold");

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

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

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

Julien Bonjean's avatar
Julien Bonjean committed
531

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


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

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

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

557
    if (c->_state != CALL_STATE_HOLD)
558
        time (&c->_time_start);
559

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

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

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

576
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
577
sflphone_set_transfert()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
578
{
579 580 581
    callable_obj_t * c = calltab_get_selected_call (current_calls);

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

588
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
589 590
}

591
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
592 593
sflphone_unset_transfert()
{
594 595 596
    callable_obj_t * c = calltab_get_selected_call (current_calls);

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

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

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

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

619
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
620
    calltree_display (current_calls);
621 622 623

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

631
void
632
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
633
{
Emmanuel Milou's avatar
Emmanuel Milou committed
634
    // We stop the tone
635
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFERT)
636
        dbus_start_tone (FALSE , 0);
637

638
    switch (keyval) {
639 640
        case GDK_Return:
        case GDK_KP_Enter:
641
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
642
            break;
643
        case GDK_Escape:
Rafaël Carré's avatar
Rafaël Carré committed
644
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
645
            break;
646
        case GDK_BackSpace:
647 648 649
            if (c->_state == CALL_STATE_TRANSFERT) {
                if (*c->_trsft_to)
                    c->_trsft_to[strlen (c->_trsft_to) - 1] = '\0';
650
                calltree_update_call (current_calls, c, NULL);
651 652 653 654 655 656 657
            } else {
                if (*c->_peer_number) {
                  c->_peer_number[strlen (c->_peer_number) -1] = '\0';
                  calltree_update_call (current_calls, c, NULL);
                }
                else
                  dbus_hang_up(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
658
            }
659 660

            break;
661 662 663 664 665
        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
666 667
            break;
        default:
668

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

672
                if (c->_state == CALL_STATE_TRANSFERT) {
Tristan Matthews's avatar
Tristan Matthews committed
673 674 675
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
676 677
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
678 679 680
                    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
681
                }
682 683

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
684
            }
685

Emmanuel Milou's avatar
Emmanuel Milou committed
686 687
            break;
    }
688 689
}

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

691
callable_obj_t *
areversat's avatar
areversat committed
692
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
693
{
Tristan Matthews's avatar
Tristan Matthews committed
694
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
695

696 697
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
698

Emmanuel Milou's avatar
Emmanuel Milou committed
699
    // Play a tone when creating a new call
700 701
    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
702

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

705 706
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
707
    calllist_add_call (current_calls, c);
708
    calltree_add_call (current_calls, c, NULL);
709
    update_actions();
710

Emmanuel Milou's avatar
Emmanuel Milou committed
711
    return c;
712 713
}

areversat's avatar
areversat committed
714

715 716
void
sflphone_keypad (guint keyval, gchar * key)
areversat's avatar
areversat committed
717
{
718 719 720 721
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if ( (active_calltree != current_calls) || (active_calltree == current_calls && !c)) {
        switch (keyval) {
722 723 724
            case GDK_Return:
            case GDK_KP_Enter:
            case GDK_Escape:
Emmanuel Milou's avatar
Emmanuel Milou committed
725 726
                break;
            default: