Began work on vNote based display for sticky notes
[citadel.git] / webcit / notes.c
1 /*
2  * $Id$
3  *
4  */
5
6 #include "webcit.h"
7 #include "groupdav.h"
8 #include "webserver.h"
9
10 /*
11  * display sticky notes
12  *
13  * msgnum = Message number on the local server of the note to be displayed
14  */
15 void display_note(long msgnum, int unread)
16 {
17         char buf[SIZ];
18         char notetext[SIZ];
19         char display_notetext[SIZ];
20         char eid[128];
21         int in_text = 0;
22         int i, len;
23
24         serv_printf("MSG0 %ld", msgnum);
25         serv_getln(buf, sizeof buf);
26         if (buf[0] != '1') {
27                 wprintf("%s<br />\n", &buf[4]);
28                 return;
29         }
30
31         strcpy(notetext, "");
32         strcpy(eid, "");
33         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
34
35                 /* Fill the buffer */
36                 if ( (in_text) && (strlen(notetext) < SIZ-256) ) {
37                         strcat(notetext, buf);
38                 }
39
40                 if ( (!in_text) && (!strncasecmp(buf, "exti=", 5)) ) {
41                         safestrncpy(eid, &buf[5], sizeof eid);
42                 }
43
44                 if ( (!in_text) && (!strcasecmp(buf, "text")) ) {
45                         in_text = 1;
46                 }
47         }
48
49         /* Now sanitize the buffer */
50         len = strlen(notetext);
51         for (i=0; i<len; ++i) {
52                 if (isspace(notetext[i])) notetext[i] = ' ';
53         }
54
55         /* Make it HTML-happy and print it. */
56         stresc(display_notetext, SIZ, notetext, 0, 0);
57
58         /* Lets try it as a draggable */
59         if (!IsEmptyStr(eid)) {
60                 wprintf ("<IMG ALIGN=MIDDLE src=\"static/storenotes_48x.gif\" id=\"note_%s\" alt=\"Note\" ", eid); 
61                 wprintf ("class=\"notes\">\n");
62                 wprintf ("<script type=\"text/javascript\">\n");
63                 wprintf ("new Draggable (\"note_%s\", {revert:true})\n", eid);
64                 wprintf ("</script>\n");
65         }
66         else {
67                 wprintf ("<IMG ALIGN=MIDDLE src=\"static/storenotes_48x.gif\" id=\"note_%ld\" ", msgnum); 
68                 wprintf ("class=\"notes\">\n");
69                 wprintf ("<script type=\"text/javascript\">\n");
70                 wprintf ("new Draggable (\"note_%ld\", {revert:true})\n", msgnum);
71                 wprintf ("</script>\n");
72         }
73         
74         if (!IsEmptyStr(eid)) {
75                 wprintf("<span id=\"note%s\">%s</span><br />\n", eid, display_notetext);
76         }
77         else {
78                 wprintf("<span id=\"note%ld\">%s</span><br />\n", msgnum, display_notetext);
79         }
80
81         /* Offer in-place editing. */
82         if (!IsEmptyStr(eid)) {
83                 wprintf("<script type=\"text/javascript\">"
84                         "new Ajax.InPlaceEditor('note%s', 'updatenote?nonce=%ld?eid=%s', {rows:5,cols:72});"
85                         "</script>\n",
86                         eid,
87                         WC->nonce,
88                         eid
89                 );
90         }
91 }
92
93
94 /*
95  * This gets called by the Ajax.InPlaceEditor when we save a note.
96  */
97 void updatenote(void)
98 {
99         char buf[SIZ];
100         char notetext[SIZ];
101         char display_notetext[SIZ];
102         long msgnum;
103         int in_text = 0;
104         int i, len;
105
106         serv_printf("ENT0 1||0|0||||||%s", bstr("eid"));
107         serv_getln(buf, sizeof buf);
108         if (buf[0] == '4') {
109                 text_to_server(bstr("value"));
110                 serv_puts("000");
111         }
112
113         begin_ajax_response();
114         msgnum = locate_message_by_uid(bstr("eid"));
115         if (msgnum >= 0L) {
116                 serv_printf("MSG0 %ld", msgnum);
117                 serv_getln(buf, sizeof buf);
118                 if (buf[0] == '1') {
119                         strcpy(notetext, "");
120                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
121                 
122                                 /* Fill the buffer */
123                                 if ( (in_text) && (strlen(notetext) < SIZ-256) ) {
124                                         strcat(notetext, buf);
125                                 }
126                 
127                                 if ( (!in_text) && (!strcasecmp(buf, "text")) ) {
128                                         in_text = 1;
129                                 }
130                         }
131                         /* Now sanitize the buffer */
132                         len = strlen(notetext);
133                         for (i=0; i<len; ++i) {
134                                 if (isspace(notetext[i])) notetext[i] = ' ';
135                         }
136                 
137                         /* Make it HTML-happy and print it. */
138                         stresc(display_notetext, SIZ, notetext, 0, 0);
139                         wprintf("%s\n", display_notetext);
140                 }
141         }
142         else {
143                 wprintf("%s", _("An error has occurred."));
144         }
145
146         end_ajax_response();
147 }
148
149
150
151
152
153
154
155
156 /*
157  * Display a <div> containing a rendered sticky note.
158  */
159 void display_vnote_div(struct vnote *v, long msgnum) {
160
161         wprintf("<div id=\"note%ld\" ", msgnum);
162         wprintf("style=\"position: relative; ");
163         wprintf("left: %dpx; ", v->pos_left);
164         wprintf("top: %dpx; ", v->pos_top);
165         wprintf("width: %dpx; ", v->pos_width);
166         wprintf("height: %dpx; ", v->pos_height);
167         wprintf("border: 1px solid black; ");
168         wprintf("background-color: #%02X%02X%02X ", v->color_red, v->color_green, v->color_blue);
169         wprintf("\">");
170
171         escputs(v->body);
172
173         wprintf("</div>\n");
174 }
175
176
177
178
179 /*
180  * display sticky notes
181  *
182  * msgnum = Message number on the local server of the note to be displayed
183  */
184 void display_note_NEW(long msgnum, int unread) {
185         char buf[1024];
186         char mime_partnum[256];
187         char mime_filename[256];
188         char mime_content_type[256];
189         char mime_disposition[256];
190         int mime_length;
191         char relevant_partnum[256];
192         char *relevant_source = NULL;
193
194         relevant_partnum[0] = '\0';
195         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
196         serv_puts(buf);
197         serv_getln(buf, sizeof buf);
198         if (buf[0] != '1') return;
199
200         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
201                 if (!strncasecmp(buf, "part=", 5)) {
202                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
203                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
204                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
205                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
206                         mime_length = extract_int(&buf[5], 5);
207
208                         if (!strcasecmp(mime_content_type, "text/vnote")) {
209                                 strcpy(relevant_partnum, mime_partnum);
210                         }
211                 }
212         }
213
214         if (!IsEmptyStr(relevant_partnum)) {
215                 relevant_source = load_mimepart(msgnum, relevant_partnum);
216                 if (relevant_source != NULL) {
217                         struct vnote *v = vnote_new_from_str(relevant_source);
218                         free(relevant_source);
219                         display_vnote_div(v, msgnum);
220                         vnote_free(v);
221
222                         /* FIXME remove these debugging messages when finished */
223                         wprintf("<script type=\"text/javascript\">");
224                         wprintf("document.write('L: ' + $('note%ld').style.left + '<br>');", msgnum);
225                         wprintf("document.write('T: ' + $('note%ld').style.top + '<br>');", msgnum);
226                         wprintf("document.write('W: ' + $('note%ld').style.width + '<br>');", msgnum);
227                         wprintf("document.write('H: ' + $('note%ld').style.height + '<br>');", msgnum);
228                         wprintf("</script>");
229                 }
230         }
231 }