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

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

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

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

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

58

59
static GHashTable * ip2ip_profile=NULL;
60

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

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

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

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

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

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

98
    return ordered_list;
99
100
}

101
void
102
sflphone_notify_voice_mail (const gchar* accountID , guint count)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
103
{
104
    // We want to notify only the current account; ie the first in the list
105
106
    gchar *id = g_strdup (accountID);
    const gchar * const current_id = account_list_get_current_id ();
107

Julien Bonjean's avatar
Julien Bonjean committed
108
    DEBUG ("sflphone_notify_voice_mail begin");
109

110
    if (g_ascii_strcasecmp (id, current_id) != 0 || account_list_get_size() == 0)
111
112
        return;

113
114
    // Set the number of voice messages for the current account
    current_account_set_message_number (count);
115
    account_t *current = account_list_get_current ();
116

117
118
    // Update the voicemail tool button
    update_voicemail_status ();
Emmanuel Milou's avatar
Emmanuel Milou committed
119

120
121
    if (current)
        notify_voice_mails (count, current);
Emmanuel Milou's avatar
Emmanuel Milou committed
122

Julien Bonjean's avatar
Julien Bonjean committed
123
    DEBUG ("sflphone_notify_voice_mail end");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
124
125
}

126
127
128
129
130
131
132
/*
 * 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
 */

133
134
135
136
137
138
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);
139
140
            c->_peer_number = new_number;
        }
141

142
        return TRUE;
Alexandre Savard's avatar
Alexandre Savard committed
143
144
    }

145
146
    return g_str_has_prefix (c->_peer_number, "sip:") ||
           g_str_has_prefix (c->_peer_number, "sips:");
147
148
149
}


150
void
151
status_bar_display_account ()
152
{
153
    gchar* msg;
154

155
    statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
156

157
158
    account_t *acc = account_list_get_current ();
    status_tray_icon_online (acc != NULL);
159
160
161
162
163
164
165
166

    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"));
167
    }
168

Julien Bonjean's avatar
Julien Bonjean committed
169
    statusbar_push_message (msg, NULL,  __MSG_ACCOUNT_DEFAULT);
170
    g_free (msg);
171
172
}

Emmanuel Milou's avatar
Emmanuel Milou committed
173

174
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
175
176
sflphone_quit ()
{
177
    if (calllist_get_size(current_calls) == 0 || main_window_ask_quit()) {
178
        // Save the history
179
180
        sflphone_save_history ();

181
        dbus_unregister (getpid());
Emmanuel Milou's avatar
Emmanuel Milou committed
182
        dbus_clean ();
Vivien Didelot's avatar
Vivien Didelot committed
183
184
185
        calllist_clean (current_calls);
        calllist_clean (contacts);
        calllist_clean (history);
Emmanuel Milou's avatar
Emmanuel Milou committed
186
187
        gtk_main_quit ();
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
188
189
}

190
191
void
sflphone_hold (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
192
{
193
    c->_state = CALL_STATE_HOLD;
194
    calltree_update_call (current_calls, c, NULL);
195
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
196
197
}

198
199
void
sflphone_ringing (callable_obj_t * c)
200
{
201
    c->_state = CALL_STATE_RINGING;
202
    calltree_update_call (current_calls, c, NULL);
203
    update_actions();
204
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
205

206
207
void
sflphone_hung_up (callable_obj_t * c)
Emmanuel Milou's avatar
Emmanuel Milou committed
208
{
Julien Bonjean's avatar
Julien Bonjean committed
209
    DEBUG ("SFLphone: Hung up");
210

211
    calllist_remove_call (current_calls, c->_callID);
212
    calltree_remove_call (current_calls, c, NULL);
213
    c->_state = CALL_STATE_DIALING;
214
    call_remove_all_errors (c);
215
    update_actions();
216

217
218
219
220
221
    if(c->_confID) {
	g_free(c->_confID);
	c->_confID = NULL;
    }

222
223
224
225
226
    // 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);
227

228
#if GTK_CHECK_VERSION(2,10,0)
229
    status_tray_icon_blink (FALSE);
230
#endif
231

232
    statusbar_update_clock("");
233
234
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
235
/** Internal to actions: Fill account list */
236
237
void sflphone_fill_account_list (void)
{
238
239
240
241

    gchar** array;
    gchar** accountID;
    unsigned int i;
242
    int count;
243

244
    DEBUG ("SFLphone: Fill account list");
245

246
    count = current_account_get_message_number ();
247

Emmanuel Milou's avatar
Emmanuel Milou committed
248
    account_list_clear ();
249

250
251
252
    array = (gchar **) dbus_account_list();

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

254
255
256
        for (accountID = array; *accountID; accountID++) {
            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 (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
        gchar * code = NULL;
309
310
        code = g_hash_table_lookup (details, REGISTRATION_STATE_CODE);

311
        if (code != NULL) {
312
            a->protocol_state_code = atoi (code);
313
        }
314
315
316

        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
317
318
    }

319
320
    // Set the current account message number
    current_account_set_message_number (count);
321

322
    sflphone_fill_codec_list ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
323
324
}

325
gboolean sflphone_init (GError **error)
326
{
327
    if (!dbus_connect (error) || !dbus_register (getpid (), "Gtk+ Client", error))
328
        return FALSE;
329
330

    abookfactory_init_factory();
331

332
333
    // Init icons factory
    init_icon_factory ();
334

335
336
337
    current_calls = calltab_init (FALSE, CURRENT_CALLS);
    contacts = calltab_init (TRUE, CONTACTS);
    history = calltab_init (TRUE, HISTORY);
338

339
340
    account_list_init ();
    codec_capabilities_load ();
341
342
    conferencelist_init (current_calls);
    conferencelist_init (history);
343

344
345
    // Fetch the configured accounts
    sflphone_fill_account_list ();
346

347
348
    // Fetch the ip2ip profile
    sflphone_fill_ip2ip_profile();
349

350
351
    // Fetch the conference list
    // sflphone_fill_conference_list();
352

353
    return TRUE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
354
355
}

356
void sflphone_fill_ip2ip_profile (void)
357
358
359
360
{
    ip2ip_profile = (GHashTable *) dbus_get_ip2_ip_details();
}

361
GHashTable *sflphone_get_ip2ip_properties(void)
362
{
363
    return ip2ip_profile;
364
365
}

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

Julien Bonjean's avatar
Julien Bonjean committed
372
    DEBUG ("SFLphone: Hang up");
373

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

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

Emmanuel Milou's avatar
Emmanuel Milou committed
400
401
402
                break;
            case CALL_STATE_FAILURE:
                dbus_hang_up (selectedCall);
403
                call_remove_all_errors (selectedCall);
404
                selectedCall->_state = CALL_STATE_DIALING;
Emmanuel Milou's avatar
Emmanuel Milou committed
405
406
407
                break;
            case CALL_STATE_INCOMING:
                dbus_refuse (selectedCall);
408
                call_remove_all_errors (selectedCall);
409
                selectedCall->_state = CALL_STATE_DIALING;
410
411
                DEBUG ("from sflphone_hang_up : ");
                stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
412
413
414
                break;
            case CALL_STATE_TRANSFERT:
                dbus_hang_up (selectedCall);
415
                call_remove_all_errors (selectedCall);
416
                time (&selectedCall->_time_stop);
Emmanuel Milou's avatar
Emmanuel Milou committed
417
418
                break;
            default:
419
                WARN ("Should not happen in sflphone_hang_up()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
420
421
                break;
        }
422
    } else if (selectedConf) {
423
        im_widget_update_state (IM_WIDGET (selectedConf->_im_widget), FALSE);
424
        dbus_hang_up_conference (selectedConf);
425
426
    }

427
    calltree_update_call (history, selectedCall, NULL);
428

429
    statusbar_update_clock("");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
430
431
}

432
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
433
434
sflphone_pick_up()
{
435
    callable_obj_t *selectedCall = calltab_get_selected_call (active_calltree);
436

437
438
    DEBUG("SFLphone: Pick up");

439
440
441
442
443
444
445
    if (!selectedCall) {
        sflphone_new_call();
        return;
    }
    switch (selectedCall->_state) {
        case CALL_STATE_DIALING:
            sflphone_place_call (selectedCall);
446

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

451
452
453
454
            break;
        case CALL_STATE_INCOMING:
            selectedCall->_history_state = INCOMING;
            calltree_update_call (history, selectedCall, NULL);
455

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

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
            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;
479
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
480
481
}

482
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
483
484
sflphone_on_hold ()
{
485
486
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
487

488
489
490
491
    DEBUG ("sflphone_on_hold");

    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
492
493
494
495
496
497
498
            case CALL_STATE_CURRENT:
                dbus_hold (selectedCall);
                break;
            case CALL_STATE_RECORD:
                dbus_hold (selectedCall);
                break;
            default:
499
                WARN ("Should not happen in sflphone_on_hold!");
Emmanuel Milou's avatar
Emmanuel Milou committed
500
501
                break;
        }
502
503
    } else if (selectedConf) {
        dbus_hold_conference (selectedConf);
504
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
505
506
}

507
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
508
509
sflphone_off_hold ()
{
510
511
512
    DEBUG ("sflphone_off_hold");
    callable_obj_t * selectedCall = calltab_get_selected_call (current_calls);
    conference_obj_t * selectedConf = calltab_get_selected_conf (active_calltree);
513

514
515
    if (selectedCall) {
        switch (selectedCall->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
516
517
518
519
            case CALL_STATE_HOLD:
                dbus_unhold (selectedCall);
                break;
            default:
520
                WARN ("Should not happen in sflphone_off_hold ()!");
Emmanuel Milou's avatar
Emmanuel Milou committed
521
522
                break;
        }
523
524
    } else if (selectedConf) {

Julien Bonjean's avatar
Julien Bonjean committed
525

526
        dbus_unhold_conference (selectedConf);
527
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
528
529
530
}


531
532
void
sflphone_fail (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
533
{
534
    c->_state = CALL_STATE_FAILURE;
535
    calltree_update_call (current_calls, c, NULL);
536
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
537
538
}

539
540
void
sflphone_busy (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
541
{
542
    c->_state = CALL_STATE_BUSY;
543
    calltree_update_call (current_calls, c, NULL);
544
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
545
546
}

547
548
void
sflphone_current (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
549
{
550

551
    if (c->_state != CALL_STATE_HOLD)
552
        time (&c->_time_start);
553

554
    c->_state = CALL_STATE_CURRENT;
555
    calltree_update_call (current_calls, c, NULL);
556
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
557
558
}

559
560
void
sflphone_record (callable_obj_t * c)
alexandresavard's avatar
alexandresavard committed
561
{
562
    if (c->_state != CALL_STATE_HOLD)
563
        time (&c->_time_start);
564

565
    c->_state = CALL_STATE_RECORD;
566
    calltree_update_call (current_calls, c, NULL);
567
    update_actions();
alexandresavard's avatar
alexandresavard committed
568
569
}

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

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

582
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
583
584
}

585
void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
586
587
sflphone_unset_transfert()
{
588
589
590
    callable_obj_t * c = calltab_get_selected_call (current_calls);

    if (c) {
591
        c->_state = CALL_STATE_CURRENT;
Tristan Matthews's avatar
Tristan Matthews committed
592
        g_free(c->_trsft_to);
593
594
        c->_trsft_to = g_strdup ("");
        calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
595
    }
596

597
    update_actions();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
598
}
Emmanuel Milou's avatar
Emmanuel Milou committed
599

600
601
void
sflphone_display_transfer_status (const gchar* message)
602
{
Julien Bonjean's avatar
Julien Bonjean committed
603
    statusbar_push_message (message , NULL, __MSG_ACCOUNT_DEFAULT);
604
605
}

606
void
607
sflphone_incoming_call (callable_obj_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
608
{
609
    c->_history_state = MISSED;
610
    calllist_add_call (current_calls, c);
611
    calltree_add_call (current_calls, c, NULL);
612

613
    update_actions();
Julien Bonjean's avatar
Julien Bonjean committed
614
    calltree_display (current_calls);
615
616
617

    // Change the status bar if we are dealing with a direct SIP call
    if (_is_direct_call (c)) {
618
        gchar *msg = g_markup_printf_escaped (_ ("Direct SIP call"));
619
        statusbar_pop_message (__MSG_ACCOUNT_DEFAULT);
Julien Bonjean's avatar
Julien Bonjean committed
620
        statusbar_push_message (msg , NULL, __MSG_ACCOUNT_DEFAULT);
621
622
        g_free (msg);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
623
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
624

625
void
626
process_dialing (callable_obj_t *c, guint keyval, gchar *key)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
627
{
Emmanuel Milou's avatar
Emmanuel Milou committed
628
    // We stop the tone
629
630
    if (strlen (c->_peer_number) == 0 && c->_state != CALL_STATE_TRANSFERT) {
        dbus_start_tone (FALSE , 0);
Emmanuel Milou's avatar
Emmanuel Milou committed
631
632
        //dbus_play_dtmf( key );
    }
633

634
    switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
635
636
        case 65293: /* ENTER */
        case 65421: /* ENTER numpad */
637
            sflphone_place_call (c);
Emmanuel Milou's avatar
Emmanuel Milou committed
638
639
            break;
        case 65307: /* ESCAPE */
Rafaël Carré's avatar
Rafaël Carré committed
640
            sflphone_hang_up ();
Emmanuel Milou's avatar
Emmanuel Milou committed
641
            break;
Alexandre Savard's avatar
Alexandre Savard committed
642
643
        case 65288: { /* BACKSPACE */
            /* Brackets mandatory because of local vars */
644
645
646
647
648
649
650
651
652
653
654
            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);
655
                    DEBUG ("SFLphone: TO: backspace %s", c->_peer_number);
Emmanuel Milou's avatar
Emmanuel Milou committed
656
                }
657
658
659
660
661

                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
662
            }
663
664
        }
        break;
Emmanuel Milou's avatar
Emmanuel Milou committed
665
666
667
668
669
670
671
        case 65289: /* TAB */
        case 65513: /* ALT */
        case 65507: /* CTRL */
        case 65515: /* SUPER */
        case 65509: /* CAPS */
            break;
        default:
672

Emmanuel Milou's avatar
Emmanuel Milou committed
673
            // if (keyval < 255 || (keyval >65453 && keyval < 65466))
674
            if (keyval < 127 || (keyval > 65400 && keyval < 65466)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
675

676
                if (c->_state == CALL_STATE_TRANSFERT) {
Tristan Matthews's avatar
Tristan Matthews committed
677
678
679
                    gchar *new_trsft = g_strconcat (c->_trsft_to, key, NULL);
                    g_free (c->_trsft_to);
                    c->_trsft_to = new_trsft;
680
681
                } else {
                    dbus_play_dtmf (key);
Tristan Matthews's avatar
Tristan Matthews committed
682
683
684
                    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
685
                }
686
687

                calltree_update_call (current_calls, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
688
            }
689

Emmanuel Milou's avatar
Emmanuel Milou committed
690
691
            break;
    }
692
693
}

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

695
callable_obj_t *
areversat's avatar
areversat committed
696
sflphone_new_call()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
697
{
Julien Bonjean's avatar
Julien Bonjean committed
698
    DEBUG ("Actions: Sflphone new call");
699

Tristan Matthews's avatar
Tristan Matthews committed
700
    callable_obj_t *current_selected_call = calltab_get_selected_call (current_calls);
701

702
703
    if ( (current_selected_call != NULL) && (current_selected_call->_confID == NULL))
        sflphone_on_hold();
704

Emmanuel Milou's avatar
Emmanuel Milou committed
705
    // Play a tone when creating a new call
706
707
    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
708

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

711
712
    c->_history_state = OUTGOING;

Alexandre Savard's avatar
Alexandre Savard committed
713
    calllist_add_call (current_calls, c);
714
    calltree_add_call (current_calls, c, NULL);
715
    update_actions();
716

Emmanuel Milou's avatar
Emmanuel Milou committed
717
    return c;
718
719
}

areversat's avatar
areversat committed
720

721
722
void
sflphone_keypad (guint keyval, gchar * key)
areversat's avatar
areversat committed
723
{
724
725
    callable_obj_t * c = calltab_get_selected_call (current_calls);

726
727
    DEBUG("SFLphone: Keypad");

728
729
    if ( (active_calltree != current_calls) || (active_calltree == current_calls && !c)) {
        DEBUG ("Not in a call, not dialing, create a new call");
Emmanuel Milou's avatar
Emmanuel Milou committed
730
731

        //dbus_play_dtmf(key);
732
        switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
733
734
735
736
737
            case 65293: /* ENTER */
            case 65421: /* ENTER numpad */
            case 65307: /* ESCAPE */
                break;
            default:
Julien Bonjean's avatar
Julien Bonjean committed
738
                calltree_display (current_calls);
739
                process_dialing (sflphone_new_call(), keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
740
741
                break;
        }
742
743
744
745
    } else if (c) {
        DEBUG ("Call is non-zero");

        switch (c->_state) {
Emmanuel Milou's avatar
Emmanuel Milou committed
746
            case CALL_STATE_DIALING: // Currently dialing => edit number
747
748
                DEBUG ("Writing a number");
                process_dialing (c, keyval, key);
Emmanuel Milou's avatar
Emmanuel Milou committed
749
750
751
                break;
            case CALL_STATE_RECORD:
            case CALL_STATE_CURRENT:
752
753

                switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
754
                    case 65307: /* ESCAPE */
755
                        dbus_hang_up (c);
756
                        time (&c->_time_stop);
757
                        calltree_update_call (history, c, NULL);
Emmanuel Milou's avatar
Emmanuel Milou committed
758
759
760
                        break;
                    default:
                        // To play the dtmf when calling mail box for instance
761
                        dbus_play_dtmf (key);
Emmanuel Milou's avatar
Emmanuel Milou committed
762
763
                        break;
                }
764

Emmanuel Milou's avatar
Emmanuel Milou committed
765
766
                break;
            case CALL_STATE_INCOMING:
767
768

                switch (keyval) {
Emmanuel Milou's avatar
Emmanuel Milou committed
769
770
                    case 65293: /* ENTER */
                    case 65421: /* ENTER numpad */
771
                        c->_history_state = INCOMING;
772
773
774
775
                        calltree_update_call (history, c, NULL);
                        dbus_accept (c);
                        DEBUG ("from sflphone_keypad ( enter ) : ");
                        stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
776
777
                        break;
                    case 65307: /* ESCAPE */
778
779
780
                        dbus_refuse (c);
                        DEBUG ("from sflphone_keypad ( escape ) : ");
                        stop_notification();
Emmanuel Milou's avatar
Emmanuel Milou committed
781
782
                        break;
                }
783

Emmanuel Milou's avatar
Emmanuel Milou committed
784
785
                break;
            case CALL_STATE_TRANSFERT:
786
787

                switch (keyval) {