2 /* -----------------------------------------------------------------------
3 * <copyright file="App.xaml.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Collections.Generic;
44 using System.Configuration;
46 using System.Diagnostics;
49 using System.Net.Mail;
50 using System.Reflection;
52 using System.Threading;
53 using System.Threading.Tasks;
56 using Microsoft.Win32;
57 using Caliburn.Micro.Logging;
58 using Pithos.Client.WPF.Properties;
61 namespace Pithos.Client.WPF
64 /// Interaction logic for App.xaml
66 public partial class App : Application
68 private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof (App));
72 log4net.Config.XmlConfigurator.Configure();
74 this.DispatcherUnhandledException += OnUnhandledException;
75 AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
76 TaskScheduler.UnobservedTaskException += OnUnobservedException;
78 //Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty
79 if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer))
81 Settings.Default.UseManualProxy = false;
82 Settings.Default.UseDefaultProxy = true;
85 InitializeComponent();
88 protected override void OnStartup(StartupEventArgs e)
90 if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup"))
96 //TODO: Possibly add a delay here?
97 if (e.Args.Contains("startup"))
99 if (Settings.Default.StartupDelay>TimeSpan.Zero)
100 Thread.Sleep(Settings.Default.StartupDelay);
103 var splashScreen = new SplashScreen("images/logo.png");
104 splashScreen.Show(true);
109 private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
111 var messages=new List<UserMessage>();
112 e.Exception.Handle(exc=>{
113 messages.Add(new UserMessage
115 Message = "Unexpected Exception",
116 Details = exc.ToString(),
117 Severity = Severity.Error
122 ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages);
126 private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
128 var message = e.IsTerminating
129 ? "Unexpected Exception. The application must terminate"
130 : "Unexpected Exception";
133 var messages = new[]{
136 Message = (e.ExceptionObject as Exception).Message,
137 Details = e.ExceptionObject.ToString(),
138 Severity = Severity.Error
141 ShowMessages("Oops!", message,messages);
144 void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
146 var messages = new[]{
149 Message = e.Exception.Message,
150 Details = e.Exception.ToString(),
151 Severity = Severity.Error
154 ShowMessages("Oops!", "Unexcpected Exception", messages);
158 void ShowMessages(string title,string message,IEnumerable<UserMessage> messages )
160 LogMessages(messages);
161 Execute.OnUIThread(()=>{
162 var messageView = new MessageView(messages);
163 messageView.Title = title;
164 messageView.Message = message;
165 messageView.ShowDialog();
169 private void LogMessages(IEnumerable<UserMessage> messages)
171 var logMessage = CreateMessage(messages);
173 _log.Error(logMessage);
176 private static string CreateMessage(IEnumerable<UserMessage> messages)
178 var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"),
179 (builder, message) =>
181 builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity,
182 message.Message, message.Details);
185 var logMessage = messageBuilder.ToString();