예제 #1
0
        }// end of method init

        /// <summary>
        /// Checks to see if any provider stored in the database is missing a key
        /// that is required.
        /// </summary>
        /// <returns></returns>
        public static DialogResult CheckForMissingKeys()
        {
            string mks  = string.Join(", ", keysMissing);
            string fMks = null;

            if (UtilityMethod.NumberOfCharacterOccurences(',', mks) > 1)
            {
                fMks = UtilityMethod.ReplaceLast(",", ", and", mks);
            }// end of if block
            else if (UtilityMethod.NumberOfCharacterOccurences(',', mks) == 1)
            {
                fMks = mks.Replace(",", " and");
            }// end of else block
            else
            {
                fMks = mks;
            }// end of else block

            string prompt = "Yahoo! Weather requires the following missing " +
                            (keysMissing.Count > 1 ? "keys" : "key") + ":\n"
                            + fMks + "\nDo you wish to add " +
                            (keysMissing.Count > 1 ? "them" : "it") + " now?";
            DialogResult result = UtilityMethod.ResponseBox(prompt, WeatherLionMain.PROGRAM_NAME + " - Add Missing Key");

            return(result);
        }// end of message CheckForMissingKeys
예제 #2
0
        }     // end of method GetSiteKeyFromDatabase

        /**
         * Load all access providers stored in the database
         */
        public static void LoadAccessProviders()
        {
            ArrayList appKeys = new ArrayList();

            webAccessGranted = new List <string>();

            try
            {
                foreach (string provider in WeatherLionMain.providerNames)
                {
                    appKeys = GetSiteKeyFromDatabase(provider);

                    if (appKeys != null)
                    {
                        switch (provider)
                        {
                        case "Dark Sky Weather":

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)darkSkyRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    WidgetUpdateService.darkSkyApiKey = Decrypt(kv[1], kv[2], true);
                                } // end of if block
                            }     // end of for each loop

                            if (WidgetUpdateService.darkSkyApiKey != null)
                            {
                                webAccessGranted.Add("Dark Sky Weather");
                            }    // end of if block

                            break;

                        case "GeoNames":

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)geoNamesRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    WidgetUpdateService.geoNameAccount = Decrypt(kv[1], kv[2], true);
                                } // end of if block
                            }     // end of for each loop

                            if (WidgetUpdateService.geoNameAccount != null)
                            {
                                webAccessGranted.Add("GeoNames");
                                geoNamesAccountLoaded = true;
                            }    // end of if block

                            break;

                        case "Open Weather Map":

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)openWeatherMapRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    WidgetUpdateService.openWeatherMapApiKey = Decrypt(kv[1], kv[2], true);
                                } // end of if block
                            }     // end of for each loop

                            if (WidgetUpdateService.openWeatherMapApiKey != null)
                            {
                                webAccessGranted.Add("Open Weather Map");
                            }    // end of if block

                            break;

                        case "Weather Bit":

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)weatherBitRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    WidgetUpdateService.weatherBitApiKey = Decrypt(kv[1], kv[2], true);
                                } // end of if block
                            }     // end of for each loop

                            if (WidgetUpdateService.weatherBitApiKey != null)
                            {
                                webAccessGranted.Add("Weather Bit");
                            }    // end of if block

                            break;

                        case "Here Maps Weather":

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)hereMapsRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    switch (kv[0].ToLower())
                                    {
                                    case "app_id":
                                        WidgetUpdateService.hereAppId = Decrypt(kv[1], kv[2], true);
                                        break;

                                    case "app_code":
                                        WidgetUpdateService.hereAppCode = Decrypt(kv[1], kv[2], true);
                                        break;

                                    default:
                                        break;
                                    } // end of switch block
                                }     // end of if block
                            }         // end of for each loop

                            if (WidgetUpdateService.hereAppId != null && WidgetUpdateService.hereAppCode != null)
                            {
                                webAccessGranted.Add("Here Maps Weather");
                            }    // end of if block
                            else if (WidgetUpdateService.hereAppId != null && WidgetUpdateService.hereAppCode == null)
                            {
                                UtilityMethod.ShowMessage("Here Maps Weather requires an app_code which is"
                                                          + " not stored in the database.", title: WeatherLionMain.PROGRAM_NAME + " - Missing Key",
                                                          mbIcon: MessageBoxIcon.Error);
                            }    // end of if block
                            else if (WidgetUpdateService.hereAppId == null && WidgetUpdateService.hereAppCode != null)
                            {
                                UtilityMethod.ShowMessage("Here Maps Weather requires an app_id which is"
                                                          + " not stored in the database.", title: WeatherLionMain.PROGRAM_NAME + " - Missing Key",
                                                          mbIcon: MessageBoxIcon.Error);
                            }    // end of if block
                            break;

                        case "Yahoo! Weather":
                            List <string> keysFound = new List <string>();

                            foreach (string key in appKeys)
                            {
                                string[] kv = key.Split(':');

                                if (((IList <string>)yahooRequiredKeys).Contains(kv[0].ToLower()))
                                {
                                    switch (kv[0].ToLower())
                                    {
                                    case "app_id":
                                        WidgetUpdateService.yahooAppId = Decrypt(kv[1], kv[2], true);
                                        keysFound.Add("app_id");
                                        break;

                                    case "consumer_key":
                                        WidgetUpdateService.yahooConsumerKey = Decrypt(kv[1], kv[2], true);
                                        keysFound.Add("consumer_key");
                                        break;

                                    case "consumer_secret":
                                        WidgetUpdateService.yahooConsumerSecret = Decrypt(kv[1], kv[2], true);
                                        keysFound.Add("consumer_secret");
                                        break;

                                    default:
                                        break;
                                    } // end of switch block
                                }     // end of if block
                            }         // end of for each loop

                            // remove all the keys that were found from the list
                            keysMissing = new List <string>(((IList <string>)yahooRequiredKeys).Except(keysFound).ToList());

                            if (keysMissing.Count == 0)
                            {
                                webAccessGranted.Add("Yahoo! Weather");
                            }    // end of if block
                            else
                            {
                                // do not check for missing keys if the form is already displayed
                                if (!addKeys.Visible)
                                {
                                    if (CheckForMissingKeys() == DialogResult.Yes)
                                    {
                                        AccessKeysForm.frmKeys.cboAccessProvider.SelectedItem = "Yahoo! Weather";
                                        AccessKeysForm.frmKeys.Visible = true;
                                        AccessKeysForm.frmKeys.Focus();
                                    }    // end of if block
                                    else
                                    {
                                        string msg = "Yahoo!Weather cannot be used as a weather source without "
                                                     + "first adding the missing " + (keysMissing.Count > 1 ? "keys" : "key") + ".";

                                        UtilityMethod.ShowMessage(msg, AccessKeysForm.frmKeys, title: $"{WeatherLionMain.PROGRAM_NAME} - Missing Key", buttons: MessageBoxButtons.OK,
                                                                  mbIcon: MessageBoxIcon.Information);
                                    } // end of else block
                                }     // end of if block
                                else
                                {
                                    AccessKeysForm.frmKeys.txtKeyName.Focus();
                                } // end of else block
                            }     // end of else block

                            break;

                        default:
                            break;
                        } // end of switch block
                    }     // end of if block
                }         // end of outer for each loop
            }             // end of try block
            catch (Exception e)
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, e.Message,
                                         $"{TAG}::LoadAccessProviders [line: {UtilityMethod.GetExceptionLineNumber(e)}]");
            }// end of catch block

            // add the only weather provider that does not require a key
            webAccessGranted.Add(WeatherLionMain.YR_WEATHER);

            if (webAccessGranted.Count > 0)
            {
                string s  = string.Join(", ", webAccessGranted);
                string fs = null;

                if (UtilityMethod.NumberOfCharacterOccurences(',', s) > 1)
                {
                    fs = UtilityMethod.ReplaceLast(",", ", and", s);
                }// end of if block
                else if (UtilityMethod.NumberOfCharacterOccurences(',', s) == 1)
                {
                    fs = s.Replace(",", " and");
                }// end of else block
                else
                {
                    fs = s;
                }// end of else block

                UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO, $"The following access providers were loaded:\n{fs}.",
                                         $"{TAG}::LoadAccessProviders");
            }// end of if block
            else
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, "No valid access privelages were stored in the database!",
                                         $"{TAG}::LoadAccessProviders");
            }// end of else block

            if (webAccessGranted.Count == 0)
            {
                if (NoAccessPrivialgesStored() == DialogResult.Yes)
                {
                    if (!AccessKeysForm.frmKeys.Visible)
                    {
                        AccessKeysForm.frmKeys.ShowDialog();
                        AccessKeysForm.frmKeys.Focus();
                    }// end of if block
                    else
                    {
                        AccessKeysForm.frmKeys.txtKeyName.Focus();
                    }// end of else block
                }
                else
                {
                    UtilityMethod.MissingRequirementsPrompt("Insufficient Access Privilages");
                } // end of if block
            }     // end of if block

            if (webAccessGranted.Count >= 1 && !geoNamesAccountLoaded)
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, "GeoNames user name not found!",
                                         $"{TAG}::LoadAccessProviders");

                // confirm that user has a GeoNames account and want's to store it
                string prompt = "This program requires a geonames username\n" +
                                "which was not stored in the database. IT IS FREE!" +
                                "\nDo you wish to add it now?";

                DialogResult result = MessageBox.Show(prompt, $"{WeatherLionMain.PROGRAM_NAME} - Add Access Privialges",
                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Yes)
                {
                    //AccessKeysForm kf = AccessKeysForm.frmKeys;

                    if (!AccessKeysForm.frmKeys.Visible)
                    {
                        AccessKeysForm.frmKeys.m_key_provider = "GeoNames";
                        AccessKeysForm.frmKeys.ShowDialog();
                    }// end of if block
                    else
                    {
                        AccessKeysForm.frmKeys.cboAccessProvider.SelectedItem = "GeoNames";
                        AccessKeysForm.frmKeys.pwdKeyValue.Focus();
                    } // end of else block
                }     // end of if block
                else
                {
                    UtilityMethod.MissingRequirementsPrompt("Insufficient Access Privilages");
                } // end of else block
            }     // end of else if block

            // if valid access was not loaded for the provider previously used, take it into account
            if (WeatherLionMain.storedPreferences != null)
            {
                if (!webAccessGranted.Contains(WeatherLionMain.storedPreferences.StoredPreferences.Provider))
                {
                    WeatherLionMain.noAccessToStoredProvider = true;
                }// end of if block
                else
                {
                    WeatherLionMain.noAccessToStoredProvider = false;
                } // end of else block
            }     // end of if block
        }         // end of method LoadAccessProcviders
예제 #3
0
        }         // end of method btnDeleteKey_Click

        private void btnFinish_Click(object sender, EventArgs e)
        {
            LionSecurityManager.LoadAccessProviders();

            if (LionSecurityManager.keysMissing != null)
            {
                if (LionSecurityManager.keysMissing.Count > 0)
                {
                    DialogResult answer = LionSecurityManager.CheckForMissingKeys();
                    LionSecurityManager.accessGranted = false;

                    if (answer == DialogResult.Yes)
                    {
                        if (!Visible)
                        {
                            Visible = true;
                        }// end of if block
                        else
                        {
                            txtKeyName.Focus();
                        } // end of else block
                    }     // end of if block
                    else
                    {
                        string wp = null;
                        //string[] keys = null;

                        if (LionSecurityManager.keysMissing.Count > 1)
                        {
                            List <string> missingHereKeys  = new List <string>();
                            List <string> missingYahooKeys = new List <string>();

                            foreach (string keyName in LionSecurityManager.keysMissing)
                            {
                                if (LionSecurityManager.hereMapsRequiredKeys.Contains(keyName))
                                {
                                    missingHereKeys.Add(keyName);
                                }// end of if block
                                else if (LionSecurityManager.hereMapsRequiredKeys.Contains(keyName))
                                {
                                    missingYahooKeys.Add(keyName);
                                } // end of else if block
                            }     // end of for each loop

                            if (missingHereKeys.Count > 0)
                            {
                                string hKeys = string.Join(", ", missingHereKeys);
                                string fs    = null;

                                if (UtilityMethod.NumberOfCharacterOccurences(',', hKeys) > 1)
                                {
                                    fs = UtilityMethod.ReplaceLast(",", ", and", hKeys);
                                }// end of if block
                                else if (UtilityMethod.NumberOfCharacterOccurences(',', hKeys) == 1)
                                {
                                    fs = hKeys.Replace(",", " and");
                                }// end of else block
                                else
                                {
                                    fs = hKeys;
                                }// end of else block

                                wp = WeatherLionMain.HERE_MAPS;
                                string kc = missingHereKeys.Count > 1 ? "keys" : "key";

                                UtilityMethod.ShowMessage($"{wp} cannot be used as a weather source without\n"
                                                          + $"first adding the missing {kc} {fs}.", this, $"{WeatherLionMain.PROGRAM_NAME} - Missing Key", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }// end of if block

                            if (missingYahooKeys.Count > 0)
                            {
                                string hKeys = string.Join(", ", missingHereKeys);
                                string fs    = null;

                                if (UtilityMethod.NumberOfCharacterOccurences(',', hKeys) > 1)
                                {
                                    fs = UtilityMethod.ReplaceLast(",", ", and", hKeys);
                                }// end of if block
                                else if (UtilityMethod.NumberOfCharacterOccurences(',', hKeys) == 1)
                                {
                                    fs = hKeys.Replace(",", " and");
                                }// end of else block
                                else
                                {
                                    fs = hKeys;
                                }// end of else block

                                wp = WeatherLionMain.YAHOO_WEATHER;
                                string kc = missingHereKeys.Count > 1 ? "keys" : "key";

                                UtilityMethod.ShowMessage($"{wp} cannot be used as a weather source without\n"
                                                          + $"first adding the missing {kc} {fs}.", this,
                                                          title: $"{WeatherLionMain.PROGRAM_NAME} - Missing Key", buttons: MessageBoxButtons.OK,
                                                          mbIcon: MessageBoxIcon.Error);
                            } // end of if block
                        }     // end of if block
                    }         // end of else block
                }             // end of if block
            }                 // end of if block

            if (LionSecurityManager.webAccessGranted.Count >= 1 &&
                !LionSecurityManager.webAccessGranted.Contains("GeoNames"))
            {
                UtilityMethod.ShowMessage("This program requires a geonames username"
                                          + " which was not stored in the database.\nIT IS FREE!", this,
                                          title: $"{WeatherLionMain.PROGRAM_NAME} - Missing Key", buttons: MessageBoxButtons.OK,
                                          mbIcon: MessageBoxIcon.Error);
            }// end of else if block
            else if (LionSecurityManager.webAccessGranted.Count == 2 &&
                     LionSecurityManager.webAccessGranted.Contains("GeoNames") &&
                     LionSecurityManager.webAccessGranted.Contains("Yr.no (Norwegian Metrological Institute)"))
            {
                UtilityMethod.ShowMessage("The program will only display weather data from"
                                          + " Yr.no (Norwegian Metrological Institute).\nObtain access keys for"
                                          + " the others if you wish to use them.", this,
                                          title: $"{WeatherLionMain.PROGRAM_NAME} - Single Weather Providery", buttons: MessageBoxButtons.OK,
                                          mbIcon: MessageBoxIcon.Information);
                LionSecurityManager.accessGranted = true;
                Close();
            }// end of else if block
            else
            {
                LionSecurityManager.accessGranted = true;
                Close();
            } // end of else block
        }     // end of method btnFinish_Click