コード例 #1
0
        /// <inheritdoc/>
        public RepositoryStorageLoadResult LoadRepositoryOrDefault(out NoteRepositoryModel repositoryModel)
        {
            if (_cachedRepository != null)
            {
                repositoryModel = _cachedRepository;
                return(RepositoryStorageLoadResult.SuccessfullyLoaded);
            }

            RepositoryStorageLoadResult result;

            repositoryModel = null;
            bool modelWasUpdated = false;

            try
            {
                // A new repository is created only if it does not yet exist, we won't overwrite
                // an invalid repository.
                if (RepositoryExists())
                {
                    string xmlFilePath = Path.Combine(GetDirectoryPath(), Config.RepositoryFileName);
                    if (!_xmlFileService.TryLoad(xmlFilePath, out XDocument xml))
                    {
                        throw new Exception("Invalid XML");
                    }

                    result          = RepositoryStorageLoadResult.SuccessfullyLoaded;
                    modelWasUpdated = _updater.Update(xml);
                    repositoryModel = XmlUtils.DeserializeFromXmlDocument <NoteRepositoryModel>(xml);
                }
                else
                {
                    result                   = RepositoryStorageLoadResult.CreatedNewEmptyRepository;
                    repositoryModel          = new NoteRepositoryModel();
                    repositoryModel.Revision = NoteRepositoryModel.NewestSupportedRevision;
                    AddWelcomeNote(repositoryModel);
                    modelWasUpdated = true;
                }
            }
            catch (Exception)
            {
                result          = RepositoryStorageLoadResult.InvalidRepository;
                repositoryModel = null;
                modelWasUpdated = false;
            }

            // Automatically save settings if they where modified by an update
            if (modelWasUpdated)
            {
                TrySaveRepository(repositoryModel);
            }
            _cachedRepository = repositoryModel;
            return(result);
        }
コード例 #2
0
        /// <inheritdoc/>
        public override async Task Run()
        {
            try
            {
                SettingsModel settings = _settingsService.LoadSettingsOrDefault();
                byte[]        binaryCloudRepository = StoryBoard.LoadFromSession <byte[]>(SynchronizationStorySessionKey.BinaryCloudRepository);

                // Try to decode with all possible transfer codes
                bool successfullyDecryptedRepository = TryDecryptWithAllTransferCodes(
                    settings, binaryCloudRepository, out byte[] decryptedRepository);

                if (successfullyDecryptedRepository)
                {
                    // Deserialize and update repository
                    XDocument cloudRepositoryXml = XmlUtils.LoadFromXmlBytes(decryptedRepository);
                    if (_noteRepositoryUpdater.IsTooNewForThisApp(cloudRepositoryXml))
                    {
                        throw new SynchronizationStoryBoard.UnsuportedRepositoryRevisionException();
                    }

                    _noteRepositoryUpdater.Update(cloudRepositoryXml);
                    NoteRepositoryModel cloudRepository = XmlUtils.DeserializeFromXmlDocument <NoteRepositoryModel>(cloudRepositoryXml);

                    // Continue with next step
                    StoryBoard.StoreToSession(SynchronizationStorySessionKey.CloudRepository, cloudRepository);
                    await StoryBoard.ContinueWith(SynchronizationStoryStepId.IsSameRepository);
                }
                else
                {
                    bool existsUserEnteredTransferCode = StoryBoard.TryLoadFromSession <string>(SynchronizationStorySessionKey.UserEnteredTransferCode, out _);
                    if (existsUserEnteredTransferCode)
                    {
                        // Keep transfercode page open and show message
                        _feedbackService.ShowToast(_languageService["sync_error_transfercode"]);
                    }
                    else
                    {
                        // Open transfercode page
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.ShowTransferCode);
                    }
                }
            }
            catch (Exception ex)
            {
                // Keep the current page open and show the error message
                ShowExceptionMessage(ex, _feedbackService, _languageService);
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public override async Task Run()
        {
            try
            {
                SettingsModel settings = _settingsService.LoadSettingsOrDefault();
                byte[]        binaryCloudRepository = StoryBoard.LoadFromSession <byte[]>(SynchronizationStorySessionKey.BinaryCloudRepository.ToInt());
                List <string> transferCodesToTry    = ListTransferCodesToTry(settings);

                // Try to decode with all possible transfer codes
                EncryptorDecryptor encryptor           = new EncryptorDecryptor("SilentNotes");
                byte[]             decryptedRepository = null;
                bool successfullyDecryptedRepository   = false;
                int  index = 0;
                while (!successfullyDecryptedRepository && index < transferCodesToTry.Count)
                {
                    string transferCodeCandidate = transferCodesToTry[index];
                    successfullyDecryptedRepository = TryDecryptRepositoryWithTransfercode(encryptor, binaryCloudRepository, transferCodeCandidate, out decryptedRepository);
                    if (successfullyDecryptedRepository)
                    {
                        // Store transfercode and encryption mode if necessary
                        if (AdoptTransferCode(settings, transferCodeCandidate) ||
                            AdoptEncryptionMode(settings, encryptor, binaryCloudRepository))
                        {
                            _settingsService.TrySaveSettingsToLocalDevice(settings);
                        }
                    }
                    index++;
                }

                if (successfullyDecryptedRepository)
                {
                    // Deserialize and update repository
                    XDocument cloudRepositoryXml = XmlUtils.LoadFromXmlBytes(decryptedRepository);
                    if (_noteRepositoryUpdater.IsTooNewForThisApp(cloudRepositoryXml))
                    {
                        throw new SynchronizationStoryBoard.UnsuportedRepositoryRevisionException();
                    }

                    _noteRepositoryUpdater.Update(cloudRepositoryXml);
                    NoteRepositoryModel cloudRepository = XmlUtils.DeserializeFromXmlDocument <NoteRepositoryModel>(cloudRepositoryXml);

                    // Continue with next step
                    StoryBoard.StoreToSession(SynchronizationStorySessionKey.CloudRepository.ToInt(), cloudRepository);
                    await StoryBoard.ContinueWith(SynchronizationStoryStepId.IsSameRepository.ToInt());
                }
                else
                {
                    bool existsUserEnteredTransferCode = StoryBoard.TryLoadFromSession <string>(SynchronizationStorySessionKey.UserEnteredTransferCode.ToInt(), out _);
                    if (existsUserEnteredTransferCode)
                    {
                        // Keep transfercode page open and show message
                        _feedbackService.ShowToast(_languageService["sync_error_transfercode"]);
                    }
                    else
                    {
                        // Open transfercode page
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.ShowTransferCode.ToInt());
                    }
                }
            }
            catch (Exception ex)
            {
                // Keep the current page open and show the error message
                ShowExceptionMessage(ex, _feedbackService, _languageService);
            }
        }