Ejemplo n.º 1
0
        /// <summary>
        /// Executes when the download button in the navigation bar is pressed.
        ///
        /// </summary>
        /// <param name="obj">Event sender</param>
        /// <param name="e">Event args</param>
        private void NavBar_DownloadButtonPress(object obj, MouseButtonEventArgs e)
        {
            // In case the app is loading a download of another webpage,
            // do not download anything else
            if (this.NavigationBar.loadingWebpageControl.IsEnabled ||
                OrganicUtility.IsLocalPageUrl(this.WebBrowser.Address))
            {
                return;
            }

            string url = this.WebBrowser.Address;   // Url to download
            var    webpageDownloader = new WebsiteDownloader.WebpageDownloader(
                url, AppData.DownloadedPagesLocation, websiteName: OrganicUtility.GetDomainName(url));

            // When the download is started enable the loading animation
            // When finished disable the loading animation
            webpageDownloader.StartedDownloading += (object sender, System.EventArgs ea) =>
            {
                this.NavigationBar.Dispatcher.Invoke(() => this.NavigationBar.loadingWebpageControl.IsEnabled = true);
            };
            webpageDownloader.FinishedDownloading += (object sender, System.EventArgs ea) =>
            {
                this.NavigationBar.Dispatcher.Invoke(() => this.NavigationBar.loadingWebpageControl.IsEnabled = false);
            };

            // Download the page asynchronously
            Task downloadTask = new Task(webpageDownloader.Download);

            downloadTask.Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Makes sure that the cachefolder exists,
        /// if does not exist, creates one.
        /// </summary>
        private static void ValidateCacheFolder()
        {
            string absCachePath = OrganicUtility.GetAbsolutePath(CachePath);

            // In case history file does not exist, it means that someone deleted the hostory, therefore
            // the cached data should be deleted as well.
            if (!File.Exists(OrganicUtility.GetAbsolutePath(AppData.HistoryPath)) && File.Exists(absCachePath))
            {
                Directory.Delete(absCachePath, recursive: true);
            }

            if (!Directory.Exists(absCachePath))
            {
                Directory.CreateDirectory(absCachePath);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes CEF
        /// </summary>
        public static void Initialize()
        {
            // Add the settings to the CefSettings object
            var settings = new CefSharp.Wpf.CefSettings();

            foreach (KeyValuePair <string, string> command in Commands)
            {
                settings.CefCommandLineArgs.Add(command);
            }

            // Set the cache path in the CefSettings object
            ValidateCacheFolder();              // Make sure that the cache folder exists
            settings.CachePath = OrganicUtility.GetAbsolutePath(CachePath);

            // Initialize Cef
            CefSharp.Cef.Initialize(settings);

            settings.Dispose();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes when loading error is occured.
        ///
        /// Basically, show an html page for each error.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebBrowser_LoadError(object sender, CefSharp.LoadErrorEventArgs e)
        {
            // Save the original url
            string originalUrl = null;

            this.NavigationBar.Dispatcher.Invoke(() => originalUrl = this.NavigationBar.urlTextBox.Text);

            // In case error aborted by CEF, do nothing
            if (e.ErrorCode == CefSharp.CefErrorCode.Aborted)
            {
                return;
            }

            // Handle loading errors
            string errorPagePath = string.Empty;

            if (OrganicUtility.IsConnected() == false)
            {
                // In case of no internet
                errorPagePath = "organic://error/no_internet";
            }
            else if (e.ErrorCode == CefSharp.CefErrorCode.NameNotResolved)
            {
                // In case of domain could not be resolved
                errorPagePath = "organic://error/could_not_resolve";
            }
            else
            {
                // Default loading error page
                System.Console.WriteLine(e.ErrorText);
                errorPagePath = "organic://error/general";
            }

            // Handle more loading errors here

            // Load the error page
            this.WebBrowser.Dispatcher.Invoke(() => this.WebBrowser.Address = OrganicUtility.GetLocalPageActualUrl(errorPagePath));

            // Restore the url
            this.NavigationBar.Dispatcher.Invoke(() => this.NavigationBar.urlTextBox.Text = originalUrl);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Executes when a key in the keyboard is up in the URL text box
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="eventArgs"></param>
 private void UrlTextBox_KeyUp(object sender, KeyEventArgs eventArgs)
 {
     if (eventArgs.Key == Key.Enter)
     {
         // In case the given url is a valid address
         if (OrganicUtility.IsValidUrl(this.NavigationBar.Url))
         {
             this.WebBrowser.Address = this.NavigationBar.Url;
         }
         // In case the given url is an organic url (such as organic://history)
         else if (OrganicUtility.IsOrganicUrl(this.NavigationBar.Url))
         {
             this.WebBrowser.Address = OrganicUtility.GetLocalPageActualUrl(this.NavigationBar.Url);
         }
         // In case the given url is NOT actually a url
         else
         {
             this.WebBrowser.Address = "https://www.google.com/search?q=" + HttpUtility.UrlEncode(this.NavigationBar.Url);
         }
     }
 }