aboutsummaryrefslogtreecommitdiffstats
path: root/executive-summary/component/e-summary-url.c
blob: 856297bb68e8382eaa419e524a0bd78dcb8831b3 (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-summary-url.c
 *
 * Authors: Iain Holmes <iain@helixcode.com>
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * 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 Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

#include <gnome.h>
#include <bonobo.h>

#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-stream.h>
#include <gal/util/e-util.h>

#include <liboaf/liboaf.h>

#include <libgnomevfs/gnome-vfs.h>
#include "e-summary.h"
#include "e-summary-url.h"
#include "e-summary-util.h"

#include "Composer.h"

typedef enum _ESummaryProtocol {
    PROTOCOL_NONE,
    PROTOCOL_HTTP,
    PROTOCOL_MAILTO,
    PROTOCOL_VIEW,
    PROTOCOL_EXEC,
    PROTOCOL_FILE,
    PROTOCOL_OTHER
} ESummaryProtocol;

#define COMPOSER_IID "OAFIID:evolution-composer:evolution-mail:cd8618ea-53e1-4b9e-88cf-ec578bdb903b"

void
e_summary_url_request (GtkHTML *html,
               const gchar *url,
               GtkHTMLStream *stream)
{
    char *filename;
    GnomeVFSHandle *handle = NULL;
    GnomeVFSResult result;

    if (strncasecmp (url, "file:", 5) == 0) {
        url += 5;
        filename = e_pixmap_file (url);
    } else if (strchr (url, ':') >= strchr (url, '/')) {
        filename = e_pixmap_file (url);
    } else
        filename = g_strdup (url);

    if (filename == NULL) {
        gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
        return;
    }
    
    result = gnome_vfs_open (&handle, filename, GNOME_VFS_OPEN_READ);
    
    if (result != GNOME_VFS_OK) {
        g_warning ("%s: %s", filename, 
               gnome_vfs_result_to_string (result));
        g_free (filename);
        gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
        return;
    }

    g_free (filename);
    while (1) {
        char buffer[4096];
        GnomeVFSFileSize size;

        /* Clear buffer */
        memset (buffer, 0x00, 4096);

        result = gnome_vfs_read (handle, buffer, 4096, &size);
        if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
            g_warning ("Error reading data: %s", 
                   gnome_vfs_result_to_string (result));
            gnome_vfs_close (handle);
            gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
        }

        if (size == 0)
            break; /* EOF */

        gtk_html_stream_write (stream, buffer, size);
    }
    
    gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
    gnome_vfs_close (handle);
}

static ESummaryProtocol
get_protocol (const char *url)
{
    char *lowerurl;
    ESummaryProtocol protocol = PROTOCOL_OTHER;
    
    lowerurl = g_strdup (url);
    g_strdown (lowerurl);
    
    /* Check for no :/ */
    if (strstr (lowerurl, "://") == NULL) {
        
        /* Annoying alternative for mailto URLs */
        if (strncmp (lowerurl, "mailto:", 6) != 0) {
            g_free (lowerurl);
            return PROTOCOL_NONE;
        } else {
            g_free (lowerurl);
            return PROTOCOL_MAILTO;
        }
    }
    
    switch (lowerurl[0]) {
    case 'h':
        if (strncmp (lowerurl + 1, "ttp", 3) == 0)
            protocol = PROTOCOL_HTTP;
        break;
        
    case 'm':
        if (strncmp (lowerurl + 1, "ailto", 5) == 0)
            protocol = PROTOCOL_MAILTO;
        break;
        
    case 'v':
        if (strncmp (lowerurl + 1, "iew", 3) == 0)
            protocol = PROTOCOL_VIEW;
        break;
        
    case 'e':
        if (strncmp (lowerurl + 1, "xec", 3) == 0)
            protocol = PROTOCOL_EXEC;
        break;
        
    case 'f':
        if (strncmp (lowerurl + 1, "ile", 3) == 0)
            protocol = PROTOCOL_FILE;
        break;
        
    default:
        break;
    }
    
    g_free (lowerurl);
    
    return protocol;
}

void
e_summary_url_click (GtkWidget *widget,
             const char *url,
             ESummary *esummary)
{
    ESummaryProtocol protocol;
    g_print ("URL: %s\n", url);
    
    protocol = get_protocol (url);
    
    switch (protocol) {
    case PROTOCOL_MAILTO:
        /* Open a composer window */
        e_summary_url_mail_compose (esummary, url); 
        break;
        
    case PROTOCOL_VIEW:
        /* Change the EShellView's current uri */
        break;
        
    case PROTOCOL_EXEC:
        /* Execute the rest of the url */
        e_summary_url_exec (url + 7);
        break;
        
    case PROTOCOL_NONE:
    case PROTOCOL_OTHER:
    case PROTOCOL_HTTP:
    case PROTOCOL_FILE:
    default:
        /* Let browser handle it */
        gnome_url_show (url);
        break;
        
    }      
}

gboolean
e_summary_url_mail_compose (ESummary *esummary,
                const char *url)
{
    CORBA_Object composer;
    CORBA_Environment ev;
    Evolution_Composer_RecipientList *to, *cc, *bcc;
    Evolution_Composer_Recipient *recipient;
    char *address, *proto;
    CORBA_char *subject;
    
    CORBA_exception_init (&ev);
    
    /* FIXME: Query for IIDs? */
    composer = oaf_activate_from_id ((char *)COMPOSER_IID, 0, NULL, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        g_warning ("Unable to start composer component!");
        return FALSE;
    }
    
    if ( (proto = strstr (url, "://")) != NULL){
        address = proto + 3;
    } else {
        address = url + 7;
    }
    
    to = Evolution_Composer_RecipientList__alloc ();
    to->_length = 1;
    to->_maximum = 1;
    to->_buffer = CORBA_sequence_Evolution_Composer_Recipient_allocbuf (to->_maximum);
    
    recipient = to->_buffer;
    recipient->name = CORBA_string_dup ("");
    recipient->address = CORBA_string_dup (address?address:"");
    
    /* FIXME: Get these out of the URL */
    cc = Evolution_Composer_RecipientList__alloc ();
    cc->_length = 0;
    cc->_maximum = 0;
    cc->_buffer = CORBA_sequence_Evolution_Composer_Recipient_allocbuf (cc->_maximum);
    
    bcc = Evolution_Composer_RecipientList__alloc ();
    bcc->_length = 0;
    bcc->_maximum = 0;
    bcc->_buffer = CORBA_sequence_Evolution_Composer_Recipient_allocbuf (bcc->_maximum);
    
    subject = CORBA_string_dup ("");
    
    CORBA_exception_init (&ev);
    Evolution_Composer_set_headers (composer, to, cc, bcc, subject, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        CORBA_free (to);
        g_warning ("%s(%d): Error setting headers", __FUNCTION__, __LINE__);
        return FALSE;
    }
    
    CORBA_free (to);
    
    CORBA_exception_init (&ev);  
    Evolution_Composer_show (composer, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        g_warning ("%s(%d): Error showing composer", __FUNCTION__, __LINE__);
        return FALSE;
    }
    
    CORBA_exception_free (&ev);
    
    /* FIXME: Free the composer? */
    
    return TRUE;
}

gboolean
e_summary_url_exec (const char *exec)
{
    gchar **exec_array;
    int argc;
    
    exec_array = g_strsplit (exec, " ", 0);
    
    argc = 0;
    while (exec_array[argc] != NULL) {
        argc++;
    }
    
    gnome_execute_async (NULL, argc, exec_array);
    
    g_strfreev (exec_array);
}