templatize uploading dialog for room/... graphics.
[citadel.git] / webcit / graphics.c
1 /*
2  * Handles HTTP upload of graphics files into the system.
3  *
4  * Copyright (c) 1996-2011 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include "webcit.h"
22
23 void display_graphics_upload(char *filename)
24 {
25         char buf[SIZ];
26
27         snprintf(buf, SIZ, "UIMG 0||%s", filename);
28         serv_puts(buf);
29         serv_getln(buf, sizeof buf);
30         if (buf[0] != '2') {
31                 strcpy(WC->ImportantMessage, &buf[4]);
32                 display_main_menu();
33                 return;
34         }
35         output_headers(1, 0, 0, 0, 1, 0);
36         do_template("files_graphicsupload");
37         end_burst();
38 }
39
40 void do_graphics_upload(char *filename)
41 {
42         const char *MimeType;
43         wcsession *WCC = WC;
44         char buf[SIZ];
45         int bytes_remaining;
46         int pos = 0;
47         int thisblock;
48         bytes_remaining = WCC->upload_length;
49
50         if (havebstr("cancel_button")) {
51                 strcpy(WC->ImportantMessage,
52                         _("Graphics upload has been cancelled."));
53                 display_main_menu();
54                 return;
55         }
56
57         if (WCC->upload_length == 0) {
58                 strcpy(WC->ImportantMessage,
59                         _("You didn't upload a file."));
60                 display_main_menu();
61                 return;
62         }
63         
64         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
65         snprintf(buf, SIZ, "UIMG 1|%s|%s", MimeType, filename);
66         serv_puts(buf);
67
68         serv_getln(buf, sizeof buf);
69         if (buf[0] != '2') {
70                 strcpy(WCC->ImportantMessage, &buf[4]);
71                 display_main_menu();
72                 return;
73         }
74         while (bytes_remaining) {
75                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
76                 serv_printf("WRIT %d", thisblock);
77                 serv_getln(buf, sizeof buf);
78                 if (buf[0] != '7') {
79                         strcpy(WCC->ImportantMessage, &buf[4]);
80                         serv_puts("UCLS 0");
81                         serv_getln(buf, sizeof buf);
82                         display_main_menu();
83                         return;
84                 }
85                 thisblock = extract_int(&buf[4], 0);
86                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
87                 pos = pos + thisblock;
88                 bytes_remaining = bytes_remaining - thisblock;
89         }
90
91         serv_puts("UCLS 1");
92         serv_getln(buf, sizeof buf);
93         if (buf[0] != 'x') {
94                 display_success(&buf[4]);
95                 return;
96         }
97 }
98
99
100 void edithellopic(void)    { do_graphics_upload("hello"); }
101 void editpic(void)         { do_graphics_upload("_userpic_"); }
102 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
103
104 /* The users photo display / upload facility */
105 void display_editpic(void) {
106         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_userpic_")));
107         putbstr("__PICDESC", NewStrBufPlain(_("your photo"), -1));
108         putbstr("__UPLURL", NewStrBufPlain(HKEY("editpic")));
109         display_graphics_upload("editpic");
110 }
111 /* room picture dispay / upload facility */
112 void display_editroompic(void) {
113         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_roompic_")));
114         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this room"), -1));
115         putbstr("__UPLURL", NewStrBufPlain(HKEY("editroompic")));
116         display_graphics_upload("editroompic");
117 }
118
119 /* the greetingpage hello pic */
120 void display_edithello(void) {
121         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("hello")));
122         putbstr("__PICDESC", NewStrBufPlain(_("the Greetingpicture for the login prompt"), -1));
123         putbstr("__UPLURL", NewStrBufPlain(HKEY("edithellopic")));
124         display_graphics_upload("edithellopic");
125 }
126
127 /* the logoff banner */
128 void display_editgoodbyepic(void) {
129         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("UIMG 0|%s|goodbuye")));
130         putbstr("__PICDESC", NewStrBufPlain(_("the Logoff banner picture"), -1));
131         putbstr("__UPLURL", NewStrBufPlain(HKEY("editgoodbuyepic")));
132         display_graphics_upload("editgoodbuyepic");
133 }
134
135 void display_editfloorpic(void) {
136         StrBuf *PicAction;
137
138         PicAction = NewStrBuf();
139         StrBufPrintf(PicAction, "_floorpic_|%s", bstr("which_floor"));
140         putbstr("__WHICHPIC", PicAction);
141         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this floor"), -1));
142         putbstr("__UPLURL", NewStrBufPlain(HKEY("editfloorpic")));
143         display_graphics_upload("editfloorpic");
144 }
145
146 void editroompic(void) {
147         char buf[SIZ];
148         snprintf(buf, SIZ, "_roompic_|%s",
149                  bstr("which_room"));
150         do_graphics_upload(buf);
151 }
152
153 void editfloorpic(void){
154         char buf[SIZ];
155         snprintf(buf, SIZ, "_floorpic_|%s",
156                  bstr("which_floor"));
157         do_graphics_upload(buf);
158 }
159
160 void 
161 InitModule_GRAPHICS
162 (void)
163 {
164         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
165         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
166         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
167         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
168         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
169         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
170         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
171         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
172         WebcitAddUrlHandler(HKEY("display_editfloorpic"), "", 0, display_editfloorpic, 0);
173         WebcitAddUrlHandler(HKEY("editfloorpic"), "", 0, editfloorpic, 0);
174 }