miniSql

6136600 » zgc123@gmail.com
11/19/2023 创建
#region License

// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE

#endregion

using System;
using System.Collections.Generic;

namespace MiniSqlQuery.Core
{
    public class MostRecentFilesService : IMostRecentFilesService
    {
        private readonly List<string> _filenames;

        public MostRecentFilesService()
        {
            _filenames = new List<string>();
        }

        public event EventHandler<MostRecentFilesChangedEventArgs> MostRecentFilesChanged;

        public IList<string> Filenames
        {
            get { return _filenames; }
        }

        public void OnMostRecentFilesChanged(MostRecentFilesChangedEventArgs e)
        {
            var handler = MostRecentFilesChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public void Register(string filename)
        {
            if (!_filenames.Contains(filename))
            {
                _filenames.Insert(0, filename);
            }
            else
            {
                // move to top of list
                if (_filenames.Count > 1)
                {
                    _filenames.Remove(filename);
                    _filenames.Insert(0, filename);
                }
            }

            // enure the list is capped
            while (_filenames.Count > MaxCommands)
            {
                _filenames.RemoveAt(_filenames.Count - 1);
            }

            NotifyListenersOfChange();
        }

        public void Remove(string filename)
        {
            if (_filenames.Contains(filename))
            {
                _filenames.Remove(filename);
            }
            NotifyListenersOfChange();
        }

        public int MaxCommands
        {
            get { return 10; }
        }

        protected void NotifyListenersOfChange()
        {
            OnMostRecentFilesChanged(new MostRecentFilesChangedEventArgs(_filenames));
        }
    }
}