File: helper.c

package info (click to toggle)
balsa 2.3.13-3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,032 kB
  • ctags: 7,261
  • sloc: ansic: 79,348; sh: 8,731; xml: 4,721; makefile: 485; awk: 60
file content (254 lines) | stat: -rw-r--r-- 8,074 bytes parent folder | download | duplicates (3)
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 * Copyright (C) 1997-2002 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * 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, 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#include "config.h"
#include <gnome.h>

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include "helper.h"
#include "i18n.h"
#include "libbalsa.h"
#include "url.h"

/*
 * #ifdef BALSA_LOCAL_INSTALL
 * #define gnome_pixmap_file(s) g_strconcat(BALSA_RESOURCE_PREFIX, "/pixmaps/", s, NULL)
 * #define gnome_unconditional_pixmap_file(s) g_strconcat(BALSA_RESOURCE_PREFIX, "/pixmaps", s, NULL)
 * #endif
 */

/* ************************************************************************** */

static void entry_changed_cb(GtkEntry * entry, EntryData * ed);

/* ************************************************************************** */

GdkPixbuf *
balsa_init_get_png(const gchar * fname)
{
    GdkPixbuf *img;
    GError *err = NULL;
    gchar *fullpath;

    g_return_val_if_fail(fname != NULL, NULL);

    fullpath = balsa_pixmap_finder(fname);

    if (!fullpath)
        return NULL;

    img = gdk_pixbuf_new_from_file(fullpath, &err);
    if (err) {
        g_print(_("Error loading %s: %s\n"), fullpath, err->message);
        g_error_free(err);
    }
    g_free(fullpath);

    return img;
}

void
balsa_init_add_table_entry(GtkTable * table, guint num, gchar * ltext,
                           const gchar * etext, EntryData * ed,
                           GnomeDruid * druid, GtkWidget ** dest)
{
    GtkWidget *l, *e;

    l = gtk_label_new_with_mnemonic(ltext);
    gtk_label_set_justify(GTK_LABEL(l), GTK_JUSTIFY_RIGHT);
    gtk_misc_set_alignment(GTK_MISC(l), 1.0, 0.5);
    gtk_table_attach(table, GTK_WIDGET(l), 0, 1, num + 1, num + 2,
                     GTK_FILL, GTK_FILL, 8, 4);

    e = gtk_entry_new();
    gtk_label_set_mnemonic_widget(GTK_LABEL(l), e);
    gtk_table_attach(table, GTK_WIDGET(e), 1, 2, num + 1, num + 2,
                     GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 8, 4);
    (*dest) = e;
    if(ed) {
        g_signal_connect(G_OBJECT(e), "changed",
                         G_CALLBACK(entry_changed_cb), ed);
        ed->num = ed->master->numentries++;
        ed->druid = druid;
        if (etext && etext[0] != '\0')
            ed->master->setbits |= (1 << num);
        
        ed->master->donemask = (ed->master->donemask << 1) | 1;
    }
    gtk_entry_set_text(GTK_ENTRY(e), etext);
}

static void
entry_changed_cb(GtkEntry * entry, EntryData * ed)
{
    g_assert(ed != NULL);

    if (entry->text_length) {
        ed->master->setbits |= (1 << ed->num);
    } else {
        ed->master->setbits &= ~(1 << ed->num);
    }

    /* The stuff below is only when we are displayed... which is not
     * always the case.
     */
    if (!GTK_WIDGET_VISIBLE(GTK_WIDGET(entry)))
        return;

    if (GNOME_IS_DRUID(ed->druid)) {
        /* Don't let them continue unless all entries have something. */
        if (ENTRY_MASTER_P_DONE(ed->master)) {
            gnome_druid_set_buttons_sensitive(ed->druid, TRUE, TRUE, TRUE,
                                              FALSE);
        } else {
            gnome_druid_set_buttons_sensitive(ed->druid, TRUE, FALSE, TRUE,
                                              FALSE);
        }
    }
}


void
balsa_init_add_table_option(GtkTable *table, guint num,
                            const gchar *ltext, const gchar **optns,
                            GnomeDruid *druid, GtkWidget **dest)
{
    GtkWidget *l, *om;
    int i;
    l = gtk_label_new_with_mnemonic(ltext);
    gtk_label_set_justify(GTK_LABEL(l), GTK_JUSTIFY_RIGHT);
    gtk_misc_set_alignment(GTK_MISC(l), 1.0, 0.5);
    gtk_table_attach(table, l, 0, 1, num + 1, num + 2,
                     GTK_FILL, GTK_FILL, 8, 4);

    *dest = om = gtk_combo_box_new_text();
    for(i=0; optns[i]; i++)
        gtk_combo_box_append_text(GTK_COMBO_BOX(om), _(optns[i]));
    gtk_label_set_mnemonic_widget(GTK_LABEL(l), om);
    gtk_combo_box_set_active(GTK_COMBO_BOX(om), 0);
    gtk_table_attach(table, om, 1, 2, num + 1, num + 2,
                     GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 8, 4);
}

gint
balsa_option_get_active(GtkWidget *option_widget)
{
    return gtk_combo_box_get_active(GTK_COMBO_BOX(option_widget));
}

void
balsa_init_add_table_checkbox(GtkTable *table, guint num,
                              const gchar *ltext, gboolean defval,
                              GnomeDruid *druid, GtkWidget **dest)
{
    GtkWidget *l;

    l = gtk_label_new_with_mnemonic(ltext);
    gtk_label_set_justify(GTK_LABEL(l), GTK_JUSTIFY_RIGHT);
    gtk_misc_set_alignment(GTK_MISC(l), 1.0, 0.5);
    gtk_table_attach(table, l, 0, 1, num + 1, num + 2,
                     GTK_FILL, GTK_FILL, 8, 4);

    *dest = gtk_check_button_new();
    gtk_table_attach(table, *dest, 1, 2, num + 1, num + 2,
                     GTK_FILL, GTK_FILL, 8, 4);
    if(defval)
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*dest), TRUE);
    gtk_label_set_mnemonic_widget(GTK_LABEL(l), *dest);
}

gboolean
balsa_init_create_to_directory(const gchar * dir, gchar ** complaint)
{
    /* Security. Well, we could create some weird directories, but
       a) that's not very destructive and b) unless we have root
       privileges (which would be so, so, wrong) we can't do any
       damage. */
    struct stat sb;
    gchar *sofar;
    guint32 i;
    url_scheme_t scheme = url_check_scheme(dir);

    if (scheme == U_IMAP || scheme == U_POP)
        return FALSE;           /* *** For now */

    if (dir[0] != '/') {
        (*complaint) =
            g_strdup_printf(_
                            ("The path %s must be relative to the filesystem root (start with /)."),
                            dir);
        return TRUE;
    }

    for (i = 1; dir[i] != '\0'; i++) {
        if (dir[i] == '/') {
            sofar = g_strndup(dir, i);

            if (stat(sofar, &sb) < 0) {
                if (mkdir(sofar, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
                    (*complaint) =
                        g_strdup_printf(_
                                        ("Couldn't create a directory: mkdir() failed on pathname \"%s\"."),
                                        sofar);
                    g_free(sofar);
                    return TRUE;
                }
            }

            if (!S_ISDIR(sb.st_mode)) {
                (*complaint) =
                    g_strdup_printf(_
                                    ("The file with pathname \"%s\" is not a directory."),
                                    sofar);
                g_free(sofar);
                return TRUE;
            }

            g_free(sofar);
        }
    }

    if (stat(dir, &sb) < 0) {
        if (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
            (*complaint) =
                g_strdup_printf(_
                                ("Couldn't create a directory: mkdir() failed on pathname \"%s\"."),
                                dir);
            return TRUE;
        }
    }

    if (!S_ISDIR(sb.st_mode)) {
        (*complaint) =
            g_strdup_printf(_
                            ("The file with pathname \"%s\" is not a directory."),
                            dir);
        return TRUE;
    }

    return FALSE;
}