~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Services/DisplayBinding/ExternalProcessDisplayBinding.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.ComponentModel;
 
6
using System.Diagnostics;
 
7
using System.IO;
 
8
using ICSharpCode.Core;
 
9
 
 
10
namespace ICSharpCode.SharpDevelop
 
11
{
 
12
        /// <summary>
 
13
        /// Display binding for opening a file in an external process.
 
14
        /// </summary>
 
15
        [TypeConverter(typeof(ExternalProcessDisplayBindingConverter))]
 
16
        public sealed class ExternalProcessDisplayBinding : IDisplayBinding
 
17
        {
 
18
                public string FileExtension { get; set; }
 
19
                public string CommandLine { get; set; }
 
20
                public string Title { get; set; }
 
21
                public string Id { get; set; }
 
22
                
 
23
                public bool CanCreateContentForFile(string fileName)
 
24
                {
 
25
                        return string.Equals(Path.GetExtension(fileName), FileExtension, StringComparison.OrdinalIgnoreCase);
 
26
                }
 
27
                
 
28
                public ICSharpCode.SharpDevelop.Gui.IViewContent CreateContentForFile(OpenedFile file)
 
29
                {
 
30
                        if (file.IsDirty) {
 
31
                                // TODO: warn user that the file must be saved
 
32
                        }
 
33
                        try {
 
34
                                string cmd;
 
35
                                if (CommandLine.Contains("%1"))
 
36
                                        cmd = CommandLine.Replace("%1", file.FileName);
 
37
                                else
 
38
                                        cmd = CommandLine + " \"" + file.FileName + "\"";
 
39
                                StartCommandLine(cmd, Path.GetDirectoryName(file.FileName));
 
40
                        } catch (Exception ex) {
 
41
                                MessageService.ShowError(ex.Message);
 
42
                        }
 
43
                        return null;
 
44
                }
 
45
                
 
46
                static void StartCommandLine(string cmd, string workingDir)
 
47
                {
 
48
                        LoggingService.Debug("ExternalProcessDisplayBinding> " + cmd);
 
49
                        cmd = cmd.Trim();
 
50
                        if (cmd.Length == 0) return;
 
51
                        ProcessStartInfo info = new ProcessStartInfo();
 
52
                        if (cmd[0] == '"') {
 
53
                                int pos = cmd.IndexOf('"', 1);
 
54
                                info.FileName = cmd.Substring(1, pos - 1);
 
55
                                info.Arguments = cmd.Substring(pos + 1).TrimStart();
 
56
                        } else {
 
57
                                int pos = cmd.IndexOf(' ', 0);
 
58
                                info.FileName = cmd.Substring(0, pos);
 
59
                                info.Arguments = cmd.Substring(pos + 1);
 
60
                        }
 
61
                        info.WorkingDirectory = workingDir;
 
62
                        Process.Start(info);
 
63
                }
 
64
                
 
65
                public bool IsPreferredBindingForFile(string fileName)
 
66
                {
 
67
                        return false;
 
68
                }
 
69
                
 
70
                public double AutoDetectFileContent(string fileName, Stream fileContent, string detectedMimeType)
 
71
                {
 
72
                        return double.NegativeInfinity;
 
73
                }
 
74
        }
 
75
        
 
76
        sealed class ExternalProcessDisplayBindingConverter : TypeConverter
 
77
        {
 
78
                public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 
79
                {
 
80
                        if (sourceType == typeof(string))
 
81
                                return true;
 
82
                        else
 
83
                                return base.CanConvertFrom(context, sourceType);
 
84
                }
 
85
                
 
86
                public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 
87
                {
 
88
                        if (destinationType == typeof(string))
 
89
                                return true;
 
90
                        else
 
91
                                return base.CanConvertTo(context, destinationType);
 
92
                }
 
93
                
 
94
                public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
 
95
                {
 
96
                        if (destinationType == typeof(string)) {
 
97
                                ExternalProcessDisplayBinding binding = (ExternalProcessDisplayBinding)value;
 
98
                                return binding.Id + "|" + binding.FileExtension + "|" + binding.Title + "|" + binding.CommandLine;
 
99
                        } else {
 
100
                                return base.ConvertTo(context, culture, value, destinationType);
 
101
                        }
 
102
                }
 
103
                
 
104
                public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
 
105
                {
 
106
                        if (value is string) {
 
107
                                string[] values = value.ToString().Split('|');
 
108
                                if (values.Length == 4) {
 
109
                                        return new ExternalProcessDisplayBinding {
 
110
                                                Id = values[0],
 
111
                                                FileExtension = values[1],
 
112
                                                Title = values[2],
 
113
                                                CommandLine = values[3]
 
114
                                        };
 
115
                                } else {
 
116
                                        return null;
 
117
                                }
 
118
                        } else {
 
119
                                return base.ConvertFrom(context, culture, value);
 
120
                        }
 
121
                }
 
122
        }
 
123
}