2017년 3월 8일 수요일

[Development Story] GFtp - #3

GFtp's second update.

1. Update list

 - To add progress bar while translate files.
 - To move between ftp directory using double click.
 - To display more detail ftp file / directory information
 - Disable editing of DataGridView.

2. Demo

 - GFtp v1.2 demo

3. Codes snippets

 - To add progress bar while translate files. 

  First, set a progressbar and whenever translate file increment step using PerformStep() function.
  Next code is to translate files with increment progress bar.

         // Translate files from local to ftp or from ftp to local
        private bool TranslateFiles(string[] files, bool IsUpload)
        {
            // Set a progressbar
            progressBar.Style = ProgressBarStyle.Continuous;
            progressBar.Minimum = 0;
            progressBar.Maximum = files.Length;
            progressBar.Step = 1;
            progressBar.Value = 0;
   

            // connect ftp
            WebClient wc = new WebClient { Proxy = null };
            wc.BaseAddress = _ftpAddress;
            if (_id != "" && _password != "")
            {
                wc.Credentials = new NetworkCredential(_id, _password);
            }

            try
            {
                foreach (string filename in files)
                {
                    string pathName = Path.Combine(_currentDirectory, filename);

                    // increment progress bar
                    progressBar.PerformStep();
                    
                    // upload
                    if (IsUpload)
                    {
                        wc.UploadFile(pathName, filename);
                    }
                    // download
                    else
                    {
                        wc.DownloadFile(filename, pathName);
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

 - To move between ftp directory using double click.
  First, I made a EventHandler for DoubleClick in both fileGridView and ftpFileGridView.
  Following codes are Add EventHandler to fileGridView and to implement that EventHandelr.
        public Form1()
        {
            InitializeComponent();
   
            // Add a event handler to fileGridView.DoubleClock.
            fileGridView.DoubleClick += FileGridView_DoubleClick;
        }

        // It's called after double clicking on fileGridView. 
        private void FileGridView_DoubleClick(object sender, EventArgs e)
        {
            if (sender != fileGridView)
                return;

            DataGridViewCell cell = fileGridView.CurrentCell;
            // Check clicked cell is file or folder
            bool isFolder = true;
            if (fileGridView.Rows[cell.RowIndex].Cells[2].Value.ToString() == "File")
                isFolder = false;

            // if clicked cell is folder then move current directory to sub directory
            if(isFolder)
            {
                _currentDirectory   = Path.Combine(_currentDirectory, fileGridView.Rows[cell.RowIndex].Cells[0].Value.ToString());
                RefreshFileGridViewOfCurrentDirectory();
            }         
        }

 - To display more detail ftp file / directory information
  To include variety of information for directory and file, I made GridFileInfo class.
  GridFileInfo Inherit IComparable and has CompareTo function for sorting from directory to file.
  I used GridFileInfo for fileGridView and ftpFileGridView. As you know, Ftp file has more detail information than local file. So it may waste memory if you use this class for both.
  But, I thought that can share some function like refresh grid view for file grid view and ftp file grid view. And I thought that it can be increased comfortation for coding.
  Following is GridFileInfo class.
    public class GridFileInfo : IComparable
    {
        public string Name { get; set; }
        public long Size { get; set; }
        public bool IsFolder { get; set; }
       

        public int CompareTo(object obj)
        {
            if(obj is GridFileInfo)
            {
                GridFileInfo tmp = (GridFileInfo)obj;
                if (tmp.IsFolder == IsFolder)
                    return 0;
                else if (tmp.IsFolder == true)
                    return 1;
                else if (IsFolder == true)
                    return -1;
            }
            throw new ArgumentException("Object is not a GridFileInfo");
        }
    }  

 Following is to refresh filegrid view of local directory and show to share FileGridInfo class.
        // Refresh FileTreeView
        // Display all file in the current directory.
        void RefreshFileGridViewOfCurrentDirectory()
        {
            // Get all file list  directory list of my pc;
            DirectoryInfo di = new DirectoryInfo(_currentDirectory);

            // Get all sub directory
            DirectoryInfo[] subDiArray = di.GetDirectories();
           
            // Get all files
            FileInfo[] fiArray = di.GetFiles("*.*");

            GridFileInfo[] gridFileInfos = new GridFileInfo[1 + subDiArray.Length + fiArray.Length];
            int idx = 0;

            // Add super directory
            {
                gridFileInfos[idx] = new GridFileInfo();
                gridFileInfos[idx].Name = "..";
                gridFileInfos[idx].Size = 0;
                gridFileInfos[idx].IsFolder = true;
                idx++;
            }
            
            // Add directory infos
            foreach (DirectoryInfo subDi in subDiArray)
            {
                gridFileInfos[idx] = new GridFileInfo();
                gridFileInfos[idx].Name = subDi.Name;
                gridFileInfos[idx].Size = 0;
                gridFileInfos[idx].IsFolder = true;
                idx++;
            }
         
            // Add file infos
            foreach(FileInfo fi in fiArray)
            {
                gridFileInfos[idx] = new GridFileInfo();
                gridFileInfos[idx].Name = fi.Name;
                gridFileInfos[idx].Size = fi.Length;
                gridFileInfos[idx].IsFolder = false;
                idx++;
            }
            // Display file to grid view
            DisplayFilesToGridView(fileGridView, gridFileInfos);
        }

4. Git

 - I am feeling that uncomfortation to management version and source code.
   So I started manage version and source code using Git.
   From now my source code is shared via GitHub.com.
   My github link is
   https://github.com/gurum77/GFtp

5. Next

 - GFtp version is 1.2. GFtp is getting complex.
   So I will refactoring GFtp. it will be also simple refactoring. Because this project is for just my studying C#.

댓글 없음:

댓글 쓰기