PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Collaboratori/Collaboratori/OutlookAddIn_Collaboratori/SattingsInvestaFinanceSync.cs

#
C# | 308 lines | 260 code | 29 blank | 19 comment | 15 complexity | e8e55c456cdbc55d21a098019761872f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. namespace OutlookAddIn_Collaboratori
  7. {
  8. public class FolderSattings
  9. {
  10. public string[,] collectionField;
  11. //Название листа SharePoint
  12. private string m_ListName;
  13. public string ListName
  14. {
  15. get { return m_ListName; }
  16. set { m_ListName = value; }
  17. }
  18. //Название папки в outlook
  19. private string m_nameFolder;
  20. public string NameFolder
  21. {
  22. get { return m_nameFolder; }
  23. set { m_nameFolder = value; }
  24. }
  25. //Класс сообщений
  26. private string m_MessageClass;
  27. public string MessageClass
  28. {
  29. get { return m_MessageClass; }
  30. set { m_MessageClass = value; }
  31. }
  32. //Url адресс листа на SharePoint
  33. private string m_Url;
  34. public string Url
  35. {
  36. get { return m_Url; }
  37. set { m_Url = value; }
  38. }
  39. //Тип корневой папки Outlook
  40. private string m_DefaultMessageClass;
  41. public string DefaultMessageClass
  42. {
  43. get { return m_DefaultMessageClass; }
  44. set { m_DefaultMessageClass = value; }
  45. }
  46. public FolderSattings() { }
  47. }
  48. public class AddinSattings//: Investafinance.Credential
  49. {
  50. public List<FolderSattings> Folders = new List<FolderSattings>();
  51. //логин
  52. private string m_UserName = null;
  53. public string UserName
  54. {
  55. get { return m_UserName; }
  56. set
  57. {
  58. if (m_UserName != null)
  59. {
  60. m_UserName = value;
  61. m_change = true;
  62. OnEventUpdateCreditional(new EventUpdateCreditionalEventArgs(m_UserName,m_Password,m_Domian));
  63. }
  64. else
  65. {
  66. m_UserName = value;
  67. }
  68. }
  69. }
  70. //домин
  71. private string m_Domian = null;
  72. public string Domian
  73. {
  74. get { return m_Domian; }
  75. set
  76. {
  77. if (m_Domian != null)
  78. {
  79. m_Domian = value;
  80. m_change = true;
  81. OnEventUpdateCreditional(new EventUpdateCreditionalEventArgs(m_UserName,m_Password,m_Domian));
  82. }
  83. else
  84. {
  85. m_Domian = value;
  86. }
  87. }
  88. }
  89. //пароль
  90. private string m_Password = null;
  91. public string Password
  92. {
  93. get { return m_Password; }
  94. set
  95. {
  96. if (m_Password != null)
  97. {
  98. m_Password = value;
  99. m_change = true;
  100. OnEventUpdateCreditional(new EventUpdateCreditionalEventArgs(m_UserName,m_Password,m_Domian));
  101. }
  102. else
  103. {
  104. m_Password = value;
  105. }
  106. }
  107. }
  108. //проверка на изменения
  109. private bool m_change = false;
  110. public bool Change
  111. {
  112. get { return m_change; }
  113. }
  114. //Конструктор
  115. public AddinSattings(string pathXML)
  116. {
  117. path = pathXML;
  118. doc = LoadConfig(path);
  119. LoadAddinSattings(doc);
  120. }
  121. private string path;
  122. private XmlDocument doc;
  123. //Диструктор
  124. //Необходимо внести изменения в файл конфигурации
  125. ~AddinSattings()
  126. {
  127. if (this.Change)
  128. {
  129. XmlNodeList nodeList = doc.SelectNodes("/Config/Connect");
  130. foreach (XmlAttribute atribut in nodeList.Item(0).Attributes)
  131. {
  132. switch (atribut.Name)
  133. {
  134. case ("UserName"):
  135. atribut.Value = this.UserName;
  136. break;
  137. case ("Domian"):
  138. atribut.Value = this.Domian;
  139. break;
  140. case ("Password"):
  141. atribut.Value = this.Password;
  142. break;
  143. }
  144. }
  145. doc.Save(path);
  146. }
  147. }
  148. //Загрузить нгастройки из файла
  149. private XmlDocument LoadConfig(string path)
  150. {
  151. XmlDocument doc = new XmlDocument();
  152. try
  153. {
  154. //загрузка из файла по пути path
  155. doc.Load(path);
  156. }
  157. catch
  158. {
  159. //в случае отсутствия xml на path создаём пустую xml
  160. doc.LoadXml("<Config> <Connect UserName='' Domian ='' Password=''/> <Folders><Folder Name='' MessageClass='' Url='' ListName=''><Field NameSharePoint='' NameOutlook='' ColumnNameOutlook='' ColumnTypeOutlook=''/></Folder></Folders></Config>");
  161. this.m_change = true;
  162. }
  163. return doc;
  164. }
  165. //выгрузить настройки из XmlDocument в AddinSattings
  166. private bool LoadAddinSattings(XmlDocument doc)
  167. {
  168. XmlNodeList nodeList = doc.SelectNodes("/Config/Folders/Folder");
  169. bool IsFolderAttribute = false;
  170. //AddinSattings Container = new AddinSattings();
  171. try
  172. {
  173. foreach (XmlNode Node in nodeList)
  174. {
  175. FolderSattings folderSattings = new FolderSattings();
  176. foreach (XmlAttribute atribut in Node.Attributes)
  177. {
  178. //считаем начальные значения
  179. switch (atribut.Name)
  180. {
  181. case ("Name"):
  182. folderSattings.NameFolder = atribut.Value;
  183. break;
  184. case ("MessageClass"):
  185. folderSattings.MessageClass = atribut.Value;
  186. break;
  187. case ("Url"):
  188. folderSattings.Url = atribut.Value;
  189. break;
  190. case ("ListName"):
  191. folderSattings.ListName = atribut.Value;
  192. break;
  193. case ("DefaultMessageClass"):
  194. folderSattings.DefaultMessageClass = atribut.Value;
  195. break;
  196. }
  197. IsFolderAttribute = true;
  198. }
  199. if (IsFolderAttribute)
  200. {
  201. XmlNodeList Fields = Node.ChildNodes;
  202. if (Fields.Count > 0)
  203. {
  204. string[,] collectionField = new string[Fields.Count, 4];
  205. int count = 0;
  206. foreach (XmlNode Field in Fields)
  207. {
  208. foreach (XmlAttribute atribut in Field.Attributes)
  209. {
  210. switch (atribut.Name)
  211. {
  212. case ("NameSharePoint"):
  213. collectionField[count, 0] = atribut.Value;
  214. break;
  215. case ("NameOutlook"):
  216. collectionField[count, 1] = atribut.Value;
  217. break;
  218. case ("ColumnNameOutlook"):
  219. collectionField[count, 2] = atribut.Value;
  220. break;
  221. case ("ColumnTypeOutlook"):
  222. collectionField[count, 3] = atribut.Value;
  223. break;
  224. }
  225. }
  226. count++;
  227. }
  228. folderSattings.collectionField = collectionField;
  229. }
  230. }
  231. IsFolderAttribute = false;
  232. this.Folders.Add(folderSattings);
  233. }
  234. nodeList = doc.SelectNodes("/Config/Connect");
  235. foreach (XmlAttribute atribut in nodeList.Item(0).Attributes)
  236. {
  237. switch (atribut.Name)
  238. {
  239. case ("UserName"):
  240. this.UserName = atribut.Value;
  241. break;
  242. case ("Domian"):
  243. this.Domian = atribut.Value;
  244. break;
  245. case ("Password"):
  246. this.Password = atribut.Value;
  247. break;
  248. }
  249. }
  250. }
  251. catch
  252. {
  253. System.Windows.Forms.MessageBox.Show("File configuration bad");
  254. return false;
  255. }
  256. return true;
  257. }
  258. public event EventHandler<EventUpdateCreditionalEventArgs> EventUpdateCreditional;
  259. protected virtual void OnEventUpdateCreditional(EventUpdateCreditionalEventArgs e)
  260. {
  261. EventHandler<EventUpdateCreditionalEventArgs> temp = EventUpdateCreditional;
  262. if (temp != null) temp(this, e);
  263. }
  264. }
  265. //Событие обновление поля
  266. public class EventUpdateCreditionalEventArgs : EventArgs
  267. {
  268. private readonly string m_name, m_password, m_domian;
  269. public EventUpdateCreditionalEventArgs(string name, string password, string domian)
  270. {
  271. m_name = name;
  272. m_password = password;
  273. m_domian = domian;
  274. }
  275. public string Name { get { return m_name; } }
  276. public string Password { get { return m_password; } }
  277. public string Domian { get { return m_domian; } }
  278. }
  279. }