aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/external-editor/external-editor.c
blob: b34cb0717d87fd33fc956e522915180ddf1b3aab (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
/* Sankar P - <psankar@novell.com> - GPL V3 or Later */

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

#include <mail/em-menu.h>
#include <mail/em-config.h>
#include <mail/em-composer-utils.h>
#include <mail/mail-config.h>
#include <e-util/e-error.h>

#include <e-msg-composer.h>

#include <sys/stat.h>
#include <sys/wait.h>

#include <stdlib.h>
#include <string.h>

#define d(x) x

#define TEMPORARY_FILE "/tmp/evolution-composer"

void org_gnome_external_editor (EPlugin *ep, EMMenuTargetSelect *select);

/* Utility function to convert an email address to CamelInternetAddress.
May be this should belong to CamelInternetAddress file itself. */

static CamelInternetAddress * convert_to_camel_internet_address (char * emails)
{
    CamelInternetAddress *cia = camel_internet_address_new();
    gchar **address_tokens = NULL;
    int i;

    if (emails && strlen (emails) > 1) {
        address_tokens = g_strsplit (emails, ",", 0);

        if (address_tokens) {
            for (i = 1; address_tokens[i]; ++i) {
                camel_internet_address_add (cia, " ", address_tokens [i]);
                d(printf ("\nAdding camel_internet_address[%s] \n", address_tokens [i]));
            }
            g_strfreev (address_tokens);

            return cia;
        }
    }
    camel_object_unref (cia);
    return NULL;
}

void org_gnome_external_editor (EPlugin *ep, EMMenuTargetSelect *select)
{
    d(printf ("\n\nexternal_editor plugin is launched \n\n"));

    /* The template to be used in the external editor */
    char template[] = "###|||Insert , seperated TO addresses below this line. Do not delete this line. Optional field\n\n###||| Insert , seperated CC addresses below this line. Do not delete this line. Optional field\n\n###|||Insert , seperated BCC addresses below this line. Do not delete this line. Optional field\n\n###|||Insert SUBJECT below this line. Do not delete this line. Optional field\n\n###|||Insert BODY of mail below this line. Do not delete this line.\n\n";


    /* Push the template contents to the intermediate file */
    g_file_set_contents (TEMPORARY_FILE, template, strlen (template), NULL);

    char *editor;

    editor = (char *) g_getenv ("EDITOR");
    if (!editor)
        editor = "gvim";

#if 1
    int status = 0;
    gchar *argv[4];

    argv[0] = editor;

    /*  README: The -- params should come via the "Configure" option */
    argv[1] = "--nofork";

    argv[2] = TEMPORARY_FILE;
    argv[3] = NULL;

    /* FIXME: I guess NULL should do fine instead of /usr/bin */
    if (!g_spawn_sync ("/usr/bin", argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &status, NULL))
    {
        g_warning ("Unable to launch %s: ", argv[0]);
        return ;
    }

    if (WEXITSTATUS (status) != 0) {
        d(printf ("\n\nsome problem here with external editor\n\n"));
        return ;
    } else {

        gchar *buf;
        CamelMimeMessage *message;
        EMsgComposer *composer;

        message = camel_mime_message_new ();

        d(printf ("\n\nexternal editor works like a charm \n\n"));

        if (g_file_get_contents (TEMPORARY_FILE, &buf, NULL, NULL)) {
            gchar **tokens;
            int i, j;

            tokens = g_strsplit (buf, "###|||", 6);

            for (i = 1; tokens[i]; ++i) {

                for (j = 0; tokens[i][j] && tokens[i][j] != '\n'; ++j) {
                    tokens [i][j] = ' ';
                }

                if (tokens[i][j] == '\n')
                    tokens[i][j] = ' ';

                g_strchug(tokens[i]);

                d(printf ("\nstripped off token[%d] is : %s \n", i, tokens[i]));
            }

            camel_mime_message_set_recipients (message, "To", convert_to_camel_internet_address(tokens[1]));
            camel_mime_message_set_recipients (message, "Cc", convert_to_camel_internet_address(tokens[2]));
            camel_mime_message_set_recipients (message, "Bcc", convert_to_camel_internet_address(tokens[3]));

            camel_mime_message_set_subject (message, tokens[4]);

            camel_mime_part_set_content ((CamelMimePart *)message, tokens [5], strlen (tokens [5]), "text/plain");


            /* FIXME: We need to make mail-remote working properly.
               So that we neednot invoke composer widget at all.
               May be we can do it now itself by invoking local CamelTransport.
               But all that is not needed for the first release.
             */

            composer = e_msg_composer_new_with_message (message);
            g_signal_connect (GTK_OBJECT (composer), "send", G_CALLBACK (em_utils_composer_send_cb), NULL);

            g_signal_connect (GTK_OBJECT (composer), "save-draft", G_CALLBACK (em_utils_composer_save_draft_cb), NULL);

            gtk_widget_show (GTK_WIDGET (composer));

            g_strfreev (tokens);
        }
    }
#else
    char *query;

    query = g_strdup_printf ("%s /tmp/evolution-composer", editor);
    system (query);
    g_free (query);

#endif
}