public static void Show(Window owner, string title, string message, Exception e, params Exception[] additionalExceptions)
        {
            string split = Environment.NewLine + "--------------------" + Environment.NewLine;

            var dlg = new ExceptionDialog
            {
                ErrorMessage =
                {
                    Text = message
                },
                ErrorTrace =
                {
                    Text = string.Join(split, new List <Exception> {
                        e
                    }.Concat(additionalExceptions).Select(ex => FormatException(ex, "")))
                },
                Title = title,
            };

            if (owner != null && owner.IsLoaded)
            {
                dlg.Owner = owner;
            }
            else
            {
                dlg.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            dlg.ShowDialog();
        }
        private void FullResync()
        {
            if (Repository.ProviderUID == Guid.Parse("37de6de1-26b0-41f5-b252-5e625d9ecfa3"))
            {
                return;                                                                                           // no full resync in headless
            }
            try
            {
                if (MessageBox.Show(Owner, "Do you really want to delete all local data and download the server data?", "Full resync?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }

                Repository.Shutdown(false);

                Repository.DeleteLocalData();

                _repository = new NoteRepository(AppSettings.PATH_LOCALDB, this, Settings, Settings.ActiveAccount, dispatcher);
                _repository.Init();

                OnExplicitPropertyChanged("Repository");

                SelectedNote = null;
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Full Synchronization failed", e);
                ExceptionDialog.Show(Owner, "Full Synchronization failed", e, string.Empty);
            }
        }
        private void DeleteFolder()
        {
            try
            {
                var fp = SelectedFolderPath;

                if (fp == null || HierachicalBaseWrapper.IsSpecial(fp))
                {
                    return;
                }

                var delNotes = Repository.Notes.Where(n => n.Path.IsPathOrSubPath(fp)).ToList();

                if (MessageBox.Show(Owner, $"Do you really want to delete this folder together with {delNotes.Count} contained notes?", "Delete folder?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }

                foreach (var note in delNotes)
                {
                    Repository.DeleteNote(note, true);
                }

                Owner.NotesViewControl.DeleteFolder(fp);

                SelectedNote = Owner.NotesViewControl.EnumerateVisibleNotes().FirstOrDefault() ?? Repository.Notes.FirstOrDefault();
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Could not delete folder", e);
                ExceptionDialog.Show(Owner, "Could not delete folder", e, string.Empty);
            }
        }
Esempio n. 4
0
        private void OnClose(EventArgs args)
        {
            try
            {
                _repository.Shutdown();
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Shutting down connection failed", e);
                ExceptionDialog.Show(Owner, "Shutting down connection failed.\r\nConnection will be forcefully aborted.", e, string.Empty);
                _repository.KillThread();
            }

            if (Settings.RememberScroll)
            {
                _scrollCache.ForceSaveNow();
            }

            if (Settings.RememberPositionAndSize || Settings.RememberWindowState)
            {
                SettingsHelper.ApplyWindowState(Owner, Settings, Settings.RememberPositionAndSize, Settings.RememberPositionAndSize, Settings.RememberWindowState);
                RequestSettingsSave();
            }

            if (_invSaveSettings.HasPendingRequests())
            {
                _invSaveSettings.CancelPendingRequests();
                SaveSettings();
            }

            Owner.MainWindow_OnClosed(args);
        }
        private void ExportNote()
        {
            if (SelectedNote == null)
            {
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();

            if (SelectedNote.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN))
            {
                sfd.Filter   = "Markdown files (*.md)|*.md";
                sfd.FileName = SelectedNote.Title + ".md";
            }
            else
            {
                sfd.Filter   = "Text files (*.txt)|*.txt";
                sfd.FileName = SelectedNote.Title + ".txt";
            }

            if (sfd.ShowDialog() == true)
            {
                try
                {
                    File.WriteAllText(sfd.FileName, SelectedNote.Text, Encoding.UTF8);
                }
                catch (Exception e)
                {
                    App.Logger.Error("Main", "Could not write to file", e);
                    ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty);
                }
            }
        }
        private void CompletedAuto(AsyncCompletedEventArgs args)
        {
            if (args.Error != null)
            {
                ExceptionDialog.Show(_owner, "Could not download new version", args.Error, "");
                ButtonsEnabled = true;
                return;
            }

            try
            {
                var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("B"));

                Directory.CreateDirectory(folder);

                ZipFile.ExtractToDirectory(_savePath, folder);

                var updater = Path.Combine(folder, @"AutoUpdater.exe");

                Process.Start(new ProcessStartInfo(updater, '"' + AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + '"'));

                _owner.Close();
                _owner.MainViewmodel.Exit();
            }
            catch (Exception e)
            {
                ExceptionDialog.Show(_owner, "Could not auto update", e, "");
            }
        }
 private void SaveAndSync()
 {
     try
     {
         Repository.SaveAll();
         Repository.SyncNow();
     }
     catch (Exception e)
     {
         App.Logger.Error("Main", "Synchronization failed", e);
         ExceptionDialog.Show(Owner, "Synchronization failed", e, string.Empty);
     }
 }
        private void CompletedManual(AsyncCompletedEventArgs args)
        {
            if (args.Error != null)
            {
                ExceptionDialog.Show(_owner, "Could not download new version", args.Error, "");
                ButtonsEnabled = true;
                return;
            }

            _owner.Close();

            Process.Start(_savePath);
        }
 private void CreateNote()
 {
     try
     {
         var path = Owner.NotesViewControl.GetNewNotesPath();
         if (Owner.Visibility == Visibility.Hidden)
         {
             ShowMainWindow();
         }
         SelectedNote = Repository.CreateNewNote(path);
     }
     catch (Exception e)
     {
         ExceptionDialog.Show(Owner, "Cannot create note", e, string.Empty);
     }
 }
Esempio n. 10
0
        private void DeleteNote()
        {
            try
            {
                if (SelectedNote == null)
                {
                    return;
                }
                if (Settings.IsReadOnlyMode)
                {
                    return;
                }

                var selection = AllSelectedNotes.ToList();
                if (selection.Count > 1)
                {
                    if (MessageBox.Show(Owner, $"Do you really want to delete {selection.Count} notes?", "Delete multiple notes?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                    {
                        return;
                    }

                    foreach (var note in selection)
                    {
                        Repository.DeleteNote(note, true);
                    }

                    SelectedNote = Repository.Notes.FirstOrDefault();
                }
                else
                {
                    if (MessageBox.Show(Owner, "Do you really want to delete this note?", "Delete note?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                    {
                        return;
                    }

                    Repository.DeleteNote(SelectedNote, true);

                    SelectedNote = Repository.Notes.FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Could not delete note(s)", e);
                ExceptionDialog.Show(Owner, "Could not delete note(s)", e, string.Empty);
            }
        }
        public static void Show(Window owner, string title, Exception e, string additionalInfo)
        {
            var dlg = new ExceptionDialog();

            dlg.Title             = "Error in AlephNote v" + App.APP_VERSION;
            dlg.ErrorMessage.Text = title;
            dlg.ErrorTrace.Text   = (FormatException(e, additionalInfo) ?? FormatStacktrace(additionalInfo));

            if (owner != null && owner.IsLoaded)
            {
                dlg.Owner = owner;
            }
            else
            {
                dlg.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            dlg.ShowDialog();
        }
        private void RenameFolder()
        {
            try
            {
                var oldPath = SelectedFolderPath;
                if (HierachicalBaseWrapper.IsSpecial(oldPath))
                {
                    return;
                }

                if (!GenericInputDialog.ShowInputDialog(Owner, "Insert the name for the folder", "Folder name", oldPath.GetLastComponent(), out var newFolderName))
                {
                    return;
                }

                var newPath = oldPath.ParentPath().SubDir(newFolderName);

                if (newPath.EqualsWithCase(oldPath))
                {
                    return;
                }

                Owner.NotesViewControl.AddFolder(newPath);

                foreach (INote n in Repository.Notes.ToList())
                {
                    if (n.Path.IsPathOrSubPath(oldPath))
                    {
                        n.Path = n.Path.Replace(oldPath, newPath);
                    }
                }

                Owner.NotesViewControl.AddFolder(newPath);
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Could not rename folder", e);
                ExceptionDialog.Show(Owner, "Could not rename folder", e, string.Empty);
            }
        }
        private void FullUpload()
        {
            try
            {
                if (MessageBox.Show(Owner, "Do you really want to trigger a full upload of your local data?\nNormally this should never be necessary, because modified notes will be automatically marked as dirty and uploaded", "Full upload?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }

                foreach (var n in Repository.Notes)
                {
                    n.SetDirty(null);
                }

                Repository.SyncNow();
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Full Upload failed", e);
                ExceptionDialog.Show(Owner, "Full Upload failed", e, string.Empty);
            }
        }
Esempio n. 14
0
        public void OnNewNoteDrop(IDataObject data)
        {
            try
            {
                var notepath = Owner.NotesViewControl.GetNewNotesPath();

                if (data.GetDataPresent(DataFormats.FileDrop, true))
                {
                    string[] paths = data.GetData(DataFormats.FileDrop, true) as string[];
                    foreach (var path in paths ?? new string[0])
                    {
                        var filename    = Path.GetFileNameWithoutExtension(path) ?? "New note from unknown file";
                        var filecontent = File.ReadAllText(path);

                        SelectedNote       = Repository.CreateNewNote(notepath);
                        SelectedNote.Title = filename;
                        SelectedNote.Text  = filecontent;
                    }
                }
                else if (data.GetDataPresent(DataFormats.Text, true))
                {
                    var notetitle   = "New note from drag&drop";
                    var notecontent = data.GetData(DataFormats.Text, true) as string;
                    if (!string.IsNullOrWhiteSpace(notecontent))
                    {
                        SelectedNote       = Repository.CreateNewNote(notepath);
                        SelectedNote.Title = notetitle;
                        SelectedNote.Text  = notecontent;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionDialog.Show(Owner, "Drag&Drop failed", "Drag and Drop operation failed due to an internal error", ex);
            }
        }
Esempio n. 15
0
        private void CreateNewNoteFromTextfile()
        {
            var notepath = Owner.NotesViewControl.GetNewNotesPath();

            var ofd = new OpenFileDialog
            {
                Multiselect     = true,
                ShowReadOnly    = true,
                DefaultExt      = ".txt",
                Title           = "Import new notes from text files",
                CheckFileExists = true,
                Filter          = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            };

            if (ofd.ShowDialog() != true)
            {
                return;
            }

            try
            {
                foreach (var path in ofd.FileNames)
                {
                    var filename    = Path.GetFileNameWithoutExtension(path) ?? "New note from unknown file";
                    var filecontent = File.ReadAllText(path);

                    SelectedNote       = Repository.CreateNewNote(notepath);
                    SelectedNote.Title = filename;
                    SelectedNote.Text  = filecontent;
                }
            }
            catch (Exception ex)
            {
                ExceptionDialog.Show(Owner, "Reading file failed", "Creating note from file failed due to an error", ex);
            }
        }
Esempio n. 16
0
        private void SaveAndSync()
        {
            try
            {
                Repository.SaveAll();

                _scrollCache.SaveIfDirty();

                Owner.NotesViewControl.SaveIfDirty();

                if (_invSaveSettings.HasPendingRequests())
                {
                    _invSaveSettings.CancelPendingRequests();
                    SaveSettings();
                }

                Repository.SyncNow();
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Synchronization failed", e);
                ExceptionDialog.Show(Owner, "Synchronization failed", e, string.Empty);
            }
        }
        private void DeleteNote()
        {
            try
            {
                if (SelectedNote == null)
                {
                    return;
                }

                if (MessageBox.Show(Owner, "Do you really want to delete this note?", "Delete note?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }

                Repository.DeleteNote(SelectedNote, true);

                SelectedNote = Repository.Notes.FirstOrDefault();
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Could not delete note", e);
                ExceptionDialog.Show(Owner, "Could not delete note", e, string.Empty);
            }
        }
Esempio n. 18
0
        public void ChangeSettings(AppSettings newSettings)
        {
            try
            {
                var sw = Stopwatch.StartNew();

                // disable LocalSync when changing account
                if (!Settings.ActiveAccount.IsEqual(newSettings.ActiveAccount) && !newSettings.RawFolderRepoSubfolders)
                {
                    newSettings.UseRawFolderRepo = false;
                }

                var diffs = AppSettings.Diff(Settings, newSettings);

                var reconnectRepo            = diffs.Any(d => d.Attribute.ReconnectRepo) || (!Settings.ActiveAccount.IsEqual(newSettings.ActiveAccount));
                var refreshNotesViewTemplate = diffs.Any(d => d.Attribute.RefreshNotesViewTemplate);
                var refreshNotesCtrlView     = diffs.Any(d => d.Attribute.RefreshNotesControlView);
                var refreshNotesTheme        = diffs.Any(d => d.Attribute.RefreshNotesTheme);

                App.Logger.Debug("Main",
                                 $"Settings changed by user ({diffs.Count} diffs)",
                                 $"reconnectRepo: {reconnectRepo}\n" +
                                 $"refreshNotesViewTemplate: {refreshNotesViewTemplate}\n" +
                                 $"refreshNotesCtrlView: {refreshNotesCtrlView}\n" +
                                 $"refreshNotesTheme: {refreshNotesTheme}\n" +
                                 "\n\nDifferences:\n" +
                                 string.Join("\n", diffs.Select(d => " - " + d.PropInfo.Name)));

                if (reconnectRepo)
                {
                    try
                    {
                        _repository.Shutdown();
                    }
                    catch (Exception e)
                    {
                        App.Logger.Error("Main", "Shutting down current connection failed", e);
                        ExceptionDialog.Show(Owner, "Shutting down current connection failed.\r\nConnection will be forcefully aborted", e, string.Empty);
                        _repository.KillThread();
                    }
                }

                var sw2 = Stopwatch.StartNew();
                Settings = newSettings;
                Settings.Save();
                App.Logger.Trace("Main", $"Settings saved in {sw2.ElapsedMilliseconds}ms");

                if (refreshNotesTheme)
                {
                    ThemeManager.Inst.ChangeTheme(Settings.Theme, Settings.ThemeModifier);
                }

                if (reconnectRepo)
                {
                    _repository = new NoteRepository(AppSettings.PATH_LOCALDB, this, Settings, Settings.ActiveAccount, dispatcher);
                    _repository.Init();

                    OnExplicitPropertyChanged("Repository");

                    SelectedNote = Repository.Notes.FirstOrDefault();
                }
                else
                {
                    _repository.ReplaceSettings(Settings);
                }

                Owner.TrayIcon.Visibility = (Settings.CloseToTray || Settings.MinimizeToTray) ? Visibility.Visible : Visibility.Collapsed;

                if (Settings.LaunchOnBoot)
                {
                    var registryKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    registryKey?.SetValue(string.Format(AppSettings.APPNAME_REG, Settings.ClientID), AppSettings.PATH_EXECUTABLE);
                }
                else
                {
                    var registryKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    if (registryKey?.GetValue(string.Format(AppSettings.APPNAME_REG, Settings.ClientID)) != null)
                    {
                        registryKey.DeleteValue(string.Format(AppSettings.APPNAME_REG, Settings.ClientID));
                    }
                }

                // refresh Template
                if (refreshNotesViewTemplate)
                {
                    Owner.UpdateNotesViewComponent(Settings);
                }

                if (refreshNotesCtrlView)
                {
                    Owner.NotesViewControl.RefreshView();
                }

                Owner.SetupScintilla(Settings);
                Owner.UpdateShortcuts(Settings);

                SearchText = string.Empty;

                App.Logger.Trace("Main", $"ChangeSettings took {sw.ElapsedMilliseconds}ms");
            }
            catch (Exception e)
            {
                App.Logger.Error("Main", "Apply Settings failed", e);
                ExceptionDialog.Show(Owner, "Apply Settings failed.\r\nSettings and Local notes could be in an invalid state.\r\nContinue with caution.", e, string.Empty);
            }
        }
Esempio n. 19
0
        private void ExportNote()
        {
            if (SelectedNote == null)
            {
                return;
            }

            var selection = AllSelectedNotes.ToList();

            if (selection.Count > 1)
            {
                var dialog = new VistaFolderBrowserDialog();
                if (!(dialog.ShowDialog() ?? false))
                {
                    return;
                }

                try
                {
                    var directory = dialog.SelectedPath;
                    foreach (var note in selection)
                    {
                        var filenameRaw = ANFilenameHelper.StripStringForFilename(note.Title);
                        var filename    = filenameRaw;
                        var ext         = SelectedNote.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN) ? ".md" : ".txt";

                        int i = 1;
                        while (File.Exists(Path.Combine(directory, filename + ext)))
                        {
                            i++;
                            filename = $"{filenameRaw} ({i})";
                        }

                        File.WriteAllText(Path.Combine(directory, filename + ext), note.Text, Encoding.UTF8);
                    }
                }
                catch (Exception e)
                {
                    App.Logger.Error("Main", "Could not write to file", e);
                    ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty);
                }
            }
            else
            {
                var sfd = new SaveFileDialog();

                if (SelectedNote.HasTagCaseInsensitive(AppSettings.TAG_MARKDOWN))
                {
                    sfd.Filter   = "Markdown files (*.md)|*.md";
                    sfd.FileName = SelectedNote.Title + ".md";
                }
                else
                {
                    sfd.Filter   = "Text files (*.txt)|*.txt";
                    sfd.FileName = SelectedNote.Title + ".txt";
                }

                if (sfd.ShowDialog() != true)
                {
                    return;
                }
                try
                {
                    File.WriteAllText(sfd.FileName, SelectedNote.Text, Encoding.UTF8);
                }
                catch (Exception e)
                {
                    App.Logger.Error("Main", "Could not write to file", e);
                    ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty);
                }
            }
        }