Changeset 50

Show
Ignore:
Timestamp:
02/12/06 16:48:01
Author:
pvanhoof
Message:

Stream hocus pocus

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libtinymail-camel/tny-camel-stream.c

    r45 r50  
    5050 
    5151 
     52 
     53void 
     54tny_camel_stream_print (CamelStream *stream) 
     55{ 
     56        char tmp_buf[4096]; 
     57        ssize_t nb_read; 
     58         
     59        camel_stream_reset (stream); 
     60 
     61        while (!camel_stream_eos (stream))  
     62        { 
     63                nb_read = camel_stream_read (stream, tmp_buf, sizeof (tmp_buf)); 
     64                g_print ("- (%d) -> %s", nb_read, tmp_buf); 
     65        } 
     66 
     67        g_print ("\n"); 
     68 
     69        return; 
     70} 
     71 
     72static ssize_t 
     73tny_camel_stream_write_to_stream (TnyStreamIface *self, TnyStreamIface *output) 
     74{ 
     75        TnyCamelStreamPriv *priv = TNY_CAMEL_STREAM_GET_PRIVATE (self); 
     76        CamelStream *stream = priv->stream; 
     77 
     78        char tmp_buf[4096]; 
     79        ssize_t total = 0; 
     80        ssize_t nb_read; 
     81        ssize_t nb_written; 
     82g_print ("1 %s\n", __FUNCTION__); 
     83        g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); 
     84        g_return_val_if_fail (TNY_IS_STREAM_IFACE (output), -1); 
     85g_print ("2 %s\n", __FUNCTION__); 
     86        while (!camel_stream_eos (stream)) { 
     87                nb_read = camel_stream_read (stream, tmp_buf, sizeof (tmp_buf)); 
     88                if (nb_read < 0) 
     89                        return -1; 
     90                else if (nb_read > 0) { 
     91                        nb_written = 0; 
     92         
     93                        while (nb_written < nb_read) { 
     94                                ssize_t len = tny_stream_iface_write (output, tmp_buf + nb_written, 
     95                                                                  nb_read - nb_written); 
     96                                if (len < 0) 
     97                                        return -1; 
     98                                nb_written += len; 
     99                        } 
     100                        total += nb_written; 
     101                } 
     102        } 
     103        return total; 
     104} 
     105 
    52106static ssize_t 
    53107tny_camel_stream_read  (TnyStreamIface *self, char *buffer, size_t n) 
     
    159213        klass->eos_func = tny_camel_stream_eos; 
    160214        klass->reset_func = tny_camel_stream_reset; 
     215        klass->write_to_stream_func = tny_camel_stream_write_to_stream; 
    161216 
    162217        return; 
  • trunk/libtinymail-camel/tny-camel-stream.h

    r45 r50  
    5353 
    5454void                    tny_camel_stream_set_stream     (TnyCamelStream *self, CamelStream *stream); 
     55void                    tny_camel_stream_print          (CamelStream *stream); 
    5556 
    5657G_END_DECLS 
  • trunk/libtinymail-camel/tny-msg-priv.h

    r48 r50  
    3030 
    3131        TnyMsgHeaderIface *header; 
    32         TnyStreamIface *stream; 
     32        TnyStreamIface *body_stream; 
    3333 
    3434        GList *attachments; 
  • trunk/libtinymail-camel/tny-msg.c

    r48 r50  
    3535        (G_TYPE_INSTANCE_GET_PRIVATE ((o), TNY_MSG_TYPE, TnyMsgPriv)) 
    3636 
     37typedef gboolean (*CamelPartFunc)(CamelMimeMessage *, CamelMimePart *, void *data); 
     38 
     39static gboolean 
     40message_foreach_part_rec (CamelMimeMessage *msg, CamelMimePart *part, CamelPartFunc callback, void *data) 
     41{ 
     42        CamelDataWrapper *containee; 
     43        int parts, i; 
     44        int go = TRUE; 
     45 
     46        if (callback (msg, part, data) == FALSE) 
     47                return FALSE; 
     48 
     49        containee = camel_medium_get_content_object (CAMEL_MEDIUM (part)); 
     50 
     51        if (containee == NULL) 
     52                return go; 
     53 
     54        /* using the object types is more accurate than using the mime/types */ 
     55        if (CAMEL_IS_MULTIPART (containee)) { 
     56                parts = camel_multipart_get_number (CAMEL_MULTIPART (containee)); 
     57                for (i = 0; go && i < parts; i++) { 
     58                        CamelMimePart *part = camel_multipart_get_part (CAMEL_MULTIPART (containee), i); 
     59 
     60                        go = message_foreach_part_rec (msg, part, callback, data); 
     61                } 
     62        } else if (CAMEL_IS_MIME_MESSAGE (containee)) { 
     63                go = message_foreach_part_rec (msg, (CamelMimePart *)containee, callback, data); 
     64        } 
     65 
     66        return go; 
     67} 
     68 
     69 
     70static gboolean 
     71received_a_part (CamelMimeMessage *message, CamelMimePart *part, void *data) 
     72{ 
     73        TnyMsgPriv *priv = data; 
     74        CamelTransferEncoding encoding = camel_mime_part_get_encoding (part); 
     75 
     76        switch (encoding) 
     77        { 
     78                case CAMEL_TRANSFER_ENCODING_DEFAULT: 
     79                case CAMEL_TRANSFER_ENCODING_7BIT: 
     80                case CAMEL_TRANSFER_ENCODING_8BIT: 
     81                case CAMEL_TRANSFER_ENCODING_QUOTEDPRINTABLE: 
     82                { 
     83                        CamelDataWrapper *wrapper; 
     84                        CamelMedium *medium = CAMEL_MEDIUM (part); 
     85                        CamelStream *stream = camel_stream_mem_new (); 
     86                        wrapper = camel_medium_get_content_object (medium); 
     87                        camel_data_wrapper_write_to_stream (wrapper, stream); 
     88 
     89                        tny_camel_stream_print (stream); 
     90 
     91                        priv->body_stream = TNY_STREAM_IFACE  
     92                                (tny_camel_stream_new (stream)); 
     93 
     94                        /* Loose my own ref (tnycamelstream keeps one) */ 
     95                        camel_object_unref (CAMEL_OBJECT (stream)); 
     96                } break; 
     97 
     98                case CAMEL_TRANSFER_ENCODING_BASE64: 
     99                case CAMEL_TRANSFER_ENCODING_BINARY: 
     100                case CAMEL_TRANSFER_ENCODING_UUENCODE: 
     101                        /* Handle attachments */ 
     102                        break; 
     103 
     104                case CAMEL_TRANSFER_NUM_ENCODINGS: 
     105                default: 
     106                        /* Huh? */ 
     107                        break; 
     108        } 
     109 
     110        return TRUE; 
     111} 
     112 
     113 
    37114void 
    38115_tny_msg_set_camel_mime_message (TnyMsg *self, CamelMimeMessage *message) 
    39116{ 
    40117        TnyMsgPriv *priv = TNY_MSG_GET_PRIVATE (self); 
    41          
    42         /* TODO: Play with priv->stream here */ 
     118        CamelDataWrapper *wrapper; 
     119        CamelMimePart *part; 
     120 
     121        message_foreach_part_rec (message, (CamelMimePart *)message, received_a_part, priv); 
     122 
     123        if (!priv->body_stream) 
     124        { 
     125                g_print ("Message has no body?!\n"); 
     126        } 
    43127 
    44128        return; 
     
    73157 
    74158static const TnyStreamIface* 
    75 tny_msg_get_stream (TnyMsgIface *self) 
    76 { 
    77         TnyMsgPriv *priv = TNY_MSG_GET_PRIVATE (TNY_MSG (self)); 
    78  
    79         return priv->stream; 
     159tny_msg_get_body_stream (TnyMsgIface *self) 
     160{ 
     161        TnyMsgPriv *priv = TNY_MSG_GET_PRIVATE (TNY_MSG (self)); 
     162 
     163        return priv->body_stream; 
    80164} 
    81165 
     
    182266                g_object_unref (G_OBJECT (priv->header)); 
    183267 
    184         if (priv->stream) 
    185                 g_object_unref (G_OBJECT (priv->stream)); 
     268        if (priv->body_stream) 
     269                g_object_unref (G_OBJECT (priv->body_stream)); 
    186270 
    187271        if (priv->attachments)  
     
    214298 
    215299        klass->get_attachments_func = tny_msg_get_attachments; 
    216         klass->get_stream_func = tny_msg_get_stream;           
     300        klass->get_body_stream_func = tny_msg_get_body_stream;                 
    217301        klass->get_header_func = tny_msg_get_header; 
    218302        klass->set_header_func = tny_msg_set_header; 
     
    250334 
    251335        priv->message = NULL; 
    252         priv->stream = NULL; 
     336        priv->body_stream = NULL; 
    253337        priv->attachments = NULL; 
    254338        priv->header = NULL; 
  • trunk/libtinymail/tny-msg-iface.c

    r48 r50  
    3030 **/ 
    3131const TnyStreamIface* 
    32 tny_msg_iface_get_stream (TnyMsgIface *self) 
     32tny_msg_iface_get_body_stream (TnyMsgIface *self) 
    3333{ 
    34         return TNY_MSG_IFACE_GET_CLASS (self)->get_stream_func (self); 
     34        return TNY_MSG_IFACE_GET_CLASS (self)->get_body_stream_func (self); 
    3535} 
    3636 
  • trunk/libtinymail/tny-msg-iface.h

    r48 r50  
    4141 
    4242        const GList*                  (*get_attachments_func) (TnyMsgIface *self); 
    43         const TnyStreamIface*         (*get_stream_func)      (TnyMsgIface *self); 
     43        const TnyStreamIface*         (*get_body_stream_func) (TnyMsgIface *self); 
    4444        const TnyMsgHeaderIface*      (*get_header_func)      (TnyMsgIface *self); 
    4545 
     
    5757 
    5858const GList*                        tny_msg_iface_get_attachments  (TnyMsgIface *self); 
    59 const TnyStreamIface*               tny_msg_iface_get_stream         (TnyMsgIface *self); 
     59const TnyStreamIface*               tny_msg_iface_get_body_stream  (TnyMsgIface *self); 
    6060const TnyMsgHeaderIface*            tny_msg_iface_get_header       (TnyMsgIface *self); 
    6161 
  • trunk/libtinymail/tny-stream-iface.c

    r45 r50  
    2020#include <tny-stream-iface.h> 
    2121 
     22ssize_t 
     23tny_stream_iface_write_to_stream (TnyStreamIface *self, TnyStreamIface *output) 
     24{ 
     25        return TNY_STREAM_IFACE_GET_CLASS (self)->write_to_stream_func (self, output); 
     26} 
    2227 
    2328ssize_t 
  • trunk/libtinymail/tny-stream-iface.h

    r45 r50  
    4646        gboolean  (*eos_func)   (TnyStreamIface *self); 
    4747        gint      (*reset_func) (TnyStreamIface *self); 
     48        ssize_t   (*write_to_stream_func) (TnyStreamIface *self, TnyStreamIface *output); 
    4849}; 
    4950 
     
    5859gboolean  tny_stream_iface_eos   (TnyStreamIface *self); 
    5960gint      tny_stream_iface_reset (TnyStreamIface *self); 
     61ssize_t   tny_stream_iface_write_to_stream (TnyStreamIface *self, TnyStreamIface *output); 
    6062 
    6163G_END_DECLS 
  • trunk/libtinymailui/Makefile.am

    r32 r50  
    99        tny-account-tree-model.h        \ 
    1010        tny-summary-window-iface.h      \ 
     11        tny-text-buffer-stream.h        \ 
    1112        tny-msg-window-iface.h 
    1213 
     
    1617        tny-account-tree-model.c        \ 
    1718        tny-summary-window-iface.c      \ 
     19        tny-text-buffer-stream.c        \ 
    1820        tny-msg-window-iface.c 
    1921 
  • trunk/tinymail/tny-msg-window.c

    r48 r50  
    2121#include <gtk/gtk.h> 
    2222#include <tny-msg-window.h> 
     23#include <tny-text-buffer-stream.h> 
    2324 
    2425static GObjectClass *parent_class = NULL; 
     
    4142        TnyMsgWindowPriv *priv = TNY_MSG_WINDOW_GET_PRIVATE (self); 
    4243        GtkTextBuffer *buffer = gtk_text_view_get_buffer (priv->textview); 
    43  
     44        TnyStreamIface *dest = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buffer)); 
     45        TnyStreamIface *source; 
    4446        TnyMsgHeaderIface *header; 
    4547        GList *attachments; 
    46         const gchar *text; 
    4748 
    4849        header = TNY_MSG_HEADER_IFACE (tny_msg_iface_get_header (priv->msg)); 
     50        source = (TnyStreamIface*)tny_msg_iface_get_body_stream (priv->msg); 
    4951        attachments = (GList*)tny_msg_iface_get_attachments (priv->msg); 
    5052 
    5153        gtk_window_set_title (GTK_WINDOW (self), tny_msg_header_iface_get_subject (header)); 
    5254 
    53         /* TODO: Use stream */ 
    54         gtk_text_buffer_set_text (buffer, tny_msg_header_iface_get_subject (header), strlen(tny_msg_header_iface_get_subject (header))); 
     55        tny_stream_iface_reset (source); 
     56        tny_stream_iface_reset (dest); 
     57 
     58        tny_stream_iface_write_to_stream (source, dest); 
    5559 
    5660        return;