using System.Data;
using System.Diagnostics;
using System.Linq;
+using System.Net.Mail;
using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
using System.Windows;
+using Caliburn.Micro;
using Microsoft.Win32;
-using WPF.Themes;
+using Caliburn.Micro.Logging;
+using Pithos.Client.WPF.Properties;
+
namespace Pithos.Client.WPF
{
/// </summary>
public partial class App : Application
{
+ private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof (App));
+
public App()
{
- var extensionController = new ShellExtensionController();
- extensionController.RegisterExtensions();
+ log4net.Config.XmlConfigurator.Configure();
+ this.DispatcherUnhandledException += OnUnhandledException;
+ AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
+ TaskScheduler.UnobservedTaskException += OnUnobservedException;
- var appPatth = Assembly.GetExecutingAssembly().Location;
- Registry.LocalMachine.SetValue(@"Software\Pithos\AppPath",appPatth );
+ //Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty
+ if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer))
+ {
+ Settings.Default.UseManualProxy = false;
+ Settings.Default.UseDefaultProxy = true;
+ }
InitializeComponent();
- //Application.Current.ApplyTheme("BureauBlue");
+ }
+
+ protected override void OnStartup(StartupEventArgs e)
+ {
+ if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup"))
+ {
+ this.Shutdown();
+ }
+ base.OnStartup(e);
+ }
+
+ private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
+ {
+ var messages=new List<UserMessage>();
+ e.Exception.Handle(exc=>{
+ messages.Add(new UserMessage
+ {
+ Message = "Unexpected Exception",
+ Details = exc.ToString(),
+ Severity = Severity.Error
+ });
+ return true;
+ });
+
+ ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages);
+ e.SetObserved();
+ }
+
+ private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
+ {
+ var message = e.IsTerminating
+ ? "Unexpected Exception. The application must terminate"
+ : "Unexpected Exception";
+
+
+ var messages = new[]{
+ new UserMessage
+ {
+ Message = (e.ExceptionObject as Exception).Message,
+ Details = e.ExceptionObject.ToString(),
+ Severity = Severity.Error
+ }
+ };
+ ShowMessages("Oops!", message,messages);
+ }
+
+ void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
+ {
+ var messages = new[]{
+ new UserMessage
+ {
+ Message = e.Exception.Message,
+ Details = e.Exception.ToString(),
+ Severity = Severity.Error
+ }
+ };
+ ShowMessages("Oops!", "Unexcpected Exception", messages);
+ e.Handled=true;
+ }
+
+ void ShowMessages(string title,string message,IEnumerable<UserMessage> messages )
+ {
+ LogMessages(messages);
+ Execute.OnUIThread(()=>{
+ var messageView = new MessageView(messages);
+ messageView.Title = title;
+ messageView.Message = message;
+ messageView.ShowDialog();
+ });
+ }
+
+ private void LogMessages(IEnumerable<UserMessage> messages)
+ {
+ var logMessage = CreateMessage(messages);
+
+ _log.Error(logMessage);
+ }
+
+ private static string CreateMessage(IEnumerable<UserMessage> messages)
+ {
+ var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"),
+ (builder, message) =>
+ {
+ builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity,
+ message.Message, message.Details);
+ return builder;
+ });
+ var logMessage = messageBuilder.ToString();
+ return logMessage;
}
}