Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Shell / FeedbackViewModel.cs @ b9f5b594

History | View | Annotate | Download (6 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="FeedbackViewModel.cs" company="GRNET">
3
// Copyright 2011 GRNET S.A. All rights reserved.
4
// 
5
// Redistribution and use in source and binary forms, with or
6
// without modification, are permitted provided that the following
7
// conditions are met:
8
// 
9
//   1. Redistributions of source code must retain the above
10
//      copyright notice, this list of conditions and the following
11
//      disclaimer.
12
// 
13
//   2. Redistributions in binary form must reproduce the above
14
//      copyright notice, this list of conditions and the following
15
//      disclaimer in the documentation and/or other materials
16
//      provided with the distribution.
17
// 
18
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
// POSSIBILITY OF SUCH DAMAGE.
30
// 
31
// The views and conclusions contained in the software and
32
// documentation are those of the authors and should not be
33
// interpreted as representing official policies, either expressed
34
// or implied, of GRNET S.A.
35
// </copyright>
36
// -----------------------------------------------------------------------
37

    
38

    
39
using System.Collections.Specialized;
40
using System.ComponentModel.Composition;
41
using System.Diagnostics;
42
using System.Diagnostics.Contracts;
43
using System.IO;
44
using System.Net;
45
using System.Reflection;
46
using System.Windows;
47
using Caliburn.Micro;
48
using Pithos.Client.WPF.Properties;
49

    
50
namespace Pithos.Client.WPF.Shell
51
{
52
    using System;
53
    using System.Collections.Generic;
54
    using System.Linq;
55
    using System.Text;
56

    
57
    /// <summary>
58
    /// TODO: Update summary.
59
    /// </summary>
60
    [Export(typeof(FeedbackViewModel))]
61
    public class FeedbackViewModel:Screen
62
    {        
63
        public string Message { get; set; }
64

    
65
        public string Data { get; set; }
66

    
67
        private IWindowManager _windowManager;
68

    
69
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos");
70

    
71

    
72
        [ImportingConstructor]
73
        public FeedbackViewModel(IWindowManager windowManager)
74
        {
75
            _windowManager = windowManager;
76
            Data = GetBasicData() ;
77
        }
78

    
79
        public void AppendData(string data)
80
        {
81
            Data = Data + "\r\n" + data;
82
        }
83

    
84
        private string GetBasicData()
85
        {
86
            Assembly assembly = Assembly.GetExecutingAssembly();
87

    
88
            var attributes = assembly.GetCustomAttributes(false);
89
            var versionAtt = attributes.OfType<AssemblyInformationalVersionAttribute>().First();
90
            var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
91
            var released = DateTime.Parse(versionAtt.InformationalVersion);
92

    
93

    
94
            var builder = new StringBuilder();
95
            
96
            builder.AppendLine("Pithos Windows Client")
97
                .Append('-', 30)
98
                .AppendLine()
99
                .AppendFormat("Released: {0}\r\n", released)
100
                .AppendFormat("File Version: {0}\r\n", fileVersion.FileVersion)
101
                .AppendFormat("Assembly Version: {0}\r\n", assembly.GetName().Version)
102
                .AppendFormat("OS: {0} 64Bit: {1}\r\n",Environment.OSVersion,Environment.Is64BitOperatingSystem)
103
                .AppendFormat("Wokring Set: {0} bytes\r\n", Environment.WorkingSet)
104
                .AppendFormat("Processors: {0} \r\n", Environment.ProcessorCount)                
105
                .Append('=', 30)
106
                .AppendLine();
107
            var basicData = builder.ToString();
108
            return basicData;
109

    
110
        }
111
        
112
        public void Send()
113
        {
114
            var message = "Pithos Windows Client Feedback:\r\n" +Message;
115
            var fields = new Dictionary<string, string> { { "feedback-msg", message }, { "feedback-data", Data } };
116
            string token="0007";
117
            if (Settings.Default.Accounts.Count>0)
118
                token = Settings.Default.Accounts[0].ApiKey;
119
            try
120
            {
121
                PostForm(Settings.Default.FeedbackUri, token,fields);
122
            }
123
            catch (Exception exc)
124
            {
125
                Log.Error("Failed to submit feedback",exc);
126
                MessageBox.Show("There was an error while submitting your feedback. The error was logged", "Failed to submit feedback",
127
                                MessageBoxButton.OK, MessageBoxImage.Error);
128
            }
129
            
130
            TryClose();
131
        }
132

    
133
        private void PostForm(string formUrl, string token,Dictionary<string, string> fields)
134
        {
135

    
136
            if (String.IsNullOrWhiteSpace(formUrl))
137
                throw new ArgumentNullException("formUrl");
138
            if (String.IsNullOrWhiteSpace(token))
139
                throw new ArgumentNullException("token");
140
            if (fields == null)
141
                throw new ArgumentNullException("fields");
142
            Contract.EndContractBlock();
143

    
144

    
145
            var client = new WebClient();
146
            client.Headers.Add("X-Auth-Token", token);
147

    
148
            var values=new NameValueCollection();
149
            fields.Apply(field => values.Add(field.Key, Uri.EscapeDataString(field.Value)));
150
            try
151
            {
152
                client.UploadValues(formUrl, values);
153
            }
154
            catch (WebException exc)
155
            {
156
                Log.WarnFormat("Unexpected status returned from feedback form: {0} - {1}", exc.Status,exc.Message);                    
157
            }
158
        }
159
    }
160
}