Fix for duplicates in selectivesync display code
[pithos-ms-client] / trunk / Pithos.Client.WPF / Shell / MessageView.xaml.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="MessageView.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.Collections.ObjectModel;
45 using System.ComponentModel;
46 using System.ComponentModel.Composition;
47 using System.Linq;
48 using System.Linq.Expressions;
49 using System.Net;
50 using System.Net.Mail;
51 using System.Text;
52 using System.Windows;
53 using System.Windows.Controls;
54 using System.Windows.Data;
55 using System.Windows.Documents;
56 using System.Windows.Input;
57 using System.Windows.Media;
58 using System.Windows.Media.Imaging;
59 using System.Windows.Shapes;
60 using Caliburn.Micro;
61 using Pithos.Client.WPF.Shell;
62 using Pithos.Network;
63
64 namespace Pithos.Client.WPF
65 {
66     /// <summary>
67     /// Interaction logic for MessageView.xaml
68     /// </summary>
69     public partial class MessageView : Window, INotifyPropertyChanged
70     {
71         public ObservableCollection<UserMessage> UserMessages { get; set; }
72
73         private string _message;
74         public string Message
75         {
76             get
77             {
78                 return _message;
79             }
80             set
81             {
82                 _message = value;
83                 NotifyOfPropertyChange(()=>Message);
84             }
85         }
86
87
88         public MessageView(IEnumerable<UserMessage> userMessages)
89         {
90             UserMessages = new ObservableCollection<UserMessage>(userMessages);
91             DataContext = this;
92             InitializeComponent();            
93         }
94
95
96         
97         public MessageView(WebException exception)
98         {
99             DataContext = this;
100             InitializeComponent();
101
102             var messages = new List<UserMessage>();
103             
104             if ((exception.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
105             {
106                 Message = "Your authorization token has expired. Please renew the token and try again ";
107                 Title = "Authorization expired";
108             }
109             else
110             {
111                 messages.Add(new UserMessage
112                 {
113                     Message = exception.InnerException.Message,
114                     Details = exception.InnerException.ToString(),
115                     Severity = Severity.Warn
116                 });
117                 Message = "There was an error while retrieving the item's information";
118                 Title = "Error";
119             }
120             UserMessages = new ObservableCollection<UserMessage>(messages);
121         }
122
123         public MessageView(RetryException exception)
124         {
125             DataContext = this;
126             InitializeComponent();
127
128             var messages = new List<UserMessage>{
129                 new UserMessage{
130                         Message = exception.InnerException.Message,
131                         Details = exception.InnerException.ToString(),
132                         Severity = Severity.Warn
133                     }
134             };
135
136             Title = "Network error";
137             Message = "The connection to the server timed out. Please check your network connection and try again later";                
138
139             UserMessages = new ObservableCollection<UserMessage>(messages);
140         }
141         
142         public MessageView(Exception exception)
143         {
144             DataContext = this;
145             InitializeComponent();
146
147             var messages = new List<UserMessage>{
148                 new UserMessage{
149                         Message = exception.InnerException.Message,
150                         Details = exception.InnerException.ToString(),
151                         Severity = Severity.Warn
152                     }
153             };
154
155             Title = "Unexpected error";
156             Message = "An unexpected error has occured";                
157
158             UserMessages = new ObservableCollection<UserMessage>(messages);
159         }
160
161         /// <summary>
162         /// Copies the messages to the clipboard
163         /// </summary>
164         private void OnCopy(object sender, ExecutedRoutedEventArgs e)
165         {
166             var text = GetLogText();
167             Clipboard.SetText(text);
168         }
169
170         private string GetLogText()
171         {
172             StringBuilder logText = new StringBuilder()
173                 .Append('-', 30)
174                 .AppendLine("\r\nPithos")
175                 .Append('-', 30)
176                 .AppendLine()
177                 .AppendLine();
178             foreach (var message in UserMessages)
179             {
180                 logText
181                     .Append('-', 20)
182                     .AppendFormat("\r\n[{0,5}]\t{1}", message.Severity, message.Message)
183                     .AppendLine()
184                     .AppendLine(message.Details)
185                     .AppendLine();
186             }
187
188             var text = logText.ToString();
189             return text;
190         }
191
192         private void OnClose(object sender, ExecutedRoutedEventArgs e)
193         {
194             this.Close();
195         }
196
197         public event PropertyChangedEventHandler PropertyChanged;
198
199         private void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
200         {
201             if (PropertyChanged!=null)
202                 PropertyChanged(this,new PropertyChangedEventArgs(property.GetMemberInfo().Name));
203         }
204
205         private void OnSend(object sender, RoutedEventArgs e)
206         {
207             var manager=IoC.Get<IWindowManager>();
208             var logText = GetLogText();
209             var feedBack = IoC.Get<FeedbackViewModel>();
210             feedBack.AppendData(logText);
211             manager.ShowWindow(feedBack);
212             this.Close();
213         }
214     }
215
216     public class UserMessage
217     {
218         public string Message { get; set; }
219
220         public Severity Severity { get; set; }
221         public string Details { get; set; }
222     }
223
224     public enum Severity
225     {
226         Info,
227         Warn,
228         Error
229     }
230 }