OSDN Git Service

1907e7dcd5b4a54f464b8875fb66ebf64111a984
[radegast/radegast.git] / Radegast / GUI / Notifications / InventoryOfferNotification.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, 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: FriendshipOfferNotification.cs 175 2009-08-29 13:52:32Z latifer@gmail.com $
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.ComponentModel;
34 using System.Drawing;
35 using System.Data;
36 using System.Linq;
37 using System.Text;
38 using System.Windows.Forms;
39 using OpenMetaverse;
40 using OpenMetaverse.Assets;
41 using OpenMetaverse.Packets;
42
43 namespace Radegast
44 {
45     public partial class ntfInventoryOffer : Notification
46     {
47         private RadegastInstance instance;
48         private GridClient client { get { return instance.Client; } }
49         private InstantMessage msg;
50         private AssetType type = AssetType.Unknown;
51         private UUID objectID = UUID.Zero;
52         UUID destinationFolderID;
53
54         public ntfInventoryOffer(RadegastInstance instance, InstantMessage msg)
55             : base (NotificationType.InventoryOffer)
56         {
57             InitializeComponent();
58             Disposed += new EventHandler(ntfInventoryOffer_Disposed);
59
60             this.instance = instance;
61             this.msg = msg;
62
63             instance.Names.NameUpdated += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
64
65             if (msg.BinaryBucket.Length > 0)
66             {
67                 type = (AssetType)msg.BinaryBucket[0];
68                 destinationFolderID = client.Inventory.FindFolderForType(type);
69
70                 if (msg.BinaryBucket.Length == 17)
71                 {
72                     objectID = new UUID(msg.BinaryBucket, 1);
73                 }
74
75                 if (msg.Dialog == InstantMessageDialog.InventoryOffered)
76                 {
77                     txtInfo.Text = string.Format("{0} has offered you {1} \"{2}\".", msg.FromAgentName, type.ToString(), msg.Message);
78                 }
79                 else if (msg.Dialog == InstantMessageDialog.TaskInventoryOffered)
80                 {
81                     txtInfo.Text = objectOfferText();
82                 }
83
84                 // Fire off event
85                 NotificationEventArgs args = new NotificationEventArgs(instance);
86                 args.Text = txtInfo.Text;
87                 args.Buttons.Add(btnAccept);
88                 args.Buttons.Add(btnDiscard);
89                 args.Buttons.Add(btnIgnore);
90                 FireNotificationCallback(args);
91             }
92             else
93             {
94                 Logger.Log("Wrong format of the item offered", Helpers.LogLevel.Warning, client);
95             }
96         }
97
98         void ntfInventoryOffer_Disposed(object sender, EventArgs e)
99         {
100             instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
101         }
102
103         void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)
104         {
105             if (e.Names.Keys.Contains(msg.FromAgentID))
106             {
107                 instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
108                 BeginInvoke(new MethodInvoker(() => txtInfo.Text = objectOfferText()));
109             }
110         }
111
112         private string objectOfferText()
113         {
114             return string.Format("Object \"{0}\" owned by {1} has offered you {2}", msg.FromAgentName, instance.Names.Get(msg.FromAgentID), msg.Message); 
115         }
116
117         private void SendReply(InstantMessageDialog dialog, byte[] bucket)
118         {
119             client.Self.InstantMessage(client.Self.Name, msg.FromAgentID, string.Empty, msg.IMSessionID, dialog, InstantMessageOnline.Offline, client.Self.SimPosition, client.Network.CurrentSim.RegionID, bucket);
120             
121             if (dialog == InstantMessageDialog.InventoryAccepted)
122             {
123                 client.Inventory.RequestFetchInventory(objectID, client.Self.AgentID);
124             }
125         }
126
127         private void btnAccept_Click(object sender, EventArgs e)
128         {
129             if (type == AssetType.Unknown) return;
130
131             if (msg.Dialog == InstantMessageDialog.InventoryOffered)
132             {
133                 SendReply(InstantMessageDialog.InventoryAccepted, destinationFolderID.GetBytes());
134             }
135             else if (msg.Dialog == InstantMessageDialog.TaskInventoryOffered)
136             {
137                 SendReply(InstantMessageDialog.TaskInventoryAccepted, destinationFolderID.GetBytes());
138             }
139             instance.MainForm.RemoveNotification(this);
140         }
141
142         private void btnDiscard_Click(object sender, EventArgs e)
143         {
144             if (type == AssetType.Unknown) return;
145
146             if (msg.Dialog == InstantMessageDialog.InventoryOffered)
147             {
148                 SendReply(InstantMessageDialog.InventoryDeclined, Utils.EmptyBytes);
149                 try
150                 {
151                     client.Inventory.Move(
152                         client.Inventory.Store.Items[objectID].Data,
153                         (InventoryFolder)client.Inventory.Store.Items[client.Inventory.FindFolderForType(AssetType.TrashFolder)].Data);
154                 }
155                 catch (Exception) { }
156             }
157             else if (msg.Dialog == InstantMessageDialog.TaskInventoryOffered)
158             {
159                 SendReply(InstantMessageDialog.TaskInventoryDeclined, Utils.EmptyBytes);
160             }
161             instance.MainForm.RemoveNotification(this);
162         }
163
164         private void btnIgnore_Click(object sender, EventArgs e)
165         {
166             instance.MainForm.RemoveNotification(this);
167         }
168
169         private void txtInfo_LinkClicked(object sender, LinkClickedEventArgs e)
170         {
171             instance.MainForm.ProcessLink(e.LinkText, true);
172         }
173     }
174 }