Palette pop up div type thing. Hmm
[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         "#dfdfdf",
12         "#ffdfdf",
13         "#dfdfff",
14         "#ffffdf",
15         "#dfffdf",
16         "#ffdfff",
17         "#dfffff",
18         "#ffdfbf",
19         "#dfbfbf"
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->body = malloc(32768);
168                                 vnote_from_body->body[0] = 0;
169                                 body_len = 0;
170                         }
171                         body_line_len = strlen(buf);
172                         if ((body_len + body_line_len + 10) < 32768) {
173                                 strcpy(&vnote_from_body->body[body_len++], " ");
174                                 strcpy(&vnote_from_body->body[body_len], buf);
175                                 body_len += body_line_len;
176                         }
177                 }
178                 else if (IsEmptyStr(buf)) {
179                         in_body = 1;
180                 }
181         }
182
183         if (!IsEmptyStr(relevant_partnum)) {
184                 relevant_source = load_mimepart(msgnum, relevant_partnum);
185                 if (relevant_source != NULL) {
186                         struct vnote *v = vnote_new_from_str(relevant_source);
187                         free(relevant_source);
188                         return(v);
189                 }
190         }
191
192         if (vnote_from_body) {
193                 return(vnote_from_body);
194         }
195         return NULL;
196 }
197
198
199 /*
200  * Serialize a vnote and write it to the server
201  */
202 void write_vnote_to_server(struct vnote *v) 
203 {
204         char buf[1024];
205
206         serv_puts("ENT0 1|||4");
207         serv_getln(buf, sizeof buf);
208         if (buf[0] == '4') {
209                 serv_puts("Content-type: text/vnote");
210                 serv_puts("");
211                 serv_puts(vnote_serialize(v));
212                 serv_puts("000");
213         }
214 }
215
216
217
218
219 /*
220  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
221  */
222 void ajax_update_note(void) {
223
224         char buf[1024];
225         int msgnum;
226         struct vnote *v = NULL;
227
228         if (!havebstr("note_uid")) {
229                 begin_ajax_response();
230                 wprintf("Received ajax_update_note() request without a note UID.");
231                 end_ajax_response();
232                 return;
233         }
234
235         lprintf(9, "Note UID = %s\n", bstr("note_uid"));
236         serv_printf("EUID %s", bstr("note_uid"));
237         serv_getln(buf, sizeof buf);
238         if (buf[0] != '2') {
239                 begin_ajax_response();
240                 wprintf("Cannot find message containing vNote with the requested uid!");
241                 end_ajax_response();
242                 return;
243         }
244         msgnum = atol(&buf[4]);
245         lprintf(9, "Note msg = %ld\n", msgnum);
246
247         // Was this request a delete operation?  If so, nuke it...
248         if (havebstr("deletenote")) {
249                 if (!strcasecmp(bstr("deletenote"), "yes")) {
250                         serv_printf("DELE %ld", msgnum);
251                         serv_getln(buf, sizeof buf);
252                         begin_ajax_response();
253                         wprintf("%s", buf);
254                         end_ajax_response();
255                         return;
256                 }
257         }
258
259         // If we get to this point it's an update, not a delete
260         v = vnote_new_from_msg(msgnum);
261         if (!v) {
262                 begin_ajax_response();
263                 wprintf("Cannot locate a vNote within message %ld\n", msgnum);
264                 end_ajax_response();
265                 return;
266         }
267
268         /* Make any requested changes */
269         if (havebstr("top")) {
270                 v->pos_top = atoi(bstr("top"));
271         }
272         if (havebstr("left")) {
273                 v->pos_left = atoi(bstr("left"));
274         }
275         if (havebstr("height")) {
276                 v->pos_height = atoi(bstr("height"));
277         }
278         if (havebstr("width")) {
279                 v->pos_width = atoi(bstr("width"));
280         }
281         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
282                 if (v->body) free(v->body);
283                 v->body = strdup(bstr("value"));
284         }
285
286         /* Serialize it and save it to the message base.  Server will delete the old one. */
287         write_vnote_to_server(v);
288
289         begin_ajax_response();
290         if (v->body) {
291                 escputs(v->body);
292         }
293         end_ajax_response();
294
295         vnote_free(v);
296 }
297
298
299
300
301 /*
302  * display sticky notes
303  *
304  * msgnum = Message number on the local server of the note to be displayed
305  */
306 void display_note(long msgnum, int unread) {
307         struct vnote *v;
308
309         v = vnote_new_from_msg(msgnum);
310         if (v) {
311                 display_vnote_div(v);
312
313                 /* uncomment these lines to see ugly debugging info 
314                 wprintf("<script type=\"text/javascript\">");
315                 wprintf("document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
316                 wprintf("document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
317                 wprintf("document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
318                 wprintf("document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
319                 wprintf("</script>");
320                 */
321
322                 vnote_free(v);
323         }
324 }
325
326
327
328 /*
329  * Create a new note
330  */
331 void add_new_note(void) {
332         struct vnote *v;
333
334         v = vnote_new();
335         if (v) {
336                 v->uid = malloc(128);
337                 generate_uuid(v->uid);
338                 v->body = strdup(_("Click on any note to edit it."));
339                 write_vnote_to_server(v);
340                 vnote_free(v);
341         }
342         
343         readloop("readfwd");
344 }