Exemplo n.º 1
0
        public override object GetData(TransferDataType type)
        {
            if (type == TransferDataType.Uri)
            {
                return((Uri)NSUrl.FromPasteboard(NSPasteboard.GeneralPasteboard));
            }

            var data = NSPasteboard.GeneralPasteboard.GetDataForType(type.ToUTI());

            if (data == null)
            {
                return(null);
            }

            if (type == TransferDataType.Text)
            {
                return(data.ToString());
            }
            if (type == TransferDataType.Image)
            {
                return(new NSImage(data));
            }

            unsafe {
                var bytes = new byte [data.Length];
                using (var stream = new UnmanagedMemoryStream((byte *)data.Bytes, bytes.Length))
                    stream.Read(bytes, 0, bytes.Length);
                return(TransferDataSource.DeserializeValue(bytes));
            }
        }
Exemplo n.º 2
0
        public void AddValue(TransferDataType type, byte[] value)
        {
            Type t = Type.GetType(type.Id);

            if (t != null)
            {
                data [type] = TransferDataSource.DeserializeValue(value);
            }
            else
            {
                data [type] = value;
            }
        }
Exemplo n.º 3
0
        T ITransferData.GetValue <T> ()
        {
            object ob = GetValue(TransferDataType.FromType(typeof(T)));

            if (ob == null || ob.GetType() == typeof(Type))
            {
                return((T)ob);
            }
            if (ob is byte[])
            {
                T val = (T)TransferDataSource.DeserializeValue((byte[])ob);
                data[TransferDataType.FromType(typeof(T))] = val;
                return(val);
            }
            return((T)ob);
        }
Exemplo n.º 4
0
        public virtual IVisual VisualOfTransferData(IGraph <IVisual, IVisualEdge> graph, ITransferData data)
        {
            var value = data.GetValue(TransferDataType.FromType(typeof(IVisual)));
            var bytes = value as byte [];

            if (bytes != null)
            {
                return(TransferDataSource.DeserializeValue(bytes) as IVisual);
            }
#if TRACE
            var dt = "";
            data.DataTypes.ForEach(d => dt += d.Id + " | ");
            Trace.WriteLine($"{nameof(DragDropViz)}.{nameof (VisualOfTransferData)}\t{dt}");
#endif
            Stream           stream  = null;
            Content <Stream> content = null;
            string           desc    = null;
            string           source  = null;

            Func <ContentInfo, Stream, Content <Stream> > fillContent = (i, s) => {
                bool newContent = content == null || s != content.Data;

                var content1 = new Content <Stream> (content)
                {
                    Data        = newContent ? s : content.Data,
                    ContentType = newContent ? i.ContentType : content.ContentType,
                    Compression = newContent ? i.Compression : content.Compression,
                };

                content1 = ContentDiggPool.Use(content1);
                desc     = desc ?? content1?.Description?.ToString();
                source   = source ?? content1?.Source?.ToString();
                return(content1);
            };

            if (data.Uris.Length > 0)
            {
                //TODO: handle more then one file
                foreach (var uri in data.Uris.OrderBy(n => n.ToString()))
                {
                    IContentIo <Stream> sink = null;
                    string uridesc           = null;
                    if (uri.IsFile)
                    {
                        var fileName = IoUtils.UriToFileName(uri);
                        if (File.Exists(fileName))    // TODO: check if filename is directory
                        {
                            stream  = File.OpenRead(fileName);
                            sink    = DataManager.SinkOf(Path.GetExtension(fileName).TrimStart('.').ToLower());
                            uridesc = Path.GetFileNameWithoutExtension(fileName);
                        }
                    }
                    else if (uri.HostNameType == UriHostNameType.Dns)
                    {
                        try {
                            using (var cli = new WebClient()) {
                                bytes  = cli.DownloadData(uri);
                                stream = new MemoryStream(bytes);
                            }
                            uridesc = uri.ToString();
                        } catch (Exception webEx) {
                            Registry.Pooled <IMessageBoxShow> ().Show("Download failed",
                                                                      $"The uri \n{uri.ToString ()}\ncould not be loaded: {webEx.Message}", MessageBoxButtons.Ok);
                        }
                    }

                    if (stream != null)
                    {
                        ContentInfo info = null;

                        if (sink == null)
                        {
                            sink = DataManager.SinkOf(stream);
                        }

                        if (sink != null)
                        {
                            info = sink.Use(stream);
                        }

                        if (sink == null)
                        {
                            info = new ContentInfo("Unknown", ContentTypes.Unknown, "*", null, CompressionType.neverCompress);
                        }

                        content = fillContent(info, stream);
                        if (content.Description == null)
                        {
                            content.Description = uridesc;
                        }
                        content.Source = uridesc;

                        if (data.Uris.Length > 1)
                        {
                            Registry.Pooled <IMessageBoxShow> ().Show("DragDrop multiple files",
                                                                      string.Format("Only one file {0} will be stored currently", uri.AbsolutePath), MessageBoxButtons.Ok);
                        }

                        break;
                    }
                }
            }
            else
            {
                var dataTypes = data.DataTypes.ToArray();

                if (data.Text != null)
                {
                    using (var dr = new StringReader(data.Text))
                        desc = dr.ReadLine();
                }

                foreach (var s in DataManager.SinksOf(dataTypes).ToArray())
                {
                    var transferType = s.Item1;
                    value = data.GetValue(transferType);
                    var sink = s.Item2;
                    stream = value as Stream;
                    bytes  = value as byte [];
                    if (bytes != null)
                    {
                        stream = new MemoryStream(bytes);
                    }
                    var text = value as string;
                    if (text != null)
                    {
                        stream = text.AsUnicodeStream();
                    }

                    if (stream != null)
                    {
                        var info = sink.Use(stream);

                        var contentSpec = sink.Detector;
                        if (info == null && contentSpec != null)
                        {
                            info = contentSpec.FindMime(s.Item1.Id);
                        }

                        if (info == null)
                        {
                            info = DataManager.InfoOf(sink, dataTypes).FirstOrDefault();
                        }

                        if (info != null && content == null || content.Data == null)
                        {
                            content = fillContent(info, stream);

                            // TODO: find a better handling of preferences; maybe MimeFingerPrints does the job?
                            if (content.Data == null || desc == null || source == null)
                            {
                                continue;
                            }
                        }
                        else if (info != null)
                        {
                            fillContent(info, stream);
                            if (desc == null || source == null)
                            {
                                continue;
                            }
                        }
                        if (content != null)
                        {
                            if (content.Description == null)
                            {
                                content.Description = desc;
                            }
                            if (content.Source == null)
                            {
                                content.Source = source;
                            }
                        }
                    }
                }
            }

            if (content != null)
            {
#if TRACE
                Trace.WriteLine($"{nameof (VisualOfTransferData)}\tDragged:\t{content.ContentType.MimeType ()}");
#endif
                var result = VisualContentViz.VisualOfContent(graph, content);
                if (stream is FileStream)
                {
                    stream.Close();
                }
                return(result);
            }

            return(null);
        }