forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.c
389 lines (316 loc) · 12.9 KB
/
dialer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
99
100
101
102
103
104
105
106
107
108
109
110
111
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
* dialer.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2014 by Judd Montgomery
*
* 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; version 2 of the License.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
******************************************************************************/
/********************************* Includes ***********************************/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.h>
#include "i18n.h"
#include "utils.h"
#include "log.h"
#include "prefs.h"
/********************************* Constants **********************************/
#define CHOOSE_PHONE 1
#define CHOOSE_EXT 2
/****************************** Prototypes ************************************/
struct dialog_data {
GtkWidget *entry_pre1;
GtkWidget *entry_pre2;
GtkWidget *entry_pre3;
GtkWidget *check_pre1;
GtkWidget *check_pre2;
GtkWidget *check_pre3;
GtkWidget *entry_phone;
GtkWidget *entry_ext;
GtkWidget *entry_command;
GtkWidget *label_prefix;
};
/****************************** Main Code *************************************/
static void cb_dialog_button(GtkWidget *widget, gpointer data)
{
GtkWidget *w;
w = gtk_widget_get_toplevel(widget);
gtk_object_get_data(GTK_OBJECT(w), "dialog_data");
gtk_widget_destroy(GTK_WIDGET(w));
}
static gboolean cb_destroy_dialog(GtkWidget *widget)
{
struct dialog_data *Pdata;
const gchar *txt;
Pdata = gtk_object_get_data(GTK_OBJECT(widget), "dialog_data");
if (!Pdata) {
return TRUE;
}
txt = gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre1));
set_pref(PREF_PHONE_PREFIX1, 0, txt, FALSE);
txt = gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre2));
set_pref(PREF_PHONE_PREFIX2, 0, txt, FALSE);
txt = gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre3));
set_pref(PREF_PHONE_PREFIX3, 0, txt, FALSE);
txt = gtk_entry_get_text(GTK_ENTRY(Pdata->entry_command));
set_pref(PREF_DIAL_COMMAND, 0, txt, FALSE);
set_pref(PREF_CHECK_PREFIX1, GTK_TOGGLE_BUTTON(Pdata->check_pre1)->active,
NULL, FALSE);
set_pref(PREF_CHECK_PREFIX2, GTK_TOGGLE_BUTTON(Pdata->check_pre2)->active,
NULL, FALSE);
set_pref(PREF_CHECK_PREFIX3, GTK_TOGGLE_BUTTON(Pdata->check_pre3)->active,
NULL, TRUE);
gtk_main_quit();
return TRUE;
}
static void set_prefix_label(struct dialog_data *Pdata)
{
char str[70];
g_snprintf(str, sizeof(str), "%s%s%s",
GTK_TOGGLE_BUTTON(Pdata->check_pre1)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre1)) : "",
GTK_TOGGLE_BUTTON(Pdata->check_pre2)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre2)) : "",
GTK_TOGGLE_BUTTON(Pdata->check_pre3)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre3)) : "");
gtk_label_set_text(GTK_LABEL(Pdata->label_prefix), str);
}
static void cb_prefix_change(GtkWidget *widget, gpointer data)
{
set_prefix_label(data);
}
static void dialer(gpointer data, int phone_or_ext)
{
struct dialog_data *Pdata;
char str[70];
char null_str[]="";
const char *Pext;
char command[1024];
const char *pref_command;
char c1, c2;
int i, len;
Pdata=data;
if (phone_or_ext==CHOOSE_PHONE) {
g_snprintf(str, sizeof(str), "%s%s%s%s",
GTK_TOGGLE_BUTTON(Pdata->check_pre1)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre1)) : "",
GTK_TOGGLE_BUTTON(Pdata->check_pre2)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre2)) : "",
GTK_TOGGLE_BUTTON(Pdata->check_pre3)->active ?
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_pre3)) : "",
gtk_entry_get_text(GTK_ENTRY(Pdata->entry_phone)));
}
if (phone_or_ext==CHOOSE_EXT) {
Pext=gtk_entry_get_text(GTK_ENTRY(Pdata->entry_ext));
if (!Pext) Pext=null_str;
g_strlcpy(str, Pext, sizeof(str));
}
pref_command = gtk_entry_get_text(GTK_ENTRY(Pdata->entry_command));
/* Make a system call command string */
memset(command, 0, sizeof(command));
for (i=0; i<MAX_PREF_LEN-1; i++) {
c1 = pref_command[i];
c2 = pref_command[i+1];
len = strlen(command);
/* expand '%n' */
if (c1=='%') {
if (c2=='n') {
i++;
strncat(command, str, 1022-len);
continue;
}
}
if (len<1020) {
command[len++]=c1;
command[len]='\0';
}
if (c1=='\0') {
break;
}
}
command[1022]='\0';
jp_logf(JP_LOG_STDOUT|JP_LOG_FILE, _("executing command = [%s]\n"), command);
if (system(command) == -1) {
jp_logf(JP_LOG_WARN, "system call failed %s %d\n", __FILE__, __LINE__);
}
}
static void cb_dial_ext(GtkWidget *widget, gpointer data)
{
dialer(data, CHOOSE_EXT);
}
static void cb_dial_phone(GtkWidget *widget, gpointer data)
{
dialer(data, CHOOSE_PHONE);
}
/*
* Dialog window for calling external dialing program
*/
int dialog_dial(GtkWindow *main_window, char *string, char *ext)
{
GtkWidget *button, *label;
GtkWidget *hbox1, *vbox1;
GtkWidget *dialog;
GtkWidget *entry;
GtkWidget *checkbox1, *checkbox2, *checkbox3;
struct dialog_data *Pdata;
long use_prefix;
const char *prefix;
const char *dial_command;
dialog = gtk_widget_new(GTK_TYPE_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
"title", _("Phone Dialer"),
NULL);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
GTK_SIGNAL_FUNC(cb_destroy_dialog), dialog);
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_window));
/* Set up a data structure for the window */
Pdata = malloc(sizeof(struct dialog_data));
gtk_object_set_data(GTK_OBJECT(dialog), "dialog_data", Pdata);
vbox1 = gtk_vbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(vbox1), 5);
gtk_container_add(GTK_CONTAINER(dialog), vbox1);
/* Prefix 1 */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
get_pref(PREF_PHONE_PREFIX1, NULL, &prefix);
get_pref(PREF_CHECK_PREFIX1, &use_prefix, NULL);
checkbox1 = gtk_check_button_new();
gtk_box_pack_start(GTK_BOX(hbox1), checkbox1, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1), use_prefix);
label = gtk_label_new(_("Prefix 1"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_text(GTK_ENTRY(entry), prefix);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
Pdata->entry_pre1=entry;
Pdata->check_pre1=checkbox1;
/* Prefix 2 */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
get_pref(PREF_PHONE_PREFIX2, NULL, &prefix);
get_pref(PREF_CHECK_PREFIX2, &use_prefix, NULL);
checkbox2 = gtk_check_button_new();
gtk_box_pack_start(GTK_BOX(hbox1), checkbox2, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox2), use_prefix);
label = gtk_label_new(_("Prefix 2"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_text(GTK_ENTRY(entry), prefix);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
Pdata->entry_pre2=entry;
Pdata->check_pre2=checkbox2;
/* Prefix 3 */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
get_pref(PREF_PHONE_PREFIX3, NULL, &prefix);
get_pref(PREF_CHECK_PREFIX3, &use_prefix, NULL);
checkbox3 = gtk_check_button_new();
gtk_box_pack_start(GTK_BOX(hbox1), checkbox3, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox3), use_prefix);
label = gtk_label_new(_("Prefix 3"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_text(GTK_ENTRY(entry), prefix);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
Pdata->entry_pre3=entry;
Pdata->check_pre3=checkbox3;
/* Phone number entry */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
label = gtk_label_new(_("Phone number:"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
/* Prefix label */
label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
Pdata->label_prefix=label;
set_prefix_label(Pdata);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_text(GTK_ENTRY(entry), string);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(cb_dial_ext), Pdata);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
Pdata->entry_phone=entry;
/* Dial Phone Button */
button = gtk_button_new_with_label(_("Dial"));
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dial_phone), Pdata);
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 1);
gtk_widget_grab_focus(GTK_WIDGET(button));
/* Extension */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
label = gtk_label_new(_("Extension"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_text(GTK_ENTRY(entry), ext);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(cb_dial_ext), Pdata);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
Pdata->entry_ext=entry;
/* Dial Phone Button */
button = gtk_button_new_with_label(_("Dial"));
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dial_ext), Pdata);
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 1);
/* Command Entry */
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
label = gtk_label_new(_("Dial Command"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(100);
gtk_entry_set_text(GTK_ENTRY(entry), ext);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
get_pref(PREF_DIAL_COMMAND, NULL, &dial_command);
if (dial_command) {
gtk_entry_set_text(GTK_ENTRY(entry), dial_command);
}
Pdata->entry_command=entry;
/* Button Box */
hbox1 = gtk_hbutton_box_new();
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 7);
gtk_button_box_set_layout(GTK_BUTTON_BOX (hbox1), GTK_BUTTONBOX_END);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
/* Buttons */
button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_1));
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 1);
/* We do this down here because the Pdata structure wasn't complete earlier */
gtk_signal_connect(GTK_OBJECT(checkbox1), "clicked",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_signal_connect(GTK_OBJECT(checkbox2), "clicked",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_signal_connect(GTK_OBJECT(checkbox3), "clicked",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_signal_connect(GTK_OBJECT(Pdata->entry_pre1), "changed",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_signal_connect(GTK_OBJECT(Pdata->entry_pre2), "changed",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_signal_connect(GTK_OBJECT(Pdata->entry_pre3), "changed",
GTK_SIGNAL_FUNC(cb_prefix_change), Pdata);
gtk_widget_show_all(dialog);
gtk_main();
free(Pdata);
return EXIT_SUCCESS;
}