Ejemplo n.º 1
0
        // Handler for when the form is being closed
        private async void NotificationThread_FormClosing(object sender, FormClosingEventArgs e)
        {
            // If the (obsolete) setting Close to tray is turned off, or something other than the user
            // requested that this Ui should close, then allow it to close and terminate the application
            bool canCloseApp = (e.CloseReason == CloseReason.UserClosing && !Core.Settings.CloseToTray) || _userExiting || e.CloseReason != CloseReason.UserClosing;

            if (canCloseApp)
            {
                // Make sure the application is aware that it's closing
                _userExiting = true;

                // Remove the icon from the tray
                trayIcon.Visible = false;
                trayIcon.Dispose();

                // Unload any notifications that might be displayed
                NotificationHelper.ClearNotifications();

                // Stop any Scrobble related functions
                ScrobbleFactory.ScrobblingEnabled = false;

                // Give the scrobblers a chance to stop running cleanly
                await Task.Delay(2000).ConfigureAwait(false);

                ScrobbleFactory.Dispose();

                // Close any instance of the (obsolete) settings form
                _settingsUI?.Close();
            }
            else
            {
                // Close / Minimize to tray is turned on (default)
                // so cancel the disposal of the Ui, and hide it from the taskbar
                e.Cancel = true;
                MinimizeToTray();
            }
        }
Ejemplo n.º 2
0
        // The authentication loop method
        private async void Startup(bool afterLogOut = false)
        {
            // If we're here because the user just launched the application and hasn't authorized the application
            if (!afterLogOut)
            {
                // Set the appropriate status on the (hidden) status bar
                SetStatus(LocalizationStrings.NotificationThread_Status_StartingUp);

                // Initialize the application
                List <Exception> pathExceptions = Core.InitializeApplication();

                if (pathExceptions != null && pathExceptions.Any())
                {
                    foreach (Exception ex in pathExceptions)
                    {
                        Logger.FileLogger.Write(_logPathAndFilename, "Scrobble Ui", $"Unexpected Exception during application start: {ex.Message}");
                    }
                }

                // Hide this user interface
                if (Core.Settings.StartMinimized)
                {
                    this.WindowState = FormWindowState.Minimized;
                }

                // Set the appropriate status on the (hidden) status bar
                SetStatus(LocalizationStrings.ScrobblerUi_Status_LoadingPlugins);

                // Get the available external scrobble plugins
                await GetPlugins().ConfigureAwait(false);
            }
            else
            {
                // Remove the online status tracking delegate
                ScrobbleFactory.OnlineStatusUpdated -= OnlineStatusUpdated;
            }

            // Set the appropriate status on the (hidden) status bar
            SetStatus(LocalizationStrings.ScrobblerUi_Status_ConnectingToLastfm);

            // Use the Last.fm API client to check the status of authorization
            await ConnectToLastFM(afterLogOut).ConfigureAwait(false);

            // Initialize the Scrobbling handler (only gets here when the client is authorized)
            await ScrobbleFactory.Initialize(base.APIClient, this, _logPathAndFilename).ConfigureAwait(false);

            // Make sure we have a default status available for all the loaded plugins
            ApplicationConfiguration.CheckPluginDefaultStatus();

            // If we got here because the application is starting
            if (!afterLogOut)
            {
                // Update the user interface to show the current settings
                DisplaySettings();
            }

            // Enable scrobbling only if there are any plugins enabled
            ScrobbleFactory.ScrobblingEnabled = Core.Settings.ScrobblerStatus.Count(plugin => plugin.IsEnabled) > 0;

            // Assign a delegate to update the Ui when the user goes on/offline with the Last.fm API
            ScrobbleFactory.OnlineStatusUpdated += OnlineStatusUpdated;
        }