Update year in copyright lines
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.java - Copyright © 2010–2019 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.template;
19
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;
25
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import net.pterodactylus.sone.core.Core;
30 import net.pterodactylus.sone.data.Profile;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.data.Sone.SoneStatus;
33 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
34 import net.pterodactylus.sone.freenet.wot.Trust;
35 import net.pterodactylus.sone.text.TimeTextConverter;
36 import net.pterodactylus.util.template.Accessor;
37 import net.pterodactylus.util.template.ReflectionAccessor;
38 import net.pterodactylus.util.template.TemplateContext;
39
40 /**
41  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
42  * <dl>
43  * <dt>niceName</dt>
44  * <dd>Will show a combination of first name, middle name, and last name, if
45  * available, otherwise the username of the Sone is returned.</dd>
46  * <dt>friend</dt>
47  * <dd>Will return {@code true} if the sone in question is a friend of the
48  * currently logged in Sone (as determined by accessing the “currentSone”
49  * variable of the given {@link TemplateContext}).</dd>
50  * <dt>current</dt>
51  * <dd>Will return {@code true} if the sone in question is the currently logged
52  * in Sone.</dd>
53  * </dl>
54  */
55 public class SoneAccessor extends ReflectionAccessor {
56
57         /** The logger. */
58         private static final Logger logger = getLogger(SoneAccessor.class.getName());
59
60         /** The core. */
61         private final Core core;
62         private final TimeTextConverter timeTextConverter;
63
64         /**
65          * Creates a new Sone accessor.
66          *
67          * @param core
68          *            The Sone core
69          */
70         public SoneAccessor(Core core, TimeTextConverter timeTextConverter) {
71                 this.core = core;
72                 this.timeTextConverter = timeTextConverter;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         public Object get(TemplateContext templateContext, Object object, String member) {
80                 Sone sone = (Sone) object;
81                 if (member.equals("niceName")) {
82                         return getNiceName(sone);
83                 } else if (member.equals("friend")) {
84                         Sone currentSone = (Sone) templateContext.get("currentSone");
85                         return (currentSone != null) && currentSone.hasFriend(sone.getId());
86                 } else if (member.equals("current")) {
87                         Sone currentSone = (Sone) templateContext.get("currentSone");
88                         return (currentSone != null) && currentSone.equals(sone);
89                 } else if (member.equals("modified")) {
90                         return core.isModifiedSone(sone);
91                 } else if (member.equals("status")) {
92                         return sone.getStatus();
93                 } else if (member.equals("unknown")) {
94                         return sone.getStatus() == SoneStatus.unknown;
95                 } else if (member.equals("idle")) {
96                         return sone.getStatus() == SoneStatus.idle;
97                 } else if (member.equals("inserting")) {
98                         return sone.getStatus() == SoneStatus.inserting;
99                 } else if (member.equals("downloading")) {
100                         return sone.getStatus() == SoneStatus.downloading;
101                 } else if (member.equals("new")) {
102                         return !sone.isKnown();
103                 } else if (member.equals("locked")) {
104                         return core.isLocked(sone);
105                 } else if (member.equals("lastUpdatedText")) {
106                         return timeTextConverter.getTimeText(sone.getTime()).getL10nText();
107                 } else if (member.equals("trust")) {
108                         Sone currentSone = (Sone) templateContext.get("currentSone");
109                         if (currentSone == null) {
110                                 return null;
111                         }
112                         Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
113                         logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
114                         if (trust == null) {
115                                 return new Trust(null, null, null);
116                         }
117                         return trust;
118                 } else if (member.equals("allImages")) {
119                         return from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
120                 } else if (member.equals("albums")) {
121                         return sone.getRootAlbum().getAlbums();
122                 }
123                 return super.get(templateContext, object, member);
124         }
125
126         //
127         // STATIC METHODS
128         //
129
130         /**
131          * Returns the nice name of the given Sone.
132          *
133          * @param sone
134          *            The Sone to get the nice name for
135          * @return The nice name of the Sone
136          */
137         public static String getNiceName(Sone sone) {
138                 Profile profile = sone.getProfile();
139                 String firstName = profile.getFirstName();
140                 String middleName = profile.getMiddleName();
141                 String lastName = profile.getLastName();
142                 if (firstName == null) {
143                         if (lastName == null) {
144                                 return String.valueOf(sone.getName());
145                         }
146                         return lastName;
147                 }
148                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
149         }
150
151 }