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