OSDN Git Service

Merged Mojito's sounds branch
[radegast/radegast.git] / Radegast / GUI / Dialogs / MainForm.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009, 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.Drawing;
34 using System.Text;
35 using System.Text.RegularExpressions;
36 using System.Timers;
37 using System.Threading;
38 using System.Windows.Forms;
39 using System.Resources;
40 using System.IO;
41 using System.Web;
42 using Radegast.Netcom;
43 using OpenMetaverse;
44 using OpenMetaverse.StructuredData;
45 using OpenMetaverse.Assets;
46
47 namespace Radegast
48 {
49     public partial class frmMain : RadegastForm
50     {
51         #region Public members
52         public static ImageList ResourceImages = new ImageList();
53         public static List<string> ImageNames = new List<string>();
54
55         public TabsConsole TabConsole
56         {
57             get { return tabsConsole; }
58         }
59
60         public MapConsole WorldMap
61         {
62             get
63             {
64                 if (MapTab != null)
65                 {
66                     return (MapConsole)MapTab.Control;
67                 }
68                 return null;
69             }
70         }
71
72         public RadegastTab MapTab
73         {
74             get
75             {
76                 if (tabsConsole.TabExists("map"))
77                 {
78                     return tabsConsole.Tabs["map"];
79                 }
80                 else
81                 {
82                     return null;
83                 }
84             }
85         }
86
87         public MediaConsole MediaConsole { get { return mediaConsole; } }
88
89         /// <summary>
90         /// Drop down that contains the tools menu
91         /// </summary>
92         public ToolStripDropDownButton ToolsMenu
93         {
94             get { return tbnTools; }
95         }
96
97         /// <summary>
98         /// Dropdown that contains the heelp menu
99         /// </summary>
100         public ToolStripDropDownButton HelpMenu
101         {
102             get { return tbtnHelp; }
103         }
104
105         /// <summary>
106         /// Drop down that contants the plugins menu. Make sure to set it Visible if
107         /// you add items to this menu, it's hidden by default
108         /// </summary>
109         public ToolStripDropDownButton PluginsMenu
110         {
111             get { return tbnPlugins; }
112         }
113
114         #endregion
115
116         #region Private members
117         private RadegastInstance instance;
118         private GridClient client { get { return instance.Client; } }
119         private RadegastNetcom netcom { get { return instance.Netcom; } }
120         private TabsConsole tabsConsole;
121         private System.Timers.Timer statusTimer;
122         private AutoPilot ap;
123         private bool AutoPilotActive = false;
124         private TransparentButton btnDialogNextControl;
125         private MediaConsole mediaConsole;
126         #endregion
127
128         #region Constructor and disposal
129         public frmMain(RadegastInstance instance)
130             : base(instance)
131         {
132             GetSLTimeZone();
133             InitializeComponent();
134             Disposed += new EventHandler(frmMain_Disposed);
135
136             this.instance = instance;
137             this.instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
138             netcom.NetcomSync = this;
139
140             pnlDialog.Visible = false;
141             btnDialogNextControl = new TransparentButton();
142             pnlDialog.Controls.Add(btnDialogNextControl);
143             pnlDialog.Top = 0;
144
145             btnDialogNextControl.Size = new Size(35, 20);
146             btnDialogNextControl.BackColor = Color.Transparent;
147             btnDialogNextControl.ForeColor = Color.Gold;
148             btnDialogNextControl.FlatAppearance.BorderSize = 0;
149             btnDialogNextControl.FlatStyle = FlatStyle.Flat;
150             btnDialogNextControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
151             btnDialogNextControl.Text = ">>";
152             btnDialogNextControl.Font = new Font(btnDialogNextControl.Font, FontStyle.Bold);
153             btnDialogNextControl.Margin = new Padding(0);
154             btnDialogNextControl.Padding = new Padding(0);
155             btnDialogNextControl.UseVisualStyleBackColor = false;
156             btnDialogNextControl.Top = btnDialogNextControl.Parent.ClientSize.Height - btnDialogNextControl.Size.Height;
157             btnDialogNextControl.Left = btnDialogNextControl.Parent.ClientSize.Width - btnDialogNextControl.Size.Width;
158             btnDialogNextControl.Click += new EventHandler(btnDialogNextControl_Click);
159
160             if (instance.MonoRuntime)
161             {
162                 statusStrip1.LayoutStyle = ToolStripLayoutStyle.Table;
163             }
164
165             // Config options
166             if (instance.GlobalSettings["transaction_notification_chat"].Type == OSDType.Unknown)
167                 instance.GlobalSettings["transaction_notification_chat"] = OSD.FromBoolean(true);
168
169             if (instance.GlobalSettings["transaction_notification_dialog"].Type == OSDType.Unknown)
170                 instance.GlobalSettings["transaction_notification_dialog"] = OSD.FromBoolean(true);
171
172             if (!instance.GlobalSettings.ContainsKey("minimize_to_tray"))
173                 instance.GlobalSettings["minimize_to_tray"] = OSD.FromBoolean(false);
174
175             // Callbacks
176             netcom.ClientLoginStatus += new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
177             netcom.ClientLoggedOut += new EventHandler(netcom_ClientLoggedOut);
178             netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
179             RegisterClientEvents(client);
180
181             InitializeStatusTimer();
182             RefreshWindowTitle();
183         }
184
185         private void RegisterClientEvents(GridClient client)
186         {
187             client.Parcels.ParcelProperties += new EventHandler<ParcelPropertiesEventArgs>(Parcels_ParcelProperties);
188             client.Self.MoneyBalanceReply += new EventHandler<MoneyBalanceReplyEventArgs>(Self_MoneyBalanceReply);
189         }
190
191         private void UnregisterClientEvents(GridClient client)
192         {
193             client.Parcels.ParcelProperties -= new EventHandler<ParcelPropertiesEventArgs>(Parcels_ParcelProperties);
194             client.Self.MoneyBalanceReply -= new EventHandler<MoneyBalanceReplyEventArgs>(Self_MoneyBalanceReply);
195         }
196
197         void instance_ClientChanged(object sender, ClientChangedEventArgs e)
198         {
199             UnregisterClientEvents(e.OldClient);
200             RegisterClientEvents(client);
201         }
202
203         void frmMain_Disposed(object sender, EventArgs e)
204         {
205             if (netcom != null)
206             {
207                 netcom.NetcomSync = null;
208                 netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
209                 netcom.ClientLoggedOut -= new EventHandler(netcom_ClientLoggedOut);
210                 netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
211             }
212             if (client != null)
213             {
214                 UnregisterClientEvents(client);
215             }
216             this.instance.CleanUp();
217         }
218         #endregion
219
220         #region Event handlers
221         void Self_MoneyBalanceReply(object sender, MoneyBalanceReplyEventArgs e)
222         {
223             if (!String.IsNullOrEmpty(e.Description))
224             {
225                 if (instance.GlobalSettings["transaction_notification_dialog"].AsBoolean())
226                     AddNotification(new ntfGeneric(instance, e.Description));
227                 if (instance.GlobalSettings["transaction_notification_chat"].AsBoolean())
228                     TabConsole.DisplayNotificationInChat(e.Description);
229             }
230         }
231
232         public void InitializeControls()
233         {
234             InitializeTabsConsole();
235
236             if (instance.MediaManager.SoundSystemAvailable)
237             {
238                 mediaConsole = new MediaConsole(instance);
239                 tbtnMedia.Visible = true;
240             }
241         }
242
243         public bool InAutoReconnect { get; set; }
244
245         private void DisplayAutoReconnectForm()
246         {
247             if (IsDisposed) return;
248
249             if (InvokeRequired)
250             {
251                 BeginInvoke(new MethodInvoker(DisplayAutoReconnectForm));
252                 return;
253             }
254
255             InAutoReconnect = true;
256             frmReconnect dialog = new frmReconnect(instance, 120);
257             dialog.ShowDialog(this);
258             dialog.Dispose();
259             dialog = null;
260         }
261
262         public void BeginAutoReconnect()
263         {
264             // Sleep for 3 seconds on a separate thread while things unwind on
265             // disconnect, since ShowDialog() blocks GUI thread
266             (new Thread(new ThreadStart(() =>
267                 {
268                     System.Threading.Thread.Sleep(3000);
269                     DisplayAutoReconnectForm();
270                 }
271                 ))
272                 {
273                     Name = "Reconnect Delay Thread",
274                     IsBackground = true
275                 }
276             ).Start();
277         }
278
279         private void netcom_ClientLoginStatus(object sender, LoginProgressEventArgs e)
280         {
281             if (e.Status == LoginStatus.Failed)
282             {
283                 if (InAutoReconnect)
284                 {
285                     if (instance.GlobalSettings["auto_reconnect"].AsBoolean())
286                         BeginAutoReconnect();
287                     else
288                         InAutoReconnect = false;
289                 }
290             }
291             else if (e.Status == LoginStatus.Success)
292             {
293                 InAutoReconnect = false;
294                 tbtnVoice.Enabled = disconnectToolStripMenuItem.Enabled =
295                 tbtnGroups.Enabled = tbnObjects.Enabled = tbtnWorld.Enabled = tbnTools.Enabled = tmnuImport.Enabled =
296                     tbtnFriends.Enabled = tbtnInventory.Enabled = tbtnSearch.Enabled = tbtnMap.Enabled = true;
297
298                 statusTimer.Start();
299                 RefreshWindowTitle();
300             }
301         }
302
303         private void netcom_ClientLoggedOut(object sender, EventArgs e)
304         {
305             tbtnVoice.Enabled = disconnectToolStripMenuItem.Enabled =
306             tbtnGroups.Enabled = tbnObjects.Enabled = tbtnWorld.Enabled = tbnTools.Enabled = tmnuImport.Enabled =
307                 tbtnFriends.Enabled = tbtnInventory.Enabled = tbtnSearch.Enabled = tbtnMap.Enabled = false;
308
309             reconnectToolStripMenuItem.Enabled = true;
310             InAutoReconnect = false;
311
312             if (statusTimer != null)
313                 statusTimer.Stop();
314
315             RefreshStatusBar();
316             RefreshWindowTitle();
317         }
318
319         private void netcom_ClientDisconnected(object sender, DisconnectedEventArgs e)
320         {
321             if (e.Reason == NetworkManager.DisconnectType.ClientInitiated) return;
322             netcom_ClientLoggedOut(sender, EventArgs.Empty);
323
324             if (instance.GlobalSettings["auto_reconnect"].AsBoolean())
325             {
326                 BeginAutoReconnect();
327             }
328         }
329
330         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
331         {
332             if (statusTimer != null)
333             {
334                 statusTimer.Stop();
335                 statusTimer.Dispose();
336                 statusTimer = null;
337             }
338
339             if (mediaConsole != null)
340             {
341                 if (tabsConsole.TabExists("media"))
342                 {
343                     tabsConsole.Tabs["media"].AllowClose = true;
344                     tabsConsole.Tabs["media"].Close();
345                 }
346                 else
347                 {
348                     mediaConsole.Dispose();
349                 }
350                 mediaConsole = null;
351             }
352
353             if (netcom.IsLoggedIn)
354             {
355                 Thread saveInvToDisk = new Thread(new ThreadStart(
356                     delegate()
357                     {
358                         client.Inventory.Store.SaveToDisk(instance.InventoryCacheFileName);
359                     }));
360                 saveInvToDisk.Name = "Save inventory to disk";
361                 saveInvToDisk.Start();
362
363                 netcom.Logout();
364             }
365         }
366         #endregion
367
368         # region Update status
369
370         void Parcels_ParcelProperties(object sender, ParcelPropertiesEventArgs e)
371         {
372             if (e.Result != ParcelResult.Single) return;
373             if (InvokeRequired)
374             {
375                 BeginInvoke(new MethodInvoker(() => Parcels_ParcelProperties(sender, e)));
376                 return;
377             }
378
379             Parcel parcel = instance.State.Parcel = e.Parcel;
380
381             tlblParcel.Text = parcel.Name;
382             tlblParcel.ToolTipText = parcel.Desc;
383
384             if ((parcel.Flags & ParcelFlags.AllowFly) != ParcelFlags.AllowFly)
385                 icoNoFly.Visible = true;
386             else
387                 icoNoFly.Visible = false;
388
389             if ((parcel.Flags & ParcelFlags.CreateObjects) != ParcelFlags.CreateObjects)
390                 icoNoBuild.Visible = true;
391             else
392                 icoNoBuild.Visible = false;
393
394             if ((parcel.Flags & ParcelFlags.AllowOtherScripts) != ParcelFlags.AllowOtherScripts)
395                 icoNoScript.Visible = true;
396             else
397                 icoNoScript.Visible = false;
398
399             if ((parcel.Flags & ParcelFlags.RestrictPushObject) == ParcelFlags.RestrictPushObject)
400                 icoNoPush.Visible = true;
401             else
402                 icoNoPush.Visible = false;
403
404             if ((parcel.Flags & ParcelFlags.AllowDamage) == ParcelFlags.AllowDamage)
405                 icoHealth.Visible = true;
406             else
407                 icoHealth.Visible = false;
408
409             if ((parcel.Flags & ParcelFlags.AllowVoiceChat) != ParcelFlags.AllowVoiceChat)
410                 icoNoVoice.Visible = true;
411             else
412                 icoNoVoice.Visible = false;
413         }
414
415         private void RefreshStatusBar()
416         {
417             if (netcom.IsLoggedIn)
418             {
419                 tlblLoginName.Text = netcom.LoginOptions.FullName;
420                 tlblMoneyBalance.Text = client.Self.Balance.ToString();
421                 icoHealth.Text = client.Self.Health.ToString() + "%";
422
423                 tlblRegionInfo.Text =
424                     client.Network.CurrentSim.Name +
425                     " (" + Math.Floor(client.Self.SimPosition.X).ToString() + ", " +
426                     Math.Floor(client.Self.SimPosition.Y).ToString() + ", " +
427                     Math.Floor(client.Self.SimPosition.Z).ToString() + ")";
428             }
429             else
430             {
431                 tlblLoginName.Text = "Offline";
432                 tlblMoneyBalance.Text = "0";
433                 icoHealth.Text = "0%";
434                 tlblRegionInfo.Text = "No Region";
435                 tlblParcel.Text = "No Parcel";
436
437                 icoHealth.Visible = false;
438                 icoNoBuild.Visible = false;
439                 icoNoFly.Visible = false;
440                 icoNoPush.Visible = false;
441                 icoNoScript.Visible = false;
442                 icoNoVoice.Visible = false;
443             }
444         }
445
446         private void RefreshWindowTitle()
447         {
448             StringBuilder sb = new StringBuilder();
449             sb.Append("Radegast - ");
450
451             if (netcom.IsLoggedIn)
452             {
453                 sb.Append("[" + netcom.LoginOptions.FullName + "]");
454
455                 if (instance.State.IsAway)
456                 {
457                     sb.Append(" - Away");
458                     if (instance.State.IsBusy) sb.Append(", Busy");
459                 }
460                 else if (instance.State.IsBusy)
461                 {
462                     sb.Append(" - Busy");
463                 }
464
465                 if (instance.State.IsFollowing)
466                 {
467                     sb.Append(" - Following ");
468                     sb.Append(instance.State.FollowName);
469                 }
470             }
471             else
472             {
473                 sb.Append("Logged Out");
474             }
475
476             this.Text = sb.ToString();
477             sb = null;
478         }
479
480         private void InitializeStatusTimer()
481         {
482             statusTimer = new System.Timers.Timer(250);
483             statusTimer.SynchronizingObject = this;
484             statusTimer.Elapsed += new ElapsedEventHandler(statusTimer_Elapsed);
485         }
486
487         private void statusTimer_Elapsed(object sender, ElapsedEventArgs e)
488         {
489             // Mono sometimes fires timer after is's disposed
490             try
491             {
492                 RefreshWindowTitle();
493                 RefreshStatusBar();
494             }
495             catch { }
496         }
497         #endregion
498
499         #region Initialization, configuration, and key shortcuts
500         private void InitializeTabsConsole()
501         {
502             tabsConsole = new TabsConsole(instance);
503             tabsConsole.Dock = DockStyle.Fill;
504             toolStripContainer1.ContentPanel.Controls.Add(tabsConsole);
505         }
506
507         private void frmMain_KeyDown(object sender, KeyEventArgs e)
508         {
509             // Ctrl-W: Close tab
510             if (e.Modifiers == Keys.Control && e.KeyCode == Keys.W)
511             {
512                 e.Handled = e.SuppressKeyPress = true;
513                 RadegastTab tab = tabsConsole.SelectedTab;
514
515                 if (tab.AllowClose)
516                 {
517                     tab.Close();
518                 }
519                 else if (tab.AllowHide)
520                 {
521                     tab.Hide();
522                 }
523
524                 return;
525             }
526
527             // Ctl-Shift-H: Teleport Home
528             if (e.Modifiers == (Keys.Control | Keys.Shift) && e.KeyCode == Keys.H)
529             {
530                 e.Handled = e.SuppressKeyPress = true;
531                 tmnuTeleportHome.PerformClick();
532                 return;
533             }
534
535             // Alt-Ctrl-D Open debug console
536             if (e.Modifiers == (Keys.Control | Keys.Alt) && e.KeyCode == Keys.D)
537             {
538                 e.Handled = e.SuppressKeyPress = true;
539                 debugConsoleToolStripMenuItem.PerformClick();
540                 return;
541             }
542
543             // Alt 1-8: Toggle various tabs
544             if (e.Modifiers == Keys.Alt)
545             {
546                 switch (e.KeyCode)
547                 {
548                     case Keys.D1:
549                         e.Handled = e.SuppressKeyPress = true;
550                         tabsConsole.Tabs["chat"].Select();
551                         return;
552
553                     case Keys.D2:
554                         e.Handled = e.SuppressKeyPress = true;
555                         tbtnFriends.PerformClick();
556                         return;
557
558                     case Keys.D3:
559                         e.Handled = e.SuppressKeyPress = true;
560                         tbtnGroups.PerformClick();
561                         return;
562
563                     case Keys.D4:
564                         e.Handled = e.SuppressKeyPress = true;
565                         tbtnInventory.PerformClick();
566                         return;
567
568                     case Keys.D5:
569                         e.Handled = e.SuppressKeyPress = true;
570                         tbtnSearch.PerformClick();
571                         return;
572
573                     case Keys.D6:
574                         e.Handled = e.SuppressKeyPress = true;
575                         tbtnMap.PerformClick();
576                         return;
577
578                     case Keys.D7:
579                         e.Handled = e.SuppressKeyPress = true;
580                         tbnObjects.PerformClick();
581                         return;
582
583                     case Keys.D8:
584                         e.Handled = e.SuppressKeyPress = true;
585                         tbtnMedia.PerformClick();
586                         return;
587
588                     case Keys.D9:
589                         e.Handled = e.SuppressKeyPress = true;
590                         tbtnVoice.PerformClick();
591                         return;
592                 }
593             }
594
595             // ctrl-g, goto slurl
596             if (e.Control && e.KeyCode == Keys.G)
597             {
598                 if (!ProcessLink(Clipboard.GetText(), true))
599                     MapToCurrentLocation();
600
601                 e.Handled = e.SuppressKeyPress = true;
602                 return;
603             }
604
605             // ctrl-(shift)-tab for next/previous tab
606             if (e.Control && e.KeyCode == Keys.Tab)
607             {
608                 if (e.Shift)
609                 {
610                     TabConsole.SelectPreviousTab();
611                 }
612                 else
613                 {
614                     TabConsole.SelectNextTab();
615                 }
616                 e.Handled = e.SuppressKeyPress = true;
617                 return;
618             }
619         }
620
621         bool firstLoad = true;
622
623         private void frmMain_Load(object sender, EventArgs e)
624         {
625             if (firstLoad)
626             {
627                 firstLoad = false;
628                 tabsConsole.SelectTab("login");
629                 ResourceManager rm = Properties.Resources.ResourceManager;
630                 ResourceSet set = rm.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
631                 System.Collections.IDictionaryEnumerator de = set.GetEnumerator();
632                 while (de.MoveNext() == true)
633                 {
634                     if (de.Entry.Value is Image)
635                     {
636                         Bitmap bitMap = de.Entry.Value as Bitmap;
637                         ResourceImages.Images.Add(bitMap);
638                         ImageNames.Add(de.Entry.Key.ToString());
639                     }
640                 }
641                 StartUpdateCheck(false);
642             }
643         }
644         #endregion
645
646         #region Public methods
647
648         private Dictionary<UUID, frmProfile> shownProfiles = new Dictionary<UUID, frmProfile>();
649
650         public void ShowAgentProfile(string name, UUID agentID)
651         {
652             lock (shownProfiles)
653             {
654                 frmProfile profile = null;
655                 if (shownProfiles.TryGetValue(agentID, out profile))
656                 {
657                     profile.WindowState = FormWindowState.Normal;
658                     profile.Focus();
659                 }
660                 else
661                 {
662                     profile = new frmProfile(instance, name, agentID);
663
664                     profile.Disposed += (object sender, EventArgs e) =>
665                         {
666                             lock (shownProfiles)
667                             {
668                                 frmProfile agentProfile = (frmProfile)sender;
669                                 if (shownProfiles.ContainsKey(agentProfile.AgentID))
670                                     shownProfiles.Remove(agentProfile.AgentID);
671                             }
672                         };
673
674                     profile.Show();
675                     profile.Focus();
676                     shownProfiles.Add(agentID, profile);
677                 }
678             }
679         }
680
681         private Dictionary<UUID, frmGroupInfo> shownGroupProfiles = new Dictionary<UUID, frmGroupInfo>();
682
683         public void ShowGroupProfile(AvatarGroup group)
684         {
685             ShowGroupProfile(new OpenMetaverse.Group()
686             {
687                 ID = group.GroupID,
688                 InsigniaID = group.GroupInsigniaID,
689                 Name = group.GroupName
690             }
691             );
692         }
693
694         public void ShowGroupProfile(OpenMetaverse.Group group)
695         {
696             lock (shownGroupProfiles)
697             {
698                 frmGroupInfo profile = null;
699                 if (shownGroupProfiles.TryGetValue(group.ID, out profile))
700                 {
701                     profile.WindowState = FormWindowState.Normal;
702                     profile.Focus();
703                 }
704                 else
705                 {
706                     profile = new frmGroupInfo(instance, group);
707
708                     profile.Disposed += (object sender, EventArgs e) =>
709                         {
710                             lock (shownGroupProfiles)
711                             {
712                                 frmGroupInfo groupProfile = (frmGroupInfo)sender;
713                                 if (shownGroupProfiles.ContainsKey(groupProfile.Group.ID))
714                                     shownGroupProfiles.Remove(groupProfile.Group.ID);
715                             }
716                         };
717
718                     profile.Show();
719                     profile.Focus();
720                     shownGroupProfiles.Add(group.ID, profile);
721                 }
722             }
723         }
724
725         public void ProcessLink(string link)
726         {
727             ProcessLink(link, false);
728         }
729
730         public bool ProcessLink(string link, bool onlyMap)
731         {
732             if (!link.Contains("://"))
733             {
734                 link = "http://" + link;
735             }
736
737             Regex r = new Regex(@"^(http://(slurl\.com|maps\.secondlife\.com)/secondlife/|secondlife://)(?<region>[^/]+)/(?<x>\d+)/(?<y>\d+)(/(?<z>\d+))?",
738                 RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase
739                 );
740             Match m = r.Match(link);
741
742             if (m.Success)
743             {
744                 string region = HttpUtility.UrlDecode(m.Groups["region"].Value);
745                 int x = int.Parse(m.Groups["x"].Value);
746                 int y = int.Parse(m.Groups["y"].Value);
747                 int z = 0;
748
749                 if (!string.IsNullOrEmpty(m.Groups["z"].Value))
750                 {
751                     z = int.Parse(m.Groups["z"].Value);
752                 }
753
754                 MapTab.Select();
755                 WorldMap.DisplayLocation(region, x, y, z);
756                 return true;
757             }
758             else if (!onlyMap)
759             {
760                 System.Diagnostics.Process.Start(link);
761             }
762             return false;
763         }
764         #endregion
765
766         #region Notifications
767         CircularList<Control> notifications = new CircularList<Control>();
768
769         public Color NotificationBackground
770         {
771             get { return pnlDialog.BackColor; }
772         }
773
774         void ResizeNotificationByControl(Control active)
775         {
776             int Width = active.Size.Width + 6;
777             int Height = notifications.Count > 1 ? active.Size.Height + 3 + btnDialogNextControl.Size.Height : active.Size.Height + 3;
778             pnlDialog.Size = new Size(Width, Height);
779             pnlDialog.Top = 0;
780             pnlDialog.Left = pnlDialog.Parent.ClientSize.Width - Width;
781
782             btnDialogNextControl.Top = btnDialogNextControl.Parent.ClientSize.Height - btnDialogNextControl.Size.Height;
783             btnDialogNextControl.Left = btnDialogNextControl.Parent.ClientSize.Width - btnDialogNextControl.Size.Width;
784
785             btnDialogNextControl.BringToFront();
786         }
787
788         public void AddNotification(Control control)
789         {
790             if (InvokeRequired)
791             {
792                 BeginInvoke(new MethodInvoker(delegate()
793                 {
794                     AddNotification(control);
795                 }
796                 ));
797                 return;
798             }
799
800             FormFlash.StartFlash(this);
801             pnlDialog.Visible = true;
802             pnlDialog.BringToFront();
803
804             foreach (Control existing in notifications)
805             {
806                 existing.Visible = false;
807             }
808
809             notifications.Add(control);
810             control.Visible = true;
811             control.Anchor = AnchorStyles.Top | AnchorStyles.Left;
812             control.Top = 3;
813             control.Left = 3;
814             pnlDialog.Controls.Add(control);
815             ResizeNotificationByControl(control);
816
817             btnDialogNextControl.Visible = notifications.Count > 1;
818         }
819
820         public void RemoveNotification(Control control)
821         {
822             pnlDialog.Controls.Remove(control);
823             notifications.Remove(control);
824             control.Dispose();
825
826             if (notifications.HasNext)
827             {
828                 pnlDialog.Visible = true;
829                 Control active = notifications.Next;
830                 active.Visible = true;
831                 ResizeNotificationByControl(active);
832             }
833             else
834             {
835                 pnlDialog.Visible = false;
836             }
837
838             btnDialogNextControl.Visible = notifications.Count > 1;
839         }
840
841         private void btnDialogNextControl_Click(object sender, EventArgs e)
842         {
843             foreach (Control existing in notifications)
844             {
845                 existing.Visible = false;
846             }
847
848             if (notifications.HasNext)
849             {
850                 pnlDialog.Visible = true;
851                 Control active = notifications.Next;
852                 active.Visible = true;
853                 ResizeNotificationByControl(active);
854             }
855             else
856             {
857                 pnlDialog.Visible = false;
858             }
859
860         }
861         #endregion Notifications
862
863         #region Menu click handlers
864
865         private void tbtnTeleport_Click(object sender, EventArgs e)
866         {
867             (new frmTeleport(instance)).ShowDialog();
868         }
869
870         private void tmnuStatusAway_Click(object sender, EventArgs e)
871         {
872             instance.State.SetAway(tmnuStatusAway.Checked);
873         }
874
875         private void tmnuHelpReadme_Click(object sender, EventArgs e)
876         {
877             System.Diagnostics.Process.Start(Application.StartupPath + @"\Readme.txt");
878         }
879
880         private void tmnuStatusBusy_Click(object sender, EventArgs e)
881         {
882             instance.State.SetBusy(tmnuStatusBusy.Checked);
883         }
884
885         private void tmnuControlFly_Click(object sender, EventArgs e)
886         {
887             instance.State.SetFlying(tmnuControlFly.Checked);
888         }
889
890         private void tmnuControlAlwaysRun_Click(object sender, EventArgs e)
891         {
892             instance.State.SetAlwaysRun(tmnuControlAlwaysRun.Checked);
893         }
894
895         private void tmnuPrefs_Click(object sender, EventArgs e)
896         {
897             (new frmSettings(instance)).ShowDialog();
898         }
899
900         private void tbtnAppearance_Click(object sender, EventArgs e)
901         {
902             client.Appearance.RequestSetAppearance(false);
903         }
904
905         private void importObjectToolStripMenuItem_Click(object sender, EventArgs e)
906         {
907             PrimDeserializer.ImportFromFile(client);
908         }
909
910         private void autopilotToolStripMenuItem_Click(object sender, EventArgs e)
911         {
912             if (ap == null)
913             {
914                 ap = new AutoPilot(client);
915                 /*
916                 ap.InsertWaypoint(new Vector3(66, 163, 21));
917                 ap.InsertWaypoint(new Vector3(66, 98, 21));
918
919                 ap.InsertWaypoint(new Vector3(101, 98, 21));
920                 ap.InsertWaypoint(new Vector3(101, 45, 21));
921                 ap.InsertWaypoint(new Vector3(93, 27, 21));
922                 ap.InsertWaypoint(new Vector3(106, 12, 21));
923                 ap.InsertWaypoint(new Vector3(123, 24, 21));
924                 ap.InsertWaypoint(new Vector3(114, 45, 21));
925                 ap.InsertWaypoint(new Vector3(114, 98, 21));
926
927                 ap.InsertWaypoint(new Vector3(130, 98, 21));
928                 ap.InsertWaypoint(new Vector3(130, 163, 21));
929                  **/
930                 ap.InsertWaypoint(new Vector3(64, 68, 21));
931                 ap.InsertWaypoint(new Vector3(65, 20, 21));
932                 ap.InsertWaypoint(new Vector3(33, 23, 21));
933                 ap.InsertWaypoint(new Vector3(17, 39, 21));
934                 ap.InsertWaypoint(new Vector3(17, 62, 21));
935
936
937             }
938             if (AutoPilotActive)
939             {
940                 AutoPilotActive = false;
941                 ap.Stop();
942             }
943             else
944             {
945                 AutoPilotActive = true;
946                 ap.Start();
947             }
948
949         }
950
951         private void cleanCacheToolStripMenuItem_Click(object sender, EventArgs e)
952         {
953             client.Assets.Cache.Clear();
954         }
955
956         private void rebakeTexturesToolStripMenuItem_Click(object sender, EventArgs e)
957         {
958             client.Appearance.RequestSetAppearance(true);
959         }
960
961         public void MapToCurrentLocation()
962         {
963             if (MapTab != null && client.Network.Connected)
964             {
965                 MapTab.Select();
966                 WorldMap.DisplayLocation(client.Network.CurrentSim.Name,
967                     (int)client.Self.SimPosition.X,
968                     (int)client.Self.SimPosition.Y,
969                     (int)client.Self.SimPosition.Z);
970             }
971         }
972
973         private void standToolStripMenuItem_Click(object sender, EventArgs e)
974         {
975             client.Self.Stand();
976         }
977
978         private void groundSitToolStripMenuItem_Click(object sender, EventArgs e)
979         {
980             client.Self.SitOnGround();
981         }
982
983         private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
984         {
985             try { System.Diagnostics.Process.Start(Application.ExecutablePath); }
986             catch (Exception) { }
987         }
988
989         private void tmnuExit_Click(object sender, EventArgs e)
990         {
991             Close();
992         }
993
994         private void tlblRegionInfo_Click(object sender, EventArgs e)
995         {
996             if (WorldMap != null && client.Network.Connected)
997             {
998                 MapTab.Select();
999             }
1000         }
1001
1002         private void scriptEditorToolStripMenuItem_Click(object sender, EventArgs e)
1003         {
1004             ScriptEditor se = new ScriptEditor(instance);
1005             se.Dock = DockStyle.Fill;
1006             se.ShowDetached();
1007         }
1008
1009         private void tmnuSetHome_Click(object sender, EventArgs e)
1010         {
1011             client.Self.SetHome();
1012         }
1013
1014         private void tmnuCreateLandmark_Click(object sender, EventArgs e)
1015         {
1016             string location = string.Format(", {0} ({1}, {2}, {3})",
1017                 client.Network.CurrentSim.Name,
1018                 (int)client.Self.SimPosition.X,
1019                 (int)client.Self.SimPosition.Y,
1020                 (int)client.Self.SimPosition.Z
1021                 );
1022
1023             string name = tlblParcel.Text;
1024             int maxLen = 63 - location.Length;
1025
1026             if (name.Length > maxLen)
1027                 name = name.Substring(0, maxLen);
1028
1029             name += location;
1030
1031             client.Inventory.RequestCreateItem(
1032                 client.Inventory.FindFolderForType(AssetType.Landmark),
1033                 name,
1034                 tlblParcel.ToolTipText,
1035                 AssetType.Landmark,
1036                 UUID.Random(),
1037                 InventoryType.Landmark,
1038                 PermissionMask.All,
1039                 (bool success, InventoryItem item) =>
1040                 {
1041                     if (success)
1042                     {
1043                         BeginInvoke(new MethodInvoker(() =>
1044                             {
1045                                 Landmark ln = new Landmark(instance, (InventoryLandmark)item);
1046                                 ln.Dock = DockStyle.Fill;
1047                                 ln.Detached = true;
1048                             }));
1049                     }
1050                 }
1051             );
1052         }
1053
1054         private void tmnuTeleportHome_Click(object sender, EventArgs e)
1055         {
1056             if (WorldMap != null)
1057             {
1058                 WorldMap.GoHome();
1059             }
1060         }
1061
1062         private TimeZoneInfo SLTime;
1063
1064         private void GetSLTimeZone()
1065         {
1066             try
1067             {
1068                 foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones())
1069                 {
1070                     if (tz.Id == "Pacific Standard Time" || tz.Id == "America/Los_Angeles")
1071                     {
1072                         SLTime = tz;
1073                         break;
1074                     }
1075                 }
1076             }
1077             catch (Exception) { }
1078         }
1079
1080         private void timerWorldClock_Tick(object sender, EventArgs e)
1081         {
1082             DateTime now;
1083             try
1084             {
1085                 if (SLTime != null)
1086                     now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, SLTime);
1087                 else
1088                     now = DateTime.UtcNow.AddHours(-7);
1089             }
1090             catch (Exception)
1091             {
1092                 now = DateTime.UtcNow.AddHours(-7);
1093             }
1094             lblTime.Text = now.ToString("h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
1095         }
1096
1097         private void reportBugsToolStripMenuItem_Click(object sender, EventArgs e)
1098         {
1099             ProcessLink("http://jira.openmetaverse.org/browse/RAD");
1100         }
1101
1102         private void aboutRadegastToolStripMenuItem_Click(object sender, EventArgs e)
1103         {
1104             (new frmAbout(instance)).ShowDialog();
1105         }
1106
1107         #region Update Checking
1108         private UpdateChecker updateChecker = null;
1109         private bool ManualUpdateCheck = false;
1110
1111         public void StartUpdateCheck(bool userInitiated)
1112         {
1113             ManualUpdateCheck = userInitiated;
1114
1115             if (updateChecker != null)
1116             {
1117                 if (ManualUpdateCheck)
1118                     tabsConsole.DisplayNotificationInChat("Update check already in progress.");
1119                 return;
1120             }
1121
1122             if (ManualUpdateCheck)
1123                 tabsConsole.DisplayNotificationInChat("Checking for updates...", ChatBufferTextStyle.StatusBlue);
1124             updateChecker = new UpdateChecker();
1125             updateChecker.OnUpdateInfoReceived += new UpdateChecker.UpdateInfoCallback(OnUpdateInfoReceived);
1126             updateChecker.StartCheck();
1127         }
1128
1129         private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
1130         {
1131             tabsConsole.SelectTab("chat");
1132             StartUpdateCheck(true);
1133         }
1134
1135         void OnUpdateInfoReceived(object sender, UpdateCheckerArgs e)
1136         {
1137             if (InvokeRequired)
1138             {
1139                 BeginInvoke(new MethodInvoker(() => OnUpdateInfoReceived(sender, e)));
1140                 return;
1141             }
1142
1143             if (!e.Success)
1144             {
1145                 if (ManualUpdateCheck)
1146                     tabsConsole.DisplayNotificationInChat("Error: Failed connecting to the update site.", ChatBufferTextStyle.StatusBlue);
1147             }
1148             else
1149             {
1150                 if (!ManualUpdateCheck && e.Info.DisplayMOTD)
1151                 {
1152                     tabsConsole.DisplayNotificationInChat(e.Info.MOTD, ChatBufferTextStyle.StatusBlue);
1153                 }
1154
1155                 if (e.Info.UpdateAvailable)
1156                 {
1157                     tabsConsole.DisplayNotificationInChat("New version available at " + e.Info.DownloadSite, ChatBufferTextStyle.Alert);
1158                 }
1159                 else
1160                 {
1161                     if (ManualUpdateCheck)
1162                         tabsConsole.DisplayNotificationInChat("Your version is up to date.", ChatBufferTextStyle.StatusBlue);
1163                 }
1164             }
1165
1166             updateChecker.Dispose();
1167             updateChecker = null;
1168         }
1169         #endregion
1170
1171         private void ToggleHidden(string tabName)
1172         {
1173             if (!tabsConsole.TabExists(tabName)) return;
1174
1175             RadegastTab tab = tabsConsole.Tabs[tabName];
1176
1177             if (tab.Hidden)
1178             {
1179                 tab.Show();
1180             }
1181             else
1182             {
1183                 if (!tab.Selected)
1184                 {
1185                     tab.Select();
1186                 }
1187                 else
1188                 {
1189                     tab.Hide();
1190                 }
1191             }
1192         }
1193
1194         private void tbtnFriends_Click(object sender, EventArgs e)
1195         {
1196             ToggleHidden("friends");
1197         }
1198
1199         private void tbtnInventory_Click(object sender, EventArgs e)
1200         {
1201             ToggleHidden("inventory");
1202         }
1203
1204         private void tbtnSearch_Click(object sender, EventArgs e)
1205         {
1206             ToggleHidden("search");
1207         }
1208
1209         private void tbtnGroups_Click(object sender, EventArgs e)
1210         {
1211             ToggleHidden("groups");
1212         }
1213
1214         private void tbtnVoice_Click(object sender, EventArgs e)
1215         {
1216             ToggleHidden("voice");
1217         }
1218
1219         private void tbtnMedia_Click(object sender, EventArgs e)
1220         {
1221             if (tabsConsole.TabExists("media"))
1222             {
1223                 ToggleHidden("media");
1224             }
1225             else
1226             {
1227                 RadegastTab tab = tabsConsole.AddTab("media", "Media", mediaConsole);
1228                 tab.AllowClose = false;
1229                 tab.AllowHide = true;
1230                 tab.Select();
1231             }
1232         }
1233
1234         private void debugConsoleToolStripMenuItem_Click(object sender, EventArgs e)
1235         {
1236             if (tabsConsole.TabExists("debug"))
1237             {
1238                 ToggleHidden("debug");
1239             }
1240             else
1241             {
1242                 RadegastTab tab = tabsConsole.AddTab("debug", "Debug", new DebugConsole(instance));
1243                 tab.AllowClose = false;
1244                 tab.AllowHide = true;
1245                 tab.Select();
1246             }
1247         }
1248
1249         private void tbnObjects_Click(object sender, EventArgs e)
1250         {
1251             if (tabsConsole.TabExists("objects"))
1252             {
1253                 RadegastTab tab = tabsConsole.Tabs["objects"];
1254                 if (!tab.Selected)
1255                 {
1256                     tab.Select();
1257                     ((ObjectsConsole)tab.Control).RefreshObjectList();
1258                 }
1259                 else
1260                 {
1261                     tab.Close();
1262                 }
1263             }
1264             else
1265             {
1266                 RadegastTab tab = tabsConsole.AddTab("objects", "Objects", new ObjectsConsole(instance));
1267                 tab.AllowClose = true;
1268                 tab.AllowDetach = true;
1269                 tab.Visible = true;
1270                 tab.AllowHide = false;
1271                 tab.Select();
1272                 ((ObjectsConsole)tab.Control).RefreshObjectList();
1273             }
1274         }
1275
1276         private void tbtnMap_Click(object sender, EventArgs e)
1277         {
1278             ToggleHidden("map");
1279             if (!MapTab.Hidden)
1280                 MapToCurrentLocation();
1281         }
1282
1283         private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
1284         {
1285             if (client.Network.Connected)
1286                 client.Network.RequestLogout();
1287         }
1288
1289         private void reconnectToolStripMenuItem_Click(object sender, EventArgs e)
1290         {
1291             instance.Reconnect();
1292         }
1293
1294         private frmKeyboardShortcuts keyboardShortcutsForm = null;
1295         private void keyboardShortcutsToolStripMenuItem_Click(object sender, EventArgs e)
1296         {
1297             if (keyboardShortcutsForm != null)
1298             {
1299                 keyboardShortcutsForm.Focus();
1300             }
1301             else
1302             {
1303                 keyboardShortcutsForm = new frmKeyboardShortcuts(instance);
1304
1305                 keyboardShortcutsForm.Disposed += (object senderx, EventArgs ex) =>
1306                     {
1307                         if (components != null)
1308                         {
1309                             components.Remove(keyboardShortcutsForm);
1310                         }
1311                         keyboardShortcutsForm = null;
1312                     };
1313
1314                 keyboardShortcutsForm.Show(this);
1315                 keyboardShortcutsForm.Top = Top + 100;
1316                 keyboardShortcutsForm.Left = Left + 100;
1317
1318                 if (components != null)
1319                 {
1320                     components.Add(keyboardShortcutsForm);
1321                 }
1322             }
1323         }
1324
1325         // Menu item for testing out stuff
1326         private void testToolStripMenuItem_Click(object sender, EventArgs e)
1327         {
1328             foreach (KeyValuePair<UUID, string> kvp in Sounds.ToDictionary())
1329             {
1330                 client.Assets.RequestAsset(kvp.Key, AssetType.Sound, true, (AssetDownload transfer, Asset asset) =>
1331                     {
1332                         System.Console.WriteLine("Sound '{0}' download success: {1}", transfer.AssetID, transfer.Success);
1333                     }
1334                 );
1335             }
1336         }
1337
1338         private void reloadInventoryToolStripMenuItem_Click(object sender, EventArgs e)
1339         {
1340             if (tabsConsole.TabExists("inventory"))
1341             {
1342                 ((InventoryConsole)tabsConsole.Tabs["inventory"].Control).ReloadInventory();
1343                 tabsConsole.Tabs["inventory"].Select();
1344             }
1345         }
1346
1347         private void btnLoadScript_Click(object sender, EventArgs e)
1348         {
1349             if (!TabConsole.TabExists("plugin_manager"))
1350             {
1351                 TabConsole.AddTab("plugin_manager", "Plugins", new PluginsTab(instance));
1352             }
1353             TabConsole.Tabs["plugin_manager"].Select();
1354         }
1355
1356         private void frmMain_Resize(object sender, EventArgs e)
1357         {
1358             if (WindowState == FormWindowState.Minimized && instance.GlobalSettings["minimize_to_tray"].AsBoolean())
1359             {
1360                 ShowInTaskbar = false;
1361                 trayIcon.Visible = true;
1362             }
1363         }
1364
1365         private void treyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
1366         {
1367             WindowState = FormWindowState.Normal;
1368             ShowInTaskbar = true;
1369             trayIcon.Visible = false;
1370         }
1371
1372         private void ctxTreyRestore_Click(object sender, EventArgs e)
1373         {
1374             treyIcon_MouseDoubleClick(this, null);
1375         }
1376
1377         private void ctxTreyExit_Click(object sender, EventArgs e)
1378         {
1379             tmnuExit_Click(this, EventArgs.Empty);
1380         }
1381
1382         #endregion
1383     }
1384 }