Added missing converter
[pithos-ms-client] / trunk / NetSparkle / NetSparkleDeviceInventory.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.Management;
7 using System.Threading;
8
9 namespace AppLimit.NetSparkle
10 {
11     internal class NetSparkleDeviceInventory
12     {
13         public Boolean x64System { get; set; }
14         public uint ProcessorSpeed { get; set; }
15         public Int64 MemorySize { get; set; }
16         public string OsVersion { get; set; }
17         public int CPUCount { get; set; }
18
19         private NetSparkleConfiguration _config;
20
21         public NetSparkleDeviceInventory(NetSparkleConfiguration config)
22         {
23             _config = config;
24         }
25
26         public void CollectInventory()
27         {
28             // x64
29             CollectProcessorBitnes();
30
31             // cpu speed
32             CollectCPUSpeed();
33
34             // cpu count
35             CollectCPUCount();
36
37             // ram size
38             CollectRamSize();
39
40             // windows
41             CollectWindowsVersion();
42         }
43
44         public String BuildRequestUrl(String baseRequestUrl)
45         {
46             String retValue = baseRequestUrl;
47             
48             // x64 
49             retValue += "cpu64bit=" + (x64System ? "1" : "0") + "&";
50
51             // cpu speed
52             retValue += "cpuFreqMHz=" + ProcessorSpeed + "&";
53
54             // ram size
55             retValue += "ramMB=" + MemorySize + "&";
56
57             // Application name (as indicated by CFBundleName)
58             retValue += "appName=" + _config.ApplicationName + "&";
59
60             // Application version (as indicated by CFBundleVersion)
61             retValue += "appVersion=" + _config.InstalledVersion + "&";
62
63             // User’s preferred language
64             retValue += "lang=" + Thread.CurrentThread.CurrentUICulture.ToString() + "&";
65
66             // Windows version
67             retValue += "osVersion=" + OsVersion + "&";
68
69             // CPU type/subtype (see mach/machine.h for decoder information on this data)
70             // ### TODO: cputype, cpusubtype ###
71
72             // Mac model
73             // ### TODO: model ###
74
75             // Number of CPUs (or CPU cores, in the case of something like a Core Duo)
76             // ### TODO: ncpu ###
77             retValue += "ncpu=" + CPUCount + "&";
78
79             // sanitize url
80             retValue = retValue.TrimEnd('&');            
81
82             // go ahead
83             return retValue;
84         }
85
86         private void CollectWindowsVersion()
87         {
88             OperatingSystem osInfo = Environment.OSVersion;
89             OsVersion = string.Format("{0}.{1}.{2}.{3}", osInfo.Version.Major, osInfo.Version.Minor, osInfo.Version.Build, osInfo.Version.Revision);
90         }
91
92         private void CollectProcessorBitnes()
93         {
94              if (Marshal.SizeOf(typeof(IntPtr)) == 8)
95                 x64System = true;
96             else
97                 x64System = false;
98         }
99
100         private void CollectCPUCount()
101         {
102             CPUCount = Environment.ProcessorCount;
103         }
104
105         private void CollectCPUSpeed()
106         {
107             ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
108             ProcessorSpeed = (uint)(Mo["CurrentClockSpeed"]);
109             Mo.Dispose();            
110         }
111
112         private void CollectRamSize()
113         {
114             MemorySize = 0;
115
116             // RAM size
117             ManagementScope oMs = new ManagementScope();
118             ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
119             ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
120             ManagementObjectCollection oCollection = oSearcher.Get();
121             
122             Int64 mCap = 0;
123
124             // In case more than one Memory sticks are installed
125             foreach (ManagementObject mobj in oCollection)
126             {
127                 mCap = Convert.ToInt64(mobj["Capacity"]);
128                 MemorySize += mCap;
129             }
130
131             MemorySize = (MemorySize / 1024) / 1024;
132         }
133     }
134 }