コード例 #1
0
ファイル: MainGUI.cs プロジェクト: trailmax/cacheCopy
        /// <summary>
        /// Replaces placeholders in file naming pattern and displays the sample below the pattern field
        /// </summary>
        private void DisplaySamplePattern()
        {
            string pattern = GetFileNamingPattern();

            if (pattern == String.Empty)
            {
                lblPatternTranslationSample.Text = "";
                return;
            }



            if (FileNaming.isPatternValid(pattern))
            {
                lblPatternTranslationSample.Text = FileNaming.GenerateSampleFileName(pattern);
            }
            else
            {
                lblPatternTranslationSample.Text = "Pattern is not valid";
            }
        }
コード例 #2
0
ファイル: Core.cs プロジェクト: trailmax/cacheCopy
        /// <summary>
        /// Executes the main routine: reads information from form
        /// and accordingly to the settings copies the files from one place to another
        /// </summary>
        public void executeMainRoutine(BackgroundWorker worker, DoWorkEventArgs e)
        {
            try
            {
                mainGUI.setProgressLabel("Reading files");

                List <String> errors = new List <string>();

                string source = mainGUI.GetSourceFolder();
                readFiles(source);

                // read data about filters from GUI and set the filters in Core
                setFilters();

                mainGUI.setProgressLabel("Filtering files");
                // apply filters to files and save error messages from there
                applyFilters(worker, e);

                int totalFilesCopied = 0;
                // finally copy the files
                string targetFolder = mainGUI.GetTargetFolder();
                for (int i = 0; i < files.Count; i++)
                {
                    FileInfo file = files[i];
                    mainGUI.setProgressLabel("Copying files: " + i.ToString() + "/" + files.Count.ToString());
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        errors.Add("Cancelled by user");
                        break;
                    }

                    // now make a new filename, according to the parameters provided by user
                    string paddedNumber = Util.PadNumberToMaximum(i, files.Count);
                    string newPath      = FileNaming.GenerateFileName(file, mainGUI.GetFileNamingPattern(), mainGUI.IsAllowOverwriteFiles(), targetFolder, paddedNumber);

                    try
                    {
                        file.CopyTo(newPath);
                        totalFilesCopied++;

                        // if user checked the box for file deletion, try to remove the file.
                        if (mainGUI.IsRemoveImagesFromCache())
                        {
                            file.Delete();
                        }
                    }
                    catch (IOException)
                    {
                        errors.Add("Could not copy or delete file: " + file.Name);
                    }

                    // update progress bar
                    reportProgress(files.Count, i, worker);
                }
                mainGUI.setProgressLabel("");
                //return errors;
                e.Result = totalFilesCopied;
            }
            catch (Exception ex)
            {
                Util.WriteToLogFile(ex);
                throw;
            }
        }
コード例 #3
0
ファイル: MainGUI.cs プロジェクト: trailmax/cacheCopy
        /// <summary>
        /// Determines whether the form has valid user input.
        /// Returns true if all the validations are fine.
        /// False if some controls fail to validate.
        /// List of messages passed in will be filled in with the error messages from
        /// the faulty validation
        /// </summary>
        /// <param name="messages">The list of error messages - will be populated on faulty validation</param>
        /// <returns>
        ///   <c>true</c> if the form is valid; otherwise, <c>false</c>.
        /// </returns>
        private Boolean IsValid(ref List <string> messages)
        {
            bool isValid = true;

            // if target folder does not exist, ask user if need to create it
            if (!Directory.Exists(targetFolderName.Text))
            {
                ConfirmAndCreateFolder(targetFolderName.Text);
            }

            // validate for TargetFolder
            if (!Directory.Exists(targetFolderName.Text))
            {
                TargetFolderErrorProvider.SetError(targetFolderName, "Folder does not exist");
                isValid = false;
                messages.Add("Target folder does not exist");
                tabControl1.SelectTab(0);
            }
            else
            {
                TargetFolderErrorProvider.SetError(targetFolderName, "");
            }

            // validate for source folder if manual selection
            if (ManualSelectionRadioButton.Checked && !Directory.Exists(SourceFolderName.Text))
            {
                ManualSelectionErrorProvider.SetError(SourceFolderName, "Directory does not exist");
                isValid = false;
                messages.Add("Source folder does not exists");
                tabControl1.SelectTab(0);
            }
            else
            {
                ManualSelectionErrorProvider.SetError(SourceFolderName, "");
            }

            if (chbxFileNamingPattern.Checked && !FileNaming.isPatternValid(GetFileNamingPattern()))
            {
                FilePatternErrorProvider.SetError(txtFileNamingPattern, "Pattern contains invalid characters or not valid");
                messages.Add("File naming pattern contains invalid characters or not valid");
                isValid = false;
                //switch to second tab and focus on text field for file pattern
                tabControl1.SelectTab(1);
                txtFileNamingPattern.Focus();
            }
            else
            {
                FilePatternErrorProvider.SetError(txtFileNamingPattern, "");
            }

            // at least one file type must be enabled
            if (!IsJPG() && !IsPNG() && !IsGIF())
            {
                ImageTypeErrorProvider.SetError(chbxGif, "At least one file type must be checked");
                messages.Add("At least one file type must be checked, otherwise nothing will be copied");
                isValid = false;
                tabControl1.SelectTab(0);
            }
            else
            {
                ImageTypeErrorProvider.SetError(chbxGif, "");
            }


            return(isValid);
        }