private bool StopRecording(ref IUser user, TvCardContext context)
        {
            bool stop = false;

            context.GetUser(ref user);
            ITvSubChannel subchannel = GetSubChannel(user.SubChannel);

            if (subchannel != null)
            {
                subchannel.StopRecording();
                _cardHandler.Card.FreeSubChannel(user.SubChannel);
                if (subchannel.IsTimeShifting == false || context.Users.Length <= 1)
                {
                    _cardHandler.Users.RemoveUser(user);
                }
                stop = true;
            }
            else
            {
                Log.Write("card: StopRecording subchannel null, skipping");
            }
            return(stop);
        }
        /// <summary>
        /// Removes the user from this card
        /// </summary>
        /// <param name="user">The user.</param>
        public void RemoveUser(IUser user)
        {
            if (_cardHandler.IsLocal == false)
            {
                try
                {
                    RemoteControl.HostName = _cardHandler.DataBaseCard.ReferencedServer().HostName;
                    RemoteControl.Instance.RemoveUserFromOtherCards(_cardHandler.DataBaseCard.IdCard, user);
                    return;
                }
                catch (Exception)
                {
                    Log.Error("card: unable to connect to slave controller at:{0}",
                              _cardHandler.DataBaseCard.ReferencedServer().HostName);
                    return;
                }
            }
            ITvCardContext context = _cardHandler.Card.Context as ITvCardContext;

            if (context == null)
            {
                return;
            }
            if (!context.DoesExists(user))
            {
                return;
            }
            context.GetUser(ref user, _cardHandler.DataBaseCard.IdCard);

            Log.Debug("usermanagement.RemoveUser: {0}, subch: {1} of {2}, card: {3}", user.Name, user.SubChannel, _cardHandler.Card.SubChannels.Length, _cardHandler.DataBaseCard.IdCard);
            context.Remove(user);
            if (!context.ContainsUsersForSubchannel(user.SubChannel))
            {
                //only remove subchannel if it exists.
                if (_cardHandler.Card.GetSubChannel(user.SubChannel) != null)
                {
                    int usedSubChannel = user.SubChannel;
                    // Before we remove the subchannel we have to stop it
                    ITvSubChannel subChannel = _cardHandler.Card.GetSubChannel(user.SubChannel);
                    if (subChannel.IsTimeShifting)
                    {
                        subChannel.StopTimeShifting();
                    }
                    else if (subChannel.IsRecording)
                    {
                        subChannel.StopRecording();
                    }
                    _cardHandler.Card.FreeSubChannel(user.SubChannel);
                    CleanTimeshiftFilesThread cleanTimeshiftFilesThread =
                        new CleanTimeshiftFilesThread(_cardHandler.DataBaseCard.TimeShiftFolder,
                                                      String.Format("live{0}-{1}.ts", _cardHandler.DataBaseCard.IdCard,
                                                                    usedSubChannel));
                    Thread cleanupThread = new Thread(cleanTimeshiftFilesThread.CleanTimeshiftFiles);
                    cleanupThread.IsBackground = true;
                    cleanupThread.Name         = "TS_File_Cleanup";
                    cleanupThread.Priority     = ThreadPriority.Lowest;
                    cleanupThread.Start();
                }
            }
            if (_cardHandler.IsIdle)
            {
                if (_cardHandler.Card.SupportsPauseGraph)
                {
                    _cardHandler.Card.PauseGraph();
                }
                else
                {
                    _cardHandler.Card.StopGraph();
                }
            }
        }
        /// <summary>
        /// Stops recording.
        /// </summary>
        /// <param name="user">User</param>
        /// <returns></returns>
        public bool Stop(ref IUser user)
        {
            try
            {
                if (_cardHandler.DataBaseCard.Enabled == false)
                {
                    return(false);
                }
                Log.Write("card: StopRecording card={0}, user={1}", _cardHandler.DataBaseCard.IdCard, user.Name);
                lock (this)
                {
                    try
                    {
                        RemoteControl.HostName = _cardHandler.DataBaseCard.ReferencedServer().HostName;
                        if (!RemoteControl.Instance.CardPresent(_cardHandler.DataBaseCard.IdCard))
                        {
                            return(false);
                        }

                        if (_cardHandler.IsLocal == false)
                        {
                            return(RemoteControl.Instance.StopRecording(ref user));
                        }
                    }
                    catch (Exception)
                    {
                        Log.Error("card: unable to connect to slave controller at:{0}",
                                  _cardHandler.DataBaseCard.ReferencedServer().HostName);
                        return(false);
                    }
                    Log.Write("card: StopRecording for card:{0}", _cardHandler.DataBaseCard.IdCard);
                    TvCardContext context = _cardHandler.Card.Context as TvCardContext;
                    if (context == null)
                    {
                        Log.Write("card: StopRecording context null");
                        return(false);
                    }
                    if (user.IsAdmin)
                    {
                        context.GetUser(ref user);
                        ITvSubChannel subchannel = _cardHandler.Card.GetSubChannel(user.SubChannel);
                        if (subchannel == null)
                        {
                            Log.Write("card: StopRecording subchannel null, skipping");
                            return(false);
                        }
                        subchannel.StopRecording();
                        _cardHandler.Card.FreeSubChannel(user.SubChannel);
                        if (subchannel.IsTimeShifting == false || context.Users.Length <= 1)
                        {
                            _cardHandler.Users.RemoveUser(user);
                        }
                    }

                    IUser[] users = context.Users;
                    for (int i = 0; i < users.Length; ++i)
                    {
                        ITvSubChannel subchannel = _cardHandler.Card.GetSubChannel(users[i].SubChannel);
                        if (subchannel != null)
                        {
                            if (subchannel.IsRecording)
                            {
                                Log.Write("card: StopRecording setting new context owner on user '{0}'", users[i].Name);
                                context.Owner = users[i];
                                break;
                            }
                        }
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            return(false);
        }