OSDN Git Service

Updated year in copyright header.
[radegast/radegast.git] / Radegast / GUI / Consoles / Assets / Notecard.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2010, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
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.
17 // 
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.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Windows.Forms;
34 using OpenMetaverse;
35 using OpenMetaverse.Assets;
36
37 namespace Radegast
38 {
39     public partial class Notecard : DettachableControl
40     {
41         private RadegastInstance instance;
42         private GridClient client { get { return instance.Client; } }
43         private InventoryNotecard notecard;
44         private AssetNotecard receivedNotecard;
45         private Primitive prim;
46
47         public Notecard(RadegastInstance instance, InventoryNotecard notecard)
48             : this(instance, notecard, null)
49         {
50         }
51
52         public Notecard(RadegastInstance instance, InventoryNotecard notecard, Primitive prim)
53         {
54             InitializeComponent();
55             Disposed += new EventHandler(Notecard_Disposed);
56
57             this.instance = instance;
58             this.notecard = notecard;
59             this.prim = prim;
60
61             Text = notecard.Name;
62
63             rtbContent.DetectUrls = false;
64
65
66             if (notecard.AssetUUID == UUID.Zero)
67             {
68                 UpdateStatus("Blank");
69             }
70             else
71             {
72                 rtbContent.Text = " ";
73                 UpdateStatus("Loading...");
74
75                 if (prim == null)
76                 {
77                     client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
78                 }
79                 else
80                 {
81                     client.Assets.RequestInventoryAsset(notecard.AssetUUID, notecard.UUID, prim.ID, prim.OwnerID, notecard.AssetType, true, Assets_OnAssetReceived);
82                 }
83             }
84         }
85
86         void Notecard_Disposed(object sender, EventArgs e)
87         {
88         }
89
90         void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)
91         {
92             if (InvokeRequired)
93             {
94                 if (IsHandleCreated)
95                     BeginInvoke(new MethodInvoker(() => Assets_OnAssetReceived(transfer, asset)));
96                 return;
97             }
98
99             if (transfer.Success)
100             {
101                 AssetNotecard n = (AssetNotecard)asset;
102                 n.Decode();
103                 receivedNotecard = n;
104
105                 string noteText = string.Empty;
106                 rtbContent.Clear();
107
108                 for (int i = 0; i < n.BodyText.Length; i++)
109                 {
110                     char c = n.BodyText[i];
111
112                     if ((int)c == 0xdbc0)
113                     {
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;
119                     }
120                     else
121                     {
122                         noteText += c;
123                     }
124                 }
125
126                 rtbContent.Text += noteText;
127
128                 if (n.EmbeddedItems != null && n.EmbeddedItems.Count > 0)
129                 {
130                     tbtnAttachments.Enabled = true;
131                     tbtnAttachments.Visible = true;
132                     foreach (InventoryItem item in n.EmbeddedItems)
133                     {
134                         int ix = InventoryConsole.GetItemImageIndex(item.AssetType.ToString().ToLower());
135                         ToolStripMenuItem titem = new ToolStripMenuItem(item.Name);
136
137                         if (ix != -1)
138                         {
139                             titem.Image = frmMain.ResourceImages.Images[ix];
140                             titem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
141                         }
142                         else
143                         {
144                             titem.DisplayStyle = ToolStripItemDisplayStyle.Text;
145                         }
146
147                         titem.Name = item.UUID.ToString(); ;
148                         titem.Tag = item;
149                         titem.Click += new EventHandler(attachmentMenuItem_Click);
150                         tbtnAttachments.DropDownItems.Add(titem);
151                     }
152                 }
153                 UpdateStatus("OK");
154                 rtbContent.Focus();
155             }
156             else
157             {
158                 UpdateStatus("Failed");
159                 rtbContent.Text = "Failed to download notecard. " + transfer.Status;
160             }
161         }
162
163         private void Inventory_OnInventoryItemCopied(InventoryBase item)
164         {
165             if (InvokeRequired)
166             {
167                 if (IsHandleCreated)
168                     BeginInvoke(new MethodInvoker(() => Inventory_OnInventoryItemCopied(item)));
169                 return;
170             }
171
172             if (null == item) return;
173
174             if (item is InventoryNotecard)
175             {
176                 Notecard nc = new Notecard(instance, (InventoryNotecard)item);
177                 nc.pnlKeepDiscard.Visible = true;
178                 nc.ShowDetached();
179             }
180         }
181
182         void attachmentMenuItem_Click(object sender, EventArgs e)
183         {
184             if (sender is ToolStripMenuItem)
185             {
186                 ToolStripMenuItem titem = (ToolStripMenuItem)sender;
187                 InventoryItem item = (InventoryItem)titem.Tag;
188
189                 switch (item.AssetType)
190                 {
191                     case AssetType.Texture:
192                         SLImageHandler ih = new SLImageHandler(instance, item.AssetUUID, string.Empty);
193                         ih.Text = item.Name;
194                         ih.ShowDetached();
195                         break;
196
197                     case AssetType.Landmark:
198                         Landmark ln = new Landmark(instance, (InventoryLandmark)item);
199                         ln.ShowDetached();
200                         break;
201
202                     case AssetType.Notecard:
203                         client.Inventory.RequestCopyItemFromNotecard(UUID.Zero,
204                             notecard.UUID,
205                             notecard.ParentUUID,
206                             item.UUID,
207                             Inventory_OnInventoryItemCopied);
208                         break;
209                 }
210             }
211         }
212
213         private void btnRefresh_Click(object sender, EventArgs e)
214         {
215             if (notecard.AssetUUID == UUID.Zero) return;
216
217             rtbContent.Text = "Loading...";
218             client.Assets.RequestInventoryAsset(notecard, true, Assets_OnAssetReceived);
219         }
220
221         private void rtbContent_LinkClicked(object sender, LinkClickedEventArgs e)
222         {
223             //instance.MainForm.processLink(e.LinkText);
224         }
225
226
227         #region Detach/Attach
228         protected override void ControlIsNotRetachable()
229         {
230             tbtnAttach.Visible = false;
231         }
232
233         protected override void Detach()
234         {
235             base.Detach();
236             tbtnAttach.Text = "Attach";
237             tbtnExit.Enabled = true;
238         }
239
240         protected override void Retach()
241         {
242             base.Retach();
243             tbtnAttach.Text = "Detach";
244             tbtnExit.Enabled = false;
245         }
246
247         private void tbtnAttach_Click(object sender, EventArgs e)
248         {
249             if (Detached)
250             {
251                 Retach();
252             }
253             else
254             {
255                 Detach();
256             }
257         }
258         #endregion
259
260         private void tbtnExit_Click(object sender, EventArgs e)
261         {
262             if (Detached)
263             {
264                 FindForm().Close();
265             }
266         }
267
268         private void tbtnSave_Click(object sender, EventArgs e)
269         {
270             bool success = false;
271             string message = "";
272             AssetNotecard n = new AssetNotecard();
273             n.BodyText = rtbContent.Text;
274             n.EmbeddedItems = new List<InventoryItem>();
275
276             if (receivedNotecard != null)
277             {
278                 for (int i = 0; i < receivedNotecard.EmbeddedItems.Count; i++)
279                 {
280                     n.EmbeddedItems.Add(receivedNotecard.EmbeddedItems[i]);
281                     int indexChar = 0xdc00 + i;
282                     n.BodyText += (char)0xdbc0;
283                     n.BodyText += (char)indexChar;
284                 }
285             }
286
287             n.Encode();
288
289             UpdateStatus("Saving...");
290
291             InventoryManager.InventoryUploadedAssetCallback handler = delegate(bool uploadSuccess, string status, UUID itemID, UUID assetID)
292                     {
293                         success = uploadSuccess;
294                         if (itemID == notecard.UUID)
295                         {
296                             if (success)
297                             {
298                                 UpdateStatus("OK");
299                                 notecard.AssetUUID = assetID;
300                             }
301                             else
302                             {
303                                 UpdateStatus("Failed");
304                             }
305
306                         }
307                         message = status ?? "Unknown error uploading notecard asset";
308                     };
309
310             if (prim == null)
311             {
312                 client.Inventory.RequestUploadNotecardAsset(n.AssetData, notecard.UUID, handler);
313             }
314             else
315             {
316                 client.Inventory.RequestUpdateNotecardTask(n.AssetData, notecard.UUID, prim.ID, handler);
317             }
318         }
319
320         void UpdateStatus(string status)
321         {
322             if (InvokeRequired)
323             {
324                 if (IsHandleCreated)
325                     BeginInvoke(new MethodInvoker(() => UpdateStatus(status)));
326                 return;
327             }
328             instance.TabConsole.DisplayNotificationInChat("Notecard status: " + status, ChatBufferTextStyle.Invisible);
329             tlblStatus.Text = status;
330         }
331
332         private void rtbContent_KeyDown(object sender, KeyEventArgs e)
333         {
334             if (e.KeyCode == Keys.S && e.Control)
335             {
336                 if (e.Shift)
337                 {
338                 }
339                 else
340                 {
341                     tbtnSave_Click(this, EventArgs.Empty);
342                     e.Handled = e.SuppressKeyPress = true;
343                 }
344             }
345
346         }
347
348         private void rtbContent_Enter(object sender, EventArgs e)
349         {
350             instance.TabConsole.DisplayNotificationInChat("Editing notecard", ChatBufferTextStyle.Invisible);
351         }
352
353         private void btnKeep_Click(object sender, EventArgs e)
354         {
355             Retach();
356         }
357
358         private void btnDiscard_Click(object sender, EventArgs e)
359         {
360             client.Inventory.MoveItem(notecard.UUID, client.Inventory.FindFolderForType(AssetType.TrashFolder), notecard.Name);
361             Retach();
362         }
363     }
364 }