Пример #1
0
        internal static void SetNewFavouriteFile(Form1 form, String pathAndFileName)
        {
            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;

            String fileContent;

            if (FavouriteManager.IsFileFavourite(pathAndFileName, out fileContent))
            {
                WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("AlreadyFavourite", className));
                return;
            }

            fileContent = fileContent + pathAndFileName + Environment.NewLine;

            SaveFileList(ConstantUtil.fvFile, fileContent);
            LoadFavouriteFiles(form, true);

            String addedFavourite;

            if (pathAndFileName.StartsWith(ConstantUtil.sessionPrefix))
            {
                pathAndFileName = pathAndFileName.Substring(ConstantUtil.sessionPrefix.Length);

                FileInfo fileInfo = new FileInfo(pathAndFileName);
                addedFavourite = String.Format(LanguageUtil.GetCurrentLanguageString("AddedFavourite", className), fileInfo.Name);
            }
            else if (pathAndFileName.StartsWith(ConstantUtil.urlPrefix))
            {
                pathAndFileName = pathAndFileName.Substring(ConstantUtil.urlPrefix.Length);
                addedFavourite  = String.Format(LanguageUtil.GetCurrentLanguageString("AddedFavouriteUrl", className), pathAndFileName);
            }
            else
            {
                FileInfo fileInfo = new FileInfo(pathAndFileName);
                addedFavourite = String.Format(LanguageUtil.GetCurrentLanguageString("AddedFavourite", className), fileInfo.Name);
            }

            toolStripStatusLabel.Text = addedFavourite.EndsWith(".") ? addedFavourite.Substring(0, addedFavourite.Length - 1) : addedFavourite;
            WindowManager.ShowInfoBox(form, addedFavourite);
        }
Пример #2
0
        internal static int OpenFile(Form1 form, int tabIdentity, String[] specificFileNames, bool showMessages, bool saveNewRecentFile, out List <String> errors)
        {
            CustomXtraTabControl pagesTabControl      = form.pagesTabControl;
            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;
            ToolStripProgressBar toolStripProgressBar = form.toolStripProgressBar;
            OpenFileDialog       openFileDialog       = form.openFileDialog;

            bool isWindowsHostsFile = false;
            int  localTabIdentity   = tabIdentity;

            errors = new List <String>();

            openFileDialog.InitialDirectory = FileUtil.GetInitialFolder(form);
            openFileDialog.Multiselect      = true;
            SetFileDialogFilter(openFileDialog);

            TrayManager.RestoreFormIfIsInTray(form);

            try
            {
                String[] fileNames;

                if (specificFileNames == null || specificFileNames.Length <= 0)
                {
                    if (openFileDialog.ShowDialog() != DialogResult.OK)
                    {
                        return(tabIdentity);
                    }

                    fileNames = openFileDialog.FileNames;
                }
                else
                {
                    fileNames = specificFileNames;
                }

                //Verify if file is a DtPad session
                if (fileNames.Length == 1 && fileNames[0].EndsWith(".dps"))
                {
                    SessionManager.OpenSession(form, fileNames[0]);
                    return(form.TabIdentity);
                }

                Application.DoEvents();
                toolStripProgressBar.Value = 0;

                foreach (String fileName in fileNames)
                {
                    //Verify if file is Windows hosts file
                    if (fileName.Contains(@"drivers\etc\hosts"))
                    {
                        if (!SystemUtil.IsUserAdministrator())
                        {
                            WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("UserNotAdmin", className));
                        }

                        isWindowsHostsFile = true;
                    }

                    if (!showMessages)
                    {
                        if (!File.Exists(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileNotExists", className), fileName));
                            continue;
                        }
                        if (FileUtil.IsFileInUse(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileInUse", className), fileName));
                            continue;
                        }
                        if (FileUtil.IsFileTooLargeForDtPad(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileTooHeavy", className), fileName));
                            continue;
                        }
                    }
                    else if (!File.Exists(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileNotExisting", className), fileName));
                        continue;
                    }
                    else if (FileUtil.IsFileInUse(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileInUse", className), fileName));
                        continue;
                    }
                    if (FileUtil.IsFileTooLargeForDtPad(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileTooHeavy", className), fileName));
                        continue;
                    }

                    //Cycle and check if the file is already open, in which case I select its tab and continue with the next one
                    XtraTabPage tabPage;
                    if (FileUtil.IsFileAlreadyOpen(form, fileName, out tabPage))
                    {
                        pagesTabControl.SelectedTabPage = tabPage;
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.Visible = false;
                        continue;
                    }

                    //Verify if file is an archive
                    try
                    {
                        ZipFile file = null;
                        bool    isZipFile;

                        try
                        {
                            file      = new ZipFile(fileName);
                            isZipFile = file.TestArchive(false, TestStrategy.FindFirstError, form.Zip_Errors);
                        }
                        finally
                        {
                            if (file != null)
                            {
                                file.Close();
                            }
                        }

                        if (isZipFile)
                        {
                            WindowManager.ShowZipExtract(form, fileName);
                            continue;
                        }
                    }
                    catch (ZipException)
                    {
                    }

                    toolStripProgressBar.Visible = true;
                    toolStripProgressBar.PerformStep();

                    String   fileContents;
                    Encoding fileEncoding;
                    bool     anonymousFile = false;

                    //Verify if file is a PDF
                    if (fileName.EndsWith(".pdf"))
                    {
                        bool success;
                        fileContents = PdfUtil.ExtractText(fileName, out success);

                        if (!success)
                        {
                            WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("InvalidPdf", className));
                            return(tabIdentity);
                        }

                        fileEncoding  = EncodingUtil.GetDefaultEncoding();
                        anonymousFile = true;
                    }
                    else
                    {
                        fileContents = FileUtil.ReadToEndWithEncoding(fileName, out fileEncoding);
                    }

                    bool favouriteFile = FavouriteManager.IsFileFavourite(fileName);
                    if (!favouriteFile && saveNewRecentFile)
                    {
                        ConfigUtil.UpdateParameter("LastUserFolder", Path.GetDirectoryName(fileName));
                        FileListManager.SetNewRecentFile(form, fileName);
                    }
                    toolStripProgressBar.PerformStep();

                    CustomRichTextBox pageTextBox = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);
                    if (!String.IsNullOrEmpty(pageTextBox.Text) || !String.IsNullOrEmpty(ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage)))
                    {
                        localTabIdentity = TabManager.AddNewPage(form, localTabIdentity);
                    }
                    toolStripProgressBar.PerformStep();

                    //Row number check
                    WindowManager.CheckLineNumbersForTextLenght(form, fileContents);

                    FileInfo fileInfo = new FileInfo(fileName);

                    pageTextBox = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);

                    //Verify if file is a tweet file
                    if (fileName.EndsWith(".tweet") && !ColumnRulerManager.IsPanelOpen(form))
                    {
                        ColumnRulerManager.TogglePanel(form);
                    }

                    pageTextBox.Text           = fileContents.Replace(Environment.NewLine, ConstantUtil.newLine);
                    pageTextBox.CustomOriginal = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage).Text.GetHashCode().ToString();
                    pageTextBox.CustomEncoding = fileEncoding.CodePage.ToString();

                    if (!anonymousFile)
                    {
                        String fileNameWithoutPath = Path.GetFileName(fileName);

                        pageTextBox.CustomModified = false;
                        ProgramUtil.SetFilenameTabPage(pagesTabControl.SelectedTabPage, fileName);
                        pagesTabControl.SelectedTabPage.ImageIndex   = fileInfo.IsReadOnly ? 2 : 0;
                        pagesTabControl.SelectedTabPage.Text         = fileNameWithoutPath;
                        pagesTabControl.SelectedTabPage.Tooltip      = fileName;
                        pagesTabControl.SelectedTabPage.TooltipTitle = fileNameWithoutPath;
                        form.Text = String.Format("DtPad - {0}", fileNameWithoutPath);
                        TabManager.ToggleTabFileTools(form, true);
                    }
                    else
                    {
                        pageTextBox.CustomModified = true;
                    }

                    toolStripStatusLabel.Text = String.Format("{0} \"{1}\" {2}", LanguageUtil.GetCurrentLanguageString("File", className), Path.GetFileName(fileName), LanguageUtil.GetCurrentLanguageString("Opened", className));
                    toolStripProgressBar.PerformStep();

                    tabIdentity = localTabIdentity;

                    if (!String.IsNullOrEmpty(fileInfo.Extension) && ConfigUtil.GetStringParameter("AutoFormatFiles").Contains(fileInfo.Extension))
                    {
                        FormatManager.FormatXml(form);
                    }

                    if (ConfigUtil.GetBoolParameter("AutoOpenHostsConfigurator") && isWindowsHostsFile)
                    {
                        pagesTabControl.SelectedTabPage.Appearance.Header.ForeColor = ConfigUtil.GetColorParameter("ColorHostsConfigurator");
                        CustomFilesManager.OpenHostsSectionPanel(form);
                        isWindowsHostsFile = false;
                    }
                }
            }
            catch (Exception exception)
            {
                TabManager.ToggleTabFileTools(form, false);

                toolStripProgressBar.Visible = false;
                toolStripProgressBar.Value   = 0;

                if (showMessages)
                {
                    WindowManager.ShowErrorBox(form, exception.Message, exception);
                }
            }
            finally
            {
                toolStripProgressBar.Visible = false;
                toolStripProgressBar.Value   = 0;
            }

            return(tabIdentity);
        }
Пример #3
0
        private static bool SaveFile(bool saveNewRecentFile, Form1 form, bool forceSaveAs, bool forceBackup, bool savingAll = false)
        {
            XtraTabControl       pagesTabControl      = form.pagesTabControl;
            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;
            ToolStripProgressBar toolStripProgressBar = form.toolStripProgressBar;
            SaveFileDialog       saveFileDialog       = form.saveFileDialog;

            try
            {
                String fileName;

                if (String.IsNullOrEmpty(ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage)) || forceSaveAs)
                {
                    saveFileDialog.InitialDirectory = FileUtil.GetInitialFolder(form);
                    SetFileDialogFilter(saveFileDialog);

                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.End);   //Useful to save all execution
                    DialogResult saveNewResult = saveFileDialog.ShowDialog();
                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.Begin); //Useful to save all execution

                    if (saveNewResult != DialogResult.OK)
                    {
                        toolStripProgressBar.Visible = false;
                        return(false);
                    }

                    fileName = saveFileDialog.FileName;
                }
                else
                {
                    fileName = ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage);
                }

                //Check that fileName is not already opened into another tab
                foreach (XtraTabPage tabPage in pagesTabControl.TabPages)
                {
                    if (tabPage == pagesTabControl.SelectedTabPage || ProgramUtil.GetFilenameTabPage(tabPage) != fileName)
                    {
                        continue;
                    }

                    pagesTabControl.SelectedTabPage = tabPage;

                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.End);   //Useful to save all execution
                    WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("FileAlreadyOpen", className));
                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.Begin); //Useful to save all execution

                    return(false);
                }

                bool favouriteFile = FavouriteManager.IsFileFavourite(fileName);

                Application.DoEvents();
                toolStripProgressBar.Value   = 0;
                toolStripProgressBar.Visible = true;

                toolStripProgressBar.PerformStep();

                if (!favouriteFile)
                {
                    ConfigUtil.UpdateParameter("LastUserFolder", Path.GetDirectoryName(fileName));
                }

                FileInfo fileInfo = new FileInfo(fileName);
                if (fileInfo.IsReadOnly && fileInfo.Exists)
                {
                    toolStripProgressBar.Visible = false;

                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.End);   //Useful to save all execution
                    WindowManager.ShowInfoBox(form, LanguageUtil.GetCurrentLanguageString("SavingReadonly", className));
                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.Begin); //Useful to save all execution

                    return(SaveFile(form, true));
                }

                bool backupConfigActive = ConfigUtil.GetBoolParameter("BackupEnabled");
                if ((!String.IsNullOrEmpty(ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage)) && !forceSaveAs) && (forceBackup || backupConfigActive))
                {
                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.End);   //Useful to save all execution
                    bool saved = BackupFileOnSaving(form, fileName);
                    TabsUpdate(pagesTabControl, savingAll, UpdatePhase.Begin); //Useful to save all execution

                    if (saved == false)
                    {
                        toolStripProgressBar.Visible = false;
                        return(false);
                    }
                }

                toolStripProgressBar.PerformStep();
                if (SaveFileCoreWithEncoding(form, fileName, savingAll) == false)
                {
                    toolStripProgressBar.Visible = false;
                    return(false);
                }

                if (!favouriteFile && saveNewRecentFile)
                {
                    FileListManager.SetNewRecentFile(form, fileName);
                }

                if (CustomFilesManager.IsHostsSectionPanelOpen(form))
                {
                    CustomFilesManager.GetSections(form, false);
                }
                toolStripProgressBar.PerformStep();

                CustomRichTextBox pageTextBox = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);

                ProgramUtil.SetFilenameTabPage(pagesTabControl.SelectedTabPage, fileName);
                pagesTabControl.SelectedTabPage.ImageIndex = 0;
                pagesTabControl.SelectedTabPage.Text       = Path.GetFileName(fileName);
                pageTextBox.CustomModified = false;
                pageTextBox.CustomOriginal = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage).Text.GetHashCode().ToString();
                form.Text = String.Format("DtPad - {0}", Path.GetFileName(fileName));
                toolStripStatusLabel.Text = String.Format("{0} \"{1}\" {2}", LanguageUtil.GetCurrentLanguageString("File", className), Path.GetFileName(fileName), LanguageUtil.GetCurrentLanguageString("Saved", className));
                TabManager.ToggleTabFileTools(form, true);

                toolStripProgressBar.PerformStep();
                toolStripProgressBar.Visible = false;
            }
            catch (Exception exception)
            {
                TabManager.ToggleTabFileTools(form, false);
                toolStripProgressBar.Visible = false;

                TabsUpdate(pagesTabControl, savingAll, UpdatePhase.End);   //Useful to save all execution
                WindowManager.ShowErrorBox(form, exception.Message, exception);
                TabsUpdate(pagesTabControl, savingAll, UpdatePhase.Begin); //Useful to save all execution

                return(false);
            }

            return(true);
        }