Skip to content

Commit

Permalink
refactoring work necessary for fixing jimmejardine#96 & jimmejardine#101
Browse files Browse the repository at this point in the history
  • Loading branch information
GerHobbelt committed Oct 9, 2019
1 parent 0db1848 commit deaadc3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 50 deletions.
18 changes: 4 additions & 14 deletions Qiqqa/Documents/PDF/DiskSerialisation/PDFAnnotationSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,14 @@ class PDFAnnotationSerializer
{
internal static void WriteToDisk(PDFDocument pdf_document)
{
// A little hack to make sure the legacies are updated...
foreach (PDFAnnotation annotation in pdf_document.Annotations)
string json = pdf_document.GetAnnotationsAsJSON();
if (!String.IsNullOrEmpty(json))
{
annotation.Color = annotation.Color;
annotation.DateCreated = annotation.DateCreated;
annotation.FollowUpDate = annotation.FollowUpDate;
pdf_document.Library.LibraryDB.PutString(pdf_document.Fingerprint, PDFDocumentFileLocations.ANNOTATIONS, json);
}

List<Dictionary<string, object>> attributes_list = new List<Dictionary<string, object>>();
foreach (PDFAnnotation annotation in pdf_document.Annotations)
{
attributes_list.Add(annotation.Dictionary.Attributes);
}
string json = JsonConvert.SerializeObject(attributes_list, Formatting.Indented);
pdf_document.Library.LibraryDB.PutString(pdf_document.Fingerprint, PDFDocumentFileLocations.ANNOTATIONS, json);
}

internal static void ReadFromDisk(PDFDocument pdf_document, PDFAnnotationList annotations, Dictionary<string, byte[]> library_items_annotations_cache)
internal static void ReadFromDisk(PDFDocument pdf_document, ref PDFAnnotationList annotations, Dictionary<string, byte[]> library_items_annotations_cache)
{
byte[] annotations_data = null;

Expand Down
11 changes: 3 additions & 8 deletions Qiqqa/Documents/PDF/DiskSerialisation/PDFHighlightSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ private static List<PDFHighlight> ReadFromStream_PROTOBUF(byte[] data)

internal static void WriteToDisk(PDFDocument pdf_document)
{
List<PDFHighlight> highlights_list = new List<PDFHighlight>();
foreach (PDFHighlight highlight in pdf_document.Highlights.GetAllHighlights())
string json = pdf_document.GetHighlightsAsJSON();
if (!String.IsNullOrEmpty(json))
{
highlights_list.Add(highlight);
pdf_document.Library.LibraryDB.PutString(pdf_document.Fingerprint, PDFDocumentFileLocations.HIGHLIGHTS, json);
}

string json = JsonConvert.SerializeObject(highlights_list, Formatting.Indented);
pdf_document.Library.LibraryDB.PutString(pdf_document.Fingerprint, PDFDocumentFileLocations.HIGHLIGHTS, json);

Logging.Info("Wrote {0} highlights to disk", highlights_list.Count);
}

#endregion --------------------------------------------------------------------------------------------------------------------------------------------
Expand Down
10 changes: 2 additions & 8 deletions Qiqqa/Documents/PDF/DiskSerialisation/PDFInkSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,10 @@ internal static void ReadFromDisk(PDFDocument pdf_document, PDFInkList inks, Dic

internal static void WriteToDisk(PDFDocument pdf_document)
{
Dictionary<int, byte[]> page_ink_blobs = new Dictionary<int, byte[]>();
foreach (var pair in pdf_document.Inks.PageInkBlobs)
{
page_ink_blobs.Add(pair.Key, pair.Value);
}
byte[] data = pdf_document.GetInksAsJSON();

// We only write to disk if we have at least one page of blobbies to write...
if (page_ink_blobs.Count > 0)
if (null != data)
{
byte[] data = SerializeFile.ProtoSaveToByteArray<Dictionary<int, byte[]>>(page_ink_blobs);
pdf_document.Library.LibraryDB.PutBlob(pdf_document.Fingerprint, PDFDocumentFileLocations.INKS, data);
}
}
Expand Down
20 changes: 15 additions & 5 deletions Qiqqa/Documents/PDF/DiskSerialisation/PDFMetadataSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static void WriteToDisk(PDFDocument pdf_document)
pdf_document.DateLastRead = pdf_document.DateLastRead;
// A little hack to make sure the legacies are updated...

string json = JsonConvert.SerializeObject(pdf_document.Dictionary.Attributes, Formatting.Indented);
string json = pdf_document.GetAttributesAsJSON();
pdf_document.Library.LibraryDB.PutString(pdf_document.Fingerprint, PDFDocumentFileLocations.METADATA, json);
Logging.Debug("Update metadata DB for PDF document {1}: JSON =\n{0}", json, pdf_document.Fingerprint);
}
Expand All @@ -31,8 +31,8 @@ public static DictionaryBasedObject ReadFromStream(byte[] data)
try
{
string json = Encoding.UTF8.GetString(data);
char a = json[0];
if (0 != a)
char a = (json.Length > 0 ? json[0] : (char)0);
if ((char)0 != a)
{
Dictionary<string, object> attributes = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
return new DictionaryBasedObject(attributes);
Expand All @@ -42,9 +42,19 @@ public static DictionaryBasedObject ReadFromStream(byte[] data)
return ReadFromStream_BINARY(data);
}
}
catch (Exception)
catch (Exception ex)
{
return ReadFromStream_BINARY(data);
Logging.Error(ex, "Failed to ReadFromStream, second attempt is to read using old binary format");

try
{
return ReadFromStream_BINARY(data);
}
catch (Exception ex2)
{
Logging.Error(ex2, "Failed second attempt, reading using old binary format");
throw ex;
}
}
}

Expand Down
85 changes: 70 additions & 15 deletions Qiqqa/Documents/PDF/PDFDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Reflection;
using System.Windows.Media;
using Newtonsoft.Json;
using Qiqqa.Common.TagManagement;
using Qiqqa.DocumentLibrary;
using Qiqqa.Documents.Common;
Expand Down Expand Up @@ -48,9 +49,10 @@ public Library Library

private DictionaryBasedObject dictionary = new DictionaryBasedObject();

internal DictionaryBasedObject Dictionary
public string GetAttributesAsJSON()
{
get { return dictionary; }
string json = JsonConvert.SerializeObject(dictionary.Attributes, Formatting.Indented);
return json;
}

static readonly PropertyDependencies property_dependencies = new PropertyDependencies();
Expand Down Expand Up @@ -857,13 +859,37 @@ public PDFAnnotationList GetAnnotations(Dictionary<string, byte[]> library_items
if (null == annotations)
{
annotations = new PDFAnnotationList();
PDFAnnotationSerializer.ReadFromDisk(this, annotations, library_items_annotations_cache);
PDFAnnotationSerializer.ReadFromDisk(this, ref annotations, library_items_annotations_cache);
annotations.OnPDFAnnotationListChanged += annotations_OnPDFAnnotationListChanged;
}

return annotations;
}

public string GetAnnotationsAsJSON()
{
string json = String.Empty;

if (null != annotations && annotations.Count > 0)
{
// A little hack to make sure the legacies are updated...
foreach (PDFAnnotation annotation in annotations)
{
annotation.Color = annotation.Color;
annotation.DateCreated = annotation.DateCreated;
annotation.FollowUpDate = annotation.FollowUpDate;
}

List<Dictionary<string, object>> attributes_list = new List<Dictionary<string, object>>();
foreach (PDFAnnotation annotation in annotations)
{
attributes_list.Add(annotation.Dictionary.Attributes);
}
json = JsonConvert.SerializeObject(attributes_list, Formatting.Indented);
}
return json;
}

public void QueueToStorage()
{
// create a clone to cross the thread boundary:
Expand Down Expand Up @@ -915,6 +941,24 @@ internal PDFHightlightList GetHighlights(Dictionary<string, byte[]> library_item
return highlights;
}

public string GetHighlightsAsJSON()
{
string json = String.Empty;

if (null != highlights && highlights.Count > 0)
{
List<PDFHighlight> highlights_list = new List<PDFHighlight>();
foreach (PDFHighlight highlight in highlights.GetAllHighlights())
{
highlights_list.Add(highlight);
}

json = JsonConvert.SerializeObject(highlights_list, Formatting.Indented);

Logging.Info("Wrote {0} highlights to JSON", highlights_list.Count);
}
return json;
}

void highlights_OnPDFHighlightListChanged()
{
Expand Down Expand Up @@ -944,6 +988,26 @@ internal PDFInkList GetInks(Dictionary<string, byte[]> library_items_inks_cache)
return inks;
}

public byte[] GetInksAsJSON()
{
byte[] data = null;

if (null != inks)
{
Dictionary<int, byte[]> page_ink_blobs = new Dictionary<int, byte[]>();
foreach (var pair in inks.PageInkBlobs)
{
page_ink_blobs.Add(pair.Key, pair.Value);
}

// We only write to disk if we have at least one page of blobbies to write...
if (page_ink_blobs.Count > 0)
{
data = SerializeFile.ProtoSaveToByteArray<Dictionary<int, byte[]>>(page_ink_blobs);
}
}
return data;
}

void inks_OnPDFInkListChanged()
{
Expand Down Expand Up @@ -979,22 +1043,13 @@ public void SaveToMetaData()
PDFMetadataSerializer.WriteToDisk(this);

// Save the annotations
if (null != annotations && annotations.Count > 0)
{
PDFAnnotationSerializer.WriteToDisk(this);
}
PDFAnnotationSerializer.WriteToDisk(this);

// Save the highlights
if (null != highlights && highlights.Count > 0)
{
PDFHighlightSerializer.WriteToDisk(this);
}
PDFHighlightSerializer.WriteToDisk(this);

// Save the inks
if (null != inks)
{
PDFInkSerializer.WriteToDisk(this);
}
PDFInkSerializer.WriteToDisk(this);
}

void bindable_PropertyChanged(object sender, PropertyChangedEventArgs e)
Expand Down

0 comments on commit deaadc3

Please sign in to comment.