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