2 * Sone - SoneAccessor.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template;
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.util.Arrays.asList;
22 import static java.util.logging.Logger.getLogger;
23 import static net.pterodactylus.sone.data.Album.FLATTENER;
24 import static net.pterodactylus.sone.data.Album.IMAGES;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.Core;
30 import net.pterodactylus.sone.data.LocalSone;
31 import net.pterodactylus.sone.data.Profile;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.sone.data.Sone.SoneStatus;
34 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
35 import net.pterodactylus.sone.freenet.wot.Trust;
36 import net.pterodactylus.sone.web.WebInterface;
37 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
38 import net.pterodactylus.util.template.Accessor;
39 import net.pterodactylus.util.template.ReflectionAccessor;
40 import net.pterodactylus.util.template.TemplateContext;
43 * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
46 * <dd>Will show a combination of first name, middle name, and last name, if
47 * available, otherwise the username of the Sone is returned.</dd>
49 * <dd>Will return {@code true} if the sone in question is a friend of the
50 * currently logged in Sone (as determined by accessing the “currentSone”
51 * variable of the given {@link TemplateContext}).</dd>
53 * <dd>Will return {@code true} if the sone in question is the currently logged
57 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59 public class SoneAccessor extends ReflectionAccessor {
62 private static final Logger logger = getLogger("Sone.Data");
65 private final Core core;
68 * Creates a new Sone accessor.
73 public SoneAccessor(Core core) {
81 public Object get(TemplateContext templateContext, Object object, String member) {
82 Sone sone = (Sone) object;
83 if (member.equals("niceName")) {
84 return getNiceName(sone);
85 } else if (member.equals("friend")) {
86 Sone currentSone = (Sone) templateContext.get("currentSone");
87 return (currentSone != null) && currentSone.hasFriend(sone.getId());
88 } else if (member.equals("current")) {
89 Sone currentSone = (Sone) templateContext.get("currentSone");
90 return (currentSone != null) && currentSone.equals(sone);
91 } else if (member.equals("modified")) {
92 return sone.isLocal() ? core.isModifiedSone((LocalSone) sone) : false;
93 } else if (member.equals("status")) {
94 return sone.getStatus();
95 } else if (member.equals("unknown")) {
96 return sone.getStatus() == SoneStatus.unknown;
97 } else if (member.equals("idle")) {
98 return sone.getStatus() == SoneStatus.idle;
99 } else if (member.equals("inserting")) {
100 return sone.getStatus() == SoneStatus.inserting;
101 } else if (member.equals("downloading")) {
102 return sone.getStatus() == SoneStatus.downloading;
103 } else if (member.equals("new")) {
104 return !sone.isKnown();
105 } else if (member.equals("locked")) {
106 return sone.isLocal() ? core.isLocked((LocalSone) sone) : false;
107 } else if (member.equals("lastUpdatedText")) {
108 return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
109 } else if (member.equals("trust")) {
110 Sone currentSone = (Sone) templateContext.get("currentSone");
111 if (currentSone == null) {
114 Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
115 logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
117 return new Trust(null, null, null);
120 } else if (member.equals("allImages")) {
121 return from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
122 } else if (member.equals("albums")) {
123 return sone.getRootAlbum().getAlbums();
125 return super.get(templateContext, object, member);
133 * Returns the nice name of the given Sone.
136 * The Sone to get the nice name for
137 * @return The nice name of the Sone
139 public static String getNiceName(Sone sone) {
140 Profile profile = sone.getProfile();
141 String firstName = profile.getFirstName();
142 String middleName = profile.getMiddleName();
143 String lastName = profile.getLastName();
144 if (firstName == null) {
145 if (lastName == null) {
146 return String.valueOf(sone.getName());
150 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");