summaryrefslogtreecommitdiff
path: root/src/tools/Dtf/Inventory/products.cs
blob: 635e54395328a7bdf3d830748e9a1869bd275780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

using System;
using System.Data;
using System.Globalization;
using System.Collections;
using System.Windows.Forms;
using WixToolset.Dtf.WindowsInstaller;

namespace WixToolset.Dtf.Tools.Inventory
{
	/// <summary>
	/// Provides inventory data about products installed or advertised on the system.
	/// </summary>
	public class ProductsInventory : IInventoryDataProvider
	{
		public ProductsInventory()
		{
		}

		public string Description
		{
			get { return "Installed products"; }
		}

		public string[] GetNodes(InventoryDataLoadStatusCallback statusCallback)
		{
            statusCallback(0, "Products");
            ArrayList nodes = new ArrayList();
			nodes.Add("Products");
			foreach(ProductInstallation product in ProductInstallation.AllProducts)
			{
                nodes.Add("Products\\" + MsiUtils.GetProductName(product.ProductCode));
			}
			statusCallback(nodes.Count, String.Empty);
			return (string[]) nodes.ToArray(typeof(string));
		}

		public bool IsNodeSearchable(string searchRoot, string searchNode)
		{
			return true;
		}

		public DataView GetData(string nodePath)
		{
			string[] path = nodePath.Split('\\');

			if(path.Length == 1 && path[0] == "Products")
			{
				return this.GetAllProductsData();
			}
			else if(path.Length == 2 && path[0] == "Products")
			{
				return this.GetProductData(MsiUtils.GetProductCode(path[1]));
			}
			return null;
		}

		private DataView GetAllProductsData()
		{
			DataTable table = new DataTable("Products");
			table.Locale = CultureInfo.InvariantCulture;
			table.Columns.Add("ProductsProductName", typeof(string));
			table.Columns.Add("ProductsProductCode", typeof(string));

            foreach (ProductInstallation product in ProductInstallation.AllProducts)
            {
                string productName = MsiUtils.GetProductName(product.ProductCode);
                table.Rows.Add(new object[] { productName, product.ProductCode });
			}
			return new DataView(table, "", "ProductsProductName ASC", DataViewRowState.CurrentRows);
		}

		private DataView GetProductData(string productCode)
		{
			DataTable table = new DataTable("ProductProperties");
			table.Locale = CultureInfo.InvariantCulture;
			table.Columns.Add("ProductPropertiesProperty", typeof(string));
			table.Columns.Add("ProductPropertiesValue", typeof(string));

			// Add a fake "ProductCode" install property, just for display convenience.
			table.Rows.Add(new object[] { "ProductCode", productCode });

            ProductInstallation product = new ProductInstallation(productCode);

			foreach(string property in new string[]
			{
				"AssignmentType",
				"DiskPrompt",
				"HelpLink",
				"HelpTelephone",
				"InstalledProductName",
				"InstallDate",
				"InstallLocation",
				"InstallSource",
				"Language",
				"LastUsedSource",
				"LastUsedType",
				"LocalPackage",
				"MediaPackagePath",
				"PackageCode",
				"PackageName",
				"ProductIcon",
				"ProductID",
				"ProductName",
				"Publisher",
				"RegCompany",
				"RegOwner",
				"State",
				"transforms",
				"Uninstallable",
				"UrlInfoAbout",
				"UrlUpdateInfo",
				"Version",
				"VersionMinor",
				"VersionMajor",
				"VersionString"
			})
			{
				try
				{
                    string value = product[property];
					table.Rows.Add(new object[] { property,  (value != null ? value : "") });
				}
				catch(InstallerException iex)
				{
					table.Rows.Add(new object[] { property, iex.Message });
				}
				catch(ArgumentException) { }
			}
			return new DataView(table, "", "ProductPropertiesProperty ASC", DataViewRowState.CurrentRows);
		}

		public string GetLink(string nodePath, DataRow row)
		{
			string[] path = nodePath.Split('\\');

			if(path.Length == 1 && path[0] == "Products")
			{
				return String.Format(@"Products\{0}", MsiUtils.GetProductName((string) row["ProductsProductCode"]));
			}
			return null;
		}
	}
}