added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Common / Source / CSharp / Shell90 / InstalledProductRegistrationAttribute.cs
blob0fbf9b47050be6e828b776eac59e84744a5079e1
1 //------------------------------------------------------------------------------
2 // <copyright from='2003' to='2004' company='Microsoft Corporation'>
3 // Copyright (c) Microsoft Corporation, All rights reserved.
4 // This code sample is provided "AS IS" without warranty of any kind,
5 // it is not recommended for use in a production environment.
6 // </copyright>
7 //------------------------------------------------------------------------------
9 using System;
10 using System.Globalization;
12 namespace Microsoft.VisualStudio.Shell
15 /// <summary>
16 /// This attribute registers an 'installed product' for your package.
17 /// This enables your package to present information on the VS
18 /// Splash Screen or Help About.
19 /// </summary>
20 [AttributeUsage(AttributeTargets.Class, Inherited=false)]
21 public sealed class InstalledProductRegistrationAttribute : RegistrationAttribute
24 private string _productName;
25 private string _name;
26 private string _productId;
27 private string _productDetails;
28 private string _iconResourceId;
29 private bool _useInterface = false;
30 private bool _usePackage = false;
32 /// <summary>
33 /// Creates a new InstalledProductRegistrationAttribute to register your package with
34 /// Help/About and enables it to present informations on the VS Splash Screen.
35 /// </summary>
36 public InstalledProductRegistrationAttribute (bool useInterface, string productName, string productDetails, string productId)
38 _usePackage = false;
39 _useInterface = useInterface;
40 if (_useInterface)
42 // If the object uses the interface in order to expose its informations to the
43 // Help/About page, then the registration must use the package, otherwise there
44 // is no way to know where to find such interface.
45 _usePackage = true;
47 else
49 // We are not using the interface, so we need the other parameters.
50 // Let's check that they are not null or empty.
51 if ((null == productName) || (productName.Trim().Length == 0))
52 throw new ArgumentNullException("productName");
53 productName = productName.Trim();
55 if ((null == productDetails) || (productDetails.Trim().Length == 0))
56 throw new ArgumentNullException("productDetails");
57 productDetails = productDetails.Trim();
59 if ((null == productId) || (productId.Trim().Length == 0))
60 throw new ArgumentNullException("productId");
61 productId = productId.Trim();
63 // Assign the values to the member variables
64 _productName = productName;
65 _productDetails = productDetails;
66 _productId = productId;
68 // Now that we know that the parameters are not empty, let's do a final
69 // validation: the Help/About code assumes that if a registration is made
70 // using a package, then both the product name and the product details are
71 // id of resources (so they are supposed to be in the form "#nnn"), but if
72 // they are strings, then they both must be string.
73 if (((ProductNameResourceID != 0) && (ProductDetailsResourceID == 0)) ||
74 ((ProductNameResourceID == 0) && (ProductDetailsResourceID != 0)))
76 // If we are here we have the resource id for only one entry and,
77 // according with the Help/About code, this is bad, so we have to
78 // throw an exception.
79 string errorMessage = string.Format(Resources.Culture, Resources.Reg_ErrorIncompatibleParametersTypes, "productName", "productDetails");
80 throw new ArgumentException(errorMessage);
83 // Now check if the name and details are resource id because in this case
84 // we have to use the package to register. Actually we check only the name
85 // because of the previous test.
86 _usePackage = (ProductNameResourceID != 0) ;
90 #region Properties
91 /// <summary>
92 /// Resource ID corresponding to the product name.
93 /// </summary>
94 public int ProductNameResourceID
96 get
98 if (_productName == null || _productName.Length < 2 || _productName[0]!='#' || !char.IsDigit(_productName[1]))
99 return 0;
100 return int.Parse( _productName.Substring(1), CultureInfo.InvariantCulture);
103 /// <summary>
104 /// Resource ID for the details.
105 /// </summary>
106 public int ProductDetailsResourceID
110 if (_productDetails == null || _productDetails.Length < 2 || _productDetails[0]!='#' || !char.IsDigit(_productDetails[1]))
111 return 0;
112 return int.Parse( _productDetails.Substring(1), CultureInfo.InvariantCulture);
115 /// <summary>
116 /// Resource ID of the icon.
117 /// </summary>
118 public int IconResourceID
122 if (String.IsNullOrEmpty(_iconResourceId) || _iconResourceId.Length < 2)
123 return 0;
124 return int.Parse( _iconResourceId.Substring(1), CultureInfo.InvariantCulture);
128 _iconResourceId = @"#" + value.ToString(CultureInfo.InvariantCulture);
131 /// <summary>
132 /// Your product ID.
133 /// </summary>
134 public string ProductId
136 get {return _productId;}
138 /// <summary>
139 /// The name of your product.
140 /// </summary>
141 public string ProductName
143 get { return _productName; }
146 /// <summary>
147 /// The name of your product.
148 /// </summary>
149 public string LanguageIndependentName
151 get { return _name; }
152 set { _name = value; }
155 private string GetNonEmptyName(RegistrationContext context)
157 string product = LanguageIndependentName;
158 if (product != null)
159 product = product.Trim();
160 if (String.IsNullOrEmpty(product))
161 product = context.ComponentType.Name;
162 return product;
164 /// <summary>
165 /// Detailed description of your product.
166 /// </summary>
167 public string ProductDetails
169 get {return _productDetails;}
171 /// <summary>
172 /// Use IVsInstalledProduct to fill in the Help about dialog.
173 /// The package must implement IVsInstalledProduct.
174 /// </summary>
175 public bool UseInterface
177 get {return _useInterface;}
179 /// <summary>
180 /// True is the product installation will use the package ID
181 /// </summary>
182 public bool UsePackage {
183 get
185 return _usePackage;
188 #endregion
190 private string RegKeyName (RegistrationContext context)
192 return string.Format(CultureInfo.InvariantCulture, "InstalledProducts\\{0}", GetNonEmptyName(context));
197 /// <summary>
198 /// Called to register this attribute with the given context.
199 /// </summary>
200 /// <param name="context">
201 /// Contains the location where the registration information should be placed.
202 /// It also contains other information such as the type being registered and path information.
203 /// </param>
204 public override void Register(RegistrationContext context)
206 if (UseInterface)
208 context.Log.WriteLine(Resources.Reg_NotifyInstalledProductInterface);
210 else
212 context.Log.WriteLine(Resources.Reg_NotifyInstalledProduct, GetNonEmptyName(context), ProductId);
215 using (Key packageKey = context.CreateKey(RegKeyName(context)))
218 // Set the 'Package' value if necessary
219 if (UsePackage)
221 packageKey.SetValue("Package", context.ComponentType.GUID.ToString("B"));
224 // Set the 'UseRegNameAsSplashName' flag if the user provided a short name that should be used on the splash screen
225 if (!String.IsNullOrEmpty(_name))
227 packageKey.SetValue("UseRegNameAsSplashName", 1);
230 // Set the 'UseInterface' value if necessary
231 if (UseInterface)
233 packageKey.SetValue("UseInterface", 1);
235 else
237 // If UseInterface is 0, then the following are required for HelpAbout
238 packageKey.SetValue("", ProductName);
239 packageKey.SetValue("ProductDetails", ProductDetails);
240 packageKey.SetValue("PID", ProductId);
242 // The icon resource id reg entry is only valid if there
243 // is a package satellite and not using the interface
244 if (UsePackage && !String.IsNullOrEmpty(_iconResourceId))
246 packageKey.SetValue("LogoID", _iconResourceId);
252 /// <summary>
253 /// Unregister this InstalledProducts entry.
254 /// </summary>
255 /// <param name="context"></param>
256 public override void Unregister(RegistrationContext context)
258 context.RemoveKey(RegKeyName(context));