public async static Task <EventStorage> DeserializeAsync() { EventStorage storage = new EventStorage(); StorageFile file = null; XmlDocument doc; try { file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.xml"); } catch { doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("root")); file = await ApplicationData.Current.LocalFolder.CreateFileAsync("data.xml"); await doc.SaveToFileAsync(file); return(storage); } doc = await XmlDocument.LoadFromFileAsync(file); var root = doc.FirstChild; List <IXmlNode> list = new List <IXmlNode>(); foreach (var node in root.ChildNodes) { Event evnt = new Event(node); if (evnt.IsPast && !evnt.Keep) { if (!evnt.IconPath.AbsoluteUri.StartsWith("ms-appx:")) { await(await StorageFile.GetFileFromApplicationUriAsync(evnt.IconPath)).DeleteAsync(); } if (evnt.MediaMessageType != MediaMessageType.None) { await(await StorageFile.GetFileFromApplicationUriAsync(evnt.MediaMessageUri)).DeleteAsync(); } if (evnt.HasStroke) { await(await(await ApplicationData.Current.LocalFolder.GetFolderAsync("Inks")).GetFileAsync(evnt.Guid.ToString() + ".gif")).DeleteAsync(); } list.Add(node); } else { storage.AddEvent(evnt); } } foreach (var node in list) { root.RemoveChild(node); } await doc.SaveToFileAsync(file); return(storage); }
public async void GoHome() { ApplicationData.Current.LocalSettings.Values["time"] = DateTimeOffset.Now.ToString(CultureInfo.InvariantCulture.DateTimeFormat); if (Storage == null) { Storage = await EventStorage.DeserializeAsync(); } if (State == null) { string state = ApplicationData.Current.LocalSettings.Values["state"] as string; if (state != null) { State = ContentState.Deserialize(state); } else { State = new ContentState(); State.MenuSelected = MenuSelection.Scheduled; State.ActivePage = PageActive.Years; } } Window.Current.Content = new MainPage(); Window.Current.Activate(); if (await BackgroundAccessQuery()) { if (!BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals("EventTrackerToastBackgroundTask"))) { BackgroundTaskBuilder builder = new BackgroundTaskBuilder() { Name = "EventTrackerToastBackgroundTask", TaskEntryPoint = "BackgroundTasks.NotificationBckgndTask" }; builder.SetTrigger(new ToastNotificationActionTrigger()); builder.Register(); } if (!BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals("EventTrackerSessionStartTask"))) { BackgroundTaskBuilder builder = new BackgroundTaskBuilder() { Name = "EventTrackerSessionStartTask", TaskEntryPoint = "BackgroundTasks.SessionConnected" }; builder.SetTrigger(new SystemTrigger(SystemTriggerType.SessionConnected, false)); builder.Register(); } } }
private async void SetLogFile(object sender, RoutedEventArgs e) { progress.Visibility = Visibility.Visible; App.DisableSelection = true; App.SgstBox.IsEnabled = false; FileOpenPicker open = new FileOpenPicker(); open.FileTypeFilter.Add(".elog"); StorageFile file = await open.PickSingleFileAsync(); if (file != null) { string text; if (add.IsChecked.Value) { text = App.Eng ? "Irrevocable operation. New records will be added to the current log." : "Необратимая операция. Новые записи будут добавлены в текущий журнал."; } else { text = App.Eng ? "Irrevocable operation. Current log will be erased." : "Необратимая операция. Текущий журнал будет удален."; } MessageDialog dialog = new MessageDialog(text); string proceed, cancel; if (App.Eng) { proceed = "Proceed"; cancel = "Cancel"; } else { proceed = "Продолжить"; cancel = "Отмена"; } bool consent = false; dialog.Commands.Add(new UICommand(proceed, x => consent = true)); dialog.Commands.Add(new UICommand(cancel)); await dialog.ShowAsync(); if (!consent) { App.DisableSelection = false; App.SgstBox.IsEnabled = true; progress.Visibility = Visibility.Collapsed; return; } file = await file.CopyAsync(ApplicationData.Current.TemporaryFolder, "events.elog", NameCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (DataReader reader = new DataReader(stream.GetInputStreamAt(0))) { await reader.LoadAsync((uint)code.Length); byte[] coded = new byte[code.Length]; for (int i = 0; i < coded.Length; i++) { coded[i] = (byte)(reader.ReadByte() ^ code[i]); } using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0))) writer.WriteBytes(coded); } } StorageFolder folder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync("temp", CreationCollisionOption.ReplaceExisting); try { await Task.Run(() => ZipFile.ExtractToDirectory(file.Path, folder.Path)); } catch { await(new MessageDialog("Wrong file format!")).ShowAsync(); await file.DeleteAsync(); await folder.DeleteAsync(); App.DisableSelection = false; App.SgstBox.IsEnabled = true; progress.Visibility = Visibility.Collapsed; return; } await file.DeleteAsync(); if (add.IsChecked.Value) { XmlDocument doc = await XmlDocument.LoadFromFileAsync(await folder.GetFileAsync("data.xml")); IXmlNode root = doc.FirstChild; StorageFolder Icons = await folder.TryGetItemAsync("Icons") as StorageFolder; StorageFolder Videos = await folder.TryGetItemAsync("Videos") as StorageFolder; StorageFolder Audios = await folder.TryGetItemAsync("Audios") as StorageFolder; StorageFolder Inks = await folder.TryGetItemAsync("Inks") as StorageFolder; StorageFolder locIcons = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Icons", CreationCollisionOption.OpenIfExists); StorageFolder locVideos = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Videos", CreationCollisionOption.OpenIfExists); StorageFolder locAudios = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Audios", CreationCollisionOption.OpenIfExists); StorageFolder locInks = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Inks", CreationCollisionOption.OpenIfExists); foreach (IXmlNode evntNode in root.ChildNodes) { Event evnt = new Event(evntNode); if (evnt.IsPast && !evnt.Keep) { continue; } if (App.Storage.Exist(evnt.Guid)) { continue; } if (evnt.IconPath.AbsoluteUri != "ms-appx:///Assets/item.png") { StorageFile iconFile = await Icons.GetFileAsync(Path.GetFileName(evnt.IconPath.AbsolutePath)); await iconFile.CopyAsync(locIcons); } switch (evnt.MediaMessageType) { case MediaMessageType.Video: StorageFile videoFile = await Videos.GetFileAsync(Path.GetFileName(evnt.MediaMessageUri.AbsolutePath)); await videoFile.CopyAsync(locVideos); break; case MediaMessageType.Voice: StorageFile audioFile = await Audios.GetFileAsync(Path.GetFileName(evnt.MediaMessageUri.AbsolutePath)); await audioFile.CopyAsync(locAudios); break; } if (evnt.HasStroke) { await(await Inks.GetFileAsync(evnt.Guid.ToString() + ".gif")).CopyAsync(locInks); } if (!evnt.IsPast && evnt.Alarm != null) { var xml = App.FormNotification(evnt.Name, evnt.Message, evnt.IconPath.AbsoluteUri, evnt.Guid.ToString()); ScheduledToastNotification notification = new ScheduledToastNotification(xml, evnt.Alarm.Value); _notifier.AddToSchedule(notification); } await evnt.SaveToFileAsync(); App.Storage.AddEvent(evnt); } } else { foreach (var item in await ApplicationData.Current.LocalFolder.GetItemsAsync()) { await item.DeleteAsync(); } var notifs = _notifier.GetScheduledToastNotifications(); foreach (var notif in notifs) { _notifier.RemoveFromSchedule(notif); } foreach (var item in await folder.GetItemsAsync()) { if (item.IsOfType(StorageItemTypes.File)) { await((StorageFile)item).MoveAsync(ApplicationData.Current.LocalFolder); } else if (item.IsOfType(StorageItemTypes.Folder)) { StorageFolder fld = (StorageFolder)item; StorageFolder locfld = await ApplicationData.Current.LocalFolder.CreateFolderAsync(fld.DisplayName); foreach (var itm in await fld.GetFilesAsync()) { await itm.MoveAsync(locfld); } } } App.Storage = await EventStorage.DeserializeAsync(); foreach (var year in App.Storage.CallendarActive) { foreach (var mnth in year.Months) { foreach (var evnt in mnth.Events) { if (evnt.Alarm != null) { var xml = App.FormNotification(evnt.Name, evnt.Message, evnt.IconPath.AbsoluteUri, evnt.Guid.ToString()); ScheduledToastNotification notification = new ScheduledToastNotification(xml, evnt.Alarm.Value); _notifier.AddToSchedule(notification); } } } } } await folder.DeleteAsync(); } App.DisableSelection = false; App.SgstBox.IsEnabled = true; progress.Visibility = Visibility.Collapsed; }