source: trunk/gtk/util.c @ 7

Last change on this file since 7 was 7, checked in by root, 16 years ago

Update 2005-11-24

File size: 3.7 KB
Line 
1/*
2  Copyright (c) 2005 Joshua Elsasser. All rights reserved.
3   
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7   
8   1. Redistributions of source code must retain the above copyright
9      notice, this list of conditions and the following disclaimer.
10   2. Redistributions in binary form must reproduce the above copyright
11      notice, this list of conditions and the following disclaimer in the
12      documentation and/or other materials provided with the distribution.
13   
14  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS "AS IS" AND
15  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <errno.h>
30#include <stdarg.h>
31#include <string.h>
32
33#include <gtk/gtk.h>
34
35#include "util.h"
36
37static void
38errcb(GtkWidget *wind, int resp, gpointer data);
39
40gboolean
41strbool(const char *str) {
42  switch(str[0]) {
43    case 'y':
44    case 'Y':
45    case '1':
46    case 'j':
47    case 'e':
48      return TRUE;
49    default:
50      if(0 == g_ascii_strcasecmp("on", str))
51        return TRUE;
52      break;
53  }
54
55  return FALSE;
56}
57
58gboolean
59mkdir_p(const char *name, mode_t mode) {
60  struct stat sb;
61  char *parent;
62  gboolean ret;
63  int oerrno;
64
65  if(0 != stat(name, &sb)) {
66    if(ENOENT != errno)
67      return FALSE;
68    parent = g_path_get_dirname(name);
69    ret = mkdir_p(parent, mode);
70    oerrno = errno;
71    g_free(parent);
72    errno = oerrno;
73    return (ret ? (0 == mkdir(name, mode)) : FALSE);
74  }
75
76  if(!S_ISDIR(sb.st_mode)) {
77    errno = ENOTDIR;
78    return FALSE;
79  }
80
81  return TRUE;
82}
83
84GtkWidget *
85errmsg(GtkWindow *wind, const char *format, ...) {
86  GtkWidget *dialog;
87  va_list ap;
88
89  va_start(ap, format);
90  dialog = verrmsg(wind, NULL, NULL, format, ap);
91  va_end(ap);
92
93  return dialog;
94}
95
96GtkWidget *
97errmsg_full(GtkWindow *wind, errfunc_t func, void *data,
98            const char *format, ...) {
99  GtkWidget *dialog;
100  va_list ap;
101
102  va_start(ap, format);
103  dialog = verrmsg(wind, func, data, format, ap);
104  va_end(ap);
105
106  return dialog;
107}
108
109GtkWidget *
110verrmsg(GtkWindow *wind, errfunc_t func, void *data,
111        const char *format, va_list ap) {
112  GtkWidget *dialog;
113  char *msg;
114  GList *funcdata;
115
116  msg = g_strdup_vprintf(format, ap);
117
118  if(NULL == wind)
119    dialog = gtk_message_dialog_new(
120      NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
121  else
122    dialog = gtk_message_dialog_new(wind,
123      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
124      GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
125
126  if(NULL == func)
127    funcdata = NULL;
128  else
129    funcdata = g_list_append(g_list_append(NULL, func), data);
130  g_signal_connect(dialog, "response", G_CALLBACK(errcb), funcdata);
131  if(NULL != wind)
132    gtk_widget_show(dialog);
133  g_free(msg);
134
135  return dialog;
136}
137
138static void
139errcb(GtkWidget *widget, int resp SHUTUP, gpointer data) {
140  GList *funcdata;
141  errfunc_t func;
142
143  if(NULL != data) {
144    funcdata = g_list_first(data);
145    func = funcdata->data;
146    data = funcdata->next->data;
147    func(data);
148    g_list_free(funcdata);
149  }
150
151  gtk_widget_destroy(widget);
152}
Note: See TracBrowser for help on using the repository browser.