source: trunk/gtk/util.c @ 17

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

Update 2005-12-04

File size: 4.1 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
58char *
59readablesize(guint64 size, int decimals) {
60  const char *sizes[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
61  unsigned int ii;
62  double small = size;
63
64  for(ii = 0; ii + 1 < ALEN(sizes) && 1024.0 <= small / 1024.0; ii++)
65    small /= 1024.0;
66
67  if(1024.0 <= small) {
68    small /= 1024.0;
69    ii++;
70  }
71
72  return g_strdup_printf("%.*f %s", decimals, small, sizes[ii]);
73}
74
75gboolean
76mkdir_p(const char *name, mode_t mode) {
77  struct stat sb;
78  char *parent;
79  gboolean ret;
80  int oerrno;
81
82  if(0 != stat(name, &sb)) {
83    if(ENOENT != errno)
84      return FALSE;
85    parent = g_path_get_dirname(name);
86    ret = mkdir_p(parent, mode);
87    oerrno = errno;
88    g_free(parent);
89    errno = oerrno;
90    return (ret ? (0 == mkdir(name, mode)) : FALSE);
91  }
92
93  if(!S_ISDIR(sb.st_mode)) {
94    errno = ENOTDIR;
95    return FALSE;
96  }
97
98  return TRUE;
99}
100
101GtkWidget *
102errmsg(GtkWindow *wind, const char *format, ...) {
103  GtkWidget *dialog;
104  va_list ap;
105
106  va_start(ap, format);
107  dialog = verrmsg(wind, NULL, NULL, format, ap);
108  va_end(ap);
109
110  return dialog;
111}
112
113GtkWidget *
114errmsg_full(GtkWindow *wind, errfunc_t func, void *data,
115            const char *format, ...) {
116  GtkWidget *dialog;
117  va_list ap;
118
119  va_start(ap, format);
120  dialog = verrmsg(wind, func, data, format, ap);
121  va_end(ap);
122
123  return dialog;
124}
125
126GtkWidget *
127verrmsg(GtkWindow *wind, errfunc_t func, void *data,
128        const char *format, va_list ap) {
129  GtkWidget *dialog;
130  char *msg;
131  GList *funcdata;
132
133  msg = g_strdup_vprintf(format, ap);
134
135  if(NULL == wind)
136    dialog = gtk_message_dialog_new(
137      NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
138  else
139    dialog = gtk_message_dialog_new(wind,
140      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
141      GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
142
143  if(NULL == func)
144    funcdata = NULL;
145  else
146    funcdata = g_list_append(g_list_append(NULL, func), data);
147  g_signal_connect(dialog, "response", G_CALLBACK(errcb), funcdata);
148  if(NULL != wind)
149    gtk_widget_show(dialog);
150  g_free(msg);
151
152  return dialog;
153}
154
155static void
156errcb(GtkWidget *widget, int resp SHUTUP, gpointer data) {
157  GList *funcdata;
158  errfunc_t func;
159
160  if(NULL != data) {
161    funcdata = g_list_first(data);
162    func = funcdata->data;
163    data = funcdata->next->data;
164    func(data);
165    g_list_free(funcdata);
166  }
167
168  gtk_widget_destroy(widget);
169}
Note: See TracBrowser for help on using the repository browser.