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