public static WriteInformation ( string message ) : void | ||
message | string | The message to write to the Debug.log file |
return | void |
public static bool DownloadImage(string targetDirectory, string sourceUrl) { if (targetDirectory == null) { throw new ArgumentNullException("targetDirectory"); } if (sourceUrl == null) { throw new ArgumentNullException("sourceUrl"); } try { using (var client = new WebClient()) { client.DownloadFile(sourceUrl, targetDirectory); } if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Image download completed"); } return(true); } catch (Exception ex) { ExceptionManager.WriteException(ex); return(false); } }
private void imagesMenuItem_Click(object sender, EventArgs e) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Images option selected."); } var processHelper = new ProcessHelper(); processHelper.BackgroundLoading(TestInternetConnection); processHelper.Start(); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Checking internet connectivity"); } //Present the message that an internet connection is required if (!_internetAvailable) { MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var imagesForm = new ImagesForm(); imagesForm.ShowDialog(); UpdateControlContent(); }
public static bool UpdateIsAvailable() { //Read in the XML file var configDocument = new XmlDocument(); try { configDocument.Load(ServerConfigFilePath); } catch (Exception ex) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("An error occurred while checking for the update."); } ExceptionManager.WriteException(ex); return(false); } //Extract configuration/applicationSettings/NasaPicOfDay.Properties.Settings/setting var settingNode = configDocument.SelectSingleNode( "/configuration/applicationSettings/NasaPicOfDay.Properties.Settings/setting[@name='CurrentVersion']/value"); if (settingNode == null) { return(false); } var versionStrings = settingNode.InnerText.Split('.'); Int32.TryParse(versionStrings[0], out _serverMajorVersion); Int32.TryParse(versionStrings[1], out _serverMinorVersion); Int32.TryParse(versionStrings[2], out _serverBuildNumber); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Application version on GIT {0}.{1}.{2}", _serverMajorVersion, _serverMinorVersion, _serverBuildNumber)); ExceptionManager.WriteInformation(string.Format("Application version on this machine {0}.{1}.{2}", GlobalVariables.CurrentMajorVersion, GlobalVariables.CurrentMinorVersion, GlobalVariables.CurrentBuildNumber)); } //Compare against global variables for version if (GlobalVariables.CurrentMajorVersion < _serverMajorVersion) { //Update is required return(true); } //Need to check other fields if (GlobalVariables.CurrentMinorVersion < _serverMinorVersion) { //Update is required return(true); } //Need to check the other fields return(GlobalVariables.CurrentBuildNumber < _serverBuildNumber); }
private void settingsMenuItem_Click(object sender, EventArgs e) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Settings option selected."); } var settingForm = new SettingsForm(); settingForm.ShowDialog(); }
public PicofDay() { InitializeComponent(); try { //Creating a timer to retrieve the latest image once a day. //We did it this way to avoid setting up a scheduled task on the user's machine. _appTimer = new Timer(); //Checking for updates at 12 p.m. EST (GMT-5) everyday if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Checking the system time for 12 p.m. EST update."); } //Setting the current UTC time DateTime utcNow = DateTime.UtcNow; if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Current UTC time is [Hours:{0}, Minutes:{1}, Seconds:{2}]", utcNow.Hour, utcNow.Minute, utcNow.Second)); } DateTime updateTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 17, 0, 0); //Get the amount of time between now and 12 p.m. EST TimeSpan timeUntilEst12Pm = utcNow - updateTime; if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Time until 12 p.m. EST [Hours:{0}, Minutes:{1}, Seconds:{2}]", Math.Abs(timeUntilEst12Pm.Hours), Math.Abs(timeUntilEst12Pm.Minutes), Math.Abs(timeUntilEst12Pm.Seconds))); } //set the interval for the timer _appTimer.Interval = Math.Abs((int)timeUntilEst12Pm.TotalMilliseconds); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Next update will occur in [Hours:{0}, Minutes:{1}, Seconds:{2}]", Math.Abs(timeUntilEst12Pm.Hours), Math.Abs(timeUntilEst12Pm.Minutes), Math.Abs(timeUntilEst12Pm.Seconds))); } _appTimer.Tick += appTimer_Tick; //Launch the main part of the data retrieval LoadApplicationContent(); //Start the application timer _appTimer.Start(); } catch (Exception ex) { ExceptionManager.WriteException(ex); } }
private void btnSaveSettings_Click(object sender, EventArgs e) { //Save Settings Settings.Default.LoggingEnabled = chkBoxLogging.Checked; GlobalVariables.LoggingEnabled = chkBoxLogging.Checked; Settings.Default.Save(); Settings.Default.Reload(); MessageBox.Show(Resources.SettingSaved, Resources.Saved, MessageBoxButtons.OK); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("##################################\tLogging is Enabled\t##################################"); } else { ExceptionManager.WriteInformation("##################################\tLogging is Disabled\t##################################"); } }
public static bool InternetAccessIsAvailable() { const string url = "http://www.nasa.gov"; try { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Building internet connectivity request."); } WebRequest myRequest = WebRequest.Create(url); //a 10 second timeout to limit the request attempt myRequest.Timeout = 10000; if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Starting request."); } using (WebResponse myResponse = myRequest.GetResponse()) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(myResponse.ToString()); } myResponse.Close(); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Request completed."); } return(true); } } catch (WebException wex) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Error occurred checking internet connection:\t{0}", wex.Message)); } ExceptionManager.WriteException(wex); return(false); } }
private void updateMenuItem_Click(object sender, EventArgs e) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Update has been requested."); } var processHelper = new ProcessHelper(); processHelper.BackgroundLoading(TestInternetConnection); processHelper.Start(); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Checking internet connectivity"); } if (!_internetAvailable) { MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } UpdateContent(); }
/// <summary> /// Set the desktop to the current picture of the day /// <param name="fileName">Full file path to the image to set as the desktop background.</param> /// </summary> public void SetDesktopBackground(string fileName) { try { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Preparing to change desktop background"); } SystemParametersInfo(SpiSetdeskwallpaper, 0, fileName, SpifUpdateinifile); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Completed desktop background change"); } } catch (Exception ex) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("An error occurred updating the desktop background."); } ExceptionManager.WriteException(ex); } }
static void Main() { //Checking to see if logging is enabled. GlobalVariables.LoggingEnabled = Settings.Default.LoggingEnabled; GlobalVariables.GetApplicationVersion(); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Application starting."); } //Checking to see if the application is running if (Mutex.WaitOne(TimeSpan.Zero, true)) { if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Checking internet connection."); } if (!NetworkHelper.InternetAccessIsAvailable()) { MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("No internet connection. Application exiting."); } Environment.Exit(Environment.ExitCode); } else { //Application is not currently running and there is an available internet connection //Check for updates if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Checking for application update."); } if (ApplicationUpdater.UpdateIsAvailable()) { //Kick off the updater var dlgResult = MessageBox.Show(Resources.UpdateQuestion, Resources.AnUpdateIsAvailable, MessageBoxButtons.YesNo); if (dlgResult == DialogResult.Yes) { //Need the 'runas' verb to allow admin privileges for update var startInfo = new ProcessStartInfo("Updater.exe") { Verb = "runas" }; Process.Start(startInfo); } else { Application.Run(new PicofDay()); } } else { Application.Run(new PicofDay()); } } } else { //If application is already running and there is no internet connection available, close the application if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Application is already running. But there is no internet connection. Exiting the application."); } if (!NetworkHelper.InternetAccessIsAvailable()) { MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); Application.ExitThread(); } } }
/// <summary> /// Overloaded GetImage that allows the retrieval of previous images based on their position in the XML document /// provided from the nasa.gov website. /// </summary> /// <param name="selectedOffset">Position of the image within the node list of images, 1 being the very first (newest)</param> /// <param name="selectedPage">Page number to retrieve the page that contains the selected image.</param> public BackgroundImage GetImage(int?selectedOffset, int?selectedPage) { try { if (!NetworkHelper.InternetAccessIsAvailable()) { throw new Exception("Attempted to retrieve image, but internet connection was not available."); } if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Retrieving system screen resolution"); } GetCurrentScreenResolution(); if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("System screen resolution is: {0}", _currentScreenResolution)); } if (selectedOffset == null) { selectedOffset = DefaultimageOffset; } //Get the JSON string data if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Preparing to download image information"); } var nasaImages = JsonHelper.DownloadSerializedJsonData(string.Format(NasaLatestImagesUrl, selectedPage)); if (nasaImages == null || nasaImages.UberNodes.Length == 0) { throw new Exception("Unable to retrieve image data from JSON request"); } //Get the image node var imageId = nasaImages.UberNodes[(int)selectedOffset].UberNodeId; var currentImageFullUrl = string.Format("{0}{1}.json", NasaApiUrl, imageId); var currentImage = JsonHelper.DownloadImageData(currentImageFullUrl); if (currentImage == null) { throw new Exception(string.Format("Unable to retrieve selected image: {0}", currentImageFullUrl)); } if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation("Selecting image based on current screen resolution"); } switch (_currentScreenResolution) { case "100x75": case "226x170": currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].LrThumbnail); break; case "346x260": case "360x225": case "466x248": case "430x323": currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop1X1); break; case "800x600": currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop2X1); break; default: currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].FullWidthFeature); break; } var localImageFolderPath = string.Format("{0}\\NASA\\PicOfTheDay", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)); if (!Directory.Exists(localImageFolderPath)) { Directory.CreateDirectory(localImageFolderPath); } var imageName = currentImage.ImageData[0].FileName; //Setting the full local image path to save the file var fullImagePath = string.Format("{0}\\{1}", localImageFolderPath, imageName); //Download the current image if (GlobalVariables.LoggingEnabled) { ExceptionManager.WriteInformation(string.Format("Preparing to download image: {0}", currentImageFullUrl)); } if (!DownloadHelper.DownloadImage(fullImagePath, currentImageFullUrl)) { throw new Exception("Error downloading current image."); } //Create BackgroundImage object var backgroundImage = new BackgroundImage { ImageUrl = currentImageFullUrl, ImageDate = Convert.ToDateTime(currentImage.UberData.PromoDateTime), ImageDescription = StripHtml(currentImage.UberData.Body), DownloadedPath = fullImagePath, ImageTitle = !string.IsNullOrEmpty(currentImage.ImageData[0].Title) ? currentImage.ImageData[0].Title : currentImage.UberData.Title }; return(backgroundImage); } catch (Exception ex) { ExceptionManager.WriteException(ex); return(null); } }