Internet Exploder fixes
[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 int pastel_palette[9][3] = {
11         { 0x80, 0x80, 0x80 },
12         { 0xff, 0x80, 0x80 },
13         { 0x80, 0x80, 0xff },
14         { 0xff, 0xff, 0x80 },
15         { 0x80, 0xff, 0x80 },
16         { 0xff, 0x80, 0xff },
17         { 0x80, 0xff, 0xff },
18         { 0xff, 0x80, 0x80 },
19         { 0x80, 0x80, 0x80 }
20 };
21
22
23 /*
24  * Display a <div> containing a rendered sticky note.
25  */
26 void display_vnote_div(struct vnote *v) {
27         int i;
28
29
30         wprintf("<div id=\"note-%s\" ", v->uid);        // begin outer div
31         wprintf("class=\"stickynote_outer\" ");
32         wprintf("style=\"");
33         wprintf("left: %dpx; ", v->pos_left);
34         wprintf("top: %dpx; ", v->pos_top);
35         wprintf("width: %dpx; ", v->pos_width);
36         wprintf("height: %dpx; ", v->pos_height);
37         wprintf("background-color: #%02X%02X%02X ", v->color_red, v->color_green, v->color_blue);
38         wprintf("\">");
39
40
41
42
43
44         wprintf("<div id=\"titlebar-%s\" ", v->uid);    // begin title bar div
45         wprintf("class=\"stickynote_titlebar\" ");
46         wprintf("onMouseDown=\"NotesDragMouseDown(event,'%s')\" ", v->uid);
47         wprintf("style=\"");
48         wprintf("background-color: #%02X%02X%02X ", v->color_red/2, v->color_green/2, v->color_blue/2);
49         wprintf("\">");
50
51         wprintf("<table border=0 cellpadding=0 cellspacing=0 valign=middle width=100%%><tr>");
52
53         wprintf("<td align=left valign=middle>");
54         wprintf("<img onclick=\"NotesClickPalette(event,'%s')\" ", v->uid);
55         wprintf("src=\"static/8paint16.gif\">");
56
57         /* embed color selector */
58         wprintf("<div id=\"palette-%s\" ", v->uid);     // begin stickynote_palette div
59         wprintf("class=\"stickynote_palette\">");
60
61         wprintf("<table border=0 cellpadding=0 cellspacing=0>");
62         for (i=0; i<9; ++i) {
63                 if ((i%3)==0) wprintf("<tr>");
64                 wprintf("<td ");
65                 wprintf("onClick=\"NotesClickColor(event,'%s',%d,%d,%d,'#%02x%02x%02x','#%02x%02x%02x')\" ",
66                         v->uid,
67                         pastel_palette[i][0],           // color values to pass to ajax call
68                         pastel_palette[i][1],
69                         pastel_palette[i][2],
70                         pastel_palette[i][0],           // new color of note
71                         pastel_palette[i][1],
72                         pastel_palette[i][2],
73                         pastel_palette[i][0] / 2,       // new color of title bar
74                         pastel_palette[i][1] / 2,
75                         pastel_palette[i][2] / 2
76                 );
77                 wprintf("bgcolor=\"#%02x%02x%02x\"> </td>",
78                         pastel_palette[i][0],
79                         pastel_palette[i][1],
80                         pastel_palette[i][2]
81                 );
82                 if (((i+1)%3)==0) wprintf("</tr>");
83         }
84         wprintf("</table>");
85
86         wprintf("</div>");                              // end stickynote_palette div
87
88         wprintf("</td>");
89
90         wprintf("<td></td>");   // nothing in the title bar, it's just for dragging
91
92         wprintf("<td align=right valign=middle>");
93         wprintf("<img onclick=\"DeleteStickyNote(event,'%s','%s')\" ", v->uid, _("Delete this note?") );
94         wprintf("src=\"static/closewindow.gif\">");
95         wprintf("</td></tr></table>");
96
97         wprintf("</div>\n");                            // end title bar div
98
99
100
101
102
103         wprintf("<div id=\"notebody-%s\" ", v->uid);    // begin body div
104         wprintf("class=\"stickynote_body\"");
105         wprintf(">");
106         escputs(v->body);
107         wprintf("</div>\n");                            // end body div
108
109         wprintf("<script type=\"text/javascript\">");
110         wprintf(" new Ajax.InPlaceEditor('notebody-%s', 'ajax_update_note?note_uid=%s', "
111                 "{rows:%d,cols:%d,onEnterHover:false,onLeaveHover:false,"
112                 "okText:'%s',cancelText:'%s',clickToEditText:'%s'});",
113                 v->uid,
114                 v->uid,
115                 (v->pos_height / 16) - 5,
116                 (v->pos_width / 9) - 1,
117                 _("Save"),
118                 _("Cancel"),
119                 _("Click on any note to edit it.")
120         );
121         wprintf("</script>\n");
122
123
124
125         wprintf("<div id=\"resize-%s\" ", v->uid);      // begin resize handle div
126         wprintf("class=\"stickynote_resize\" ");
127         wprintf("onMouseDown=\"NotesResizeMouseDown(event,'%s')\"", v->uid);
128         wprintf("> </div>");                            // end resize handle div
129
130
131
132         wprintf("</div>\n");                            // end outer div
133 }
134
135
136
137 /*
138  * Fetch a message from the server and extract a vNote from it
139  */
140 struct vnote *vnote_new_from_msg(long msgnum) {
141         char buf[1024];
142         char mime_partnum[256];
143         char mime_filename[256];
144         char mime_content_type[256];
145         char mime_disposition[256];
146         int mime_length;
147         char relevant_partnum[256];
148         char *relevant_source = NULL;
149         char uid_from_headers[256];
150         int in_body = 0;
151         int body_line_len = 0;
152         int body_len = 0;
153         struct vnote *vnote_from_body = NULL;
154
155         relevant_partnum[0] = 0;
156         uid_from_headers[0] = 0;
157         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
158         serv_puts(buf);
159         serv_getln(buf, sizeof buf);
160         if (buf[0] != '1') return NULL;
161
162         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
163                 if (!strncasecmp(buf, "exti=", 5)) {
164                         safestrncpy(uid_from_headers, &buf[5], sizeof uid_from_headers);
165                 }
166                 else if (!strncasecmp(buf, "part=", 5)) {
167                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
168                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
169                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
170                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
171                         mime_length = extract_int(&buf[5], 5);
172
173                         if (!strcasecmp(mime_content_type, "text/vnote")) {
174                                 strcpy(relevant_partnum, mime_partnum);
175                         }
176                 }
177                 else if ((in_body) && (IsEmptyStr(relevant_partnum)) && (!IsEmptyStr(uid_from_headers))) {
178                         // Convert an old-style note to a vNote
179                         if (!vnote_from_body) {
180                                 vnote_from_body = vnote_new();
181                                 vnote_from_body->uid = strdup(uid_from_headers);
182                                 vnote_from_body->color_red = pastel_palette[3][0];
183                                 vnote_from_body->color_green = pastel_palette[3][1];
184                                 vnote_from_body->color_blue = pastel_palette[3][2];
185                                 vnote_from_body->body = malloc(32768);
186                                 vnote_from_body->body[0] = 0;
187                                 body_len = 0;
188                         }
189                         body_line_len = strlen(buf);
190                         if ((body_len + body_line_len + 10) < 32768) {
191                                 strcpy(&vnote_from_body->body[body_len++], " ");
192                                 strcpy(&vnote_from_body->body[body_len], buf);
193                                 body_len += body_line_len;
194                         }
195                 }
196                 else if (IsEmptyStr(buf)) {
197                         in_body = 1;
198                 }
199         }
200
201         if (!IsEmptyStr(relevant_partnum)) {
202                 relevant_source = load_mimepart(msgnum, relevant_partnum);
203                 if (relevant_source != NULL) {
204                         struct vnote *v = vnote_new_from_str(relevant_source);
205                         free(relevant_source);
206                         return(v);
207                 }
208         }
209
210         if (vnote_from_body) {
211                 return(vnote_from_body);
212         }
213         return NULL;
214 }
215
216
217 /*
218  * Serialize a vnote and write it to the server
219  */
220 void write_vnote_to_server(struct vnote *v) 
221 {
222         char buf[1024];
223
224         serv_puts("ENT0 1|||4");
225         serv_getln(buf, sizeof buf);
226         if (buf[0] == '4') {
227                 serv_puts("Content-type: text/vnote");
228                 serv_puts("");
229                 serv_puts(vnote_serialize(v));
230                 serv_puts("000");
231         }
232 }
233
234
235
236
237 /*
238  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
239  */
240 void ajax_update_note(void) {
241
242         char buf[1024];
243         int msgnum;
244         struct vnote *v = NULL;
245
246         if (!havebstr("note_uid")) {
247                 begin_ajax_response();
248                 wprintf("Received ajax_update_note() request without a note UID.");
249                 end_ajax_response();
250                 return;
251         }
252
253         // lprintf(9, "Note UID = %s\n", bstr("note_uid"));
254         serv_printf("EUID %s", bstr("note_uid"));
255         serv_getln(buf, sizeof buf);
256         if (buf[0] != '2') {
257                 begin_ajax_response();
258                 wprintf("Cannot find message containing vNote with the requested uid!");
259                 end_ajax_response();
260                 return;
261         }
262         msgnum = atol(&buf[4]);
263         // lprintf(9, "Note msg = %ld\n", msgnum);
264
265         // Was this request a delete operation?  If so, nuke it...
266         if (havebstr("deletenote")) {
267                 if (!strcasecmp(bstr("deletenote"), "yes")) {
268                         serv_printf("DELE %ld", msgnum);
269                         serv_getln(buf, sizeof buf);
270                         begin_ajax_response();
271                         wprintf("%s", buf);
272                         end_ajax_response();
273                         return;
274                 }
275         }
276
277         // If we get to this point it's an update, not a delete
278         v = vnote_new_from_msg(msgnum);
279         if (!v) {
280                 begin_ajax_response();
281                 wprintf("Cannot locate a vNote within message %ld\n", msgnum);
282                 end_ajax_response();
283                 return;
284         }
285
286         /* Make any requested changes */
287         if (havebstr("top")) {
288                 v->pos_top = atoi(bstr("top"));
289         }
290         if (havebstr("left")) {
291                 v->pos_left = atoi(bstr("left"));
292         }
293         if (havebstr("height")) {
294                 v->pos_height = atoi(bstr("height"));
295         }
296         if (havebstr("width")) {
297                 v->pos_width = atoi(bstr("width"));
298         }
299         if (havebstr("red")) {
300                 v->color_red = atoi(bstr("red"));
301         }
302         if (havebstr("green")) {
303                 v->color_green = atoi(bstr("green"));
304         }
305         if (havebstr("blue")) {
306                 v->color_blue = atoi(bstr("blue"));
307         }
308         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
309                 if (v->body) free(v->body);
310                 v->body = strdup(bstr("value"));
311         }
312
313         /* Serialize it and save it to the message base.  Server will delete the old one. */
314         write_vnote_to_server(v);
315
316         begin_ajax_response();
317         if (v->body) {
318                 escputs(v->body);
319         }
320         end_ajax_response();
321
322         vnote_free(v);
323 }
324
325
326
327
328 /*
329  * display sticky notes
330  *
331  * msgnum = Message number on the local server of the note to be displayed
332  */
333 void display_note(long msgnum, int unread) {
334         struct vnote *v;
335
336         v = vnote_new_from_msg(msgnum);
337         if (v) {
338                 display_vnote_div(v);
339
340                 /* uncomment these lines to see ugly debugging info 
341                 wprintf("<script type=\"text/javascript\">");
342                 wprintf("document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
343                 wprintf("document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
344                 wprintf("document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
345                 wprintf("document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
346                 wprintf("</script>");
347                 */
348
349                 vnote_free(v);
350         }
351 }
352
353
354
355 /*
356  * Create a new note
357  */
358 void add_new_note(void) {
359         struct vnote *v;
360
361         v = vnote_new();
362         if (v) {
363                 v->uid = malloc(128);
364                 generate_uuid(v->uid);
365                 v->color_red = pastel_palette[3][0];
366                 v->color_green = pastel_palette[3][1];
367                 v->color_blue = pastel_palette[3][2];
368                 v->body = strdup(_("Click on any note to edit it."));
369                 write_vnote_to_server(v);
370                 vnote_free(v);
371         }
372         
373         readloop("readfwd");
374 }