Esempio n. 1
0
        public async Task SaveSessionAsync()
        {
            if (!IsBackupEnabled)
            {
                LoggingService.LogInfo("Session backup is disabled.");
                return;
            }

            // Serialize saves
            await _semaphoreSlim.WaitAsync();

            StorageFolder backupFolder = await SessionUtility.GetBackupFolderAsync();

            LoggingService.LogInfo("Session backup is starting. Backup folder: " + backupFolder.Path);
            Stopwatch stopwatch = Stopwatch.StartNew();

            TextEditor[] textEditors        = _notepadsCore.GetAllTextEditors();
            TextEditor   selectedTextEditor = _notepadsCore.GetSelectedTextEditor();

            FileSystemUtility.ClearFutureAccessList();

            NotepadsSessionDataV1 sessionData = new NotepadsSessionDataV1();

            foreach (TextEditor textEditor in textEditors)
            {
                if (textEditor.EditingFile != null)
                {
                    // Add the opened file to FutureAccessList so we can access it next launch
                    FileSystemUtility.TryAddToFutureAccessList(ToToken(textEditor.Id), textEditor.EditingFile);
                }

                if (!_sessionData.TryGetValue(textEditor.Id, out TextEditorSessionData textEditorData))
                {
                    textEditorData = new TextEditorSessionData {
                        Id = textEditor.Id
                    };

                    if (textEditor.EditingFile != null)
                    {
                        // Persist the last save known to the app, which might not be up-to-date (if the file was modified outside the app)
                        BackupMetadata lastSaved = await SaveLastSavedChangesAsync(textEditor);

                        if (lastSaved == null)
                        {
                            continue;
                        }

                        textEditorData.LastSaved = lastSaved;
                    }

                    if (textEditor.IsModified)
                    {
                        // Persist pending changes relative to the last save
                        BackupMetadata pending = await SavePendingChangesAsync(textEditor);

                        if (pending == null)
                        {
                            continue;
                        }

                        textEditorData.Pending = pending;
                    }

                    // We will not create new backup files for this text editor unless it has changes
                    _sessionData.TryAdd(textEditor.Id, textEditorData);
                }

                if (textEditorData.LastSaved != null || textEditorData.Pending != null)
                {
                    sessionData.TextEditors.Add(textEditorData);

                    if (textEditor == selectedTextEditor)
                    {
                        sessionData.SelectedTextEditor = textEditor.Id;
                    }
                }
            }

            try
            {
                string sessionJson = JsonConvert.SerializeObject(sessionData, _encodingConverter);
                ApplicationData.Current.LocalSettings.Values[SessionDataKey] = sessionJson;
            }
            catch
            {
                return; // Failed to save the session - do not proceed to delete backup files
            }

            await DeleteOrphanedBackupFilesAsync(sessionData);

            stopwatch.Stop();
            LoggingService.LogInfo("Successfully saved the current session. Total time: " + stopwatch.Elapsed.TotalMilliseconds + " milliseconds.");

            _semaphoreSlim.Release();
        }