actions.c 40 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
4
 *  Author: Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
Emmanuel Milou's avatar
Emmanuel Milou committed
5
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
6 7
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 3 of the License, or
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
9
 *  (at your option) any later version.
Emmanuel Milou's avatar
Emmanuel Milou committed
10
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
11 12 13 14
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
Emmanuel Milou's avatar
Emmanuel Milou committed
15
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
16 17 18
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 20 21 22 23 24 25 26 27 28 29
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
30
 */
31

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

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

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

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

64

65
static GHashTable * ip2ip_profile;
66

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

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

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

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

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

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

104
    return ordered_list;
105 106
}

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

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

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

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

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

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

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

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

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

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

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


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

161
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
162

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

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

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

Emmanuel Milou's avatar
Emmanuel Milou committed
179

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

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

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

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

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

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

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

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

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

238
    statusbar_update_clock("");
239 240
}

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

Emmanuel Milou's avatar
Emmanuel Milou committed
246
    account_list_clear ();
247

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

Emmanuel Milou's avatar
Emmanuel Milou committed
257
        g_strfreev (array);
258
    }
259

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

267 268 269 270 271
        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;
        }
272

273
        a->properties = details;
274 275

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

278 279
        gchar * status = g_hash_table_lookup (details, REGISTRATION_STATUS);

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

304 305
        gchar * code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);
        if (code != NULL)
306 307 308 309
            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
310 311
    }

312 313
    // Set the current account message number
    current_account_set_message_number (count);
314

315
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
316 317
}

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

Rafaël Carré's avatar
Rafaël Carré committed
323
    abook_init();
324

325 326
    // Init icons factory
    init_icon_factory ();
327

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

332 333
    account_list_init ();
    codec_capabilities_load ();
334 335
    conferencelist_init (current_calls);
    conferencelist_init (history);
336

337 338
    // Fetch the configured accounts
    sflphone_fill_account_list ();
339

340 341
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
342

343 344
    // Fetch the conference list
    // sflphone_fill_conference_list();
345

346
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
347 348
}

349
void sflphone_fill_ip2ip_profile (void)
350 351 352 353
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

354
GHashTable *sflphone_get_ip2ip_properties(void)
355
{
356
    return ip2ip_profile;
357 358
}

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

Julien Bonjean's avatar
Julien Bonjean committed
365
    DEBUG ("SFLphone: Hang up");
366

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

                //if ( (im_window_get_nb_tabs() > 1) && selectedCall->_im_widget &&
                //        ! (IM_WIDGET (selectedCall->_im_widget)->containText))
                //    im_window_remove_tab (selectedCall->_im_widget);
                //else
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 439
            // 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);
440

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

446 447 448 449
            // 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);
            }
450

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

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

477 478
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
479 480 481 482 483
            case CALL_STATE_CURRENT:
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
484
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
485 486
                break;
        }
487 488
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
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_off_hold ()
{
495 496 497
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
498

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

615
    switch (keyval) {
616 617
        case GDK_Return:
        case GDK_KP_Enter:
618
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
619
            break;
620
        case GDK_Escape:
Rafaël Carré's avatar
Rafaël Carré committed
621
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
622
            break;
623
        case GDK_BackSpace:
624 625 626 627 628 629 630
        {
            gchar *num = (c->_state == CALL_STATE_TRANSFER) ? c->_trsft_to : c->_peer_number;
            size_t len = strlen(num);
            printf("\"%s\" : %zu\n", num, len);
            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:
Emmanuel Milou's avatar
Emmanuel Milou committed
704 705
                break;
            default:
Julien Bonjean's avatar
Julien Bonjean committed
706
                calltree_display (current_calls);
707
                process_dialing (sflphone_new_call(), keyval, key);