Beispiel #1
0
        /// <summary>
        /// Handles the <see cref="ButtonBase.Click"/> event for the "Find" <see cref="Button"/>.
        /// </summary>
        /// <param name="sender">
        /// The <see cref="Object"/> where the event handler is attached.</param>
        /// <param name="args">
        /// A <see cref="RoutedEventArgs"/> object containing event data.</param>
        /// <remarks><para>
        /// <b>OnMapiFind</b> invokes <see cref="MapiMail.ResolveName"/> to retrieve the Simple MAPI
        /// address book data for the player name shown in the "Name" text box.
        /// </para><para>
        /// On success, <b>OnMapiFind</b> sets the name and e-mail address of the selected <see
        /// cref="HumanPlayer"/> to the retrieved name and e-mail address, and sets the <see
        /// cref="DataChanged"/> flag if any data was changed.
        /// </para><para>
        /// <b>OnMapiFind</b> shows an error message if <see cref="MapiMail.ResolveName"/> fails,
        /// and returns silently if the user cancelled any dialog that was shown.</para></remarks>

        private void OnMapiFind(object sender, RoutedEventArgs args)
        {
            args.Handled = true;

            // retrieve selected human player, if any
            HumanPlayer human = SelectedHumanPlayer;

            if (human == null)
            {
                return;
            }

            // do nothing if name is empty
            if (NameBox.Text.Length == 0)
            {
                return;
            }

            MapiAddress resolved;

            try {
                // ask Simple MAPI to resolve player name
                resolved = MapiMail.ResolveName(NameBox.Text);
            }
            catch (MapiException e) {
                // show error message unless user cancelled
                if (e.Code != MapiException.Abort)
                {
                    MessageDialog.Show(this,
                                       Global.Strings.DialogMailErrorMapi, Global.Strings.TitleMailErrorMapi,
                                       e, MessageBoxButton.OK, Images.Warning);
                }
                return;
            }

            // check for broken MAPI client
            if (!resolved.Address.IsValidEmail())
            {
                MessageBox.Show(this,
                                Global.Strings.DialogMailInvalidMapi, Global.Strings.TitleMailInvalid,
                                MessageBoxButton.OK, MessageBoxImage.Information);

                resolved = new MapiAddress(resolved.Name, "");
            }

            // broadcast data changes, if any
            if (human.Name != resolved.Name || human.Email != resolved.Address)
            {
                NameBox.Text = human.Name = resolved.Name;
                human.Email  = resolved.Address;
                UpdatePlayers();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handles the <see cref="ButtonBase.Click"/> event for the "Browse Address Book" <see
        /// cref="Button"/>.</summary>
        /// <param name="sender">
        /// The <see cref="Object"/> where the event handler is attached.</param>
        /// <param name="args">
        /// A <see cref="RoutedEventArgs"/> object containing event data.</param>
        /// <remarks><para>
        /// <b>OnMapiBrowse</b> invokes <see cref="MapiMail.Address"/> to show the Simple MAPI
        /// address book, allowing the user to select one or more "To" recipients as players.
        /// </para><para>
        /// On success, <b>OnMapiBrowse</b> sets the name and e-mail address of the selected <see
        /// cref="HumanPlayer"/> to the corresponding values of the first selected recipient, and
        /// sets the <see cref="DataChanged"/> flag if any data was changed.
        /// </para><para>
        /// <b>OnMapiBrowse</b> shows an error message if <see cref="MapiMail.Address"/> fails, and
        /// returns silently if the user cancelled the dialog.</para></remarks>

        private void OnMapiBrowse(object sender, RoutedEventArgs args)
        {
            args.Handled = true;

            // retrieve selected human player, if any
            HumanPlayer human = SelectedHumanPlayer;

            if (human == null)
            {
                return;
            }

            MapiAddress[] entries = null;
            try {
                // let user select recipients
                entries = MapiMail.Address();
            }
            catch (MapiException e) {
                // show error message unless user cancelled
                if (e.Code != MapiException.Abort)
                {
                    MessageDialog.Show(this,
                                       Global.Strings.DialogMailErrorMapi, Global.Strings.TitleMailErrorMapi,
                                       e, MessageBoxButton.OK, Images.Warning);
                }

                return;
            }

            // quit if no recipients selected
            if (entries == null || entries.Length == 0)
            {
                return;
            }

            // check for broken MAPI client
            if (!entries[0].Address.IsValidEmail())
            {
                MessageBox.Show(this,
                                Global.Strings.DialogMailInvalidMapi, Global.Strings.TitleMailInvalid,
                                MessageBoxButton.OK, MessageBoxImage.Information);

                entries[0] = new MapiAddress(entries[0].Name, "");
            }

            // broadcast data changes, if any
            if (human.Name != entries[0].Name || human.Email != entries[0].Address)
            {
                NameBox.Text = human.Name = entries[0].Name;
                human.Email  = entries[0].Address;
                UpdatePlayers();
            }
        }
 private void OnResolveName(object sender, RoutedEventArgs args)
 {
     args.Handled = true;
     try {
         MapiAddress recip = MapiMail.ResolveName(AddressBox.Text);
         AddressBox.Text = StringUtility.Validate(recip.Address, recip.Name);
     }
     catch (MapiException e) {
         MessageBox.Show(this, e.Message, "Simple MAPI Error",
                         MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Handles the <see cref="Application.DispatcherUnhandledException"/> event.</summary>
        /// <param name="information">
        /// Information about the <see cref="Exception"/> and the current application state, or a
        /// null reference for no available information.</param>
        /// <param name="rulesFile">
        /// The file path to the current scenario rule script, or a null reference if no such file
        /// exists.</param>
        /// <param name="saveScenario">
        /// An optional <see cref="Action{String}"/> delegate that is invoked to save the current
        /// scenario data to an XML file.</param>
        /// <param name="saveSession">
        /// An optional <see cref="Action{String}"/> delegate that is invoked to save the current
        /// session data to an XML file.</param>
        /// <remarks><para>
        /// <b>OnUnhandledException</b> creates a text file describing the error which includes the
        /// specified <paramref name="information"/>. Additionally, up to two debug XML files
        /// containing scenario and session data are created by invoking any of the supplied <see
        /// cref="Action{String}"/> delegates that are not null references.
        /// </para><para>
        /// The user is also asked to e-mail all created files, plus the specified <paramref
        /// name="rulesFile"/> if it exists, to the e-mail address specified by the <see
        /// cref="ApplicationInfo.Email"/> property via Simple MAPI, or if this fails, to send basic
        /// error information to the same address using the Explorer "mailto" method.
        /// </para><para>
        /// Finally, <b>OnUnhandledException</b> terminates the application using <see
        /// cref="Environment.Exit"/> and returns an error code of -1.</para></remarks>

        public static void OnUnhandledException(string information, string rulesFile,
                                                Action <String> saveScenario, Action <String> saveSession)
        {
            // check for original distribution package
            bool original = ApplicationInfo.IsSigned;

            // determine error and debug file names
            string errorFile    = FilePaths.CreateUserPath("FatalError.txt").AbsolutePath;
            string scenarioFile = FilePaths.GetScenarioFile(ScenarioFileType.Debug).AbsolutePath;
            string sessionFile  = FilePaths.GetSessionFile(SessionFileType.Debug).AbsolutePath;

            // create an error e-mail?
            bool wantEmail = false;

            if (original)
            {
                // ask user to create e-mail with attachments
                string message = String.Format(ApplicationInfo.Culture,
                                               Strings.DialogFatalErrorOriginal, errorFile, scenarioFile, sessionFile);

                wantEmail = ShowFatalError(message, null, true);
            }
            else
            {
                // just notify user that files are being saved
                string message = String.Format(ApplicationInfo.Culture,
                                               Strings.DialogFatalErrorModified, errorFile, scenarioFile, sessionFile);

                ShowFatalError(message, null, false);
            }

            // create state information string
            StringBuilder info = new StringBuilder();

            info.AppendFormat("{0} ", ApplicationInfo.Signature);
            info.Append(Environment.NewLine);
            info.AppendFormat("Kynosarges Signature: {0}", original);
            info.Append(Environment.NewLine);
            info.AppendFormat("Public Key Token: {0}",
                              StringUtility.Validate(ApplicationInfo.PublicKeyToken));
            info.Append(Environment.NewLine);
            info.AppendFormat("Home: \"{0}\" ", FilePaths.ApplicationFolder);
            info.Append(Environment.NewLine);
            info.Append(Environment.NewLine);
            info.AppendFormat("{0} ", Environment.OSVersion);
            info.Append(Environment.NewLine);
            info.AppendFormat("{0} ", WindowsUtility.GetMemoryStatus());
            info.Append(Environment.NewLine);
            info.Append(Environment.NewLine);

            // append additional information if specified
            if (!String.IsNullOrEmpty(information))
            {
                info.AppendFormat("{0} ", information);
                info.Append(Environment.NewLine);
            }

            // create subject for e-mail message
            string subject = String.Concat(ApplicationInfo.Signature, " ", Strings.LabelError);

            // collection to hold file attachment data
            List <MapiAddress> files = new List <MapiAddress>();

            try {
                try {
                    // always create error information file
                    files.Add(new MapiAddress(Path.GetFileName(errorFile), errorFile));
                    using (StreamWriter writer = new StreamWriter(errorFile, false, Encoding.UTF8))
                        writer.WriteLine(info.ToString());

                    // attach rule script file if specified
                    RootedPath rulesPath = FilePaths.CreateCommonPath(rulesFile);
                    if (!rulesPath.IsEmpty && File.Exists(rulesPath.AbsolutePath))
                    {
                        files.Add(new MapiAddress(rulesPath.FileName, rulesPath.AbsolutePath));
                    }

                    // create scenario debug file if possible
                    if (saveScenario != null)
                    {
                        saveScenario(scenarioFile);
                        files.Add(new MapiAddress(Path.GetFileName(scenarioFile), scenarioFile));
                    }

                    // create session debug file if possible
                    if (saveSession != null)
                    {
                        saveSession(sessionFile);
                        files.Add(new MapiAddress(Path.GetFileName(sessionFile), sessionFile));
                    }
                }
                catch (Exception e) {
                    // only proceed if user wanted e-mail
                    if (wantEmail)
                    {
                        // ask user to try simpler e-mail format
                        wantEmail = ShowFatalError(Strings.DialogFatalErrorSaveMail, e, true);
                        throw; // rethrow to try mailto:
                    }

                    // user declined e-mail, just show error
                    ShowFatalError(Strings.DialogFatalErrorSave, e, false);
                    return; // quit, nothing else to do
                }

                // quit now if user declined e-mail
                if (!wantEmail)
                {
                    return;
                }

                try {
                    // address e-mail to application author
                    MapiAddress recipient = new MapiAddress(
                        ApplicationInfo.Company, ApplicationInfo.Email);

                    // try sending e-mail with attached files
                    MapiMail.SendMail(subject, "", new[] { recipient }, files.ToArray());
                }
                catch (Exception e) {
                    // quit if user cancelled MAPI session
                    MapiException me = e as MapiException;
                    if (me != null && me.Code == MapiException.Abort)
                    {
                        return;
                    }

                    // ask user to try simpler e-mail format
                    wantEmail = ShowFatalError(Strings.DialogFatalErrorMail, e, true);
                    throw; // rethrow to try mailto:
                }
            }
            catch {
                // quit now if user declined e-mail
                if (!wantEmail)
                {
                    return;
                }

                // construct shell command to create e-mail
                string mailto = String.Format(CultureInfo.InvariantCulture,
                                              "mailto:{0}?subject={1}&body={2}", ApplicationInfo.Email, subject, info);

                try {
                    // request default e-mail client
                    Process.Start(mailto);
                }
                catch (Exception e) {
                    ShowExplorerError(null, Strings.DialogExplorerMailError, e);
                }
            }
            finally {
                // quit application upon return
                Environment.Exit(-1);
            }
        }