コード例 #1
0
        /// <inheritdoc />
        protected override void OnPluginCrashed(PluginInstance pluginInstance)
        {
            ToastContent toastContent = new ToastContent
            {
                Visual = new ToastVisual
                {
                    BindingGeneric = new ToastBindingGeneric
                    {
                        Children =
                        {
                            new AdaptiveText
                            {
                                Text = $"{pluginInstance.ToString().CapitalizeFirst()} has crashed."
                            }
                        }
                    }
                },
                Actions = new ToastActionsCustom
                {
                    Buttons =
                    {
                        // Restart action
                        new ToastButton("Restart",
                                        new QueryString
                        {
                            { "action",                        ToastActionRestartAfterCrash},
                            { ToastActionParameterPluginId,    pluginInstance.Package.Id  }
                        }.ToString())
                        {
                            ActivationType = ToastActivationType.Background
                        },

                        // Open logs folder action
                        new ToastButton("Open the logs folder",SMAFileSystem.LogDir.FullPathWin)
                        {
                            ActivationType = ToastActivationType.Protocol
                        }
                    }
                }
            };

            var doc = new XmlDocument();

            doc.LoadXml(toastContent.GetContent());

            // And create the toast notification
            var toast = new ToastNotification(doc);

            // And then show it
            DesktopNotificationManager.CreateToastNotifier().Show(toast);
        }
コード例 #2
0
        /// <summary>Sends a Windows desktop notification toast about the success or failure of the update</summary>
        /// <param name="updateVersion">The final release version</param>
        private void NotifyUpdateResult(ReleaseEntry updateVersion)
        {
            var msg = State == SMAUpdateState.Updated
        ? $"SMA has been updated to version {updateVersion.Version}. Restart SMA to use the new version."
        : $"An error occured while updating SMA to version {updateVersion.Version}. Check the logs for more information.";

            ToastContent toastContent = new ToastContent
            {
                Visual = new ToastVisual
                {
                    BindingGeneric = new ToastBindingGeneric
                    {
                        Children =
                        {
                            new AdaptiveText
                            {
                                Text = msg
                            }
                        }
                    }
                }
            };

            if (State == SMAUpdateState.Error)
            {
                toastContent.Actions = new ToastActionsCustom
                {
                    Buttons =
                    {
                        new ToastButton("Open the logs folder", SMAFileSystem.LogDir.FullPathWin)
                        {
                            ActivationType = ToastActivationType.Protocol
                        }
                    }
                }
            }
            ;

            var doc = new XmlDocument();

            doc.LoadXml(toastContent.GetContent());

            // And create the toast notification
            var toast = new ToastNotification(doc);

            // And then show it
            DesktopNotificationManager.CreateToastNotifier().Show(toast);
        }

        #endregion
    }
コード例 #3
0
        public bool ShowDesktopNotification(string toastXml, Guid pluginSessionGuid)
        {
            try
            {
                var plugin = SMAPluginManager.Instance[pluginSessionGuid];

                if (plugin == null)
                {
                    LogTo.Warning("A plugin tried to display windows desktop notification but its session GUID is invalid.");
                    return(false);
                }

                if (DesktopNotificationManager.IsApiAvailable() == false)
                {
                    LogTo.Warning("Plugin {PluginName} tried to display windows desktop notification but Windows API is not available.",
                                  plugin.Package?.Id);
                    return(false);
                }

                // Create and augment the XML definition
                var doc = new XmlDocument();
                doc.LoadXml(toastXml);

                AddPluginSessionGuidArgument(doc, pluginSessionGuid);

                // And create the toast notification
                var toast = new ToastNotification(doc);

                // And then show it
                DesktopNotificationManager.CreateToastNotifier().Show(toast);

                return(true);
            }
            catch (Exception ex)
            {
                // ToastNotifier.Show() throws a generic exception when the notification API isn't available
                LogTo.Warning(ex, "Failed to show desktop toast notification.\r\nToast xml: {ToastXml}", toastXml);
            }

            return(false);
        }
コード例 #4
0
        private async Task LoadAppAsync(SMAParameters args)
        {
            //
            // Installer events https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/custom-squirrel-events.md
            if (SMAInstaller.HandleEvent(args, out var firstRun))
            {
                if (firstRun)
                {
                    MessageBox.Show("SuperMemo Assistant has been successfully installed.", "Installation success");
                }

                Shutdown();
                return;
            }

            //
            // Make sure assemblies are available, and SMA is installed in "%LocalAppData%\SuperMemoAssistant"
            if (AssemblyCheck.CheckRequired(out var errMsg) == false || CheckSMALocation(out errMsg) == false)
            {
                LogTo.Warning(errMsg);
                await errMsg.ErrorMsgBox().ConfigureAwait(false);

                Shutdown(SMAExitCodes.ExitCodeDependencyError);
                return;
            }

            //
            // Load main configuration files
            var(success, nativeDataCfg, coreCfg) = await LoadConfigs().ConfigureAwait(true);

            if (success == false)
            {
                errMsg =
                    $"At least one essential config file could not be loaded: nativeDataCfg ? {nativeDataCfg == null} ; startupCfg ? {coreCfg == null}";
                LogTo.Warning(errMsg);
                await errMsg.ErrorMsgBox().ConfigureAwait(false);

                Shutdown(SMAExitCodes.ExitCodeConfigError);
                return;
            }

            SMA.Core.CoreConfig = coreCfg;

            //
            // Setup Windows Toast notifications
            DesktopNotificationManager.RegisterAumidAndComServer <SMANotificationActivator>(SMANotificationActivator.AppUserModelId);
            DesktopNotificationManager.RegisterActivator <SMANotificationActivator>();

            //
            // Initialize the plugin manager
            await SMAPluginManager.Instance.InitializeAsync().ConfigureAwait(true);

            //
            // Check if SMA is setup, and run the setup wizard if it isn't
            if (SMASetup.Run(nativeDataCfg, coreCfg) == false)
            {
                LogTo.Warning("SMA Setup canceled. Exiting.");

                Shutdown(SMAExitCodes.ExitCodeSMASetupError);
                return;
            }

            //
            // Start plugins
            var pluginStartTask = SMAPluginManager.Instance.StartPlugins().ConfigureAwait(true);

            //
            // (Optional) Start the debugging tool Key logger (logs key strokes with modifiers, e.g. ctrl, alt, ..)
            if (args.KeyLogger)
            {
                SMA.Core.KeyboardHotKey.MainCallback = hk => LogTo.Debug("Key pressed: {Hk}", hk);
            }

            //
            // Show the change logs if necessary
            ChangeLogWindow.ShowIfUpdated(coreCfg);

            //
            // Determine which collection to open
            SMCollection smCollection = null;
            var          selectionWdw = new CollectionSelectionWindow(coreCfg);

            // Try to open command line collection, if one was passed
            if (args.CollectionKnoPath != null && selectionWdw.ValidateSuperMemoPath())
            {
                smCollection = new SMCollection(args.CollectionKnoPath, DateTime.Now);

                if (selectionWdw.ValidateCollection(smCollection) == false)
                {
                    smCollection = null;
                }
            }

            // No valid collection passed, show selection window
            if (smCollection == null)
            {
                selectionWdw.ShowDialog();

                smCollection = selectionWdw.Collection;
            }

            //
            // If a collection was selected, start SMA
            if (smCollection != null)
            {
                _splashScreen = new SplashScreenWindow();
                _splashScreen.Show();

                SMA.Core.SMA.OnSMStartingInternalEvent += OnSMStartingEventAsync;
                SMA.Core.SMA.OnSMStoppedInternalEvent  += OnSMStoppedEvent;

                // Wait for plugins to start
                await pluginStartTask;

                Exception ex;

                if ((ex = await SMA.Core.SMA.StartAsync(nativeDataCfg, smCollection).ConfigureAwait(true)) != null)
                {
                    _splashScreen?.Close();
                    _splashScreen = null;

                    await $"SMA failed to start: {ex.Message}".ErrorMsgBox().ConfigureAwait(false);

                    Shutdown(SMAExitCodes.ExitCodeSMAStartupError);

                    return;
                }

                if (SMAExecutableInfo.Instance.IsDev == false)
                {
                    await SMAInstaller.Instance.Update().ConfigureAwait(false);
                }
            }
            else
            {
                Shutdown();
            }
        }