public static NoteInfo ParseJson (Hyena.Json.JsonObject jsonObj) { if (jsonObj == null) throw new ArgumentException ("jsonObj does not contain a valid NoteInfo representation"); // TODO: Checks NoteInfo note = new NoteInfo (); note.Guid = (string) jsonObj ["guid"]; // TODO: Decide how much is required object val; if (jsonObj.TryGetValue (TitleElementName, out val)) note.Title = (string) val; if (jsonObj.TryGetValue (NoteContentElementName, out val)) note.NoteContent = (string) val; if (jsonObj.TryGetValue (NoteContentVersionElementName, out val)) note.NoteContentVersion = (double) val; if (jsonObj.TryGetValue (LastChangeDateElementName, out val)) note.LastChangeDate = DateTime.Parse ((string) val); if (jsonObj.TryGetValue (LastMetadataChangeDateElementName, out val)) note.LastMetadataChangeDate = DateTime.Parse ((string) val); if (jsonObj.TryGetValue (CreateDateElementName, out val)) note.CreateDate = DateTime.Parse ((string) val); if (jsonObj.TryGetValue (LastSyncRevisionElementName, out val)) note.LastSyncRevision = (int) val; if (jsonObj.TryGetValue (OpenOnStartupElementName, out val)) note.OpenOnStartup = (bool) val; if (jsonObj.TryGetValue (PinnedElementName, out val)) note.Pinned = (bool) val; if (jsonObj.TryGetValue (TagsElementName, out val)) { Hyena.Json.JsonArray tagsJsonArray = (Hyena.Json.JsonArray) val; note.Tags = new List<string> (tagsJsonArray.Count); foreach (string tag in tagsJsonArray) note.Tags.Add (tag); } if (jsonObj.TryGetValue (ResourceReferenceElementName, out val)) note.ResourceReference = ResourceReference.ParseJson ((Hyena.Json.JsonObject) val); return note; }
public void DeleteNotes (IList<string> deletedNoteUUIDs) { foreach (string uuid in deletedNoteUUIDs) { NoteInfo noteInfo = new NoteInfo (); noteInfo.Command = "delete"; noteInfo.Guid = uuid; pendingCommits.Add (noteInfo); } }
public static NoteInfo ToNoteInfo (Note note) { NoteInfo noteInfo = new NoteInfo (); noteInfo.Guid = note.Id; noteInfo.Title = note.Title .Replace ("&", "&") .Replace ("<", "<") .Replace (">", ">") .Replace ("\"", """) .Replace ("\'", "'"); noteInfo.OpenOnStartup = note.IsOpenOnStartup; noteInfo.Pinned = note.IsPinned; noteInfo.CreateDate = note.CreateDate; noteInfo.LastChangeDate = note.ChangeDate; noteInfo.LastMetadataChangeDate = note.MetadataChangeDate; noteInfo.Tags = new List<string> (); foreach (Tag tag in note.Tags) noteInfo.Tags.Add (tag.Name); const string noteContentRegex = @"^<note-content([^>]+version=""(?<contentVersion>[^""]*)"")?[^>]*((/>)|(>(?<innerContent>.*)</note-content>))$"; Match m = Regex.Match (note.XmlContent, noteContentRegex, RegexOptions.Singleline); Group versionGroup = m.Groups ["contentVersion"]; Group contentGroup = m.Groups ["innerContent"]; double contentVersion; if (versionGroup.Success && double.TryParse (versionGroup.Value, out contentVersion)) { noteInfo.NoteContentVersion = contentVersion; } else noteInfo.NoteContentVersion = 0.1; // TODO: Constants, transformations, etc, if this changes if (contentGroup.Success) { string [] splits = contentGroup.Value.Split (new char [] {'\n'}, 2); if (splits.Length > 1 && splits [1].Length > 0) { StringBuilder builder = new StringBuilder (contentGroup.Value.Length); bool inTag = false; // Strip everything out of first line, except for XML tags // TODO: Handle 'note-title' element differently? // Ideally we would want to get rid of it completely. foreach (char c in splits [0]) { if (!inTag && c == '<') inTag = true; if (inTag) { builder.Append (c); if (c == '>') inTag = false; } } // Trim leading newline, if there is one if (splits [1][0] == '\n') builder.Append (splits [1], 1, splits [1].Length - 1); else builder.Append (splits [1]); noteInfo.NoteContent = builder.ToString (); } } if (noteInfo.NoteContent == null) noteInfo.NoteContent = string.Empty; return noteInfo; }
public static string ToNoteXml (NoteInfo noteInfo) { NoteData noteData = ToNoteData (noteInfo); return NoteArchiver.WriteString (noteData); }
public static NoteData ToNoteData (NoteInfo noteInfo) { // NOTE: For now, we absolutely require values for // Guid, Title, NoteContent, and NoteContentVersion // TODO: Is this true? What happens if dates are excluded? NoteData noteData = new NoteData (NoteUriFromGuid (noteInfo.Guid)); noteData.Title = noteInfo.Title .Replace ("&", "&") .Replace ("<", "<") .Replace (">", ">") .Replace (""", "\"") .Replace ("'", "\'"); noteData.Text = string.Format ("<note-content version=\"{0}\">{1}\n\n{2}</note-content>", noteInfo.NoteContentVersion, noteInfo.Title, noteInfo.NoteContent); if (noteInfo.LastChangeDate.HasValue) noteData.ChangeDate = noteInfo.LastChangeDate.Value; if (noteInfo.LastMetadataChangeDate.HasValue) noteData.MetadataChangeDate = noteInfo.LastMetadataChangeDate.Value; if (noteInfo.CreateDate.HasValue) noteData.CreateDate = noteInfo.CreateDate.Value; if (noteInfo.OpenOnStartup.HasValue) noteData.IsOpenOnStartup = noteInfo.OpenOnStartup.Value; // TODO: support Pinned -- http://bugzilla.gnome.org/show_bug.cgi?id=433412 if (noteInfo.Tags != null) { foreach (string tagName in noteInfo.Tags) { Tag tag = TagManager.GetOrCreateTag (tagName); noteData.Tags [tag.NormalizedName] = tag; } } return noteData; }
public static NoteInfo ParseJson(Hyena.Json.JsonObject jsonObj) { if (jsonObj == null) { throw new ArgumentException("jsonObj does not contain a valid NoteInfo representation"); } // TODO: Checks NoteInfo note = new NoteInfo(); note.Guid = (string)jsonObj ["guid"]; // TODO: Decide how much is required object val = 0; string key = "<unknown>"; try { key = TitleElementName; if (jsonObj.TryGetValue(key, out val)) { note.Title = (string)val; } key = NoteContentElementName; if (jsonObj.TryGetValue(key, out val)) { note.NoteContent = (string)val; } key = NoteContentVersionElementName; if (jsonObj.TryGetValue(key, out val)) { note.NoteContentVersion = (double)val; } key = LastChangeDateElementName; if (jsonObj.TryGetValue(key, out val)) { note.LastChangeDate = DateTime.Parse((string)val); } key = LastMetadataChangeDateElementName; if (jsonObj.TryGetValue(key, out val)) { note.LastMetadataChangeDate = DateTime.Parse((string)val); } key = CreateDateElementName; if (jsonObj.TryGetValue(key, out val)) { note.CreateDate = DateTime.Parse((string)val); } key = LastSyncRevisionElementName; if (jsonObj.TryGetValue(key, out val)) { note.LastSyncRevision = (int)val; } key = OpenOnStartupElementName; if (jsonObj.TryGetValue(key, out val)) { note.OpenOnStartup = (bool)val; } key = PinnedElementName; if (jsonObj.TryGetValue(key, out val)) { note.Pinned = (bool)val; } key = TagsElementName; if (jsonObj.TryGetValue(key, out val)) { Hyena.Json.JsonArray tagsJsonArray = (Hyena.Json.JsonArray)val; note.Tags = new List <string> (tagsJsonArray.Count); foreach (string tag in tagsJsonArray) { note.Tags.Add(tag); } } key = ResourceReferenceElementName; if (jsonObj.TryGetValue(key, out val)) { note.ResourceReference = ResourceReference.ParseJson((Hyena.Json.JsonObject)val); } } catch (InvalidCastException e) { Logger.Error("Note '{0}': Key '{1}', value '{2}' failed to parse due to invalid type", note.Guid, key, val); throw e; } return(note); }