mainwindow.c 7.78 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@squidy.info>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *                                                                              
 *  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 <config.h>
#include <actions.h>
#include <calllist.h> 
23
#include <calltree.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
24
#include <configwindow.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
25
#include <dialpad.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
26
#include <mainwindow.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
27
28
#include <menus.h>
#include <screen.h>
29
#include <sliders.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
30
31
32
33

#include <gtk/gtk.h>

/** Local variables */
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
34
GtkAccelGroup * accelGroup = NULL;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
35
36
37
38
39
GtkWidget * window    = NULL;
GtkWidget * subvbox   = NULL;
GtkWidget * dialpad   = NULL;
GtkWidget * statusBar = NULL;
gboolean showDialpad  = FALSE; // true if the dialpad have been showned
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
40

41

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * Terminate the main loop.
 */
static gboolean
on_delete (GtkWidget * widget, gpointer data)
{
  /* Must return FALSE to have the window destroyed */
  return !sflphone_quit();
}

/** Ask the user if he wants to hangup current calls */
gboolean 
main_window_ask_quit(){
  guint count = call_list_get_size();
  GtkWidget * dialog;
  guint response;
  gchar * question;
  
  if(count == 1)
  {
    question = "<b>There is one call in progress.</b>\nDo you still want to quit?";
  }
  else
  {
    question = "<b>There are calls in progress.</b>\nDo you still want to quit?";
  }
  
  dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(window) ,
                                  GTK_DIALOG_MODAL,
                                  GTK_MESSAGE_QUESTION,
                                  GTK_BUTTONS_YES_NO,
                                  question);
  
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  
  gtk_widget_destroy (dialog);
  if(response == GTK_RESPONSE_YES)
  { 
    return TRUE;
  }
  else if(response == GTK_RESPONSE_NO)
  {
    return FALSE;
  }
  return TRUE;
}

static gboolean
on_key_released (GtkWidget   *widget,
                GdkEventKey *event,
                gpointer     user_data)  
{
#ifdef DEBUG
  g_print("KEY %s, %d\n", event->string, event->keyval);
#endif 
  // If a modifier key is pressed, it's a shortcut, pass along
  if(event->state & GDK_CONTROL_MASK || 
99
     event->state & GDK_MOD1_MASK    ||
100
101
102
     event->keyval == 60             || // <
     event->keyval == 62             || // >
     event->keyval == 34             || // "
103
     event->keyval == 65361          || // left arrow
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
104
     event->keyval == 65363          || // right arrow
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
105
     event->keyval >= 65470          || // F-keys
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
106
     event->keyval == 32                // space
107
     )
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
108
109
110
111
112
113
114
115
    return FALSE;
  sflphone_keypad(event->keyval, event->string);
  return TRUE;
}                

void
create_main_window ()
{
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
116
  GtkWidget *widget;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
117
118
119
120
121
  GtkWidget *vbox;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  gtk_window_set_title (GTK_WINDOW (window), PACKAGE);
122
  gtk_window_set_default_size (GTK_WINDOW (window), 230, 320);
123
  gtk_window_set_default_icon_from_file (ICON_DIR "/sflphone.png", 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
                                          NULL);

  /* Connect the destroy event of the window with our on_destroy function
    * When the window is about to be destroyed we get a notificaiton and
    * stop the main GTK loop
    */
  g_signal_connect (G_OBJECT (window), "delete-event",
                    G_CALLBACK (on_delete), NULL);
  g_signal_connect (G_OBJECT (window), "key-press-event",
                    G_CALLBACK (on_key_released), NULL);

  /* Create an accel group for window's shortcuts */
  accelGroup = gtk_accel_group_new ();
  gtk_window_add_accel_group(GTK_WINDOW(window), accelGroup);

  vbox = gtk_vbox_new ( FALSE /*homogeneous*/, 0 /*spacing*/);
140
141
  subvbox = gtk_vbox_new ( FALSE /*homogeneous*/, 5 /*spacing*/);
  gtk_container_set_border_width (GTK_CONTAINER(subvbox), 5);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
142
  
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
143
144
  widget = create_menus();
  gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
145
146
147
148
149
150
  
  widget = create_toolbar();
  gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
  gtk_box_pack_start (GTK_BOX (vbox), create_call_tree(), TRUE /*expand*/, TRUE /*fill*/,  0 /*padding*/);
  
  gtk_box_pack_start (GTK_BOX (vbox), subvbox, FALSE /*expand*/, FALSE /*fill*/, 0 /*padding*/);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
151
152
 
  widget = create_screen();
153
154
  // TODO Add the screen when we are decided
  //gtk_box_pack_start (GTK_BOX (subvbox), widget, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
155
  
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
156
157
158
159
  widget = create_slider("speaker");
  gtk_box_pack_start (GTK_BOX (subvbox), widget, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
  widget = create_slider("mic");
  gtk_box_pack_start (GTK_BOX (subvbox), widget, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
160
  
161
  /* Status bar */
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
162
163
  statusBar = gtk_statusbar_new();
  gtk_box_pack_start (GTK_BOX (vbox), statusBar, FALSE /*expand*/, TRUE /*fill*/,  0 /*padding*/);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
164
165
166
167
168
169
  gtk_container_add (GTK_CONTAINER (window), vbox);

  /* make sure that everything, window and label, are visible */
  gtk_widget_show_all (window);
  
  screen_clear();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

  // Welcome screen
  if (account_list_get_size() == 0)
  {
    GtkWidget * dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(window),
                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                  GTK_MESSAGE_INFO,
                                  GTK_BUTTONS_YES_NO,
                                  "<b><big>Welcome to SFLphone!</big></b>\n\nThere are no VoIP accounts configured, would you like to edit the preferences now?");

    int response = gtk_dialog_run (GTK_DIALOG(dialog));
    
    gtk_widget_destroy (GTK_WIDGET(dialog));

    if (response == GTK_RESPONSE_YES)
    {
      show_config_window();
    }
   
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
190
191
192
193
194
195
196
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
229
230
}

GtkAccelGroup * 
get_accel_group()
{
  return accelGroup;
}

GtkWidget * 
get_main_window()
{
  return window;
}

void
main_window_message(GtkMessageType type, gchar * markup){
  GtkWidget * dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(get_main_window()),
                                      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                      type,
                                      GTK_BUTTONS_CLOSE,
                                      markup);
  gtk_dialog_run (GTK_DIALOG(dialog));
  
  gtk_widget_destroy (GTK_WIDGET(dialog));
}

void
main_window_error_message(gchar * markup){
  main_window_message(GTK_MESSAGE_ERROR, markup);
}

void
main_window_warning_message(gchar * markup){
  main_window_message(GTK_MESSAGE_WARNING, markup);
}

void
main_window_info_message(gchar * markup){
  main_window_message(GTK_MESSAGE_INFO, markup);
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
231
232
233
234
235
236
237
238
239
240
241
void
main_window_dialpad(gboolean show){
  if(show && !showDialpad)
  {
    dialpad = create_dialpad();
    gtk_box_pack_end (GTK_BOX (subvbox), dialpad, FALSE /*expand*/, TRUE /*fill*/, 0 /*padding*/);
    gtk_box_reorder_child(GTK_BOX (subvbox), dialpad, 1);
    gtk_widget_show_all (dialpad);
  }
  else if (!show && showDialpad)
  {
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
242
    gtk_container_remove(GTK_CONTAINER (subvbox), dialpad);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
243
244
  }
  showDialpad = show;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
245
246
247
248
249
250
251
}


void 
status_bar_message(const gchar * message)
{ 
  gtk_statusbar_push(GTK_STATUSBAR(statusBar), 0, message);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
252
253
}