~ubuntu-branches/ubuntu/karmic/libtinymail/karmic

« back to all changes in this revision

Viewing changes to libtinymailui/tny-mime-part-save-strategy.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-10-12 11:21:12 UTC
  • Revision ID: james.westby@ubuntu.com-20071012112112-fod9fs7yrooxjr7i
Tags: upstream-0.0.2
ImportĀ upstreamĀ versionĀ 0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libtinymail - The Tiny Mail base library
 
2
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with self library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <tny-mime-part-save-strategy.h>
 
23
 
 
24
/**
 
25
 * tny_mime_part_save_strategy_perform_save:
 
26
 * @self: A #TnyMimePartSaveStrategy instance
 
27
 * @part: The #TnyMimePart instance that must be saved
 
28
 *
 
29
 * With @self being a delegate of a #TnyMimePartSaver, this method performs the
 
30
 * saving of @part.
 
31
 *
 
32
 * A save strategy for a mime part is used with a type that implements the 
 
33
 * #TnyMimePartSaver interface. Types that do, will often also implement the 
 
34
 * #TnyMsgView or #TnyMimePartView interface (it's not a requirement). In this
 
35
 * case they say that the view has functionality for saving mime parts.
 
36
 *
 
37
 * You can for example inherit an implementation of a #TnyMsgView, like the 
 
38
 * #TnyGtkMsgView one, and let yours also implement #TnyMimePartSaver. The 
 
39
 * example shown here is for example such a situation.
 
40
 *
 
41
 * Example:
 
42
 * <informalexample><programlisting>
 
43
 * static void 
 
44
 * tny_my_msg_view_save (TnyMimePartView *self_i, TnyMimePart *attachment)
 
45
 * {
 
46
 *     TnyMyMsgView *self = TNY_MY_MSG_VIEW (self_i);
 
47
 *     tny_mime_part_save_strategy_perform_save (self->mime_part_save_strategy, attachment);
 
48
 * }
 
49
 * </programlisting></informalexample>
 
50
 *
 
51
 * Implementors: The idea is that devices can have specific strategies that can
 
52
 * be changed at runtime.
 
53
 *
 
54
 * For example a strategy that sends it to another computer and/or a strategy
 
55
 * that saves it to a flash disk. Configurable at runtime by simply switching
 
56
 * the strategy property of a #TnyMimePartSaver.
 
57
 *
 
58
 * The implementation shown in this example implements it using the gtk+ toolkit.
 
59
 * If your device doesn't support saving mime parts, saving a mime part can also
 
60
 * be implemented by doing nothing. For example a #TnyMyDoNothingSaveStrategy. 
 
61
 * Maybe you will implement it by letting it contact a service and sending the
 
62
 * mime part to it? It's up to you.
 
63
 *
 
64
 * Example:
 
65
 * <informalexample><programlisting>
 
66
 * static void
 
67
 * tny_gtk_mime_part_save_strategy_perform_save (TnyMimePartSaveStrategy *self, TnyMimePart *part)
 
68
 * {
 
69
 *      GtkFileChooserDialog *dialog;
 
70
 *      dialog = GTK_FILE_CHOOSER_DIALOG 
 
71
 *            (gtk_file_chooser_dialog_new (_("MimePartSave File"), NULL,
 
72
 *            GTK_FILE_CHOOSER_ACTION_MIME_PART_SAVE,
 
73
 *            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_MIME_PART_SAVE, 
 
74
 *            GTK_RESPONSE_ACCEPT, NULL));
 
75
 *      gtk_file_chooser_set_current_name (dialog, 
 
76
 *                    tny_mime_part_get_filename (part));
 
77
 *      if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) 
 
78
 *      {
 
79
 *            gchar *uri; int fd;
 
80
 *            uri = gtk_file_chooser_get_filename (dialog);
 
81
 *            fd = open (uri, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
 
82
 *            if (fd != -1) {
 
83
 *                      TnyStream *stream = tny_fs_stream_new (fd);
 
84
 *                      tny_mime_part_decode_to_stream (part, TNY_STREAM (stream));
 
85
 *                      g_object_unref (G_OBJECT (stream));
 
86
 *            }
 
87
 *      }
 
88
 *      gtk_widget_destroy (GTK_WIDGET (dialog));
 
89
 * }
 
90
 * </programlisting></informalexample>
 
91
 *
 
92
 * The method is typically called by the implementation of a #TnyMsgView.
 
93
 * For example a clicked handler of a popup menu of a attachment view in your
 
94
 * #TnyMsgView implementation.
 
95
 * 
 
96
 * Note that a mime part can mean both the entire message (without its headers)
 
97
 * and one individual mime part in such a message or a message in a message (in 
 
98
 * case of a messge/rfc822 mime part).
 
99
 *
 
100
 * A #TnyMsg inherits from #TnyMimePart which means that if you use the message
 
101
 * instance with a #TnyMimePartSaveStrategy instance that the strategy for
 
102
 * saving it must save the entire message. Whereas when you pass it just one
 
103
 * individual mime part instance, the strategy must save only that part.
 
104
 **/
 
105
void
 
106
tny_mime_part_save_strategy_perform_save (TnyMimePartSaveStrategy *self, TnyMimePart *part)
 
107
{
 
108
#ifdef DEBUG
 
109
        if (!TNY_MIME_PART_SAVE_STRATEGY_GET_IFACE (self)->perform_save_func)
 
110
                g_critical ("You must implement tny_mime_part_save_strategy_perform_save\n");
 
111
#endif
 
112
 
 
113
        TNY_MIME_PART_SAVE_STRATEGY_GET_IFACE (self)->perform_save_func (self, part);
 
114
        return;
 
115
}
 
116
 
 
117
static void
 
118
tny_mime_part_save_strategy_base_init (gpointer g_class)
 
119
{
 
120
        static gboolean initialized = FALSE;
 
121
 
 
122
        if (!initialized) 
 
123
                initialized = TRUE;
 
124
}
 
125
 
 
126
GType
 
127
tny_mime_part_save_strategy_get_type (void)
 
128
{
 
129
        static GType type = 0;
 
130
 
 
131
        if (G_UNLIKELY(type == 0))
 
132
        {
 
133
                static const GTypeInfo info = 
 
134
                {
 
135
                  sizeof (TnyMimePartSaveStrategyIface),
 
136
                  tny_mime_part_save_strategy_base_init,   /* base_init */
 
137
                  NULL,   /* base_finalize */
 
138
                  NULL,   /* class_init */
 
139
                  NULL,   /* class_finalize */
 
140
                  NULL,   /* class_data */
 
141
                  0,
 
142
                  0,      /* n_preallocs */
 
143
                  NULL    /* instance_init */
 
144
                };
 
145
                type = g_type_register_static (G_TYPE_INTERFACE, 
 
146
                        "TnyMimePartSaveStrategy", &info, 0);
 
147
 
 
148
                g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
 
149
        }
 
150
 
 
151
        return type;
 
152
}