OSDN Git Service

Don't cause exception when destroying tabs on shutdown.
[radegast/radegast.git] / Radegast / GUI / Consoles / TabsConsole.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2011, 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.ComponentModel;
34 using System.Drawing;
35 using System.Windows.Forms;
36 using Radegast.Netcom;
37 using OpenMetaverse;
38
39 namespace Radegast
40 {
41     public partial class TabsConsole : UserControl
42     {
43
44         /// <summary>
45         /// List of nearby avatars (radar data)
46         /// </summary>
47         public List<NearbyAvatar> NearbyAvatars
48         {
49             get
50             {
51                 List<NearbyAvatar> res = new List<NearbyAvatar>();
52
53                 if (TabExists("chat"))
54                 {
55                     ChatConsole c = (ChatConsole)Tabs["chat"].Control;
56                     lock (c.agentSimHandle)
57                     {
58                         foreach (ListViewItem item in c.lvwObjects.Items)
59                         {
60                             if (item.Name != client.Self.AgentID.ToString())
61                             {
62                                 res.Add(new NearbyAvatar() { ID = new UUID(item.Name), Name = item.Text });
63                             }
64                         }
65                     }
66                 }
67
68                 return res;
69             }
70         }
71
72         /// <summary>
73         /// Delegate invoked on tab operations
74         /// </summary>
75         /// <param name="sender">Event sender</param>
76         /// <param name="e">Event arguments</param>
77         public delegate void TabCallback(object sender, TabEventArgs e);
78
79         /// <summary>
80         /// Fired when a tab is selected
81         /// </summary>
82         public event TabCallback OnTabSelected;
83
84
85         /// <summary>
86         /// Delegate invoked when chat notification is printed
87         /// </summary>
88         /// <param name="sender">Event sender</param>
89         /// <param name="e">Event arguments</param>
90         public delegate void ChatNotificationCallback(object sender, ChatNotificationEventArgs e);
91
92         /// <summary>
93         /// Fired when a tab is selected
94         /// </summary>
95         public event ChatNotificationCallback OnChatNotification;
96
97         /// <summary>
98         /// Fired when a new tab is added
99         /// </summary>
100         public event TabCallback OnTabAdded;
101
102         /// <summary>
103         /// Fired when a tab is removed
104         /// </summary>
105         public event TabCallback OnTabRemoved;
106
107         private RadegastInstance instance;
108         private GridClient client { get { return instance.Client; } }
109         private RadegastNetcom netcom { get { return instance.Netcom; } }
110         private ChatTextManager mainChatManger;
111         public ChatTextManager MainChatManger { get { return mainChatManger; } }
112
113         private Dictionary<string, RadegastTab> tabs = new Dictionary<string, RadegastTab>();
114         public Dictionary<string, RadegastTab> Tabs { get { return tabs; } }
115
116         private ChatConsole chatConsole;
117
118         private RadegastTab selectedTab;
119
120         /// <summary>
121         /// Currently selected tab
122         /// </summary>
123         public RadegastTab SelectedTab
124         {
125             get
126             {
127                 return selectedTab;
128             }
129         }
130
131         private Form owner;
132
133         public TabsConsole(RadegastInstance instance)
134         {
135             InitializeComponent();
136             Disposed += new EventHandler(TabsConsole_Disposed);
137
138             this.instance = instance;
139             this.instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
140
141             AddNetcomEvents();
142
143             InitializeMainTab();
144             InitializeChatTab();
145
146             // Callbacks
147             RegisterClientEvents(client);
148         }
149
150         private void RegisterClientEvents(GridClient client)
151         {
152             client.Self.ScriptQuestion += new EventHandler<ScriptQuestionEventArgs>(Self_ScriptQuestion);
153             client.Self.ScriptDialog += new EventHandler<ScriptDialogEventArgs>(Self_ScriptDialog);
154             client.Self.LoadURL += new EventHandler<LoadUrlEventArgs>(Self_LoadURL);
155             client.Self.SetDisplayNameReply += new EventHandler<SetDisplayNameReplyEventArgs>(Self_SetDisplayNameReply);
156             client.Avatars.DisplayNameUpdate += new EventHandler<DisplayNameUpdateEventArgs>(Avatars_DisplayNameUpdate);
157             client.Network.EventQueueRunning += new EventHandler<EventQueueRunningEventArgs>(Network_EventQueueRunning);
158             client.Network.RegisterCallback(OpenMetaverse.Packets.PacketType.ScriptTeleportRequest, ScriptTeleportRequestHandler);
159         }
160
161         private void UnregisterClientEvents(GridClient client)
162         {
163             client.Self.ScriptQuestion -= new EventHandler<ScriptQuestionEventArgs>(Self_ScriptQuestion);
164             client.Self.ScriptDialog -= new EventHandler<ScriptDialogEventArgs>(Self_ScriptDialog);
165             client.Self.LoadURL -= new EventHandler<LoadUrlEventArgs>(Self_LoadURL);
166             client.Self.SetDisplayNameReply -= new EventHandler<SetDisplayNameReplyEventArgs>(Self_SetDisplayNameReply);
167             client.Avatars.DisplayNameUpdate -= new EventHandler<DisplayNameUpdateEventArgs>(Avatars_DisplayNameUpdate);
168             client.Network.EventQueueRunning -= new EventHandler<EventQueueRunningEventArgs>(Network_EventQueueRunning);
169             client.Network.UnregisterCallback(OpenMetaverse.Packets.PacketType.ScriptTeleportRequest, ScriptTeleportRequestHandler);
170         }
171
172         void instance_ClientChanged(object sender, ClientChangedEventArgs e)
173         {
174             UnregisterClientEvents(e.OldClient);
175             RegisterClientEvents(client);
176         }
177
178         void TabsConsole_Disposed(object sender, EventArgs e)
179         {
180             RemoveNetcomEvents();
181             UnregisterClientEvents(client);
182         }
183
184         private void AddNetcomEvents()
185         {
186             netcom.ClientLoginStatus += new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
187             netcom.ClientLoggedOut += new EventHandler(netcom_ClientLoggedOut);
188             netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
189             netcom.ChatSent += new EventHandler<ChatSentEventArgs>(netcom_ChatSent);
190             netcom.AlertMessageReceived += new EventHandler<AlertMessageEventArgs>(netcom_AlertMessageReceived);
191             netcom.InstantMessageReceived += new EventHandler<InstantMessageEventArgs>(netcom_InstantMessageReceived);
192         }
193
194         private void RemoveNetcomEvents()
195         {
196             netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
197             netcom.ClientLoggedOut -= new EventHandler(netcom_ClientLoggedOut);
198             netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
199             netcom.ChatSent -= new EventHandler<ChatSentEventArgs>(netcom_ChatSent);
200             netcom.AlertMessageReceived -= new EventHandler<AlertMessageEventArgs>(netcom_AlertMessageReceived);
201             netcom.InstantMessageReceived -= new EventHandler<InstantMessageEventArgs>(netcom_InstantMessageReceived);
202         }
203
204         void ScriptTeleportRequestHandler(object sender, PacketReceivedEventArgs e)
205         {
206             if (InvokeRequired)
207             {
208                 if (IsHandleCreated || !instance.MonoRuntime)
209                     BeginInvoke(new MethodInvoker(() => ScriptTeleportRequestHandler(sender, e)));
210                 return;
211             }
212
213             var msg = (OpenMetaverse.Packets.ScriptTeleportRequestPacket)e.Packet;
214
215             if (TabExists("map"))
216             {
217                 Tabs["map"].Select();
218                 ((MapConsole)Tabs["map"].Control).CenterOnGlobalPos(
219                     (float)(client.Self.GlobalPosition.X - client.Self.SimPosition.X) + msg.Data.SimPosition.X,
220                     (float)(client.Self.GlobalPosition.Y - client.Self.SimPosition.Y) + msg.Data.SimPosition.Y,
221                     msg.Data.SimPosition.Z);
222             }
223         }
224
225         void Network_EventQueueRunning(object sender, EventQueueRunningEventArgs e)
226         {
227             if (InvokeRequired)
228             {
229                 BeginInvoke(new MethodInvoker(() => Network_EventQueueRunning(sender, e)));
230                 return;
231             }
232
233             if (TabExists("friends")) return;
234             if (e.Simulator == client.Network.CurrentSim)
235             {
236                 client.Self.UpdateAgentLanguage("en", true);
237                 InitializeOnlineTabs();
238             }
239         }
240
241         void Self_ScriptDialog(object sender, ScriptDialogEventArgs e)
242         {
243             // Is this object muted
244             if (null != client.Self.MuteList.Find(m => (m.Type == MuteType.Object && m.ID == e.ObjectID) // muted object by id
245                 || (m.Type == MuteType.ByName && m.Name == e.ObjectName) // object muted by name
246                 )) return;
247
248             instance.MainForm.AddNotification(new ntfScriptDialog(instance, e.Message, e.ObjectName, e.ImageID, e.ObjectID, e.FirstName, e.LastName, e.Channel, e.ButtonLabels));
249         }
250
251         void Self_ScriptQuestion(object sender, ScriptQuestionEventArgs e)
252         {
253             // Is this object muted
254             if (null != client.Self.MuteList.Find(m => (m.Type == MuteType.Object && m.ID == e.TaskID) // muted object by id
255                 || (m.Type == MuteType.ByName && m.Name == e.ObjectName) // object muted by name
256                 )) return;
257
258             instance.MainForm.AddNotification(new ntfPermissions(instance, e.Simulator, e.TaskID, e.ItemID, e.ObjectName, e.ObjectOwnerName, e.Questions));
259         }
260
261         private void netcom_ClientLoginStatus(object sender, LoginProgressEventArgs e)
262         {
263             if (e.Status == LoginStatus.Failed)
264             {
265                 DisplayNotificationInChat("Login error: " + e.Message, ChatBufferTextStyle.Error);
266             }
267             else if (e.Status == LoginStatus.Success)
268             {
269                 DisplayNotificationInChat("Logged in as " + netcom.LoginOptions.FullName + ".", ChatBufferTextStyle.StatusDarkBlue);
270                 DisplayNotificationInChat("Login reply: " + e.Message, ChatBufferTextStyle.StatusDarkBlue);
271
272                 if (tabs.ContainsKey("login"))
273                 {
274                     if (selectedTab.Name == "login")
275                         SelectDefaultTab();
276                     ForceCloseTab("login");
277                 }
278
279                 client.Self.RetrieveInstantMessages();
280             }
281         }
282
283         private void netcom_ClientLoggedOut(object sender, EventArgs e)
284         {
285             DisposeOnlineTabs();
286
287             SelectDefaultTab();
288             DisplayNotificationInChat("Logged out.");
289
290         }
291
292         private void netcom_ClientDisconnected(object sender, DisconnectedEventArgs e)
293         {
294             if (e.Reason == NetworkManager.DisconnectType.ClientInitiated) return;
295
296             DisposeOnlineTabs();
297             SelectDefaultTab();
298             DisplayNotificationInChat("Disconnected: " + e.Message, ChatBufferTextStyle.Error);
299         }
300
301         void Avatars_DisplayNameUpdate(object sender, DisplayNameUpdateEventArgs e)
302         {
303             DisplayNotificationInChat(string.Format("({0}) is now known as {1}", e.DisplayName.UserName, e.DisplayName.DisplayName));
304         }
305
306         void Self_SetDisplayNameReply(object sender, SetDisplayNameReplyEventArgs e)
307         {
308             if (e.Status == 200)
309             {
310                 DisplayNotificationInChat("You are now knows as " + e.DisplayName.DisplayName);
311             }
312             else
313             {
314                 DisplayNotificationInChat("Failed to set a new display name: " + e.Reason, ChatBufferTextStyle.Error);
315             }
316         }
317
318         private void netcom_AlertMessageReceived(object sender, AlertMessageEventArgs e)
319         {
320             tabs["chat"].Highlight();
321         }
322
323         private void netcom_ChatSent(object sender, ChatSentEventArgs e)
324         {
325             tabs["chat"].Highlight();
326         }
327
328         void Self_LoadURL(object sender, LoadUrlEventArgs e)
329         {
330             // Is the object or the owner muted?
331             if (null != client.Self.MuteList.Find(m => (m.Type == MuteType.Object && m.ID == e.ObjectID) // muted object by id 
332                 || (m.Type == MuteType.ByName && m.Name == e.ObjectName) // object muted by name
333                 || (m.Type == MuteType.Resident && m.ID == e.OwnerID) // object's owner muted
334                 )) return;
335
336             instance.MainForm.AddNotification(new ntfLoadURL(instance, e));
337         }
338
339         private void netcom_InstantMessageReceived(object sender, InstantMessageEventArgs e)
340         {
341             // Messaage from someone we muted?
342             if (null != client.Self.MuteList.Find(me => me.Type == MuteType.Resident && me.ID == e.IM.FromAgentID)) return;
343
344             switch (e.IM.Dialog)
345             {
346                 case InstantMessageDialog.SessionSend:
347                     if (instance.Groups.ContainsKey(e.IM.IMSessionID))
348                     {
349                         HandleGroupIM(e);
350                     }
351                     else
352                     {
353                         HandleConferenceIM(e);
354                     }
355                     break;
356
357                 case InstantMessageDialog.MessageFromAgent:
358                     if (e.IM.FromAgentName == "Second Life")
359                     {
360                         HandleIMFromObject(e);
361                     }
362                     else if (e.IM.GroupIM || instance.Groups.ContainsKey(e.IM.IMSessionID))
363                     {
364                         HandleGroupIM(e);
365                     }
366                     else if (e.IM.BinaryBucket.Length > 1)
367                     { // conference
368                         HandleConferenceIM(e);
369                     }
370                     else
371                     {
372                         HandleIM(e);
373                     }
374                     break;
375
376                 case InstantMessageDialog.MessageFromObject:
377                     HandleIMFromObject(e);
378                     break;
379
380                 case InstantMessageDialog.StartTyping:
381                     if (TabExists(e.IM.FromAgentName))
382                     {
383                         RadegastTab tab = tabs[e.IM.FromAgentName.ToLower()];
384                         if (!tab.Highlighted) tab.PartialHighlight();
385                     }
386
387                     break;
388
389                 case InstantMessageDialog.StopTyping:
390                     if (TabExists(e.IM.FromAgentName))
391                     {
392                         RadegastTab tab = tabs[e.IM.FromAgentName.ToLower()];
393                         if (!tab.Highlighted) tab.Unhighlight();
394                     }
395
396                     break;
397
398                 case InstantMessageDialog.MessageBox:
399                     instance.MainForm.AddNotification(new ntfGeneric(instance, e.IM.Message));
400                     break;
401
402                 case InstantMessageDialog.RequestTeleport:
403                     if (instance.RLV.AutoAcceptTP(e.IM.FromAgentID))
404                     {
405                         DisplayNotificationInChat("Auto accepting teleprot from " + e.IM.FromAgentName);
406                         instance.Client.Self.TeleportLureRespond(e.IM.FromAgentID, e.IM.IMSessionID, true);
407                     }
408                     else
409                     {
410                         instance.MainForm.AddNotification(new ntfTeleport(instance, e.IM));
411                     }
412                     break;
413
414                 case InstantMessageDialog.GroupInvitation:
415                     instance.MainForm.AddNotification(new ntfGroupInvitation(instance, e.IM));
416                     break;
417
418                 case InstantMessageDialog.FriendshipOffered:
419                     if (e.IM.FromAgentName == "Second Life")
420                     {
421                         HandleIMFromObject(e);
422                     }
423                     else
424                     {
425                         instance.MainForm.AddNotification(new ntfFriendshipOffer(instance, e.IM));
426                     }
427                     break;
428
429                 case InstantMessageDialog.InventoryAccepted:
430                     DisplayNotificationInChat(e.IM.FromAgentName + " accepted your inventory offer.");
431                     break;
432
433                 case InstantMessageDialog.InventoryDeclined:
434                     DisplayNotificationInChat(e.IM.FromAgentName + " declined your inventory offer.");
435                     break;
436
437                 case InstantMessageDialog.GroupNotice:
438                     // Is this group muted?
439                     if (null != client.Self.MuteList.Find(me => me.Type == MuteType.Group && me.ID == e.IM.FromAgentID)) break;
440
441                     instance.MainForm.AddNotification(new ntfGroupNotice(instance, e.IM));
442                     break;
443
444                 case InstantMessageDialog.InventoryOffered:
445                     instance.MainForm.AddNotification(new ntfInventoryOffer(instance, e.IM));
446                     break;
447
448                 case InstantMessageDialog.TaskInventoryOffered:
449                     // Is the object muted by name?
450                     if (null != client.Self.MuteList.Find(me => me.Type == MuteType.ByName && me.Name == e.IM.FromAgentName)) break;
451
452                     instance.MainForm.AddNotification(new ntfInventoryOffer(instance, e.IM));
453                     break;
454             }
455         }
456
457         /// <summary>
458         /// Make default tab (chat) active
459         /// </summary>
460         public void SelectDefaultTab()
461         {
462             if (IsHandleCreated && TabExists("chat"))
463                 tabs["chat"].Select();
464         }
465
466         /// <summary>
467         /// Displays notification in the main chat tab
468         /// </summary>
469         /// <param name="msg">Message to be printed in the chat tab</param>
470         public void DisplayNotificationInChat(string msg)
471         {
472             DisplayNotificationInChat(msg, ChatBufferTextStyle.ObjectChat);
473         }
474
475         /// <summary>
476         /// Displays notification in the main chat tab
477         /// </summary>
478         /// <param name="msg">Message to be printed in the chat tab</param>
479         /// <param name="style">Style of the message to be printed, normal, object, etc.</param>
480         public void DisplayNotificationInChat(string msg, ChatBufferTextStyle style)
481         {
482             DisplayNotificationInChat(msg, style, true);
483         }
484
485         /// <summary>
486         /// Displays notification in the main chat tab
487         /// </summary>
488         /// <param name="msg">Message to be printed in the chat tab</param>
489         /// <param name="style">Style of the message to be printed, normal, object, etc.</param>
490         /// <param name="highlightChatTab">Highligt (and flash in taskbar) chat tab if not selected</param>
491         public void DisplayNotificationInChat(string msg, ChatBufferTextStyle style, bool highlightChatTab)
492         {
493             if (InvokeRequired)
494             {
495                 if (!instance.MonoRuntime || IsHandleCreated)
496                     BeginInvoke(new MethodInvoker(() => DisplayNotificationInChat(msg, style, highlightChatTab)));
497                 return;
498             }
499
500             if (style != ChatBufferTextStyle.Invisible)
501             {
502                 ChatBufferItem line = new ChatBufferItem(
503                     DateTime.Now,
504                     string.Empty,
505                     UUID.Zero,
506                     msg,
507                     style
508                 );
509
510                 try
511                 {
512                     mainChatManger.ProcessBufferItem(line, true);
513                     if (highlightChatTab)
514                     {
515                         tabs["chat"].Highlight();
516                     }
517                 }
518                 catch (Exception) { }
519             }
520
521             if (OnChatNotification != null)
522             {
523                 try { OnChatNotification(this, new ChatNotificationEventArgs(msg, style)); }
524                 catch { }
525             }
526         }
527
528         private void HandleIMFromObject(InstantMessageEventArgs e)
529         {
530             // Is the object or the owner muted?
531             if (null != client.Self.MuteList.Find(m => (m.Type == MuteType.Object && m.ID == e.IM.IMSessionID) // muted object by id 
532                 || (m.Type == MuteType.ByName && m.Name == e.IM.FromAgentName) // object muted by name
533                 || (m.Type == MuteType.Resident && m.ID == e.IM.FromAgentID) // object's owner muted
534                 )) return;
535
536             DisplayNotificationInChat(e.IM.FromAgentName + ": " + e.IM.Message);
537         }
538
539         private void HandleIM(InstantMessageEventArgs e)
540         {
541             if (TabExists(e.IM.IMSessionID.ToString()))
542             {
543                 RadegastTab tab = tabs[e.IM.IMSessionID.ToString()];
544                 if (!tab.Selected) tab.Highlight();
545                 return;
546             }
547
548             instance.MediaManager.PlayUISound(UISounds.IM);
549
550             IMTabWindow imTab = AddIMTab(e);
551             tabs[e.IM.IMSessionID.ToString()].Highlight();
552         }
553
554         private void HandleConferenceIM(InstantMessageEventArgs e)
555         {
556             if (TabExists(e.IM.IMSessionID.ToString()))
557             {
558                 RadegastTab tab = tabs[e.IM.IMSessionID.ToString()];
559                 if (!tab.Selected) tab.Highlight();
560                 return;
561             }
562
563             instance.MediaManager.PlayUISound(UISounds.IM);
564
565             ConferenceIMTabWindow imTab = AddConferenceIMTab(e);
566             tabs[e.IM.IMSessionID.ToString()].Highlight();
567         }
568
569         private void HandleGroupIM(InstantMessageEventArgs e)
570         {
571             // Ignore group IM from a muted group
572             if (null != client.Self.MuteList.Find(me => me.Type == MuteType.Group && (me.ID == e.IM.IMSessionID || me.ID == e.IM.FromAgentID))) return;
573
574             if (TabExists(e.IM.IMSessionID.ToString()))
575             {
576                 RadegastTab tab = tabs[e.IM.IMSessionID.ToString()];
577                 if (!tab.Selected) tab.Highlight();
578                 return;
579             }
580
581             instance.MediaManager.PlayUISound(UISounds.IM);
582
583             GroupIMTabWindow imTab = AddGroupIMTab(e);
584             tabs[e.IM.IMSessionID.ToString()].Highlight();
585         }
586
587         private void InitializeMainTab()
588         {
589             LoginConsole loginConsole = new LoginConsole(instance);
590
591             RadegastTab tab = AddTab("login", "Login", loginConsole);
592             tab.AllowClose = false;
593             tab.AllowDetach = false;
594             tab.AllowMerge = false;
595             tab.AllowHide = false;
596
597             loginConsole.RegisterTab(tab);
598         }
599
600         private void InitializeChatTab()
601         {
602             chatConsole = new ChatConsole(instance);
603             mainChatManger = chatConsole.ChatManager;
604
605             RadegastTab tab = AddTab("chat", "Chat", chatConsole);
606             tab.AllowClose = false;
607             tab.AllowDetach = false;
608             tab.AllowHide = false;
609         }
610
611
612         /// <summary>
613         /// Create Tabs that only make sense when connected
614         /// </summary>
615         private void InitializeOnlineTabs()
616         {
617             RadegastTab tab = AddTab("friends", "Friends", new FriendsConsole(instance));
618             tab.AllowClose = false;
619             tab.AllowDetach = true;
620             tab.Visible = false;
621
622             tab = AddTab("groups", "Groups", new GroupsConsole(instance));
623             tab.AllowClose = false;
624             tab.AllowDetach = true;
625             tab.Visible = false;
626
627             // Ugly workaround for a mono bug
628             InventoryConsole inv = new InventoryConsole(instance);
629             if (instance.MonoRuntime) inv.invTree.Scrollable = false;
630             tab = AddTab("inventory", "Inventory", inv);
631             if (instance.MonoRuntime) inv.invTree.Scrollable = true;
632             tab.AllowClose = false;
633             tab.AllowDetach = true;
634             tab.Visible = false;
635
636             tab = AddTab("search", "Search", new SearchConsole(instance));
637             tab.AllowClose = false;
638             tab.AllowDetach = true;
639             tab.Visible = false;
640
641             tab = AddTab("map", "Map", new MapConsole(instance));
642             tab.AllowClose = false;
643             tab.AllowDetach = true;
644             tab.Visible = false;
645
646             tab = AddTab("voice", "Voice", new VoiceConsole(instance));
647             tab.AllowClose = false;
648             tab.AllowDetach = true;
649             tab.Visible = false;
650         }
651
652         /// <summary>
653         /// Close tabs that make no sense when not connected
654         /// </summary>
655         private void DisposeOnlineTabs()
656         {
657             lock (tabs)
658             {
659                 ForceCloseTab("voice");
660                 ForceCloseTab("map");
661                 ForceCloseTab("search");
662                 ForceCloseTab("inventory");
663                 ForceCloseTab("groups");
664                 ForceCloseTab("friends");
665             }
666         }
667
668         private void ForceCloseTab(string name)
669         {
670             if (!TabExists(name)) return;
671
672             RadegastTab tab = tabs[name];
673             if (tab.Merged) SplitTab(tab);
674
675             tab.AllowClose = true;
676             tab.Close();
677             tab = null;
678         }
679
680
681         public void RegisterContextAction(Type libomvType, String label, EventHandler handler)
682         {
683             instance.ContextActionManager.RegisterContextAction(libomvType, label, handler);
684         }
685
686         public void RegisterContextAction(ContextAction handler)
687         {
688             instance.ContextActionManager.RegisterContextAction(handler);
689         }
690
691         public void AddTab(RadegastTab tab)
692         {
693             ToolStripButton button = (ToolStripButton)tstTabs.Items.Add(tab.Label);
694             button.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
695             button.Image = null;
696             button.AutoToolTip = false;
697             button.Tag = tab.Name;
698             button.Click += new EventHandler(TabButtonClick);
699             tab.Button = button;
700             tabs.Add(tab.Name, tab);
701
702             if (OnTabAdded != null)
703             {
704                 try { OnTabAdded(this, new TabEventArgs(tab)); }
705                 catch (Exception) { }
706             }
707         }
708
709         public RadegastTab AddTab(string name, string label, Control control)
710         {
711             // WORKAROUND: one should never add tab that alrady exists
712             // but under some weird conditions disconnect/connect
713             // fire in the wrong order
714             if (TabExists(name))
715             {
716                 Logger.Log("Force closing tab '" + name + "'", Helpers.LogLevel.Warning, client);
717                 ForceCloseTab(name);
718             }
719
720             control.Visible = false;
721             control.Dock = DockStyle.Fill;
722             toolStripContainer1.ContentPanel.Controls.Add(control);
723
724             ToolStripButton button = (ToolStripButton)tstTabs.Items.Add(label);
725             button.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
726             button.Image = null;
727             button.AutoToolTip = false;
728             button.Tag = name.ToLower();
729             button.AllowDrop = true;
730             button.Click += new EventHandler(TabButtonClick);
731
732             RadegastTab tab = new RadegastTab(instance, button, control, name.ToLower(), label);
733             if (control is RadegastTabControl)
734                 ((RadegastTabControl)control).RadegastTab = tab;
735             tab.TabAttached += new EventHandler(tab_TabAttached);
736             tab.TabDetached += new EventHandler(tab_TabDetached);
737             tab.TabSelected += new EventHandler(tab_TabSelected);
738             tab.TabClosed += new EventHandler(tab_TabClosed);
739             tab.TabHidden += new EventHandler(tab_TabHidden);
740             tabs.Add(name.ToLower(), tab);
741
742             if (OnTabAdded != null)
743             {
744                 try { OnTabAdded(this, new TabEventArgs(tab)); }
745                 catch (Exception) { }
746             }
747
748             button.MouseDown += (msender, margs) =>
749             {
750                 if (margs.Button == MouseButtons.Middle)
751                 {
752                     if (tab.AllowClose)
753                     {
754                         tab.Close();
755                     }
756                     else if (tab.AllowHide)
757                     {
758                         tab.Hide();
759                     }
760                 }
761             };
762
763             return tab;
764         }
765
766         private void tab_TabAttached(object sender, EventArgs e)
767         {
768             RadegastTab tab = (RadegastTab)sender;
769             tab.Select();
770         }
771
772         private void tab_TabDetached(object sender, EventArgs e)
773         {
774             RadegastTab tab = (RadegastTab)sender;
775             frmDetachedTab form = (frmDetachedTab)tab.Owner;
776
777             form.ReattachStrip = tstTabs;
778             form.ReattachContainer = toolStripContainer1.ContentPanel;
779         }
780
781         private void tab_TabSelected(object sender, EventArgs e)
782         {
783             RadegastTab tab = (RadegastTab)sender;
784
785             if (selectedTab != null &&
786                 selectedTab != tab)
787             { selectedTab.Deselect(); }
788
789             selectedTab = tab;
790
791             tbtnCloseTab.Enabled = tab.AllowClose || tab.AllowHide;
792
793             if (owner != null)
794             {
795                 owner.AcceptButton = tab.DefaultControlButton;
796             }
797
798             if (OnTabSelected != null)
799             {
800                 try { OnTabSelected(this, new TabEventArgs(selectedTab)); }
801                 catch (Exception) { }
802             }
803         }
804
805         void tab_TabHidden(object sender, EventArgs e)
806         {
807             RadegastTab tab = (RadegastTab)sender;
808
809             if (selectedTab != null && selectedTab == tab)
810             {
811                 tab.Deselect();
812                 SelectDefaultTab();
813             }
814         }
815
816         private void tab_TabClosed(object sender, EventArgs e)
817         {
818             RadegastTab tab = (RadegastTab)sender;
819
820             if (selectedTab != null && selectedTab == tab && tab.Name != "chat")
821             {
822                 tab.Deselect();
823                 SelectDefaultTab();
824             }
825
826             tabs.Remove(tab.Name);
827
828             if (OnTabRemoved != null)
829             {
830                 try { OnTabRemoved(this, new TabEventArgs(tab)); }
831                 catch (Exception) { }
832             }
833
834             tab = null;
835         }
836
837         private void TabButtonClick(object sender, EventArgs e)
838         {
839             ToolStripButton button = (ToolStripButton)sender;
840
841             RadegastTab tab = tabs[button.Tag.ToString()];
842             tab.Select();
843         }
844
845         public void RemoveTabEntry(RadegastTab tab)
846         {
847             if (tstTabs.Items.Contains(tab.Button))
848             {
849                 tstTabs.Items.Remove(tab.Button);
850             }
851
852             tab.Button.Dispose();
853             tabs.Remove(tab.Name);
854         }
855
856         public void RemoveTab(string name)
857         {
858             if (tabs.ContainsKey(name))
859             {
860                 tabs.Remove(name);
861             }
862         }
863
864         //Used for outside classes that have a reference to TabsConsole
865         public void SelectTab(string name)
866         {
867             if (TabExists(name.ToLower()))
868                 tabs[name.ToLower()].Select();
869         }
870
871         public bool TabExists(string name)
872         {
873             return tabs.ContainsKey(name.ToLower());
874         }
875
876         public RadegastTab GetTab(string name)
877         {
878             if (TabExists(name.ToLower()))
879                 return tabs[name.ToLower()];
880             else
881                 return null;
882         }
883
884         public List<RadegastTab> GetOtherTabs()
885         {
886             List<RadegastTab> otherTabs = new List<RadegastTab>();
887
888             foreach (ToolStripItem item in tstTabs.Items)
889             {
890                 if (item.Tag == null) continue;
891                 if ((ToolStripItem)item == selectedTab.Button) continue;
892
893                 RadegastTab tab = tabs[item.Tag.ToString()];
894                 if (!tab.AllowMerge) continue;
895                 if (tab.Merged) continue;
896
897                 otherTabs.Add(tab);
898             }
899
900             return otherTabs;
901         }
902
903         /// <summary>
904         /// Activates the next tab
905         /// </summary>
906         public void SelectNextTab()
907         {
908             List<ToolStripItem> buttons = new List<ToolStripItem>();
909
910             foreach (ToolStripItem item in tstTabs.Items)
911             {
912                 if (item.Tag == null || !item.Visible) continue;
913
914                 buttons.Add(item);
915             }
916
917             int current = 0;
918
919             for (int i = 0; i < buttons.Count; i++)
920             {
921                 if (buttons[i] == selectedTab.Button)
922                 {
923                     current = i;
924                     break;
925                 }
926             }
927
928             current++;
929
930             if (current == buttons.Count)
931                 current = 0;
932
933             SelectTab(tabs[buttons[current].Tag.ToString()].Name);
934         }
935
936         /// <summary>
937         /// Activates the previous tab
938         /// </summary>
939         public void SelectPreviousTab()
940         {
941             List<ToolStripItem> buttons = new List<ToolStripItem>();
942
943             foreach (ToolStripItem item in tstTabs.Items)
944             {
945                 if (item.Tag == null || !item.Visible) continue;
946
947                 buttons.Add(item);
948             }
949
950             int current = 0;
951
952             for (int i = 0; i < buttons.Count; i++)
953             {
954                 if (buttons[i] == selectedTab.Button)
955                 {
956                     current = i;
957                     break;
958                 }
959             }
960
961             current--;
962
963             if (current == -1)
964                 current = buttons.Count - 1;
965
966             SelectTab(tabs[buttons[current].Tag.ToString()].Name);
967         }
968
969
970         public IMTabWindow AddIMTab(UUID target, UUID session, string targetName)
971         {
972             IMTabWindow imTab = new IMTabWindow(instance, target, session, targetName);
973
974             RadegastTab tab = AddTab(session.ToString(), "IM: " + targetName, imTab);
975             imTab.SelectIMInput();
976             tab.Highlight();
977
978             return imTab;
979         }
980
981         public ConferenceIMTabWindow AddConferenceIMTab(UUID session, string name)
982         {
983             ConferenceIMTabWindow imTab = new ConferenceIMTabWindow(instance, session, name);
984
985             RadegastTab tab = AddTab(session.ToString(), name, imTab);
986             imTab.SelectIMInput();
987
988             return imTab;
989         }
990
991
992         public ConferenceIMTabWindow AddConferenceIMTab(InstantMessageEventArgs e)
993         {
994             ConferenceIMTabWindow imTab = AddConferenceIMTab(e.IM.IMSessionID, Utils.BytesToString(e.IM.BinaryBucket));
995             imTab.TextManager.ProcessIM(e);
996             return imTab;
997         }
998
999         public GroupIMTabWindow AddGroupIMTab(UUID session, string name)
1000         {
1001             GroupIMTabWindow imTab = new GroupIMTabWindow(instance, session, name);
1002
1003             RadegastTab tab = AddTab(session.ToString(), name, imTab);
1004             imTab.SelectIMInput();
1005
1006             return imTab;
1007         }
1008
1009         public GroupIMTabWindow AddGroupIMTab(InstantMessageEventArgs e)
1010         {
1011             GroupIMTabWindow imTab = AddGroupIMTab(e.IM.IMSessionID, Utils.BytesToString(e.IM.BinaryBucket));
1012             imTab.TextManager.ProcessIM(e);
1013             return imTab;
1014         }
1015
1016         public IMTabWindow AddIMTab(InstantMessageEventArgs e)
1017         {
1018             IMTabWindow imTab = AddIMTab(e.IM.FromAgentID, e.IM.IMSessionID, e.IM.FromAgentName);
1019             imTab.TextManager.ProcessIM(e);
1020             return imTab;
1021         }
1022
1023         public OutfitTextures AddOTTab(Avatar avatar)
1024         {
1025             OutfitTextures otTab = new OutfitTextures(instance, avatar);
1026
1027             RadegastTab tab = AddTab("OT: " + avatar.Name, "OT: " + avatar.Name, otTab);
1028             otTab.GetTextures();
1029             return otTab;
1030         }
1031
1032         public MasterTab AddMSTab(Avatar avatar)
1033         {
1034             MasterTab msTab = new MasterTab(instance, avatar);
1035
1036             RadegastTab tab = AddTab("MS: " + avatar.Name, "MS: " + avatar.Name, msTab);
1037             return msTab;
1038         }
1039
1040         public AnimTab AddAnimTab(Avatar avatar)
1041         {
1042             AnimTab animTab = new AnimTab(instance, avatar);
1043
1044             RadegastTab tab = AddTab("Anim: " + avatar.Name, "Anim: " + avatar.Name, animTab);
1045             return animTab;
1046         }
1047
1048         private void tbtnTabOptions_Click(object sender, EventArgs e)
1049         {
1050             tmnuMergeWith.Enabled = selectedTab.AllowMerge;
1051             tmnuDetachTab.Enabled = selectedTab.AllowDetach;
1052
1053             tmnuMergeWith.DropDown.Items.Clear();
1054
1055             if (!selectedTab.AllowMerge) return;
1056             if (!selectedTab.Merged)
1057             {
1058                 tmnuMergeWith.Text = "Merge With";
1059
1060                 List<RadegastTab> otherTabs = GetOtherTabs();
1061
1062                 tmnuMergeWith.Enabled = (otherTabs.Count > 0);
1063                 if (!tmnuMergeWith.Enabled) return;
1064
1065                 foreach (RadegastTab tab in otherTabs)
1066                 {
1067                     ToolStripItem item = tmnuMergeWith.DropDown.Items.Add(tab.Label);
1068                     item.Tag = tab.Name;
1069                     item.Click += new EventHandler(MergeItemClick);
1070                 }
1071             }
1072             else
1073             {
1074                 tmnuMergeWith.Text = "Split";
1075                 tmnuMergeWith.Click += new EventHandler(SplitClick);
1076             }
1077         }
1078
1079         private void MergeItemClick(object sender, EventArgs e)
1080         {
1081             ToolStripItem item = (ToolStripItem)sender;
1082             RadegastTab tab = tabs[item.Tag.ToString()];
1083
1084             selectedTab.MergeWith(tab);
1085
1086             SplitContainer container = (SplitContainer)selectedTab.Control;
1087             toolStripContainer1.ContentPanel.Controls.Add(container);
1088
1089             selectedTab.Select();
1090             RemoveTabEntry(tab);
1091
1092             tabs.Add(tab.Name, selectedTab);
1093         }
1094
1095         private void SplitClick(object sender, EventArgs e)
1096         {
1097             SplitTab(selectedTab);
1098             selectedTab.Select();
1099         }
1100
1101         public void SplitTab(RadegastTab tab)
1102         {
1103             RadegastTab otherTab = tab.Split();
1104             if (otherTab == null) return;
1105
1106             toolStripContainer1.ContentPanel.Controls.Add(tab.Control);
1107             toolStripContainer1.ContentPanel.Controls.Add(otherTab.Control);
1108
1109             tabs.Remove(otherTab.Name);
1110             AddTab(otherTab);
1111         }
1112
1113         private void tmnuDetachTab_Click(object sender, EventArgs e)
1114         {
1115             if (!selectedTab.AllowDetach) return;
1116             RadegastTab tab = selectedTab;
1117             SelectDefaultTab();
1118             tab.Detach(instance);
1119         }
1120
1121         private void tbtnCloseTab_Click(object sender, EventArgs e)
1122         {
1123             RadegastTab tab = selectedTab;
1124             if (tab.AllowClose)
1125                 tab.Close();
1126             else if (tab.AllowHide)
1127                 tab.Hide();
1128         }
1129
1130         private void TabsConsole_Load(object sender, EventArgs e)
1131         {
1132             owner = this.FindForm();
1133         }
1134
1135         private void ctxTabs_Opening(object sender, CancelEventArgs e)
1136         {
1137             e.Cancel = false;
1138
1139             Point pt = this.PointToClient(Cursor.Position);
1140             ToolStripItem stripItem = tstTabs.GetItemAt(pt);
1141
1142             if (stripItem == null)
1143             {
1144                 e.Cancel = true;
1145             }
1146             else
1147             {
1148                 tabs[stripItem.Tag.ToString()].Select();
1149
1150                 ctxBtnClose.Enabled = !selectedTab.Merged && (selectedTab.AllowClose || selectedTab.AllowHide);
1151                 ctxBtnDetach.Enabled = selectedTab.AllowDetach;
1152                 ctxBtnMerge.Enabled = selectedTab.AllowMerge;
1153                 ctxBtnMerge.DropDown.Items.Clear();
1154
1155                 if (!ctxBtnClose.Enabled && !ctxBtnDetach.Enabled && !ctxBtnMerge.Enabled)
1156                 {
1157                     e.Cancel = true;
1158                     return;
1159                 }
1160
1161                 if (!selectedTab.AllowMerge) return;
1162                 if (!selectedTab.Merged)
1163                 {
1164                     ctxBtnMerge.Text = "Merge With";
1165
1166                     List<RadegastTab> otherTabs = GetOtherTabs();
1167
1168                     ctxBtnMerge.Enabled = (otherTabs.Count > 0);
1169                     if (!ctxBtnMerge.Enabled) return;
1170
1171                     foreach (RadegastTab tab in otherTabs)
1172                     {
1173                         ToolStripItem item = ctxBtnMerge.DropDown.Items.Add(tab.Label);
1174                         item.Tag = tab.Name;
1175                         item.Click += new EventHandler(MergeItemClick);
1176                     }
1177                 }
1178                 else
1179                 {
1180                     ctxBtnMerge.Text = "Split";
1181                     ctxBtnMerge.Click += new EventHandler(SplitClick);
1182                 }
1183
1184             }
1185         }
1186
1187     }
1188
1189     /// <summary>
1190     /// Arguments for tab events
1191     /// </summary>
1192     public class TabEventArgs : EventArgs
1193     {
1194         /// <summary>
1195         /// Tab that was manipulated in the event
1196         /// </summary>
1197         public RadegastTab Tab;
1198
1199         public TabEventArgs()
1200             : base()
1201         {
1202         }
1203
1204         public TabEventArgs(RadegastTab tab)
1205             : base()
1206         {
1207             Tab = tab;
1208         }
1209     }
1210
1211     /// <summary>
1212     /// Argument for chat notification events
1213     /// </summary>
1214     public class ChatNotificationEventArgs : EventArgs
1215     {
1216         public string Message;
1217         public ChatBufferTextStyle Style;
1218
1219         public ChatNotificationEventArgs(string message, ChatBufferTextStyle style)
1220         {
1221             Message = message;
1222             Style = style;
1223         }
1224     }
1225
1226     /// <summary>
1227     /// Element of list of nearby avatars
1228     /// </summary>
1229     public class NearbyAvatar
1230     {
1231         public UUID ID { get; set; }
1232         public string Name { get; set; }
1233     }
1234
1235 }