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