示例#1
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
        }     // end of method Init

        private static void HealthCheck()
        {
            // the program CANNOT RUN with the assets directory
            if (!Directory.Exists(ASSETS_PATH))
            {
                UtilityMethod.MissingRequirementsPrompt("Missing Assets Directory");
            }// end of if block
            else
            {
                UtilityMethod.subDirectoriesFound.Clear(); // clear any previous list

                List <string> iconPacks = UtilityMethod.GetSubdirectories(WEATHER_ICONS_PATH);

                if (iconPacks == null || iconPacks.Count == 0)
                {
                    UtilityMethod.MissingRequirementsPrompt("Empty Assets Directory");
                }// end of if block
                else
                {
                    UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO,
                                             "Found " + iconPacks.Count + " icon " +
                                             (iconPacks.Count > 1 ? "packs..." : "pack..."),
                                             $"{TAG}::HealthCheck");

                    if (!iconPacks.Contains(DEFAULT_ICON_SET))
                    {
                        UtilityMethod.MissingRequirementsPrompt("Missing Default Icons");
                    }// end of if block
                    else if (!iconPacks.Contains(Preference.GetSavedPreferences().StoredPreferences.IconSet))
                    {
                        UtilityMethod.LogMessage(UtilityMethod.LogLevel.WARNING,
                                                 $"The {storedPreferences.StoredPreferences.IconSet.ToUpper()}" +
                                                 $" icon pack could not be found so the default {DEFAULT_ICON_SET.ToUpper()}" +
                                                 " will be used!", $"{TAG}::HealthCheck");

                        Preference.SaveProgramConfiguration("prefs", "IconSet", "default");
                    }// end of else if block
                    else
                    {
                        string iconsInUse =
                            $"{WEATHER_ICONS_PATH}{storedPreferences.StoredPreferences.IconSet}/";
                        int imageCount = UtilityMethod.GetFileCount(iconsInUse);

                        if (imageCount < 23)
                        {
                            UtilityMethod.MissingRequirementsPrompt("Insufficient Icon Count");
                        }// end of if block
                        else
                        {
                            UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO, $"Found {imageCount}" +
                                                     (imageCount > 1 ? " images" : " image") + " in the " +
                                                     UtilityMethod.ToProperCase(storedPreferences.StoredPreferences.IconSet) +
                                                     " icon pack...", $"{TAG}::HealthCheck");
                        }// end of else block

                        // check for the background and icon  images

                        if (!Directory.Exists(WIDGET_BACKGROUNDS_PATH))
                        {
                            UtilityMethod.MissingRequirementsPrompt("Missing Background Image Directory");
                        }// end of if block
                        else
                        {
                            imageCount = UtilityMethod.GetFileCount(WIDGET_BACKGROUNDS_PATH);

                            if (imageCount < 3)
                            {
                                UtilityMethod.MissingRequirementsPrompt(imageCount > 1 ? "Missing Background Images" :
                                                                        "Missing Background Image");
                            }// end of if block
                            else
                            {
                                UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO,
                                                         "Found " + imageCount + (imageCount > 1 ? " images" : " image")
                                                         + " in the backgrounds directory...", $"{TAG}::HealthCheck");
                            } // end of else block
                        }     // end of else block

                        if (!Directory.Exists(WIDGET_ICONS_PATH))
                        {
                            UtilityMethod.MissingRequirementsPrompt("Missing Background Image Directory");
                        }// end of if block
                        else
                        {
                            imageCount = UtilityMethod.GetFileCount(WIDGET_ICONS_PATH);

                            if (imageCount < 11)
                            {
                                UtilityMethod.MissingRequirementsPrompt(imageCount > 1 ? "Missing Icon Images" :
                                                                        "Missing Icon Image");
                            }// end of if block
                            else
                            {
                                UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO,
                                                         "Found " + imageCount +
                                                         (imageCount > 1 ? " images" : " image") +
                                                         " in the icons directory...",
                                                         $"{TAG}::HealthCheck");
                            } // end of else block
                        }     // end of else block
                    }         // end of else block
                }             // end of else block
            }                 // end of else block
        }                     // end of method HealthCheck