PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Neolog/AppSettings.cs

http://neolog.codeplex.com
C# | 304 lines | 265 code | 38 blank | 1 comment | 17 complexity | 8f2d6b2be86253b89670695c6c0bc452 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.IO.IsolatedStorage;
  3. using System.Net;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Documents;
  7. using System.Windows.Ink;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using System.Diagnostics;
  13. using System.Text.RegularExpressions;
  14. namespace Neolog
  15. {
  16. public sealed class AppSettings
  17. {
  18. private static IsolatedStorageSettings isolatedStore;
  19. private static volatile AppSettings instance;
  20. private static object syncRoot = new Object();
  21. #region Constructor
  22. private AppSettings()
  23. {
  24. try
  25. {
  26. AppSettings.isolatedStore = IsolatedStorageSettings.ApplicationSettings;
  27. }
  28. catch (Exception e)
  29. {
  30. AppSettings.LogThis("Exception while using IsolatedStorageSettings: " + e.ToString());
  31. }
  32. }
  33. public static AppSettings Instance
  34. {
  35. get
  36. {
  37. if (instance == null)
  38. {
  39. lock (syncRoot)
  40. {
  41. if (instance == null)
  42. instance = new AppSettings();
  43. }
  44. }
  45. return instance;
  46. }
  47. }
  48. #endregion
  49. #region Variables
  50. public static string AppName = "Neolog";
  51. public static string DBConnectionString = "Data Source=isostore:/Neolog.sdf";
  52. public static string ServicesURL = "http://www.neolog.bg/service.php";
  53. public static bool InDebug = false;
  54. public static string DateTimeFormat = "dd-MM-yyyy HH:mm:ss";
  55. //public static string BackgroundColor = "#FF29435E";
  56. public static Color BackgroundColor = Color.FromArgb(255, 41, 67, 94);
  57. public static string[] CyrillicLetters = new string[] {"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"};
  58. public static string FacebookAppID = "";
  59. public static string FacebookAppSecret = "";
  60. public static string TwitterRequestTokenUri = "https://api.twitter.com/oauth/request_token";
  61. public static string TwitterAuthorizeUri = "https://api.twitter.com/oauth/authorize";
  62. public static string TwitterAccessTokenUri = "https://api.twitter.com/oauth/access_token";
  63. public static string TwitterCallbackUri = "oob";
  64. public static string TwitterStatusUpdateUrl { get { return "http://api.twitter.com"; } }
  65. public static string TwitterConsumerKey = "";
  66. public static string TwitterConsumerKeySecret = "";
  67. public static string TwitterOAuthVersion = "1.0a";
  68. public enum ServiceOp
  69. {
  70. ServiceOpUnknown,
  71. ServiceOpTexts,
  72. ServiceOpNests,
  73. ServiceOpSendWord,
  74. ServiceOpWords,
  75. ServiceOpWordComments,
  76. ServiceOpSendComment
  77. }
  78. #endregion
  79. #region Helpers
  80. public static void LogThis(params string[] logs)
  81. {
  82. if (AppSettings.InDebug)
  83. {
  84. Debugger.Log(3, "Warning", "[____Neolog-Log] " + string.Join(" ", logs));
  85. Debug.WriteLine("[____Neolog-Log] " + string.Join(" ", logs));
  86. }
  87. }
  88. public static string DoLongDate(DateTime dt)
  89. {
  90. return AppSettings.DoLongDate(dt, true, false);
  91. }
  92. public static string DoLongDate(DateTime dt, bool showTime, bool showYear)
  93. {
  94. if (dt == null)
  95. return "";
  96. string date = "";
  97. if (showTime)
  98. {
  99. date += dt.ToString("HH:mm");
  100. date += " ";
  101. }
  102. date += AppResources.ResourceManager.GetString("weekday_" + ((int)dt.DayOfWeek + 1));
  103. date += ", ";
  104. date += dt.Day;
  105. date += " ";
  106. date += AppResources.ResourceManager.GetString("monthFull_" + dt.Month);
  107. if (showYear)
  108. date += " " + dt.Year;
  109. return date;
  110. }
  111. public static string DoShortDate(DateTime dt)
  112. {
  113. if (dt == null)
  114. return "";
  115. string date = "";
  116. date += dt.ToString("HH:mm");
  117. date += " ";
  118. date += AppResources.ResourceManager.GetString("weekday_" + ((int)dt.DayOfWeek + 1));
  119. date += ", ";
  120. date += dt.Day;
  121. date += " ";
  122. date += AppResources.ResourceManager.GetString("monthShort_" + dt.Month);
  123. return date;
  124. }
  125. public static string Hyperlinkify(string strvar)
  126. {
  127. string final = strvar;
  128. Regex regex = new Regex(@"<nolink>(.*?)</nolink>",
  129. RegexOptions.IgnoreCase | RegexOptions.Singleline |
  130. RegexOptions.CultureInvariant |
  131. RegexOptions.IgnorePatternWhitespace |
  132. RegexOptions.Compiled);
  133. MatchCollection theMatches = regex.Matches(strvar);
  134. for (int index = 0; index < theMatches.Count; index++)
  135. final = final.Replace(theMatches[index].ToString(), theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
  136. regex = new Regex(@"<a(.*?)</a>", RegexOptions.IgnoreCase |
  137. RegexOptions.Singleline | RegexOptions.CultureInvariant |
  138. RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
  139. theMatches = regex.Matches(final);
  140. for (int index = 0; index < theMatches.Count; index++)
  141. {
  142. final = final.Replace(theMatches[index].ToString(),
  143. theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
  144. }
  145. final = Regex.Replace(final, @"(?<=\d)\.(?=\d)", "[[[pk:period]]]");
  146. Regex tags = new Regex(@"([a-zA-Z0-9\:/\-]*[a-zA-Z0-9\-_]\" +
  147. @".[a-zA-Z0-9\-_][a-zA-Z0-9\-_][a-zA-Z0-9\?\" +
  148. @"=&#_\-/\.]*[^<>,;\.\s\)\(\]\[\""])");
  149. final = tags.Replace(final, "<a href=\"http://$&\">$&</a>");
  150. final = final.Replace("http://https://", "https://");
  151. final = final.Replace("http://http://", "http://");
  152. final = final.Replace("[[[pk:period]]]", ".");
  153. final = final.Replace("<nolink>", "");
  154. final = final.Replace("</nolink>", "");
  155. return final;
  156. }
  157. public static bool ValidateEmail(string email)
  158. {
  159. return Regex.IsMatch(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
  160. }
  161. #endregion
  162. #region Settings
  163. const string ConfNameWordSync = "ConfNameWordSync";
  164. const string ConfNamePrivateData = "ConfNamePrivateData";
  165. const string ConfNamePDEmail = "ConfNamePDEmail";
  166. const string ConfNamePDName = "ConfNamePDName";
  167. const string ConfNamePDURL = "ConfNamePDURL";
  168. const bool ConfDefaultWordSync = true;
  169. const bool ConfDefaultPrivateData = true;
  170. const string ConfDefaultPDEmail = "";
  171. const string ConfDefaultPDName = "";
  172. const string ConfDefaultPDURL = "";
  173. public static bool AddOrUpdateValue(string Key, Object value)
  174. {
  175. bool valueChanged = false;
  176. if (isolatedStore.Contains(Key))
  177. {
  178. if (AppSettings.isolatedStore[Key] != value)
  179. {
  180. AppSettings.isolatedStore[Key] = value;
  181. valueChanged = true;
  182. }
  183. }
  184. else
  185. {
  186. AppSettings.isolatedStore.Add(Key, value);
  187. valueChanged = true;
  188. }
  189. return valueChanged;
  190. }
  191. public static valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
  192. {
  193. valueType value;
  194. if (AppSettings.isolatedStore.Contains(Key))
  195. value = (valueType)AppSettings.isolatedStore[Key];
  196. else
  197. value = defaultValue;
  198. return value;
  199. }
  200. public static void Save()
  201. {
  202. isolatedStore.Save();
  203. }
  204. public static bool ConfWordSync
  205. {
  206. get
  207. {
  208. return GetValueOrDefault<bool>(ConfNameWordSync, ConfDefaultWordSync);
  209. }
  210. set
  211. {
  212. AddOrUpdateValue(ConfNameWordSync, value);
  213. Save();
  214. }
  215. }
  216. public static bool ConfPrivateData
  217. {
  218. get
  219. {
  220. return GetValueOrDefault<bool>(ConfNamePrivateData, ConfDefaultPrivateData);
  221. }
  222. set
  223. {
  224. AddOrUpdateValue(ConfNamePrivateData, value);
  225. Save();
  226. }
  227. }
  228. public static string ConfPDEmail
  229. {
  230. get
  231. {
  232. return GetValueOrDefault<string>(ConfNamePDEmail, ConfDefaultPDEmail);
  233. }
  234. set
  235. {
  236. AddOrUpdateValue(ConfNamePDEmail, value);
  237. Save();
  238. }
  239. }
  240. public static string ConfPDName
  241. {
  242. get
  243. {
  244. return GetValueOrDefault<string>(ConfNamePDName, ConfDefaultPDName);
  245. }
  246. set
  247. {
  248. AddOrUpdateValue(ConfNamePDName, value);
  249. Save();
  250. }
  251. }
  252. public static string ConfPDURL
  253. {
  254. get
  255. {
  256. return GetValueOrDefault<string>(ConfNamePDURL, ConfDefaultPDURL);
  257. }
  258. set
  259. {
  260. AddOrUpdateValue(ConfNamePDURL, value);
  261. Save();
  262. }
  263. }
  264. #endregion
  265. }
  266. }