Remove obsolete loading animation
[Sone.git] / src / main / java / net / pterodactylus / sone / web / EditProfilePage.java
index 380e054..cffdeb9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - EditProfilePage.java - Copyright © 2010 David Roden
+ * Sone - EditProfilePage.java - Copyright © 2010–2016 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.web;
 
+import static net.pterodactylus.sone.text.TextFilter.filter;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
 import java.util.List;
 
 import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.DuplicateField;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.page.Page.Request.Method;
-import net.pterodactylus.util.number.Numbers;
-import net.pterodactylus.util.template.DataProvider;
+import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.web.Method;
 import freenet.clients.http.ToadletContext;
 
 /**
@@ -55,8 +59,7 @@ public class EditProfilePage extends SoneTemplatePage {
         * {@inheritDoc}
         */
        @Override
-       protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException {
-               super.processTemplate(request, dataProvider);
+       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                ToadletContext toadletContenxt = request.getToadletContext();
                Sone currentSone = getCurrentSone(toadletContenxt);
                Profile profile = currentSone.getProfile();
@@ -66,37 +69,40 @@ public class EditProfilePage extends SoneTemplatePage {
                Integer birthDay = profile.getBirthDay();
                Integer birthMonth = profile.getBirthMonth();
                Integer birthYear = profile.getBirthYear();
+               String avatarId = profile.getAvatar();
                List<Field> fields = profile.getFields();
                if (request.getMethod() == Method.POST) {
                        if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
                                firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
                                middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
                                lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
-                               birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
-                               birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
-                               birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
+                               birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null);
+                               birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null);
+                               birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null);
+                               avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
                                profile.setFirstName(firstName.length() > 0 ? firstName : null);
                                profile.setMiddleName(middleName.length() > 0 ? middleName : null);
                                profile.setLastName(lastName.length() > 0 ? lastName : null);
                                profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
+                               profile.setAvatar(webInterface.getCore().getImage(avatarId, false));
                                for (Field field : fields) {
                                        String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
-                                       field.setValue(value);
+                                       String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value);
+                                       field.setValue(filteredValue);
                                }
                                currentSone.setProfile(profile);
-                               webInterface.getCore().saveSone(currentSone);
+                               webInterface.getCore().touchConfiguration();
                                throw new RedirectException("editProfile.html");
                        } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
                                String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
                                try {
                                        profile.addField(fieldName);
                                        currentSone.setProfile(profile);
-                                       fields = profile.getFields();
-                                       webInterface.getCore().saveSone(currentSone);
+                                       webInterface.getCore().touchConfiguration();
                                        throw new RedirectException("editProfile.html#profile-fields");
-                               } catch (IllegalArgumentException iae1) {
-                                       dataProvider.set("fieldName", fieldName);
-                                       dataProvider.set("duplicateFieldName", true);
+                               } catch (DuplicateField df1) {
+                                       templateContext.set("fieldName", fieldName);
+                                       templateContext.set("duplicateFieldName", true);
                                }
                        } else {
                                String id = getFieldId(request, "delete-field-");
@@ -129,13 +135,14 @@ public class EditProfilePage extends SoneTemplatePage {
                                }
                        }
                }
-               dataProvider.set("firstName", firstName);
-               dataProvider.set("middleName", middleName);
-               dataProvider.set("lastName", lastName);
-               dataProvider.set("birthDay", birthDay);
-               dataProvider.set("birthMonth", birthMonth);
-               dataProvider.set("birthYear", birthYear);
-               dataProvider.set("fields", fields);
+               templateContext.set("firstName", firstName);
+               templateContext.set("middleName", middleName);
+               templateContext.set("lastName", lastName);
+               templateContext.set("birthDay", birthDay);
+               templateContext.set("birthMonth", birthMonth);
+               templateContext.set("birthYear", birthYear);
+               templateContext.set("avatarId", avatarId);
+               templateContext.set("fields", fields);
        }
 
        //
@@ -153,7 +160,7 @@ public class EditProfilePage extends SoneTemplatePage {
         * @return The parsed ID, or {@code null} if there was no part matching the
         *         given string
         */
-       private String getFieldId(Request request, String partNameStart) {
+       private static String getFieldId(FreenetRequest request, String partNameStart) {
                for (String partName : request.getHttpRequest().getParts()) {
                        if (partName.startsWith(partNameStart)) {
                                return partName.substring(partNameStart.length());