4 * $Id: util.c,v 1.18 1999/08/18 15:29:06 gram Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48 #ifdef NEED_SNPRINTF_H
54 # include "snprintf.h"
59 #include "image/icon-excl.xpm"
60 #include "image/icon-ethereal.xpm"
62 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
64 const gchar *bm_key = "button mask";
66 /* Simple dialog function - Displays a dialog box with the supplied message
70 * type : One of ESD_TYPE_*.
71 * btn_mask : The address of a gint. The value passed in determines if
72 * the 'Cancel' button is displayed. The button pressed by the
73 * user is passed back.
74 * msg_format : Sprintf-style format of the text displayed in the dialog.
75 * ... : Argument list for msg_format
79 #define ESD_MAX_MSG_LEN 2048
81 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
82 GtkWidget *win, *main_vb, *top_hb, *type_pm, *msg_label,
83 *bbox, *ok_btn, *cancel_btn;
89 gchar message[ESD_MAX_MSG_LEN];
93 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
94 gtk_container_border_width(GTK_CONTAINER(win), 7);
98 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
102 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
103 icon = icon_excl_xpm;
107 icon = icon_ethereal_xpm;
108 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
112 gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
114 /* Container for our rows */
115 main_vb = gtk_vbox_new(FALSE, 5);
116 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
117 gtk_container_add(GTK_CONTAINER(win), main_vb);
118 gtk_widget_show(main_vb);
120 /* Top row: Icon and message text */
121 top_hb = gtk_hbox_new(FALSE, 10);
122 gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
123 gtk_widget_show(top_hb);
125 style = gtk_widget_get_style(win);
126 cmap = gdk_colormap_get_system();
127 pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap, &mask,
128 &style->bg[GTK_STATE_NORMAL], icon);
129 type_pm = gtk_pixmap_new(pixmap, mask);
130 gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
131 gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
132 gtk_widget_show(type_pm);
134 /* Load our vararg list into the message string */
135 va_start(ap, msg_format);
136 vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
138 msg_label = gtk_label_new(message);
139 gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
140 gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
141 gtk_widget_show(msg_label);
144 bbox = gtk_hbutton_box_new();
145 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
146 gtk_container_add(GTK_CONTAINER(main_vb), bbox);
147 gtk_widget_show(bbox);
149 ok_btn = gtk_button_new_with_label ("OK");
150 gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
151 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win));
152 gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
153 GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
154 gtk_widget_grab_default(ok_btn);
155 gtk_widget_show(ok_btn);
157 if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
158 cancel_btn = gtk_button_new_with_label("Cancel");
159 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
160 GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
161 gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
162 GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
163 gtk_widget_show(cancel_btn);
167 *btn_mask = ESD_BTN_OK;
169 gtk_widget_show(win);
173 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
174 gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
177 *btn_mask = ESD_BTN_CANCEL;
178 gtk_widget_destroy(GTK_WIDGET(win));
182 setup_tmpdir(char *dir)
184 int len = strlen(dir);
187 /* Append slash if necessary */
188 if (dir[len - 1] == '/') {
192 newdir = g_malloc(len + 2);
200 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
202 static const char suffix[] = "XXXXXXXXXX";
203 int namelen = strlen(namebuf) + strlen(pfx) + sizeof suffix;
205 if (namebuflen < namelen) {
206 errno = ENAMETOOLONG;
209 strcpy(namebuf, dir);
210 strcat(namebuf, pfx);
211 strcat(namebuf, suffix);
212 return mkstemp(namebuf);
215 static char *tmpdir = NULL;
217 static char *temp = NULL;
219 static char *E_tmpdir;
222 #define P_tmpdir "/var/tmp"
226 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
230 static gboolean initialized;
233 if ((dir = getenv("TMPDIR")) != NULL)
234 tmpdir = setup_tmpdir(dir);
236 if ((dir = getenv("TEMP")) != NULL)
237 temp = setup_tmpdir(dir);
240 E_tmpdir = setup_tmpdir(P_tmpdir);
244 if (tmpdir != NULL) {
245 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
252 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
258 fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
262 return try_tempfile(namebuf, namebuflen, "/tmp", pfx);