OSDN Git Service

Updated year in copyright header.
[radegast/radegast.git] / Radegast / GUI / Dialogs / MainForm.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.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 tmnuStatusAway_Click(object sender, EventArgs e)
866         {
867             instance.State.SetAway(tmnuStatusAway.Checked);
868         }
869
870         private void tmnuHelpReadme_Click(object sender, EventArgs e)
871         {
872             System.Diagnostics.Process.Start(Application.StartupPath + @"\Readme.txt");
873         }
874
875         private void tmnuStatusBusy_Click(object sender, EventArgs e)
876         {
877             instance.State.SetBusy(tmnuStatusBusy.Checked);
878         }
879
880         private void tmnuControlFly_Click(object sender, EventArgs e)
881         {
882             instance.State.SetFlying(tmnuControlFly.Checked);
883         }
884
885         private void tmnuControlAlwaysRun_Click(object sender, EventArgs e)
886         {
887             instance.State.SetAlwaysRun(tmnuControlAlwaysRun.Checked);
888         }
889
890         private void tmnuPrefs_Click(object sender, EventArgs e)
891         {
892             (new frmSettings(instance)).ShowDialog();
893         }
894
895         private void tbtnAppearance_Click(object sender, EventArgs e)
896         {
897             client.Appearance.RequestSetAppearance(false);
898         }
899
900         private void importObjectToolStripMenuItem_Click(object sender, EventArgs e)
901         {
902             PrimDeserializer.ImportFromFile(client);
903         }
904
905         private void autopilotToolStripMenuItem_Click(object sender, EventArgs e)
906         {
907             if (ap == null)
908             {
909                 ap = new AutoPilot(client);
910                 /*
911                 ap.InsertWaypoint(new Vector3(66, 163, 21));
912                 ap.InsertWaypoint(new Vector3(66, 98, 21));
913
914                 ap.InsertWaypoint(new Vector3(101, 98, 21));
915                 ap.InsertWaypoint(new Vector3(101, 45, 21));
916                 ap.InsertWaypoint(new Vector3(93, 27, 21));
917                 ap.InsertWaypoint(new Vector3(106, 12, 21));
918                 ap.InsertWaypoint(new Vector3(123, 24, 21));
919                 ap.InsertWaypoint(new Vector3(114, 45, 21));
920                 ap.InsertWaypoint(new Vector3(114, 98, 21));
921
922                 ap.InsertWaypoint(new Vector3(130, 98, 21));
923                 ap.InsertWaypoint(new Vector3(130, 163, 21));
924                  **/
925                 ap.InsertWaypoint(new Vector3(64, 68, 21));
926                 ap.InsertWaypoint(new Vector3(65, 20, 21));
927                 ap.InsertWaypoint(new Vector3(33, 23, 21));
928                 ap.InsertWaypoint(new Vector3(17, 39, 21));
929                 ap.InsertWaypoint(new Vector3(17, 62, 21));
930
931
932             }
933             if (AutoPilotActive)
934             {
935                 AutoPilotActive = false;
936                 ap.Stop();
937             }
938             else
939             {
940                 AutoPilotActive = true;
941                 ap.Start();
942             }
943
944         }
945
946         private void cleanCacheToolStripMenuItem_Click(object sender, EventArgs e)
947         {
948             client.Assets.Cache.Clear();
949         }
950
951         private void rebakeTexturesToolStripMenuItem_Click(object sender, EventArgs e)
952         {
953             client.Appearance.RequestSetAppearance(true);
954         }
955
956         public void MapToCurrentLocation()
957         {
958             if (MapTab != null && client.Network.Connected)
959             {
960                 MapTab.Select();
961                 WorldMap.DisplayLocation(client.Network.CurrentSim.Name,
962                     (int)client.Self.SimPosition.X,
963                     (int)client.Self.SimPosition.Y,
964                     (int)client.Self.SimPosition.Z);
965             }
966         }
967
968         private void standToolStripMenuItem_Click(object sender, EventArgs e)
969         {
970             client.Self.Stand();
971         }
972
973         private void groundSitToolStripMenuItem_Click(object sender, EventArgs e)
974         {
975             client.Self.SitOnGround();
976         }
977
978         private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
979         {
980             try { System.Diagnostics.Process.Start(Application.ExecutablePath); }
981             catch (Exception) { }
982         }
983
984         private void tmnuExit_Click(object sender, EventArgs e)
985         {
986             Close();
987         }
988
989         private void tlblRegionInfo_Click(object sender, EventArgs e)
990         {
991             if (WorldMap != null && client.Network.Connected)
992             {
993                 MapTab.Select();
994             }
995         }
996
997         private void scriptEditorToolStripMenuItem_Click(object sender, EventArgs e)
998         {
999             ScriptEditor se = new ScriptEditor(instance);
1000             se.Dock = DockStyle.Fill;
1001             se.ShowDetached();
1002         }
1003
1004         private void tmnuSetHome_Click(object sender, EventArgs e)
1005         {
1006             client.Self.SetHome();
1007         }
1008
1009         private void tmnuCreateLandmark_Click(object sender, EventArgs e)
1010         {
1011             string location = string.Format(", {0} ({1}, {2}, {3})",
1012                 client.Network.CurrentSim.Name,
1013                 (int)client.Self.SimPosition.X,
1014                 (int)client.Self.SimPosition.Y,
1015                 (int)client.Self.SimPosition.Z
1016                 );
1017
1018             string name = tlblParcel.Text;
1019             int maxLen = 63 - location.Length;
1020
1021             if (name.Length > maxLen)
1022                 name = name.Substring(0, maxLen);
1023
1024             name += location;
1025
1026             client.Inventory.RequestCreateItem(
1027                 client.Inventory.FindFolderForType(AssetType.Landmark),
1028                 name,
1029                 tlblParcel.ToolTipText,
1030                 AssetType.Landmark,
1031                 UUID.Random(),
1032                 InventoryType.Landmark,
1033                 PermissionMask.All,
1034                 (bool success, InventoryItem item) =>
1035                 {
1036                     if (success)
1037                     {
1038                         BeginInvoke(new MethodInvoker(() =>
1039                             {
1040                                 Landmark ln = new Landmark(instance, (InventoryLandmark)item);
1041                                 ln.Dock = DockStyle.Fill;
1042                                 ln.Detached = true;
1043                             }));
1044                     }
1045                 }
1046             );
1047         }
1048
1049         private TimeZoneInfo SLTime;
1050
1051         private void GetSLTimeZone()
1052         {
1053             try
1054             {
1055                 foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones())
1056                 {
1057                     if (tz.Id == "Pacific Standard Time" || tz.Id == "America/Los_Angeles")
1058                     {
1059                         SLTime = tz;
1060                         break;
1061                     }
1062                 }
1063             }
1064             catch (Exception) { }
1065         }
1066
1067         private void timerWorldClock_Tick(object sender, EventArgs e)
1068         {
1069             DateTime now;
1070             try
1071             {
1072                 if (SLTime != null)
1073                     now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, SLTime);
1074                 else
1075                     now = DateTime.UtcNow.AddHours(-7);
1076             }
1077             catch (Exception)
1078             {
1079                 now = DateTime.UtcNow.AddHours(-7);
1080             }
1081             lblTime.Text = now.ToString("h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
1082         }
1083
1084         private void reportBugsToolStripMenuItem_Click(object sender, EventArgs e)
1085         {
1086             ProcessLink("http://jira.openmetaverse.org/browse/RAD");
1087         }
1088
1089         private void aboutRadegastToolStripMenuItem_Click(object sender, EventArgs e)
1090         {
1091             (new frmAbout(instance)).ShowDialog();
1092         }
1093
1094         #region Update Checking
1095         private UpdateChecker updateChecker = null;
1096         private bool ManualUpdateCheck = false;
1097
1098         public void StartUpdateCheck(bool userInitiated)
1099         {
1100             ManualUpdateCheck = userInitiated;
1101
1102             if (updateChecker != null)
1103             {
1104                 if (ManualUpdateCheck)
1105                     tabsConsole.DisplayNotificationInChat("Update check already in progress.");
1106                 return;
1107             }
1108
1109             if (ManualUpdateCheck)
1110                 tabsConsole.DisplayNotificationInChat("Checking for updates...", ChatBufferTextStyle.StatusBlue);
1111             updateChecker = new UpdateChecker();
1112             updateChecker.OnUpdateInfoReceived += new UpdateChecker.UpdateInfoCallback(OnUpdateInfoReceived);
1113             updateChecker.StartCheck();
1114         }
1115
1116         private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
1117         {
1118             tabsConsole.SelectTab("chat");
1119             StartUpdateCheck(true);
1120         }
1121
1122         void OnUpdateInfoReceived(object sender, UpdateCheckerArgs e)
1123         {
1124             if (InvokeRequired)
1125             {
1126                 BeginInvoke(new MethodInvoker(() => OnUpdateInfoReceived(sender, e)));
1127                 return;
1128             }
1129
1130             if (!e.Success)
1131             {
1132                 if (ManualUpdateCheck)
1133                     tabsConsole.DisplayNotificationInChat("Error: Failed connecting to the update site.", ChatBufferTextStyle.StatusBlue);
1134             }
1135             else
1136             {
1137                 if (!ManualUpdateCheck && e.Info.DisplayMOTD)
1138                 {
1139                     tabsConsole.DisplayNotificationInChat(e.Info.MOTD, ChatBufferTextStyle.StatusBlue);
1140                 }
1141
1142                 if (e.Info.UpdateAvailable)
1143                 {
1144                     tabsConsole.DisplayNotificationInChat("New version available at " + e.Info.DownloadSite, ChatBufferTextStyle.Alert);
1145                 }
1146                 else
1147                 {
1148                     if (ManualUpdateCheck)
1149                         tabsConsole.DisplayNotificationInChat("Your version is up to date.", ChatBufferTextStyle.StatusBlue);
1150                 }
1151             }
1152
1153             updateChecker.Dispose();
1154             updateChecker = null;
1155         }
1156         #endregion
1157
1158         private void ToggleHidden(string tabName)
1159         {
1160             if (!tabsConsole.TabExists(tabName)) return;
1161
1162             RadegastTab tab = tabsConsole.Tabs[tabName];
1163
1164             if (tab.Hidden)
1165             {
1166                 tab.Show();
1167             }
1168             else
1169             {
1170                 if (!tab.Selected)
1171                 {
1172                     tab.Select();
1173                 }
1174                 else
1175                 {
1176                     tab.Hide();
1177                 }
1178             }
1179         }
1180
1181         private void tbtnFriends_Click(object sender, EventArgs e)
1182         {
1183             ToggleHidden("friends");
1184         }
1185
1186         private void tbtnInventory_Click(object sender, EventArgs e)
1187         {
1188             ToggleHidden("inventory");
1189         }
1190
1191         private void tbtnSearch_Click(object sender, EventArgs e)
1192         {
1193             ToggleHidden("search");
1194         }
1195
1196         private void tbtnGroups_Click(object sender, EventArgs e)
1197         {
1198             ToggleHidden("groups");
1199         }
1200
1201         private void tbtnVoice_Click(object sender, EventArgs e)
1202         {
1203             ToggleHidden("voice");
1204         }
1205
1206         private void tbtnMedia_Click(object sender, EventArgs e)
1207         {
1208             if (tabsConsole.TabExists("media"))
1209             {
1210                 ToggleHidden("media");
1211             }
1212             else
1213             {
1214                 RadegastTab tab = tabsConsole.AddTab("media", "Media", mediaConsole);
1215                 tab.AllowClose = false;
1216                 tab.AllowHide = true;
1217                 tab.Select();
1218             }
1219         }
1220
1221         private void debugConsoleToolStripMenuItem_Click(object sender, EventArgs e)
1222         {
1223             if (tabsConsole.TabExists("debug"))
1224             {
1225                 ToggleHidden("debug");
1226             }
1227             else
1228             {
1229                 RadegastTab tab = tabsConsole.AddTab("debug", "Debug", new DebugConsole(instance));
1230                 tab.AllowClose = false;
1231                 tab.AllowHide = true;
1232                 tab.Select();
1233             }
1234         }
1235
1236         private void tbnObjects_Click(object sender, EventArgs e)
1237         {
1238             if (tabsConsole.TabExists("objects"))
1239             {
1240                 RadegastTab tab = tabsConsole.Tabs["objects"];
1241                 if (!tab.Selected)
1242                 {
1243                     tab.Select();
1244                     ((ObjectsConsole)tab.Control).RefreshObjectList();
1245                 }
1246                 else
1247                 {
1248                     tab.Close();
1249                 }
1250             }
1251             else
1252             {
1253                 RadegastTab tab = tabsConsole.AddTab("objects", "Objects", new ObjectsConsole(instance));
1254                 tab.AllowClose = true;
1255                 tab.AllowDetach = true;
1256                 tab.Visible = true;
1257                 tab.AllowHide = false;
1258                 tab.Select();
1259                 ((ObjectsConsole)tab.Control).RefreshObjectList();
1260             }
1261         }
1262
1263         private void tbtnMap_Click(object sender, EventArgs e)
1264         {
1265             ToggleHidden("map");
1266             if (!MapTab.Hidden)
1267                 MapToCurrentLocation();
1268         }
1269
1270         private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
1271         {
1272             if (client.Network.Connected)
1273                 client.Network.RequestLogout();
1274         }
1275
1276         private void reconnectToolStripMenuItem_Click(object sender, EventArgs e)
1277         {
1278             instance.Reconnect();
1279         }
1280
1281         private frmKeyboardShortcuts keyboardShortcutsForm = null;
1282         private void keyboardShortcutsToolStripMenuItem_Click(object sender, EventArgs e)
1283         {
1284             if (keyboardShortcutsForm != null)
1285             {
1286                 keyboardShortcutsForm.Focus();
1287             }
1288             else
1289             {
1290                 keyboardShortcutsForm = new frmKeyboardShortcuts(instance);
1291
1292                 keyboardShortcutsForm.Disposed += (object senderx, EventArgs ex) =>
1293                     {
1294                         if (components != null)
1295                         {
1296                             components.Remove(keyboardShortcutsForm);
1297                         }
1298                         keyboardShortcutsForm = null;
1299                     };
1300
1301                 keyboardShortcutsForm.Show(this);
1302                 keyboardShortcutsForm.Top = Top + 100;
1303                 keyboardShortcutsForm.Left = Left + 100;
1304
1305                 if (components != null)
1306                 {
1307                     components.Add(keyboardShortcutsForm);
1308                 }
1309             }
1310         }
1311
1312         // Menu item for testing out stuff
1313         private void testToolStripMenuItem_Click(object sender, EventArgs e)
1314         {
1315             client.Settings.SEND_AGENT_THROTTLE = true;
1316             client.Settings.SEND_AGENT_UPDATES = true;
1317         }
1318
1319         private void reloadInventoryToolStripMenuItem_Click(object sender, EventArgs e)
1320         {
1321             if (tabsConsole.TabExists("inventory"))
1322             {
1323                 ((InventoryConsole)tabsConsole.Tabs["inventory"].Control).ReloadInventory();
1324                 tabsConsole.Tabs["inventory"].Select();
1325             }
1326         }
1327
1328         private void btnLoadScript_Click(object sender, EventArgs e)
1329         {
1330             if (!TabConsole.TabExists("plugin_manager"))
1331             {
1332                 TabConsole.AddTab("plugin_manager", "Plugins", new PluginsTab(instance));
1333             }
1334             TabConsole.Tabs["plugin_manager"].Select();
1335         }
1336
1337         private void frmMain_Resize(object sender, EventArgs e)
1338         {
1339             if (WindowState == FormWindowState.Minimized && instance.GlobalSettings["minimize_to_tray"].AsBoolean())
1340             {
1341                 ShowInTaskbar = false;
1342                 trayIcon.Visible = true;
1343             }
1344         }
1345
1346         private void treyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
1347         {
1348             WindowState = FormWindowState.Normal;
1349             ShowInTaskbar = true;
1350             trayIcon.Visible = false;
1351         }
1352
1353         private void ctxTreyRestore_Click(object sender, EventArgs e)
1354         {
1355             treyIcon_MouseDoubleClick(this, null);
1356         }
1357
1358         private void ctxTreyExit_Click(object sender, EventArgs e)
1359         {
1360             tmnuExit_Click(this, EventArgs.Empty);
1361         }
1362
1363         #endregion
1364     }
1365 }