#region /* ----------------------------------------------------------------------- * * * Copyright 2011-2012 GRNET S.A. All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and * documentation are those of the authors and should not be * interpreted as representing official policies, either expressed * or implied, of GRNET S.A. * * ----------------------------------------------------------------------- */ #endregion using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.ComponentModel.Composition; using System.Linq; using System.Linq.Expressions; using System.Net; using System.Net.Mail; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Caliburn.Micro; using Pithos.Client.WPF.Shell; using Pithos.Network; namespace Pithos.Client.WPF { /// /// Interaction logic for MessageView.xaml /// public partial class MessageView : Window, INotifyPropertyChanged { public ObservableCollection UserMessages { get; set; } private string _message; public string Message { get { return _message; } set { _message = value; NotifyOfPropertyChange(()=>Message); } } public MessageView(IEnumerable userMessages) { UserMessages = new ObservableCollection(userMessages); DataContext = this; InitializeComponent(); } public MessageView(WebException exception) { DataContext = this; InitializeComponent(); var messages = new List(); if ((exception.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized) { Message = "Your authorization token has expired. Please renew the token and try again "; Title = "Authorization expired"; } else { messages.Add(new UserMessage { Message = exception.InnerException.Message, Details = exception.InnerException.ToString(), Severity = Severity.Warn }); Message = "There was an error while retrieving the item's information"; Title = "Error"; } UserMessages = new ObservableCollection(messages); } public MessageView(RetryException exception) { DataContext = this; InitializeComponent(); var messages = new List{ new UserMessage{ Message = exception.InnerException.Message, Details = exception.InnerException.ToString(), Severity = Severity.Warn } }; Title = "Network error"; Message = "The connection to the server timed out. Please check your network connection and try again later"; UserMessages = new ObservableCollection(messages); } public MessageView(Exception exception) { DataContext = this; InitializeComponent(); var messages = new List{ new UserMessage{ Message = exception.InnerException.Message, Details = exception.InnerException.ToString(), Severity = Severity.Warn } }; Title = "Unexpected error"; Message = "An unexpected error has occured"; UserMessages = new ObservableCollection(messages); } /// /// Copies the messages to the clipboard /// private void OnCopy(object sender, ExecutedRoutedEventArgs e) { var text = GetLogText(); Clipboard.SetText(text); } private string GetLogText() { StringBuilder logText = new StringBuilder() .Append('-', 30) .AppendLine("\r\nPithos") .Append('-', 30) .AppendLine() .AppendLine(); foreach (var message in UserMessages) { logText .Append('-', 20) .AppendFormat("\r\n[{0,5}]\t{1}", message.Severity, message.Message) .AppendLine() .AppendLine(message.Details) .AppendLine(); } var text = logText.ToString(); return text; } private void OnClose(object sender, ExecutedRoutedEventArgs e) { this.Close(); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyOfPropertyChange(Expression> property) { if (PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs(property.GetMemberInfo().Name)); } private void OnSend(object sender, RoutedEventArgs e) { var manager=IoC.Get(); var logText = GetLogText(); var feedBack = IoC.Get(); feedBack.AppendData(logText); manager.ShowWindow(feedBack); this.Close(); } } public class UserMessage { public string Message { get; set; } public Severity Severity { get; set; } public string Details { get; set; } } public enum Severity { Info, Warn, Error } }