* Sticky note color selection is now feature complete,
[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,"
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(">");
124
125         wprintf("<img src=\"static/resizecorner.png\">");
126
127         wprintf("</div>\n");
128
129         /* end of note */
130
131         wprintf("</div>\n");
132 }
133
134
135
136 /*
137  * Fetch a message from the server and extract a vNote from it
138  */
139 struct vnote *vnote_new_from_msg(long msgnum) {
140         char buf[1024];
141         char mime_partnum[256];
142         char mime_filename[256];
143         char mime_content_type[256];
144         char mime_disposition[256];
145         int mime_length;
146         char relevant_partnum[256];
147         char *relevant_source = NULL;
148         char uid_from_headers[256];
149         int in_body = 0;
150         int body_line_len = 0;
151         int body_len = 0;
152         struct vnote *vnote_from_body = NULL;
153
154         relevant_partnum[0] = 0;
155         uid_from_headers[0] = 0;
156         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
157         serv_puts(buf);
158         serv_getln(buf, sizeof buf);
159         if (buf[0] != '1') return NULL;
160
161         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
162                 if (!strncasecmp(buf, "exti=", 5)) {
163                         safestrncpy(uid_from_headers, &buf[5], sizeof uid_from_headers);
164                 }
165                 else if (!strncasecmp(buf, "part=", 5)) {
166                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
167                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
168                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
169                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
170                         mime_length = extract_int(&buf[5], 5);
171
172                         if (!strcasecmp(mime_content_type, "text/vnote")) {
173                                 strcpy(relevant_partnum, mime_partnum);
174                         }
175                 }
176                 else if ((in_body) && (IsEmptyStr(relevant_partnum)) && (!IsEmptyStr(uid_from_headers))) {
177                         // Convert an old-style note to a vNote
178                         if (!vnote_from_body) {
179                                 vnote_from_body = vnote_new();
180                                 vnote_from_body->uid = strdup(uid_from_headers);
181                                 vnote_from_body->color_red = pastel_palette[3][0];
182                                 vnote_from_body->color_green = pastel_palette[3][1];
183                                 vnote_from_body->color_blue = pastel_palette[3][2];
184                                 vnote_from_body->body = malloc(32768);
185                                 vnote_from_body->body[0] = 0;
186                                 body_len = 0;
187                         }
188                         body_line_len = strlen(buf);
189                         if ((body_len + body_line_len + 10) < 32768) {
190                                 strcpy(&vnote_from_body->body[body_len++], " ");
191                                 strcpy(&vnote_from_body->body[body_len], buf);
192                                 body_len += body_line_len;
193                         }
194                 }
195                 else if (IsEmptyStr(buf)) {
196                         in_body = 1;
197                 }
198         }
199
200         if (!IsEmptyStr(relevant_partnum)) {
201                 relevant_source = load_mimepart(msgnum, relevant_partnum);
202                 if (relevant_source != NULL) {
203                         struct vnote *v = vnote_new_from_str(relevant_source);
204                         free(relevant_source);
205                         return(v);
206                 }
207         }
208
209         if (vnote_from_body) {
210                 return(vnote_from_body);
211         }
212         return NULL;
213 }
214
215
216 /*
217  * Serialize a vnote and write it to the server
218  */
219 void write_vnote_to_server(struct vnote *v) 
220 {
221         char buf[1024];
222
223         serv_puts("ENT0 1|||4");
224         serv_getln(buf, sizeof buf);
225         if (buf[0] == '4') {
226                 serv_puts("Content-type: text/vnote");
227                 serv_puts("");
228                 serv_puts(vnote_serialize(v));
229                 serv_puts("000");
230         }
231 }
232
233
234
235
236 /*
237  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
238  */
239 void ajax_update_note(void) {
240
241         char buf[1024];
242         int msgnum;
243         struct vnote *v = NULL;
244
245         if (!havebstr("note_uid")) {
246                 begin_ajax_response();
247                 wprintf("Received ajax_update_note() request without a note UID.");
248                 end_ajax_response();
249                 return;
250         }
251
252         lprintf(9, "Note UID = %s\n", bstr("note_uid"));
253         serv_printf("EUID %s", bstr("note_uid"));
254         serv_getln(buf, sizeof buf);
255         if (buf[0] != '2') {
256                 begin_ajax_response();
257                 wprintf("Cannot find message containing vNote with the requested uid!");
258                 end_ajax_response();
259                 return;
260         }
261         msgnum = atol(&buf[4]);
262         lprintf(9, "Note msg = %ld\n", msgnum);
263
264         // Was this request a delete operation?  If so, nuke it...
265         if (havebstr("deletenote")) {
266                 if (!strcasecmp(bstr("deletenote"), "yes")) {
267                         serv_printf("DELE %ld", msgnum);
268                         serv_getln(buf, sizeof buf);
269                         begin_ajax_response();
270                         wprintf("%s", buf);
271                         end_ajax_response();
272                         return;
273                 }
274         }
275
276         // If we get to this point it's an update, not a delete
277         v = vnote_new_from_msg(msgnum);
278         if (!v) {
279                 begin_ajax_response();
280                 wprintf("Cannot locate a vNote within message %ld\n", msgnum);
281                 end_ajax_response();
282                 return;
283         }
284
285         /* Make any requested changes */
286         if (havebstr("top")) {
287                 v->pos_top = atoi(bstr("top"));
288         }
289         if (havebstr("left")) {
290                 v->pos_left = atoi(bstr("left"));
291         }
292         if (havebstr("height")) {
293                 v->pos_height = atoi(bstr("height"));
294         }
295         if (havebstr("width")) {
296                 v->pos_width = atoi(bstr("width"));
297         }
298         if (havebstr("red")) {
299                 v->color_red = atoi(bstr("red"));
300         }
301         if (havebstr("green")) {
302                 v->color_green = atoi(bstr("green"));
303         }
304         if (havebstr("blue")) {
305                 v->color_blue = atoi(bstr("blue"));
306         }
307         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
308                 if (v->body) free(v->body);
309                 v->body = strdup(bstr("value"));
310         }
311
312         /* Serialize it and save it to the message base.  Server will delete the old one. */
313         write_vnote_to_server(v);
314
315         begin_ajax_response();
316         if (v->body) {
317                 escputs(v->body);
318         }
319         end_ajax_response();
320
321         vnote_free(v);
322 }
323
324
325
326
327 /*
328  * display sticky notes
329  *
330  * msgnum = Message number on the local server of the note to be displayed
331  */
332 void display_note(long msgnum, int unread) {
333         struct vnote *v;
334
335         v = vnote_new_from_msg(msgnum);
336         if (v) {
337                 display_vnote_div(v);
338
339                 /* uncomment these lines to see ugly debugging info 
340                 wprintf("<script type=\"text/javascript\">");
341                 wprintf("document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
342                 wprintf("document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
343                 wprintf("document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
344                 wprintf("document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
345                 wprintf("</script>");
346                 */
347
348                 vnote_free(v);
349         }
350 }
351
352
353
354 /*
355  * Create a new note
356  */
357 void add_new_note(void) {
358         struct vnote *v;
359
360         v = vnote_new();
361         if (v) {
362                 v->uid = malloc(128);
363                 generate_uuid(v->uid);
364                 v->color_red = pastel_palette[3][0];
365                 v->color_green = pastel_palette[3][1];
366                 v->color_blue = pastel_palette[3][2];
367                 v->body = strdup(_("Click on any note to edit it."));
368                 write_vnote_to_server(v);
369                 vnote_free(v);
370         }
371         
372         readloop("readfwd");
373 }