OSDN Git Service

Update copyright year
[radegast/radegast.git] / Radegast / GUI / Dialogs / Pay.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, 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
36 namespace Radegast
37 {
38     public partial class frmPay : RadegastForm
39     {
40         private RadegastInstance instance;
41         private GridClient client { get { return instance.Client; } }
42         private UUID target;
43         private string name;
44         private UUID owner;
45         private bool isObject;
46         private Button[] buttons;
47         private int[] defaultAmounts = new int[4] { 1, 5, 10, 20 };
48         public static int LastPayed = -1;
49
50         public frmPay(RadegastInstance instance, UUID target, string name, bool isObject)
51         {
52             InitializeComponent();
53             Disposed += new EventHandler(frmPay_Disposed);
54
55             this.instance = instance;
56             this.target = target;
57             this.name = name;
58             this.isObject = isObject;
59
60             // Buttons
61             buttons = new Button[4] { btnFastPay1, btnFastPay2, btnFastPay3, btnFastPay4 };
62             for (int i = 0; i < buttons.Length; i++)
63             {
64                 buttons[i].Click += new EventHandler(frmPay_Click);
65                 buttons[i].Text = string.Format("L${0}", defaultAmounts[i]);
66                 buttons[i].Tag = defaultAmounts[i];
67             }
68
69             // Callbacks
70             client.Objects.PayPriceReply += new EventHandler<PayPriceReplyEventArgs>(Objects_PayPriceReply);
71             client.Objects.ObjectPropertiesFamily += new EventHandler<ObjectPropertiesFamilyEventArgs>(Objects_ObjectPropertiesFamily);
72             instance.Names.NameUpdated += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
73
74             if (isObject)
75             {
76                 client.Objects.RequestPayPrice(client.Network.CurrentSim, target);
77                 client.Objects.RequestObjectPropertiesFamily(client.Network.CurrentSim, target);
78                 lblObject.Visible = true;
79                 lblObject.Text = string.Format("Via object: {0}: ", name);
80             }
81             else
82             {
83                 lblObject.Visible = false;
84                 lblResident.Text = string.Format("Pay resident: {0}", name);
85             }
86
87             if (LastPayed > 0)
88             {
89                 txtAmount.Text = LastPayed.ToString();
90             }
91         }
92
93         void frmPay_Disposed(object sender, EventArgs e)
94         {
95             client.Objects.PayPriceReply -= new EventHandler<PayPriceReplyEventArgs>(Objects_PayPriceReply);
96             client.Objects.ObjectPropertiesFamily -= new EventHandler<ObjectPropertiesFamilyEventArgs>(Objects_ObjectPropertiesFamily);
97             instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
98         }
99
100         void frmPay_Click(object sender, EventArgs e)
101         {
102             int amount = (int)((Button)sender).Tag;
103
104             if (!isObject)
105             {
106                 client.Self.GiveAvatarMoney(target, amount);
107             }
108             else
109             {
110                 client.Self.GiveObjectMoney(target, amount, name);
111             }
112             Close();
113         }
114
115         void UpdateResident()
116         {
117             if (InvokeRequired)
118             {
119                 BeginInvoke(new MethodInvoker(delegate()
120                 {
121                     UpdateResident();
122                 }
123                 ));
124                 return;
125             }
126
127             lblResident.Text = string.Format("Pay resident: {0}", instance.Names.Get(owner));
128         }
129
130         void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)
131         {
132             if (e.Names.ContainsKey(owner))
133             {
134                 instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
135                 UpdateResident();
136             }
137         }
138
139
140         void Objects_ObjectPropertiesFamily(object sender, ObjectPropertiesFamilyEventArgs e)
141         {
142             if (e.Properties.ObjectID == target)
143             {
144                 owner = e.Properties.OwnerID;
145                 UpdateResident();
146             }
147         }
148
149         void Objects_PayPriceReply(object sender, PayPriceReplyEventArgs e)
150         {
151             if (e.ObjectID != target) return;
152
153             if (InvokeRequired)
154             {
155                 BeginInvoke(new MethodInvoker(() => Objects_PayPriceReply(sender, e)));
156                 return;
157             }
158
159             switch (e.DefaultPrice)
160             {
161                 case (int)PayPriceType.Default:
162                     txtAmount.Text = string.Empty;
163                     break;
164                 case (int)PayPriceType.Hide:
165                     txtAmount.Visible = false;
166                     break;
167                 default:
168                     txtAmount.Text = e.DefaultPrice.ToString();
169                     break;
170             }
171
172             for (int i = 0; i < buttons.Length; i++)
173             {
174                 switch (e.ButtonPrices[i])
175                 {
176                     case (int)PayPriceType.Default:
177                         buttons[i].Text = string.Format("L${0}", defaultAmounts[i]);
178                         buttons[i].Tag = defaultAmounts[i];
179                         break;
180                     case (int)PayPriceType.Hide:
181                         buttons[i].Visible = false;
182                         break;
183                     default:
184                         buttons[i].Text = string.Format("L${0}", e.ButtonPrices[i]);
185                         buttons[i].Tag = e.ButtonPrices[i];
186                         break;
187                 }
188
189             }
190         }
191
192
193         private void btnCancel_Click(object sender, EventArgs e)
194         {
195             Close();
196         }
197
198         private void btnPay_Click(object sender, EventArgs e)
199         {
200             int amount;
201
202             if (int.TryParse(txtAmount.Text, out amount) && amount > 0)
203             {
204                 if (amount > client.Self.Balance)
205                 {
206                     lblStatus.Visible = true;
207                     return;
208                 }
209
210                 LastPayed = amount;
211
212                 if (!isObject)
213                 {
214                     client.Self.GiveAvatarMoney(target, amount);
215                 }
216                 else
217                 {
218                     client.Self.GiveObjectMoney(target, amount, name);
219                 }
220             }
221             Close();
222         }
223
224         private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
225         {
226             if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
227             {
228                 e.Handled = true;
229             }
230         }
231
232         private void txtAmount_TextChanged(object sender, EventArgs e)
233         {
234             int amount = 0;
235             if (int.TryParse(txtAmount.Text, out amount) && amount > 0)
236             {
237                 if (amount > client.Self.Balance)
238                 {
239                     lblStatus.Visible = true;
240                     btnPay.Enabled = false;
241                 }
242                 else
243                 {
244                     lblStatus.Visible = false;
245                     btnPay.Enabled = true;
246                 }
247             }
248             else
249             {
250                 btnPay.Enabled = false;
251             }
252         }
253     }
254 }