2 // Copyright (c) 2010-2014 Yves Langisch. All rights reserved.
3 // http://cyberduck.ch/
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // Bug fixes, suggestions and comments should be sent to:
20 using System
.Collections
.Generic
;
21 using System
.ComponentModel
;
22 using System
.Diagnostics
;
23 using System
.Globalization
;
26 using System
.Reflection
;
27 using ch
.cyberduck
.core
;
28 using ch
.cyberduck
.core
.local
;
30 using Microsoft
.Win32
;
31 using org
.apache
.commons
.io
;
32 using org
.apache
.log4j
;
33 using Collection
= java
.util
.Collection
;
35 namespace Ch
.Cyberduck
.Core
39 public delegate object ApplyPerItemForwardDelegate
<T
>(T item
);
41 public delegate T ApplyPerItemReverseDelegate
<T
>(object item
);
43 private static readonly List
<String
> ExtendedCharsets
= new List
<string>
69 public static readonly bool IsVistaOrLater
= OperatingSystemVersion
.Current
>= OSVersionInfo
.Vista
;
70 public static readonly bool IsWin7OrLater
= OperatingSystemVersion
.Current
>= OSVersionInfo
.Win7
;
71 private static readonly Logger Log
= Logger
.getLogger(typeof (Utils
).FullName
);
73 public static bool IsBlank(string value)
75 if (String
.IsNullOrEmpty(value))
80 return String
.IsNullOrEmpty(value.Trim());
83 public static Assembly
Me()
85 return Assembly
.GetExecutingAssembly();
88 public static bool IsNotBlank(string value)
90 return !IsBlank(value);
94 /// Get file extension. Ignores OS specific special characters. Includes the dot if available.
96 /// <param name="filename"></param>
97 /// <returns></returns>
98 public static string GetSafeExtension(string filename
)
100 if (IsNotBlank(filename
))
102 //see http://windevblog.blogspot.com/2008/09/get-default-application-in-windows-xp.html
103 string extension
= FilenameUtils
.getExtension(filename
);
104 if (IsBlank(extension
))
108 return "." + extension
;
113 public static string SafeString(string s
)
115 return s
?? string.Empty
;
119 /// Check if a given object is parseable to an int32
121 /// <param name="expression"></param>
122 /// <returns></returns>
123 public static bool IsInt(object expression
)
127 bool isNum
= int.TryParse(Convert
.ToString(expression
), NumberStyles
.Any
, NumberFormatInfo
.InvariantInfo
,
133 /// Convert a generic IEnumerable to a java list
135 /// <typeparam name="T"></typeparam>
136 /// <param name="list">IEnumerable to convert</param>
137 /// <param name="applyPerItem">Apply this delegate to all list items before adding</param>
138 /// <returns>A java list</returns>
139 public static List ConvertToJavaList
<T
>(IEnumerable
<T
> list
, ApplyPerItemForwardDelegate
<T
> applyPerItem
)
141 ArrayList javaList
= new ArrayList();
142 foreach (T item
in list
)
144 if (null != applyPerItem
)
146 javaList
.add(applyPerItem(item
));
157 /// <typeparam name="K"></typeparam>
158 /// <typeparam name="V"></typeparam>
159 /// <param name="dictionary"></param>
160 /// <returns></returns>
161 public static Map ConvertToJavaMap
<K
, V
>(IDictionary
<K
, V
> dictionary
)
163 Map javaMap
= new HashMap();
164 foreach (KeyValuePair
<K
, V
> pair
in dictionary
)
166 javaMap
.put(pair
.Key
, pair
.Value
);
174 /// <typeparam name="K"></typeparam>
175 /// <typeparam name="V"></typeparam>
176 /// <param name="javaMap"></param>
177 /// <returns></returns>
178 public static IDictionary
<K
, V
> ConvertFromJavaMap
<K
, V
>(Map javaMap
)
180 IDictionary
<K
, V
> result
= new Dictionary
<K
, V
>();
181 Iterator iterator
= javaMap
.entrySet().iterator();
182 while (iterator
.hasNext())
184 Map
.Entry entry
= (Map
.Entry
) iterator
.next();
185 result
.Add((K
) entry
.getKey(), (V
) entry
.getValue());
191 /// Convert a generic IEnumerable to a java list
193 /// <typeparam name="T"></typeparam>
194 /// <param name="list">IEnumerable to convert</param>
195 /// <returns>A java list</returns>
196 public static List ConvertToJavaList
<T
>(IEnumerable
<T
> list
)
198 return ConvertToJavaList(list
, null);
202 /// Convert a java list to a generic collection
204 /// <typeparam name="T"></typeparam>
205 /// <param name="collection"></param>
206 /// <returns>A List<typeparamref name="T"/></returns>
207 public static ICollection
<T
> ConvertFromJavaList
<T
>(Collection collection
)
209 return ConvertFromJavaList
<T
>(collection
, null);
213 /// Convert a java list to a generic collection
215 /// <typeparam name="T"></typeparam>
216 /// <param name="collection"></param>
217 /// <param name="applyPerItem">Apply this delegate to all list items before adding</param>
218 /// <returns>A List<typeparamref name="T"/></returns>
219 public static IList
<T
> ConvertFromJavaList
<T
>(Collection collection
, ApplyPerItemReverseDelegate
<T
> applyPerItem
)
221 List
<T
> result
= new List
<T
>(collection
.size());
222 for (Iterator iterator
= collection
.iterator(); iterator
.hasNext();)
224 Object next
= iterator
.next();
225 if (null != applyPerItem
)
227 result
.Add(applyPerItem(next
));
230 result
.Add((T
) next
);
235 public static IList
<KeyValuePair
<string, string>> OpenWithListForExtension(String ext
)
237 IList
<String
> progs
= new List
<string>();
238 List
<KeyValuePair
<string, string>> map
= new List
<KeyValuePair
<string, string>>();
240 if (IsBlank(ext
)) return map
;
242 if (!ext
.StartsWith(".")) ext
= "." + ext
;
243 using (RegistryKey clsExt
= Registry
.ClassesRoot
.OpenSubKey(ext
))
245 IList
<string> rootList
= OpenWithListForExtension(ext
, clsExt
);
246 foreach (string s
in rootList
)
253 Registry
.CurrentUser
.OpenSubKey(
254 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + ext
))
256 IList
<string> explorerList
= OpenWithListForExtension(ext
, clsExt
);
257 foreach (string s
in explorerList
)
263 foreach (string exe
in progs
.Distinct())
265 ApplicationFinder finder
= ApplicationFinderFactory
.get();
266 Application application
= finder
.getDescription(exe
);
267 if (finder
.isInstalled(application
))
269 map
.Add(new KeyValuePair
<string, string>(application
.getName(), exe
));
273 map
.Add(new KeyValuePair
<string, string>(LocalFactory
.get(exe
).getName(), exe
));
277 delegate(KeyValuePair
<string, string> pair1
, KeyValuePair
<string, string> pair2
)
279 return pair1
.Key
.CompareTo(pair2
.Key
);
285 public static IList
<String
> OpenWithListForExtension(String ext
, RegistryKey rootKey
)
287 IList
<String
> result
= new List
<string>();
292 String perceivedType
= (String
) rootKey
.GetValue("PerceivedType");
293 if (null != perceivedType
)
296 RegistryKey openWithKey
=
297 Registry
.ClassesRoot
.OpenSubKey("SystemFileAssociations\\" + perceivedType
+
300 IList
<String
> appCmds
= GetApplicationCmdsFromOpenWithList(openWithKey
);
301 foreach (string appCmd
in appCmds
)
309 using (RegistryKey key
= rootKey
.OpenSubKey("OpenWithProgIds"))
311 IList
<String
> appCmds
= GetApplicationCmdsFromOpenWithProgIds(key
);
312 foreach (string appCmd
in appCmds
)
319 using (RegistryKey openWithKey
= rootKey
.OpenSubKey("OpenWithList"))
321 IList
<String
> appCmds
= GetApplicationCmdsFromOpenWithList(openWithKey
);
322 foreach (string appCmd
in appCmds
)
331 private static IList
<String
> GetApplicationCmdsFromOpenWithList(RegistryKey openWithKey
)
333 IList
<String
> appCmds
= new List
<string>();
334 if (openWithKey
!= null)
337 string[] exes
= openWithKey
.GetSubKeyNames();
338 IList
<String
> cands
= exes
.ToList();
340 string[] values
= openWithKey
.GetValueNames();
341 foreach (string value in values
)
343 object o
= openWithKey
.GetValue(value);
346 cands
.Add(o
as String
);
351 foreach (string s
in exes
)
356 foreach (string progid
in cands
)
358 using (RegistryKey key
= Registry
.ClassesRoot
.OpenSubKey("Applications\\" + progid
))
360 String cmd
= GetExeFromOpenCommand(key
);
361 if (!String
.IsNullOrEmpty(cmd
))
371 private static IList
<String
> GetApplicationCmdsFromOpenWithProgIds(RegistryKey key
)
373 IList
<String
> appCmds
= new List
<string>();
376 string[] progids
= key
.GetValueNames();
377 foreach (string progid
in progids
)
379 if (!string.IsNullOrEmpty(progid
))
381 using (RegistryKey clsProgid
= Registry
.ClassesRoot
.OpenSubKey(progid
))
383 String cmd
= GetExeFromOpenCommand(clsProgid
);
384 if (!String
.IsNullOrEmpty(cmd
))
395 public static bool StartProcess(Process process
)
402 catch (InvalidOperationException e
)
406 catch (Win32Exception e
)
408 Log
.error(String
.Format("Error while StartProcess: {0},{1}", e
.Message
, e
.NativeErrorCode
));
413 public static string ExtractApplicationPath(string cmd
)
415 if (!String
.IsNullOrEmpty(cmd
) && !cmd
.Contains("rundll32.exe"))
417 String command
= null;
418 if (cmd
.StartsWith("\""))
420 int i
= cmd
.IndexOf("\"", 1);
422 command
= cmd
.Substring(1, i
- 1);
426 int i
= cmd
.IndexOf(" ");
428 command
= cmd
.Substring(0, i
);
431 if (File
.Exists(command
))
440 /// Extract open command
442 /// <param name="root">expected substructure is shell/open/command</param>
443 /// <returns>null if not found</returns>
444 public static string GetExeFromOpenCommand(RegistryKey root
)
448 using (var editSk
= root
.OpenSubKey("shell\\open\\command"))
452 String cmd
= (String
) editSk
.GetValue(String
.Empty
);
453 return ExtractApplicationPath(cmd
);
461 /// method for retrieving the users default web browser
463 /// <returns></returns>
464 public static string GetSystemDefaultBrowser()
468 //for Vista and later we first check the UserChoice
471 Registry
.CurrentUser
.OpenSubKey(
472 @"HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice")
477 string progid
= (string) uc
.GetValue("Progid");
480 string exe
= GetExeFromOpenCommand(Registry
.ClassesRoot
);
489 //set the registry key we want to open
490 using (var regKey
= Registry
.ClassesRoot
.OpenSubKey("HTTP\\shell\\open\\command", false))
492 return ExtractExeFromCommand((string) regKey
.GetValue(null));
501 public static string ExtractExeFromCommand(string command
)
503 if (!String
.IsNullOrEmpty(command
))
506 if (command
.StartsWith("\""))
508 int i
= command
.IndexOf("\"", 1);
510 cmd
= command
.Substring(1, i
- 1);
514 int i
= command
.IndexOf(" ");
516 cmd
= command
.Substring(0, i
);
519 if (null != cmd
&& LocalFactory
.get(cmd
).exists())