root / trunk / Pithos.Client.WPF / App.xaml.cs @ f2d88248
History | View | Annotate | Download (7.2 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; |
53 |
using System.Threading.Tasks; |
54 |
using System.Windows; |
55 |
using Caliburn.Micro; |
56 |
using Microsoft.Win32; |
57 |
using Caliburn.Micro.Logging; |
58 |
using Pithos.Client.WPF.Properties; |
59 |
|
60 |
|
61 |
namespace Pithos.Client.WPF |
62 |
{ |
63 |
/// <summary> |
64 |
/// Interaction logic for App.xaml |
65 |
/// </summary> |
66 |
public partial class App : Application |
67 |
{ |
68 |
private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof (App)); |
69 |
|
70 |
public App() |
71 |
{ |
72 |
log4net.Config.XmlConfigurator.Configure(); |
73 |
|
74 |
this.DispatcherUnhandledException += OnUnhandledException; |
75 |
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; |
76 |
TaskScheduler.UnobservedTaskException += OnUnobservedException; |
77 |
|
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)) |
80 |
{ |
81 |
Settings.Default.UseManualProxy = false; |
82 |
Settings.Default.UseDefaultProxy = true; |
83 |
} |
84 |
|
85 |
InitializeComponent(); |
86 |
} |
87 |
|
88 |
protected override void OnStartup(StartupEventArgs e) |
89 |
{ |
90 |
if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup")) |
91 |
{ |
92 |
this.Shutdown(); |
93 |
return; |
94 |
} |
95 |
|
96 |
//TODO: Possibly add a delay here? |
97 |
if (e.Args.Contains("startup")) |
98 |
{ |
99 |
if (Settings.Default.StartupDelay>TimeSpan.Zero) |
100 |
Thread.Sleep(Settings.Default.StartupDelay); |
101 |
} |
102 |
|
103 |
var splashScreen = new SplashScreen("images/logo.png"); |
104 |
splashScreen.Show(true); |
105 |
|
106 |
base.OnStartup(e); |
107 |
} |
108 |
|
109 |
private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e) |
110 |
{ |
111 |
var messages=new List<UserMessage>(); |
112 |
e.Exception.Handle(exc=>{ |
113 |
messages.Add(new UserMessage |
114 |
{ |
115 |
Message = "Unexpected Exception", |
116 |
Details = exc.ToString(), |
117 |
Severity = Severity.Error |
118 |
}); |
119 |
return true; |
120 |
}); |
121 |
|
122 |
ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages); |
123 |
e.SetObserved(); |
124 |
} |
125 |
|
126 |
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) |
127 |
{ |
128 |
var message = e.IsTerminating |
129 |
? "Unexpected Exception. The application must terminate" |
130 |
: "Unexpected Exception"; |
131 |
|
132 |
|
133 |
var messages = new[]{ |
134 |
new UserMessage |
135 |
{ |
136 |
Message = (e.ExceptionObject as Exception).Message, |
137 |
Details = e.ExceptionObject.ToString(), |
138 |
Severity = Severity.Error |
139 |
} |
140 |
}; |
141 |
ShowMessages("Oops!", message,messages); |
142 |
} |
143 |
|
144 |
void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) |
145 |
{ |
146 |
var messages = new[]{ |
147 |
new UserMessage |
148 |
{ |
149 |
Message = e.Exception.Message, |
150 |
Details = e.Exception.ToString(), |
151 |
Severity = Severity.Error |
152 |
} |
153 |
}; |
154 |
ShowMessages("Oops!", "Unexcpected Exception", messages); |
155 |
e.Handled=true; |
156 |
} |
157 |
|
158 |
void ShowMessages(string title,string message,IEnumerable<UserMessage> messages ) |
159 |
{ |
160 |
LogMessages(messages); |
161 |
Execute.OnUIThread(()=>{ |
162 |
var messageView = new MessageView(messages); |
163 |
messageView.Title = title; |
164 |
messageView.Message = message; |
165 |
messageView.ShowDialog(); |
166 |
}); |
167 |
} |
168 |
|
169 |
private void LogMessages(IEnumerable<UserMessage> messages) |
170 |
{ |
171 |
var logMessage = CreateMessage(messages); |
172 |
|
173 |
_log.Error(logMessage); |
174 |
} |
175 |
|
176 |
private static string CreateMessage(IEnumerable<UserMessage> messages) |
177 |
{ |
178 |
var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"), |
179 |
(builder, message) => |
180 |
{ |
181 |
builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity, |
182 |
message.Message, message.Details); |
183 |
return builder; |
184 |
}); |
185 |
var logMessage = messageBuilder.ToString(); |
186 |
return logMessage; |
187 |
} |
188 |
} |
189 |
|
190 |
} |