Implements attachments display in transcript
This commit is contained in:
74
src/models/attachment.vala
Normal file
74
src/models/attachment.vala
Normal file
@@ -0,0 +1,74 @@
|
||||
public class AttributionInfo : Object {
|
||||
// Picture width
|
||||
public int64 width;
|
||||
|
||||
// Picture height
|
||||
public int64 height;
|
||||
|
||||
public static AttributionInfo from_variant(Variant variant) {
|
||||
var attribution_info = new AttributionInfo();
|
||||
|
||||
VariantDict dict = new VariantDict(variant);
|
||||
attribution_info.width = dict.lookup_value("width", VariantType.INT32)?.get_int32() ?? 0;
|
||||
attribution_info.height = dict.lookup_value("height", VariantType.INT32)?.get_int32() ?? 0;
|
||||
|
||||
return attribution_info;
|
||||
}
|
||||
}
|
||||
|
||||
public class AttachmentMetadata : Object {
|
||||
public AttributionInfo? attribution_info = null;
|
||||
|
||||
public static AttachmentMetadata from_variant(Variant variant) {
|
||||
var metadata = new AttachmentMetadata();
|
||||
|
||||
VariantDict dict = new VariantDict(variant);
|
||||
metadata.attribution_info = AttributionInfo.from_variant(dict.lookup_value("attribution_info", VariantType.DICTIONARY));
|
||||
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
|
||||
public class Attachment : Object {
|
||||
public string guid;
|
||||
public string path;
|
||||
public string preview_path;
|
||||
public bool downloaded;
|
||||
public bool preview_downloaded;
|
||||
public AttachmentMetadata? metadata;
|
||||
|
||||
public Attachment(string guid, AttachmentMetadata? metadata) {
|
||||
this.guid = guid;
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public Attachment.from_variant(Variant variant) {
|
||||
VariantIter iter;
|
||||
variant.get("a{sv}", out iter);
|
||||
|
||||
string key;
|
||||
Variant val;
|
||||
while (iter.next("{sv}", out key, out val)) {
|
||||
switch (key) {
|
||||
case "guid":
|
||||
this.guid = val.get_string();
|
||||
break;
|
||||
case "path":
|
||||
this.path = val.get_string();
|
||||
break;
|
||||
case "preview_path":
|
||||
this.preview_path = val.get_string();
|
||||
break;
|
||||
case "downloaded":
|
||||
this.downloaded = val.get_boolean();
|
||||
break;
|
||||
case "preview_downloaded":
|
||||
this.preview_downloaded = val.get_boolean();
|
||||
break;
|
||||
case "metadata":
|
||||
this.metadata = AttachmentMetadata.from_variant(val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ public class Message : Object
|
||||
public string text { get; set; default = ""; }
|
||||
public DateTime date { get; set; default = new DateTime.now_local(); }
|
||||
public string sender { get; set; default = null; }
|
||||
|
||||
public Attachment[] attachments { get; set; default = {}; }
|
||||
|
||||
public bool from_me {
|
||||
get {
|
||||
@@ -25,5 +27,18 @@ public class Message : Object
|
||||
text = message_data["text"].get_string();
|
||||
sender = message_data["sender"].get_string();
|
||||
date = new DateTime.from_unix_utc(message_data["date"].get_int64());
|
||||
|
||||
// Attachments
|
||||
var attachments_variant = message_data["attachments"];
|
||||
var attachments = new Gee.ArrayList<Attachment>();
|
||||
if (attachments_variant != null) {
|
||||
for (int i = 0; i < attachments_variant.n_children(); i++) {
|
||||
var attachment_variant = attachments_variant.get_child_value(i);
|
||||
var attachment = new Attachment.from_variant(attachment_variant);
|
||||
attachments.add(attachment);
|
||||
}
|
||||
}
|
||||
|
||||
this.attachments = attachments.to_array();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user