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