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;
46 using System.Reflection;
48 using System.Threading;
49 using System.Threading.Tasks;
52 using Pithos.Client.WPF.Configuration;
53 using Pithos.Client.WPF.Properties;
54 using log4net.Appender;
56 using log4net.Repository.Hierarchy;
59 namespace Pithos.Client.WPF
62 /// Interaction logic for App.xaml
64 public partial class App : Application
66 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
72 //var instanceMutex=new Mutex()
77 DispatcherUnhandledException += OnDispatcherUnhandledException;
78 AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
79 TaskScheduler.UnobservedTaskException += OnUnobservedException;
82 //Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty
83 if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer))
85 Settings.Default.UseManualProxy = false;
86 Settings.Default.UseDefaultProxy = true;
90 InitializeComponent();
95 private void OnUpdateDetected(object sender, UpdateDetectedEventArgs e)
97 Log.InfoFormat("Update Detected {0}",e.LatestVersion.Version);
101 private static void InitializeLogging()
103 log4net.Config.XmlConfigurator.Configure();
107 var pithosDataPath = PithosSettings.PithosDataPath;
108 if (!Directory.Exists(pithosDataPath))
109 Directory.CreateDirectory(pithosDataPath);
111 var loggerRepository = (Hierarchy)log4net.LogManager.GetRepository();
113 var appenders = loggerRepository.GetAppenders();
116 var lossyAppender = appenders.OfType<BufferingForwardingAppender>()
117 .FirstOrDefault(appender => appender.Name == "LossyFileAppender");
118 if (lossyAppender!=null)
120 var dumpAppender = lossyAppender.Appenders.OfType<RollingFileAppender>().First();
121 dumpAppender.File = Path.Combine(pithosDataPath, "errorlog.xml");
122 dumpAppender.ActivateOptions();
125 var debugAppender =appenders.OfType<RollingFileAppender>()
126 .FirstOrDefault(a => a.Name == "DebugFileAppender");
127 if (debugAppender != null)
129 debugAppender.File = Path.Combine(pithosDataPath, "debuglog.xml");
130 if (!Settings.Default.DebugLoggingEnabled)
131 debugAppender.Threshold = Level.Off;
132 debugAppender.ActivateOptions();
135 catch (Exception exc)
137 Log.Error("Dumpl appender initialization failed",exc);
141 private Mutex _singleInstanceMutex;
143 protected override void OnStartup(StartupEventArgs e)
145 if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup"))
152 _singleInstanceMutex = new Mutex(false, "PITHOSMUTEX", out firstInstance);
155 _singleInstanceMutex.Dispose();
156 _singleInstanceMutex = null;
161 //Delay during startup
162 if (e.Args.Contains("startup"))
164 if (Settings.Default.StartupDelay>TimeSpan.Zero)
165 Thread.Sleep(Settings.Default.StartupDelay);
168 var splashScreen = new SplashScreen("images/pithos_logo-title-margin-splash-600-whitebg.png");
169 splashScreen.Show(true);
174 protected override void OnExit(ExitEventArgs e)
178 if (_singleInstanceMutex!=null)
179 _singleInstanceMutex.Dispose();
180 _singleInstanceMutex = null;
186 private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
188 Log.Error("Unobserved Task Exception", e.Exception);
190 var messages = new List<UserMessage>();
191 e.Exception.Handle(exc=>{
192 messages.Add(new UserMessage
194 Message = "Unexpected Exception",
195 Details = exc.ToString(),
196 Severity = Severity.Error
202 var message = String.Format(@"{0}<LineBreak/>{1}<LineBreak/><LineBreak/>{2}",
203 WPF.Properties.Resources.Unexpected_Error,
204 WPF.Properties.Resources.We_Apologize,
205 WPF.Properties.Resources.Please_Submit_Error);
206 ShowMessages("Oops!",message, messages);
210 private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
212 Log.Error("Unhandled exception", (Exception)e.ExceptionObject);
214 var description = e.IsTerminating
215 ? WPF.Properties.Resources.Unexpected_Error_Terminating
216 : WPF.Properties.Resources.Unexpected_Error;
217 var message = String.Format(@"{0}<LineBreak/>{1}<LineBreak/><LineBreak/>{2}",
219 WPF.Properties.Resources.We_Apologize,
220 WPF.Properties.Resources.Please_Submit_Error);
223 var exc = ((Exception) e.ExceptionObject);
224 var messages = new[]{
227 Message = exc.Message,
228 Details = exc.ToString(),
229 Severity = Severity.Error
234 ShowMessages("Oops!", message,messages);
237 void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
239 Log.Error("Unhandled Dispatcher exception", e.Exception);
241 var messages = new[]{
244 Message = e.Exception.Message,
245 Details = e.Exception.ToString(),
246 Severity = Severity.Error
249 ShowMessages(WPF.Properties.Resources.Error_Title,
250 WPF.Properties.Resources.Unexpected_Error,
255 void ShowMessages(string title,string message,IEnumerable<UserMessage> messages )
257 var messageList = messages.ToList();
258 LogMessages(messageList);
259 Execute.OnUIThread(()=>{
260 var messageView = new MessageView(messageList)
265 messageView.ShowDialog();
269 private void LogMessages(IEnumerable<UserMessage> messages)
271 var logMessage = CreateMessage(messages);
273 Log.Error(logMessage);
276 private static string CreateMessage(IEnumerable<UserMessage> messages)
278 var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"),
279 (builder, message) =>
281 builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity,
282 message.Message, message.Details);
285 var logMessage = messageBuilder.ToString();