aboutsummaryrefslogtreecommitdiffstats
path: root/tools/evolution-launch-composer.c
blob: 834ba2ff1510526b7b9ae9cd742f894f8ee39b66 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * evolution-sendmail.c - 
 * Copyright (C) 2002, Ximian, Inc.
 *
 * Authors:
 *   Christopher James Lahey <clahey@ximian.com>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This file 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 file; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 **/

#include <config.h>

#include <Evolution-Composer.h>
#include <bonobo/bonobo-object-client.h>
#include <popt.h>
#include <gal/util/e-util.h>
#include <gal/util/e-i18n.h>
#include <liboaf/liboaf.h>
#include <gtk/gtkmain.h>

#include <bonobo/bonobo-main.h>

#include <libgnomeui/gnome-init.h>

#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-xfer.h>
#include <libgnomevfs/gnome-vfs-init.h>

#define READ_CHUNK_SIZE 8192

#define E_SHELL_OAFIID  "OAFIID:GNOME_Evolution_Shell"
#define COMPOSER_OAFIID "OAFIID:GNOME_Evolution_Mail_Composer"
#define NOTNULL(x) ((x) ? (x) : "")

typedef struct {
       char *filename;
       char *basename;
       char *content;
       int   size;
       char *description;
       char *content_type;
       gboolean show_inline;
} attachment_t;

GList *attachments; /* Of type attachment_t */

char *subject;

static void
free_attachment (attachment_t *attachment)
{
       g_free (attachment->content);
       g_free (attachment->filename);
       g_free (attachment->basename);
       g_free (attachment->content_type);
       g_free (attachment->description);
       g_free (attachment);
}

GnomeVFSResult
elc_read_entire_file (const char *uri,
                     int *file_size,
                     char **file_contents,
                     char **content_type)
{
       GnomeVFSResult result;
       GnomeVFSHandle *handle;
       char *buffer;
       GnomeVFSFileSize total_bytes_read;
       GnomeVFSFileSize bytes_read;

       *file_size = 0;
       *file_contents = NULL;

       /* Open the file. */
       result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
       if (result != GNOME_VFS_OK) {
               return result;
       }

       /* Read the whole thing. */
       buffer = NULL;
       total_bytes_read = 0;
       do {
               buffer = g_realloc (buffer, total_bytes_read + READ_CHUNK_SIZE);
               result = gnome_vfs_read (handle,
                                        buffer + total_bytes_read,
                                        READ_CHUNK_SIZE,
                                        &bytes_read);
               if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
                       g_free (buffer);
                       gnome_vfs_close (handle);
                       return result;
               }

               /* Check for overflow. */
               if (total_bytes_read + bytes_read < total_bytes_read) {
                       g_free (buffer);
                       gnome_vfs_close (handle);
                       return GNOME_VFS_ERROR_TOO_BIG;
               }

               total_bytes_read += bytes_read;
       } while (result == GNOME_VFS_OK);

       if (content_type) {
               GnomeVFSFileInfo *info;

               info = gnome_vfs_file_info_new ();
               result = gnome_vfs_get_file_info_from_handle (handle, info,
                                                             GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
                                                             GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
               if (result == GNOME_VFS_OK)
                       *content_type = g_strdup (gnome_vfs_file_info_get_mime_type (info));
               else
                       *content_type = g_strdup ("application/octet-stream");

               gnome_vfs_file_info_unref (info);
       }

       /* Close the file. */
       result = gnome_vfs_close (handle);
       if (result != GNOME_VFS_OK) {
               g_free (buffer);
               return result;
       }

       /* Return the file. */
       *file_size = total_bytes_read;
       *file_contents = g_realloc (buffer, total_bytes_read);
       return GNOME_VFS_OK;
}


static void
do_send (BonoboObjectClient *bonobo_server)
{
       GNOME_Evolution_Composer composer_server;
       CORBA_Environment ev;

       GNOME_Evolution_Composer_AttachmentData *attach_data;

       GNOME_Evolution_Composer_RecipientList *to_list, *cc_list, *bcc_list;

       composer_server = bonobo_object_corba_objref (BONOBO_OBJECT (bonobo_server));

       CORBA_exception_init (&ev);

       attachments = g_list_reverse (attachments);

       while (attachments) {
               attachment_t *attachment = attachments->data;
               GList *temp;

               attach_data = GNOME_Evolution_Composer_AttachmentData__alloc();
               attach_data->_maximum = attach_data->_length = attachment->size;
               attach_data->_buffer = CORBA_sequence_CORBA_char_allocbuf (attach_data->_length);
               strcpy (attach_data->_buffer, attachment->content);

               GNOME_Evolution_Composer_attachData (composer_server, 
                                                    NOTNULL (attachment->content_type),
                                                    NOTNULL (attachment->basename),
                                                    NOTNULL (attachment->description),
                                                    attachment->show_inline,
                                                    attach_data,
                                                    &ev);

               if (ev._major != CORBA_NO_EXCEPTION) {
                       g_printerr ("evolution-sendmail.c: I couldn't attach data to the composer via CORBA! Aagh.\n");
                       CORBA_exception_free (&ev);
                       return;
               }

               CORBA_free (attach_data);

               free_attachment (attachment);

               temp = attachments;
               attachments = g_list_remove_link (attachments, attachments);
               g_list_free_1 (temp);
       }

       to_list = GNOME_Evolution_Composer_RecipientList__alloc ();
       to_list->_maximum = to_list->_length = 0;

       cc_list = GNOME_Evolution_Composer_RecipientList__alloc ();
       cc_list->_maximum = cc_list->_length = 0;

       bcc_list = GNOME_Evolution_Composer_RecipientList__alloc ();
       bcc_list->_maximum = bcc_list->_length = 0;

       GNOME_Evolution_Composer_setHeaders (composer_server, "", to_list, cc_list, bcc_list, NOTNULL (subject), &ev);

       CORBA_free (to_list);
       CORBA_free (cc_list);
       CORBA_free (bcc_list);

       GNOME_Evolution_Composer_show (composer_server, &ev);

       if (ev._major != CORBA_NO_EXCEPTION) {
               g_printerr ("evolution-sendmail.c: I couldn't show the composer via CORBA! Aagh.\n");
               CORBA_exception_free (&ev);
               return;
       }

       CORBA_exception_free (&ev);
}


static BonoboObjectClient *
get_composer ()
{
       BonoboObjectClient *bonobo_server;

       /* First, I obtain an object reference that represents the Shell, to make sure it's running. */
       bonobo_server = bonobo_object_activate (E_SHELL_OAFIID, 0);

       g_return_val_if_fail (bonobo_server != NULL, NULL);

       /* Next, I obtain an object reference that represents the Composer. */
       bonobo_server = bonobo_object_activate (COMPOSER_OAFIID, 0);

       return bonobo_server;
}

static gboolean
composer_timeout (gpointer data)
{
       BonoboObjectClient *bonobo_server;

       bonobo_server = get_composer ();

       if (bonobo_server) {
               do_send (bonobo_server);
               gtk_main_quit ();
               return FALSE;
       } else {
               return TRUE;
       }
}

static void
cb (poptContext con, 
    enum poptCallbackReason reason,
    /*@null@*/ const struct poptOption * opt,
    /*@null@*/ const char * arg,
    /*@null@*/ const void * data)
{
       const char *name = opt->longName;
       if (!strcmp (name, "add-attachment")) {
               attachment_t *new_attach = g_new (attachment_t, 1);
               new_attach->filename = g_strdup (arg);
               new_attach->basename = strrchr (new_attach->filename, '/');
               if (new_attach->basename)
                       new_attach->basename ++;
               else
                       new_attach->basename = new_attach->filename;
               new_attach->basename = g_strdup (new_attach->basename);
               if (new_attach->filename)
                       elc_read_entire_file (new_attach->filename,
                                             &new_attach->size,
                                             &new_attach->content,
                                             &new_attach->content_type);
               new_attach->description = NULL;
               new_attach->show_inline = FALSE;
               if (new_attach->content) {
                       attachments = g_list_prepend (attachments, new_attach);
               } else {
                       free_attachment (new_attach);
               }
       } else if (!strcmp (name, "file-name")) {
               if (attachments) {
                       attachment_t *attachment = attachments->data;
                       g_free (attachment->basename);
                       attachment->basename = g_strdup (arg);
               } else {
               }
       } else if (!strcmp (name, "content-type")) {
               if (attachments) {
                       attachment_t *attachment = attachments->data;
                       if (attachment->content_type) {
                       }
                       attachment->content_type = g_strdup (arg);
               } else {
               }
       } else if (!strcmp (name, "description")) {
               if (attachments) {
                       attachment_t *attachment = attachments->data;
                       if (attachment->description) {
                       }
                       attachment->description = g_strdup (arg);
               } else {
               }
       } else if (!strcmp (name, "show-inline")) {
               if (attachments) {
                       attachment_t *attachment = attachments->data;
                       if (attachment->show_inline) {
                       }
                       attachment->show_inline = TRUE;
               } else {
               }
       }
}

static struct poptOption cap_options[] = {
       { NULL, '\0', POPT_ARG_CALLBACK, cb, 0, NULL, NULL },
       { "add-attachment", 'a', POPT_ARG_STRING, NULL, 0, N_("An attachment to add."), NULL },
       { "content-type", 't', POPT_ARG_STRING, NULL, 0, N_("Content type of the attachment."), NULL },
       { "file-name", 'f', POPT_ARG_STRING, NULL, 0, N_("The filename to display in the mail."), NULL },
       { "description", 'd', POPT_ARG_STRING, NULL, 0, N_("Description of the attachment."), NULL },
       { "show-inline", 'i', POPT_ARG_NONE, NULL, 0, N_("Mark attachment to be shown inline by default."), NULL },
       { "subject", 's', POPT_ARG_STRING, &subject, 0, N_("Default subject for the message."), NULL },
       { NULL, '\0', 0, NULL, 0 }
};

int
main(int argc, char *argv[])
{
       BonoboObjectClient *bonobo_server;

        bindtextdomain (PACKAGE, EVOLUTION_LOCALEDIR);
        textdomain (PACKAGE);

       gnome_vfs_init ();

       gnome_init_with_popt_table ("evolution-launch-composer", VERSION, argc, argv, cap_options, 0, NULL);

       if (bonobo_init (CORBA_OBJECT_NIL, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE)
               g_error (_("Could not initialize Bonobo"));

       bonobo_server = get_composer ();

       if (bonobo_server) {
               do_send (bonobo_server);
       } else {
               g_timeout_add(1000, composer_timeout, NULL);
               gtk_main ();
       }

       return 0;
}