PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/razpub/src/razie/assets/AssetBriefImpl.scala

http://razpub.googlecode.com/
Scala | 259 lines | 132 code | 45 blank | 82 comment | 17 complexity | 0fee983964a21ff8d3288e5dc7455598 MD5 | raw file
  1/**
  2 * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
  3 * details. No warranty implied nor any liability assumed for this code.
  4 */
  5package razie.assets
  6
  7import java.util.HashMap;
  8import java.util.Map;
  9
 10import org.json.JSONException;
 11import org.json.JSONObject;
 12import org.json.JSONString;
 13
 14import com.razie.pub.assets._
 15import com.razie.pub.base._
 16import com.razie.pub.base.data.HtmlRenderUtils;
 17import com.razie.pub.base.data.HttpUtils;
 18import com.razie.pub.base.data.MimeUtils;
 19import com.razie.pub.comms.SimpleActionToInvoke;
 20import com.razie.pub.comms.Agents;
 21import com.razie.pub.comms.LightAuth;
 22import com.razie.pub.comms.ServiceActionToInvoke;
 23import razie.draw._
 24import com.razie.pub.resources._
 25import razie.base._
 26
 27object NoAssetBrief extends AssetBriefImpl (new AssetKey("Unknown", "Unknown")) {}
 28
 29class AssetBriefImpl (var key : AssetKey) extends AttrAccessImpl with AssetBrief { 
 30
 31   def this () = this (null)
 32  
 33   def this (key:AssetKey, name:String, icon:String, briefDesc:String) = {
 34      this (key)
 35      this.name=name
 36      this.icon=icon
 37      this.briefDesc=briefDesc
 38   }
 39   
 40   override def getKey() = this.key 
 41   def setKey(k:AssetKey ) = this.key = k
 42
 43   protected def toAA() = {
 44      val a:AttrAccess = this;
 45      a.set("ref", getKey().toUrlEncodedString);
 46      a.set("name", name)
 47      a.set("icon", icon)
 48      a.set("image", image)
 49      a.set("briefDesc", briefDesc)
 50      a.set("largeDesc", largeDesc)
 51      a.set("urlForDetails", getUrlForDetails().makeActionUrl());
 52      a;
 53   }
 54
 55//   override def toString = toAA.toString
 56
 57   def toJSONString =  toAA.toJson(new JSONObject()).toString()
 58   
 59   override def toXml = "<SdkAssetBrief>" + toAA().toXml() + "</SdkAssetBrief>"
 60
 61    /** shortcut to render self - don't like controllers that much */
 62   override def render(t:Technology , stream:DrawStream ) :AnyRef = 
 63        Renderer.Helper.draw(this, t, stream);
 64   override def getRenderer(t:Technology ) : Renderer[AnyRef] = new ABRender(DetailLevel.LIST)
 65   
 66   /**
 67    * for streamable assets (movie, music, photo) an URL you can use to stream the asset
 68    * 
 69    * TODO define the protocol to serve/get/use the description
 70    * 
 71    * @return a url you can use to stream the asset or null if streaming is not supported. the url
 72    *         you can use to stream the file - it includes the filename just so IE can figure out
 73    *         what to do with it (open the right player etc)...
 74    */
 75   private[this] var _urlForStreaming:ActionToInvoke = null
 76   override def urlForStreaming = _urlForStreaming
 77   override def urlForStreaming_= (a:ActionToInvoke) = _urlForStreaming=a
 78}
 79
 80class FileAssetBriefImpl extends AssetBriefImpl with FileAssetBrief {
 81   override protected def toAA() = {
 82      val a:AttrAccess = super.toAA()
 83      a.set("localDir", getLocalDir());
 84      a.set("fileName", getFileName());
 85      a;
 86   }
 87   
 88   def toUpnpItem(parentID:String ) = {
 89      var s = "\n<item id=\"" + getKey().toUrlEncodedString + "\" parentID=\"" + parentID + "\" restricted=\"false\"" + ">\n";
 90
 91      val a = new AttrAccessImpl();
 92
 93      a.set("dc\\:title", getName());
 94      a.set("upnp\\:class", AssetBrief.upnptypes.get(getKey().getType()).getOrElse("UNKNOWN type: "+getKey().getType()));
 95      // if (series != null)
 96      // a.setAttr("series", getSeries().toUrlEncodedString());
 97
 98      // a.setAttr("upnp\\:genre", "");
 99      // a.setAttr("upnp\\:longDescription", getLargeDesc());
100      a.set("dc\\:description", getBriefDesc());
101
102      // a.setAttr("upnp\\:storageMedium", "");
103      // a.setAttr("upnp\\:channelName", "");
104
105      s += a.toXml();
106
107      val p = "\n<res protocolInfo=\"http-get:*:" + getMimeType() + ":*\" size=\"200000\">" + getUrlForStreaming().makeActionUrl() + "</res>";
108
109      s + p + "\n</item>";
110   }
111
112   /**
113    * for streamable assets (movie, music, photo) an URL you can use to stream the asset
114    * 
115    * TODO define the protocol to serve/get/use the description
116    * 
117    * @return a url you can use to stream the asset or null if streaming is not supported. the url
118    *         you can use to stream the file - it includes the filename just so IE can figure out
119    *         what to do with it (open the right player etc)...
120    */
121   override def urlForStreaming = {
122      // TODO 1-1 series
123//      if (series != null) {
124//         new ActionToInvoke(new ActionItem(AssetBrief.STREAM.name + "/" + this.fileName,
125//               RazIcons.DOWNLOAD), "ref", getKey().toUrlEncodedString, "series", this.series.toString());
126         new SimpleActionToInvoke(new ActionItem(AssetBrief.STREAM.name + "/" + this.fileName,
127               RazIcons.DOWNLOAD.name()), "ref", getKey().toUrlEncodedString);
128   }
129   
130   override def urlForStreaming_= (a:ActionToInvoke) = throw new UnsupportedOperationException()
131}
132
133class ABDrawable (val brief:AssetBrief, val detailLevel:DetailLevel) extends Drawable3 {
134    /** shortcut to render self - don't like controllers that much */
135   override def render(t:Technology , stream:DrawStream ) :AnyRef = 
136        Renderer.Helper.draw(brief, t, stream);
137   override def getRenderer(t:Technology ) : Renderer[AnyRef] = new ABRender(DetailLevel.LIST)
138}
139
140class ABRender (val detailLevel:DetailLevel) extends Renderer[AnyRef] {
141
142   override def render(o:AnyRef , t:Technology , stream:DrawStream ) : AnyRef = {
143            val b : AssetBrief = o match {
144               case b:AssetBrief => b
145               case abd : ABDrawable => abd.brief
146            }
147            
148            if (Technology.HTML.equals(t)) {
149                return toHtml(b, detailLevel);
150            } else if (Technology.XML.equals(t)) {
151                return b.toXml;
152            } else if (Technology.JSON.equals(t)) {
153                return b.toJSONString;
154            } else if (Technology.UPNP.equals(t)) {
155                return b.asInstanceOf[FileAssetBrief].toUpnpItem(b.getParentID());
156            }
157
158            // default rendering
159            return b.toString();
160        }
161
162        /**
163         * @param b
164         * @return
165         */
166        protected def toHtml(b:AssetBrief, detailLevel:DetailLevel) : AnyRef = {
167            var s = "<table align=middle><tr>";
168            val img = b.getIconImgUrl();
169
170            // NavButton button = new NavButton(DETAILS, "");
171
172            var width = detailLevel match {
173               case DetailLevel.LIST => "80"
174               case DetailLevel.LARGE => "150" 
175               case DetailLevel.BRIEFLIST => "30" 
176               case DetailLevel.FULL =>"400" 
177               case _ => "300"
178            }
179
180            // THIS IS FUCKED - the nokia770 tablet browser has a problem if only height is
181            // specified. it accepts width however !!!!
182
183            if (detailLevel.equals(DetailLevel.LARGE)) {
184                s += "<td align=center>" + "<a href=\"" + b.getUrlForDetails().makeActionUrl() + "\">" + "<img border=0 " + " width=\"" + width + "\" " + " src=\"" + img + "\"/>" + "</a>";
185                s += "<br><b>" + b.getName() + "</b>";
186            } else {
187                s += "<td>" + "<a href=\"" + b.getUrlForDetails().makeActionUrl() + "\">" + "<img border=0 " + " width=\"" + width + "\" " + " src=\"" + img + "\"/>" + "</a>";
188                s += "</td>";
189                s += "<td><b>" + b.getName() + "</b><br>";
190                s += b.getBriefDesc() + "<br>";
191                if (detailLevel.equals(DetailLevel.LIST)) {
192                    s += b.getLargeDesc() + "<br>";
193                } else if (detailLevel.equals(DetailLevel.FULL)) {
194                    s += b.getLargeDesc() + "<br>";
195
196                    if (b.isInstanceOf[FileAssetBrief] && b.asInstanceOf[FileAssetBriefImpl].getLocalDir() != null)
197                        s += b.asInstanceOf[FileAssetBriefImpl].getLocalDir().replace("/", " /") + "<br>";
198                }
199            }
200
201            // IF full details, then print all buttons inside:
202            if (detailLevel.equals(DetailLevel.FULL)) {
203                val l = new DrawList();
204                for (a <- razie.M apply AssetMgr.pres().makeAllButtons(b, false))
205                    l.write(a);
206
207                s += l.render(Technology.HTML, null);
208            }
209
210            s += "</td>";
211
212            s += "</tr>";
213            s += "</table>";
214
215            return s;//HtmlRenderUtils.textToHtml(s);
216        }
217
218}
219
220
221//   // TODO 1-2 setup the entire rss feed thing. implies format specs
222//   public String toRssMediaItem() {
223//       String s = "\n<item>\n";
224//
225//       AttrAccess a = new AttrAccessImpl();
226//
227//       a.setAttr("title", getName());
228//       a.setAttr("media:title", getName());
229//       a.setAttr("link", "?");// TODO build page to view the item
230//       a.setAttr("media:player", "?");
231//
232////       a.setAttr("upnp:class", "object.item.videoItem.movie");
233//       // if (series != null)
234//       // a.setAttr("series", getSeries().toUrlEncodedString());
235//
236////       a.setAttr("upnp:genre", "");
237//       a.setAttr("description", getLargeDesc());
238//       a.setAttr("media:description", getBriefDesc());
239//
240//       a.setAttr("media:category", "?");
241//
242////       a.setAttr("upnp:storageMedium", "");
243////       a.setAttr("upnp:channelName", "");
244//
245//       s += a.toXml();
246//
247//       // String p = "\n<res protocolInfo=\"http-get:*:" + getMimeType() + ":*\" size=\"200000\">"
248//       // + "http://"
249//       // + Devices.getMyUrl() + "/mutant/stream?ref=" + getRef().toUrlEncodedString() + "</res>";
250//       String p = "\n<res protocolInfo=\"http-get:*:" + getMimeType() + ":*\" size=\"200000\">"
251//               + getUrlForStreaming().makeActionUrl() + "</res>";
252//
253//       return s + p + "\n</item>";
254//   }
255
256//    @Override 
257//    public String toJSONString () {
258//       toAA.toJson(new JSONObject()).toString();
259//    }