fcefc095e09b3d8bd779c07628a2d60fc334c22a
[pithos-ms-client] / trunk%2FPithos.Client.WPF%2FShell%2FFeedbackViewModel.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="FeedbackViewModel.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.Collections.Specialized;
43 using System.ComponentModel.Composition;
44 using System.Diagnostics;
45 using System.Diagnostics.Contracts;
46 using System.IO;
47 using System.Net;
48 using System.Reflection;
49 using System.Windows;
50 using Caliburn.Micro;
51 using Pithos.Client.WPF.Properties;
52
53 namespace Pithos.Client.WPF.Shell
54 {
55     using System;
56     using System.Collections.Generic;
57     using System.Linq;
58     using System.Text;
59
60     /// <summary>
61     /// TODO: Update summary.
62     /// </summary>
63     [Export(typeof(FeedbackViewModel))]
64     public class FeedbackViewModel:Screen
65     {        
66         public string Message { get; set; }
67
68         public string Data { get; set; }
69
70         private IWindowManager _windowManager;
71
72         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
73
74
75         [ImportingConstructor]
76         public FeedbackViewModel(IWindowManager windowManager)
77         {
78             _windowManager = windowManager;
79             Data = GetBasicData() ;
80         }
81
82         public void AppendData(string data)
83         {
84             Data = Data + "\r\n" + data;
85         }
86
87         private string GetBasicData()
88         {
89             Assembly assembly = Assembly.GetExecutingAssembly();
90
91             var attributes = assembly.GetCustomAttributes(false);
92             var versionAtt = attributes.OfType<AssemblyInformationalVersionAttribute>().First();
93             var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
94             var released = DateTime.Parse(versionAtt.InformationalVersion);
95
96
97             var builder = new StringBuilder();
98             
99             builder.AppendLine("Pithos+ Windows Client")
100                 .Append('-', 30)
101                 .AppendLine()
102                 .AppendFormat("Released: {0}\r\n", released)
103                 .AppendFormat("File Version: {0}\r\n", fileVersion.FileVersion)
104                 .AppendFormat("Assembly Version: {0}\r\n", assembly.GetName().Version)
105                 .AppendFormat("OS: {0} 64Bit: {1}\r\n",Environment.OSVersion,Environment.Is64BitOperatingSystem)
106                 .AppendFormat("Wokring Set: {0} bytes\r\n", Environment.WorkingSet)
107                 .AppendFormat("Processors: {0} \r\n", Environment.ProcessorCount)                
108                 .Append('=', 30)
109                 .AppendLine();
110             var basicData = builder.ToString();
111             return basicData;
112
113         }
114         
115         public void Send()
116         {
117             var message = "Pithos+ Windows Client Feedback:\r\n" +Message;
118             var fields = new Dictionary<string, string> { { "feedback-msg", message }, { "feedback-data", Data } };
119             string token="0007";
120             if (Settings.Default.Accounts.Count>0)
121                 token = Settings.Default.Accounts[0].ApiKey;
122             try
123             {
124                 PostForm(Settings.Default.FeedbackUri, token,fields);
125             }
126             catch (Exception exc)
127             {
128                 Log.Error("Failed to submit feedback",exc);
129                 MessageBox.Show("There was an error while submitting your feedback. The error was logged", "Failed to submit feedback",
130                                 MessageBoxButton.OK, MessageBoxImage.Error);
131             }
132             
133             TryClose();
134         }
135
136         private void PostForm(string formUrl, string token,Dictionary<string, string> fields)
137         {
138
139             if (String.IsNullOrWhiteSpace(formUrl))
140                 throw new ArgumentNullException("formUrl");
141             if (String.IsNullOrWhiteSpace(token))
142                 throw new ArgumentNullException("token");
143             if (fields == null)
144                 throw new ArgumentNullException("fields");
145             Contract.EndContractBlock();
146
147
148             var client = new WebClient();
149             client.Headers.Add("X-Auth-Token", token);
150
151             var values=new NameValueCollection();
152             fields.Apply(field => values.Add(field.Key, Uri.EscapeDataString(field.Value)));
153             try
154             {
155                 client.UploadValues(formUrl, values);
156             }
157             catch (WebException exc)
158             {
159                 Log.WarnFormat("Unexpected status returned from feedback form: {0} - {1}", exc.Status,exc.Message);                    
160             }
161         }
162     }
163 }