protected override void SetObjectData(SerializationReader sr) { base.SetObjectData(sr); _Protocol = (UrlProtocol)sr.ReadInt32(); _Url = sr.ReadString(); }
public UrlMessagePartModel(string url, string text) : this(url, text, UrlProtocol.None) { _Protocol = ParseProtocol(url); if (_Protocol == UrlProtocol.None) { _Protocol = UrlProtocol.Http; } }
public UrlMessagePartModel(TextMessagePartModel model) : base(model) { if (model is UrlMessagePartModel) { _Protocol = (model as UrlMessagePartModel)._Protocol; _Url = (model as UrlMessagePartModel)._Url; } }
public UrlMessagePartModel(string url) : base(url) { _Protocol = ParseProtocol(url); if (_Protocol == UrlProtocol.None) { _Protocol = UrlProtocol.Http; } }
/// <summary> /// Collects data about URL protocol handlers indicated by registered application capabilities. /// </summary> /// <param name="capsKey">A registry key containing capability information for a registered application.</param> /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param> /// <param name="capabilities">The capability list to add the collected data to.</param> /// <exception cref="IOException">There was an error accessing the registry.</exception> /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception> private static void CollectProtocolAssocsEx(RegistryKey capsKey, CommandMapper commandMapper, CapabilityList capabilities) { #region Sanity checks if (capsKey == null) { throw new ArgumentNullException(nameof(capsKey)); } if (commandMapper == null) { throw new ArgumentNullException(nameof(commandMapper)); } if (capabilities == null) { throw new ArgumentNullException(nameof(capabilities)); } #endregion using var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs); if (urlAssocKey == null) { return; } foreach (string protocol in urlAssocKey.GetValueNames()) { string?progID = urlAssocKey.GetValue(protocol)?.ToString(); if (string.IsNullOrEmpty(progID)) { continue; } using var progIDKey = Registry.ClassesRoot.OpenSubKey(progID); if (progIDKey == null) { continue; } var prefix = new KnownProtocolPrefix { Value = protocol }; var existing = capabilities.GetCapability <UrlProtocol>(progID); if (existing == null) { var capability = new UrlProtocol { ID = progID, Descriptions = { progIDKey.GetValue("", defaultValue: "")?.ToString() ?? "" }, KnownPrefixes = { prefix } }; capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper)); capabilities.Entries.Add(capability); } else { existing.KnownPrefixes.Add(prefix); } } }
/// <summary> /// Collects data about URL protocol handlers indicated by registered application capabilities. /// </summary> /// <param name="capsKey">A registry key containing capability information for a registered application.</param> /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param> /// <param name="capabilities">The capability list to add the collected data to.</param> /// <exception cref="IOException">There was an error accessing the registry.</exception> /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception> private static void CollectProtocolAssocsEx([NotNull] RegistryKey capsKey, [NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities) { #region Sanity checks if (capsKey == null) { throw new ArgumentNullException("capsKey"); } if (commandMapper == null) { throw new ArgumentNullException("commandMapper"); } if (capabilities == null) { throw new ArgumentNullException("capabilities"); } #endregion using (var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs)) { if (urlAssocKey == null) { return; } // TODO: Fold multiple prefixes pointing to one protocol together foreach (string protocol in urlAssocKey.GetValueNames()) { string progID = urlAssocKey.GetValue(protocol, "").ToString(); using (var progIDKey = Registry.ClassesRoot.OpenSubKey(progID)) { if (progIDKey == null) { continue; } var capability = new UrlProtocol { ID = progID, Descriptions = { progIDKey.GetValue("", "").ToString() }, KnownPrefixes = { new KnownProtocolPrefix { Value = protocol } } }; capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper)); capabilities.Entries.Add(capability); } } } }
public UrlMessagePartModel(string url, string text) : this(url, text, UrlProtocol.None) { _Protocol = ParseProtocol(url); if (_Protocol == UrlProtocol.None) { // assume http if no protocol was specified _Protocol = UrlProtocol.Http; // text should stay pristine if (Text == null) { Text = _Url; } _Url = String.Format("http://{0}", _Url); } }
/// <summary> /// Occures if the url protocoll requires registration. /// </summary> /// <param name="name">The application uri e.g. exampleApplication://</param> protected virtual void OnUrlProtocolRegistration(string name) { try { UrlProtocol.Register(name); } catch (UnauthorizedAccessException) { var location = Assembly.GetEntryAssembly().Location; var processInfo = new ProcessStartInfo(); processInfo.Verb = "runas"; processInfo.FileName = Path.Combine(Path.GetDirectoryName(location), Path.GetFileName(location).Replace(".vshost.exe", ".exe")); processInfo.Arguments = "registerUriScheme"; Process.Start(processInfo); } }
/// <summary> /// Registers all url protocols listed in <see cref="ApplicationUrlProtocols"/>. /// </summary> public static void RegisterUrlProtocols() { if (ApplicationUrlProtocols.Count == 0) { return; } try { foreach (var item in ApplicationUrlProtocols) { UrlProtocol.Register(item); } } catch (UnauthorizedAccessException) { UrlProtocol.RegisterElevated(ApplicationUrlProtocols[0]); return; } }
private async void ApplicationBase_Startup(object sender, StartupEventArgs e) { this.isInitialized = true; if (this.UrlProtocolNames != null && this.UrlProtocolNames.Length > 0) { for (int i = 0; i < this.UrlProtocolNames.Length; i++) { if (UrlProtocol.RequiresRegistration(this.UrlProtocolNames[i])) { this.OnUrlProtocolRegistration(this.UrlProtocolNames[i]); } } if (e.Args.Length > 0 && e.Args[0] == "registerUriScheme") { this.ShutdownMode = ShutdownMode.OnExplicitShutdown; this.Shutdown(0); return; } } // If an application is being run by VS then it will have the .vshost suffix to its proc // name. We have to also check them var proc = Process.GetCurrentProcess(); var processName = proc.ProcessName.Replace(".vshost", ""); var processes = Process.GetProcesses().Where(x => (x.ProcessName == processName || x.ProcessName == proc.ProcessName || x.ProcessName == proc.ProcessName + ".vshost") && x.Id != proc.Id).ToArray(); // Special case ... If we recieve a call from an uri protocol we will be passing this to // all instances of the application if (this.UrlProtocolNames != null && this.UrlProtocolNames.Length > 0 && e.Args.Length > 0 && processes.Length > 0 && this.IsUrlProtocol(e.Args)) { foreach (var process in processes.Where(x => x.Id != proc.Id)) { Win32Api.SendMessage(process.MainWindowHandle, $"{e.Args.Join("\n")}"); } this.ShutdownMode = ShutdownMode.OnExplicitShutdown; this.Shutdown(); } if (this.IsSingleInstance && processes.Length > 0) { var hwnd = processes[0].MainWindowHandle; if (hwnd != IntPtr.Zero) { Win32Api.SendMessage(hwnd, $"{e.Args.Join("\n")}"); this.ShutdownMode = ShutdownMode.OnExplicitShutdown; this.Shutdown(); } } else { if (this.IsSinglePage) { WindowType windowType = null; var window = Common.CreateWindow(ref windowType); window.ContentTemplateSelector = new CauldronTemplateSelector(); window.MinHeight = 353; window.MinWidth = 502; window.ShowInTaskbar = true; window.Topmost = false; window.WindowStartupLocation = WindowStartupLocation.Manual; window.WindowState = WindowState.Normal; window.Icon = await Win32Api.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location).ToBitmapImageAsync(); window.Title = ApplicationInfo.ApplicationName; PersistentWindowInformation.Load(window, this.GetType()); window.SizeChanged += (s, e1) => { if (window.WindowState == WindowState.Normal) { PersistentWindowProperties.SetHeight(window, e1.NewSize.Height); PersistentWindowProperties.SetWidth(window, e1.NewSize.Width); } }; window.Closing += (s, e1) => PersistentWindowInformation.Save(window, this.GetType()); window.Show(); window.Content = this; window.Activate(); await this.OnPreload(); var rootFrame = new NavigationFrame(); rootFrame.DataContext = this; window.Content = rootFrame; window.InputBindings.Add(new KeyBinding(new RelayCommand(async() => { await rootFrame.GoBack(); }, () => rootFrame.CanGoBack), Key.Back, ModifierKeys.None)); } else if (this.GetType().GetCustomAttribute <ViewAttribute>() != null || Application.Current.Resources.Contains($"View_{this.GetType().Name}")) { this.Navigator.As <Navigator>()?.NavigateInternal <ApplicationBase>(this, null, null); await this.OnPreload(); Application.Current.MainWindow.Activate(); } await this.OnStartup(new LaunchActivatedEventArgs(e.Args)); this.Navigator.TryClose(this); if (Application.Current.MainWindow != null) { HwndSource.FromHwnd(Application.Current.MainWindow.GetWindowHandle())?.AddHook(new HwndSourceHook(HandleMessages)); Application.Current.MainWindow.Activate(); } } }
/// <inheritdoc/> public UrlProtocolModel(UrlProtocol capability, bool used) : base(capability, used) { _urlProtocol = capability; }
private static string GetUrlMessage(UrlProtocol urlProtocol) { return(urlProtocol == UrlProtocol.NotRequired ? "Please enter a valid url" : "Please enter a fully qualified url"); }
/// <summary> /// Collects data about URL protocol handlers indicated by registered application capabilities. /// </summary> /// <param name="capsKey">A registry key containing capability information for a registered application.</param> /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param> /// <param name="capabilities">The capability list to add the collected data to.</param> /// <exception cref="IOException">There was an error accessing the registry.</exception> /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception> private static void CollectProtocolAssocsEx([NotNull] RegistryKey capsKey, [NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities) { #region Sanity checks if (capsKey == null) throw new ArgumentNullException("capsKey"); if (commandMapper == null) throw new ArgumentNullException("commandMapper"); if (capabilities == null) throw new ArgumentNullException("capabilities"); #endregion using (var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs)) { if (urlAssocKey == null) return; foreach (string protocol in urlAssocKey.GetValueNames()) { string progID = urlAssocKey.GetValue(protocol, "").ToString(); using (var progIDKey = Registry.ClassesRoot.OpenSubKey(progID)) { if (progIDKey == null) continue; var prefix = new KnownProtocolPrefix {Value = protocol}; var existing = capabilities.GetCapability<UrlProtocol>(progID); if (existing == null) { var capability = new UrlProtocol { ID = progID, Descriptions = {progIDKey.GetValue("", "").ToString()}, KnownPrefixes = {prefix} }; capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper)); capabilities.Entries.Add(capability); } else existing.KnownPrefixes.Add(prefix); } } } }
private static Regex GetUrlProtocolRegexVariant(UrlProtocol urlProtocol) { return(urlProtocol == UrlProtocol.NotRequired ? CommonRegularExpressions.UrlOnly : CommonRegularExpressions.UrlWithProtocol); }
/// <summary> /// Initializes a new instance of the UrlAttribute class. /// </summary> /// <param name="errorMessageAccessor"> /// A function which will return the error message /// to be shown on failure. /// </param> /// <param name="urlProtocol"> /// The format to validate. /// </param> public UrlAttribute(Func <string> errorMessageAccessor, UrlProtocol urlProtocol = UrlProtocol.NotRequired) : base(GetUrlProtocolRegexVariant(urlProtocol), errorMessageAccessor) { }
public UrlMessagePartModel(string url, string text, UrlProtocol protocol) : base(text) { _Url = url; _Protocol = protocol; }
private VerbCapability GetFileType([NotNull] string progID, [NotNull] CommandMapper commandMapper) { #region Sanity checks if (string.IsNullOrEmpty(progID)) throw new ArgumentNullException("progID"); if (commandMapper == null) throw new ArgumentNullException("commandMapper"); #endregion using (var progIDKey = Registry.ClassesRoot.OpenSubKey(progID)) { if (progIDKey == null) return null; VerbCapability capability; if (progIDKey.GetValue(DesktopIntegration.Windows.UrlProtocol.ProtocolIndicator) == null) { // Normal file type var fileType = new FileType {ID = progID}; foreach (var fileAssoc in FileAssocs.Where(fileAssoc => fileAssoc.Value == progID && !string.IsNullOrEmpty(fileAssoc.Key))) { using (var assocKey = Registry.ClassesRoot.OpenSubKey(fileAssoc.Key)) { if (assocKey == null) continue; fileType.Extensions.Add(new FileTypeExtension { Value = fileAssoc.Key, MimeType = assocKey.GetValue(DesktopIntegration.Windows.FileType.RegValueContentType, "").ToString(), PerceivedType = assocKey.GetValue(DesktopIntegration.Windows.FileType.RegValuePerceivedType, "").ToString() }); } } capability = fileType; } else { // URL protocol handler capability = new UrlProtocol {ID = progID}; } string description = progIDKey.GetValue(DesktopIntegration.Windows.FileType.RegValueFriendlyName, "").ToString(); if (string.IsNullOrEmpty(description)) description = progIDKey.GetValue("", "").ToString(); capability.Descriptions.Add(description); capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper)); // Only return capabilities that have verbs associated with them return capability.Verbs.Count == 0 ? null : capability; } }
/// <summary> /// Retrieves data about a specific file type or URL protocol from a snapshot diff. /// </summary> /// <param name="progID">The programmatic identifier of the file type.</param> /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param> /// <returns>Data about the file type or <see paramref="null"/> if no file type for this <paramref name="progID"/> was detected.</returns> /// <exception cref="IOException">There was an error accessing the registry.</exception> /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception> private VerbCapability?GetFileType(string progID, CommandMapper commandMapper) { #region Sanity checks if (string.IsNullOrEmpty(progID)) { throw new ArgumentNullException(nameof(progID)); } if (commandMapper == null) { throw new ArgumentNullException(nameof(commandMapper)); } #endregion using var progIDKey = Registry.ClassesRoot.OpenSubKey(progID); if (progIDKey == null) { return(null); } VerbCapability capability; if (progIDKey.GetValue(DesktopIntegration.Windows.UrlProtocol.ProtocolIndicator) == null) { // Normal file type var fileType = new FileType { ID = progID }; foreach (var fileAssoc in FileAssocs.Where(fileAssoc => fileAssoc.Value == progID && !string.IsNullOrEmpty(fileAssoc.Key))) { using var assocKey = Registry.ClassesRoot.OpenSubKey(fileAssoc.Key); if (assocKey == null) { continue; } fileType.Extensions.Add(new FileTypeExtension { Value = fileAssoc.Key, MimeType = assocKey.GetValue(DesktopIntegration.Windows.FileType.RegValueContentType)?.ToString(), PerceivedType = assocKey.GetValue(DesktopIntegration.Windows.FileType.RegValuePerceivedType)?.ToString() }); } capability = fileType; } else { // URL protocol handler capability = new UrlProtocol { ID = progID }; } var description = progIDKey.GetValue(DesktopIntegration.Windows.FileType.RegValueFriendlyName) ?? progIDKey.GetValue(""); if (description != null) { capability.Descriptions.Add(description.ToString()); } capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper)); // Only return capabilities that have verbs associated with them return(capability.Verbs.Count == 0 ? null : capability); }
/// <summary> /// Initializes a new instance of the UrlAttribute class. /// </summary> /// <param name="urlProtocol">Determines how to check the incoming URL.</param> public UrlAttribute(UrlProtocol urlProtocol = UrlProtocol.NotRequired) : base(GetUrlProtocolRegexVariant(urlProtocol), GetUrlMessage(urlProtocol)) { }
protected override void SetObjectData(SerializationReader sr) { base.SetObjectData(sr); _Protocol = (UrlProtocol) sr.ReadInt32(); }
/// <summary> /// Initializes a new instance of the UrlAttribute class. /// </summary> /// <param name="errorMessage"> /// The error message to be shown on validation failure. /// </param> /// <param name="urlProtocol"> /// The format to validate. /// </param> public UrlAttribute(string errorMessage, UrlProtocol urlProtocol = UrlProtocol.NotRequired) : base(GetUrlProtocolRegexVariant(urlProtocol), errorMessage) { }
public UrlMessagePartModel(string url, UrlProtocol protocol) : base(url) { _Protocol = protocol; }