// ctrlOptionChat.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System.ComponentModel;
using System.Windows.Forms;
using FileScope;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for ctrlOptionChat.
    /// </summary>
    public class ctrlOptionShares : UserControl
    {

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private Container components;
        private ListBox lstShares;
        private Button btnRemoveShare;
        private Button bntShareAdd;
        private Label lblShareText;
        private FolderBrowserDialog folderBrowserDialog1;

        private GroupBox groupBox1;

        public ctrlOptionShares()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitializeComponent call
            LoadShares();
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {

            //zzoi 28 Feb 2009
            //update the lib with new shares (save the shares)
            //Sharing
            lock (Stats.shareList)
            {
                Stats.shareList.Clear();
                foreach (object shareFolder in lstShares.Items)
                    Stats.shareList.Add(shareFolder);
            }
            Stats.LoadSave.UpdateShares();


            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lblShareText = new System.Windows.Forms.Label();
            this.btnRemoveShare = new System.Windows.Forms.Button();
            this.bntShareAdd = new System.Windows.Forms.Button();
            this.lstShares = new System.Windows.Forms.ListBox();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.lblShareText);
            this.groupBox1.Controls.Add(this.btnRemoveShare);
            this.groupBox1.Controls.Add(this.bntShareAdd);
            this.groupBox1.Controls.Add(this.lstShares);
            this.groupBox1.Location = new System.Drawing.Point(8, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(288, 181);
            this.groupBox1.TabIndex = 17;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Shares";
            // 
            // lblShareText
            // 
            this.lblShareText.AutoSize = true;
            this.lblShareText.Location = new System.Drawing.Point(22, 23);
            this.lblShareText.Name = "lblShareText";
            this.lblShareText.Size = new System.Drawing.Size(136, 13);
            this.lblShareText.TabIndex = 3;
            this.lblShareText.Text = "Choose the folders to share";
            // 
            // btnRemoveShare
            // 
            this.btnRemoveShare.Location = new System.Drawing.Point(181, 51);
            this.btnRemoveShare.Name = "btnRemoveShare";
            this.btnRemoveShare.Size = new System.Drawing.Size(75, 23);
            this.btnRemoveShare.TabIndex = 2;
            this.btnRemoveShare.Text = "Remove Share";
            this.btnRemoveShare.UseVisualStyleBackColor = true;
            this.btnRemoveShare.Click += new System.EventHandler(this.btnRemoveShare_Click);
            // 
            // bntShareAdd
            // 
            this.bntShareAdd.Location = new System.Drawing.Point(17, 51);
            this.bntShareAdd.Name = "bntShareAdd";
            this.bntShareAdd.Size = new System.Drawing.Size(75, 23);
            this.bntShareAdd.TabIndex = 1;
            this.bntShareAdd.Text = "Add Share";
            this.bntShareAdd.UseVisualStyleBackColor = true;
            this.bntShareAdd.Click += new System.EventHandler(this.bntShareAdd_Click);
            // 
            // lstShares
            // 
            this.lstShares.FormattingEnabled = true;
            this.lstShares.Location = new System.Drawing.Point(6, 80);
            this.lstShares.Name = "lstShares";
            this.lstShares.Size = new System.Drawing.Size(276, 95);
            this.lstShares.TabIndex = 0;
            // 
            // ctrlOptionShares
            // 
            this.Controls.Add(this.groupBox1);
            this.Name = "ctrlOptionShares";
            this.Size = new System.Drawing.Size(328, 184);
            this.Load += new System.EventHandler(this.ctrlOptionShares_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        //zzoi 28 Feb 2009
        //load existing shares into list
        private void LoadShares()
        {
            //Sharing
            lock (Stats.shareList)
            {
                foreach (object shareFolder in Stats.shareList)
                    lstShares.Items.Add(shareFolder);
            }
        }

        private void bntShareAdd_Click(object sender, System.EventArgs e)
        {
            string strDir = GetFolder();
            if (strDir.Length > 0)
            {

                if (!System.IO.Directory.Exists(strDir))
                {
                    //zzoi 28 Feb 2009 no hardcoded strings
                    // add to resource file
                    MessageBox.Show("The download directory does not exist!");
                    return;
                }
                if (!lstShares.Items.Contains(strDir))
                    lstShares.Items.Add(strDir);
            }
        }

        private void btnRemoveShare_Click(object sender, System.EventArgs e)
        {
            //remove selected shares
            for (int count = lstShares.SelectedIndices.Count - 1; count >= 0; count--)
                lstShares.Items.RemoveAt(lstShares.SelectedIndices[count]);
        }

        private string GetFolder()
        {
            folderBrowserDialog1.ShowDialog();
            return folderBrowserDialog1.SelectedPath;
        }

        private void ctrlOptionShares_Load(object sender, System.EventArgs e)
        {

        }
    }
}