public static void addSubstitutions(Substitutions substituter,
                                     GadgetSpec spec, UserPrefs values)
 {
     foreach (UserPref pref in spec.getUserPrefs())
     {
         String name  = pref.getName();
         String value = values.getPref(name);
         if (value == null)
         {
             value = pref.getDefaultValue();
             if (value == null)
             {
                 value = "";
             }
         }
         substituter.addSubstitution(Substitutions.Type.USER_PREF, name, HttpUtility.HtmlEncode(value));
     }
 }
Exemple #2
0
        private JsonObject CallJob(GadgetContext context)
        {
            try
            {
                JsonObject gadgetJson = new JsonObject();
                Gadget     gadget     = Processor.Process(context);

                GadgetSpec  spec  = gadget.getSpec();
                ModulePrefs prefs = spec.getModulePrefs();

                // TODO: modularize response fields based on requested items.
                JsonObject views = new JsonObject();
                foreach (View view in spec.getViews().Values)
                {
                    views.Put(view.getName(), new JsonObject()
                              // .Put("content", view.getContent())
                              .Put("type", view.getType().ToString().ToLower())
                              .Put("quirks", view.getQuirks())
                              .Put("preferredHeight", view.getPreferredHeight())
                              .Put("preferredWidth", view.getPreferredWidth()));
                }

                // Features.
                List <String> feats = new List <String>();
                foreach (var entry in prefs.getFeatures())
                {
                    feats.Add(entry.Key);
                }
                string[] features = new string[feats.Count];
                feats.CopyTo(features, 0);

                // Links
                JsonObject links = new JsonObject();
                foreach (LinkSpec link in prefs.getLinks().Values)
                {
                    links.Put(link.getRel(), link.getHref());
                }

                JsonObject userPrefs = new JsonObject();

                // User pref specs
                foreach (UserPref pref in spec.getUserPrefs())
                {
                    JsonObject up = new JsonObject()
                                    .Put("displayName", pref.getDisplayName())
                                    .Put("type", pref.getDataType().ToString().ToLower())
                                    .Put("default", pref.getDefaultValue())
                                    .Put("enumValues", pref.getEnumValues())
                                    .Put("orderedEnumValues", getOrderedEnums(pref));
                    userPrefs.Put(pref.getName(), up);
                }

                // TODO: This should probably just copy all data from
                // ModulePrefs.getAttributes(), but names have to be converted to
                // camel case.
                gadgetJson.Put("iframeUrl", UrlGenerator.getIframeUrl(gadget))
                .Put("url", context.getUrl().ToString())
                .Put("moduleId", context.getModuleId())
                .Put("title", prefs.getTitle())
                .Put("titleUrl", prefs.getTitleUrl().ToString())
                .Put("views", views)
                .Put("features", features)
                .Put("userPrefs", userPrefs)
                .Put("links", links)

                // extended meta data
                .Put("directoryTitle", prefs.getDirectoryTitle())
                .Put("description", prefs.getDescription())
                .Put("thumbnail", prefs.getThumbnail().ToString())
                .Put("screenshot", prefs.getScreenshot().ToString())
                .Put("author", prefs.getAuthor())
                .Put("authorEmail", prefs.getAuthorEmail())
                .Put("authorAffiliation", prefs.getAuthorAffiliation())
                .Put("authorLocation", prefs.getAuthorLocation())
                .Put("authorPhoto", prefs.getAuthorPhoto())
                .Put("authorAboutme", prefs.getAuthorAboutme())
                .Put("authorQuote", prefs.getAuthorQuote())
                .Put("authorLink", prefs.getAuthorLink())
                .Put("categories", prefs.getCategories())
                .Put("screenshot", prefs.getScreenshot().ToString())
                .Put("height", prefs.getHeight())
                .Put("width", prefs.getWidth())
                .Put("showStats", prefs.getShowStats())
                .Put("showInDirectory", prefs.getShowInDirectory())
                .Put("singleton", prefs.getSingleton())
                .Put("scaling", prefs.getScaling())
                .Put("scrolling", prefs.getScrolling());
                return(gadgetJson);
            }
            catch (ProcessingException e)
            {
                throw new RpcException(context, e);
                //throw e;
            }
            catch (JsonException e)
            {
                // Shouldn't be possible
                throw new RpcException(context, e);
            }
        }