Ejemplo n.º 1
0
        private static AppInstance GetLastActiveInstance()
        {
            var instances = AppInstance.GetInstances();

            if (instances.Count == 0)
            {
                return(null);
            }
            else if (instances.Count == 1)
            {
                return(instances.FirstOrDefault());
            }

            string activeInstance = ApplicationSettingsStore.Read("ActiveInstance") as string;

            if (activeInstance == null)
            {
                return(null);
            }

            foreach (var appInstance in instances)
            {
                if (appInstance.Key == activeInstance)
                {
                    return(appInstance);
                }
            }

            // activeInstance might be closed, let's return the first instance
            return(instances.FirstOrDefault());
        }
Ejemplo n.º 2
0
        private static AppInstance GetLastActiveInstance()
        {
            var instances = AppInstance.GetInstances();

            if (instances.Count == 0)
            {
                return(null);
            }
            else if (instances.Count == 1)
            {
                return(instances.FirstOrDefault());
            }

            if (!(ApplicationSettingsStore.Read(SettingsKey.ActiveInstanceIdStr) is string activeInstance))
            {
                return(null);
            }

            foreach (var appInstance in instances)
            {
                if (appInstance.Key == activeInstance)
                {
                    return(appInstance);
                }
            }

            // activeInstance might be closed already, let's return the first instance in this case
            return(instances.FirstOrDefault());
        }
Ejemplo n.º 3
0
        public async Task <int> LoadLastSessionAsync()
        {
            if (_loaded)
            {
                return(0); // Already loaded
            }

            if (!(ApplicationSettingsStore.Read(SessionDataKey) is string data))
            {
                return(0); // No session data found
            }

            NotepadsSessionDataV1 sessionData;

            try
            {
                sessionData = JsonConvert.DeserializeObject <NotepadsSessionDataV1>(data, _encodingConverter);
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"Failed to load session metadata: {ex.Message}");
                return(0);
            }

            ITextEditor selectedTextEditor = null;

            foreach (TextEditorSessionData textEditorData in sessionData.TextEditors)
            {
                ITextEditor textEditor;

                try
                {
                    textEditor = await RecoverTextEditorAsync(textEditorData);
                }
                catch
                {
                    continue;
                }

                if (textEditor != null)
                {
                    _sessionData.TryAdd(textEditor.Id, textEditorData);

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

            if (selectedTextEditor != null)
            {
                _notepadsCore.SwitchTo(selectedTextEditor);
            }

            _loaded = true;

            return(_sessionData.Count);
        }
Ejemplo n.º 4
0
        private void Sets_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
        {
            if (ApplicationSettingsStore.Read(SetDragAndDropActionStatus) is string setDragAndDropActionStatus && setDragAndDropActionStatus == "Handled")
            {
                if (args.Items.FirstOrDefault() is ITextEditor editor)
                {
                    TextEditorMovedToAnotherAppInstance?.Invoke(this, editor);
                }
            }

            ApplicationSettingsStore.Remove(SetDragAndDropActionStatus);
        }
Ejemplo n.º 5
0
        private void Sets_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
        {
            if (ApplicationSettingsStore.Read(SetDragAndDropActionStatus) is string setDragAndDropActionStatus && setDragAndDropActionStatus == "Handled")
            {
                if (args.Items.FirstOrDefault() is ITextEditor editor)
                {
                    DeleteTextEditor(editor);
                }
            }

            ApplicationSettingsStore.Remove(SetDragAndDropActionStatus);
        }
Ejemplo n.º 6
0
        private static void RedirectOrCreateNewInstance()
        {
            var instance = (GetLastActiveInstance() ?? AppInstance.FindOrRegisterInstanceForKey(App.Id.ToString()));

            if (instance.IsCurrentInstance)
            {
                Windows.UI.Xaml.Application.Start(p => new App());
            }
            else
            {
                // open new instance if user prefers to
                if (ApplicationSettingsStore.Read(SettingsKey.AlwaysOpenNewWindowBool) is bool alwaysOpenNewWindowBool && alwaysOpenNewWindowBool)
                {
                    OpenNewInstance();
                }
Ejemplo n.º 7
0
        public async Task SaveSessionAsync()
        {
            if (!IsBackupEnabled)
            {
                LoggingService.LogInfo("Session backup is disabled.");
                return;
            }

            // Serialize saves
            await _semaphoreSlim.WaitAsync();

            Stopwatch stopwatch = Stopwatch.StartNew();

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

            FileSystemUtility.ClearFutureAccessList();

            NotepadsSessionDataV1 sessionData = new NotepadsSessionDataV1();

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

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

                    if (textEditor.IsModified)
                    {
                        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;
                        }

                        // 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);
                }

                sessionData.TextEditors.Add(textEditorData);

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

            bool sessionDataSaved = false;

            try
            {
                string sessionJsonStr = JsonConvert.SerializeObject(sessionData, _encodingConverter);

                if (!(ApplicationSettingsStore.Read(SessionDataKey) is string currentValue) || !string.Equals(currentValue, sessionJsonStr, StringComparison.OrdinalIgnoreCase))
                {
                    ApplicationSettingsStore.Write(SessionDataKey, sessionJsonStr);
                    sessionDataSaved = true;
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"Failed to save session metadata: {ex.Message}");
                return; // Failed to save the session - do not proceed to delete backup files
            }

            if (sessionDataSaved)
            {
                await DeleteOrphanedBackupFilesAsync(sessionData);
            }

            stopwatch.Stop();

            if (sessionDataSaved)
            {
                LoggingService.LogInfo($"Successfully saved the current session. Total time: {stopwatch.Elapsed.TotalMilliseconds} milliseconds.", consoleOnly: true);
            }

            _semaphoreSlim.Release();
        }