OSDN Git Service

Added licensing and revision infro to source files
[radegast/radegast.git] / Radegast / GUI / Consoles / AnimDetail.cs
1 // \r
2 // Radegast Metaverse Client\r
3 // Copyright (c) 2009, Radegast Development Team\r
4 // All rights reserved.\r
5 // \r
6 // Redistribution and use in source and binary forms, with or without\r
7 // modification, are permitted provided that the following conditions are met:\r
8 // \r
9 //     * Redistributions of source code must retain the above copyright notice,\r
10 //       this list of conditions and the following disclaimer.\r
11 //     * Redistributions in binary form must reproduce the above copyright\r
12 //       notice, this list of conditions and the following disclaimer in the\r
13 //       documentation and/or other materials provided with the distribution.\r
14 //     * Neither the name of the application "Radegast", nor the names of its\r
15 //       contributors may be used to endorse or promote products derived from\r
16 //       this software without specific prior written permission.\r
17 // \r
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 //\r
29 // $Id$\r
30 //\r
31 using System;\r
32 using System.Collections.Generic;\r
33 using System.ComponentModel;\r
34 using System.Drawing;\r
35 using System.Data;\r
36 using System.Text;\r
37 using System.Windows.Forms;\r
38 using System.IO;\r
39 using OpenMetaverse;\r
40 using OpenMetaverse.Assets;\r
41 \r
42 namespace Radegast\r
43 {\r
44     public partial class AnimDetail : UserControl\r
45     {\r
46         private RadegastInstance instance;\r
47         private Avatar av;\r
48         private UUID anim;\r
49         private int n;\r
50         private List<FriendInfo> friends;\r
51         private FriendInfo friend;\r
52 \r
53         public AnimDetail(RadegastInstance instance, Avatar av, UUID anim, int n)\r
54         {\r
55             InitializeComponent();\r
56             Disposed += new EventHandler(AnimDetail_Disposed);\r
57             this.instance = instance;\r
58             this.av = av;\r
59             this.anim = anim;\r
60             this.n = n;\r
61 \r
62             // Callbacks\r
63             instance.Client.Assets.OnAssetReceived += new AssetManager.AssetReceivedCallback(Assets_OnAssetReceived);\r
64 \r
65         }\r
66 \r
67         void AnimDetail_Disposed(object sender, EventArgs e)\r
68         {\r
69             instance.Client.Assets.OnAssetReceived -= new AssetManager.AssetReceivedCallback(Assets_OnAssetReceived);\r
70         }\r
71 \r
72         private void AnimDetail_Load(object sender, EventArgs e)\r
73         {\r
74             if (File.Exists(instance.animCacheDir + "/" + anim + ".fail")) {\r
75                 Visible = false;\r
76                 return;\r
77             }\r
78 \r
79             groupBox1.Text = "Animation " + n + " (" + anim + ") for " + av.Name;\r
80 \r
81             friends = instance.Client.Friends.FriendList.FindAll(delegate(FriendInfo f) { return true; });\r
82 \r
83             pnlSave.Visible = false;\r
84             boxAnimName.Text = "Animation " + n;\r
85             cbFriends.DropDownStyle = ComboBoxStyle.DropDownList;\r
86 \r
87             foreach (FriendInfo f in friends) {\r
88                 cbFriends.Items.Add(f);\r
89             }\r
90 \r
91             if (File.Exists(instance.animCacheDir + "/" + anim + ".sla"))\r
92             {\r
93                 pnlSave.Visible = true;\r
94                 return;\r
95             }\r
96 \r
97 \r
98             instance.Client.Assets.RequestAsset(anim, AssetType.Animation, true);\r
99         }\r
100 \r
101         private void btnSave_Click(object sender, EventArgs e)\r
102         {\r
103             WindowWrapper mainWindow = new WindowWrapper(frmMain.ActiveForm.Handle);\r
104             System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog();\r
105             dlg.AddExtension = true;\r
106             dlg.RestoreDirectory = true;\r
107             dlg.Title = "Save animation as...";\r
108             dlg.Filter = "Second Life Animation (*.sla)|*.sla";\r
109             DialogResult res = dlg.ShowDialog();\r
110 \r
111             if (res == DialogResult.OK) {\r
112                 try {\r
113                     byte[] data = File.ReadAllBytes(instance.animCacheDir + "/" + anim + ".sla");\r
114                     File.WriteAllBytes(dlg.FileName, data);\r
115                 } catch (Exception ex) {\r
116                     Logger.Log("Saving animation failed: " + ex.Message, Helpers.LogLevel.Debug);\r
117                 }\r
118             }\r
119         }\r
120 \r
121         void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)\r
122         {\r
123             if (transfer.AssetID != anim) return;\r
124 \r
125             if (InvokeRequired) {\r
126                 Invoke(new MethodInvoker(delegate()\r
127                 {\r
128                     Assets_OnAssetReceived(transfer, asset);\r
129                 }));\r
130                 return;\r
131             }\r
132 \r
133             if (transfer.Success) {\r
134                 Logger.Log("Animation " + anim + " download success " + asset.AssetData.Length + " bytes.", Helpers.LogLevel.Debug);\r
135                 File.WriteAllBytes(instance.animCacheDir + "/" + anim + ".sla", asset.AssetData);\r
136                 pnlSave.Visible = true;\r
137             } else {\r
138                 Logger.Log("Animation " + anim + " download failed.", Helpers.LogLevel.Debug);\r
139                 FileStream f = File.Create(instance.animCacheDir + "/" + anim + ".fail");\r
140                 f.Close();\r
141                 Visible = false;\r
142             }\r
143         }\r
144 \r
145         private void playBox_CheckStateChanged(object sender, EventArgs e)\r
146         {\r
147             if (playBox.CheckState == CheckState.Checked) {\r
148                 instance.Client.Self.AnimationStart(anim, true);\r
149             } else {\r
150                 instance.Client.Self.AnimationStop(anim, true);\r
151             }\r
152         }\r
153 \r
154         private void btnSend_Click(object sender, EventArgs e)\r
155         {\r
156             byte[] data = File.ReadAllBytes(instance.animCacheDir + "/" + anim + ".sla");\r
157             instance.Client.Inventory.RequestCreateItemFromAsset(data, boxAnimName.Text, "(No description)", AssetType.Animation, InventoryType.Animation, instance.Client.Inventory.FindFolderForType(AssetType.Animation), On_ItemCreated);\r
158             lblStatus.Text = "Uploading...";\r
159             cbFriends.Enabled = false;\r
160         }\r
161 \r
162         void On_ItemCreated(bool success, string status, UUID itemID, UUID assetID)\r
163         {\r
164             if (InvokeRequired) {\r
165                 Invoke(new MethodInvoker(delegate()\r
166                 {\r
167                     On_ItemCreated(success, status, itemID, assetID);\r
168                 }\r
169                 ));\r
170                 return;\r
171             }\r
172 \r
173             if (!success) {\r
174                 lblStatus.Text = "Failed.";\r
175                 Logger.Log("Failed creating asset", Helpers.LogLevel.Debug);\r
176                 return;\r
177             } else {\r
178                 Logger.Log("Created inventory item " + itemID.ToString(), Helpers.LogLevel.Info);\r
179 \r
180                 lblStatus.Text = "Sending to " + friend.Name;\r
181                 Refresh();\r
182 \r
183                 // Fix the permissions on the new upload since they are fscked by default\r
184                 InventoryItem item = instance.Client.Inventory.FetchItem(itemID, instance.Client.Self.AgentID, 1000);\r
185 \r
186                 if (item != null) {\r
187                     // item.Permissions.EveryoneMask = PermissionMask.All;\r
188                     item.Permissions.NextOwnerMask = PermissionMask.All;\r
189                     instance.Client.Inventory.RequestUpdateItem(item);\r
190 \r
191 \r
192                     // FIXME: We should be watching the callback for RequestUpdateItem instead of a dumb sleep\r
193                     System.Threading.Thread.Sleep(2000);\r
194 \r
195                     \r
196 \r
197                     Logger.Log("Sending item to " + friend.Name, Helpers.LogLevel.Info);\r
198                     instance.Client.Inventory.GiveItem(itemID, item.Name, AssetType.Animation, friend.UUID, true);\r
199                     lblStatus.Text = "Sent";\r
200                 }\r
201             }\r
202 \r
203             cbFriends.Enabled = true;\r
204         }\r
205 \r
206 \r
207         private void cbFriends_SelectedValueChanged(object sender, EventArgs e)\r
208         {\r
209             if (cbFriends.SelectedIndex >= 0) {\r
210                 btnSend.Enabled = true;\r
211                 friend = friends[cbFriends.SelectedIndex];\r
212             } else {\r
213                 btnSend.Enabled = false;\r
214             }\r
215         }\r
216     }\r
217 }\r