9dec1dce16bfadd8e61223610239639db7f54c2c
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ImageBrowserPage.java
1 /*
2  * Sone - ImageBrowserPage.java - Copyright © 2011–2013 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 net.pterodactylus.sone.data.Album.FLATTENER;
21 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
22 import static net.pterodactylus.sone.data.Album.TITLE_COMPARATOR;
23
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import com.google.common.base.Optional;
30 import com.google.common.collect.FluentIterable;
31
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.web.page.FreenetRequest;
36 import net.pterodactylus.util.collection.Pagination;
37 import net.pterodactylus.util.number.Numbers;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateContext;
40
41 /**
42  * The image browser page is the entry page for the image management.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class ImageBrowserPage extends SoneTemplatePage {
47
48         /**
49          * Creates a new image browser page.
50          *
51          * @param template
52          *            The template to render
53          * @param webInterface
54          *            The Sone web interface
55          */
56         public ImageBrowserPage(Template template, WebInterface webInterface) {
57                 super("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true);
58         }
59
60         //
61         // SONETEMPLATEPAGE METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
69                 super.processTemplate(request, templateContext);
70                 String albumId = request.getHttpRequest().getParam("album", null);
71                 if (albumId != null) {
72                         Album album = webInterface.getCore().getAlbum(albumId, false);
73                         templateContext.set("albumRequested", true);
74                         templateContext.set("album", album);
75                         templateContext.set("page", request.getHttpRequest().getParam("page"));
76                         return;
77                 }
78                 String imageId = request.getHttpRequest().getParam("image", null);
79                 if (imageId != null) {
80                         Image image = webInterface.getCore().getImage(imageId, false);
81                         templateContext.set("imageRequested", true);
82                         templateContext.set("image", image);
83                         return;
84                 }
85                 String soneId = request.getHttpRequest().getParam("sone", null);
86                 if (soneId != null) {
87                         Optional<Sone> sone = webInterface.getCore().getSone(soneId);
88                         templateContext.set("soneRequested", true);
89                         templateContext.set("sone", sone.orNull());
90                         return;
91                 }
92                 String mode = request.getHttpRequest().getParam("mode", null);
93                 if ("gallery".equals(mode)) {
94                         templateContext.set("galleryRequested", true);
95                         List<Album> albums = new ArrayList<Album>();
96                         for (Sone sone : webInterface.getCore().getSones()) {
97                                 albums.addAll(FluentIterable.from(sone.getAlbums()).transformAndConcat(FLATTENER).filter(NOT_EMPTY).toList());
98                         }
99                         Collections.sort(albums, TITLE_COMPARATOR);
100                         Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
101                         templateContext.set("albumPagination", albumPagination);
102                         templateContext.set("albums", albumPagination.getItems());
103                         return;
104                 }
105                 Sone sone = getCurrentSone(request.getToadletContext(), false);
106                 templateContext.set("soneRequested", true);
107                 templateContext.set("sone", sone);
108         }
109
110         /**
111          * {@inheritDoc}
112          */
113         @Override
114         public boolean isLinkExcepted(URI link) {
115                 return true;
116         }
117
118 }