summaryrefslogtreecommitdiff
path: root/attic/aaina/libnflick/nflick-pixbuf-fetch.c
blob: 88929079f41bb4af65b5bdc3f8e3f59cb1704e09 (plain)
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
/******************************************************************************/
/*                                                                            */
/* GPL license, Copyright (c) 2005-2006 by:                                   */
/*                                                                            */
/* Authors:                                                                   */
/*      Michael Dominic K. <michaldominik@gmail.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, 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 "nflick-pixbuf-fetch.h"
#include "nflick-pixbuf-fetch-private.h"

GdkPixbuf*                      nflick_pixbuf_fetch_try_cache (const gchar *token)
{
        return NULL;
}

GdkPixbuf*                      nflick_pixbuf_fetch (const gchar *url, gint32 width, gint32 height, const gchar *cache_token)
{
        g_return_val_if_fail (url != NULL, NULL);

        ne_uri *uri = NULL;         /* Neon uri */
        ne_request *request = NULL; /* Http request */
        ne_session *session = NULL; /* Neon session */
        gboolean result = TRUE;     
        GdkPixbuf *pixbuf = NULL;

        /* Allocate new neon uri */
        uri = g_new0 (ne_uri, 1);
        if (uri == NULL) {
                result = FALSE;
                goto Done;
        }

        /* Parse the incoming url into valid neon uri */
        if (ne_uri_parse (url, uri) || uri->host == NULL || uri->path == NULL) {
	        result = FALSE;
                goto Done;
        }

        /* Set defaults. */
        if (uri->scheme == NULL)
                uri->scheme = g_strdup ("http");
        if (uri->port == 0)
                uri->port = ne_uri_defaultport (uri->scheme);

        /* Create the session */
        session = ne_session_create (uri->scheme, uri->host, uri->port);
        if (session == NULL) {
                result = FALSE;
                goto Done;
        }

        /* Create the request */
        request = ne_request_create (session, "GET", uri->path);
        if (request == NULL) {
                result = FALSE;
                goto Done;
        }

        /* Allocate our struct */
        PixbufFetchHelper *helper = g_new0 (PixbufFetchHelper, 1);
        if (helper == NULL) {
                result = FALSE;
                goto Done;
        }
        
        helper->Loader = gdk_pixbuf_loader_new ();
        if (helper->Loader == NULL) {
                result = FALSE;
                goto Done;
        }

        // Open the cache file if applies...
        // FIXME: Move this shit as func param
        
        if (cache_token != NULL && 1) {
                gchar *file_name = NULL;
                file_name = get_cache_file (cache_token);
                if (file_name != NULL) {
                        helper->CacheFile = fopen (file_name, "wb");
                        g_free (file_name);
                }
        }
	
        g_signal_connect (G_OBJECT (helper->Loader), "size-prepared", (gpointer) on_size_prepared, helper);

        helper->Width = width;
        helper->Height = height;

        ne_add_response_body_reader (request, ne_accept_always, (gpointer) block_reader, helper);

        result = (ne_request_dispatch (request) == NE_OK) ? TRUE : FALSE;

        if (helper->CacheFile != NULL)
                fclose (helper->CacheFile);
        gdk_pixbuf_loader_close (helper->Loader, NULL); 
        
        if (result == TRUE) {
                pixbuf = gdk_pixbuf_loader_get_pixbuf (helper->Loader);
                if (pixbuf)
                        g_object_ref (pixbuf);
        } else {
                // FIXME: Remove the cached file
        }

Done:
        if (uri != NULL) {
                ne_uri_free (uri);
                g_free (uri);
        }

        if (session != NULL)
                ne_session_destroy (session);

        if (request != NULL)
                ne_request_destroy (request);

        if (helper != NULL) {
                if (helper->Loader != NULL)
                        g_object_unref (helper->Loader);
                g_free (helper);
        }

        return pixbuf;
}

static gchar*                   get_cache_file (const gchar *token)
{
        g_return_val_if_fail (token != NULL, NULL);

        return g_build_filename ("cache", token, NULL);
}

static int                      block_reader (PixbufFetchHelper *helper, gchar *buffer, int len)
{
        g_return_val_if_fail (helper != NULL, -1);
        g_return_val_if_fail (helper->Loader != NULL, -1);

        if (helper->CacheFile != NULL)
                fwrite (buffer, 1, len, helper->CacheFile);

        gdk_pixbuf_loader_write (helper->Loader, buffer, len, NULL);
        
        return 0; 
}

static void                     on_size_prepared (GdkPixbufLoader *loader, gint width, gint height, PixbufFetchHelper *helper)
{
        g_return_if_fail (helper != NULL);

        if (helper->Width == 0 && helper->Height == 0)
                return;

        if (width != helper->Width && height != helper->Height)
                gdk_pixbuf_loader_set_size (loader, helper->Width, helper->Height);
}