actions.c 43.8 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

159
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
160

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

    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"));
171
    }
172

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

Emmanuel Milou's avatar
Emmanuel Milou committed
177

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

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

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

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

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

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

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

227 228 229 230 231
    // 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);
232

233
#if GTK_CHECK_VERSION(2,10,0)
234
    status_tray_icon_blink (FALSE);
235
#endif
236

237
    statusbar_update_clock("");
238 239
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
240
/** Internal to actions: Fill account list */
241 242
void sflphone_fill_account_list (void)
{
243 244 245 246

    gchar** array;
    gchar** accountID;
    unsigned int i;
247
    int count;
248

249
    DEBUG ("SFLphone: Fill account list");
250

251
    count = current_account_get_message_number ();
252

Emmanuel Milou's avatar
Emmanuel Milou committed
253
    account_list_clear ();
254

255 256 257
    array = (gchar **) dbus_account_list();

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

259 260 261
        for (accountID = array; *accountID; accountID++) {
            account_t * a = g_new0 (account_t,1);
            a->accountID = g_strdup (*accountID);
262
            a->credential_information = NULL;
263
            account_list_add (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
264
        }
265

Emmanuel Milou's avatar
Emmanuel Milou committed
266
        g_strfreev (array);
267
    }
268

269
    for (i = 0; i < account_list_get_size(); i++) {
Emmanuel Milou's avatar
Emmanuel Milou committed
270
        account_t  * a = account_list_get_nth (i);
271 272
        if(a == NULL) {
            ERROR("SFLphone: Error: Could not find account %d in list", i);
273
            break;
274
        }
275

276 277 278 279 280
        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;
        }
281

282
        a->properties = details;
283 284

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

287 288
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

289
        if (g_strcmp0 (status, "REGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
290
            a->state = ACCOUNT_STATE_REGISTERED;
291
        } else if (g_strcmp0 (status, "UNREGISTERED") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
292
            a->state = ACCOUNT_STATE_UNREGISTERED;
293
        } else if (g_strcmp0 (status, "TRYING") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
294
            a->state = ACCOUNT_STATE_TRYING;
295
        } else if (g_strcmp0 (status, "ERROR") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
296
            a->state = ACCOUNT_STATE_ERROR;
297
        } else if (g_strcmp0 (status , "ERROR_AUTH") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
298
            a->state = ACCOUNT_STATE_ERROR_AUTH;
299
        } else if (g_strcmp0 (status , "ERROR_NETWORK") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
300
            a->state = ACCOUNT_STATE_ERROR_NETWORK;
301
        } else if (g_strcmp0 (status , "ERROR_HOST") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
302
            a->state = ACCOUNT_STATE_ERROR_HOST;
303
        } else if (g_strcmp0 (status , "ERROR_CONF_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
304
            a->state = ACCOUNT_STATE_ERROR_CONF_STUN;
305
        } else if (g_strcmp0 (status , "ERROR_EXIST_STUN") == 0) {
Emmanuel Milou's avatar
Emmanuel Milou committed
306
            a->state = ACCOUNT_STATE_ERROR_EXIST_STUN;
307
        } else if (g_strcmp0 (status, "READY") == 0) {
308 309
            a->state = IP2IP_PROFILE_STATUS;
        } else {
Emmanuel Milou's avatar
Emmanuel Milou committed
310 311 312
            a->state = ACCOUNT_STATE_INVALID;
        }

313
        gchar * code = NULL;
314 315
        code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);

316
        if (code != NULL) {
317
            a->protocol_state_code = atoi (code);
318
        }
319 320 321

        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
322 323
    }

324 325
    // Set the current account message number
    current_account_set_message_number (count);
326

327
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
328 329
}

330
gboolean sflphone_init (GError **error)
331
{
332
    if (!dbus_connect (error)) {
333
        return FALSE;
334
    }
Emmanuel Milou's avatar
Emmanuel Milou committed
335

336
    if (!dbus_register (getpid (), "Gtk+ Client", error)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
337
        return FALSE;
338
    }
339 340

    abookfactory_init_factory();
341

342 343
    // Init icons factory
    init_icon_factory ();
344

345 346 347
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
348

349 350
    account_list_init ();
    codec_capabilities_load ();
351 352
    conferencelist_init (current_calls);
    conferencelist_init (history);
353

354 355
    // Fetch the configured accounts
    sflphone_fill_account_list ();
356

357 358
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
359

360 361
    // Fetch the conference list
    // sflphone_fill_conference_list();
362

363
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
364 365
}

366
void sflphone_fill_ip2ip_profile (void)
367 368 369 370
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

371
GHashTable *sflphone_get_ip2ip_properties(void)
372
{
373
    return ip2ip_profile;
374 375
}

376
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
377
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
378
{
379 380
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
381

Julien Bonjean's avatar
Julien Bonjean committed
382
    DEBUG ("SFLphone: Hang up");
383

384 385
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
386 387 388 389 390
            case CALL_STATE_DIALING:
                dbus_hang_up (selectedCall);
                break;
            case CALL_STATE_RINGING:
                dbus_hang_up (selectedCall);
391
                call_remove_all_errors (selectedCall);
392 393
                selectedCall->_state = CALL_STATE_DIALING;
                //selectedCall->_stop = 0;
Emmanuel Milou's avatar
Emmanuel Milou committed
394 395 396 397
                break;
            case CALL_STATE_CURRENT:
            case CALL_STATE_HOLD:
            case CALL_STATE_BUSY:
398
            case CALL_STATE_RECORD:
Emmanuel Milou's avatar
Emmanuel Milou committed
399
                dbus_hang_up (selectedCall);
400
                call_remove_all_errors (selectedCall);
401
                selectedCall->_state = CALL_STATE_DIALING;
402
                time (&selectedCall->_time_stop);
403 404 405 406 407

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

Emmanuel Milou's avatar
Emmanuel Milou committed
410 411 412
                break;
            case CALL_STATE_FAILURE:
                dbus_hang_up (selectedCall);
413
                call_remove_all_errors (selectedCall);
414
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
415 416 417
                break;
            case CALL_STATE_INCOMING:
                dbus_refuse (selectedCall);
418
                call_remove_all_errors (selectedCall);
419
                selectedCall->_state = CALL_STATE_DIALING;
420 421
                DEBUG ("from sflphone_hang_up : ");
                stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
422 423 424
                break;
            case CALL_STATE_TRANSFERT:
                dbus_hang_up (selectedCall);
425
                call_remove_all_errors (selectedCall);
426
                time (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
427 428
                break;
            default:
429
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
430 431
                break;
        }
432
    } else if (selectedConf) {
433
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
434
        dbus_hang_up_conference (selectedConf);
435 436
    }

437
    calltree_update_call (history, selectedCall, NULL);
438

439
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
440 441
}

442
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
443 444
sflphone_pick_up()
{
445
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
446

447 448
    DEBUG("SFLphone: Pick up");

449 450 451 452 453 454 455
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
456

457 458 459
            // 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);
460

461 462 463 464
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
465

466 467 468 469
            // 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);
            }
470

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
            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;
489
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
490 491
}

492
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
493 494
sflphone_on_hold ()
{
495 496
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
497

498 499 500 501
    DEBUG ("sflphone_on_hold");

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

517
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
518 519
sflphone_off_hold ()
{
520 521 522
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
523

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

Julien Bonjean's avatar
Julien Bonjean committed
535

536
        dbus_unhold_conference (selectedConf);
537
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
538 539 540
}


541 542
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
543
{
544
    c->_state = CALL_STATE_FAILURE;
545
    calltree_update_call (current_calls, c, NULL);
546
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
547 548
}

549 550
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
551
{
552
    c->_state = CALL_STATE_BUSY;
553
    calltree_update_call (current_calls, c, NULL);
554
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
555 556
}

557 558
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
559
{
560

561
    if (c->_state != CALL_STATE_HOLD)
562
        time (&c->_time_start);
563

564
    c->_state = CALL_STATE_CURRENT;
565
    calltree_update_call (current_calls, c, NULL);
566
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
567 568
}

569 570
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
571
{
572
    if (c->_state != CALL_STATE_HOLD)
573
        time (&c->_time_start);
574

575
    c->_state = CALL_STATE_RECORD;
576
    calltree_update_call (current_calls, c, NULL);
577
    update_actions();
alexandresavard's avatar
alexandresavard committed
578 579
}

580
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
581
sflphone_set_transfert()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
582
{
583 584 585
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
586
        c->_state = CALL_STATE_TRANSFERT;
Tristan Matthews's avatar
Tristan Matthews committed
587
        g_free(c->_trsft_to);
588 589
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
590
    }
591

592
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
593 594
}

595
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
596 597
sflphone_unset_transfert()
{
598 599 600
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
601
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
602
        g_free(c->_trsft_to);
603 604
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
605
    }
606

607
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
608
}
Emmanuel Milou's avatar
Emmanuel Milou committed
609

610 611
void
sflphone_display_transfer_status (const gchar* message)
612
{
Julien Bonjean's avatar
Julien Bonjean committed
613
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
614 615
}

616
void
617
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
618
{
619
    gchar *msg = "";
620

621
    c->_history_state = MISSED;
622 623
    calllist_add_call (current_calls, c);
    calllist_add_call (history, c);
624
    calltree_add_call (current_calls, c, NULL);
625
    calltree_add_call (history, c, NULL);
626
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
627
    calltree_display (current_calls);
628 629 630 631 632

    // 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
633
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
634 635
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
636
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
637

638
void
639
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
640
{
Emmanuel Milou's avatar
Emmanuel Milou committed
641
    // We stop the tone
642 643
    if (strlen (c->_peer_number) == 0 && c->_state != CALL_STATE_TRANSFERT) {
        dbus_start_tone (FALSE , 0);
Emmanuel Milou's avatar
Emmanuel Milou committed
644 645
        //dbus_play_dtmf( key );
    }
646

647
    switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
648 649
        case 65293: /* ENTER */
        case 65421: /* ENTER numpad */
650
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
651 652
            break;
        case 65307: /* ESCAPE */
Rafaël Carré's avatar
Rafaël Carré committed
653
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
654
            break;
Alexandre Savard's avatar
Alexandre Savard committed
655 656
        case 65288: { /* BACKSPACE */
            /* Brackets mandatory because of local vars */
657 658 659 660 661 662 663 664 665 666 667
            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);
668
                    DEBUG ("SFLphone: TO: backspace %s", c->_peer_number);
Emmanuel Milou's avatar
Emmanuel Milou committed
669
                }
670 671 672 673 674

                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
675
            }
676 677
        }
        break;
Emmanuel Milou's avatar
Emmanuel Milou committed
678 679 680 681 682 683 684
        case 65289: /* TAB */
        case 65513: /* ALT */
        case 65507: /* CTRL */
        case 65515: /* SUPER */
        case 65509: /* CAPS */
            break;
        default:
685

Emmanuel Milou's avatar
Emmanuel Milou committed
686
            // if (keyval < 255 || (keyval >65453 && keyval < 65466))
687
            if (keyval < 127 || (keyval > 65400 && keyval < 65466)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
688

689
                if (c->_state == CALL_STATE_TRANSFERT) {
Tristan Matthews's avatar
Tristan Matthews committed
690 691 692
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
693 694
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
695 696 697
                    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
698
                }
699 700

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
701
            }
702

Emmanuel Milou's avatar
Emmanuel Milou committed
703 704
            break;
    }
705 706
}

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

708
callable_obj_t *
areversat's avatar
areversat committed
709
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
710
{
Julien Bonjean's avatar
Julien Bonjean committed
711
    DEBUG ("Actions: Sflphone new call");
712

Tristan Matthews's avatar
Tristan Matthews committed
713
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
714

715 716
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
717

Emmanuel Milou's avatar
Emmanuel Milou committed
718
    // Play a tone when creating a new call
719 720
    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
721

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

724 725
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
726
    calllist_add_call (current_calls, c);
727
    calltree_add_call (current_calls, c, NULL);
728
    update_actions();
729

Emmanuel Milou's avatar
Emmanuel Milou committed
730
    return c;
731 732
}

areversat's avatar
areversat committed
733