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

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

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

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

64

65
static GHashTable * ip2ip_profile;
66

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

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

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

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

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

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

104
    return ordered_list;
105 106
}

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

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

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

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

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

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

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

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

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

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

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


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

161
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
162

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

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

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

Emmanuel Milou's avatar
Emmanuel Milou committed
179

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

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

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

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

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

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

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

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

234
    status_tray_icon_blink (FALSE);
235

236
    statusbar_update_clock("");
237 238
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
239
/** Internal to actions: Fill account list */
240 241
void sflphone_fill_account_list (void)
{
242
    int count = current_account_get_message_number ();
243

Emmanuel Milou's avatar
Emmanuel Milou committed
244
    account_list_clear ();
245

246
    gchar **array = dbus_account_list();
247
    if (array) {
248
        for (gchar **accountID = array; *accountID; accountID++) {
249 250
            account_t * a = g_new0 (account_t,1);
            a->accountID = g_strdup (*accountID);
251
            a->credential_information = NULL;
252
            account_list_add (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
253
        }
254

Emmanuel Milou's avatar
Emmanuel Milou committed
255
        g_strfreev (array);
256
    }
257

258
    for (unsigned i = 0; i < account_list_get_size(); i++) {
Emmanuel Milou's avatar
Emmanuel Milou committed
259
        account_t  * a = account_list_get_nth (i);
260 261
        if(a == NULL) {
            ERROR("SFLphone: Error: Could not find account %d in list", i);
262
            break;
263
        }
264

265 266 267 268 269
        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;
        }
270

271
        a->properties = details;
272 273

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

276 277
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

278
        if (g_strcmp0 (status, "REGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
279
            a->state = ACCOUNT_STATE_REGISTERED;
280
        } else if (g_strcmp0 (status, "UNREGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
281
            a->state = ACCOUNT_STATE_UNREGISTERED;
282
        } else if (g_strcmp0 (status, "TRYING") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
283
            a->state = ACCOUNT_STATE_TRYING;
284
        } else if (g_strcmp0 (status, "ERROR") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
285
            a->state = ACCOUNT_STATE_ERROR;
286
        } else if (g_strcmp0 (status , "ERROR_AUTH") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
287
            a->state = ACCOUNT_STATE_ERROR_AUTH;
288
        } else if (g_strcmp0 (status , "ERROR_NETWORK") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
289
            a->state = ACCOUNT_STATE_ERROR_NETWORK;
290
        } else if (g_strcmp0 (status , "ERROR_HOST") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
291
            a->state = ACCOUNT_STATE_ERROR_HOST;
292
        } else if (g_strcmp0 (status , "ERROR_CONF_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
293
            a->state = ACCOUNT_STATE_ERROR_CONF_STUN;
294
        } else if (g_strcmp0 (status , "ERROR_EXIST_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
295
            a->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
296
        } else if (g_strcmp0 (status, "READY") == 0) {
297 298
            a->state = IP2IP_PROFILE_STATUS;
        } else {
Emmanuel Milou's avatar
Emmanuel Milou committed
299 300 301
            a->state = ACCOUNT_STATE_INVALID;
        }

302 303
        gchar * code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);
        if (code != NULL)
304 305 306 307
            a->protocol_state_code = atoi (code);

        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
308 309
    }

310 311
    // Set the current account message number
    current_account_set_message_number (count);
312

313
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
314 315
}

316
gboolean sflphone_init (GError **error)
317
{
318
    if (!dbus_connect (error) || !dbus_register (getpid (), "Gtk+ Client", error))
319
        return FALSE;
320

Rafaël Carré's avatar
Rafaël Carré committed
321
    abook_init();
322

323 324
    // Init icons factory
    init_icon_factory ();
325

326 327 328
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
329

330 331
    account_list_init ();
    codec_capabilities_load ();
332 333
    conferencelist_init (current_calls);
    conferencelist_init (history);
334

335 336
    // Fetch the configured accounts
    sflphone_fill_account_list ();
337

338 339
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
340

341 342
    // Fetch the conference list
    // sflphone_fill_conference_list();
343

344
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
345 346
}

347
void sflphone_fill_ip2ip_profile (void)
348 349 350 351
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

352
GHashTable *sflphone_get_ip2ip_properties(void)
353
{
354
    return ip2ip_profile;
355 356
}

357
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
358
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
359
{
360 361
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
362

Julien Bonjean's avatar
Julien Bonjean committed
363
    DEBUG ("SFLphone: Hang up");
364

365 366
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
367 368 369 370 371
            case CALL_STATE_DIALING:
                dbus_hang_up (selectedCall);
                break;
            case CALL_STATE_RINGING:
                dbus_hang_up (selectedCall);
372
                call_remove_all_errors (selectedCall);
373 374
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
375 376 377 378
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
379
            case CALL_STATE_RECORD:
Emmanuel Milou's avatar
Emmanuel Milou committed
380
                dbus_hang_up (selectedCall);
381
                call_remove_all_errors (selectedCall);
382
                selectedCall->_state = CALL_STATE_DIALING;
383
                time (&selectedCall->_time_stop);
384

385
                im_widget_update_state (IM_WIDGET (selectedCall->_im_widget), FALSE);
386

Emmanuel Milou's avatar
Emmanuel Milou committed
387 388 389
                break;
            case CALL_STATE_FAILURE:
                dbus_hang_up (selectedCall);
390
                call_remove_all_errors (selectedCall);
391
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
392 393 394
                break;
            case CALL_STATE_INCOMING:
                dbus_refuse (selectedCall);
395
                call_remove_all_errors (selectedCall);
396
                selectedCall->_state = CALL_STATE_DIALING;
397
                DEBUG ("from sflphone_hang_up : ");
Emmanuel Milou's avatar
Emmanuel Milou committed
398
                break;
399
            case CALL_STATE_TRANSFER:
Emmanuel Milou's avatar
Emmanuel Milou committed
400
                dbus_hang_up (selectedCall);
401
                call_remove_all_errors (selectedCall);
402
                time (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
403 404
                break;
            default:
405
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
406 407
                break;
        }
408
    } else if (selectedConf) {
409
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
410
        dbus_hang_up_conference (selectedConf);
411 412
    }

413
    calltree_update_call (history, selectedCall, NULL);
414

415
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
416 417
}

418
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
419 420
sflphone_pick_up()
{
421 422 423 424 425
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
426

427 428 429
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
430

431 432
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (im_window_is_visible())
433 434
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
435

436 437 438 439
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
440

441
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
442 443 444
            if (im_window_is_visible())
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
445

446 447
            dbus_accept (selectedCall);
            break;
448 449
        case CALL_STATE_TRANSFER:
            dbus_transfer (selectedCall);
450 451 452 453 454 455 456 457 458 459 460 461 462
            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;
463
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
464 465
}

466
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
467 468
sflphone_on_hold ()
{
469 470
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
471

472 473
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
474 475 476 477 478
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
479
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
480 481
                break;
        }
482 483
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
484
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
485 486
}

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

494 495
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
496 497 498 499
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
500
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
501 502
                break;
        }
503 504
    } else if (selectedConf) {
        dbus_unhold_conference (selectedConf);
505
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
506 507 508
}


509 510
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
511
{
512
    c->_state = CALL_STATE_FAILURE;
513
    calltree_update_call (current_calls, c, NULL);
514
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
515 516
}

517 518
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
519
{
520
    c->_state = CALL_STATE_BUSY;
521
    calltree_update_call (current_calls, c, NULL);
522
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
523 524
}

525 526
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
527
{
528

529
    if (c->_state != CALL_STATE_HOLD)
530
        time (&c->_time_start);
531

532
    c->_state = CALL_STATE_CURRENT;
533
    calltree_update_call (current_calls, c, NULL);
534
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
535 536
}

537 538
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
539
{
540
    if (c->_state != CALL_STATE_HOLD)
541
        time (&c->_time_start);
542

543
    c->_state = CALL_STATE_RECORD;
544
    calltree_update_call (current_calls, c, NULL);
545
    update_actions();
alexandresavard's avatar
alexandresavard committed
546 547
}

548
void
549
sflphone_set_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
550
{
551 552 553
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
554
        c->_state = CALL_STATE_TRANSFER;
Tristan Matthews's avatar
Tristan Matthews committed
555
        g_free(c->_trsft_to);
556 557
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
558
    }
559

560
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
561 562
}

563
void
564
sflphone_unset_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
565
{
566 567 568
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
569
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
570
        g_free(c->_trsft_to);
571 572
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
573
    }
574

575
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
576
}
Emmanuel Milou's avatar
Emmanuel Milou committed
577

578 579
void
sflphone_display_transfer_status (const gchar* message)
580
{
Julien Bonjean's avatar
Julien Bonjean committed
581
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
582 583
}

584
void
585
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
586
{
587
    c->_history_state = MISSED;
588
    calllist_add_call (current_calls, c);
589
    calltree_add_call (current_calls, c, NULL);
590

591
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
592
    calltree_display (current_calls);
593 594 595

    // Change the status bar if we are dealing with a direct SIP call
    if (_is_direct_call (c)) {
596
        gchar *msg = g_markup_printf_escaped (_ ("Direct SIP call"));
597
        statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
Julien Bonjean's avatar
Julien Bonjean committed
598
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
599 600
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
601
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
602

603
static void
604
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
605
{
Emmanuel Milou's avatar
Emmanuel Milou committed
606
    // We stop the tone
607
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFER)
608
        dbus_start_tone (FALSE, 0);
609

610
    switch (keyval) {
611 612
        case GDK_Return:
        case GDK_KP_Enter:
613
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
614
            break;
615
        case GDK_Escape:
Rafaël Carré's avatar
Rafaël Carré committed
616
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
617
            break;
618
        case GDK_BackSpace:
619 620 621 622 623 624
        {
            gchar *num = (c->_state == CALL_STATE_TRANSFER) ? c->_trsft_to : c->_peer_number;
            size_t len = strlen(num);
            if (len) {
                len--; // delete one character
                num[len] = '\0';
625
                calltree_update_call (current_calls, c, NULL);
626

627
                /* If number is now empty, hang up immediately */
628
                if (c->_state != CALL_STATE_TRANSFER && len == 0)
629
                    dbus_hang_up(c);
Emmanuel Milou's avatar
Emmanuel Milou committed
630
            }
631
            break;
632
        }
633 634 635 636 637
        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
638 639
            break;
        default:
640

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

644
                if (c->_state == CALL_STATE_TRANSFER) {
Tristan Matthews's avatar
Tristan Matthews committed
645 646 647
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
648 649
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
650 651 652
                    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
653
                }
654 655

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
656
            }
657

Emmanuel Milou's avatar
Emmanuel Milou committed
658 659
            break;
    }
660 661
}

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

663
callable_obj_t *
areversat's avatar
areversat committed
664
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
665
{
Tristan Matthews's avatar
Tristan Matthews committed
666
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
667

668 669
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
670

Emmanuel Milou's avatar
Emmanuel Milou committed
671
    // Play a tone when creating a new call
672 673
    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
674

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

677 678
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
679
    calllist_add_call (current_calls, c);
680
    calltree_add_call (current_calls, c, NULL);
681
    update_actions();
682

Emmanuel Milou's avatar
Emmanuel Milou committed
683
    return c;
684 685
}

areversat's avatar
areversat committed
686

687 688
void
sflphone_keypad (guint keyval, gchar * key)
areversat's avatar
areversat committed
689
{
690 691 692 693
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if ( (active_calltree != current_calls) || (active_calltree == current_calls && !c)) {
        switch (keyval) {
694 695 696
            case GDK_Return:
            case GDK_KP_Enter:
            case GDK_Escape:
697
            case GDK_BackSpace:
Emmanuel Milou's avatar
Emmanuel Milou committed
698 699
                break;
            default:
Julien Bonjean's avatar
Julien Bonjean committed
700
                calltree_display (current_calls);
701
                process_dialing (sflphone_new_call(), keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
702 703
                break;
        }
Emmanuel Milou's avatar