root / trunk / Pithos.Client.WPF / App.xaml.cs @ 255f5f86
History | View | Annotate | Download (7 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="App.xaml.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
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. |
19 |
* |
20 |
* |
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. |
33 |
* |
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. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System; |
43 |
using System.Collections.Generic; |
44 |
using System.Configuration; |
45 |
using System.Data; |
46 |
using System.Diagnostics; |
47 |
using System.Linq; |
48 |
using System.Net; |
49 |
using System.Net.Mail; |
50 |
using System.Reflection; |
51 |
using System.Text; |
52 |
using System.Threading.Tasks; |
53 |
using System.Windows; |
54 |
using Caliburn.Micro; |
55 |
using Microsoft.Win32; |
56 |
using Caliburn.Micro.Logging; |
57 |
using Pithos.Client.WPF.Properties; |
58 |
|
59 |
|
60 |
namespace Pithos.Client.WPF |
61 |
{ |
62 |
/// <summary> |
63 |
/// Interaction logic for App.xaml |
64 |
/// </summary> |
65 |
public partial class App : Application |
66 |
{ |
67 |
private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof (App)); |
68 |
|
69 |
public App() |
70 |
{ |
71 |
log4net.Config.XmlConfigurator.Configure(); |
72 |
|
73 |
this.DispatcherUnhandledException += OnUnhandledException; |
74 |
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; |
75 |
TaskScheduler.UnobservedTaskException += OnUnobservedException; |
76 |
|
77 |
//Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty |
78 |
if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer)) |
79 |
{ |
80 |
Settings.Default.UseManualProxy = false; |
81 |
Settings.Default.UseDefaultProxy = true; |
82 |
} |
83 |
|
84 |
InitializeComponent(); |
85 |
} |
86 |
|
87 |
protected override void OnStartup(StartupEventArgs e) |
88 |
{ |
89 |
if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup")) |
90 |
{ |
91 |
this.Shutdown(); |
92 |
return; |
93 |
} |
94 |
|
95 |
var splashScreen = new SplashScreen("images/logo.png"); |
96 |
splashScreen.Show(true); |
97 |
|
98 |
base.OnStartup(e); |
99 |
} |
100 |
|
101 |
private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e) |
102 |
{ |
103 |
var messages=new List<UserMessage>(); |
104 |
e.Exception.Handle(exc=>{ |
105 |
messages.Add(new UserMessage |
106 |
{ |
107 |
Message = "Unexpected Exception", |
108 |
Details = exc.ToString(), |
109 |
Severity = Severity.Error |
110 |
}); |
111 |
return true; |
112 |
}); |
113 |
|
114 |
ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages); |
115 |
e.SetObserved(); |
116 |
} |
117 |
|
118 |
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) |
119 |
{ |
120 |
var message = e.IsTerminating |
121 |
? "Unexpected Exception. The application must terminate" |
122 |
: "Unexpected Exception"; |
123 |
|
124 |
|
125 |
var messages = new[]{ |
126 |
new UserMessage |
127 |
{ |
128 |
Message = (e.ExceptionObject as Exception).Message, |
129 |
Details = e.ExceptionObject.ToString(), |
130 |
Severity = Severity.Error |
131 |
} |
132 |
}; |
133 |
ShowMessages("Oops!", message,messages); |
134 |
} |
135 |
|
136 |
void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) |
137 |
{ |
138 |
var messages = new[]{ |
139 |
new UserMessage |
140 |
{ |
141 |
Message = e.Exception.Message, |
142 |
Details = e.Exception.ToString(), |
143 |
Severity = Severity.Error |
144 |
} |
145 |
}; |
146 |
ShowMessages("Oops!", "Unexcpected Exception", messages); |
147 |
e.Handled=true; |
148 |
} |
149 |
|
150 |
void ShowMessages(string title,string message,IEnumerable<UserMessage> messages ) |
151 |
{ |
152 |
LogMessages(messages); |
153 |
Execute.OnUIThread(()=>{ |
154 |
var messageView = new MessageView(messages); |
155 |
messageView.Title = title; |
156 |
messageView.Message = message; |
157 |
messageView.ShowDialog(); |
158 |
}); |
159 |
} |
160 |
|
161 |
private void LogMessages(IEnumerable<UserMessage> messages) |
162 |
{ |
163 |
var logMessage = CreateMessage(messages); |
164 |
|
165 |
_log.Error(logMessage); |
166 |
} |
167 |
|
168 |
private static string CreateMessage(IEnumerable<UserMessage> messages) |
169 |
{ |
170 |
var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"), |
171 |
(builder, message) => |
172 |
{ |
173 |
builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity, |
174 |
message.Message, message.Details); |
175 |
return builder; |
176 |
}); |
177 |
var logMessage = messageBuilder.ToString(); |
178 |
return logMessage; |
179 |
} |
180 |
} |
181 |
|
182 |
} |