PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/0.3/GmaWebService/Code/Helper/Utils.cs

#
C# | 274 lines | 233 code | 38 blank | 3 comment | 37 complexity | 74be62b8a52451181c7acaf24bd1b951 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. using System.Text;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using GmaWebService.Interfaces;
  10. namespace GmaWebService.Code.Helper
  11. {
  12. public class DBLocations
  13. {
  14. public string Music;
  15. public string Pictures;
  16. public string TvSeries;
  17. public string MovingPictures;
  18. public string Shares;
  19. public string Videos;
  20. }
  21. public class Utils
  22. {
  23. private static string logDir = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
  24. private static Dictionary<String, WebBannerPath> CachedWebBannerPaths = null;
  25. private static String CachedMPLocation;
  26. private static DBLocations CachedDbLocation;
  27. private static String CachedUser;
  28. private static String CachedPassword;
  29. public static void GetLogin(out string uid, out string pwd, bool overwriteCached)
  30. {
  31. if (overwriteCached || CachedUser == null || CachedPassword == null)
  32. {
  33. XmlDocument doc = new XmlDocument();
  34. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  35. XmlNode userNode = doc.SelectSingleNode("/appconfig/config/username");
  36. XmlNode passNode = doc.SelectSingleNode("/appconfig/config/password");
  37. CachedUser = userNode.InnerText;
  38. CachedPassword = passNode.InnerText;
  39. }
  40. uid = CachedUser;
  41. pwd = CachedPassword;
  42. }
  43. public static bool SetLogin(string uid, string pwd)
  44. {
  45. try
  46. {
  47. XmlDocument doc = new XmlDocument();
  48. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  49. XmlNode userNode = doc.SelectSingleNode("/appconfig/config/username");
  50. userNode.InnerText = uid;
  51. XmlNode passNode = doc.SelectSingleNode("/appconfig/config/password");
  52. passNode.InnerText = pwd;
  53. doc.Save(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  54. return true;
  55. }
  56. catch (Exception ex)
  57. {
  58. return false;
  59. }
  60. }
  61. public static String GetMpConfigPath()
  62. {
  63. if (CachedMPLocation == null)
  64. {
  65. XmlDocument doc = new XmlDocument();
  66. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  67. XmlNode gNode = doc.SelectSingleNode("/appconfig/config/mpconfig");
  68. string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  69. CachedMPLocation = gNode.InnerText.Replace("%ProgramData%", appData);
  70. }
  71. return CachedMPLocation;
  72. }
  73. public static DBLocations GetMPDbLocations()
  74. {
  75. if (Utils.CachedDbLocation == null)
  76. {
  77. DBLocations dbLocations = new DBLocations();
  78. XmlDocument doc = new XmlDocument();
  79. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  80. XmlNodeList dbNodes = doc.SelectNodes("/appconfig/mpdatabases/database");
  81. string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  82. Log.Debug("Reading database paths");
  83. foreach (XmlNode node in dbNodes)
  84. {
  85. string name = node.Attributes["name"].Value;
  86. // select alternateFilename only when the prefered one is non-existent or has a 0-byte size and the alternate one
  87. // exists and is bigger then 0 bytes
  88. string value = node.Attributes["filename"].Value.Replace("%ProgramData%", appData);
  89. string alternateValue = node.Attributes["alternateFilename"] != null ?
  90. node.Attributes["alternateFilename"].Value.Replace("%ProgramData%", appData) :
  91. null;
  92. if (alternateValue != null &&
  93. (!File.Exists(value) || new FileInfo(value).Length == 0) &&
  94. (File.Exists(alternateValue) && new FileInfo(alternateValue).Length > 0))
  95. value = alternateValue;
  96. Log.Debug(name + ": " + value);
  97. switch (name)
  98. {
  99. case "music":
  100. dbLocations.Music = value.Replace("%ProgramData%", appData);
  101. break;
  102. case "pictures":
  103. dbLocations.Pictures = value.Replace("%ProgramData%", appData);
  104. break;
  105. case "tvseries":
  106. dbLocations.TvSeries = value.Replace("%ProgramData%", appData);
  107. break;
  108. case "movingpictures":
  109. dbLocations.MovingPictures = value.Replace("%ProgramData%", appData);
  110. break;
  111. case "shares":
  112. dbLocations.Shares = value.Replace("%ProgramData%", appData);
  113. break;
  114. case "videos":
  115. dbLocations.Videos = value.Replace("%ProgramData%", appData);
  116. break;
  117. }
  118. }
  119. CachedDbLocation = dbLocations;
  120. }
  121. return CachedDbLocation;
  122. }
  123. public static void ChangeDbLocation(String _db, String _newPath)
  124. {
  125. XmlDocument doc = new XmlDocument();
  126. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  127. XmlNodeList dbNodes = doc.SelectNodes("/appconfig/mpdatabases/database");
  128. Log.Debug("Reading database paths");
  129. foreach (XmlNode node in dbNodes)
  130. {
  131. String name = node.Attributes["name"].Value;
  132. if (name.Equals(_db))
  133. {
  134. node.Attributes["filename"].Value = _newPath;
  135. }
  136. }
  137. doc.Save(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  138. }
  139. public static Dictionary<String, WebBannerPath> GetWebBannerPaths()
  140. {
  141. if (Utils.CachedWebBannerPaths == null)
  142. {
  143. XmlDocument doc = new XmlDocument();
  144. doc.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
  145. XmlNodeList dbNodes = doc.SelectNodes("/appconfig/thumbpaths/thumb");
  146. Dictionary<String, WebBannerPath> retList = new Dictionary<String, WebBannerPath>();
  147. string cappdata = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  148. foreach (XmlNode node in dbNodes)
  149. {
  150. retList.Add(node.Attributes["name"].Value, new WebBannerPath(node.Attributes["name"].Value,
  151. node.Attributes["path"].Value.Replace("%ProgramData%", cappdata),
  152. node.Attributes["virtualpath"].Value));
  153. }
  154. CachedWebBannerPaths = retList;
  155. return retList;
  156. }
  157. else
  158. {
  159. return CachedWebBannerPaths;
  160. }
  161. }
  162. public static String[] SplitString(String _stringToSplit)
  163. {
  164. if (_stringToSplit != null)
  165. {
  166. _stringToSplit = _stringToSplit.Trim(new char[] { '|', ' ' });
  167. return _stringToSplit.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
  168. }
  169. else
  170. {
  171. return null;
  172. }
  173. }
  174. public static string ClearString(String _stringToClean)
  175. {
  176. return _stringToClean.Substring(2, (_stringToClean.Length - 5));
  177. }
  178. public static string GetCoverArtName(string strFolder, string strFileName)
  179. {
  180. if (string.IsNullOrEmpty(strFolder) || string.IsNullOrEmpty(strFileName))
  181. return string.Empty;
  182. return string.Format(@"{0}\{1}{2}", strFolder, Utils.MakeFileName(strFileName), ".jpg");
  183. }
  184. public static string GetLargeCoverArtName(string strFolder, string strFileName)
  185. {
  186. if (string.IsNullOrEmpty(strFolder) || string.IsNullOrEmpty(strFileName))
  187. return string.Empty;
  188. return Utils.GetCoverArtName(strFolder, strFileName + "L");
  189. }
  190. public static string GetLargeAlbumCover(string artistName, string albumName)
  191. {
  192. if (string.IsNullOrEmpty(artistName) || string.IsNullOrEmpty(albumName))
  193. return string.Empty;
  194. artistName = artistName.Trim(new char[] { '|', ' ' });
  195. albumName = albumName.Replace(":", "_");
  196. String musicThumbPath = Utils.GetWebBannerPaths()["music"].Path;
  197. return Utils.GetCoverArtName(Path.Combine(musicThumbPath, "Albums"), artistName + "-" + albumName + "L");
  198. }
  199. public static string MakeFileName(string strText)
  200. {
  201. if (strText == null) return string.Empty;
  202. if (strText.Length == 0) return string.Empty;
  203. string strFName = strText.Replace(':', '_');
  204. strFName = strFName.Replace('/', '_');
  205. strFName = strFName.Replace('\\', '_');
  206. strFName = strFName.Replace('*', '_');
  207. strFName = strFName.Replace('?', '_');
  208. strFName = strFName.Replace('\"', '_');
  209. strFName = strFName.Replace('<', '_');
  210. strFName = strFName.Replace('>', '_');
  211. strFName = strFName.Replace('|', '_');
  212. bool unclean = true;
  213. char[] invalids = Path.GetInvalidFileNameChars();
  214. while (unclean)
  215. {
  216. unclean = false;
  217. char[] filechars = strFName.ToCharArray();
  218. foreach (char c in filechars)
  219. {
  220. if (!unclean)
  221. foreach (char i in invalids)
  222. {
  223. if (c == i)
  224. {
  225. unclean = true;
  226. //Log.Warn("Utils: *** File name {1} still contains invalid chars - {0}", Convert.ToString(c), strFName);
  227. strFName = strFName.Replace(c, '_');
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. return strFName;
  234. }
  235. }
  236. }