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