Ejemplo n.º 1
0
        /// <summary>
        ///     Read a website into a string
        /// </summary>
        /// <param name="URL">URL to change into a string</param>
        /// <returns></returns>
        public static string ReadExternalWebsite(string URL)
        {
            String       website = "";
            UTF8Encoding utf8    = new UTF8Encoding();

            WebClient webClient = new WebClient();

            if (NetworkUtils.CheckInternetConnection())
            {
                try
                {
                    byte[] bytes = webClient.DownloadData(URL);
                    website =
                        utf8.GetString(webClient.ResponseHeaders["Content-Encoding"] == "gzip"
                                           ? UnGzip(bytes, 0)
                                           : bytes);
                    NetworkUtils.InternetSuccess();
                }
                catch (Exception)
                {
                    NetworkUtils.InternetFailure();
                }
            }
            return(website);
        }
 private void _initConfig()
 {
     logger.Debug("initializing the config manager");
     try
     {
         // init the config man
         _cmConfigManager = new ConfigurationManager();
         // load the local config
         _cmConfigManager.LoadLocal();
         // check if a db should be used, or not
         if (_cmConfigManager.Config.UseDataBase && !string.IsNullOrWhiteSpace(_cmConfigManager.Config.DatabaseKeyFile))
         {
             logger.Debug("config manager using the database");
             // check for internet connection: http://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net
             if (NetworkUtils.CheckInternetConnection())
             {
                 logger.Debug("initialising the google api wrapper");
                 // init the google wrapper
                 //_gawGoogleWrapper = new GoogleApiWrapper();
                 _gawGoogleWrapper.LoadAuthParams(_cmConfigManager.Config.DatabaseKeyFile);
                 _gawGoogleWrapper.TokenRefreshed += _gawGoogleWrapper_TokenRefreshed;
                 if (!_gawGoogleWrapper.NeedsAuthorization)
                 { // it needs to be authorized before use
                     logger.Debug("initialising the database wrapper");
                     // init the database handler
                     DatabaseWrapper dbWrapper = new DatabaseWrapper(_gawGoogleWrapper.AuthParams);
                     // try initing the config table
                     if (dbWrapper.InitializeConfigTable() && dbWrapper.InitializeActivityLogTable())
                     {
                         // tell the config man to use the database
                         _cmConfigManager.Database = dbWrapper;
                         // reload the config
                         _cmConfigManager.Load();
                         // tell the activity log man to use the database
                         _almActivityLogManager.Database = dbWrapper;
                     }
                 }
                 else
                 {
                     logger.Warn("App not authorized: {0}", _cmConfigManager.Config.DatabaseKeyFile);
                     _niTrayicon.ShowBalloonTip(15000, "Warning", "The app needs to be authorized before using the database! Go to settings!", ToolTipIcon.Warning);
                 }
             }
             else
             {
                 logger.Warn("UseDatabase = true, but no internet connection");
                 _niTrayicon.ShowBalloonTip(15000, "Warning", "The app is configured to use the database, but no internet connection available. Falling back to offline mode.", ToolTipIcon.Warning);
             }
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex, "Error while loading the config: {0}", ex.Message);
         MessageBox.Show("Error loading config! Using default values.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }