calllist.c 5.06 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1 2
/*
 *  Copyright (C) 2007 Savoir-Faire Linux inc.
3
 *  Author: Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
4
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
5 6
 *  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
7
 *  the Free Software Foundation; either version 3 of the License, or
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
8
 *  (at your option) any later version.
9
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
10 11 12 13
 *  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.
14
 *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
15 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

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
20
#include <calllist.h>
21 22
#include <calltree.h>
#include <dbus.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
23

24
#include <glib/gprintf.h>
25

26 27 28 29
/*
 * GQueue * callQueue = NULL;
 * call_t * selectedCall = NULL;
 */
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
30 31

/* GCompareFunc to compare a callID (gchar* and a call_t) */
32
gint
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
33 34 35
is_callID_callstruct ( gconstpointer a, gconstpointer b)
{
  call_t * c = (call_t*)a;
36
  if(g_strcasecmp(c->callID, (const gchar*) b) == 0)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
37 38 39 40 41 42 43 44 45 46
  {
    return 0;
  }
  else
  {
    return 1;
  }
}

/* GCompareFunc to get current call (gchar* and a call_t) */
47
gint
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
48 49 50 51 52 53 54 55 56 57 58 59 60
get_state_callstruct ( gconstpointer a, gconstpointer b)
{
  call_t * c = (call_t*)a;
  if( c->state == *((call_state_t*)b))
  {
    return 0;
  }
  else
  {
    return 1;
  }
}

61
void
62
call_list_init (calltab_t* tab)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
63
{
64 65
  tab->callQueue = g_queue_new ();
  tab->selectedCall = NULL;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
66 67
}

68
void
69
call_list_clean (calltab_t* tab)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
70
{
71
  g_queue_free (tab->callQueue);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
72 73
}

74
void
Emmanuel Milou's avatar
Emmanuel Milou committed
75 76 77 78 79 80
call_list_reset (calltab_t* tab)
{
  g_queue_free (tab->callQueue);
  tab->callQueue = g_queue_new();
}

81
void
82
call_list_add (calltab_t* tab, call_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
83
{
84
  if( tab == history )
85
  {
86
    // First case: can still add calls to the list
87 88 89
    if( call_list_get_size(tab) < dbus_get_max_calls() )
    {
      g_queue_push_tail (tab->callQueue, (gpointer *) c);
90 91 92 93 94
      update_call_tree_add( history , c );
    }
    // List full -> Remove the last call from history and preprend the new call to the list
    else
    {
95
      update_call_tree_remove( history , (call_t*)g_queue_pop_head( tab -> callQueue ) );
96 97
      g_queue_push_tail (tab->callQueue, (gpointer *) c);
      update_call_tree_add( history , c );
98 99 100 101
    }
  }
  else
    g_queue_push_tail (tab->callQueue, (gpointer *) c);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
102 103
}

104
void
Emmanuel Milou's avatar
Emmanuel Milou committed
105
call_list_clean_history( void )
106
{
Emmanuel Milou's avatar
Emmanuel Milou committed
107
  unsigned int i;
Emmanuel Milou's avatar
Emmanuel Milou committed
108 109 110 111 112 113 114 115 116 117 118
  guint size = call_list_get_size( history );
  g_print("history list size = %i\n", call_list_get_size( history ));
  for( i = 0 ; i < size ; i++ )
  {
    g_print("Delete calls");
    call_t* c = call_list_get_nth( history , i );
    // Delete the call from the call tree
    g_print("Delete calls");
    update_call_tree_remove(history , c);
  }
  call_list_reset( history );
119
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
120

121 122 123 124 125 126 127 128
void
call_list_remove_from_history( call_t* c )
{
  call_list_remove( history, c->callID );
  update_call_tree_remove( history, c );
  g_print("Size of history = %i\n" , call_list_get_size( history ));
}

129
void
130
call_list_remove (calltab_t* tab, const gchar * callID)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
131
{
132
  call_t * c = call_list_get(tab, callID);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
133 134
  if (c)
  {
135
    g_queue_remove(tab->callQueue, c);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
136 137 138 139
  }
}


140
call_t *
141
call_list_get_by_state (calltab_t* tab, call_state_t state )
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
142
{
143
  GList * c = g_queue_find_custom (tab->callQueue, &state, get_state_callstruct);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
144 145 146 147
  if (c)
  {
    return (call_t *)c->data;
  }
148
  else
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
149 150 151
  {
    return NULL;
  }
152

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
153 154 155
}

guint
156
call_list_get_size (calltab_t* tab)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
157
{
158
  return g_queue_get_length (tab->callQueue);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
159 160
}

161
call_t *
162
call_list_get_nth (calltab_t* tab, guint n )
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
163
{
164
  return g_queue_peek_nth (tab->callQueue, n);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
165 166
}

167
gchar *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
168 169 170
call_get_name (const call_t * c)
{
  gchar * end = g_strrstr(c->from, "\"");
171 172 173 174 175 176
  if (!end) {
    return g_strndup(c->from, 0);
  } else {
    gchar * name = c->from +1;
    return g_strndup(name, end - name);
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
177 178
}

179
gchar *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
180 181 182 183 184 185 186 187
call_get_number (const call_t * c)
{
  gchar * number = g_strrstr(c->from, "<") + 1;
  gchar * end = g_strrstr(c->from, ">");
  number = g_strndup(number, end - number  );
  return number;
}

Emmanuel Milou's avatar
Emmanuel Milou committed
188 189 190 191 192 193
gchar *
call_get_recipient( const call_t * c )
{
  return c->to;
}

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

195
call_t *
196
call_list_get (calltab_t* tab, const gchar * callID )
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
197
{
198
  GList * c = g_queue_find_custom (tab->callQueue, callID, is_callID_callstruct);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
199 200 201 202
  if (c)
  {
    return (call_t *)c->data;
  }
203
  else
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
204 205 206 207
  {
    return NULL;
  }
}
208 209

void
210
call_select (calltab_t* tab, call_t * c )
211
{
212
  tab->selectedCall = c;
213 214 215 216
}


call_t *
217
call_get_selected (calltab_t* tab)
218
{
219
  return tab->selectedCall;
220
}
Emmanuel Milou's avatar
Emmanuel Milou committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

void create_new_call (gchar *to, gchar *from, call_state_t state, gchar *accountID, call_t **new_call) {

    gchar *call_id;
    call_t *call;

    call = g_new0 (call_t, 1);
    call->to = g_strdup (to); 
    call->from = g_strdup (from);
    call->state = state;
    call->accountID = g_strdup (accountID);
    call->_start = 0;
    call->_stop = 0;

    call_id = g_new0(gchar, 30);
    g_sprintf(call_id, "%d", rand());
    call->callID = g_strdup (call_id);

    *new_call = call;
}
241 242 243 244

void attach_thumbnail (call_t *call, GdkPixbuf *pixbuf) {
    call->contact_thumbnail = pixbuf;
}