Added code to activate color selection in javascript.
[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)\" ",
64                         v->uid,
65                         pastel_palette[i][0],
66                         pastel_palette[i][1],
67                         pastel_palette[i][2]
68                 );
69                 wprintf("bgcolor=\"#%02x%02x%02x\"> </td>",
70                         pastel_palette[i][0],
71                         pastel_palette[i][1],
72                         pastel_palette[i][2]
73                 );
74                 if (((i+1)%3)==0) wprintf("</tr>");
75         }
76         wprintf("</table>");
77         wprintf("</div>");
78
79         wprintf("</td>");
80
81         wprintf("<td></td>");   // nothing in the title bar, it's just for resizing
82
83         wprintf("<td align=right valign=middle>");
84         wprintf("<img onclick=\"DeleteStickyNote(event,'%s','%s')\" ", v->uid, _("Delete this note?") );
85         wprintf("src=\"static/closewindow.gif\">");
86         wprintf("</td></tr></table>");
87
88         wprintf("</div>\n");
89
90         /* begin note body */
91
92         wprintf("<div id=\"notebody-%s\" ", v->uid);
93         wprintf("class=\"stickynote_body\"");
94         wprintf(">");
95         escputs(v->body);
96         wprintf("</div>\n");
97
98         wprintf("<script type=\"text/javascript\">");
99         wprintf(" new Ajax.InPlaceEditor('notebody-%s', 'ajax_update_note?note_uid=%s', "
100                 "{rows:%d,cols:%d,highlightcolor:'#%02X%02X%02X',highlightendcolor:'#%02X%02X%02X',"
101                 "okText:'%s',cancelText:'%s',clickToEditText:'%s'});",
102                 v->uid,
103                 v->uid,
104                 (v->pos_height / 16) - 5,
105                 (v->pos_width / 9) - 1,
106                 v->color_red, v->color_green, v->color_blue,
107                 v->color_red, v->color_green, v->color_blue,
108                 _("Save"),
109                 _("Cancel"),
110                 _("Click on any note to edit it.")
111         );
112         wprintf("</script>\n");
113
114         /* begin resize handle */
115
116         wprintf("<div id=\"resize-%s\" ", v->uid);
117         wprintf("class=\"stickynote_resize\" ");
118         wprintf("onMouseDown=\"NotesResizeMouseDown(event,'%s')\"", v->uid);
119         wprintf(">");
120
121         wprintf("<img src=\"static/resizecorner.png\">");
122
123         wprintf("</div>\n");
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("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
295                 if (v->body) free(v->body);
296                 v->body = strdup(bstr("value"));
297         }
298
299         /* Serialize it and save it to the message base.  Server will delete the old one. */
300         write_vnote_to_server(v);
301
302         begin_ajax_response();
303         if (v->body) {
304                 escputs(v->body);
305         }
306         end_ajax_response();
307
308         vnote_free(v);
309 }
310
311
312
313
314 /*
315  * display sticky notes
316  *
317  * msgnum = Message number on the local server of the note to be displayed
318  */
319 void display_note(long msgnum, int unread) {
320         struct vnote *v;
321
322         v = vnote_new_from_msg(msgnum);
323         if (v) {
324                 display_vnote_div(v);
325
326                 /* uncomment these lines to see ugly debugging info 
327                 wprintf("<script type=\"text/javascript\">");
328                 wprintf("document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
329                 wprintf("document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
330                 wprintf("document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
331                 wprintf("document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
332                 wprintf("</script>");
333                 */
334
335                 vnote_free(v);
336         }
337 }
338
339
340
341 /*
342  * Create a new note
343  */
344 void add_new_note(void) {
345         struct vnote *v;
346
347         v = vnote_new();
348         if (v) {
349                 v->uid = malloc(128);
350                 generate_uuid(v->uid);
351                 v->color_red = pastel_palette[3][0];
352                 v->color_green = pastel_palette[3][1];
353                 v->color_blue = pastel_palette[3][2];
354                 v->body = strdup(_("Click on any note to edit it."));
355                 write_vnote_to_server(v);
356                 vnote_free(v);
357         }
358         
359         readloop("readfwd");
360 }