Exemplo n.º 1
0
        public void ExtractFilesToWorkspace(string templatePath)
        {
            if (!File.Exists(templatePath))
            {
                MessageBox.Show(String.Format("{0}\r\n{1}",
                                              Resources.TemplateNotFound, templatePath),
                                Resources.TemplateCreationError,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                return;
            }

            string workspace = Directory.GetCurrentDirectory();

            /*
             * We don't want to overwrite existing files when
             * extracting zip archives. We warn the user if there
             * are files present in the workspace and if the template
             * zip doesn't start with the word "New ".
             * The convention here is that a template archive containing
             * files to be renamed will start with 'New'. If not
             * the files will be deployed as they are.
             */

            int    fileCount = Directory.GetFiles(workspace, "*.*").Length;
            string fileName  = Path.GetFileName(templatePath);

            bool projectHasUntitledFiles = fileName.StartsWith("New ");

            /*
             * If template doesn't have untitled files, template contents
             * will not be given uniques names and could therefore
             * overwrite files already present in the folder. Give a
             * warning if this could happen.
             */

            if (fileCount > 0 && !projectHasUntitledFiles)
            {
                if (MessageBox.Show(String.Format("{0}\r\n{1}\r\n{2}",
                                                  Resources.TemplateOverwriteWarningMessage1,
                                                  Resources.TemplateOverwriteWarningMessage2,
                                                  Resources.TemplateOverwriteWarningMessage3),
                                    Resources.TemplateOverwriteWarningTitle,
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Warning) == DialogResult.No)
                {
                    return;
                }
            }

            string basename = _documentManager.GetNextUntitledFileBasename();

            /*
             * If we're going to be renaming untitled files get the
             * basename for the new file(s) from the user. We need to do
             * this before unzipping the file in case the user cancels.
             */

            if (projectHasUntitledFiles)
            {
                NewFilenameForm nff = new NewFilenameForm(
                    basename,
                    Path.GetFileNameWithoutExtension(fileName));

                if (nff.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                basename = nff.Filename;

                /*
                 * basename may already exist but checks later on will prevent
                 * existing files from being overwritten. File will get next
                 * default name instead of name provided. Can't check to see if
                 * it exists at this stage as we don't know what the actual file
                 * names will be until we unzip the template.
                 */
            }

            /*
             * Extract the files.
             */

            FastZip fz = new FastZip();

            fz.CreateEmptyDirectories = true;
            fz.ExtractZip(templatePath, workspace, ".");

            if (projectHasUntitledFiles)
            {
                /*
                 * Rename any "__untitled__" files.
                 */

                string[] files = Directory.GetFiles(
                    workspace, Constants.UNTITLED_FILENAME_TEMPLATE + ".*");

                foreach (string file in files)
                {
                    string newname = file.Replace(
                        Constants.UNTITLED_FILENAME_TEMPLATE,
                        basename);

                    /*
                     * Make sure we don't already have this name.
                     */

                    while (File.Exists(newname))
                    {
                        basename =
                            _documentManager.GetNextUntitledFileBasename();

                        newname = file.Replace(
                            Constants.UNTITLED_FILENAME_TEMPLATE,
                            basename);
                    }

                    /*
                     * Update the filename.
                     */

                    try
                    {
                        FileTools.ChangeFileName(file, newname);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("{0}\r\n{1}",
                                                      Resources.TemplateFileRenameError,
                                                      ex.Message),
                                        Resources.TemplateCreationError,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }

                    /*
                     * Update the content to include the new filename.
                     */

                    try
                    {
                        // Detect encoding from BOM, assume ANSI if none found
                        StreamReader sr       = new StreamReader(newname, Encoding.ASCII, true);
                        String       content  = sr.ReadToEnd();
                        Encoding     encoding = sr.CurrentEncoding;
                        sr.Close();

                        content = content.Replace(
                            Constants.UNTITLED_FILENAME_TEMPLATE, basename);

                        // Rewrite the file using the detected encoding
                        StreamWriter sw = new StreamWriter(newname, false, encoding);
                        sw.Write(content);
                        sw.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("{0}\r\n{1}",
                                                      Resources.TemplateContentUpdateError,
                                                      ex.Message),
                                        Resources.TemplateCreationError,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                }
            }

            /*
             * Refresh any file system views.
             */

            _applicationManager.NotifyFileSystemChange();
        }