public SuperMemoFilePath(FilePath filePath, NativeDataCfg nativeDataCfg)
            {
                FilePath = filePath;

                HasErrors = SMA.Utils.SuperMemoFinder.CheckSuperMemoExecutable(nativeDataCfg, filePath, out _, out var ex) == false;
                _ex       = ex;
            }
Exemplo n.º 2
0
        //
        // Collection loading management

        public async Task <bool> Start(
            NativeDataCfg nativeDataCfg,
            StartupCfg startupCfg,
            SMCollection collection)
        {
            try
            {
                if (_sm != null)
                {
                    throw new InvalidOperationException("_sm is already instantiated");
                }

                await LoadConfig(collection, startupCfg);

                var nativeData = CheckSuperMemoExecutable(nativeDataCfg);

                _sm = InstantiateSuperMemo(collection, nativeData.SMVersion);

                // TODO: Move somewhere else
                _sm.UI.ElementWdw.OnAvailable += OnSuperMemoWindowsAvailable;

                await _sm.Start(nativeData);

                // TODO: Ensure opened collection (windows title) matches parameter
            }
            catch (Exception ex)
            {
                if (ex is SMAException)
                {
                    LogTo.Warning(ex, "Failed to start SM.");
                }

                else
                {
                    LogTo.Error(ex, "Failed to start SM.");
                }

                _sm?.Dispose();
                _sm = null;

                try
                {
                    if (OnSMStoppedEvent != null)
                    {
                        await OnSMStoppedEvent.InvokeAsync(this, new SMProcessArgs(_sm, null)).ConfigureAwait(true);
                    }
                }
                catch (Exception pluginEx)
                {
                    LogTo.Error(pluginEx, "Exception while notifying plugins OnSMStoppedEvent.");
                }

                // TODO: Handle exception

                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
 public static bool ShouldFindSuperMemo(StartupCfg startupCfg, NativeDataCfg nativeDataCfg)
 {
     return(string.IsNullOrWhiteSpace(startupCfg.SMBinPath) ||
            SuperMemoFinderUtil.CheckSuperMemoExecutable(
                nativeDataCfg,
                startupCfg.SMBinPath,
                out _,
                out _) == false);
 }
Exemplo n.º 4
0
        private Task <bool> LoadConfigs(out NativeDataCfg nativeDataCfg, out StartupCfg startupCfg)
        {
            nativeDataCfg = LoadNativeDataConfig().Result;
            startupCfg    = LoadStartupConfig().Result;

            if (nativeDataCfg == null || startupCfg == null)
            {
                return(Task.FromResult(false));
            }

            return(Task.FromResult(true));
        }
        public SuperMemoFinder(NativeDataCfg nativeDataCfg, CoreCfg startupCfg)
        {
            _nativeDataCfg = nativeDataCfg;
            _startupCfg    = startupCfg;

            _initialIsSetup = ShouldFindSuperMemo(startupCfg, nativeDataCfg) == false;
            Description     = BuildDescriptionText();

            BrowseCommand = new RelayCommand(Browse);

            InitializeComponent();
        }
Exemplo n.º 6
0
        private Task <bool> LoadConfigs(out NativeDataCfg nativeDataCfg, out CoreCfg coreCfg)
        {
            nativeDataCfg = LoadNativeDataConfig().Result;
            coreCfg       = LoadCoreConfig().Result;

            if (nativeDataCfg == null || coreCfg == null)
            {
                return(Task.FromResult(false));
            }

            return(Task.FromResult(true));
        }
Exemplo n.º 7
0
        private NativeData CheckSuperMemoExecutable(NativeDataCfg nativeDataCfg)
        {
            var smFile = new FilePath(StartupConfig.SMBinPath);

            if (SuperMemoFinder.CheckSuperMemoExecutable(nativeDataCfg, smFile, out var nativeData, out var ex) == false)
            {
                throw ex;
            }

            LogTo.Information($"SuperMemo version {nativeData.SMVersion} detected");

            return(nativeData);
        }
Exemplo n.º 8
0
        public static bool Run(NativeDataCfg nativeDataCfg, CoreCfg startupCfg)
        {
            var setup = new SMASetup(nativeDataCfg, startupCfg);

            if (setup.IsSetupRequired() == false)
            {
                return(true);
            }

            setup.ShowDialog();

            return(setup.DialogResult ?? false);
        }
Exemplo n.º 9
0
        protected SMASetup(NativeDataCfg nativeDataCfg, CoreCfg startupCfg)
        {
            NextCommand = new RelayCommand(ShowNextScreen /*, CanNextExecute*/);
            BackCommand = new RelayCommand(ShowPreviousScreen);

            Screens = new ObservableCollection <SMASetupScreenBase>
            {
                new TermsOfLicense(startupCfg),
                new SuperMemoFinder(nativeDataCfg, startupCfg),
                new ImportCollections(startupCfg),
                new PluginSetup(),
            };

            InitializeComponent();
        }
Exemplo n.º 10
0
        //
        // Collection loading management

        public async Task <Exception> StartAsync(
            NativeDataCfg nativeDataCfg,
            SMCollection collection)
        {
            try
            {
                if (_sm != null)
                {
                    throw new InvalidOperationException("_sm is already instantiated");
                }

                // Load collection config
                await LoadConfigAsync(collection).ConfigureAwait(false);

                // Make sure the SuperMemo version is compatible and instantiate it
                var nativeData = CheckSuperMemoExecutable(nativeDataCfg);

                _sm = GetSuperMemoFactory(collection, nativeData.SMVersion);

                // Notify Plugins of selected collection
                await OnCollectionSelectedAsync(collection).ConfigureAwait(false);

                await _sm.StartAsync(nativeData).ConfigureAwait(false);

                // TODO: Ensure opened collection (windows title) matches parameter
            }
            catch (Exception ex)
            {
                if (ex is SMAException)
                {
                    LogTo.Warning(ex, "Failed to start SM.");
                }

                else
                {
                    LogTo.Error(ex, "Failed to start SM.");
                }

                _sm?.Dispose();
                _sm = null;

                // TODO: Handle exception

                return(ex);
            }

            return(null);
        }
        public static bool CheckSuperMemoExecutable(
            NativeDataCfg nativeDataCfg,
            FilePath smFile,
            out NativeData nativeData,
            out SMAException ex)
        {
            nativeData = null;

            if (smFile == null)
            {
                ex = new SMAException("SM exe file path is null", new ArgumentNullException(nameof(smFile)));
                return(false);
            }

            if (smFile.Exists() == false)
            {
                ex = new SMAException(
                    $"Invalid file path for sm executable file: '{smFile}' could not be found. SMA cannot continue.");
                return(false);
            }

            if (smFile.HasPermission(FileIOPermissionAccess.Read) == false)
            {
                ex = new SMAException($"SMA needs read access to execute SM executable at {smFile.FullPath}.");
                return(false);
            }

            if (smFile.IsLocked())
            {
                ex = new SMAException($"{smFile.FullPath} is locked. Make sure it isn't already running.");
                return(false);
            }

            var smFileCrc32 = FileEx.GetCrc32(smFile.FullPath);

            nativeData = nativeDataCfg.SafeGet(smFileCrc32.ToUpper(CultureInfo.InvariantCulture));

            if (nativeData == null)
            {
                ex = new SMAException($"Unknown SM executable version with crc32 {smFileCrc32}.");
                return(false);
            }

            ex = null;

            return(true);
        }
Exemplo n.º 12
0
        //
        // Collection loading management

        public async Task <Exception> Start(
            NativeDataCfg nativeDataCfg,
            SMCollection collection)
        {
            try
            {
                if (_sm != null)
                {
                    throw new InvalidOperationException("_sm is already instantiated");
                }

                await LoadConfig(collection);

                var nativeData = CheckSuperMemoExecutable(nativeDataCfg);

                _sm = InstantiateSuperMemo(collection, nativeData.SMVersion);

                // TODO: Move somewhere else
                _sm.UI.ElementWdw.OnAvailable += OnSuperMemoWindowsAvailable;

                await _sm.Start(nativeData);

                // TODO: Ensure opened collection (windows title) matches parameter
            }
            catch (Exception ex)
            {
                if (ex is SMAException)
                {
                    LogTo.Warning(ex, "Failed to start SM.");
                }

                else
                {
                    LogTo.Error(ex, "Failed to start SM.");
                }

                _sm?.Dispose();
                _sm = null;

                // TODO: Handle exception

                return(ex);
            }

            return(null);
        }
        public SuperMemoFinder(NativeDataCfg nativeDataCfg, StartupCfg startupCfg)
        {
            _nativeDataCfg = nativeDataCfg;
            _startupCfg    = startupCfg;

            SMExeFilePath           = new SuperMemoFilePath(startupCfg.SMBinPath, nativeDataCfg);
            SMExeSuggestedFilePaths = SuperMemoFinderUtil.SearchSuperMemoInDefaultLocations()
                                      .Select(fp => fp.FullPathWin)
                                      .ToHashSet();

            AcceptCommand = new RelayCommand(Accept, CanAcceptExecute);
            BrowseCommand = new RelayCommand(Browse);

            BuildDescriptionText();

            Task.Run(SearchForSuperMemo).RunAsync();

            InitializeComponent();
        }