PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Iface/LicensesSystem.cs

https://github.com/moscrif/ide
C# | 180 lines | 148 code | 31 blank | 1 comment | 23 complexity | aaf95e794a889eb58ef28ea236325af5 MD5 | raw file
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Collections.Specialized;
  6. using System.IO;
  7. using System.Timers;
  8. using System.Threading;
  9. using System.Collections.Generic;
  10. using System.Security.Cryptography;
  11. using System.Reflection;
  12. using Moscrif.IDE.Tool;
  13. using Moscrif.IDE.Controls;
  14. using Moscrif.IDE.Iface.Entities;
  15. using System.Linq;
  16. namespace Moscrif.IDE.Iface
  17. {
  18. public class LicensesSystem
  19. {
  20. private string licenceUrl = MainClass.Settings.licenceUrl ;
  21. private string licenceFile ;
  22. public Licenses Licenses;
  23. public License GetLicence(string typ){
  24. if(Licenses.Items==null || Licenses.Items.Count<=0)
  25. return null;
  26. return Licenses.Items.Find(x=>x.Typ==typ);
  27. }
  28. public License GetNextLicence(string typ){
  29. int iTyp =0;
  30. if(Int32.TryParse(typ,out iTyp)){
  31. iTyp = iTyp-100;
  32. }
  33. return GetLicence(iTyp.ToString());
  34. }
  35. public LicensesSystem()
  36. {
  37. Licenses = Licenses.OpenLicensesCache();
  38. GenerateList();
  39. }
  40. private void GenerateList(){
  41. ListFrameworkClass = new List<FrameworkClass>();
  42. ListFrameworkClass.Add(new FrameworkClass("facebook","libraries_facebook","net - Facebook",LicenceTyp.BASIC));
  43. ListFrameworkClass.Add(new FrameworkClass("sqlite","libraries_sqlitelib","sqlite",LicenceTyp.BASIC));
  44. ListFrameworkClass.Add(new FrameworkClass("zip","libraries_ziplib","zip",LicenceTyp.BASIC));
  45. ListFrameworkClass.Add(new FrameworkClass("ads","libraries_ad","ads",LicenceTyp.BASIC));
  46. ListFrameworkClass.Add(new FrameworkClass("crypto","libraries_cryptolib","crypto",LicenceTyp.PROFESIONAL));
  47. ListFrameworkClass.Add(new FrameworkClass("store","libraries_storelib","store",LicenceTyp.PROFESIONAL));
  48. ListFrameworkClass.Add(new FrameworkClass("signapp","basic_compiledsourcecode","compile & sign",LicenceTyp.PROFESIONAL));
  49. ListFrameworkClass.Add(new FrameworkClass("marketdistribution","---","Market - Distribution",LicenceTyp.BASIC));
  50. ListFrameworkClass.Add(new FrameworkClass("conditions","---","Conditions",LicenceTyp.BASIC));
  51. ListFrameworkClass.Add(new FrameworkClass("androidsupporteddevices","---","Android supported devices",LicenceTyp.PROFESIONAL));
  52. ListFrameworkClass.Add(new FrameworkClass("windowsandmac","publishtools_desktop_1","Windows and Mac OS",LicenceTyp.BASIC));
  53. }
  54. public License GetUserLicense(){
  55. if(MainClass.User!=null){
  56. if(MainClass.User.License!= null)
  57. return MainClass.User.License;
  58. }
  59. return GetLicence("-100");
  60. }
  61. public List<FrameworkClass> ListFrameworkClass;
  62. public void LoadFromWeb(){
  63. if(GetLicenses() && !string.IsNullOrEmpty(licenceFile)){
  64. Licenses = Licenses.LoadLicenses(licenceFile);
  65. }
  66. if(!Licenses.IsCache){
  67. Licenses.SaveLicensesCache();
  68. }
  69. }
  70. public List<Feature> GetUserDifferent(License lcs2 ){
  71. if(lcs2== null) return new List<Feature>();
  72. License userLicence = MainClass.LicencesSystem.GetUserLicense();
  73. if(userLicence == null){
  74. return lcs2.Featutes;
  75. }
  76. List<Feature> difList = new List<Feature>(lcs2.Featutes.Except(userLicence.Featutes,new FeatureComparer()).ToArray());
  77. return difList;
  78. }
  79. public bool CheckFunction(string functionName,Gtk.Window parentError){
  80. string userLicenceId ="-100";
  81. if(MainClass.User != null){
  82. userLicenceId = MainClass.User.LicenseId;
  83. }
  84. int iTyp =0;
  85. if(!Int32.TryParse(userLicenceId,out iTyp)){
  86. iTyp = -100;
  87. }
  88. FrameworkClass fc = ListFrameworkClass.Find(x=>x.Name == functionName);
  89. if(fc!= null){
  90. if(iTyp <= (int)fc.Typ ){
  91. return true;
  92. } else
  93. {
  94. BuyDialog ld = new BuyDialog((int)fc.Typ,fc.Title,parentError);
  95. if(ld.Run() == (int)Gtk.ResponseType.Ok){
  96. }
  97. ld.Destroy();
  98. return true;
  99. //return false;
  100. }
  101. }
  102. return true;
  103. }
  104. public bool GetLicenses(){
  105. string URL =licenceUrl;
  106. SystemWebClient client = new SystemWebClient();
  107. try{
  108. licenceFile = client.DownloadString(new Uri(URL));
  109. }catch(Exception ex){
  110. string statusDesc = "";
  111. GetStatusCode(client,out statusDesc);
  112. Console.WriteLine(ex.Message);
  113. return false;
  114. }
  115. return true;
  116. }
  117. private static int GetStatusCode(SystemWebClient client, out string statusDescription)
  118. {
  119. FieldInfo responseField = client.GetType().GetField("m_WebRequest", BindingFlags.Instance | BindingFlags.NonPublic);
  120. if (responseField != null)
  121. {
  122. HttpWebResponse response = responseField.GetValue(client) as HttpWebResponse;
  123. if (response != null)
  124. {
  125. statusDescription = response.StatusDescription;
  126. return (int)response.StatusCode;
  127. }
  128. }
  129. statusDescription = null;
  130. return 0;
  131. }
  132. public class FrameworkClass{
  133. public FrameworkClass(string name, string code,string title,LicenceTyp typ){
  134. Name = name;
  135. Code = code;
  136. Typ = typ;
  137. Title = title;
  138. }
  139. public string Name;
  140. public string Code;
  141. public string Title;
  142. public LicenceTyp Typ;
  143. }
  144. public enum LicenceTyp{
  145. BASIC = -200,
  146. PROFESIONAL = -300
  147. }
  148. }
  149. }