Example #1
0
        private void ParseActivationCode()
        {
            if (!string.IsNullOrEmpty(this.txtActivationCode.Text))
            {
                try
                {
                    this.DataSource = DAL.Licenses.OnlineCatalogLicenses.FromLicenseStream(this.txtActivationCode.Text);
                    this.DataSource.ToLicenseFile();
                    MessageBox.Show(this.DataSource[0].Activated ? "License activated" : "Activation failed", "License activation", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.lblCurrentStatus.Text = "Activated";
                    this.lblExpiresAfter.Text  = this.DataSource[0].ExpirationDate.ToShortDateString();
                }
                catch
                {
                }
            }
        }
 private void btnImport_Click(object sender, EventArgs e)
 {
     using (var popup = new OpenFileDialog())
     {
         popup.Filter      = "(*.lic)|*.lic|(*.LIC)|*.LIC";
         popup.Multiselect = false;
         Stream fileStream = null;
         if (popup.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(popup.FileName))
         {
             if (File.Exists(popup.FileName))
             {
                 try
                 {
                     if ((fileStream = popup.OpenFile()) != null)
                     {
                         using (fileStream)
                         {
                             StreamReader reader       = new StreamReader(fileStream);
                             string       fileContents = reader.ReadToEnd();
                             this.DataSource = DAL.Licenses.OnlineCatalogLicenses.FromLicenseStream(fileContents);
                             this.DataSource.ToLicenseFile(DAL.ApplicationSettings.Settings.LocalLicenseFilePath);
                             if (this.DataSource[0].Activated)
                             {
                                 if (SplashForm != null)
                                 {
                                     var spcSplashContainer = (SplitContainer)SplashForm.Controls["spcSplashContainer"];
                                     spcSplashContainer.Panel2.Controls.Clear();
                                     var licensedProgramControl = new LicensedProgramControl();
                                     licensedProgramControl.OpenNewProject    += LicensedProgramControl_OpenNewProject;
                                     licensedProgramControl.OpenExistsProject += LicensedProgramControl_OpenExistsProject;
                                     licensedProgramControl.ControlClosed     += pbClose_Click;
                                     spcSplashContainer.Panel2.Controls.Add(licensedProgramControl);
                                 }
                             }
                         }
                     }
                 }
                 catch (Exception)
                 {
                 }
             }
         }
     }
 }
Example #3
0
        private static void _tmrOnlineCatalogManager_Tick(object sender, EventArgs e)
        {
            _tmrOnlineCatalogManager.Stop();

            var deserialisedLicenses = new DAL.Licenses.OnlineCatalogLicenses();
            var licenseServerURL     = string.Format(Properties.Resources.LICENSE_CATALOG_URL, CustomerID);

            using (var webclient = new WebClient())
            {
                try
                {
                    var jsonData = webclient.DownloadString(licenseServerURL);

                    if (!string.IsNullOrEmpty(jsonData))
                    {
                        //convert to xml (change the dictionary entries 1-2-3 to serializerdata
                        var jsonAsXml = XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonData), new XmlDictionaryReaderQuotas()));
                        if (jsonAsXml != null)
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.LoadXml(jsonAsXml.ToString());
                            foreach (XmlNode rootNode in xmlDoc.SelectNodes("/root"))
                            {
                                //skip numeric indexes
                                foreach (XmlNode numericNode in rootNode.ChildNodes)
                                {
                                    //convert sibbling nodes to
                                    foreach (XmlNode licenseNode in numericNode.ChildNodes)
                                    {
                                        var deserializedLicense = new DAL.Licenses.OnlineCatalogLicense();
                                        foreach (XmlNode licenseDataNode in licenseNode.ChildNodes)
                                        {
                                            switch (licenseDataNode.Name)
                                            {
                                            case "code":
                                                deserializedLicense.Description = licenseDataNode.InnerText;
                                                break;

                                            case "activation_date":
                                                foreach (XmlNode activationDateNode in licenseDataNode)
                                                {
                                                    if (activationDateNode.Name == "date")
                                                    {
                                                        deserializedLicense.ActivationDate = DateTime.ParseExact(activationDateNode.InnerText, "yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture);
                                                    }
                                                }
                                                break;

                                            case "expiration_date":
                                                foreach (XmlNode activationDateNode in licenseDataNode)
                                                {
                                                    if (activationDateNode.Name == "date")
                                                    {
                                                        deserializedLicense.ExpirationDate = DateTime.ParseExact(activationDateNode.InnerText, "yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture);
                                                    }
                                                }
                                                break;

                                            case "active":
                                                if (licenseDataNode.InnerText == "true")
                                                {
                                                    deserializedLicense.Activated = true;
                                                }
                                                break;

                                            case "amount":
                                                deserializedLicense.Amount = int.Parse(licenseDataNode.InnerText);
                                                break;

                                            case "type":
                                                deserializedLicense.LicenseTypeAsGuid = licenseDataNode.InnerText;
                                                break;
                                            }
                                        }

                                        if (deserializedLicense.Activated && deserializedLicense.Amount > 0 && deserializedLicense.ExpirationDate >= DateTime.Now)
                                        {
                                            deserialisedLicenses.Add(deserializedLicense);
                                        }
                                    }
                                }
                            }
                        }

                        OnCatalogLicensesFetched?.Invoke(deserialisedLicenses);
                    }
                    else
                    {
                        EventLogManager.WriteToEventLog("Failed to fetch current license(s)", "Invalid license format");
                    }
                }
                catch (Exception exc)
                {
                    EventLogManager.WriteToEventLog("Failed to fetch current license(s)", exc.Message);
                }
            }

            _tmrOnlineCatalogManager.Start();
        }