OSDN Git Service

Adjust to the introduction of FolderType ENUM in libomv
[radegast/radegast.git] / Radegast / GUI / Consoles / Assets / Notecard.cs
index 205f363..5ec07c9 100644 (file)
-// \r
-// Radegast Metaverse Client\r
-// Copyright (c) 2009, Radegast Development Team\r
-// All rights reserved.\r
-// \r
-// Redistribution and use in source and binary forms, with or without\r
-// modification, are permitted provided that the following conditions are met:\r
-// \r
-//     * Redistributions of source code must retain the above copyright notice,\r
-//       this list of conditions and the following disclaimer.\r
-//     * Redistributions in binary form must reproduce the above copyright\r
-//       notice, this list of conditions and the following disclaimer in the\r
-//       documentation and/or other materials provided with the distribution.\r
-//     * Neither the name of the application "Radegast", nor the names of its\r
-//       contributors may be used to endorse or promote products derived from\r
-//       this software without specific prior written permission.\r
-// \r
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
-// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
-// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
-// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
-// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
-// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
-// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
-//\r
-// $Id$\r
-//\r
-using System;\r
-using System.Collections.Generic;\r
-using System.Windows.Forms;\r
-using OpenMetaverse;\r
-using OpenMetaverse.Assets;\r
-\r
-namespace Radegast\r
-{\r
-    public partial class Notecard : DettachableControl\r
-    {\r
-        private RadegastInstance instance;\r
-        private GridClient client { get { return instance.Client; } }\r
-        private InventoryNotecard notecard;\r
-        private AssetNotecard recievedNotecard;\r
-\r
-        public Notecard(RadegastInstance instance, InventoryNotecard notecard)\r
-        {\r
-            InitializeComponent();\r
-            Disposed += new EventHandler(Notecard_Disposed);\r
-\r
-            this.instance = instance;\r
-            this.notecard = notecard;\r
-            Text = notecard.Name;\r
-\r
-            rtbContent.DetectUrls = false;\r
-\r
-            rtbContent.Text = " ";\r
-            UpdateStatus("Loading...");\r
-            client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);\r
-        }\r
-\r
-        void Notecard_Disposed(object sender, EventArgs e)\r
-        {\r
-        }\r
-\r
-        void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)\r
-        {\r
-            if (InvokeRequired)\r
-            {\r
-                BeginInvoke(new MethodInvoker(delegate()\r
-                    {\r
-                        Assets_OnAssetReceived(transfer, asset);\r
-                    }\r
-                ));\r
-                return;\r
-            }\r
-\r
-            if (transfer.Success)\r
-            {\r
-                AssetNotecard n = (AssetNotecard)asset;\r
-                n.Decode();\r
-                recievedNotecard = n;\r
-\r
-                string noteText = string.Empty;\r
-                rtbContent.Clear();\r
-\r
-                for (int i = 0; i < n.BodyText.Length; i++)\r
-                {\r
-                    char c = n.BodyText[i];\r
-\r
-                    if ((int)c == 0xdbc0)\r
-                    {\r
-                        int index = (int)n.BodyText[++i] - 0xdc00;\r
-                        InventoryItem e = n.EmbeddedItems[index];\r
-                        rtbContent.AppendText(noteText);\r
-                        rtbContent.InsertLink(e.Name, string.Format("radegast://embeddedasset/{0}", index));\r
-                        noteText = string.Empty;\r
-                    }\r
-                    else\r
-                    {\r
-                        noteText += c;\r
-                    }\r
-                }\r
-\r
-                rtbContent.Text += noteText;\r
-\r
-                if (n.EmbeddedItems != null && n.EmbeddedItems.Count > 0)\r
-                {\r
-                    tbtnAttachments.Enabled = true;\r
-                    tbtnAttachments.Visible = true;\r
-                    foreach (InventoryItem item in n.EmbeddedItems)\r
-                    {\r
-                        int ix = InventoryConsole.GetItemImageIndex(item.AssetType.ToString().ToLower());\r
-                        ToolStripMenuItem titem = new ToolStripMenuItem(item.Name);\r
-\r
-                        if (ix != -1)\r
-                        {\r
-                            titem.Image = frmMain.ResourceImages.Images[ix];\r
-                            titem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;\r
-                        }\r
-                        else\r
-                        {\r
-                            titem.DisplayStyle = ToolStripItemDisplayStyle.Text;\r
-                        }\r
-\r
-                        titem.Name = item.UUID.ToString(); ;\r
-                        titem.Tag = item;\r
-                        titem.Click += new EventHandler(attachmentMenuItem_Click);\r
-                        tbtnAttachments.DropDownItems.Add(titem);\r
-                    }\r
-                }\r
-                UpdateStatus("OK");\r
-            }\r
-            else\r
-            {\r
-                UpdateStatus("Failed");\r
-                rtbContent.Text = "Failed to download notecard.";\r
-            }\r
-        }\r
-\r
-        void attachmentMenuItem_Click(object sender, EventArgs e)\r
-        {\r
-            if (sender is ToolStripMenuItem)\r
-            {\r
-                ToolStripMenuItem titem = (ToolStripMenuItem)sender;\r
-                InventoryItem item = (InventoryItem)titem.Tag;\r
-\r
-                switch (item.AssetType)\r
-                {\r
-                    case AssetType.Texture:\r
-                        SLImageHandler ih = new SLImageHandler(instance, item.AssetUUID, string.Empty);\r
-                        ih.Text = item.Name;\r
-                        ih.ShowDetached();\r
-                        break;\r
-\r
-                    case AssetType.Landmark:\r
-                        Landmark ln = new Landmark(instance, (InventoryLandmark)item);\r
-                        ln.ShowDetached();\r
-                        break;\r
-                }\r
-            }\r
-        }\r
-\r
-        private void btnRefresh_Click(object sender, EventArgs e)\r
-        {\r
-            rtbContent.Text = "Loading...";\r
-            client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);\r
-        }\r
-\r
-        private void rtbContent_LinkClicked(object sender, LinkClickedEventArgs e)\r
-        {\r
-            //instance.MainForm.processLink(e.LinkText);\r
-        }\r
-\r
-\r
-        #region Detach/Attach\r
-        protected override void ControlIsNotRetachable()\r
-        {\r
-            tbtnAttach.Visible = false;\r
-        }\r
-\r
-        protected override void Detach()\r
-        {\r
-            base.Detach();\r
-            tbtnAttach.Text = "Attach";\r
-            tbtnExit.Enabled = true;\r
-        }\r
-\r
-        protected override void Retach()\r
-        {\r
-            base.Retach();\r
-            tbtnAttach.Text = "Detach";\r
-            tbtnExit.Enabled = false;\r
-        }\r
-\r
-        private void tbtnAttach_Click(object sender, EventArgs e)\r
-        {\r
-            if (Detached)\r
-            {\r
-                Retach();\r
-            }\r
-            else\r
-            {\r
-                Detach();\r
-            }\r
-        }\r
-        #endregion\r
-\r
-        private void tbtnExit_Click(object sender, EventArgs e)\r
-        {\r
-            if (Detached)\r
-            {\r
-                FindForm().Close();\r
-            }\r
-        }\r
-\r
-        private void tbtnSave_Click(object sender, EventArgs e)\r
-        {\r
-            bool success = false;\r
-            string message = "";\r
-            AssetNotecard n = new AssetNotecard();\r
-            n.BodyText = rtbContent.Text;\r
-            n.EmbeddedItems = new List<InventoryItem>();\r
-\r
-            for (int i = 0; i < recievedNotecard.EmbeddedItems.Count; i++)\r
-            {\r
-                n.EmbeddedItems.Add(recievedNotecard.EmbeddedItems[i]);\r
-                int indexChar = 0xdc00 + i;\r
-                n.BodyText += (char)0xdbc0;\r
-                n.BodyText += (char)indexChar;\r
-            }\r
-\r
-            n.Encode();\r
-\r
-            UpdateStatus("Saving...");\r
-\r
-\r
-            client.Inventory.RequestUploadNotecardAsset(n.AssetData, notecard.UUID,\r
-                delegate(bool uploadSuccess, string status, UUID itemID, UUID assetID)\r
-                {\r
-                    success = uploadSuccess;\r
-                    if (itemID == notecard.UUID)\r
-                    {\r
-                        if (success)\r
-                        {\r
-                            UpdateStatus("OK");\r
-                            notecard.AssetUUID = assetID;\r
-                        }\r
-                        else\r
-                        {\r
-                            UpdateStatus("Failed");\r
-                        }\r
-\r
-                    }\r
-                    message = status ?? "Unknown error uploading notecard asset";\r
-                }\r
-            );\r
-        }\r
-\r
-        void UpdateStatus(string status)\r
-        {\r
-            if (InvokeRequired)\r
-            {\r
-                BeginInvoke(new MethodInvoker(delegate() { UpdateStatus(status); }));\r
-                return;\r
-            }\r
-\r
-            tlblStatus.Text = status;\r
-        }\r
-\r
-    }\r
-}\r
+// 
+// Radegast Metaverse Client
+// Copyright (c) 2009-2014, Radegast Development Team
+// All rights reserved.
+// 
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+// 
+//     * Redistributions of source code must retain the above copyright notice,
+//       this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above copyright
+//       notice, this list of conditions and the following disclaimer in the
+//       documentation and/or other materials provided with the distribution.
+//     * Neither the name of the application "Radegast", nor the names of its
+//       contributors may be used to endorse or promote products derived from
+//       this software without specific prior written permission.
+// 
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// $Id$
+//
+using System;
+using System.Collections.Generic;
+using System.Windows.Forms;
+using OpenMetaverse;
+using OpenMetaverse.Assets;
+
+namespace Radegast
+{
+    public partial class Notecard : DettachableControl
+    {
+        private RadegastInstance instance;
+        private GridClient client { get { return instance.Client; } }
+        private InventoryNotecard notecard;
+        private AssetNotecard receivedNotecard;
+        private Primitive prim;
+
+        public Notecard(RadegastInstance instance, InventoryNotecard notecard)
+            : this(instance, notecard, null)
+        {
+        }
+
+        public Notecard(RadegastInstance instance, InventoryNotecard notecard, Primitive prim)
+        {
+            InitializeComponent();
+            Disposed += new EventHandler(Notecard_Disposed);
+
+            this.instance = instance;
+            this.notecard = notecard;
+            this.prim = prim;
+
+            Text = notecard.Name;
+
+            rtbContent.DetectUrls = false;
+
+
+            if (notecard.AssetUUID == UUID.Zero)
+            {
+                UpdateStatus("Blank");
+            }
+            else
+            {
+                rtbContent.Text = " ";
+                UpdateStatus("Loading...");
+
+                if (prim == null)
+                {
+                    client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
+                }
+                else
+                {
+                    client.Assets.RequestInventoryAsset(notecard.AssetUUID, notecard.UUID, prim.ID, prim.OwnerID, notecard.AssetType, true, Assets_OnAssetReceived);
+                }
+            }
+        }
+
+        void Notecard_Disposed(object sender, EventArgs e)
+        {
+        }
+
+        void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)
+        {
+            if (InvokeRequired)
+            {
+                if (!instance.MonoRuntime || IsHandleCreated)
+                    BeginInvoke(new MethodInvoker(() => Assets_OnAssetReceived(transfer, asset)));
+                return;
+            }
+
+            if (transfer.Success)
+            {
+                AssetNotecard n = (AssetNotecard)asset;
+                n.Decode();
+                receivedNotecard = n;
+
+                string noteText = string.Empty;
+                rtbContent.Clear();
+
+                for (int i = 0; i < n.BodyText.Length; i++)
+                {
+                    char c = n.BodyText[i];
+
+                    if ((int)c == 0xdbc0)
+                    {
+                        int index = (int)n.BodyText[++i] - 0xdc00;
+                        InventoryItem e = n.EmbeddedItems[index];
+                        rtbContent.AppendText(noteText);
+                        rtbContent.InsertLink(e.Name, string.Format("radegast://embeddedasset/{0}", index));
+                        noteText = string.Empty;
+                    }
+                    else
+                    {
+                        noteText += c;
+                    }
+                }
+
+                rtbContent.Text += noteText;
+
+                if (n.EmbeddedItems != null && n.EmbeddedItems.Count > 0)
+                {
+                    tbtnAttachments.Enabled = true;
+                    tbtnAttachments.Visible = true;
+                    foreach (InventoryItem item in n.EmbeddedItems)
+                    {
+                        int ix = InventoryConsole.GetItemImageIndex(item.AssetType.ToString().ToLower());
+                        ToolStripMenuItem titem = new ToolStripMenuItem(item.Name);
+
+                        if (ix != -1)
+                        {
+                            titem.Image = frmMain.ResourceImages.Images[ix];
+                            titem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
+                        }
+                        else
+                        {
+                            titem.DisplayStyle = ToolStripItemDisplayStyle.Text;
+                        }
+
+                        titem.Name = item.UUID.ToString(); ;
+                        titem.Tag = item;
+                        titem.Click += new EventHandler(attachmentMenuItem_Click);
+
+                        var saveToInv = new ToolStripMenuItem("Save to inventory");
+                        saveToInv.Click += (object xsender, EventArgs xe) =>
+                            {
+                                client.Inventory.RequestCopyItemFromNotecard(UUID.Zero,
+                                    notecard.UUID,
+                                    client.Inventory.FindFolderForType(item.AssetType),
+                                    item.UUID,
+                                    Inventory_OnInventoryItemCopied);
+                            };
+
+                        titem.DropDownItems.Add(saveToInv);
+                        tbtnAttachments.DropDownItems.Add(titem);
+                    }
+                }
+                UpdateStatus("OK");
+                rtbContent.Focus();
+            }
+            else
+            {
+                UpdateStatus("Failed");
+                rtbContent.Text = "Failed to download notecard. " + transfer.Status;
+            }
+        }
+
+        private void Inventory_OnInventoryItemCopied(InventoryBase item)
+        {
+            if (InvokeRequired)
+            {
+                if (!instance.MonoRuntime || IsHandleCreated)
+                    BeginInvoke(new MethodInvoker(() => Inventory_OnInventoryItemCopied(item)));
+                return;
+            }
+
+            if (null == item) return;
+
+            instance.TabConsole.DisplayNotificationInChat(
+                string.Format("{0} saved to inventory", item.Name),
+                ChatBufferTextStyle.Invisible);
+
+            tlblStatus.Text = "Saved";
+            
+            if (item is InventoryNotecard)
+            {
+                Notecard nc = new Notecard(instance, (InventoryNotecard)item);
+                nc.pnlKeepDiscard.Visible = true;
+                nc.ShowDetached();
+            }
+        }
+
+        void attachmentMenuItem_Click(object sender, EventArgs e)
+        {
+            if (sender is ToolStripMenuItem)
+            {
+                ToolStripMenuItem titem = (ToolStripMenuItem)sender;
+                InventoryItem item = (InventoryItem)titem.Tag;
+
+                switch (item.AssetType)
+                {
+                    case AssetType.Texture:
+                        SLImageHandler ih = new SLImageHandler(instance, item.AssetUUID, string.Empty);
+                        ih.Text = item.Name;
+                        ih.ShowDetached();
+                        break;
+
+                    case AssetType.Landmark:
+                        Landmark ln = new Landmark(instance, (InventoryLandmark)item);
+                        ln.ShowDetached();
+                        break;
+
+                    case AssetType.Notecard:
+                        client.Inventory.RequestCopyItemFromNotecard(UUID.Zero,
+                            notecard.UUID,
+                            notecard.ParentUUID,
+                            item.UUID,
+                            Inventory_OnInventoryItemCopied);
+                        break;
+                }
+            }
+        }
+
+        private void btnRefresh_Click(object sender, EventArgs e)
+        {
+            if (notecard.AssetUUID == UUID.Zero) return;
+
+            rtbContent.Text = "Loading...";
+            client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
+        }
+
+        private void rtbContent_LinkClicked(object sender, LinkClickedEventArgs e)
+        {
+            //instance.MainForm.processLink(e.LinkText);
+        }
+
+
+        #region Detach/Attach
+        protected override void ControlIsNotRetachable()
+        {
+            tbtnAttach.Visible = false;
+        }
+
+        protected override void Detach()
+        {
+            base.Detach();
+            tbtnAttach.Text = "Attach";
+            tbtnExit.Enabled = true;
+        }
+
+        protected override void Retach()
+        {
+            base.Retach();
+            tbtnAttach.Text = "Detach";
+            tbtnExit.Enabled = false;
+        }
+
+        private void tbtnAttach_Click(object sender, EventArgs e)
+        {
+            if (Detached)
+            {
+                Retach();
+            }
+            else
+            {
+                Detach();
+            }
+        }
+        #endregion
+
+        private void tbtnExit_Click(object sender, EventArgs e)
+        {
+            if (Detached)
+            {
+                FindForm().Close();
+            }
+        }
+
+        private void tbtnSave_Click(object sender, EventArgs e)
+        {
+            bool success = false;
+            string message = "";
+            AssetNotecard n = new AssetNotecard();
+            n.BodyText = rtbContent.Text;
+            n.EmbeddedItems = new List<InventoryItem>();
+
+            if (receivedNotecard != null)
+            {
+                for (int i = 0; i < receivedNotecard.EmbeddedItems.Count; i++)
+                {
+                    n.EmbeddedItems.Add(receivedNotecard.EmbeddedItems[i]);
+                    int indexChar = 0xdc00 + i;
+                    n.BodyText += (char)0xdbc0;
+                    n.BodyText += (char)indexChar;
+                }
+            }
+
+            n.Encode();
+
+            UpdateStatus("Saving...");
+
+            InventoryManager.InventoryUploadedAssetCallback handler = delegate(bool uploadSuccess, string status, UUID itemID, UUID assetID)
+                    {
+                        success = uploadSuccess;
+                        if (itemID == notecard.UUID)
+                        {
+                            if (success)
+                            {
+                                UpdateStatus("OK");
+                                notecard.AssetUUID = assetID;
+                            }
+                            else
+                            {
+                                UpdateStatus("Failed");
+                            }
+
+                        }
+                        message = status ?? "Unknown error uploading notecard asset";
+                    };
+
+            if (prim == null)
+            {
+                client.Inventory.RequestUploadNotecardAsset(n.AssetData, notecard.UUID, handler);
+            }
+            else
+            {
+                client.Inventory.RequestUpdateNotecardTask(n.AssetData, notecard.UUID, prim.ID, handler);
+            }
+        }
+
+        void UpdateStatus(string status)
+        {
+            if (InvokeRequired)
+            {
+                if (!instance.MonoRuntime || IsHandleCreated)
+                    BeginInvoke(new MethodInvoker(() => UpdateStatus(status)));
+                return;
+            }
+            instance.TabConsole.DisplayNotificationInChat("Notecard status: " + status, ChatBufferTextStyle.Invisible);
+            tlblStatus.Text = status;
+        }
+
+        private void rtbContent_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.KeyCode == Keys.S && e.Control)
+            {
+                if (e.Shift)
+                {
+                }
+                else
+                {
+                    tbtnSave_Click(this, EventArgs.Empty);
+                    e.Handled = e.SuppressKeyPress = true;
+                }
+            }
+
+        }
+
+        private void rtbContent_Enter(object sender, EventArgs e)
+        {
+            instance.TabConsole.DisplayNotificationInChat("Editing notecard", ChatBufferTextStyle.Invisible);
+        }
+
+        private void btnKeep_Click(object sender, EventArgs e)
+        {
+            Retach();
+        }
+
+        private void btnDiscard_Click(object sender, EventArgs e)
+        {
+            client.Inventory.MoveItem(notecard.UUID, client.Inventory.FindFolderForType(FolderType.Trash), notecard.Name);
+            Retach();
+        }
+    }
+}