Incrase maximum size for content to 2 MiB
[Sone.git] / src / main / java / net / pterodactylus / sone / web / CreateSonePage.java
1 /*
2  * Sone - CreateSonePage.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import static java.util.logging.Logger.getLogger;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.core.Core;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
33 import net.pterodactylus.sone.web.page.FreenetRequest;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
36 import net.pterodactylus.util.web.Method;
37 import freenet.clients.http.ToadletContext;
38
39 /**
40  * The “create Sone” page lets the user create a new Sone.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class CreateSonePage extends SoneTemplatePage {
45
46         /** The logger. */
47         private static final Logger logger = getLogger(CreateSonePage.class.getName());
48
49         /**
50          * Creates a new “create Sone” page.
51          *
52          * @param template
53          *            The template to render
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public CreateSonePage(Template template, WebInterface webInterface) {
58                 super("createSone.html", template, "Page.CreateSone.Title", webInterface, false);
59         }
60
61         //
62         // STATIC ACCESSORS
63         //
64
65         /**
66          * Returns a sorted list of all own identities that do not have the “Sone”
67          * context.
68          *
69          * @param core
70          *            The core
71          * @return The list of own identities without the “Sone” context
72          */
73         public static List<OwnIdentity> getOwnIdentitiesWithoutSone(Core core) {
74                 List<OwnIdentity> identitiesWithoutSone = new ArrayList<OwnIdentity>();
75                 Set<OwnIdentity> allOwnIdentity = core.getIdentityManager().getAllOwnIdentities();
76                 for (OwnIdentity ownIdentity : allOwnIdentity) {
77                         if (!ownIdentity.hasContext("Sone")) {
78                                 identitiesWithoutSone.add(ownIdentity);
79                         }
80                 }
81                 Collections.sort(identitiesWithoutSone, new Comparator<OwnIdentity>() {
82
83                         @Override
84                         public int compare(OwnIdentity leftIdentity, OwnIdentity rightIdentity) {
85                                 return (leftIdentity.getNickname() + "@" + leftIdentity.getId()).compareToIgnoreCase(rightIdentity.getNickname() + "@" + rightIdentity.getId());
86                         }
87                 });
88                 return identitiesWithoutSone;
89         }
90
91         //
92         // TEMPLATEPAGE METHODS
93         //
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
100                 List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
101                 Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
102                 templateContext.set("sones", localSones);
103                 List<OwnIdentity> ownIdentitiesWithoutSone = getOwnIdentitiesWithoutSone(webInterface.getCore());
104                 templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
105                 if (request.getMethod() == Method.POST) {
106                         String id = request.getHttpRequest().getPartAsStringFailsafe("identity", 44);
107                         OwnIdentity selectedIdentity = null;
108                         for (OwnIdentity ownIdentity : ownIdentitiesWithoutSone) {
109                                 if (ownIdentity.getId().equals(id)) {
110                                         selectedIdentity = ownIdentity;
111                                         break;
112                                 }
113                         }
114                         if (selectedIdentity == null) {
115                                 templateContext.set("errorNoIdentity", true);
116                                 return;
117                         }
118                         /* create Sone. */
119                         Sone sone = webInterface.getCore().createSone(selectedIdentity);
120                         if (sone == null) {
121                                 logger.log(Level.SEVERE, String.format("Could not create Sone for OwnIdentity: %s", selectedIdentity));
122                                 /* TODO - go somewhere else */
123                         }
124
125                         /* log in the new Sone. */
126                         setCurrentSone(request.getToadletContext(), sone);
127                         throw new RedirectException("index.html");
128                 }
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         public boolean isEnabled(ToadletContext toadletContext) {
136                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
137                         return false;
138                 }
139                 return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1);
140         }
141
142 }