1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
//
// PlatformService.cs
//
// Author:
// Geoff Norton <gnorton@novell.com>
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Mono.Addins;
using MonoDevelop.Core;
using Mono.Unix;
using MonoDevelop.Ide.Extensions;
using MonoDevelop.Core.Execution;
namespace MonoDevelop.Ide.Desktop
{
public abstract class PlatformService
{
Hashtable iconHash = new Hashtable ();
public abstract DesktopApplication GetDefaultApplication (string mimetype);
public abstract DesktopApplication [] GetAllApplications (string mimetype);
public abstract string DefaultMonospaceFont { get; }
public abstract string Name { get; }
/// <summary>
/// Used in the text editor. Valid values are found in MonoDevelop.SourceEditor.ControlLeftRightMode in the
/// source editor project.
/// </summary>
public virtual string DefaultControlLeftRightBehavior {
get {
return "MonoDevelop";
}
}
public virtual void ShowUrl (string url)
{
Process.Start (url);
}
public string GetMimeTypeForUri (string uri)
{
if (!String.IsNullOrEmpty (uri)) {
FileInfo file = new FileInfo (uri);
MimeTypeNode mt = FindMimeTypeForFile (file.Name);
if (mt != null)
return mt.Id;
}
return OnGetMimeTypeForUri (uri) ?? "text/plain";
}
public string GetMimeTypeDescription (string mimeType)
{
if (mimeType == "text/plain")
return GettextCatalog.GetString ("Text file");
MimeTypeNode mt = FindMimeType (mimeType);
if (mt != null && mt.Description != null)
return mt.Description;
else
return OnGetMimeTypeDescription (mimeType) ?? string.Empty;
}
public bool GetMimeTypeIsText (string mimeType)
{
return GetMimeTypeIsSubtype (mimeType, "text/plain");
}
public bool GetMimeTypeIsSubtype (string subMimeType, string baseMimeType)
{
foreach (string mt in GetMimeTypeInheritanceChain (subMimeType))
if (mt == baseMimeType)
return true;
return false;
}
public IEnumerable<string> GetMimeTypeInheritanceChain (string mimeType)
{
yield return mimeType;
while (mimeType != null && mimeType != "text/plain") {
MimeTypeNode mt = FindMimeType (mimeType);
if (mt != null && !string.IsNullOrEmpty (mt.BaseType))
mimeType = mt.BaseType;
else {
if (mimeType.EndsWith ("+xml"))
mimeType = "application/xml";
else if (mimeType.StartsWith ("text") || OnGetMimeTypeIsText (mimeType))
mimeType = "text/plain";
else
break;
}
yield return mimeType;
}
}
public Gdk.Pixbuf GetPixbufForFile (string filename, Gtk.IconSize size)
{
Gdk.Pixbuf pic = null;
string icon = GetIconForFile (filename);
if (icon != null)
pic = ImageService.GetPixbuf (icon, size, false);
if (pic == null)
pic = OnGetPixbufForFile (filename, size);
if (pic == null) {
string mtype = GetMimeTypeForUri (filename);
if (mtype != null) {
foreach (string mt in GetMimeTypeInheritanceChain (mtype)) {
pic = GetPixbufForType (mt, size);
if (pic != null)
return pic;
}
}
}
return pic ?? GetDefaultIcon (size);
}
public Gdk.Pixbuf GetPixbufForType (string mimeType, Gtk.IconSize size)
{
Gdk.Pixbuf bf = (Gdk.Pixbuf) iconHash [mimeType+size];
if (bf != null)
return bf;
foreach (string type in GetMimeTypeInheritanceChain (mimeType)) {
// Try getting an icon name for the type
string icon = GetIconForType (type);
if (icon != null) {
bf = ImageService.GetPixbuf (icon, size, false);
if (bf != null)
break;
}
// Try getting a pixbuff
bf = OnGetPixbufForType (type, size);
if (bf != null)
break;
}
if (bf == null) {
bf = ImageService.GetPixbuf (mimeType, size, false);
if (bf == null)
bf = GetDefaultIcon (size);
}
iconHash [mimeType+size] = bf;
return bf;
}
Gdk.Pixbuf GetDefaultIcon (Gtk.IconSize size)
{
string id = "__default" + size;
Gdk.Pixbuf bf = (Gdk.Pixbuf) iconHash [id];
if (bf != null)
return bf;
string icon = DefaultFileIcon;
if (icon != null)
bf = ImageService.GetPixbuf (icon, size, false);
if (bf == null)
bf = OnGetDefaultFileIcon (size);
if (bf == null)
bf = ImageService.GetPixbuf ("md-regular-file", size, true);
iconHash [id] = bf;
return bf;
}
string GetIconForFile (string fileName)
{
MimeTypeNode mt = FindMimeTypeForFile (fileName);
if (mt != null)
return mt.Icon;
else
return OnGetIconForFile (fileName);
}
string GetIconForType (string type)
{
if (type == "text/plain")
return "md-text-file-icon";
MimeTypeNode mt = FindMimeType (type);
if (mt != null)
return mt.Icon;
else
return OnGetIconForType (type);
}
MimeTypeNode FindMimeTypeForFile (string fileName)
{
foreach (MimeTypeNode mt in AddinManager.GetExtensionNodes ("/MonoDevelop/Core/MimeTypes")) {
if (mt.SupportsFile (fileName))
return mt;
}
return null;
}
MimeTypeNode FindMimeType (string type)
{
foreach (MimeTypeNode mt in AddinManager.GetExtensionNodes ("/MonoDevelop/Core/MimeTypes")) {
if (mt.Id == type)
return mt;
}
return null;
}
protected virtual string OnGetMimeTypeForUri (string uri)
{
return null;
}
protected virtual string OnGetMimeTypeDescription (string mimeType)
{
return null;
}
protected virtual bool OnGetMimeTypeIsText (string mimeType)
{
return false;
}
protected virtual string OnGetIconForFile (string filename)
{
return null;
}
protected virtual string OnGetIconForType (string type)
{
return null;
}
protected virtual Gdk.Pixbuf OnGetPixbufForFile (string filename, Gtk.IconSize size)
{
return null;
}
protected virtual Gdk.Pixbuf OnGetPixbufForType (string type, Gtk.IconSize size)
{
return null;
}
protected virtual string DefaultFileIcon {
get { return null; }
}
protected virtual Gdk.Pixbuf OnGetDefaultFileIcon (Gtk.IconSize size)
{
return null;
}
public virtual bool SetGlobalMenu (MonoDevelop.Components.Commands.CommandManager commandManager, string commandMenuAddinPath)
{
return false;
}
// Used for preserve the file attributes when monodevelop opens & writes a file.
// This should work on unix & mac platform.
public virtual object GetFileAttributes (string fileName)
{
UnixFileSystemInfo info = UnixFileSystemInfo.GetFileSystemEntry (fileName);
if (info == null)
return null;
return info.FileAccessPermissions;
}
public virtual void SetFileAttributes (string fileName, object attributes)
{
if (attributes == null)
return;
UnixFileSystemInfo info = UnixFileSystemInfo.GetFileSystemEntry (fileName);
info.FileAccessPermissions = (FileAccessPermissions)attributes;
}
public virtual IProcessAsyncOperation StartConsoleProcess (string command, string arguments, string workingDirectory,
IDictionary<string, string> environmentVariables,
string title, bool pauseWhenFinished)
{
return null;
}
public virtual bool CanOpenTerminal {
get {
return false;
}
}
public virtual void OpenInTerminal (FilePath directory)
{
throw new InvalidOperationException ();
}
}
}
|