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

32 33 34
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <stdlib.h>
35
#include <string.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
36 37
#include <sys/types.h>
#include <unistd.h>
38
#include <assert.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
39

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

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

49 50 51 52 53 54 55 56 57
#include "actions.h"
#include "dbus/dbus.h"
#include "contacts/searchbar.h"
#include "contacts/addrbookfactory.h"
#include "icons/icon_factory.h"
#include "imwindow.h"
#include "statusicon.h"
#include "widget/imwidget.h"

58

59
static GHashTable * ip2ip_profile=NULL;
60

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

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

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

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

Tristan Matthews's avatar
Tristan Matthews committed
83 84 85 86 87 88 89 90 91
        if (g_hash_table_lookup_extended(result, key_to_min, &key, &value)) {
            GSList *llist = (GSList *)value;
            while (llist) {
                ordered_list = (void *) g_realloc(ordered_list, (size + 1) * sizeof (void *));
                *(ordered_list + size) = g_strdup((gchar *)llist->data);
                size++;
                llist = g_slist_next(llist);
            }
            g_hash_table_remove(result, key_to_min);
92 93 94
        }
    }

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

98
    return ordered_list;
99 100
}

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

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

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

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

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

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

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

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

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

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

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

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


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

160
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
161

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

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

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

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

    DEBUG ("status_bar_display_account_end");
181 182
}

Emmanuel Milou's avatar
Emmanuel Milou committed
183

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

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

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

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

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

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

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

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

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

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

244 245 246 247 248
    // test wether the widget contain text, if not remove it
    if ( (im_window_get_nb_tabs() > 1) && c->_im_widget && ! (IM_WIDGET (c->_im_widget)->containText))
        im_window_remove_tab (c->_im_widget);
    else
        im_widget_update_state (IM_WIDGET (c->_im_widget), FALSE);
249

250
#if GTK_CHECK_VERSION(2,10,0)
251
    status_tray_icon_blink (FALSE);
252
#endif
253

254
    stop_call_clock(c);
255
    calltree_update_clock();
256 257
}

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

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

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

269
    count = current_account_get_message_number ();
270

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

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

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

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

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

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

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

300
        a->properties = details;
301 302

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

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

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

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

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

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

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

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

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

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

    abookfactory_init_factory();
359

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

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

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

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

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

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

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

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

389
GHashTable *sflphone_get_ip2ip_properties(void)
390
{
391
    return ip2ip_profile;
392 393
}

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

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

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

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

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

455
    calltree_update_call (history, selectedCall, NULL);
456 457

    calltree_update_clock();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
458 459
}

460
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
461 462
sflphone_pick_up()
{
463
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
464

465 466
    DEBUG("SFLphone: Pick up");

467 468 469 470 471 472 473
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
474

475 476 477
            // 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);
478

479 480 481 482
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
483

484 485 486 487
            // 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);
            }
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
            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;
507
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
508 509
}

510
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
511 512
sflphone_on_hold ()
{
513 514
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
515

516 517 518 519
    DEBUG ("sflphone_on_hold");

    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
520 521 522 523 524 525 526
            case CALL_STATE_CURRENT:
                dbus_hold (selectedCall);
                break;
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
527
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
528 529
                break;
        }
530 531
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
532
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
533 534
}

535
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
536 537
sflphone_off_hold ()
{
538 539 540
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
541

542 543
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
544 545 546 547
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
548
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
549 550
                break;
        }
551 552
    } else if (selectedConf) {

Julien Bonjean's avatar
Julien Bonjean committed
553

554
        dbus_unhold_conference (selectedConf);
555
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
556 557 558
}


559 560
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
561
{
562
    c->_state = CALL_STATE_FAILURE;
563
    calltree_update_call (current_calls, c, NULL);
564
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
565 566
}

567 568
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
569
{
570
    c->_state = CALL_STATE_BUSY;
571
    calltree_update_call (current_calls, c, NULL);
572
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
573 574
}

575 576
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
577
{
578

579
    if (c->_state != CALL_STATE_HOLD)
580
        time (&c->_time_start);
581

582
    c->_state = CALL_STATE_CURRENT;
583
    calltree_update_call (current_calls, c, NULL);
584
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
585 586
}

587 588
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
589
{
590
    if (c->_state != CALL_STATE_HOLD)
591
        time (&c->_time_start);
592

593
    c->_state = CALL_STATE_RECORD;
594
    calltree_update_call (current_calls, c, NULL);
595
    update_actions();
alexandresavard's avatar
alexandresavard committed
596 597
}

598
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
599
sflphone_set_transfert()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
600
{
601 602 603
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
604
        c->_state = CALL_STATE_TRANSFERT;
Tristan Matthews's avatar
Tristan Matthews committed
605
        g_free(c->_trsft_to);
606 607
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
608
    }
609

610
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
611 612
}

613
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
614 615
sflphone_unset_transfert()
{
616 617 618
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
619
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
620
        g_free(c->_trsft_to);
621 622
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
623
    }
624

625
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
626
}
Emmanuel Milou's avatar
Emmanuel Milou committed
627

628 629
void
sflphone_display_transfer_status (const gchar* message)
630
{
Julien Bonjean's avatar
Julien Bonjean committed
631
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
632 633
}

634
void
635
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
636
{
637
    gchar *msg = "";
638

639
    c->_history_state = MISSED;
640 641
    calllist_add_call (current_calls, c);
    calllist_add_call (history, c);
642
    calltree_add_call (current_calls, c, NULL);
643
    calltree_add_call (history, c, NULL);
644
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
645
    calltree_display (current_calls);
646 647 648 649 650

    // 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
651
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
652 653
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
654
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
655

656
void
657
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
658
{
Emmanuel Milou's avatar
Emmanuel Milou committed
659
    // We stop the tone
660 661
    if (strlen (c->_peer_number) == 0 && c->_state != CALL_STATE_TRANSFERT) {
        dbus_start_tone (FALSE , 0);
Emmanuel Milou's avatar
Emmanuel Milou committed
662 663
        //dbus_play_dtmf( key );
    }
664

665
    switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
666 667
        case 65293: /* ENTER */
        case 65421: /* ENTER numpad */
668
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
669 670
            break;
        case 65307: /* ESCAPE */
Rafaël Carré's avatar
Rafaël Carré committed
671
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
672
            break;
Alexandre Savard's avatar
Alexandre Savard committed
673 674
        case 65288: { /* BACKSPACE */
            /* Brackets mandatory because of local vars */
675 676 677 678 679 680 681 682 683 684 685
            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);
686
                    DEBUG ("SFLphone: TO: backspace %s", c->_peer_number);
Emmanuel Milou's avatar
Emmanuel Milou committed
687
                }
688 689 690 691 692

                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
693
            }
694 695
        }
        break;
Emmanuel Milou's avatar
Emmanuel Milou committed
696 697 698 699 700 701 702
        case 65289: /* TAB */
        case 65513: /* ALT */
        case 65507: /* CTRL */
        case 65515: /* SUPER */
        case 65509: /* CAPS */
            break;
        default:
703

Emmanuel Milou's avatar
Emmanuel Milou committed
704
            // if (keyval < 255 || (keyval >65453 && keyval < 65466))
705
            if (keyval < 127 || (keyval > 65400 && keyval < 65466)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
706

707
                if (c->_state == CALL_STATE_TRANSFERT) {
Tristan Matthews's avatar
Tristan Matthews committed
708 709 710
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
711 712
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
713 714 715
                    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
716
                }
717 718

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
719
            }
720

Emmanuel Milou's avatar
Emmanuel Milou committed
721 722
            break;
    }
723 724
}

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

726
callable_obj_t *
areversat's avatar
areversat committed
727
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
728
{
Julien Bonjean's avatar
Julien Bonjean committed
729
    DEBUG ("Actions: Sflphone new call");
730

Tristan Matthews's avatar
Tristan Matthews committed
731
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
732

733 734
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
735

Emmanuel Milou's avatar
Emmanuel Milou committed
736
    // Play a tone when creating a new call
737 738
    if (calllist_get_size (current_calls) == 0)
        dbus_start_tone (TRUE , (current_account_has_new_message ()  > 0) ? TONE_WITH_MESSAGE : TONE_WITHOUT_MESSAGE) ;