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

#include <gtk/gtk.h>
#include <calltree.h>
#include <calllist.h>

GtkListStore * store;
25
26
GtkWidget *view;

27
28
GtkWidget * callButton;
GtkWidget * hangupButton;
29
GtkWidget * holdButton;
30
GtkWidget * transfertButton;
31
GtkWidget * unholdButton;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
32
33
34

call_t * selectedCall;

35
/**
36
 * Make a call
37
38
 */
static void 
39
call_button( GtkWidget *widget, gpointer   data )
40
{
41
  if(selectedCall)
42
  {
43
44
45
46
47
48
49
50
51
    switch(selectedCall->state)
    {
      case CALL_STATE_DIALING:
        dbus_place_call (selectedCall);
        break;
      case CALL_STATE_INCOMING:
        dbus_accept (selectedCall);
        break;
    }
52
53
54
55
  }
}

/**
56
 * Hang up the line
57
58
 */
static void 
59
hang_up( GtkWidget *widget, gpointer   data )
60
{
61
  if(selectedCall)
62
  {
63
64
65
66
67
68
69
70
71
72
73
    switch(selectedCall->state)
    {
      case CALL_STATE_CURRENT:
      case CALL_STATE_HOLD:
      case CALL_STATE_DIALING:
        dbus_hang_up (selectedCall);
        break;
      case CALL_STATE_INCOMING:  
        dbus_refuse (selectedCall);
        break;
    }
74
75
76
77
  }
}

/**
78
 * Hold the line
79
80
 */
static void 
81
hold( GtkWidget *widget, gpointer   data )
82
{
83
  if(selectedCall)
84
  {
85
86
87
88
89
90
    switch(selectedCall->state)
    {
      case CALL_STATE_CURRENT:
        dbus_hold (selectedCall);
        break;
    }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  }
}

/**
 * Transfert the line
 */
static void 
transfert( GtkWidget *widget, gpointer   data )
{
  call_t * c = (call_t*) call_list_get_by_state (CALL_STATE_CURRENT);
  if(c)
  {
    dbus_transfert(c,"124");
  }
}

107
108
109
/**
 * Refuse incoming call
 */
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
110
static void 
111
refuse( GtkWidget *widget, gpointer   data )
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
112
{
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  if(selectedCall)
  {
    dbus_refuse(selectedCall);
  }
}

/**
 * Unhold call
 */
static void 
unhold( GtkWidget *widget, gpointer   data )
{
  if(selectedCall)
  {
    switch(selectedCall->state)
    {
      case CALL_STATE_HOLD:
        dbus_unhold (selectedCall);
        break;
      }
  }
}
void 
update_buttons ()
{
  gtk_widget_set_sensitive( GTK_WIDGET(callButton),       FALSE);
  gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     FALSE);
  gtk_widget_set_sensitive( GTK_WIDGET(holdButton),       FALSE);
  gtk_widget_set_sensitive( GTK_WIDGET(transfertButton),  FALSE);
  gtk_widget_set_sensitive( GTK_WIDGET(unholdButton),     FALSE);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
143
144
145
146
147
	
	if(selectedCall)
	{
	  if( selectedCall->state == CALL_STATE_INCOMING)
	  {
148
149
      gtk_widget_set_sensitive( GTK_WIDGET(callButton),       TRUE);
      gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     TRUE);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
150
151
152
    }
    else if( selectedCall->state == CALL_STATE_HOLD)
	  {
153
154
155
156
157
158
      gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     TRUE);
      gtk_widget_set_sensitive( GTK_WIDGET(unholdButton),       TRUE);
    }
    else if( selectedCall->state == CALL_STATE_RINGING)
	  {
      gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     TRUE);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
159
    }
160
161
162
    else if(selectedCall->state == CALL_STATE_DIALING)
    {
      gtk_widget_set_sensitive( GTK_WIDGET(callButton),       TRUE);
163
      gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     TRUE);
164
165
166
167
168
169
170
171
172
173
174
175
176
    }
    else if (selectedCall->state == CALL_STATE_CURRENT)
    {
      //gtk_widget_hide( callButton  );
      /* Hack : if hangupButton is put on the window in create_screen()
       * the hbox will request space for 4 buttons making the window larger than needed */
      //gtk_box_pack_start (GTK_BOX (hbox), hangupButton, TRUE /*expand*/, TRUE /*fill*/, 10 /*padding*/);
      //gtk_box_reorder_child(GTK_BOX (hbox), hangupButton, 0);
      gtk_widget_show( hangupButton );
      gtk_widget_set_sensitive( GTK_WIDGET(hangupButton),     TRUE);
      gtk_widget_set_sensitive( GTK_WIDGET(holdButton),       TRUE);
      gtk_widget_set_sensitive( GTK_WIDGET(transfertButton),  TRUE);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
177
178
	  
	}
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
/* Call back when the user click on a call in the list */
static void 
selected(GtkTreeSelection *sel, GtkTreeModel *model) 
{
	GtkTreeIter  iter;
	GValue val;
	
	if (! gtk_tree_selection_get_selected (sel, &model, &iter))
		return;
  
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (model, &iter, 2, &val);
	
	selectedCall = (call_t*) g_value_get_pointer(&val);
  g_value_unset(&val);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
195
  
196
  update_buttons();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
}

/**
 * Accept incoming call
 */
static void 
accept( GtkWidget *widget, gpointer   data )
{
  if(selectedCall)
  {
    dbus_accept(selectedCall);
  }
}



GtkWidget * 
create_call_tree (){
  GtkWidget *ret;
	GtkWidget *sw;
  GtkWidget *hbox;
	GtkWidget *image;
	GtkWidget *bbox;
	GtkCellRenderer *rend;
	GtkTreeViewColumn *col;
	GtkTreeSelection *sel;
	GtkTargetEntry te[2] = {{"text/uri-list", 0, 1},{"STRING", 0, 2}};

	ret = gtk_vbox_new(FALSE, 10); 
	gtk_container_set_border_width (GTK_CONTAINER (ret), 0);

	sw = gtk_scrolled_window_new( NULL, NULL);
229
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
230
231
232
233
234
235
236
237
238
239
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);

	store = gtk_list_store_new (3, 
	  GDK_TYPE_PIXBUF,// Icon 
	  G_TYPE_STRING,  // Description
	  G_TYPE_POINTER  // Pointer to the Object
	  );


	view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
240
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(view), FALSE);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	
  rend = gtk_cell_renderer_pixbuf_new();
  col = gtk_tree_view_column_new_with_attributes ("Icon",
							rend,
							"pixbuf", 0,
							NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);

	rend = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes ("Description",
							rend,
							"markup", 1,
							NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);
	g_object_unref(G_OBJECT(store));
	gtk_container_add(GTK_CONTAINER(sw), view);

  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
  g_signal_connect (G_OBJECT (sel), "changed",
			  G_CALLBACK (selected),
			  store);
  
  /* Action button */     
264
265
  hbox = gtk_hbox_new (FALSE, 5);       
  gtk_button_box_set_spacing ( hbox, 5);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
266
267
  gtk_button_box_set_layout ( GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_START);
  
268
  callButton = gtk_button_new ();
269
270
271
272
  gtk_widget_set_state( GTK_WIDGET(callButton), GTK_STATE_INSENSITIVE);
  image = gtk_image_new_from_file( PIXMAPS_DIR "/call.svg");
  gtk_button_set_image(GTK_BUTTON(callButton), image);
  //gtk_button_set_image_position( button, GTK_POS_TOP);
273
  gtk_box_pack_start (GTK_BOX (hbox), callButton, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
274
  g_signal_connect (G_OBJECT (callButton), "clicked",
275
                    G_CALLBACK (call_button), NULL);
276

277
  hangupButton = gtk_button_new ();
278
279
  gtk_widget_hide( hangupButton );
  gtk_widget_set_state( GTK_WIDGET(hangupButton), GTK_STATE_INSENSITIVE);
280
  gtk_box_pack_start (GTK_BOX (hbox), hangupButton, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
281
282
283
284
285
  image = gtk_image_new_from_file( PIXMAPS_DIR "/hang_up.svg");
  gtk_button_set_image(GTK_BUTTON(hangupButton), image);
  //gtk_button_set_image_position( button, GTK_POS_TOP);
  g_signal_connect (G_OBJECT (hangupButton), "clicked",
                    G_CALLBACK (hang_up), NULL);
286
287
288
289
290
291
292
293
294
295
                      
  unholdButton = gtk_button_new ();
  image = gtk_image_new_from_file( PIXMAPS_DIR "/unhold.svg");
  gtk_button_set_image(GTK_BUTTON(unholdButton), image);
  gtk_widget_set_state( GTK_WIDGET(unholdButton), GTK_STATE_INSENSITIVE);
  gtk_box_pack_end (GTK_BOX (hbox), unholdButton, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
  g_signal_connect (G_OBJECT (unholdButton), "clicked",
                    G_CALLBACK (unhold), NULL);
             
  holdButton = gtk_button_new ();
296
297
  image = gtk_image_new_from_file( PIXMAPS_DIR "/hold.svg");
  gtk_button_set_image(GTK_BUTTON(holdButton), image);
298
299
  gtk_widget_set_state( GTK_WIDGET(holdButton), GTK_STATE_INSENSITIVE);
  gtk_box_pack_end (GTK_BOX (hbox), holdButton, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
300
301
302
  g_signal_connect (G_OBJECT (holdButton), "clicked",
                    G_CALLBACK (hold), NULL);

303
  transfertButton = gtk_button_new ();
304
305
  image = gtk_image_new_from_file( PIXMAPS_DIR "/transfert.svg");
  gtk_button_set_image(GTK_BUTTON(transfertButton), image);
306
307
  gtk_widget_set_state( GTK_WIDGET(transfertButton), GTK_STATE_INSENSITIVE);
  gtk_box_pack_end (GTK_BOX (hbox), transfertButton, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
308
309
  g_signal_connect (G_OBJECT (transfertButton), "clicked",
                    G_CALLBACK (transfert), NULL);
310
311
312
313
                    
  gtk_box_pack_start (GTK_BOX (ret), hbox, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
  gtk_box_pack_start(GTK_BOX(ret), sw, TRUE, TRUE, 0);
	
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
314
315
316
317
318
319
	gtk_widget_show(ret); 
	
	return ret;
	
}
void 
320
update_call_tree_remove (call_t * c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
321
{
322
  
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
323
	GtkTreeIter iter;
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	GValue val;
	call_t * iterCall;
	
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (GTK_TREE_MODEL(store), &iter, 2, &val);
	
	iterCall = (call_t*) g_value_get_pointer(&val);
  g_value_unset(&val);
    
	int nbChild = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store), NULL);
  int i;
	for( i = 0; i < nbChild; i++)
	{
	  if(gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), &iter, NULL, i))
	  {
	    val.g_type = G_TYPE_POINTER;
    	gtk_tree_model_get_value (GTK_TREE_MODEL(store), &iter, 2, &val);
    	
    	iterCall = (call_t*) g_value_get_pointer(&val);
      g_value_unset(&val);
      
      if(iterCall == c)
      {
        gtk_list_store_remove(store, &iter);
      }
	  }
	}
	if(selectedCall == c)
	  selectedCall = NULL;
	update_buttons();
}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
355

356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
void 
update_call_tree (call_t * c)
{
  GdkPixbuf *pixbuf;
  GtkTreeIter iter;
	GValue val;
	call_t * iterCall;
	
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (GTK_TREE_MODEL(store), &iter, 2, &val);
	
	iterCall = (call_t*) g_value_get_pointer(&val);
  g_value_unset(&val);
    
	int nbChild = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
371
  int i;
372
	for( i = 0; i < nbChild; i++)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
373
	{
374
375
376
377
378
379
380
381
382
	  if(gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), &iter, NULL, i))
	  {
	    val.g_type = G_TYPE_POINTER;
    	gtk_tree_model_get_value (GTK_TREE_MODEL(store), &iter, 2, &val);
    	
    	iterCall = (call_t*) g_value_get_pointer(&val);
      g_value_unset(&val);
      
      if(iterCall == c)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
383
      {
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
        // Existing call in the list
        gchar * markup;
        if (c->state == CALL_STATE_CURRENT)
    		{
    		  markup = g_markup_printf_escaped("<big><b>%s</b></big>\n"
    						    "%s", 
    						    call_get_name(c), 
    						    call_get_number(c));
    		}
    		else 
    		{
    		  markup = g_markup_printf_escaped("<b>%s</b>\n"
    						    "%s", 
    						    call_get_name(c), 
    						    call_get_number(c));
    		}
    		
    		gtk_list_store_set (store, &iter);
    		
    		if (c->state == CALL_STATE_HOLD)
    		{
    		  pixbuf = gdk_pixbuf_new_from_file(PIXMAPS_DIR "/hold.svg", NULL);
    		}
    		else if (c->state == CALL_STATE_RINGING)
    		{
    		  pixbuf = gdk_pixbuf_new_from_file(PIXMAPS_DIR "/ring.svg", NULL);
    		}
    	  else if (c->state == CALL_STATE_CURRENT)
    		{
    		  pixbuf = gdk_pixbuf_new_from_file(PIXMAPS_DIR "/current.svg", NULL);
    		}
    	  //Resize it
        if(pixbuf)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
417
        {
418
419
420
421
          if(gdk_pixbuf_get_width(pixbuf) > 32 || gdk_pixbuf_get_height(pixbuf) > 32)
          {
            pixbuf =  gdk_pixbuf_scale_simple(pixbuf, 32, 32, GDK_INTERP_BILINEAR);
          }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
422
        }
423
424
425
426
427
428
429
430
431
432
433
    		gtk_list_store_set(store, &iter,
    				   0, pixbuf, // Icon
    				   1, markup, // Description
    				   2, c,      // Pointer
    				   -1);
    				   
    		if (pixbuf != NULL)
    			g_object_unref(G_OBJECT(pixbuf));
        
      } 
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
434
435
    
  } 
436
  update_buttons();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
437
	//return row_ref;
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483

}

void 
update_call_tree_add (call_t * c)
{
  GdkPixbuf *pixbuf;
	GtkTreeIter iter;

  // New call in the list
  gchar * markup;
  markup = g_markup_printf_escaped("<b>%s</b>\n"
  				    "%s", 
  				    call_get_name(c), 
  				    call_get_number(c));
  
  gtk_list_store_append (store, &iter);

  if (c->state == CALL_STATE_INCOMING)
  {
    pixbuf = gdk_pixbuf_new_from_file(PIXMAPS_DIR "/ring.svg", NULL);
  }
  else if (c->state == CALL_STATE_DIALING)
  {
    pixbuf = gdk_pixbuf_new_from_file(PIXMAPS_DIR "/dial.svg", NULL);
  }
  //Resize it
  if(pixbuf)
  {
    if(gdk_pixbuf_get_width(pixbuf) > 32 || gdk_pixbuf_get_height(pixbuf) > 32)
    {
      pixbuf =  gdk_pixbuf_scale_simple(pixbuf, 32, 32, GDK_INTERP_BILINEAR);
    }
  }
  gtk_list_store_set(store, &iter,
  		   0, pixbuf, // Icon
  		   1, markup, // Description
  		   2, c,      // Pointer
  		   -1);
  		   
  if (pixbuf != NULL)
  	g_object_unref(G_OBJECT(pixbuf));

  //g_free(markup);
  
  update_buttons();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
484
}