root/trunk/libtinymailui/tny-mime-part-save-strategy.c
| Revision 3666 (checked in by jdapena, 6 months ago) |
|---|
| Line | |
|---|---|
| 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., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * TnyMimePartSaveStrategy: |
| 22 | * |
| 23 | * A strategy for saving a #TnyMimePart |
| 24 | * |
| 25 | * free-function: g_object_unref |
| 26 | **/ |
| 27 | |
| 28 | #include <config.h> |
| 29 | |
| 30 | #include <tny-mime-part-save-strategy.h> |
| 31 | |
| 32 | /** |
| 33 | * tny_mime_part_save_strategy_perform_save: |
| 34 | * @self: a #TnyMimePartSaveStrategy |
| 35 | * @part: a #TnyMimePart that must be saved |
| 36 | * |
| 37 | * With @self being a delegate of a #TnyMimePartSaver, this method performs the |
| 38 | * saving of @part. |
| 39 | * |
| 40 | * A save strategy for a mime part is used with a type that implements the |
| 41 | * #TnyMimePartSaver interface. Very often are such types also implementing the |
| 42 | * #TnyMsgView and/or #TnyMimePartView interfaces (although it's not a |
| 43 | * requirement). When implementing #TnyMimePartSaver you say that the view has |
| 44 | * functionality for saving mime parts. |
| 45 | * |
| 46 | * Example: |
| 47 | * <informalexample><programlisting> |
| 48 | * static void |
| 49 | * tny_my_msg_view_save (TnyMimePartView *self_i, TnyMimePart *attachment) |
| 50 | * { |
| 51 | * TnyMyMsgView *self = TNY_MY_MSG_VIEW (self_i); |
| 52 | * tny_mime_part_save_strategy_perform_save (self->mime_part_save_strategy, |
| 53 | * attachment); |
| 54 | * } |
| 55 | * </programlisting></informalexample> |
| 56 | * |
| 57 | * Devices can have specific strategies that are changed at runtime. For example |
| 58 | * a save-strategy that sends the content of the mime part it to another computer |
| 59 | * and/or a save-strategy that saves it to a flash disk. Configurable at runtime |
| 60 | * by simply switching the save-strategy property of a #TnyMimePartSaver. |
| 61 | * |
| 62 | * Example: |
| 63 | * <informalexample><programlisting> |
| 64 | * static void |
| 65 | * tny_gtk_mime_part_save_strategy_perform_save (TnyMimePartSaveStrategy *self, TnyMimePart *part) |
| 66 | * { |
| 67 | * GtkFileChooserDialog *dialog; |
| 68 | * dialog = GTK_FILE_CHOOSER_DIALOG |
| 69 | * (gtk_file_chooser_dialog_new (_("Save attachment"), NULL, |
| 70 | * GTK_FILE_CHOOSER_ACTION_MIME_PART_SAVE, |
| 71 | * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_MIME_PART_SAVE, |
| 72 | * GTK_RESPONSE_ACCEPT, NULL)); |
| 73 | * gtk_file_chooser_set_current_name (dialog, |
| 74 | * tny_mime_part_get_filename (part)); |
| 75 | * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { |
| 76 | * gchar *uri; int fd; |
| 77 | * uri = gtk_file_chooser_get_filename (dialog); |
| 78 | * fd = open (uri, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); |
| 79 | * if (fd != -1) { |
| 80 | * TnyStream *stream = tny_fs_stream_new (fd); |
| 81 | * tny_mime_part_decode_to_stream (part, stream, NULL); |
| 82 | * g_object_unref (stream); |
| 83 | * } |
| 84 | * } |
| 85 | * gtk_widget_destroy (GTK_WIDGET (dialog)); |
| 86 | * } |
| 87 | * </programlisting></informalexample> |
| 88 | * |
| 89 | * An example when to use this method is in a clicked handler of a popup menu |
| 90 | * of a attachment #TnyMimePartView in your #TnyMsgView. |
| 91 | * |
| 92 | * Note that a mime part can mean both the entire message (without its headers) |
| 93 | * and one individual mime part in such a message or a message in a message (in |
| 94 | * case of a messge/rfc822 mime part). |
| 95 | * |
| 96 | * since: 1.0 |
| 97 | * audience: application-developer, type-implementer |
| 98 | **/ |
| 99 | void |
| 100 | tny_mime_part_save_strategy_perform_save (TnyMimePartSaveStrategy *self, TnyMimePart *part) |
| 101 | { |
| 102 | #ifdef DEBUG |
| 103 | if (!TNY_MIME_PART_SAVE_STRATEGY_GET_IFACE (self)->perform_save) |
| 104 | g_critical ("You must implement tny_mime_part_save_strategy_perform_save\n"); |
| 105 | #endif |
| 106 | |
| 107 | TNY_MIME_PART_SAVE_STRATEGY_GET_IFACE (self)->perform_save(self, part); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | tny_mime_part_save_strategy_base_init (gpointer g_class) |
| 113 | { |
| 114 | static gboolean initialized = FALSE; |
| 115 | |
| 116 | if (!initialized) |
| 117 | initialized = TRUE; |
| 118 | } |
| 119 | |
| 120 | static gpointer |
| 121 | tny_mime_part_save_strategy_register_type (gpointer notused) |
| 122 | { |
| 123 | GType type = 0; |
| 124 | |
| 125 | static const GTypeInfo info = |
| 126 | { |
| 127 | sizeof (TnyMimePartSaveStrategyIface), |
| 128 | tny_mime_part_save_strategy_base_init, /* base_init */ |
| 129 | NULL, /* base_finalize */ |
| 130 | NULL, /* class_init */ |
| 131 | NULL, /* class_finalize */ |
| 132 | NULL, /* class_data */ |
| 133 | 0, |
| 134 | 0, /* n_preallocs */ |
| 135 | NULL /* instance_init */ |
| 136 | }; |
| 137 | type = g_type_register_static (G_TYPE_INTERFACE, |
| 138 | "TnyMimePartSaveStrategy", &info, 0); |
| 139 | |
| 140 | g_type_interface_add_prerequisite (type, G_TYPE_OBJECT); |
| 141 | |
| 142 | return GUINT_TO_POINTER (type); |
| 143 | } |
| 144 | |
| 145 | GType |
| 146 | tny_mime_part_save_strategy_get_type (void) |
| 147 | { |
| 148 | static GOnce once = G_ONCE_INIT; |
| 149 | g_once (&once, tny_mime_part_save_strategy_register_type, NULL); |
| 150 | return GPOINTER_TO_UINT (once.retval); |
| 151 | } |
Note: See TracBrowser for help on using the browser.
