Modified the uploader to add new directories to Selected Folders if Selective Sync...
[pithos-ms-client] / trunk / Pithos.Client.WPF / App.xaml.cs
index 4105bfe..b399db6 100644 (file)
@@ -43,15 +43,20 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Net;
 using System.Reflection;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Windows;
 using Caliburn.Micro;
+using Pithos.Client.WPF.Configuration;
 using Pithos.Client.WPF.Properties;
+using Pithos.Interfaces;
 using log4net.Appender;
+using log4net.Core;
 using log4net.Repository.Hierarchy;
+using System.Runtime.InteropServices;
 
 
 namespace Pithos.Client.WPF
@@ -62,14 +67,20 @@ namespace Pithos.Client.WPF
     public partial class App : Application
     {
         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
-
-
-
+        
+            ///DLL Import to add restart manager support
+            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
+            static extern uint RegisterApplicationRestart(string pwzCommandLine, RestartFlags dwFlags);
+               
         public App()
         {
 
+            //var instanceMutex=new Mutex()
             InitializeLogging();
 
+            ///Register Application in the Restartmanager service
+            RegisterApplicationRestart("Upgrade", RestartFlags.NONE);
+
 
             DispatcherUnhandledException += OnDispatcherUnhandledException;
             AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
@@ -83,8 +94,13 @@ namespace Pithos.Client.WPF
                 Settings.Default.UseDefaultProxy = true;
             }
 
+            if (Settings.Default.IgnoreCertificateErrors)
+                ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
+
+
             InitializeComponent();            
-        }
+        }       
+
 
 /*
         private void OnUpdateDetected(object sender, UpdateDetectedEventArgs e)
@@ -99,19 +115,33 @@ namespace Pithos.Client.WPF
 
             try
             {
-                var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
-                var pithosDataPath= Path.Combine(appDataPath , "GRNET");
+                var pithosDataPath = PithosSettings.PithosDataPath;
                 if (!Directory.Exists(pithosDataPath))
                     Directory.CreateDirectory(pithosDataPath);
 
                 var loggerRepository = (Hierarchy)log4net.LogManager.GetRepository();
-                
+
                 var appenders = loggerRepository.GetAppenders();
+                
+
                 var lossyAppender = appenders.OfType<BufferingForwardingAppender>()
-                    .First(appender => appender.Name == "LossyFileAppender");
-                var dumpAppender = lossyAppender.Appenders.OfType<RollingFileAppender>().First();                
-                dumpAppender.File = Path.Combine(pithosDataPath, "errorlog.txt");
-                dumpAppender.ActivateOptions();
+                    .FirstOrDefault(appender => appender.Name == "LossyFileAppender");
+                if (lossyAppender!=null)
+                {
+                    var dumpAppender = lossyAppender.Appenders.OfType<RollingFileAppender>().First();
+                    dumpAppender.File = Path.Combine(pithosDataPath, "errorlog.xml");
+                    dumpAppender.ActivateOptions();
+                }
+                
+                var debugAppender =appenders.OfType<RollingFileAppender>()
+                    .FirstOrDefault(a => a.Name == "DebugFileAppender");
+                if (debugAppender != null)
+                {
+                    debugAppender.File = Path.Combine(pithosDataPath, "debuglog.xml");
+                    if (!Settings.Default.DebugLoggingEnabled)
+                        debugAppender.Threshold = Level.Off;
+                    debugAppender.ActivateOptions();
+                }
             }
             catch (Exception exc)
             {
@@ -119,6 +149,8 @@ namespace Pithos.Client.WPF
             }
         }
 
+        private Mutex _singleInstanceMutex;
+
         protected override void OnStartup(StartupEventArgs e)
         {
             if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup"))
@@ -127,6 +159,16 @@ namespace Pithos.Client.WPF
                 return;
             }
 
+            bool firstInstance;
+            _singleInstanceMutex = new Mutex(false, "PITHOSMUTEX", out firstInstance);
+            if (!firstInstance)
+            {
+                _singleInstanceMutex.Dispose();
+                _singleInstanceMutex = null;
+                Shutdown();
+                return;
+            }
+
             //Delay during startup
             if (e.Args.Contains("startup"))
             {
@@ -134,15 +176,29 @@ namespace Pithos.Client.WPF
                     Thread.Sleep(Settings.Default.StartupDelay);
             }
 
-            var splashScreen = new SplashScreen("images/logo.png");
+            var splashScreen = new SplashScreen("images/pithos_logo-title-margin-splash-600-whitebg.png");
             splashScreen.Show(true);
             
             base.OnStartup(e);
         }
 
+        protected override void OnExit(ExitEventArgs e)
+        {
+            try
+            {
+                if (_singleInstanceMutex!=null)
+                    _singleInstanceMutex.Dispose();
+                _singleInstanceMutex = null;
+            }
+            catch { }
+            base.OnExit(e);
+        }
+
         private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
-        {            
-            var messages=new List<UserMessage>();
+        {
+            Log.Error("Unobserved Task Exception", e.Exception); 
+            
+            var messages = new List<UserMessage>();
             e.Exception.Handle(exc=>{
                 messages.Add(new UserMessage
                 {
@@ -153,29 +209,61 @@ namespace Pithos.Client.WPF
                 return true;
             });
 
-            Log.Error("Unobserved Task Exception",e.Exception);
-            
-            var message = String.Format(@"{0}\r\n{1}\r\n\r\n{2}", 
+
+            //Do not display any messages if this is a 304 code
+            var ignoreExceptions = from exc in e.Exception.InnerExceptions.OfType<WebException>()
+                                   where IsAllowedException(exc)
+                                   select exc;
+
+            if (ignoreExceptions.Any())
+            {
+                e.SetObserved();
+                return;
+            }
+
+
+           /* var message = String.Format(@"{0}" + Environment.NewLine + "{1}" + Environment.NewLine + "{2}", 
                 WPF.Properties.Resources.Unexpected_Error,
                 WPF.Properties.Resources.We_Apologize, 
                 WPF.Properties.Resources.Please_Submit_Error);
-            ShowMessages("Oops!",message, messages);
-            e.SetObserved();
+            ShowMessages("Oops!",message, messages);*/
+            e.SetObserved();            
+        }
+
+        private bool IsAllowedException(Exception exc)
+        {
+            var we = exc as WebException;
+            if (we==null)
+            {
+                return false;
+            }
+            var response = we.Response as HttpWebResponse;
+            return (response != null && response.StatusCode == HttpStatusCode.NotModified);
         }
 
         private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
         {
+
+            //Do not display any messages if this is a 304 code
+            if (IsAllowedException(e.ExceptionObject as Exception))
+            {
+                return;
+            }
+
             Log.Error("Unhandled exception", (Exception)e.ExceptionObject);
 
+            if (!e.IsTerminating)
+                //Do not display a message unless the application is terminating
+                return;
+
             var description = e.IsTerminating
                               ? WPF.Properties.Resources.Unexpected_Error_Terminating
                               : WPF.Properties.Resources.Unexpected_Error;
-            var message = String.Format(@"{0}\r\n{1}\r\n\r\n{2}",
+            var message = String.Format(@"{0}<LineBreak/>{1}<LineBreak/><LineBreak/>{2}",
                 description,
                 WPF.Properties.Resources.We_Apologize,
                 WPF.Properties.Resources.Please_Submit_Error);
 
-
             var exc = ((Exception) e.ExceptionObject);
             var messages = new[]{
                                    new UserMessage
@@ -190,11 +278,19 @@ namespace Pithos.Client.WPF
             ShowMessages("Oops!", message,messages);
         }
 
+
+
         void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
         {
+            //Do not display any messages if this is a 304 code
+            if (IsAllowedException(e.Exception))
+            {
+                return;
+            }
+
             Log.Error("Unhandled Dispatcher exception", e.Exception);
             
-            var messages = new[]{
+           /* var messages = new[]{
                                    new UserMessage
                                        {
                                            Message = e.Exception.Message, 
@@ -204,7 +300,7 @@ namespace Pithos.Client.WPF
                                };
             ShowMessages(WPF.Properties.Resources.Error_Title, 
                 WPF.Properties.Resources.Unexpected_Error,
-                messages);
+                messages);*/
             e.Handled=true;
         }
 
@@ -244,3 +340,15 @@ namespace Pithos.Client.WPF
     }
 
 }
+
+    enum RestartFlags
+    {
+        NONE = 0,
+        RESTART_CYCLICAL = 1,
+        RESTART_NOTIFY_SOLUTION = 2,
+        RESTART_NOTIFY_FAULT = 4,
+        RESTART_NO_CRASH = 8,
+        RESTART_NO_HANG = 16,
+        RESTART_NO_PATCH = 32,
+        RESTART_NO_REBOOT = 64
+    }
\ No newline at end of file