/// <summary> /// Generates a local url for the given media field. /// </summary> /// <param name="app">The application service</param> /// <param name="field">The field</param> /// <returns>The url</returns> public static string Url(this IApplicationService app, MediaField field) { if (field != null && field.Media != null) { return(Url(app, field.Media.PublicUrl)); } return(""); }
private ContentField CreateMediaField(MediaFieldDto mediaFieldDto) { var mediaField = new MediaField(); mediaField.Type = nameof(MediaField); var mediaItemDto = mediaFieldDto.Media.FirstOrDefault(); if (mediaItemDto != null) { mediaField.Media = Create(mediaItemDto); } return(mediaField); }
/// <summary> /// Implements support for data forms. Data Forms are defined in the following XEPs: /// /// XEP-0004: Data Forms: /// http://xmpp.org/extensions/xep-0004.html /// /// XEP-0122: Data Forms Validation: /// http://xmpp.org/extensions/xep-0122.html /// /// XEP-0141: Data Forms Layout /// http://xmpp.org/extensions/xep-0141.html /// /// XEP-0221: Data Forms Media Element /// http://xmpp.org/extensions/xep-0221.html /// /// XEP-0331: Data Forms - Color Field Types /// http://xmpp.org/extensions/xep-0331.html /// /// XEP-0336: Data Forms - Dynamic Forms /// http://xmpp.org/extensions/xep-0336.html /// /// XEP-0348: Signing Forms: /// http://xmpp.org/extensions/xep-0348.html /// </summary> /// <param name="Client">XMPP Client.</param> /// <param name="X">Data Form definition.</param> /// <param name="OnSubmit">Method called when the form is submitted.</param> /// <param name="OnCancel">Method called when the form is cancelled.</param> /// <param name="From">From where the form came.</param> /// <param name="To">To where the form was sent.</param> public DataForm(XmppClient Client, XmlElement X, DataFormEventHandler OnSubmit, DataFormEventHandler OnCancel, string From, string To) { List <string> Instructions = new List <string>(); List <Field> Fields = new List <Field>(); List <Field[]> Records = new List <Field[]>(); List <Page> Pages = null; this.client = Client; this.onSubmit = OnSubmit; this.onCancel = OnCancel; this.from = From; this.to = To; switch (XML.Attribute(X, "type").ToLower()) { case "cancel": this.type = FormType.Cancel; break; case "form": this.type = FormType.Form; break; case "result": this.type = FormType.Result; break; case "submit": this.type = FormType.Submit; break; default: this.type = FormType.Undefined; break; } foreach (XmlNode N in X.ChildNodes) { switch (N.LocalName) { case "instructions": Instructions.Add(N.InnerText.Trim()); break; case "title": this.title = N.InnerText.Trim(); break; case "field": Field Field = this.ParseField((XmlElement)N, out Media Media); Fields.Add(Field); if (Field.PostBack) { this.containsPostBackFields = true; } if (!string.IsNullOrEmpty(Field.Var)) { this.fieldsByVar[Field.Var] = Field; } if (Media != null) { Field = new MediaField(this, Guid.NewGuid().ToString(), string.Empty, false, null, null, string.Empty, new StringDataType(), new BasicValidation(), Media, string.Empty, false, true, false); Fields.Add(Field); this.fieldsByVar[Field.Var] = Field; this.hasMedia = true; } break; case "reported": List <Field> Header = new List <Field>(); foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "field") { Field = this.ParseField((XmlElement)N2, out Media); Header.Add(Field); } } this.header = Header.ToArray(); break; case "item": List <Field> Record = new List <Field>(); foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "field") { Field = this.ParseField((XmlElement)N2, out Media); Record.Add(Field); } } Records.Add(Record.ToArray()); break; case "page": if (Pages == null) { Pages = new List <Page>(); } Pages.Add(new Page(this, (XmlElement)N)); break; } } this.instructions = Instructions.ToArray(); this.fields = Fields.ToArray(); this.records = Records.ToArray(); if (this.header == null) { this.header = new Field[0]; } if (this.hasPages = (Pages != null)) { this.pages = Pages.ToArray(); } else if (this.fields.Length > 0) { this.pages = new Page[] { new Page(this, this.title, this.fields) } } ; else { this.pages = new Page[] { new Page(this, this.title, new ReportedReference(this)) } }; if (this.hasMedia) { Dictionary <string, byte[]> Bob = new Dictionary <string, byte[]>(StringComparer.CurrentCultureIgnoreCase); foreach (XmlNode N in X.ParentNode.ChildNodes) { if (N is XmlElement E && E.LocalName == "data" && E.NamespaceURI == BobClient.Namespace) { string Cid = XML.Attribute(E, "cid"); byte[] Bin = Convert.FromBase64String(E.InnerText); Bob["cid:" + Cid] = Bin; } } foreach (Field F in this.fields) { if (F is MediaField MediaField && MediaField.Media != null) { foreach (KeyValuePair <string, Uri> Uri in MediaField.Media.URIs) { switch (Uri.Value.Scheme.ToLower()) { case "cid": if (Bob.TryGetValue(Uri.Value.ToString(), out byte[] Bin))
public T AddMediaField(MediaField mediaField) { _mediaFields.Add(mediaField); return((T)this); }
private void Layout(Panel Container, MediaField Field, DataForm Form) { MediaElement MediaElement; Uri Uri = null; Grade Best = Runtime.Inventory.Grade.NotAtAll; Grade Grade; bool IsImage = false; bool IsVideo = false; bool IsAudio = false; bool TopMarginLaidOut = this.LayoutControlLabel(Container, Field); foreach (KeyValuePair <string, Uri> P in Field.Media.URIs) { if (P.Key.StartsWith("image/")) { IsImage = true; Uri = P.Value; break; } else if (P.Key.StartsWith("video/")) { switch (P.Key.ToLower()) { case "video/x-ms-asf": case "video/x-ms-wvx": case "video/x-ms-wm": case "video/x-ms-wmx": Grade = Grade.Perfect; break; case "video/mp4": Grade = Grade.Excellent; break; case "video/3gp": case "video/3gpp ": case "video/3gpp2 ": case "video/3gpp-tt": case "video/h263": case "video/h263-1998": case "video/h263-2000": case "video/h264": case "video/h264-rcdo": case "video/h264-svc": Grade = Grade.Ok; break; default: Grade = Grade.Barely; break; } if (Grade > Best) { Best = Grade; Uri = P.Value; IsVideo = true; } } else if (P.Key.StartsWith("audio/")) { switch (P.Key.ToLower()) { case "audio/x-ms-wma": case "audio/x-ms-wax": case "audio/x-ms-wmv": Grade = Grade.Perfect; break; case "audio/mp4": case "audio/mpeg": Grade = Grade.Excellent; break; case "audio/amr": case "audio/amr-wb": case "audio/amr-wb+": case "audio/pcma": case "audio/pcma-wb": case "audio/pcmu": case "audio/pcmu-wb": Grade = Grade.Ok; break; default: Grade = Grade.Barely; break; } if (Grade > Best) { Best = Grade; Uri = P.Value; IsAudio = true; } } } if (IsImage) { BitmapImage BitmapImage = new System.Windows.Media.Imaging.BitmapImage(); BitmapImage.BeginInit(); try { if (Field.Media.Binary != null) { BitmapImage.UriSource = new Uri(Waher.Content.Markdown.Model.Multimedia.ImageContent.GetTemporaryFile(Field.Media.Binary)); } else if (Uri != null) { BitmapImage.UriSource = Uri; } else if (!string.IsNullOrEmpty(Field.Media.URL)) { BitmapImage.UriSource = new Uri(Field.Media.URL); } } finally { BitmapImage.EndInit(); } Image Image = new Image() { Source = BitmapImage, ToolTip = Field.Description, Margin = new Thickness(0, TopMarginLaidOut ? 0 : 5, 0, 5) }; if (Field.Media.Width.HasValue) { Image.Width = Field.Media.Width.Value; } if (Field.Media.Height.HasValue) { Image.Height = Field.Media.Height.Value; } Container.Children.Add(Image); } else if (IsVideo || IsAudio) { MediaElement = new MediaElement() { Source = Uri, LoadedBehavior = MediaState.Manual, ToolTip = Field.Description }; Container.Children.Add(MediaElement); if (IsVideo) { MediaElement.Margin = new Thickness(0, TopMarginLaidOut ? 0 : 5, 0, 5); if (Field.Media.Width.HasValue) { MediaElement.Width = Field.Media.Width.Value; } if (Field.Media.Height.HasValue) { MediaElement.Height = Field.Media.Height.Value; } } DockPanel ControlPanel = new DockPanel() { Width = 290 }; Container.Children.Add(ControlPanel); Button Button = new Button() { Width = 50, Height = 23, Margin = new Thickness(0, 0, 5, 0), Content = "<<", Tag = MediaElement }; ControlPanel.Children.Add(Button); Button.Click += new RoutedEventHandler(Rewind_Click); Button = new Button() { Width = 50, Height = 23, Margin = new Thickness(5, 0, 5, 0), Content = "Play", Tag = MediaElement }; ControlPanel.Children.Add(Button); Button.Click += new RoutedEventHandler(Play_Click); Button = new Button() { Width = 50, Height = 23, Margin = new Thickness(5, 0, 5, 0), Content = "Pause", Tag = MediaElement }; ControlPanel.Children.Add(Button); Button.Click += new RoutedEventHandler(Pause_Click); Button = new Button() { Width = 50, Height = 23, Margin = new Thickness(5, 0, 5, 0), Content = "Stop", Tag = MediaElement }; ControlPanel.Children.Add(Button); Button.Click += new RoutedEventHandler(Stop_Click); Button = new Button() { Width = 50, Height = 23, Margin = new Thickness(5, 0, 0, 0), Content = ">>", Tag = MediaElement }; ControlPanel.Children.Add(Button); Button.Click += new RoutedEventHandler(Forward_Click); MediaElement.Play(); } }
/// <summary> /// Generates an absolute url for the given media field. /// </summary> /// <param name="app">The application service</param> /// <param name="field">The field</param> /// <returns>The url</returns> public static string AbsoluteUrl(this IApplicationService app, MediaField field) { return($"{ AbsoluteUrlStart(app) }{ Url(app, field) }"); }
/// <summary> /// Implements support for data forms. Data Forms are defined in the following XEPs: /// /// XEP-0004: Data Forms: /// http://xmpp.org/extensions/xep-0004.html /// /// XEP-0122: Data Forms Validation: /// http://xmpp.org/extensions/xep-0122.html /// /// XEP-0141: Data Forms Layout /// http://xmpp.org/extensions/xep-0141.html /// /// XEP-0221: Data Forms Media Element /// http://xmpp.org/extensions/xep-0221.html /// /// XEP-0331: Data Forms - Color Field Types /// http://xmpp.org/extensions/xep-0331.html /// /// XEP-0336: Data Forms - Dynamic Forms /// http://xmpp.org/extensions/xep-0336.html /// /// XEP-0348: Signing Forms: /// http://xmpp.org/extensions/xep-0348.html /// </summary> /// <param name="Client">XMPP Client.</param> /// <param name="X">Data Form definition.</param> /// <param name="OnSubmit">Method called when the form is submitted.</param> /// <param name="OnCancel">Method called when the form is cancelled.</param> /// <param name="From">From where the form came.</param> /// <param name="To">To where the form was sent.</param> public DataForm(XmppClient Client, XmlElement X, DataFormEventHandler OnSubmit, DataFormEventHandler OnCancel, string From, string To) { List <string> Instructions = new List <string>(); List <Field> Fields = new List <Field>(); List <Field[]> Records = new List <Field[]>(); List <Page> Pages = null; this.client = Client; this.onSubmit = OnSubmit; this.onCancel = OnCancel; this.from = From; this.to = To; switch (XML.Attribute(X, "type").ToLower()) { case "cancel": this.type = FormType.Cancel; break; case "form": this.type = FormType.Form; break; case "result": this.type = FormType.Result; break; case "submit": this.type = FormType.Submit; break; default: this.type = FormType.Undefined; break; } foreach (XmlNode N in X.ChildNodes) { switch (N.LocalName) { case "instructions": Instructions.Add(N.InnerText.Trim()); break; case "title": this.title = N.InnerText.Trim(); break; case "field": Field Field = this.ParseField((XmlElement)N, out Media Media); Fields.Add(Field); if (Field.PostBack) { this.containsPostBackFields = true; } if (!string.IsNullOrEmpty(Field.Var)) { this.fieldsByVar[Field.Var] = Field; } if (!(Media is null)) { Field = new MediaField(this, Guid.NewGuid().ToString(), string.Empty, false, null, null, string.Empty, new StringDataType(), new BasicValidation(), Media, string.Empty, false, true, false); Fields.Add(Field); this.fieldsByVar[Field.Var] = Field; this.hasMedia = true; } break; case "reported": List <Field> Header = new List <Field>(); foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "field") { Field = this.ParseField((XmlElement)N2, out Media); Header.Add(Field); } } this.header = Header.ToArray(); break; case "item": List <Field> Record = new List <Field>(); foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "field") { Field = this.ParseField((XmlElement)N2, out Media); Record.Add(Field); } } Records.Add(Record.ToArray()); break; case "page": if (Pages is null) { Pages = new List <Page>(); } Pages.Add(new Page(this, (XmlElement)N)); break; } } this.instructions = Instructions.ToArray(); this.fields = Fields.ToArray(); this.records = Records.ToArray(); if (this.header is null) { this.header = new Field[0]; } if (this.hasPages = (!(Pages is null))) { this.pages = Pages.ToArray(); }
private Field ParseField(XmlElement E) { string Label = XML.Attribute(E, "label"); string Type = XML.Attribute(E, "type"); string Var = XML.Attribute(E, "var"); List <string> ValueStrings = null; List <KeyValuePair <string, string> > OptionStrings = null; string Description = string.Empty; string DataTypeName = null; DataType DataType = null; ValidationMethod ValidationMethod = null; Media Media = null; Field Field; string Error = null; bool Required = false; bool PostBack = false; bool ReadOnly = false; bool NotSame = false; foreach (XmlNode N2 in E.ChildNodes) { switch (N2.LocalName) { case "desc": Description = N2.InnerText; break; case "required": Required = true; break; case "value": if (ValueStrings == null) { ValueStrings = new List <string>(); } ValueStrings.Add(N2.InnerText); break; case "option": if (OptionStrings == null) { OptionStrings = new List <KeyValuePair <string, string> >(); } string OptionLabel = XML.Attribute((XmlElement)N2, "label"); string OptionValue = string.Empty; foreach (XmlNode N3 in N2.ChildNodes) { if (N3.LocalName == "value") { OptionValue = N3.InnerText; break; } } OptionStrings.Add(new KeyValuePair <string, string>(OptionLabel, OptionValue)); break; case "validate": DataTypeName = XML.Attribute((XmlElement)N2, "datatype"); foreach (XmlNode N3 in N2.ChildNodes) { switch (N3.LocalName) { case "basic": ValidationMethod = new BasicValidation(); break; case "open": ValidationMethod = new OpenValidation(); break; case "range": XmlElement E3 = (XmlElement)N3; ValidationMethod = new RangeValidation( XML.Attribute(E3, "min"), XML.Attribute(E3, "max")); break; case "regex": ValidationMethod = new RegexValidation(N3.InnerText); break; case "list-range": E3 = (XmlElement)N3; ValidationMethod = new ListRangeValidation(ValidationMethod, XML.Attribute(E3, "min", 0), XML.Attribute(E3, "max", int.MaxValue)); break; } } break; case "media": Media = new Media((XmlElement)N2); break; case "postBack": PostBack = true; break; case "readOnly": ReadOnly = true; break; case "notSame": NotSame = true; break; case "error": Error = N2.InnerText; break; } } if (string.IsNullOrEmpty(DataTypeName)) { if (Type == "boolean") { DataTypeName = "xs:boolean"; } else { DataTypeName = "xs:string"; } } switch (DataTypeName.ToLower()) { case "xs:boolean": DataType = BooleanDataType.Instance; break; case "xs:string": default: DataType = StringDataType.Instance; break; case "anyuri": DataType = AnyUriDataType.Instance; break; case "xs:byte": DataType = ByteDataType.Instance; break; case "xs:date": DataType = DateDataType.Instance; break; case "xs:datetime": DataType = DateTimeDataType.Instance; break; case "xs:decimal": DataType = DecimalDataType.Instance; break; case "xs:double": DataType = DoubleDataType.Instance; break; case "xs:int": DataType = IntDataType.Instance; break; case "xs:integer": DataType = IntegerDataType.Instance; break; case "xs:language": DataType = LanguageDataType.Instance; break; case "xs:long": DataType = LongDataType.Instance; break; case "xs:short": DataType = ShortDataType.Instance; break; case "xs:time": DataType = TimeDataType.Instance; break; case "xdc:Color": DataType = ColorDataType.Instance; break; case "xdc:ColorAlpha": DataType = ColorAlphaDataType.Instance; break; } if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } switch (Type) { case "boolean": Field = new BooleanField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "fixed": Field = new FixedField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "hidden": Field = new HiddenField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "jid-multi": Field = new JidMultiField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "jid-single": Field = new JidSingleField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "list-multi": Field = new ListMultiField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "list-single": Field = new ListSingleField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "text-multi": Field = new TextMultiField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "text-private": Field = new TextPrivateField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; case "text-single": Field = new TextSingleField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); break; default: if (Media == null) { Field = new TextSingleField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Error, PostBack, ReadOnly, NotSame); } else { Field = new MediaField(this, Var, Label, Required, ValueStrings?.ToArray(), OptionStrings?.ToArray(), Description, DataType, ValidationMethod, Media, Error, PostBack, ReadOnly, NotSame); } break; } return(Field); }
/// <summary> /// Projects each product of a sequence into a new form. /// </summary> public static IQueryable <MediaItem> Select(this IQueryable <PortalMedia> query, MediaField fields) { Expression <Func <PortalMedia, MediaItem> > selector = media => new MediaItem { Id = media.Id, Uri = media.Uri, Type = media.Type, Name = media.Name, ModifiedDate = media.ModifiedDate }; //if (fields.HasFlag(MediaField.Portal)) //{ // selector = selector.Merge(media => new MediaItem // { // Portal = new ResourceItem // { // Id = media.Portal.Id, // Uri = media.Portal.Uri, // Name = media.Portal.Name // } // }); //} return(query.Select(selector)); }