}// end of method btnWindReading_TextChanged #endregion #region Form Events private void frmWidget_FormClosing(object sender, FormClosingEventArgs e) { // Save configuration WeatherLionMain.storedPreferences.ConfigData.StartPosition = Location; WeatherLionMain.storedPreferences.StoredPreferences.Interval = tmrUpdate.Interval; Preference.SaveProgramConfiguration("config", "StartPosition", Location); }// end of method frmWidget_FormClosing
}// end of method tmrIconUpdater_Tick private void tmrPreferenceUpdate_Tick(object sender, EventArgs e) { if (applyPreferenceUpdates) { if (preferenceUpdated.Count > 0) { foreach (string preference in preferenceUpdated.Keys) { switch (preference) { case WeatherLionMain.CURRENT_LOCATION_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference]); // update running data WeatherLionMain.storedPreferences.StoredPreferences.Location = preferenceUpdated[preference]; // reset the re-attempted flag before running the service reAttempted = false; // run the weather service ws = new WidgetUpdateService(false, this); ws.Run(); break; case WeatherLionMain.WEATHER_SOURCE_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference]); // update running data WeatherLionMain.storedPreferences.StoredPreferences.Provider = preferenceUpdated[preference]; // reset the re-attempted flag before running the service reAttempted = false; // run the weather service ws = new WidgetUpdateService(false, this); ws.Run(); break; case WeatherLionMain.UPDATE_INTERVAL: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference]); // update running data WeatherLionMain.storedPreferences.StoredPreferences.Interval = int.Parse(preferenceUpdated[preference]); break; case WeatherLionMain.USE_SYSTEM_LOCATION_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference].ToLower()); // update running data WeatherLionMain.storedPreferences.StoredPreferences.UseSystemLocation = bool.Parse(preferenceUpdated[preference].ToLower()); // reset the re-attempted flag before running the service reAttempted = false; // run the weather service ws = new WidgetUpdateService(false, this); ws.Run(); break; case WeatherLionMain.USE_METRIC_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference].ToLower()); // update running data WeatherLionMain.storedPreferences.StoredPreferences.UseMetric = bool.Parse(preferenceUpdated[preference].ToLower()); // update the units displayed on the widget ws = new WidgetUpdateService(true, this); ws.Run(); break; case WeatherLionMain.ICON_SET_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference]); // update running data WeatherLionMain.storedPreferences.StoredPreferences.IconSet = preferenceUpdated[preference]; WeatherLionMain.iconSet = WeatherLionMain.storedPreferences.StoredPreferences.IconSet; UpdateIconSet(); break; case WeatherLionMain.WIDGET_BACKGROUND_PREFERENCE: // update the local preference file Preference.SaveProgramConfiguration("prefs", preference, preferenceUpdated[preference]); // update running data WeatherLionMain.storedPreferences.StoredPreferences.WidgetBackground = preferenceUpdated[preference]; switch (WeatherLionMain.storedPreferences.StoredPreferences.WidgetBackground) { case "default": default: BlendedBackground = (Bitmap)Image.FromFile(WeatherLionMain.DEFAULT_BACKGROUND_IMAGE); Refresh(); break; case "android": BlendedBackground = (Bitmap)Image.FromFile(WeatherLionMain.ANDROID_BACKGROUND_IMAGE); Refresh(); break; case "rabalac": BlendedBackground = (Bitmap)Image.FromFile(WeatherLionMain.RABALAC_BACKGROUND_IMAGE); Refresh(); break; } // end of switch block BackgroundUpdate(); break; default: break; } // end of switch block } // end of for each loop // remove all items from the object preferenceUpdated.Clear(); // refresh the UI immediately Refresh(); // reset flag to perfom changes applyPreferenceUpdates = false; } // end of if block } // end of if block } // end of method tmrPreferenceUpdate_Tick
} // 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
} // end of method HealthCheck public static bool LocationCheck() { bool locationSet = false; if (storedPreferences.StoredPreferences.Location.Equals("not set")) { DialogResult setCurrentCity; if (systemLocation != null) { string prompt = "You must specify a current location in order to run the program. " + $"Your current location is detected as {systemLocation}.\n" + "Would you like to use it as your current location?"; DialogResult useSystemLocation = UtilityMethod.ResponseBox(prompt, PROGRAM_NAME + " - Setup"); if (useSystemLocation == DialogResult.Yes) { storedPreferences.StoredPreferences.UseSystemLocation = true; storedPreferences.StoredPreferences.Location = systemLocation; Preference.SaveProgramConfiguration("prefs", "UseSystemLocation", "true"); Preference.SaveProgramConfiguration("prefs", "Location", systemLocation); // save the city to the local WorldCites database UtilityMethod.AddCityToDatabase( currentCity.cityName, currentCity.countryName, currentCity.countryCode, currentCity.regionName, currentCity.regionCode, currentCity.latitude, currentCity.longitude); JSONHelper.ExportToJSON(currentCity); XMLHelper.ExportToXML(currentCity); locationSet = true; Application.Run(new WidgetForm()); }// end of if block else { prompt = "You must specify a current location in order to run the program.\n" + "Would you like to specify it now?"; setCurrentCity = UtilityMethod.ResponseBox(prompt, PROGRAM_NAME + " - Setup"); if (setCurrentCity == DialogResult.Yes) { preferences.ShowDialog(); // loop until a city is selected while (PreferencesForm.locationSelected) { UtilityMethod.LogMessage(UtilityMethod.LogLevel.WARNING, "Waiting for location to be set!", $"{TAG}::LocationCheck"); }// end of while loop locationSet = PreferencesForm.locationSelected; } // end of if block } // end of else block } // end of if block else { setCurrentCity = UtilityMethod.ResponseBox("You must specify a current location in order to run the program.\n" + "Would you like to specify it now?", WeatherLionMain.PROGRAM_NAME + " Setup"); if (setCurrentCity == DialogResult.Yes) { preferences.Show(); // loop until a city is selected while (!PreferencesForm.locationSelected) { Console.WriteLine("Waiting for location to be set!"); }// end of while loop locationSet = PreferencesForm.locationSelected; }// end of if block else { UtilityMethod.ShowMessage("The program will not run without a location set.\nGoodbye.", null, title: $"{WeatherLionMain.PROGRAM_NAME}", buttons: MessageBoxButtons.OK, mbIcon: MessageBoxIcon.Information); Application.Exit(); //Exit the application. }// end of else block }// end of else block }// end of if block else { // the location was already set locationSet = true; }// end of else block return(locationSet); } // end of method LocationCheck
/// <summary> /// Load a required assets and prepare for program execution /// </summary> public static void Launch() { #region WeatherLion launch sequence UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO, "Initiating startup...", "WeatherLionMain::Launch"); // build the required storage files if (BuildRequiredDatabases() == 1) { UtilityMethod.LogMessage(UtilityMethod.LogLevel.INFO, "All required databases constructed successfully.", "WeatherLionMain::Launch"); }// end of if block else { UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, "All required databases were not constructed successfully.", "WeatherLionMain::Launch"); }// end of else block // check that the required access keys are available LionSecurityManager.Init(); // Load only the providers who have access keys assigned to them List <string> wxOnly = LionSecurityManager.webAccessGranted; wxOnly.Sort(); // sort the list // GeoNames is not a weather provider so it cannot be select here wxOnly.Remove("GeoNames"); authorizedProviders = wxOnly.ToArray(); // ensure that program has all the default assets needed for functioning properly HealthCheck(); // load user preferences storedPreferences = Preference.GetSavedPreferences(); string previousWeatherData = $"{DATA_DIRECTORY_PATH}{WEATHER_DATA_XML}"; connectedToInternet = UtilityMethod.HasInternetConnection(); // check for an Internet connection or previous weather data stored local if (!connectedToInternet && !File.Exists(previousWeatherData)) { UtilityMethod.ShowMessage("The program will not run without a working internet connection or " + "data that was previously stored locally" + "\nResolve your Internet connection and relaunch the program.", null); Application.Exit(); // terminate the program }// end of if block else if (connectedToInternet) { // obtain the current city of the connected Internet service currentCity = UtilityMethod.GetSystemLocation(); if (currentCity != null) { if (currentCity.regionCode != null) { systemLocation = $"{currentCity.cityName}, {currentCity.regionCode}"; }// end of if block else { systemLocation = $"{currentCity.cityName}, {currentCity.countryName}"; } // end of else block } // end of if block // if the user requires the current detected city location to be used as default if (storedPreferences.StoredPreferences.UseSystemLocation) { if (systemLocation != null) { // use the detected city location as the default storedPreferences.StoredPreferences.Location = systemLocation; if (!storedPreferences.StoredPreferences.Location.Equals(systemLocation)) { // update the preferences file Preference.SaveProgramConfiguration("prefs", "Location", systemLocation); // save the city to the local WorldCites database UtilityMethod.AddCityToDatabase( currentCity.cityName, currentCity.countryName, currentCity.countryCode, currentCity.regionName, currentCity.regionCode, currentCity.latitude, currentCity.longitude); JSONHelper.ExportToJSON(currentCity); XMLHelper.ExportToXML(currentCity); } // end of if block } // end of if block else { UtilityMethod.ShowMessage("The program was unable to obtain your system's location." + "\nYour location will have to be set manually using the preferences dialog.", null); PreferencesForm pf = new PreferencesForm(); pf.Show(); } // end of else block } // end of if block } // end of else if block Init(); #endregion }// end of method Launch