actions.c 39.2 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
#include "actions.h"
#include "dbus/dbus.h"
57
#include "logger.h"
58 59 60 61 62 63 64
#include "contacts/searchbar.h"
#include "contacts/addrbookfactory.h"
#include "icons/icon_factory.h"
#include "imwindow.h"
#include "statusicon.h"
#include "widget/imwidget.h"

65

66
static GHashTable * ip2ip_profile;
67

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

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

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

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

Tristan Matthews's avatar
Tristan Matthews committed
90 91 92 93 94 95 96 97 98
        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);
99 100 101
        }
    }

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

105
    return ordered_list;
106 107
}

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

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

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

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

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

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

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

133 134 135 136 137 138 139
/*
 * 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
 */

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

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

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


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

162
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
163

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

    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"));
174
    }
175

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

Emmanuel Milou's avatar
Emmanuel Milou committed
180

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

188
        dbus_unregister (getpid());
Emmanuel Milou's avatar
Emmanuel Milou committed
189
        dbus_clean ();
Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
190
        account_list_free();
Vivien Didelot's avatar
Vivien Didelot committed
191 192 193
        calllist_clean (current_calls);
        calllist_clean (contacts);
        calllist_clean (history);
Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
194 195 196
        gtk_tree_store_clear(history->store);
        gtk_tree_store_clear(current_calls->store);
        gtk_tree_store_clear(contacts->store);
Emmanuel Milou's avatar
Emmanuel Milou committed
197 198
        gtk_main_quit ();
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
199 200
}

201 202
void
sflphone_hold (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
203
{
204
    c->_state = CALL_STATE_HOLD;
205
    calltree_update_call (current_calls, c, NULL);
206
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
207 208
}

209 210
void
sflphone_ringing (callable_obj_t * c)
211
{
212
    c->_state = CALL_STATE_RINGING;
213
    calltree_update_call (current_calls, c, NULL);
214
    update_actions();
215
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
216

217 218
void
sflphone_hung_up (callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
219
{
Julien Bonjean's avatar
Julien Bonjean committed
220
    DEBUG ("SFLphone: Hung up");
221

222
    calllist_remove_call (current_calls, c->_callID);
223
    calltree_remove_call (current_calls, c, NULL);
224
    c->_state = CALL_STATE_DIALING;
225
    call_remove_all_errors (c);
226
    update_actions();
227

228 229 230 231 232
    if(c->_confID) {
	g_free(c->_confID);
	c->_confID = NULL;
    }

233 234 235 236 237
    // 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);
238

239
    status_tray_icon_blink (FALSE);
240

241
    statusbar_update_clock("");
242 243
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
244
/** Internal to actions: Fill account list */
245 246
void sflphone_fill_account_list (void)
{
247
    int count = current_account_get_message_number ();
248

Rafaël Carré's avatar
Cleanup  
Rafaël Carré committed
249 250
    account_list_free();
    account_list_init();
251

252
    gchar **array = dbus_account_list();
253
    if (array) {
254
        for (gchar **accountID = array; *accountID; accountID++) {
255 256
            account_t * a = g_new0 (account_t,1);
            a->accountID = g_strdup (*accountID);
257
            a->credential_information = NULL;
258
            account_list_add (a);
Emmanuel Milou's avatar
Emmanuel Milou committed
259
        }
260

Emmanuel Milou's avatar
Emmanuel Milou committed
261
        g_strfreev (array);
262
    }
263

264
    for (unsigned i = 0; i < account_list_get_size(); i++) {
Emmanuel Milou's avatar
Emmanuel Milou committed
265
        account_t  * a = account_list_get_nth (i);
266 267
        if(a == NULL) {
            ERROR("SFLphone: Error: Could not find account %d in list", i);
268
            break;
269
        }
270

271 272 273 274 275
        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;
        }
276

277
        a->properties = details;
278 279

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

282 283
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

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

308 309
        gchar * code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);
        if (code != NULL)
310 311 312 313
            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
314 315
    }

316 317
    // Set the current account message number
    current_account_set_message_number (count);
318

319
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
320 321
}

322
gboolean sflphone_init (GError **error)
323
{
324
    if (!dbus_connect (error) || !dbus_register (getpid (), "Gtk+ Client", error))
325
        return FALSE;
326

Rafaël Carré's avatar
Rafaël Carré committed
327
    abook_init();
328

329 330
    // Init icons factory
    init_icon_factory ();
331

332 333 334
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
335

336 337
    account_list_init ();
    codec_capabilities_load ();
338 339
    conferencelist_init (current_calls);
    conferencelist_init (history);
340

341 342
    // Fetch the configured accounts
    sflphone_fill_account_list ();
343

344 345
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
346

347 348
    // Fetch the conference list
    // sflphone_fill_conference_list();
349

350
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
351 352
}

353
void sflphone_fill_ip2ip_profile (void)
354 355 356 357
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

358
GHashTable *sflphone_get_ip2ip_properties(void)
359
{
360
    return ip2ip_profile;
361 362
}

363
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
364
sflphone_hang_up()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
365
{
366 367
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
368

Julien Bonjean's avatar
Julien Bonjean committed
369
    DEBUG ("SFLphone: Hang up");
370

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

391
                im_widget_update_state (IM_WIDGET (selectedCall->_im_widget), FALSE);
392

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

419
    calltree_update_call (history, selectedCall, NULL);
420

421
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
422 423
}

424
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
425 426
sflphone_pick_up()
{
427 428 429 430 431
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
432

433 434 435
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
436

437 438
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
            if (im_window_is_visible())
439 440
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
441

442 443 444 445
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
446

447
            // if instant messaging window is visible, create new tab (deleted automatically if not used)
448 449 450
            if (im_window_is_visible())
                if (!selectedCall->_im_widget)
                    selectedCall->_im_widget = im_widget_display(selectedCall->_callID);
451

452 453
            dbus_accept (selectedCall);
            break;
454 455
        case CALL_STATE_TRANSFER:
            dbus_transfer (selectedCall);
456 457 458 459 460 461 462 463 464 465 466 467 468
            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;
469
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
470 471
}

472
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
473 474
sflphone_on_hold ()
{
475 476
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
477

478 479
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
480 481 482 483 484
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
485
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
486 487
                break;
        }
488 489
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
490
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
491 492
}

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

500 501
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
502 503 504 505
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
506
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
507 508
                break;
        }
509 510
    } else if (selectedConf) {
        dbus_unhold_conference (selectedConf);
511
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
512 513 514
}


515 516
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
517
{
518
    c->_state = CALL_STATE_FAILURE;
519
    calltree_update_call (current_calls, c, NULL);
520
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
521 522
}

523 524
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
525
{
526
    c->_state = CALL_STATE_BUSY;
527
    calltree_update_call (current_calls, c, NULL);
528
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
529 530
}

531 532
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
533
{
534

535
    if (c->_state != CALL_STATE_HOLD)
536
        time (&c->_time_start);
537

538
    c->_state = CALL_STATE_CURRENT;
539
    calltree_update_call (current_calls, c, NULL);
540
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
541 542
}

543 544
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
545
{
546
    if (c->_state != CALL_STATE_HOLD)
547
        time (&c->_time_start);
548

549
    c->_state = CALL_STATE_RECORD;
550
    calltree_update_call (current_calls, c, NULL);
551
    update_actions();
alexandresavard's avatar
alexandresavard committed
552 553
}

554
void
555
sflphone_set_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
556
{
557 558 559
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
560
        c->_state = CALL_STATE_TRANSFER;
Tristan Matthews's avatar
Tristan Matthews committed
561
        g_free(c->_trsft_to);
562 563
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
564
    }
565

566
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
567 568
}

569
void
570
sflphone_unset_transfer()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
571
{
572 573 574
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
575
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
576
        g_free(c->_trsft_to);
577 578
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
579
    }
580

581
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
582
}
Emmanuel Milou's avatar
Emmanuel Milou committed
583

584 585
void
sflphone_display_transfer_status (const gchar* message)
586
{
Julien Bonjean's avatar
Julien Bonjean committed
587
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
588 589
}

590
void
591
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
592
{
593
    c->_history_state = MISSED;
594
    calllist_add_call (current_calls, c);
595
    calltree_add_call (current_calls, c, NULL);
596

597
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
598
    calltree_display (current_calls);
599 600 601

    // Change the status bar if we are dealing with a direct SIP call
    if (_is_direct_call (c)) {
602
        gchar *msg = g_markup_printf_escaped (_ ("Direct SIP call"));
603
        statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
Julien Bonjean's avatar
Julien Bonjean committed
604
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
605 606
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
607
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
608

609
static void
610
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
611
{
Emmanuel Milou's avatar
Emmanuel Milou committed
612
    // We stop the tone
613
    if (!*c->_peer_number && c->_state != CALL_STATE_TRANSFER)
614
        dbus_start_tone (FALSE, 0);
615

616
    switch (keyval) {
617 618
        case GDK_Return:
        case GDK_KP_Enter:
619
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
620
            break;
621
        case GDK_Escape:
Rafaël Carré's avatar
Rafaël Carré committed
622
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
623
            break;
624
        case GDK_BackSpace:
625 626 627 628 629 630
        {
            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';
631
                calltree_update_call (current_calls, c, NULL);
632

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

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

650
                if (c->_state == CALL_STATE_TRANSFER) {
Tristan Matthews's avatar
Tristan Matthews committed
651 652 653
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
654 655
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
656 657 658
                    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
659
                }
660 661

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
662
            }
663

Emmanuel Milou's avatar
Emmanuel Milou committed
664 665
            break;
    }
666 667
}

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

669
callable_obj_t *
areversat's avatar
areversat committed
670
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
671
{
Tristan Matthews's avatar
Tristan Matthews committed
672
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
673

674 675
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
676

Emmanuel Milou's avatar
Emmanuel Milou committed
677
    // Play a tone when creating a new call
678 679
    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
680

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

683 684
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
685
    calllist_add_call (current_calls, c);
686
    calltree_add_call (current_calls, c, NULL);
687
    update_actions();
688

Emmanuel Milou's avatar
Emmanuel Milou committed
689
    return c;
690 691
}

areversat's avatar
areversat committed
692

693 694
void
sflphone_keypad (guint keyval, gchar * key)
areversat's avatar
areversat committed
695
{
696 697 698 699
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if ( (active_calltree != current_calls) || (active_calltree == current_calls && !c)) {
        switch (keyval) {
700 701 702
            case GDK_Return:
            case GDK_KP_Enter:
            case GDK_Escape:
703
            case GDK_BackSpace: