aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-msg-composer-select-file.c
blob: 3726124837f690d7f793300a1d38677545305fe8 (plain) (blame)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 *  Authors: Ettore Perazzoli <ettore@ximian.com>
 *           Jeffrey Stedfast <fejj@ximian.com>
 *       Michael Zucchi <notzed@ximian.com>
 *
 *  Copyright 2002 Ximian, Inc. (www.ximian.com)
 *
 *  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 of the License, 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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtkbox.h>
#include <gtk/gtkcheckbutton.h>
#include <gtk/gtkfilesel.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>

#include <libgnomeui/gnome-uidefs.h>
#include <libgnomeui/gnome-window-icon.h>

#include "e-msg-composer-select-file.h"

static GtkFileSelection *
run_selector(EMsgComposer *composer, const char *title, int multi, gboolean *showinline_p)
{
    GtkFileSelection *selection;
    GtkWidget *showinline = NULL;
    char *path;

    selection = (GtkFileSelection *)gtk_file_selection_new(title);
    gtk_window_set_transient_for((GtkWindow *)selection, (GtkWindow *)composer);
    gtk_window_set_wmclass((GtkWindow *)selection, "fileselection", "Evolution:composer");
    gtk_window_set_modal((GtkWindow *)selection, TRUE);
    gnome_window_icon_set_from_file((GtkWindow *)selection, EVOLUTION_DATADIR "/images/evolution/compose-message.png");
    gtk_file_selection_set_select_multiple((GtkFileSelection *)selection, multi);

    /* restore last path used */
    path = g_object_get_data((GObject *)composer, "attach_path");
    if (path == NULL) {
        path = g_strdup_printf("%s/", g_get_home_dir());
        gtk_file_selection_set_filename(selection, path);
        g_free(path);
    } else {
        gtk_file_selection_set_filename(selection, path);
    }

    if (showinline_p) {
        showinline = gtk_check_button_new_with_label (_("Suggest automatic display of attachment"));
        gtk_widget_show (showinline);
        gtk_box_pack_end (GTK_BOX (selection->main_vbox), showinline, FALSE, FALSE, 4);
    }

    if (gtk_dialog_run((GtkDialog *)selection) == GTK_RESPONSE_OK) {
        if (showinline_p)
            *showinline_p = gtk_toggle_button_get_active((GtkToggleButton *)showinline);
        path = g_path_get_dirname(gtk_file_selection_get_filename(selection));
        g_object_set_data_full((GObject *)composer, "attach_path", g_strdup_printf("%s/", path), g_free);
        g_free(path);
    } else {
        gtk_widget_destroy((GtkWidget *)selection);
        selection = NULL;
    }

    return selection;
}

/**
 * e_msg_composer_select_file:
 * @composer: a composer
 * @title: the title for the file selection dialog box
 *
 * This pops up a file selection dialog box with the given title
 * and allows the user to select a file.
 *
 * Return value: the selected filename, or %NULL if the user
 * cancelled.
 **/
char *
e_msg_composer_select_file (EMsgComposer *composer, const char *title)
{
    GtkFileSelection *selection;
    char *name = NULL;

    selection = run_selector (composer, title, TRUE, NULL);
    if (selection) {
        name = g_strdup(gtk_file_selection_get_filename(selection));
        gtk_widget_destroy((GtkWidget *)selection);
    }

    return name;
}

GPtrArray *
e_msg_composer_select_file_attachments (EMsgComposer *composer, gboolean *showinline_p)
{
    GtkFileSelection *selection;
    GPtrArray *list = NULL;
    char **files;
    int i;

    selection = run_selector(composer, _("Attach file(s)"), TRUE, showinline_p);
    if (selection) {
        files = gtk_file_selection_get_selections(selection);
        
        if (files != NULL) {
            list = g_ptr_array_new ();
            for (i = 0; files[i]; i++)
                g_ptr_array_add (list, g_strdup (files[i]));
            
            g_strfreev (files);
        }
        
        gtk_widget_destroy((GtkWidget *)selection);
    }

    return list;
}