/// <summary> /// Handler for the Channel Added event /// </summary> private void TeamspeakService_ChannelAdded(object sender, TS3.Data.ChannelEventArgs e) { Threading.BeginInvokeOnUI(() => { if (e.Channel.IsSpacer) return; // Totally ignore spacers var newChannel = new ChannelViewModel(e.Channel, this.TeamspeakService); // Check if we have any orphans that are a subchannel of this new channel var matchingOrphans = this.orphanChannels.Where(c => c.ParentID == newChannel.ID); foreach (var orphan in matchingOrphans) { newChannel.Subchannels.Add(orphan); } this.orphanChannels.RemoveAll(c => c.ParentID == newChannel.ID); if (newChannel.ParentID != 0) { // This has a parent channel - find it var parentChannel = this.FindParentChannel(this.Channels, newChannel); if (parentChannel != null) { parentChannel.Subchannels.Add(newChannel); } else { // This is an orphan channel... add it to our orphan list for now this.orphanChannels.Add(newChannel); } } else { // No parent this.Channels.Insert(0, newChannel); } this.OnPropertyChanged(() => this.AreChannelsLoaded); }); }
/// <summary> /// Handler for the Channel Removed event /// </summary> private void TeamspeakService_ChannelRemoved(object sender, TS3.Data.ChannelEventArgs e) { Threading.BeginInvokeOnUI(() => { var removedChannel = new ChannelViewModel(e.Channel, this.TeamspeakService); if (removedChannel.ParentID != 0) { // This has a parent channel - find it var parentChannel = this.FindParentChannel(this.Channels, removedChannel); if (parentChannel != null) { var toRemove = parentChannel.Subchannels.FirstOrDefault(channel => channel.ID == removedChannel.ID); parentChannel.Subchannels.Remove(toRemove); } } else { // No parent var toRemove = this.Channels.FirstOrDefault(channel => channel.ID == removedChannel.ID); this.Channels.Remove(toRemove); } this.OnPropertyChanged(() => this.AreChannelsLoaded); }); }
/// <summary> /// Handler for the Channel Updated event /// </summary> private void TeamspeakService_ChannelUpdated(object sender, TS3.Data.ChannelEventArgs e) { Threading.BeginInvokeOnUI(() => { // Find the matching existing channel ChannelViewModel existingChannel = this.FindChannel(this.Channels, e.Channel.ID); if (existingChannel == null) { // This shouldn't happen, but if it does, don't crash, just treat it as a "channel added" this.TeamspeakService_ChannelAdded(sender, e); return; } existingChannel.Name = e.Channel.Name; existingChannel.OrderIndex = e.Channel.Order; existingChannel.ClientsCount = e.Channel.ClientsCount; // Check to see if the parent ID has changed. If so, update it and move the channel if (existingChannel.ParentID != e.Channel.ParentID) { // Find the existing parent ChannelViewModel existingParent = this.FindParentChannel(this.Channels, existingChannel); if (existingParent != null) { // Remove it from the parent existingParent.Subchannels.Remove(existingChannel); } // Update the parent ID existingChannel.ParentID = e.Channel.ParentID; // Find the new parent ChannelViewModel newParent = this.FindParentChannel(this.Channels, existingChannel); if (newParent != null) { // Add it to the parent newParent.Subchannels.Add(existingChannel); } else { // Orphan... this.orphanChannels.Add(existingChannel); } } this.OnPropertyChanged(() => this.AreChannelsLoaded); }); }
/// <summary> /// Handler for the Channel Added event /// </summary> private void TeamspeakService_ChannelAdded(object sender, TS3.Data.ChannelEventArgs e) { Threading.BeginInvokeOnUI(() => { if (e.Channel.IsSpacer) { return; // Totally ignore spacers } var newChannel = new ChannelViewModel(e.Channel, this.TeamspeakService); // Check if we have any orphans that are a subchannel of this new channel var matchingOrphans = this.orphanChannels.Where(c => c.ParentID == newChannel.ID); foreach (var orphan in matchingOrphans) { newChannel.Subchannels.Add(orphan); } this.orphanChannels.RemoveAll(c => c.ParentID == newChannel.ID); if (newChannel.ParentID != 0) { // This has a parent channel - find it var parentChannel = this.FindParentChannel(this.Channels, newChannel); if (parentChannel != null) { parentChannel.Subchannels.Add(newChannel); } else { // This is an orphan channel... add it to our orphan list for now this.orphanChannels.Add(newChannel); } } else { // No parent this.Channels.Insert(0, newChannel); } this.OnPropertyChanged(() => this.AreChannelsLoaded); }); }
/// <summary> /// Recursively searches through a collection of channels to find a subChannel's parent ChannelViewModel object /// </summary> /// <param name="channelCollection">The collection to search</param> /// <param name="subChannel">The subChannel of which to the find the parent of</param> /// <returns>The parent of the subchannel, or null if not found</returns> private ChannelViewModel FindParentChannel(ICollection <ChannelViewModel> channelCollection, ChannelViewModel subChannel) { foreach (var channel in channelCollection) { if (channel.ID == subChannel.ParentID) { return(channel); } else { // Recurse within the subchannels of this channel var parent = this.FindParentChannel(channel.Subchannels, subChannel); if (parent != null) { return(parent); } } } // Didn't find parent return(null); }
/// <summary> /// Recursively searches through a collection of channels to find a subChannel's parent ChannelViewModel object /// </summary> /// <param name="channelCollection">The collection to search</param> /// <param name="subChannel">The subChannel of which to the find the parent of</param> /// <returns>The parent of the subchannel, or null if not found</returns> private ChannelViewModel FindParentChannel(ICollection<ChannelViewModel> channelCollection, ChannelViewModel subChannel) { foreach (var channel in channelCollection) { if (channel.ID == subChannel.ParentID) { return channel; } else { // Recurse within the subchannels of this channel var parent = this.FindParentChannel(channel.Subchannels, subChannel); if (parent != null) { return parent; } } } // Didn't find parent return null; }