Example #1
0
        private void UpdateFileListsNEW(Direction dir)
        {
            // get all transfer for one direction and map them to their hash (-> list)
            Dictionary <string, FileTransfer> list = new Dictionary <string, FileTransfer>();

            Dictionary <string, FileTransfer> .ValueCollection fileTransfers = _fileTransfersNEW.Values;
            foreach (FileTransfer ft in fileTransfers)
            {
                if (dir == ft.direction)
                {
                    list.Add(ft.file.hash, ft);
                }
            }

            if (list.Count == 0)
            {
                if (dir == Direction.DIRECTION_DOWNLOAD)
                {
                    _b.GUI.dgv_filesDownloads.Rows.Clear();
                }
                else
                {
                    _b.GUI.dgv_filesUploads.Rows.Clear();
                }
                return;
            }


            object[] row;
            byte     hashIndex;
            string   selectedHash;
            // get all rows of the DataGridView and map them to their hash (-> list)
            Dictionary <string, object[]> rows = new Dictionary <string, object[]>();
            DataGridViewRowCollection     dgvRowCollection;

            if (dir == Direction.DIRECTION_DOWNLOAD)
            {
                hashIndex        = 5;
                dgvRowCollection = _b.GUI.dgv_filesDownloads.Rows;
                row = new object[6];

                foreach (DataGridViewRow dgvRow in dgvRowCollection)
                {
                    row[0] = (bool)dgvRow.Cells[0].Value;
                    row[1] = (string)dgvRow.Cells[1].Value;
                    row[2] = (string)dgvRow.Cells[2].Value;
                    row[3] = (string)dgvRow.Cells[3].Value;
                    row[4] = (string)dgvRow.Cells[4].Value;
                    row[5] = (string)dgvRow.Cells[5].Value;

                    rows.Add(row[hashIndex].ToString(), row);

                    if (dgvRow.Selected)
                    {
                        selectedHash = (string)row[5];
                    }
                }
            }
            else
            {
                hashIndex        = 3;
                dgvRowCollection = _b.GUI.dgv_filesUploads.Rows;
                row = new object[4];

                foreach (DataGridViewRow dgvRow in dgvRowCollection)
                {
                    row[0] = (string)dgvRow.Cells[0].Value;
                    row[1] = (string)dgvRow.Cells[1].Value;
                    row[2] = (string)dgvRow.Cells[2].Value;
                    row[3] = (string)dgvRow.Cells[3].Value;

                    rows.Add(row[hashIndex].ToString(), row);

                    if (dgvRow.Selected)
                    {
                        selectedHash = (string)row[3];
                    }
                }
            }


            string hash;

            // update old & add new
            foreach (FileTransfer ft in list.Values)
            {
                hash = ft.file.hash;
                if (dir == Direction.DIRECTION_DOWNLOAD)
                {
                    row = new object[] {
                        false,
                        String.Format("{0:0,0.00}", ft.fraction * 100) + "%",
                        String.Format("{0:0,0.00}", ft.rate_kBs),
                        ft.file.name,
                        Processor.BuildSizeString(ft.file.size),
                        hash
                    };
                }
                else
                {
                    row = new object[] {
                        String.Format("{0:0,0.00}", ft.rate_kBs),
                        ft.file.name,
                        Processor.BuildSizeString(ft.file.size),
                        hash
                    };
                }
                if (rows.ContainsKey(hash)) // update
                {
                    rows[hash] = row;
                }
                else //add new
                {
                    rows.Add(hash, row);
                }
            }

            // remove old
            object[][] values = new object[rows.Count][];
            rows.Values.CopyTo(values, 0);
            foreach (object[] row2 in values)
            {
                if (!list.ContainsKey(row2[hashIndex].ToString()))
                {
                    rows.Remove(row[hashIndex].ToString());
                }
            }


            if (dir == Direction.DIRECTION_DOWNLOAD)
            {
                _b.GUI.dgv_filesDownloads.Rows.Clear();
                foreach (object[] row2 in rows.Values)
                {
                    //{
                    _b.GUI.dgv_filesDownloads.Rows.Add(row2);
                }
                //    if((string)row2[5] == selectedHash)
                //        _b.GUI.dgv_filesDownloads.Rows[_b.GUI.dgv_filesDownloads.se
                //}
            }
            else
            {
                _b.GUI.dgv_filesUploads.Rows.Clear();
                foreach (object[] row2 in rows.Values)
                {
                    _b.GUI.dgv_filesUploads.Rows.Add(row2);
                }
            }
        }