|
9 | 9 | using System.IO; |
10 | 10 | using System.Linq; |
11 | 11 | using System.Windows.Media; |
| 12 | +using SupernoteSharp.Business; |
| 13 | +using SupernoteSharp.Common; |
| 14 | +using SupernoteSharp.Entities; |
| 15 | +using CommunityToolkit.Mvvm.Messaging; |
| 16 | +using SupernoteDesktopClient.Messages; |
| 17 | +using System.Threading.Tasks; |
12 | 18 |
|
13 | 19 | namespace SupernoteDesktopClient.Models |
14 | 20 | { |
@@ -54,23 +60,60 @@ public FileSystemObjectInfo(FileSystemInfo info) |
54 | 60 | } |
55 | 61 |
|
56 | 62 | [RelayCommand] |
57 | | - private void OnOpenSelectedItem(object parameter) |
| 63 | + private async Task OnOpenSelectedItem(object parameter) |
| 64 | + { |
| 65 | + await Task.Run(() => ConvertNoteDocument(parameter)); |
| 66 | + } |
| 67 | + |
| 68 | + private void ConvertNoteDocument(object parameter) |
58 | 69 | { |
59 | 70 | FileSystemObjectInfo item = parameter as FileSystemObjectInfo; |
60 | 71 | if ((item.FileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) |
61 | 72 | return; |
62 | 73 |
|
63 | | - ProcessStartInfo psi = new ProcessStartInfo(); |
64 | | - psi.FileName = item.FileSystemInfo.FullName; |
65 | | - psi.UseShellExecute = true; |
| 74 | + // skip *.mark files until the application can support them |
| 75 | + if (item.FileSystemInfo.Extension == ".mark") |
| 76 | + return; |
| 77 | + |
66 | 78 | try |
67 | 79 | { |
| 80 | + string selectedItemFullName = item.FileSystemInfo.FullName; |
| 81 | + |
| 82 | + if (item.FileSystemInfo.Extension == ".note" && item.FileSystemInfo.Exists == true) |
| 83 | + { |
| 84 | + WeakReferenceMessenger.Default.Send(new ProgressTrackActionMessage(true)); // action started |
| 85 | + |
| 86 | + selectedItemFullName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(item.FileSystemInfo.Name) + "_sdc.pdf"); |
| 87 | + |
| 88 | + using (FileStream fileStream = new FileStream(item.FileSystemInfo.FullName, FileMode.Open, FileAccess.Read)) |
| 89 | + { |
| 90 | + Parser parser = new Parser(); |
| 91 | + Notebook notebook = parser.LoadNotebook(fileStream, Policy.Strict); |
| 92 | + Converter.PdfConverter converter = new Converter.PdfConverter(notebook, DefaultColorPalette.Grayscale); |
| 93 | + |
| 94 | + // convert all pages to vector PDF and build all links |
| 95 | + byte[] allPages = converter.ConvertAll(vectorize: true, enableLinks: true); |
| 96 | + // save the result |
| 97 | + File.WriteAllBytes(selectedItemFullName, allPages); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + ProcessStartInfo psi = new ProcessStartInfo |
| 102 | + { |
| 103 | + FileName = selectedItemFullName, |
| 104 | + UseShellExecute = true |
| 105 | + }; |
| 106 | + |
68 | 107 | Process process = Process.Start(psi); |
69 | 108 | } |
70 | 109 | catch (Exception) |
71 | 110 | { |
72 | 111 | // TODO: Error handling |
73 | 112 | } |
| 113 | + finally |
| 114 | + { |
| 115 | + WeakReferenceMessenger.Default.Send(new ProgressTrackActionMessage(false)); // action completed |
| 116 | + } |
74 | 117 | } |
75 | 118 |
|
76 | 119 | private void FileSystemObjectInfo_PropertyChanged(object sender, PropertyChangedEventArgs e) |
|
0 commit comments