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