1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.ComponentModel;
6
using System.Diagnostics;
8
using ICSharpCode.Core;
10
namespace ICSharpCode.SharpDevelop
13
/// Display binding for opening a file in an external process.
15
[TypeConverter(typeof(ExternalProcessDisplayBindingConverter))]
16
public sealed class ExternalProcessDisplayBinding : IDisplayBinding
18
public string FileExtension { get; set; }
19
public string CommandLine { get; set; }
20
public string Title { get; set; }
21
public string Id { get; set; }
23
public bool CanCreateContentForFile(string fileName)
25
return string.Equals(Path.GetExtension(fileName), FileExtension, StringComparison.OrdinalIgnoreCase);
28
public ICSharpCode.SharpDevelop.Gui.IViewContent CreateContentForFile(OpenedFile file)
31
// TODO: warn user that the file must be saved
35
if (CommandLine.Contains("%1"))
36
cmd = CommandLine.Replace("%1", file.FileName);
38
cmd = CommandLine + " \"" + file.FileName + "\"";
39
StartCommandLine(cmd, Path.GetDirectoryName(file.FileName));
40
} catch (Exception ex) {
41
MessageService.ShowError(ex.Message);
46
static void StartCommandLine(string cmd, string workingDir)
48
LoggingService.Debug("ExternalProcessDisplayBinding> " + cmd);
50
if (cmd.Length == 0) return;
51
ProcessStartInfo info = new ProcessStartInfo();
53
int pos = cmd.IndexOf('"', 1);
54
info.FileName = cmd.Substring(1, pos - 1);
55
info.Arguments = cmd.Substring(pos + 1).TrimStart();
57
int pos = cmd.IndexOf(' ', 0);
58
info.FileName = cmd.Substring(0, pos);
59
info.Arguments = cmd.Substring(pos + 1);
61
info.WorkingDirectory = workingDir;
65
public bool IsPreferredBindingForFile(string fileName)
70
public double AutoDetectFileContent(string fileName, Stream fileContent, string detectedMimeType)
72
return double.NegativeInfinity;
76
sealed class ExternalProcessDisplayBindingConverter : TypeConverter
78
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
80
if (sourceType == typeof(string))
83
return base.CanConvertFrom(context, sourceType);
86
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
88
if (destinationType == typeof(string))
91
return base.CanConvertTo(context, destinationType);
94
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
96
if (destinationType == typeof(string)) {
97
ExternalProcessDisplayBinding binding = (ExternalProcessDisplayBinding)value;
98
return binding.Id + "|" + binding.FileExtension + "|" + binding.Title + "|" + binding.CommandLine;
100
return base.ConvertTo(context, culture, value, destinationType);
104
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
106
if (value is string) {
107
string[] values = value.ToString().Split('|');
108
if (values.Length == 4) {
109
return new ExternalProcessDisplayBinding {
111
FileExtension = values[1],
113
CommandLine = values[3]
119
return base.ConvertFrom(context, culture, value);