Exemple #1
0
        private void frmFileExplorer_Load(object sender, EventArgs e)
        {
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            Text = $"FFT - File Explorer - {_client.IP}:{_client.Port}";

            // Set list to display drive information by default
            SetDriveColumns(true);

            // Load default images
            lstIcons.Images.Add(IconReader.GetFileIcon("", IconReader.IconSize.Small, false));
            lstIcons.Images.Add(IconReader.GetFileIcon("dummy", IconReader.IconSize.Small, false));
            lstIcons.Images.Add(IconReader.GetFolderIcon(IconReader.IconSize.Small, IconReader.FolderType.Open));


            // Send request for list of drives
            _client.Transmit(new Packet(PacketHeader.DrivesGet));
            lstFiles.Enabled = false;

            // Setup Event handlers for form controls
            lstFiles.DoubleClick            += LstFiles_DoubleClick;
            FormClosing                     += FrmFileExplorer_FormClosing;
            mnuRefresh.Click                += MnuRefresh_Click;
            lstTransfers.ColumnWidthChanged += LstFiles_ColumnWidthChanged;

            #region mnuEvents
            // Directory buttons
            mnuDirectoryCreate.Click   += MnuDirectoryCreate_Click;
            mnuDirectoryMove.Click     += MnuDirectoryMove_Click;
            mnuDirectoryDelete.Click   += MnuDirectoryDelete_Click;
            mnuDirectoryCompress.Click += MnuDirectoryCompress_Click;

            // File Buttons
            mnuFileCompress.Click += MnuFileCompress_Click;
            mnuFileDelete.Click   += MnuFileDelete_Click;
            mnuFileMove.Click     += MnuFileMove_Click;
            mnuChecksum.Click     += MnuChecksum_Click;

            // Transfer Buttons
            mnuDownload.Click       += MnuDownload_Click;
            mnuUpload.Click         += MnuUpload_Click;
            mnuTransferPause.Click  += MnuTransferPause_Click;
            mnuTransferCancel.Click += MnuTransferCancel_Click;
            #endregion

            // Create transfer socket
            Visible = false;

            _transfer        = new Client(Protocol.FFTST, _client.IP, _client.Port, _client.Password, _client.CryptoServiceAlgorithm);
            _transfer.Ready += _transfer_Ready;
            _transfer.Connect(Program.Configuration.MaxBufferSize);
        }
Exemple #2
0
        private void _client_PacketReceived(Client client, Packet packet)
        {
            using (MemoryStream ms = new MemoryStream(packet.Payload))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    Invoke((MethodInvoker) delegate
                    {
                        switch (packet.PacketHeader)
                        {
                        case PacketHeader.DrivesGet:
                            SetDriveColumns();

                            int drives = br.ReadInt32();
                            for (int i = 0; i < drives; i++)
                            {
                                string[] columns = new string[]
                                {
                                    br.ReadString(),        // name
                                    br.ReadString(),        // label
                                    br.ReadString(),        // total
                                    br.ReadString(),        // used
                                    br.ReadString(),        // free
                                    br.ReadString(),        // format
                                    br.ReadString()         // type
                                };
                                var item = new ListViewItem(columns, 0);
                                item.Tag = "drive";

                                if (br.ReadString() == true.ToString())
                                {
                                    item.ImageIndex = 1;
                                }

                                lstFiles.Items.Add(item);
                            }

                            lstFiles.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                            lstFiles.Enabled = true;
                            break;

                        case PacketHeader.DirectoryGet:
                            bool resize = _drives;
                            SetFileColumns();
                            lstFiles.BeginUpdate();

                            // Folders
                            int rows = br.ReadInt32();
                            for (int i = 0; i < rows; i++)
                            {
                                string[] columns = new string[]
                                {
                                    br.ReadString(),        // Name
                                    br.ReadString(),        // Full path
                                    br.ReadString(),        // File count
                                    br.ReadString(),        // Created On
                                    br.ReadString()         // Last Modified
                                };

                                var item        = new ListViewItem(columns);
                                item.ImageIndex = 3;
                                item.Tag        = "folder";

                                if (item.SubItems[0].Text.Contains("*"))
                                {
                                    item.SubItems[0].Text = item.SubItems[0].Text.Replace("*", "");
                                    item.ForeColor        = Color.Red;
                                }

                                lstFiles.Items.Add(item);
                            }

                            // Files
                            rows = br.ReadInt32();
                            for (int i = 0; i < rows; i++)
                            {
                                string[] columns = new string[]
                                {
                                    br.ReadString(),        // Name
                                    br.ReadString(),        // Full path
                                    br.ReadString(),        // size
                                    br.ReadString(),        // Created On
                                    br.ReadString(),        // Last Modified
                                };
                                var len              = long.Parse(br.ReadString());
                                var item             = new ListViewItem(columns);
                                item.Tag             = "file";
                                item.SubItems[2].Tag = len;     // full file size on subitem item tag

                                string ex = Path.GetExtension(columns[1]);
                                if (ex == "")
                                {
                                    item.ImageIndex = 2;
                                }
                                else
                                {
                                    if (lstIcons.Images.ContainsKey(ex))
                                    {
                                        item.ImageIndex = lstIcons.Images.IndexOfKey(ex);
                                    }
                                    else
                                    {
                                        Icon icon = IconReader.GetFileIcon(ex, IconReader.IconSize.Small, false);
                                        if (icon != null)
                                        {
                                            Logger.Debug($"Adding icon for ext={ex}");
                                            lstIcons.Images.Add(ex, icon);
                                            item.ImageIndex = lstIcons.Images.Count - 1;
                                        }
                                    }
                                }

                                lstFiles.Items.Add(item);
                            }
                            if (resize)
                            {
                                lstFiles.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            }
                            lstFiles.Enabled = true;
                            lstFiles.EndUpdate();
                            break;

                        case PacketHeader.DirectoryCreate:
                        case PacketHeader.DirectoryDelete:
                        case PacketHeader.DirectoryCompress:
                        case PacketHeader.DirectoryMove:
                        case PacketHeader.FileCompress:
                        case PacketHeader.FileDelete:
                        case PacketHeader.FileMove:
                            lstFiles.Enabled = false;
                            client.Transmit(new Packet(PacketHeader.DirectoryGet, currentPath));
                            break;

                        case PacketHeader.Checksum:
                            string p = br.ReadString(), h = br.ReadString();

                            MessageBox.Show($"File Path:\n{p}\n\nSHA256:\n{h}", "SHA256 Checksum", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;

                        case PacketHeader.Exception:
                            MessageBox.Show(packet.PayloadAsString(), "An error occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lstFiles.Enabled = true;
                            break;

                        default:     // Unhandled packet
                            break;
                        }
                    });
                }
            }
        }