actions.c 45.3 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 33 34
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <stdlib.h>
35
#include <string.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
36 37
#include <sys/types.h>
#include <unistd.h>
38
#include <assert.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
39

40
#include <arpa/nameser.h>
41
#include <netinet/in.h>
42 43
#include <resolv.h>

44
#include <linux/if.h>
45 46 47 48
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

49 50 51 52 53 54 55 56 57
#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"

58

59
static GHashTable * ip2ip_profile=NULL;
60

61
static gchar ** sflphone_order_history_hash_table(GHashTable *result)
62 63
{
    GHashTableIter iter;
64 65
    gint size = 0;
    gchar **ordered_list = NULL;
66

67
    assert(result);
68
    while (g_hash_table_size (result)) {
69
        gpointer key, key_to_min, value;
70 71 72 73

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

74
        gint min_timestamp = G_MAXINT;
75
        while (g_hash_table_iter_next (&iter, &key, &value))  {
76 77
            gint timestamp = atoi ( (gchar*) key);
            if (timestamp < min_timestamp) {
78 79 80 81 82
                min_timestamp = timestamp;
                key_to_min = key;
            }
        }

Tristan Matthews's avatar
Tristan Matthews committed
83 84 85 86 87 88 89 90 91
        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);
92 93 94
        }
    }

95 96
    ordered_list = (void *) g_realloc(ordered_list, (size + 1) * sizeof(void *));
    *(ordered_list + size) = NULL;
97

98
    return ordered_list;
99 100
}

101
void
102
sflphone_notify_voice_mail (const gchar* accountID , guint count)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
103
{
104
    gchar *id;
105
    gchar *current_id;
106
    account_t *current;
Emmanuel Milou's avatar
Emmanuel Milou committed
107

108 109 110 111
    // We want to notify only the current account; ie the first in the list
    id = g_strdup (accountID);
    current_id = account_list_get_current_id ();

Julien Bonjean's avatar
Julien Bonjean committed
112
    DEBUG ("sflphone_notify_voice_mail begin");
113

114
    if (g_strcasecmp (id, current_id) != 0 || account_list_get_size() == 0)
115 116
        return;

117 118 119
    // Set the number of voice messages for the current account
    current_account_set_message_number (count);
    current = account_list_get_current ();
120

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

124 125
    if (current)
        notify_voice_mails (count, current);
Emmanuel Milou's avatar
Emmanuel Milou committed
126

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

130 131 132 133 134 135 136
/*
 * 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
 */

137 138 139 140 141 142
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);
143 144
            c->_peer_number = new_number;
        }
145

146
        return TRUE;
Alexandre Savard's avatar
Alexandre Savard committed
147 148
    }

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


154
void
155
status_bar_display_account ()
156
{
157
    gchar* msg;
158
    account_t* acc;
159

160
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
161

Julien Bonjean's avatar
Julien Bonjean committed
162
    DEBUG ("status_bar_display_account begin");
163

164
    acc = account_list_get_current ();
165 166 167 168 169 170 171 172 173 174

    if (acc) {
        status_tray_icon_online (TRUE);
        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 {
        status_tray_icon_online (FALSE);
        msg = g_markup_printf_escaped (_ ("No registered accounts"));
175
    }
176

Julien Bonjean's avatar
Julien Bonjean committed
177
    statusbar_push_message (msg, NULL,  __MSG_ACCOUNT_DEFAULT);
178
    g_free (msg);
Julien Bonjean's avatar
Julien Bonjean committed
179 180

    DEBUG ("status_bar_display_account_end");
181 182
}

Emmanuel Milou's avatar
Emmanuel Milou committed
183

184
gboolean
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
185 186
sflphone_quit ()
{
Emmanuel Milou's avatar
Emmanuel Milou committed
187
    gboolean quit = FALSE;
188 189 190
    guint count = calllist_get_size (current_calls);

    if (count > 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
191
        quit = main_window_ask_quit();
192
    } else {
Emmanuel Milou's avatar
Emmanuel Milou committed
193 194 195
        quit = TRUE;
    }

196 197
    if (quit) {
        // Save the history
198 199
        sflphone_save_history ();

200
        dbus_unregister (getpid());
Emmanuel Milou's avatar
Emmanuel Milou committed
201
        dbus_clean ();
Vivien Didelot's avatar
Vivien Didelot committed
202 203 204
        calllist_clean (current_calls);
        calllist_clean (contacts);
        calllist_clean (history);
Emmanuel Milou's avatar
Emmanuel Milou committed
205 206 207
        //account_list_clean()
        gtk_main_quit ();
    }
208

Emmanuel Milou's avatar
Emmanuel Milou committed
209
    return quit;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
210 211
}

212 213
void
sflphone_hold (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
214
{
215
    c->_state = CALL_STATE_HOLD;
216
    calltree_update_call (current_calls, c, NULL);
217
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
218 219
}

220 221
void
sflphone_ringing (callable_obj_t * c)
222
{
223
    c->_state = CALL_STATE_RINGING;
224
    calltree_update_call (current_calls, c, NULL);
225
    update_actions();
226
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
227

228 229
void
sflphone_hung_up (callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
230
{
Julien Bonjean's avatar
Julien Bonjean committed
231
    DEBUG ("SFLphone: Hung up");
232

233
    calllist_remove_call (current_calls, c->_callID);
234
    calltree_remove_call (current_calls, c, NULL);
235
    c->_state = CALL_STATE_DIALING;
236
    call_remove_all_errors (c);
237
    update_actions();
238

239 240 241 242 243
    if(c->_confID) {
	g_free(c->_confID);
	c->_confID = NULL;
    }

244 245 246 247 248
    // 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);
249

250
#if GTK_CHECK_VERSION(2,10,0)
251
    status_tray_icon_blink (FALSE);
252
#endif
253
    stop_call_clock(c);
254
    calltree_update_clock();
255 256
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
257
/** Internal to actions: Fill account list */
258 259
void sflphone_fill_account_list (void)
{
260 261 262 263

    gchar** array;
    gchar** accountID;
    unsigned int i;
264
    int count;
265

266
    DEBUG ("SFLphone: Fill account list");
267

268
    count = current_account_get_message_number ();
269

Emmanuel Milou's avatar
Emmanuel Milou committed
270
    account_list_clear ();
271

272 273 274
    array = (gchar **) dbus_account_list();

    if (array) {
Julien Bonjean's avatar
Julien Bonjean committed
275

276 277 278
        for (accountID = array; *accountID; accountID++) {
            account_t * a = g_new0 (account_t,1);
            a->accountID = g_strdup (*accountID);
279
            a->credential_information = NULL;
280
            account_list_add (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
281
        }
282

Emmanuel Milou's avatar
Emmanuel Milou committed
283
        g_strfreev (array);
284
    }
285

286
    for (i = 0; i < account_list_get_size(); i++) {
Emmanuel Milou's avatar
Emmanuel Milou committed
287
        account_t  * a = account_list_get_nth (i);
288 289
        if(a == NULL) {
            ERROR("SFLphone: Error: Could not find account %d in list", i);
290
            break;
291
        }
292

293 294 295 296 297
        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;
        }
298

299
        a->properties = details;
300 301

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

304 305
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

306
        if (g_strcmp0 (status, "REGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
307
            a->state = ACCOUNT_STATE_REGISTERED;
308
        } else if (g_strcmp0 (status, "UNREGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
309
            a->state = ACCOUNT_STATE_UNREGISTERED;
310
        } else if (g_strcmp0 (status, "TRYING") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
311
            a->state = ACCOUNT_STATE_TRYING;
312
        } else if (g_strcmp0 (status, "ERROR") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
313
            a->state = ACCOUNT_STATE_ERROR;
314
        } else if (g_strcmp0 (status , "ERROR_AUTH") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
315
            a->state = ACCOUNT_STATE_ERROR_AUTH;
316
        } else if (g_strcmp0 (status , "ERROR_NETWORK") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
317
            a->state = ACCOUNT_STATE_ERROR_NETWORK;
318
        } else if (g_strcmp0 (status , "ERROR_HOST") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
319
            a->state = ACCOUNT_STATE_ERROR_HOST;
320
        } else if (g_strcmp0 (status , "ERROR_CONF_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
321
            a->state = ACCOUNT_STATE_ERROR_CONF_STUN;
322
        } else if (g_strcmp0 (status , "ERROR_EXIST_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
323
            a->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
324
        } else if (g_strcmp0 (status, "READY") == 0) {
325 326
            a->state = IP2IP_PROFILE_STATUS;
        } else {
Emmanuel Milou's avatar
Emmanuel Milou committed
327 328 329
            a->state = ACCOUNT_STATE_INVALID;
        }

330
        gchar * code = NULL;
331 332
        code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);

333
        if (code != NULL) {
334
            a->protocol_state_code = atoi (code);
335
        }
336 337 338

        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
339 340
    }

341 342
    // Set the current account message number
    current_account_set_message_number (count);
343

344
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
345 346
}

347
gboolean sflphone_init (GError **error)
348
{
349
    if (!dbus_connect (error)) {
350
        return FALSE;
351
    }
Emmanuel Milou's avatar
Emmanuel Milou committed
352

353
    if (!dbus_register (getpid (), "Gtk+ Client", error)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
354
        return FALSE;
355
    }
356 357

    abookfactory_init_factory();
358

359 360
    // Init icons factory
    init_icon_factory ();
361

362 363 364
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
365

366 367
    account_list_init ();
    codec_capabilities_load ();
368 369
    conferencelist_init (current_calls);
    conferencelist_init (history);
370

371 372
    // Fetch the configured accounts
    sflphone_fill_account_list ();
373

374 375
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
376

377 378
    // Fetch the conference list
    // sflphone_fill_conference_list();
379

380
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
381 382
}

383
void sflphone_fill_ip2ip_profile (void)
384 385 386 387
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

Emmanuel Milou's avatar
Emmanuel Milou committed
388
void sflphone_get_ip2ip_properties (GHashTable **properties)
389
{
390
    *properties	= ip2ip_profile;
391 392
}

393
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
394
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
395
{
396 397
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
398

Julien Bonjean's avatar
Julien Bonjean committed
399
    DEBUG ("SFLphone: Hang up");
400

401 402
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
403 404 405 406 407
            case CALL_STATE_DIALING:
                dbus_hang_up (selectedCall);
                break;
            case CALL_STATE_RINGING:
                dbus_hang_up (selectedCall);
408
                call_remove_all_errors (selectedCall);
409 410
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
411 412 413 414
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
415
            case CALL_STATE_RECORD:
Emmanuel Milou's avatar
Emmanuel Milou committed
416
                dbus_hang_up (selectedCall);
417
                call_remove_all_errors (selectedCall);
418
                selectedCall->_state = CALL_STATE_DIALING;
419
                set_timestamp (&selectedCall->_time_stop);
420 421 422 423 424

                //if ( (im_window_get_nb_tabs() > 1) && selectedCall->_im_widget &&
                //        ! (IM_WIDGET (selectedCall->_im_widget)->containText))
                //    im_window_remove_tab (selectedCall->_im_widget);
                //else
425
                im_widget_update_state (IM_WIDGET (selectedCall->_im_widget), FALSE);
426

Emmanuel Milou's avatar
Emmanuel Milou committed
427 428 429
                break;
            case CALL_STATE_FAILURE:
                dbus_hang_up (selectedCall);
430
                call_remove_all_errors (selectedCall);
431
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
432 433 434
                break;
            case CALL_STATE_INCOMING:
                dbus_refuse (selectedCall);
435
                call_remove_all_errors (selectedCall);
436
                selectedCall->_state = CALL_STATE_DIALING;
437 438
                DEBUG ("from sflphone_hang_up : ");
                stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
439 440 441
                break;
            case CALL_STATE_TRANSFERT:
                dbus_hang_up (selectedCall);
442
                call_remove_all_errors (selectedCall);
443
                set_timestamp (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
444 445
                break;
            default:
446
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
447 448
                break;
        }
449
    } else if (selectedConf) {
450
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
451
        dbus_hang_up_conference (selectedConf);
452 453
    }

454
    calltree_update_call (history, selectedCall, NULL);
455

Rafaël Carré's avatar
Rafaël Carré committed
456 457
    if (selectedCall)
        stop_call_clock (selectedCall);
458

459
    calltree_update_clock();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
460 461 462
}


Alexandre Savard's avatar
Alexandre Savard committed
463 464 465
void
sflphone_conference_hang_up()
{
466
    conference_obj_t * selectedConf = calltab_get_selected_conf(current_calls);
Alexandre Savard's avatar
Alexandre Savard committed
467

468
    if (selectedConf) {
469
        dbus_hang_up_conference (selectedConf);
470
    }
Alexandre Savard's avatar
Alexandre Savard committed
471 472 473
}


474
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
475 476
sflphone_pick_up()
{
477
    callable_obj_t * selectedCall = NULL;
478 479
    selectedCall = calltab_get_selected_call (active_calltree);

480 481
    DEBUG("SFLphone: Pick up");

482 483
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
484 485
            case CALL_STATE_DIALING:
                sflphone_place_call (selectedCall);
486 487 488 489 490

                // 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);

Emmanuel Milou's avatar
Emmanuel Milou committed
491 492
                break;
            case CALL_STATE_INCOMING:
493
                selectedCall->_history_state = INCOMING;
494
                calltree_update_call (history, selectedCall, NULL);
495 496

                // if instant messaging window is visible, create new tab (deleted automatically if not used)
497
                if (selectedCall->_im_widget && im_window_is_visible()) {
498
                    im_widget_display ( (IMWidget **) (&selectedCall->_im_widget), NULL, selectedCall->_callID, NULL);
499
	        }
500

Emmanuel Milou's avatar
Emmanuel Milou committed
501
                dbus_accept (selectedCall);
502
                stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
503
                break;
504
            case CALL_STATE_HOLD:
Emmanuel Milou's avatar
Emmanuel Milou committed
505 506 507 508
                sflphone_new_call();
                break;
            case CALL_STATE_TRANSFERT:
                dbus_transfert (selectedCall);
509
                set_timestamp (&selectedCall->_time_stop);
510
                calltree_remove_call(current_calls, selectedCall, NULL);
511
                calllist_remove_call(current_calls, selectedCall->_callID);
512
		break;
Emmanuel Milou's avatar
Emmanuel Milou committed
513 514 515 516 517 518 519 520
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
                sflphone_new_call();
                break;
            case CALL_STATE_RINGING:
                sflphone_new_call();
                break;
            default:
521
                WARN ("Should not happen in sflphone_pick_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
522 523
                break;
        }
524
    } else {
525 526
        sflphone_new_call();
    }
527

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
528 529
}

530
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
531 532
sflphone_on_hold ()
{
533 534
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
535

536 537 538 539
    DEBUG ("sflphone_on_hold");

    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
540 541 542 543 544 545 546
            case CALL_STATE_CURRENT:
                dbus_hold (selectedCall);
                break;
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
547
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
548 549
                break;
        }
550 551
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
552
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
553 554
}

555
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
556 557
sflphone_off_hold ()
{
558 559 560
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
561

562 563
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
564 565 566 567
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
568
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
569 570
                break;
        }
571 572
    } else if (selectedConf) {

Julien Bonjean's avatar
Julien Bonjean committed
573

574
        dbus_unhold_conference (selectedConf);
575
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
576 577 578
}


579 580
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
581
{
582
    c->_state = CALL_STATE_FAILURE;
583
    calltree_update_call (current_calls, c, NULL);
584
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
585 586
}

587 588
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
589
{
590
    c->_state = CALL_STATE_BUSY;
591
    calltree_update_call (current_calls, c, NULL);
592
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
593 594
}

595 596
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
597
{
598

599
    if (c->_state != CALL_STATE_HOLD)
600
        set_timestamp (&c->_time_start);
601

602
    c->_state = CALL_STATE_CURRENT;
603
    calltree_update_call (current_calls, c, NULL);
604
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
605 606
}

607 608
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
609
{
610
    if (c->_state != CALL_STATE_HOLD)
611
        set_timestamp (&c->_time_start);
612

613
    c->_state = CALL_STATE_RECORD;
614
    calltree_update_call (current_calls, c, NULL);
615
    update_actions();
alexandresavard's avatar
alexandresavard committed
616 617
}

618
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
619
sflphone_set_transfert()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
620
{
621 622 623
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
624
        c->_state = CALL_STATE_TRANSFERT;
Tristan Matthews's avatar
Tristan Matthews committed
625
        g_free(c->_trsft_to);
626 627
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
628
    }
629

630
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
631 632
}

633
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
634 635
sflphone_unset_transfert()
{
636 637 638
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
639
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
640
        g_free(c->_trsft_to);
641 642
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
643
    }
644

645
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
646
}
Emmanuel Milou's avatar
Emmanuel Milou committed
647

648 649
void
sflphone_display_transfer_status (const gchar* message)
650
{
Julien Bonjean's avatar
Julien Bonjean committed
651
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
652 653
}

654
void
655
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
656
{
657
    gchar *msg = "";
658

659
    c->_history_state = MISSED;
660 661
    calllist_add_call (current_calls, c);
    calllist_add_call (history, c);
662
    calltree_add_call (current_calls, c, NULL);
663
    calltree_add_call (history, c, NULL);
664
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
665
    calltree_display (current_calls);
666 667 668 669 670

    // Change the status bar if we are dealing with a direct SIP call
    if (_is_direct_call (c)) {
        msg = g_markup_printf_escaped (_ ("Direct SIP call"));
        statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
Julien Bonjean's avatar
Julien Bonjean committed
671
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
672 673
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
674
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
675

676
void
677
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
678
{
Emmanuel Milou's avatar
Emmanuel Milou committed
679
    // We stop the tone
680 681
    if (strlen (c->_peer_number) == 0 && c->_state != CALL_STATE_TRANSFERT) {
        dbus_start_tone (FALSE , 0);
Emmanuel Milou's avatar
Emmanuel Milou committed
682 683
        //dbus_play_dtmf( key );
    }
684

685
    switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
686 687
        case 65293: /* ENTER */
        case 65421: /* ENTER numpad */
688
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
689 690
            break;
        case 65307: /* ESCAPE */
Rafaël Carré's avatar
Rafaël Carré committed
691
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
692
            break;
Alexandre Savard's avatar
Alexandre Savard committed
693 694
        case 65288: { /* BACKSPACE */
            /* Brackets mandatory because of local vars */
695 696 697 698 699 700 701 702 703 704 705
            gchar * before = c->_peer_number;

            if (strlen (c->_peer_number) >= 1) {

                if (c->_state == CALL_STATE_TRANSFERT) {
                    // Process backspace if and only if string not NULL
                    if (strlen (c->_trsft_to) > 0)
                        c->_trsft_to = g_strndup (c->_trsft_to, strlen (c->_trsft_to) - 1);
                } else {
                    c->_peer_number = g_strndup (c->_peer_number, strlen (c->_peer_number) -1);
                    g_free (before);
706
                    DEBUG ("SFLphone: TO: backspace %s", c->_peer_number);
Emmanuel Milou's avatar
Emmanuel Milou committed
707
                }
708 709 710 711 712

                calltree_update_call (current_calls, c, NULL);
            } else if (strlen (c->_peer_number) == 0) {
                if (c->_state != CALL_STATE_TRANSFERT)
                    dbus_hang_up (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
713
            }
714 715
        }
        break;
Emmanuel Milou's avatar
Emmanuel Milou committed
716 717 718 719 720 721 722
        case 65289: /* TAB */
        case 65513: /* ALT */
        case 65507: /* CTRL */
        case 65515: /* SUPER */
        case 65509: /* CAPS */
            break;
        default:
723

Emmanuel Milou's avatar
Emmanuel Milou committed
724
            // if (keyval < 255 || (keyval >65453 && keyval < 65466))
725
            if (keyval < 127 || (keyval > 65400 && keyval < 65466)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
726

727
                if (c->_state == CALL_STATE_TRANSFERT) {