2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 using System.Collections.Generic;
33 using System.Windows.Forms;
35 using OpenMetaverse.Assets;
39 public partial class Notecard : DettachableControl
41 private RadegastInstance instance;
42 private GridClient client { get { return instance.Client; } }
43 private InventoryNotecard notecard;
44 private AssetNotecard receivedNotecard;
45 private Primitive prim;
47 public Notecard(RadegastInstance instance, InventoryNotecard notecard)
48 : this(instance, notecard, null)
52 public Notecard(RadegastInstance instance, InventoryNotecard notecard, Primitive prim)
54 InitializeComponent();
55 Disposed += new EventHandler(Notecard_Disposed);
57 this.instance = instance;
58 this.notecard = notecard;
63 rtbContent.DetectUrls = false;
66 if (notecard.AssetUUID == UUID.Zero)
68 UpdateStatus("Blank");
72 rtbContent.Text = " ";
73 UpdateStatus("Loading...");
77 client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
81 client.Assets.RequestInventoryAsset(notecard.AssetUUID, notecard.UUID, prim.ID, prim.OwnerID, notecard.AssetType, true, Assets_OnAssetReceived);
86 void Notecard_Disposed(object sender, EventArgs e)
90 void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)
94 if (!instance.MonoRuntime || IsHandleCreated)
95 BeginInvoke(new MethodInvoker(() => Assets_OnAssetReceived(transfer, asset)));
101 AssetNotecard n = (AssetNotecard)asset;
103 receivedNotecard = n;
105 string noteText = string.Empty;
108 for (int i = 0; i < n.BodyText.Length; i++)
110 char c = n.BodyText[i];
112 if ((int)c == 0xdbc0)
114 int index = (int)n.BodyText[++i] - 0xdc00;
115 InventoryItem e = n.EmbeddedItems[index];
116 rtbContent.AppendText(noteText);
117 rtbContent.InsertLink(e.Name, string.Format("radegast://embeddedasset/{0}", index));
118 noteText = string.Empty;
126 rtbContent.Text += noteText;
128 if (n.EmbeddedItems != null && n.EmbeddedItems.Count > 0)
130 tbtnAttachments.Enabled = true;
131 tbtnAttachments.Visible = true;
132 foreach (InventoryItem item in n.EmbeddedItems)
134 int ix = InventoryConsole.GetItemImageIndex(item.AssetType.ToString().ToLower());
135 ToolStripMenuItem titem = new ToolStripMenuItem(item.Name);
139 titem.Image = frmMain.ResourceImages.Images[ix];
140 titem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
144 titem.DisplayStyle = ToolStripItemDisplayStyle.Text;
147 titem.Name = item.UUID.ToString(); ;
149 titem.Click += new EventHandler(attachmentMenuItem_Click);
151 var saveToInv = new ToolStripMenuItem("Save to inventory");
152 saveToInv.Click += (object xsender, EventArgs xe) =>
154 client.Inventory.RequestCopyItemFromNotecard(UUID.Zero,
156 client.Inventory.FindFolderForType(item.AssetType),
158 Inventory_OnInventoryItemCopied);
161 titem.DropDownItems.Add(saveToInv);
162 tbtnAttachments.DropDownItems.Add(titem);
170 UpdateStatus("Failed");
171 rtbContent.Text = "Failed to download notecard. " + transfer.Status;
175 private void Inventory_OnInventoryItemCopied(InventoryBase item)
179 if (!instance.MonoRuntime || IsHandleCreated)
180 BeginInvoke(new MethodInvoker(() => Inventory_OnInventoryItemCopied(item)));
184 if (null == item) return;
186 instance.TabConsole.DisplayNotificationInChat(
187 string.Format("{0} saved to inventory", item.Name),
188 ChatBufferTextStyle.Invisible);
190 tlblStatus.Text = "Saved";
192 if (item is InventoryNotecard)
194 Notecard nc = new Notecard(instance, (InventoryNotecard)item);
195 nc.pnlKeepDiscard.Visible = true;
200 void attachmentMenuItem_Click(object sender, EventArgs e)
202 if (sender is ToolStripMenuItem)
204 ToolStripMenuItem titem = (ToolStripMenuItem)sender;
205 InventoryItem item = (InventoryItem)titem.Tag;
207 switch (item.AssetType)
209 case AssetType.Texture:
210 SLImageHandler ih = new SLImageHandler(instance, item.AssetUUID, string.Empty);
215 case AssetType.Landmark:
216 Landmark ln = new Landmark(instance, (InventoryLandmark)item);
220 case AssetType.Notecard:
221 client.Inventory.RequestCopyItemFromNotecard(UUID.Zero,
225 Inventory_OnInventoryItemCopied);
231 private void btnRefresh_Click(object sender, EventArgs e)
233 if (notecard.AssetUUID == UUID.Zero) return;
235 rtbContent.Text = "Loading...";
236 client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
239 private void rtbContent_LinkClicked(object sender, LinkClickedEventArgs e)
241 //instance.MainForm.processLink(e.LinkText);
245 #region Detach/Attach
246 protected override void ControlIsNotRetachable()
248 tbtnAttach.Visible = false;
251 protected override void Detach()
254 tbtnAttach.Text = "Attach";
255 tbtnExit.Enabled = true;
258 protected override void Retach()
261 tbtnAttach.Text = "Detach";
262 tbtnExit.Enabled = false;
265 private void tbtnAttach_Click(object sender, EventArgs e)
278 private void tbtnExit_Click(object sender, EventArgs e)
286 private void tbtnSave_Click(object sender, EventArgs e)
288 bool success = false;
290 AssetNotecard n = new AssetNotecard();
291 n.BodyText = rtbContent.Text;
292 n.EmbeddedItems = new List<InventoryItem>();
294 if (receivedNotecard != null)
296 for (int i = 0; i < receivedNotecard.EmbeddedItems.Count; i++)
298 n.EmbeddedItems.Add(receivedNotecard.EmbeddedItems[i]);
299 int indexChar = 0xdc00 + i;
300 n.BodyText += (char)0xdbc0;
301 n.BodyText += (char)indexChar;
307 UpdateStatus("Saving...");
309 InventoryManager.InventoryUploadedAssetCallback handler = delegate(bool uploadSuccess, string status, UUID itemID, UUID assetID)
311 success = uploadSuccess;
312 if (itemID == notecard.UUID)
317 notecard.AssetUUID = assetID;
321 UpdateStatus("Failed");
325 message = status ?? "Unknown error uploading notecard asset";
330 client.Inventory.RequestUploadNotecardAsset(n.AssetData, notecard.UUID, handler);
334 client.Inventory.RequestUpdateNotecardTask(n.AssetData, notecard.UUID, prim.ID, handler);
338 void UpdateStatus(string status)
342 if (!instance.MonoRuntime || IsHandleCreated)
343 BeginInvoke(new MethodInvoker(() => UpdateStatus(status)));
346 instance.TabConsole.DisplayNotificationInChat("Notecard status: " + status, ChatBufferTextStyle.Invisible);
347 tlblStatus.Text = status;
350 private void rtbContent_KeyDown(object sender, KeyEventArgs e)
352 if (e.KeyCode == Keys.S && e.Control)
359 tbtnSave_Click(this, EventArgs.Empty);
360 e.Handled = e.SuppressKeyPress = true;
366 private void rtbContent_Enter(object sender, EventArgs e)
368 instance.TabConsole.DisplayNotificationInChat("Editing notecard", ChatBufferTextStyle.Invisible);
371 private void btnKeep_Click(object sender, EventArgs e)
376 private void btnDiscard_Click(object sender, EventArgs e)
378 client.Inventory.MoveItem(notecard.UUID, client.Inventory.FindFolderForType(AssetType.TrashFolder), notecard.Name);