X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FSoneAccessor.java;h=50fa2720330f127e6fb3945466bb073a1ce6da77;hb=144cf48d9b40d0379456d61a504999612f530733;hp=97cf32751de0ef0d1c28bb3af9a677b4682adb77;hpb=44f2f20011d28bc1420dcfcc8b9ff61183e86e24;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java b/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java
index 97cf327..50fa272 100644
--- a/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java
+++ b/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java
@@ -1,5 +1,5 @@
/*
- * Sone - SoneAccessor.java - Copyright © 2010 David Roden
+ * Sone - SoneAccessor.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
@@ -17,11 +17,26 @@
package net.pterodactylus.sone.template;
+import static com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.Album.FLATTENER;
+import static net.pterodactylus.sone.data.Album.IMAGES;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.Trust;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
import net.pterodactylus.util.template.Accessor;
-import net.pterodactylus.util.template.DataProvider;
import net.pterodactylus.util.template.ReflectionAccessor;
+import net.pterodactylus.util.template.TemplateContext;
/**
* {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
@@ -29,11 +44,11 @@ import net.pterodactylus.util.template.ReflectionAccessor;
*
niceName
* Will show a combination of first name, middle name, and last name, if
* available, otherwise the username of the Sone is returned.
- * isFriend
+ * friend
* Will return {@code true} if the sone in question is a friend of the
* currently logged in Sone (as determined by accessing the âcurrentSoneâ
- * variable of the given {@link DataProvider}).
- * isCurrent
+ * variable of the given {@link TemplateContext}).
+ * current
* Will return {@code true} if the sone in question is the currently logged
* in Sone.
*
@@ -42,25 +57,71 @@ import net.pterodactylus.util.template.ReflectionAccessor;
*/
public class SoneAccessor extends ReflectionAccessor {
+ /** The logger. */
+ private static final Logger logger = getLogger(SoneAccessor.class.getName());
+
+ /** The core. */
+ private final Core core;
+
+ /**
+ * Creates a new Sone accessor.
+ *
+ * @param core
+ * The Sone core
+ */
+ public SoneAccessor(Core core) {
+ this.core = core;
+ }
+
/**
* {@inheritDoc}
*/
@Override
- public Object get(DataProvider dataProvider, Object object, String member) {
+ public Object get(TemplateContext templateContext, Object object, String member) {
Sone sone = (Sone) object;
if (member.equals("niceName")) {
return getNiceName(sone);
- } else if (member.equals("isFriend")) {
- Sone currentSone = (Sone) dataProvider.getData("currentSone");
- return currentSone.hasFriend(sone) ? true : null;
- } else if (member.equals("isCurrent")) {
- Sone currentSone = (Sone) dataProvider.getData("currentSone");
- return currentSone.equals(sone);
- } else if (member.equals("blocked")) {
- Sone currentSone = (Sone) dataProvider.getData("currentSone");
- return currentSone.isSoneBlocked(sone.getId());
+ } else if (member.equals("friend")) {
+ Sone currentSone = (Sone) templateContext.get("currentSone");
+ return (currentSone != null) && currentSone.hasFriend(sone.getId());
+ } else if (member.equals("current")) {
+ Sone currentSone = (Sone) templateContext.get("currentSone");
+ return (currentSone != null) && currentSone.equals(sone);
+ } else if (member.equals("modified")) {
+ return core.isModifiedSone(sone);
+ } else if (member.equals("status")) {
+ return sone.getStatus();
+ } else if (member.equals("unknown")) {
+ return sone.getStatus() == SoneStatus.unknown;
+ } else if (member.equals("idle")) {
+ return sone.getStatus() == SoneStatus.idle;
+ } else if (member.equals("inserting")) {
+ return sone.getStatus() == SoneStatus.inserting;
+ } else if (member.equals("downloading")) {
+ return sone.getStatus() == SoneStatus.downloading;
+ } else if (member.equals("new")) {
+ return !sone.isKnown();
+ } else if (member.equals("locked")) {
+ return core.isLocked(sone);
+ } else if (member.equals("lastUpdatedText")) {
+ return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
+ } else if (member.equals("trust")) {
+ Sone currentSone = (Sone) templateContext.get("currentSone");
+ if (currentSone == null) {
+ return null;
+ }
+ Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
+ logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
+ if (trust == null) {
+ return new Trust(null, null, null);
+ }
+ return trust;
+ } else if (member.equals("allImages")) {
+ return from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
+ } else if (member.equals("albums")) {
+ return sone.getRootAlbum().getAlbums();
}
- return super.get(dataProvider, object, member);
+ return super.get(templateContext, object, member);
}
//
@@ -81,7 +142,7 @@ public class SoneAccessor extends ReflectionAccessor {
String lastName = profile.getLastName();
if (firstName == null) {
if (lastName == null) {
- return sone.getName();
+ return String.valueOf(sone.getName());
}
return lastName;
}