private async Task DownloadAdvanced(Directory directory, string toLocalPath)
        {
            labelAction.Text  = "Downloading...";
            labelContent.Text = directory.Path;
            bool   rootDownload = directory.Path == BrickExplorer.ROOT_PATH;
            string localPath    = (rootDownload) ? System.IO.Path.Combine(toLocalPath, Manager.Brick.Name) : System.IO.Path.Combine(toLocalPath, directory.Name);

            System.IO.Directory.CreateDirectory(localPath);

            File[] files = await BrickExplorer.GetFiles(directory.Path);

            foreach (File file in files)
            {
                labelContent.Text = file.Path;
                byte[] data = await file.Download();

                if (data != null && data.Length > 0)
                {
                    string filePath = System.IO.Path.Combine(localPath, file.FileName);
                    using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                    {
                        fs.Write(data, 0, data.Length);
                    }
                }
            }
            Directory[] subDirectories = await BrickExplorer.GetDirectories(directory.Path);

            foreach (Directory subDirectory in subDirectories)
            {
                await DownloadAdvanced(subDirectory, localPath);
            }
        }
Example #2
0
        private async Task BasicTree(TreeNode treeNode)
        {
            Directory[] directories;
            TreeNode    node;

            node = new TreeNode("Drive")
            {
                Tag                = await BrickExplorer.GetDirectory(BrickExplorer.PROJECTS_PATH),
                ImageIndex         = 2,
                SelectedImageIndex = 2
            };
            directories = await Manager.Brick.Drive.GetDirectories();

            Append(node, directories);
            treeNode.Nodes.Add(node);

            if (Manager.Brick.SDCard != null)
            {
                node = new TreeNode("SD Card")
                {
                    Tag                = await BrickExplorer.GetDirectory(BrickExplorer.SDCARD_PATH),
                    ImageIndex         = 3,
                    SelectedImageIndex = 3
                };
                directories = await Manager.Brick.SDCard.GetDirectories();

                Append(node, directories);
                treeNode.Nodes.Add(node);
            }
        }
        private async Task UploadDirectoryRecursive(string directoryPath, string path)
        {
            labelAction.Text  = "Uploading...";
            labelContent.Text = path;

            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(path);
            string brickPath = $"{directoryPath}{directoryInfo.Name}/";

            await BrickExplorer.CreateDirectory(brickPath);

            foreach (System.IO.FileInfo file in directoryInfo.GetFiles())
            {
                if (BrickExplorer.IsRobotFile(file.FullName))
                {
                    labelContent.Text = file.FullName;
                    await BrickExplorer.UploadFile(file.FullName, brickPath, file.Name);
                }
            }

            if (UserSettings.Mode != Mode.BASIC)
            {
                foreach (System.IO.DirectoryInfo subDir in directoryInfo.GetDirectories())
                {
                    await UploadDirectoryRecursive(brickPath, subDir.FullName);
                }
            }
        }
        private async void ToolStripButtonClicked(object sender, EventArgs e)
        {
            ToolStripButton button    = (ToolStripButton)sender;
            string          path      = button.Tag.ToString();
            Directory       directory = await BrickExplorer.GetDirectory(path);

            DirectoryAction?.Invoke(this, directory, ActionType.OPEN);
        }
Example #5
0
        /// <summary>
        /// Gets the actual brick name
        /// </summary>
        /// <returns></returns>
        public async Task <string> GetBrickName()
        {
            byte[] data = await BrickExplorer.DownloadFile("../sys/settings/BrickName");

            string name = Encoding.ASCII.GetString(data);

            return(name.TrimEnd('\0', '\n'));
        }
Example #6
0
        /// <summary>
        /// Uploads a file to this directory. Any existing file will not be overriden.
        /// </summary>
        /// <param name="localFilePath">The absolute local filePath to a lego file on the local machine</param>
        /// <param name="fileName">The renamed fileName as stored on brick incl. extension</param>
        /// <returns></returns>
        public async Task <File> UploadFile(string localFilePath, string fileName)
        {
            bool success = await BrickExplorer.UploadFile(localFilePath, Path, fileName);

            if (success)
            {
                return(await GetFile(fileName));
            }
            return(null);
        }
Example #7
0
        /// <summary>
        /// Uploads a file to this directory. Any existing file will not be overriden.
        /// </summary>
        /// <param name="file">The byte[] filedata of a lego file on the local machine or resource file</param>
        /// <param name="type">The type of the filedata</param>
        /// <param name="fileName">The renamed fileName as stored on brick excl. extension</param>
        /// <returns></returns>
        public async Task <File> UploadFile(byte[] file, FileType type, string fileName)
        {
            fileName = $"{System.IO.Path.GetFileNameWithoutExtension(fileName)}{File.GetExtension(type)}";
            bool success = await BrickExplorer.UploadFile(file, Path, fileName);

            if (success)
            {
                return(await GetFile(fileName));
            }
            return(null);
        }
Example #8
0
        /// <summary>
        /// Uploads a file to this directory
        /// </summary>
        /// <param name="stream">The stream to a lego file on the local machine or resource file</param>
        /// <param name="type">The type of the filedata</param>
        /// <param name="fileName">The renamed fileName as stored on brick excl. extension</param>
        /// <returns></returns>
        public async Task <File> UploadFile(System.IO.Stream stream, FileType type, string fileName)
        {
            fileName = System.IO.Path.ChangeExtension(fileName, File.GetExtension(type));
            bool success = await BrickExplorer.UploadFile(stream, Path, fileName);

            if (success)
            {
                return(await GetFile(fileName));
            }
            return(null);
        }
Example #9
0
        private async Task AdvancedTree(TreeNode parent)
        {
            Directory[] directories = await BrickExplorer.GetDirectories(((Directory)parent.Tag).Path);

            foreach (Directory directory in directories)
            {
                TreeNode node = ConvertToNode(directory);
                parent.Nodes.Add(node);
                await AdvancedTree(node);
            }
        }
 public async Task UpLoadFile(Directory directory, string[] paths)
 {
     labelAction.Text = "Uploading...";
     foreach (string path in paths)
     {
         labelContent.Text = path;
         if (BrickExplorer.IsRobotFile(path))
         {
             await directory.UploadFile(path);
         }
     }
     Close();
 }
        public async Task Initialize()
        {
            Directory root = await BrickExplorer.GetDirectory(BrickExplorer.ROOT_PATH);

            CURRENT_DIRECTORY  = root;
            SELECTED_DIRECTORY = root;
            await folderTree.Initialize(root);

            await previewPane.Initialize(root);

            navigationBar.Initialize();
            DirectoryContent content = await DirectoryContent.Get(root);

            directoryContentPane.Set(content);
            statusBar.Set(content.Info);

            if (UserSettings.Mode == Mode.BASIC)
            {
                Directory drive = await BrickExplorer.GetDirectory(BrickExplorer.PROJECTS_PATH);

                Execute(this, drive, ActionType.OPEN);
            }
        }
Example #12
0
 /// <summary>
 /// Downloads this file from the Brick and returns this file as byte[]
 /// </summary>
 /// <returns>byte[] data of the file</returns>
 public async Task <byte[]> Download()
 {
     return(await BrickExplorer.DownloadFile(Path));
 }
Example #13
0
 /// <summary>
 /// Deletes this lego robot file
 /// Use with care!!
 /// </summary>
 public async Task <bool> Delete()
 {
     return(await BrickExplorer.DeleteFile(Path));
 }
        public static async Task <DirectoryContent> Get(Directory directory)
        {
            DirectoryContent content = new DirectoryContent
            {
                Path = directory.Path,
                Info = new DirectoryInfo(),
            };

            switch (UserSettings.Mode)
            {
            case Mode.BASIC:
            {
                switch (directory.Path)
                {
                case BrickExplorer.ROOT_PATH:
                {
                    List <Directory> dirs = new List <Directory>();
                    dirs.Add(await BrickExplorer.GetDirectory(BrickExplorer.PROJECTS_PATH));
                    if (Manager.Brick.SDCard != null)
                    {
                        dirs.Add(await BrickExplorer.GetDirectory(BrickExplorer.SDCARD_PATH));
                    }
                    content.Directories    = dirs.ToArray();
                    content.Info.ItemCount = content.Directories.Length;
                    break;
                }

                case BrickExplorer.PROJECTS_PATH:
                {
                    content.Directories = await Manager.Brick.Drive.GetDirectories();

                    content.Info.ItemCount = content.Directories.Length;
                    break;
                }

                case BrickExplorer.SDCARD_PATH:
                {
                    content.Directories = await Manager.Brick.SDCard.GetDirectories();

                    content.Info.ItemCount = content.Directories.Length;
                    break;
                }

                default:
                {
                    //only get files
                    content.Files = await BrickExplorer.GetFiles(directory.Path);

                    foreach (File file in content.Files)
                    {
                        content.Info.TotalByteSize += file.Size;
                    }
                    content.Info.ItemCount = content.Files.Length;
                    break;
                }
                }
                return(content);
            }

            default:
            {
                Framework.Core.DirectoryContent dc = await BrickExplorer.GetDirectoryContent(directory.Path);

                content.Directories = dc.Directories;
                content.Files       = dc.Files;
                foreach (File file in content.Files)
                {
                    content.Info.TotalByteSize += file.Size;
                }
                content.Info.ItemCount = content.Directories.Length + content.Files.Length;
                return(content);
            }
            }
        }
Example #15
0
        public async Task <bool> Save()
        {
            errorProvider.Clear();
            if (string.IsNullOrWhiteSpace(textBoxFileName.Text))
            {
                errorProvider.SetError(textBoxFileName, "Filename is required");
                return(false);
            }

            string fileName  = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text.Trim());
            bool   isNewName = !fileName.Equals(originalFileName, StringComparison.InvariantCultureIgnoreCase);

            if (!TextHasChanged && !isNewName)
            {
                return(true);
            }

            //textbox returns lines as /n .. weird should ne /r/n
            //so hack work around
            string[] lines = richTextBox.Lines;

            string text = "-";

            if (lines?.Length > 0)
            {
                StringBuilder sb        = new StringBuilder();
                int           lineCount = lines.Length;
                for (int i = 0; i < lineCount; i++)
                {
                    if (i + 1 < lineCount)
                    {
                        sb.AppendLine(lines[i]);
                    }
                    else
                    {
                        sb.Append(lines[i]);
                    }
                }
                text = sb.ToString();
            }

            byte[] data = FileConverter.TexttoRTF(text);



            if (TextFile != null)
            {
                await BrickExplorer.UploadFile(data, Directory.Path, $"{fileName}.rtf");

                if (isNewName)
                {
                    await TextFile.Delete();
                }
            }
            else
            {
                await BrickExplorer.UploadFile(data, Directory.Path, $"{fileName}.rtf");
            }

            TextHasChanged   = false;
            RefreshDirectory = true;
            return(true);
        }
Example #16
0
        public async Task Refresh(Directory directory)
        {
            tree.BeginUpdate();

            TreeNode node = Find(directory);

            if (node != null)
            {
                Directory[] directories;
                switch (UserSettings.Mode)
                {
                case Mode.BASIC:
                {
                    switch (directory.Path)
                    {
                    case BrickExplorer.PROJECTS_PATH:
                    {
                        directories = await Manager.Brick.Drive.GetDirectories();

                        break;
                    }

                    case BrickExplorer.SDCARD_PATH:
                    {
                        directories = await Manager.Brick.SDCard.GetDirectories();

                        break;
                    }

                    default:
                    {
                        //Should never get here
                        throw new InvalidOperationException(nameof(tree));
                    }
                    }
                    break;
                }

                default:
                {
                    directories = await BrickExplorer.GetDirectories(directory.Path);

                    break;
                }
                }

                node.Nodes.Clear();
                foreach (Directory dir in directories)
                {
                    node.Nodes.Add(ConvertToNode(dir));
                }

                //int nodeCount = node.Nodes.Count;
                //int ni = 0;
                //for (int i = 0; i < dirs.Length; i++)
                //{
                //    if (i < nodeCount)
                //    {
                //        if (((Directory)node.Nodes[ni].Tag).Path != dirs[i].Path)
                //        {
                //            node.Nodes.Insert(i, ConvertToNode(dirs[i]));
                //            break;
                //        }
                //    }
                //    else
                //    {
                //        node.Nodes.Add(ConvertToNode(dirs[i]));
                //    }
                //    ni++;
                //}
            }
            tree.EndUpdate();
        }
Example #17
0
 /// <summary>
 /// Gets all files in this directory
 /// </summary>
 /// <returns><c>File[]</c></returns>
 public async Task <File[]> GetFiles()
 {
     return(await BrickExplorer.GetFiles(Path));
 }
Example #18
0
        /// <summary>
        /// Gets a file in this directory by file name
        /// </summary>
        /// <param name="fileName">filename incl. extension</param>
        /// <returns><c>File</c></returns>
        public async Task <File> GetFile(string fileName)
        {
            string path = $"{Path}{fileName}";

            return(await BrickExplorer.GetFile(path));
        }
        private async void Execute(object sender, File file, ActionType type)
        {
            switch (type)
            {
            case ActionType.OPEN:
            {
                if (CURRENT_FILE?.Path != file.Path)
                {
                    CURRENT_FILE  = file;
                    SELECTED_FILE = file;
                    await previewPane.Set(file);
                }
                Entry entry = new Entry(file.Path, EntryType.FILE);
                if (entry.IsOpenEnabled)
                {
                    switch (file.Type)
                    {
                    case FileType.SoundFile:
                    {
                        Manager.Brick.Sound.Play((SoundFile)file, 50);
                        break;
                    }

                    case FileType.TextFile:
                    {
                        using (TextFileEditor textFileEditor = new TextFileEditor())
                        {
                            await textFileEditor.Initialize((TextFile)file);

                            textFileEditor.Directory = CURRENT_DIRECTORY;
                            textFileEditor.ShowDialog();
                            if (textFileEditor.RefreshDirectory)
                            {
                                Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                            }
                        }
                        break;
                    }
                    }
                }
                break;
            }

            case ActionType.SELECT:
            {
                SELECTED_FILE = file;
                await previewPane.Set(file);

                break;
            }

            case ActionType.DOWNLOAD:
            {
                ((Manager)Parent).UseWaitCursor = true;
                await Download(SELECTED_FILE);

                ((Manager)Parent).UseWaitCursor = false;
                break;
            }

            case ActionType.UPLOAD_FILE:
            {
                await UpLoadFile();

                Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                break;
            }

            case ActionType.DELETE:
            {
                if (MessageBox.Show("Are you sure you want to permanently delete this file?", "Delete file", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    await BrickExplorer.DeleteFile(file.Path);

                    Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                }
                break;
            }

            case ActionType.NEW_RTF_FILE:
            {
                using (TextFileEditor textFileEditor = new TextFileEditor())
                {
                    await textFileEditor.Initialize();

                    textFileEditor.Directory = CURRENT_DIRECTORY;
                    textFileEditor.ShowDialog();
                    if (textFileEditor.RefreshDirectory)
                    {
                        Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                    }
                }
                break;
            }

            case ActionType.NEW_IMAGE_FILE:
            {
                await UploadImage();

                Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                break;
            }
            }
        }
        private async Task DownloadBasic(Directory directory, string toLocalPath)
        {
            labelAction.Text  = "Downloading...";
            labelContent.Text = directory.Path;
            string localPath = System.IO.Path.Combine(toLocalPath, directory.Name);

            switch (directory.Path)
            {
            case BrickExplorer.ROOT_PATH:
            {
                localPath = System.IO.Path.Combine(toLocalPath, Manager.Brick.Name);
                break;
            }

            case BrickExplorer.PROJECTS_PATH:
            {
                localPath = System.IO.Path.Combine(toLocalPath, "Drive");
                break;
            }

            case BrickExplorer.SDCARD_PATH:
            {
                localPath = System.IO.Path.Combine(toLocalPath, "SDCard");
                break;
            }
            }

            System.IO.Directory.CreateDirectory(localPath);

            if (directory.Path != BrickExplorer.ROOT_PATH)
            {
                File[] files = await BrickExplorer.GetFiles(directory.Path);

                foreach (File file in files)
                {
                    labelContent.Text = file.Path;
                    byte[] data = await file.Download();

                    if (data != null && data.Length > 0)
                    {
                        string filePath = System.IO.Path.Combine(localPath, file.FileName);
                        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                        {
                            fs.Write(data, 0, data.Length);
                        }
                    }
                }
            }

            switch (directory.Path)
            {
            case BrickExplorer.ROOT_PATH:
            {
                string drivePath = System.IO.Path.Combine(localPath, "Drive");
                System.IO.Directory.CreateDirectory(drivePath);
                foreach (Directory dir in await Manager.Brick.Drive.GetDirectories())
                {
                    await DownloadBasic(dir, drivePath);
                }
                if (Manager.Brick.SDCard != null)
                {
                    string sdPath = System.IO.Path.Combine(localPath, "SDCard");
                    System.IO.Directory.CreateDirectory(sdPath);
                    foreach (Directory dir in await Manager.Brick.SDCard.GetDirectories())
                    {
                        await DownloadBasic(dir, sdPath);
                    }
                }
                break;
            }

            case BrickExplorer.PROJECTS_PATH:
            {
                foreach (Directory dir in await Manager.Brick.Drive.GetDirectories())
                {
                    await DownloadBasic(dir, localPath);
                }
                break;
            }

            case BrickExplorer.SDCARD_PATH:
            {
                foreach (Directory dir in await Manager.Brick.SDCard.GetDirectories())
                {
                    await DownloadBasic(dir, localPath);
                }
                break;
            }
            }
        }
        private async void Execute(object sender, Directory directory, ActionType type)
        {
            switch (type)
            {
            case ActionType.OPEN:
            {
                if (CURRENT_DIRECTORY.Path == directory.Path)
                {
                    return;                                                   // do not open again is already open
                }
                CURRENT_DIRECTORY  = directory;
                SELECTED_DIRECTORY = directory;
                statusBar.SetLoading();
                ((Manager)Parent).UseWaitCursor = true;
                DirectoryContent content = await DirectoryContent.Get(directory);

                previewPane.Set(directory, content.Info);
                directoryContentPane.Set(content);
                statusBar.Set(content.Info);
                navigationBar.Set(directory);
                ((Manager)Parent).UseWaitCursor = false;
                break;
            }

            case ActionType.SELECT:
            {
                SELECTED_DIRECTORY = directory;
                previewPane.Set(directory, null);
                break;
            }

            case ActionType.DOWNLOAD:
            {
                ((Manager)Parent).UseWaitCursor = true;
                await Download(SELECTED_DIRECTORY);

                ((Manager)Parent).UseWaitCursor = false;
                break;
            }

            case ActionType.CREATE_DIRECTORY:
            {
                using (CreateEntryDialog dialog = new CreateEntryDialog(CURRENT_DIRECTORY, EntryType.DIRECTORY, null))
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        string path = System.IO.Path.Combine(CURRENT_DIRECTORY.Path, dialog.EntryName);
                        await BrickExplorer.CreateDirectory(path);

                        await folderTree.Refresh(CURRENT_DIRECTORY);

                        Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                    }
                }
                break;
            }

            case ActionType.DELETE:
            {
                if (MessageBox.Show("Are you sure you want to permanently delete this directory and all it's content?", "Delete directory", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    ((Manager)Parent).UseWaitCursor = true;
                    await BrickExplorer.DeleteDirectory(directory.Path, true);

                    await folderTree.Refresh(CURRENT_DIRECTORY);

                    Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                    ((Manager)Parent).UseWaitCursor = false;
                }
                break;
            }

            case ActionType.REFRESH_DIRECTORY:
            {
                statusBar.SetLoading();
                ((Manager)Parent).UseWaitCursor = true;
                DirectoryContent content = await DirectoryContent.Get(CURRENT_DIRECTORY);

                directoryContentPane.Set(content);
                previewPane.Set(directory, content.Info);
                statusBar.Set(content.Info);
                ((Manager)Parent).UseWaitCursor = false;
                break;
            }

            case ActionType.UPLOAD_DIRECTORY:
            {
                await UploadDirectory();

                await folderTree.Refresh(CURRENT_DIRECTORY);

                Execute(this, CURRENT_DIRECTORY, ActionType.REFRESH_DIRECTORY);
                break;
            }
            }
        }