~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/addins/GnomePlatform/GnomePlatform.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using Gnome;
 
30
using MonoDevelop.Ide.Desktop;
29
31
using System;
 
32
using System.Collections.Generic;
 
33
using System.Runtime.InteropServices;
 
34
using System.Diagnostics;
30
35
using System.IO;
31
 
using System.Collections;
32
 
using System.Diagnostics;
33
 
using System.Text.RegularExpressions;
34
 
using System.Runtime.InteropServices;
35
 
 
36
 
using MonoDevelop.Core.Gui;
37
 
using Gnome;
38
 
using Gnome.Vfs;
39
 
 
 
36
using MonoDevelop.Core.Execution;
 
37
using MonoDevelop.Core;
40
38
 
41
39
namespace MonoDevelop.Platform
42
40
{
54
52
                        } catch (Exception ex) {
55
53
                                Console.WriteLine (ex);
56
54
                        }
 
55
                        //apparently Gnome.Icon needs GnomeVFS initialized even when we're using GIO.
57
56
                        Gnome.Vfs.Vfs.Initialize ();
58
57
                }
59
58
 
63
62
                        if (useGio)
64
63
                                return Gio.GetDefaultForType (mimeType);
65
64
 
66
 
                        MimeApplication app = Mime.GetDefaultApplication (mimeType);
 
65
                        var app = Gnome.Vfs.Mime.GetDefaultApplication (mimeType);
67
66
                        if (app != null)
68
67
                                return (DesktopApplication) Marshal.PtrToStructure (app.Handle, typeof(DesktopApplication));
69
68
                        else
75
74
                        if (useGio)
76
75
                                return Gio.GetAllForType (mimeType);
77
76
 
78
 
                        ArrayList list = new ArrayList ();
79
 
                        MimeApplication[] apps = Mime.GetAllApplications (mimeType);
80
 
                        foreach (MimeApplication app in apps) {
81
 
                                DesktopApplication dap = (DesktopApplication) Marshal.PtrToStructure (app.Handle, typeof(DesktopApplication));
 
77
                        var list = new List<DesktopApplication> ();
 
78
                        var apps = Gnome.Vfs.Mime.GetAllApplications (mimeType);
 
79
                        foreach (var app in apps) {
 
80
                                var dap = (DesktopApplication) Marshal.PtrToStructure (app.Handle, typeof(DesktopApplication));
82
81
                                list.Add (dap);
83
82
                        }
84
 
                        return (DesktopApplication[]) list.ToArray (typeof(DesktopApplication));
 
83
                        return list.ToArray ();
85
84
                }
86
85
 
87
86
                protected override string OnGetMimeTypeDescription (string mt)
89
88
                        if (useGio)
90
89
                                return Gio.GetMimeTypeDescription (mt);
91
90
                        else
92
 
                                return Mime.GetDescription (mt);
 
91
                                return Gnome.Vfs.Mime.GetDescription (mt);
93
92
                }
94
93
 
95
94
                protected override string OnGetMimeTypeForUri (string uri)
96
95
                {
 
96
                        if (uri == null)
 
97
                                return null;
 
98
                        
97
99
                        if (useGio) {
98
100
                                string mt = Gio.GetMimeTypeForUri (uri);
99
101
                                if (mt != null)
100
102
                                        return mt;
101
103
                        }
102
 
                        return uri != null ? Gnome.Vfs.MimeType.GetMimeTypeForUri (ConvertFileNameToVFS (uri)) : null;
 
104
                        return Gnome.Vfs.MimeType.GetMimeTypeForUri (ConvertFileNameToVFS (uri));
103
105
                }
104
106
                
105
107
                protected override bool OnGetMimeTypeIsText (string mimeType)
140
142
                                string icon = null;
141
143
                                Gnome.IconLookupResultFlags result;
142
144
                                try {
143
 
                                        icon = Gnome.Icon.LookupSync (IconTheme.Default, thumbnailFactory, filename, null, Gnome.IconLookupFlags.None, out result);
 
145
                                        icon = Gnome.Icon.LookupSync (IconTheme.Default, thumbnailFactory, filename, null, 
 
146
                                                                      Gnome.IconLookupFlags.None, out result);
144
147
                                } catch {}
145
148
                                if (icon != null && icon.Length > 0)
146
149
                                        return icon;
173
176
                        result = result.Replace ("#", "%23");
174
177
                        result = result.Replace ("?", "%3F");
175
178
                        return result;
176
 
                }               
 
179
                }
 
180
                
 
181
                
 
182
                delegate string TerminalRunnerHandler (string command, string args, string dir, string title, bool pause);
 
183
        
 
184
                string terminal_command;
 
185
                bool terminal_probed;
 
186
                TerminalRunnerHandler runner;
 
187
                
 
188
                public override IProcessAsyncOperation StartConsoleProcess (string command, string arguments, string workingDirectory,
 
189
                                                                            IDictionary<string, string> environmentVariables, 
 
190
                                                                            string title, bool pauseWhenFinished)
 
191
                {
 
192
                        ProbeTerminal ();
 
193
                        
 
194
                        string exec = runner (command, arguments, workingDirectory, title, pauseWhenFinished);
 
195
                        var psi = new ProcessStartInfo (terminal_command, exec) {
 
196
                                CreateNoWindow = true,
 
197
                                UseShellExecute = false,
 
198
                        };
 
199
                        foreach (var env in environmentVariables)
 
200
                                psi.EnvironmentVariables [env.Key] = env.Value;
 
201
                        
 
202
                        ProcessWrapper proc = new ProcessWrapper ();
 
203
                        proc.StartInfo = psi;
 
204
                        proc.Start ();
 
205
                        return proc;
 
206
                }
 
207
                
 
208
#region Terminal runner implementations
 
209
                
 
210
                private static string GnomeTerminalRunner (string command, string args, string dir, string title, bool pause)
 
211
                {
 
212
                        string extra_commands = pause 
 
213
                                ? BashPause.Replace ("'", "\\\"")
 
214
                                : String.Empty;
 
215
                        
 
216
                        return String.Format (@" --disable-factory --title ""{4}"" -e ""bash -c 'cd {3} ; {0} {1} ; {2}'""",
 
217
                                command,
 
218
                                EscapeArgs (args),
 
219
                                extra_commands,
 
220
                                EscapeDir (dir),
 
221
                                title);
 
222
                }
 
223
                
 
224
                private static string XtermRunner (string command, string args, string dir, string title, bool pause)
 
225
                {
 
226
                        string extra_commands = pause 
 
227
                                ? BashPause
 
228
                                : String.Empty;
 
229
                        
 
230
                        return String.Format (@" -title ""{4}"" -e bash -c ""cd {3} ; '{0}' {1} ; {2}""",
 
231
                                command,
 
232
                                EscapeArgs (args),
 
233
                                extra_commands,
 
234
                                EscapeDir (dir),
 
235
                                title);
 
236
                }
 
237
                
 
238
                private static string EscapeArgs (string args)
 
239
                {
 
240
                        return args.Replace ("\\", "\\\\").Replace ("\"", "\\\"");
 
241
                }
 
242
                
 
243
                private static string EscapeDir (string dir)
 
244
                {
 
245
                        return dir.Replace (" ", "\\ ");
 
246
                }
 
247
                
 
248
                private static string BashPause {
 
249
                        get { return @"echo; read -p 'Press any key to continue...' -n1;"; }
 
250
                }
 
251
 
 
252
#endregion
 
253
 
 
254
#region Probing for preferred terminal
 
255
 
 
256
                private void ProbeTerminal ()
 
257
                {
 
258
                        if (terminal_probed) {
 
259
                                return;
 
260
                        }
 
261
                        
 
262
                        terminal_probed = true;
 
263
                        
 
264
                        string fallback_terminal = "xterm";
 
265
                        string preferred_terminal;
 
266
                        TerminalRunnerHandler preferred_runner = null;
 
267
                        TerminalRunnerHandler fallback_runner = XtermRunner;
 
268
 
 
269
                        if (!String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("GNOME_DESKTOP_SESSION_ID"))) {
 
270
                                preferred_terminal = "gnome-terminal";
 
271
                                preferred_runner = GnomeTerminalRunner;
 
272
                        }
 
273
                        else {
 
274
                                preferred_terminal = fallback_terminal;
 
275
                                preferred_runner = fallback_runner;
 
276
                        }
 
277
 
 
278
                        terminal_command = FindExec (preferred_terminal);
 
279
                        if (terminal_command != null) {
 
280
                                runner = preferred_runner;
 
281
                                return;
 
282
                        }
 
283
                        
 
284
                        FindExec (fallback_terminal);
 
285
                        runner = fallback_runner;
 
286
                }
 
287
 
 
288
                private string FindExec (string command)
 
289
                {
 
290
                        foreach (string path in GetExecPaths ()) {
 
291
                                string full_path = Path.Combine (path, command);
 
292
                                try {
 
293
                                        FileInfo info = new FileInfo (full_path);
 
294
                                        // FIXME: System.IO is super lame, should check for 0755
 
295
                                        if (info.Exists) {
 
296
                                                return full_path;
 
297
                                        }
 
298
                                } catch {
 
299
                                }
 
300
                        }
 
301
 
 
302
                        return null;
 
303
                }
 
304
 
 
305
                private string [] GetExecPaths ()
 
306
                {
 
307
                        string path = Environment.GetEnvironmentVariable ("PATH");
 
308
                        if (String.IsNullOrEmpty (path)) {
 
309
                                return new string [] { "/bin", "/usr/bin", "/usr/local/bin" };
 
310
                        }
 
311
 
 
312
                        // this is super lame, should handle quoting/escaping
 
313
                        return path.Split (':');
 
314
                }
 
315
 
 
316
#endregion
 
317
                
 
318
                //FIXME: probe for terminal
 
319
                static string TerminalCommand {
 
320
                        get {
 
321
                                return PropertyService.Get ("MonoDevelop.Shell", "gnome-terminal");
 
322
                        }
 
323
                }
 
324
                
 
325
                public override bool CanOpenTerminal {
 
326
                        get {
 
327
                                return true;
 
328
                        }
 
329
                }
 
330
                
 
331
                public override void OpenInTerminal (FilePath directory)
 
332
                {
 
333
                        Runtime.ProcessService.StartProcess (TerminalCommand, "", directory, null);
 
334
                }
177
335
        }
178
336
}