~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-client-gnome/src/callable_obj.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <codeclist.h>
33
33
#include <sflphone_const.h>
34
34
#include <time.h>
 
35
#include "contacts/calltree.h"
 
36
#include  <unistd.h>
 
37
 
35
38
 
36
39
#define UNIX_DAY                        86400
37
40
#define UNIX_WEEK                       86400 * 6
38
 
#define UNIX_TWO_DAYS           86400 * 2
 
41
#define UNIX_TWO_DAYS                   86400 * 2
39
42
 
40
 
gint is_callID_callstruct ( gconstpointer a, gconstpointer b)
 
43
gint is_callID_callstruct (gconstpointer a, gconstpointer b)
41
44
{
42
 
    callable_obj_t * c = (callable_obj_t*)a;
43
 
    if(g_strcasecmp(c->_callID, (const gchar*) b) == 0)
44
 
    {
 
45
    callable_obj_t * c = (callable_obj_t*) a;
 
46
 
 
47
    if (g_strcasecmp (c->_callID, (const gchar*) b) == 0) {
45
48
        return 0;
46
 
    }
47
 
    else
48
 
    {
 
49
    } else {
49
50
        return 1;
50
51
    }
51
52
}
52
53
 
53
 
gint get_state_callstruct ( gconstpointer a, gconstpointer b)
 
54
gint get_state_callstruct (gconstpointer a, gconstpointer b)
54
55
{
55
 
    callable_obj_t * c = (callable_obj_t*)a;
56
 
    if( c->_state == *((call_state_t*)b))
57
 
    {
 
56
    callable_obj_t * c = (callable_obj_t*) a;
 
57
 
 
58
    if (c->_state == * ( (call_state_t*) b)) {
58
59
        return 0;
59
 
    }
60
 
    else
61
 
    {
 
60
    } else {
62
61
        return 1;
63
62
    }
64
63
}
67
66
{
68
67
    const gchar *end, *name;
69
68
 
70
 
    DEBUG("    callable_obj: %s", format);
 
69
    DEBUG ("    callable_obj: %s", format);
71
70
 
72
71
    end = g_strrstr (format, "<");
73
72
 
75
74
        return g_strndup (format, 0);
76
75
    } else {
77
76
        name = format;
78
 
        return g_strndup(name, end - name);
 
77
        return g_strndup (name, end - name);
79
78
    }
80
79
}
81
80
 
82
81
gchar* call_get_peer_number (const gchar *format)
83
82
{
84
 
    DEBUG("    callable_obj: %s", format);
85
 
 
86
 
    gchar * number = g_strrstr(format, "<") + 1;
87
 
    gchar * end = g_strrstr(format, ">");
88
 
 
89
 
    if(end && number)
90
 
        number = g_strndup(number, end - number  );
 
83
    DEBUG ("    callable_obj: %s", format);
 
84
 
 
85
    gchar * number = g_strrstr (format, "<") + 1;
 
86
    gchar * end = g_strrstr (format, ">");
 
87
 
 
88
    if (end && number)
 
89
        number = g_strndup (number, end - number);
91
90
    else
92
 
        number = g_strdup(format);
93
 
    
 
91
        number = g_strdup (format);
 
92
 
94
93
    return number;
95
94
}
96
95
 
97
96
gchar* call_get_audio_codec (callable_obj_t *obj)
98
97
{
99
 
        gchar *audio_codec = "";
100
 
        codec_t *codec;
101
 
        gchar *format ="";
102
 
        int samplerate;
103
 
        
104
 
        if (obj)
105
 
        {
106
 
                audio_codec = dbus_get_current_codec_name (obj);        
107
 
                codec = codec_list_get_by_name (audio_codec, NULL);
108
 
                if (codec){
109
 
                        samplerate = codec->sample_rate;
110
 
                        format = g_markup_printf_escaped ("%s/%i", audio_codec, samplerate);
111
 
                }
112
 
        }
113
 
        return format;
 
98
    gchar *audio_codec = "";
 
99
    codec_t *codec;
 
100
    gchar *format ="";
 
101
    int samplerate;
 
102
 
 
103
    if (obj) {
 
104
        audio_codec = dbus_get_current_codec_name (obj);
 
105
        codec = codec_list_get_by_name (audio_codec, NULL);
 
106
 
 
107
        if (codec) {
 
108
            samplerate = codec->sample_rate;
 
109
            format = g_markup_printf_escaped ("%s/%i", audio_codec, samplerate);
 
110
        }
 
111
    }
 
112
 
 
113
    return format;
114
114
}
115
115
 
116
 
void call_add_error(callable_obj_t * call, gpointer dialog)
 
116
void call_add_error (callable_obj_t * call, gpointer dialog)
117
117
{
118
118
    g_ptr_array_add (call->_error_dialogs, dialog);
119
119
}
120
120
 
121
 
void call_remove_error(callable_obj_t * call, gpointer dialog)
 
121
void call_remove_error (callable_obj_t * call, gpointer dialog)
122
122
{
123
123
    g_ptr_array_remove (call->_error_dialogs, dialog);
124
124
}
125
125
 
126
 
void call_remove_all_errors(callable_obj_t * call)
 
126
void call_remove_all_errors (callable_obj_t * call)
127
127
{
128
128
    g_ptr_array_foreach (call->_error_dialogs, (GFunc) gtk_widget_destroy, NULL);
129
129
}
130
130
 
 
131
void threaded_clock_incrementer (void *pc)
 
132
{
 
133
 
 
134
    callable_obj_t *call = (callable_obj_t *) pc;
 
135
 
 
136
 
 
137
    while (call->clockStarted) {
 
138
 
 
139
        int duration;
 
140
        time_t start, current;
 
141
 
 
142
        gdk_threads_enter ();
 
143
 
 
144
        set_timestamp (& (call->_time_current));
 
145
 
 
146
        start = call->_time_start;
 
147
        current = call->_time_current;
 
148
 
 
149
        if (current == start) {
 
150
            g_snprintf (call->_timestr, 20, "00:00");
 
151
 
 
152
        }
 
153
 
 
154
        duration = (int) difftime (current, start);
 
155
 
 
156
        if (duration / 60 == 0) {
 
157
            if (duration < 10) {
 
158
                g_snprintf (call->_timestr, 20, "00:0%d", duration);
 
159
            } else {
 
160
                g_snprintf (call->_timestr, 20, "00:%d", duration);
 
161
            }
 
162
        } else {
 
163
            if (duration%60 < 10) {
 
164
                g_snprintf (call->_timestr, 20, "0%d:0%d", duration/60, duration%60);
 
165
            } else {
 
166
                g_snprintf (call->_timestr, 20, "%d:%d", duration/60, duration%60);
 
167
            }
 
168
        }
 
169
 
 
170
        // Update clock only if call is active (current, hold, recording transfer)
 
171
        if ( (call->_state != CALL_STATE_INVALID) &&
 
172
                (call->_state != CALL_STATE_INCOMING) &&
 
173
                (call->_state != CALL_STATE_RINGING) &&
 
174
                (call->_state != CALL_STATE_DIALING) &&
 
175
                (call->_state != CALL_STATE_FAILURE) &&
 
176
                (call->_state != CALL_STATE_BUSY)) {
 
177
            calltree_update_clock();
 
178
        }
 
179
 
 
180
        // gdk_flush();
 
181
        gdk_threads_leave ();
 
182
 
 
183
 
 
184
        usleep (1000000);
 
185
 
 
186
    }
 
187
 
 
188
    DEBUG ("CallableObj: Stopping Thread");
 
189
 
 
190
    g_thread_exit (NULL);
 
191
 
 
192
}
 
193
 
 
194
void stop_call_clock (callable_obj_t *c)
 
195
{
 
196
 
 
197
    DEBUG ("CallableObj: Stop call clock");
 
198
 
 
199
    if (!c) {
 
200
        ERROR ("CallableObj: Callable object is NULL");
 
201
        return;
 
202
    }
 
203
 
 
204
    if (c->_type == CALL && c->clockStarted) {
 
205
        c->clockStarted = 0;
 
206
        /// no need to join here, only need to call g_thread_exit at the end of the threaded function
 
207
        // g_thread_join (c->tid);
 
208
    }
 
209
}
 
210
 
131
211
void create_new_call (callable_type_t type, call_state_t state, gchar* callID , gchar* accountID, gchar* peer_name, gchar* peer_number, callable_obj_t ** new_call)
132
212
{
133
213
 
 
214
    GError *err1 = NULL ;
134
215
    callable_obj_t *obj;
135
216
    gchar *call_id;
136
217
 
 
218
    DEBUG ("CallableObj: Create new call");
 
219
 
 
220
    DEBUG ("Account: %s", accountID);
 
221
 
137
222
    // Allocate memory
138
223
    obj = g_new0 (callable_obj_t, 1);
139
224
 
150
235
    obj->_peer_info = g_strdup (get_peer_info (peer_name, peer_number));
151
236
 
152
237
    obj->_trsft_to = "";
153
 
    set_timestamp (&(obj->_time_start));
154
 
    set_timestamp (&(obj->_time_stop));
 
238
    set_timestamp (& (obj->_time_start));
 
239
    set_timestamp (& (obj->_time_current));
 
240
    set_timestamp (& (obj->_time_stop));
 
241
    // g_snprintf(obj->_timestr, 20, "00:00");
155
242
 
156
243
    if (g_strcasecmp (callID, "") == 0)
157
244
        call_id = generate_call_id ();
158
245
    else
159
246
        call_id = callID;
 
247
 
160
248
    // Set the IDs
161
249
    obj->_callID = g_strdup (call_id);
162
250
    obj->_confID = NULL;
163
251
 
 
252
    obj->clockStarted = 1;
 
253
 
 
254
    if (obj->_type == CALL) {
 
255
        // pthread_create(&(obj->tid), NULL, threaded_clock_incrementer, obj);
 
256
        if ( (obj->tid = g_thread_create ( (GThreadFunc) threaded_clock_incrementer, (void *) obj, TRUE, &err1)) == NULL) {
 
257
            DEBUG ("Thread creation failed!");
 
258
            g_error_free (err1) ;
 
259
        }
 
260
    }
 
261
 
164
262
    *new_call = obj;
165
263
}
166
264
 
179
277
    if (g_strcasecmp (state_str, "CURRENT") == 0)
180
278
        state = CALL_STATE_CURRENT;
181
279
 
182
 
        else if (g_strcasecmp (state_str, "RINGING") == 0)
183
 
                state = CALL_STATE_RINGING;
 
280
    else if (g_strcasecmp (state_str, "RINGING") == 0)
 
281
        state = CALL_STATE_RINGING;
184
282
 
185
 
        else if (g_strcasecmp (state_str, "INCOMING") == 0)
186
 
                state = CALL_STATE_INCOMING;
 
283
    else if (g_strcasecmp (state_str, "INCOMING") == 0)
 
284
        state = CALL_STATE_INCOMING;
187
285
 
188
286
    else if (g_strcasecmp (state_str, "HOLD") == 0)
189
287
        state = CALL_STATE_HOLD;
194
292
    else
195
293
        state = CALL_STATE_FAILURE;
196
294
 
197
 
    create_new_call (CALL, state, (gchar*)call_id, accountID, peer_name, call_get_peer_number(peer_number), &new_call);
 
295
    create_new_call (CALL, state, (gchar*) call_id, accountID, peer_name, call_get_peer_number (peer_number), &new_call);
198
296
    *call = new_call;
199
297
}
200
298
 
210
308
 
211
309
    // details is in serialized form, i e: calltype%to%from%callid
212
310
 
213
 
    if ((ptr = g_strsplit(details, delim,5)) != NULL) {
214
 
 
215
 
        while (ptr != NULL && token < 5) {
216
 
            
217
 
            switch (token)
218
 
            {
 
311
    if ( (ptr = g_strsplit (details, delim,5)) != NULL) {
 
312
 
 
313
        while (ptr != NULL && token < 5) {
 
314
 
 
315
            switch (token) {
219
316
                case 0:
220
317
                    history_state = get_history_state_from_id (*ptr);
221
318
                    break;
222
 
                case 1: 
 
319
                case 1:
223
320
                    peer_number = *ptr;
224
321
                    break;
225
322
                case 2:
236
333
            }
237
334
 
238
335
            token++;
239
 
            ptr++;
 
336
            ptr++;
240
337
 
241
 
        }
 
338
        }
242
339
 
243
340
    }
 
341
 
244
342
    if (g_strcasecmp (peer_name, "empty") == 0)
245
343
        peer_name="";
246
344
 
254
352
 
255
353
void free_callable_obj_t (callable_obj_t *c)
256
354
{
 
355
    DEBUG ("CallableObj: Free callable object");
 
356
 
 
357
    stop_call_clock (c);
 
358
 
257
359
    g_free (c->_callID);
258
360
    g_free (c->_accountID);
259
361
    g_free (c->_peer_name);
260
362
    g_free (c->_peer_number);
261
363
    g_free (c->_peer_info);
262
364
    g_free (c);
 
365
 
 
366
    DEBUG ("If you don't see it that is because there is a problem");
 
367
 
 
368
    calltree_update_clock();
263
369
}
264
370
 
265
 
void attach_thumbnail (callable_obj_t *call, GdkPixbuf *pixbuf) {
 
371
void attach_thumbnail (callable_obj_t *call, GdkPixbuf *pixbuf)
 
372
{
266
373
    call->_contact_thumbnail = pixbuf;
267
374
}
268
375
 
270
377
{
271
378
    gchar *call_id;
272
379
 
273
 
    call_id = g_new0(gchar, 30);
274
 
    g_sprintf(call_id, "%d", rand());
 
380
    call_id = g_new0 (gchar, 30);
 
381
    g_sprintf (call_id, "%d", rand());
275
382
    return call_id;
276
383
}
277
384
 
279
386
{
280
387
    gchar *info;
281
388
 
282
 
    info = g_strconcat("\"", name, "\" <", number, ">", NULL);
 
389
    info = g_strconcat ("\"", name, "\" <", number, ">", NULL);
283
390
    return info;
284
391
}
285
392
 
286
 
history_state_t get_history_state_from_id (gchar *indice){
 
393
history_state_t get_history_state_from_id (gchar *indice)
 
394
{
287
395
 
288
396
    history_state_t state;
289
397
 
308
416
 
309
417
    start = obj->_time_start;
310
418
    end = obj->_time_stop;
311
 
    
 
419
 
312
420
    if (start == end)
313
 
        return g_markup_printf_escaped("<small>Duration:</small> 0:00");
314
 
 
315
 
    duration = (int) difftime(end, start);
316
 
 
317
 
    if( duration / 60 == 0 )
318
 
    {
319
 
        if( duration < 10 )
320
 
            res = g_markup_printf_escaped("00:0%i", duration);
321
 
        else
322
 
            res = g_markup_printf_escaped("00:%i", duration);
323
 
    }
324
 
    else
325
 
    {
326
 
        if( duration%60 < 10 )
327
 
            res = g_markup_printf_escaped("%i:0%i" , duration/60 , duration%60);
328
 
        else
329
 
            res = g_markup_printf_escaped("%i:%i" , duration/60 , duration%60);
330
 
    }
331
 
    return g_markup_printf_escaped("<small>Duration:</small> %s", res);
 
421
        return g_markup_printf_escaped ("<small>Duration:</small> 0:00");
 
422
 
 
423
    duration = (int) difftime (end, start);
 
424
 
 
425
    if (duration / 60 == 0) {
 
426
        if (duration < 10)
 
427
            res = g_markup_printf_escaped ("00:0%i", duration);
 
428
        else
 
429
            res = g_markup_printf_escaped ("00:%i", duration);
 
430
    } else {
 
431
        if (duration%60 < 10)
 
432
            res = g_markup_printf_escaped ("%i:0%i" , duration/60 , duration%60);
 
433
        else
 
434
            res = g_markup_printf_escaped ("%i:%i" , duration/60 , duration%60);
 
435
    }
 
436
 
 
437
    return g_markup_printf_escaped ("<small>Duration:</small> %s", res);
332
438
 
333
439
}
334
440
 
335
441
gchar* serialize_history_entry (callable_obj_t *entry)
336
442
{
337
443
    // "0|514-276-5468|Savoir-faire Linux|144562458" for instance
338
 
    
 
444
 
339
445
    gchar* result;
340
446
    gchar* separator = "|";
341
447
    gchar* history_state, *timestamp;
345
451
    // and the timestamps
346
452
    timestamp = convert_timestamp_to_gchar (entry->_time_stop);
347
453
 
348
 
    result = g_strconcat (history_state, separator, 
349
 
                          entry->_peer_number, separator, 
350
 
                          g_strcasecmp (entry->_peer_name,"") ==0 ? "empty": entry->_peer_name, separator, 
351
 
                          timestamp, separator, 
352
 
                          g_strcasecmp (entry->_accountID,"") ==0 ? "empty": entry->_accountID, 
 
454
    gchar* peer_name = (entry->_peer_name == NULL || g_strcasecmp (entry->_peer_name,"") == 0) ? "empty": entry->_peer_name;
 
455
    gchar* account_id = (entry->_accountID == NULL || g_strcasecmp (entry->_accountID,"") == 0) ? "empty": entry->_accountID;
 
456
 
 
457
    result = g_strconcat (history_state, separator,
 
458
                          entry->_peer_number, separator,
 
459
                          peer_name, separator,
 
460
                          timestamp, separator,
 
461
                          account_id,
353
462
                          NULL);
354
 
    
 
463
 
355
464
    return result;
356
465
}
357
466
 
358
467
gchar* get_history_id_from_state (history_state_t state)
359
468
{
360
469
    // Refer to history_state_t enum in callable_obj.h
361
 
    switch (state)
362
 
    {
 
470
    switch (state) {
363
471
        case MISSED:
364
472
            return "0";
365
473
        case INCOMING:
371
479
    }
372
480
}
373
481
 
374
 
gchar* get_formatted_start_timestamp (callable_obj_t *obj) { 
375
 
        
 
482
gchar* get_formatted_start_timestamp (callable_obj_t *obj)
 
483
{
 
484
 
376
485
    struct tm* ptr;
377
486
    time_t lt, now;
378
487
    unsigned char str[100];
379
488
 
380
 
    if (obj)
381
 
    {
382
 
                // Fetch the current timestamp
383
 
                (void) time (&now);
 
489
    if (obj) {
 
490
        // Fetch the current timestamp
 
491
        (void) time (&now);
384
492
        lt = obj->_time_start;
385
493
 
386
494
        ptr = localtime (&lt);
387
495
 
388
 
                if (now - lt < UNIX_WEEK) {
389
 
                        if (now-lt < UNIX_DAY) {
390
 
                                strftime((char *)str, 100, N_("today at %R"), (const struct tm *)ptr);
391
 
                        } else {
392
 
                                if (now - lt < UNIX_TWO_DAYS) {
393
 
                                        strftime((char *)str, 100, N_("yesterday at %R"), (const struct tm *)ptr);
394
 
                                } else {
395
 
                                        strftime((char *)str, 100, N_("%A at %R"), (const struct tm *)ptr);
396
 
                                }
397
 
                        }
398
 
                } else {
399
 
                        strftime((char *)str, 100, N_("%x at %R"), (const struct tm *)ptr);
400
 
                }
 
496
        if (now - lt < UNIX_WEEK) {
 
497
            if (now-lt < UNIX_DAY) {
 
498
                strftime ( (char *) str, 100, N_ ("today at %R"), (const struct tm *) ptr);
 
499
            } else {
 
500
                if (now - lt < UNIX_TWO_DAYS) {
 
501
                    strftime ( (char *) str, 100, N_ ("yesterday at %R"), (const struct tm *) ptr);
 
502
                } else {
 
503
                    strftime ( (char *) str, 100, N_ ("%A at %R"), (const struct tm *) ptr);
 
504
                }
 
505
            }
 
506
        } else {
 
507
            strftime ( (char *) str, 100, N_ ("%x at %R"), (const struct tm *) ptr);
 
508
        }
401
509
 
402
510
        // result function of the current locale
403
 
        return g_markup_printf_escaped("\n%s\n" , str);
 
511
        return g_markup_printf_escaped ("\n%s\n" , str);
404
512
    }
 
513
 
405
514
    return "";
406
515
}
407
516
 
410
519
    time_t tmp;
411
520
 
412
521
    // Set to the current value
413
 
    (void) time(&tmp);
 
522
    (void) time (&tmp);
414
523
    *timestamp=tmp;
415
524
}
416
525
 
417
526
gchar* convert_timestamp_to_gchar (time_t timestamp)
418
527
{
419
 
    return g_markup_printf_escaped ("%i", (int)timestamp);
 
528
    return g_markup_printf_escaped ("%i", (int) timestamp);
420
529
}
421
530
 
422
531
time_t convert_gchar_to_timestamp (gchar *timestamp)
423
532
{
424
533
    return (time_t) atoi (timestamp);
425
534
}
 
535
 
 
536
gchar*
 
537
get_peer_information (callable_obj_t *c)
 
538
{
 
539
 
 
540
    if (g_strcasecmp (c->_peer_name,"") == 0)
 
541
        return g_strdup (c->_peer_number);
 
542
    else
 
543
        return g_strdup (c->_peer_name);
 
544
}
 
545
 
 
546