private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // If we quit from the 'More Options' screen, apply the UI settings
            if (Content is MoreOptions)
            {
                ((MoreOptions)Content).ApplySettingsToUserSettings();
            }

            // Write the settings
            ProjectFileInterface.WriteSettingsToFile();

            // Check if any projects have incomplete logs
            if (UserSettings.DisplayIncompleteLogWarning)
            {
                foreach (var project in ProjectOrganizer.Projects)
                {
                    // If the project has an incomplete log, ask the user if they want to complete it
                    if (project.IncompleteLog != null)
                    {
                        ConfirmationDialog incLogDialog = new ConfirmationDialog(string.Format(
                            "Project '{0}' has an incomplete log. Do you wish to complete it?", project.Name));

                        // If they do, finish it
                        if (incLogDialog.ShowDialog() == true)
                        {
                            project.FinishIncompleteLog();
                        }
                    }
                }
            }

            // Write the projects
            ProjectFileInterface.WriteAllProjectData();

            // If errors occured, tell the user
            if (ErrorLogger.ErrorsOccured)
            {
                NotificationDialog window = new NotificationDialog("Errors Occured",
                    string.Format("Errors occured during this session. Please view the log file at\n\n{0}\n\nto see which errors occurred.", ErrorLogger.ErrorLogFilename));
                window.Show();
            }
        }
Esempio n. 2
0
        //------------------//
        // External Methods //
        //------------------//
        /// <summary>
        /// Adds the given string to the log file. Automatically adds a timestamp.
        /// </summary>
        /// <param name="log">The log string to add.</param>
        public static void AddLog(string log, ErrorSeverity severity,
            [CallerMemberName] string currentFunction = "", 
            [CallerFilePath] string currentFile = "",
            [CallerLineNumber] int line = 0)
        {
            try
            {
                // Note that errors have occured
                ErrorsOccured = true;

                // Get the date time as a string and the current file contents
                string dateTimeNowString = DateTime.Now.ToString();
                string oldFileContents = File.ReadAllText(errorLogFilename);

                // Build the new file contents
                StringBuilder newFileContents = new StringBuilder();
                string lineOfDashes = new String('-', 50);

                // Header
                newFileContents.Append(string.Format("{0}\n", lineOfDashes));
                newFileContents.Append(dateTimeNowString);
                newFileContents.Append("\n");
                newFileContents.Append(log);
                newFileContents.Append(string.Format("\nError Severity: {0}\n", errorSeverityToStringMap[severity]));

                // Immediate caller
                newFileContents.Append("\nImmediate Context:\n");
                newFileContents.Append(string.Format("    Method {0}()\n", currentFunction));
                newFileContents.Append(string.Format("    in {0}\n", currentFile));
                newFileContents.Append(string.Format("    @ line {0}\n", line));

                // Full callstack
                newFileContents.Append("\nCallstack:\n");
                newFileContents.Append(Environment.StackTrace);

                // End
                newFileContents.Append(string.Format("\n{0}\n\n", lineOfDashes));
                newFileContents.Append(oldFileContents);

                // Write the file
                File.WriteAllText(errorLogFilename, newFileContents.ToString());

                // If we're in debug mode, pop up an error message
                if (UserSettings.DebugModeOn)
                {
                    NotificationDialog window = new NotificationDialog("Error",
                        string.Format("This program experienced an error:\n{0}", log));
                    window.Show();
                }
            }
            catch (IOException e)
            {
                // If we get here, something went pretty wrong. Let the user know.
                NotificationDialog window = new NotificationDialog("Error",
                    string.Format("This program encountered an error, and the error couldn't get logged.\n\nInitial error:\n{0}\n\nError log write error:\n{1}\n\n" +
                        "If this problem persists, please contact us and let us know that it's happening.", log, e.Message));

                window.Show();
            }
        }
        /// <summary>
        /// Backup the user's data to the backup directory.
        /// </summary>
        private void BackupData()
        {
            // Get the id for this backup
            int id = ProjectFileInterface.RequestBackupId();
            if (id <= -1)
            {
                NotificationDialog errorDialog = new NotificationDialog("Backup Failed",
                    "Error creating new id for this backup. Please check the log file for more details.");
                errorDialog.ShowDialog();
                return;
            }

            // Create the backup dirs
            string backupDir = Path.Combine(DataDirectory, string.Format("backup\\backup_{0}", id));
            Directory.CreateDirectory(backupDir);

            // Write all project data to make sure that it's current
            ProjectFileInterface.WriteAllProjectData();

            // Copy the data to the backup location
            CopyDirectoryRecursively(DataDirectory, backupDir, true);

            // Delete any backups that we copied
            string copiedBackupsDir = Path.Combine(backupDir, "backup");
            Directory.Delete(copiedBackupsDir, true);

            // Let the user know that the operation succeeded
            NotificationDialog dialog = new NotificationDialog("Backup Successful", string.Format(
                "Backup to\n\n{0}\n\nwas successful.", backupDir));
            dialog.ShowDialog();
        }
        /// <summary>
        /// Change's the directory for the project data.
        /// </summary>
        /// <param name="newDir">The new directory for the data.</param>
        private void ChangeDataDirectory(string newDir)
        {
            string oldDir = DataDirectory;

            // Check to see if new dir is empty
            if (Directory.EnumerateFiles(newDir).Any())
            {
                // If it's not empty, warn the user that overwrites may occur
                ConfirmationDialog dialog = new ConfirmationDialog(string.Format(
                    "{0} is not empty. Any files it contains may be overwritten. Proceed?", newDir));
                if (dialog.ShowDialog() != true)
                {
                    // If the user opted not to proceed, abort the operation
                    return;
                }
            }

            // Make sure all the project notes are saved before we copy
            NotificationDialog notifDialog = new NotificationDialog("Save Data", "Please save all project notes files. Press OK when all files are saved.");
            notifDialog.ShowDialog();

            // Write all project data to make sure that it's current
            ProjectFileInterface.WriteAllProjectData();

            // Copy the data to the new directory
            CopyDirectoryRecursively(oldDir, newDir, true);

            // At the end, set the new directory and rename the files in PFI
            DataDirectory = newDir;
            UserSettings.DataDirectory = newDir;
            ProjectFileInterface.RenameFullFilenamesForDataDirectoryChange();

            // Let the user know that the operation was successful
            notifDialog = new NotificationDialog("Success", "Data directory successfully changed.");
            notifDialog.ShowDialog();
        }