Only display transferred images, display file names, open in system viewer, include and use pgp embedded file names

This commit is contained in:
fiaxh 2018-01-28 20:56:27 +01:00
parent bec810e234
commit f6db249c92
13 changed files with 52 additions and 75 deletions

View file

@ -98,8 +98,8 @@ public class FileTransfer : Object {
notify.connect(on_update); notify.connect(on_update);
} }
public string get_uri() { public File get_file() {
return Path.build_filename(Dino.get_storage_dir(), "files", path); return File.new_for_path(Path.build_filename(Dino.get_storage_dir(), "files", path));
} }
private void on_update(Object o, ParamSpec sp) { private void on_update(Object o, ParamSpec sp) {
@ -118,6 +118,8 @@ public class FileTransfer : Object {
update_builder.set(db.file_transfer.local_time, (long) local_time.to_unix()); break; update_builder.set(db.file_transfer.local_time, (long) local_time.to_unix()); break;
case "encryption": case "encryption":
update_builder.set(db.file_transfer.encryption, encryption); break; update_builder.set(db.file_transfer.encryption, encryption); break;
case "file-name":
update_builder.set(db.file_transfer.file_name, file_name); break;
case "state": case "state":
update_builder.set(db.file_transfer.state, state); break; update_builder.set(db.file_transfer.state, state); break;
case "provider": case "provider":

View file

@ -126,8 +126,7 @@ public class FileManager : StreamInteractionModule, Object {
save_file(file_transfer); save_file(file_transfer);
try { try {
File file = File.new_for_path(file_transfer.get_uri()); FileInfo file_info = file_transfer.get_file().query_info("*", FileQueryInfoFlags.NONE);
FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE);
file_transfer.mime_type = file_info.get_content_type(); file_transfer.mime_type = file_info.get_content_type();
} catch (Error e) { } } catch (Error e) { }

View file

@ -37,11 +37,16 @@ public class MessageProcessor : StreamInteractionModule, Object {
received_pipeline.connect(new MamMessageListener(stream_interactor)); received_pipeline.connect(new MamMessageListener(stream_interactor));
} }
public void send_message(string text, Conversation conversation) { public Entities.Message send_text(string text, Conversation conversation) {
Entities.Message message = create_out_message(text, conversation); Entities.Message message = create_out_message(text, conversation);
return send_message(message, conversation);
}
public Entities.Message send_message(Entities.Message message, Conversation conversation) {
stream_interactor.get_module(MessageStorage.IDENTITY).add_message(message, conversation); stream_interactor.get_module(MessageStorage.IDENTITY).add_message(message, conversation);
send_xmpp_message(message, conversation); send_xmpp_message(message, conversation);
message_sent(message, conversation); message_sent(message, conversation);
return message;
} }
public void send_unsent_messages(Account account, Jid? jid = null) { public void send_unsent_messages(Account account, Jid? jid = null) {
@ -224,7 +229,7 @@ public class MessageProcessor : StreamInteractionModule, Object {
} }
} }
private Entities.Message create_out_message(string text, Conversation conversation) { public Entities.Message create_out_message(string text, Conversation conversation) {
Entities.Message message = new Entities.Message(text); Entities.Message message = new Entities.Message(text);
message.type_ = Util.get_message_type_for_conversation(conversation); message.type_ = Util.get_message_type_for_conversation(conversation);
message.stanza_id = random_uuid(); message.stanza_id = random_uuid();

View file

@ -2,49 +2,26 @@
<interface> <interface>
<object class="GtkBox" id="main"> <object class="GtkBox" id="main">
<property name="valign">end</property> <property name="valign">end</property>
<property name="halign">end</property>
<property name="margin">10</property> <property name="margin">10</property>
<property name="visible">True</property> <property name="visible">True</property>
<child> <child>
<object class="GtkToolItem"> <object class="GtkToolItem">
<property name="visible">True</property> <property name="visible">True</property>
<child> <child>
<object class="GtkBox" id="url_box"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
<child> <child>
<object class="GtkLabel" id="url_label"> <object class="GtkLabel" id="url_label">
<property name="max_width_chars">10</property> <property name="max_width_chars">25</property>
<property name="ellipsize">middle</property> <property name="ellipsize">middle</property>
<property name="hexpand">True</property> <property name="hexpand">True</property>
<property name="margin-left">5</property> <property name="margin-left">5</property>
<property name="visible">True</property> <property name="visible">True</property>
</object> </object>
</child> </child>
<child>
<object class="GtkButton" id="copy_button">
<property name="visible">True</property>
<property name="relief">none</property>
<child>
<object class="GtkImage" id="copy_image">
<property name="icon-name">edit-copy-symbolic</property>
<property name="icon-size">1</property>
<property name="visible">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<child>
<object class="GtkToolItem">
<property name="visible">True</property>
<child> <child>
<object class="GtkButton" id="open_button"> <object class="GtkButton" id="open_button">
<property name="margin-left">5</property>
<property name="relief">none</property> <property name="relief">none</property>
<property name="visible">True</property> <property name="visible">True</property>
<child> <child>

View file

@ -105,7 +105,7 @@ public class View : Box {
break; break;
} }
} }
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(text, conversation); stream_interactor.get_module(MessageProcessor.IDENTITY).send_text(text, conversation);
} }
private bool on_text_input_key_press(EventKey event) { private bool on_text_input_key_press(EventKey event) {

View file

@ -66,9 +66,9 @@ public class DefaultFileDisplay : Plugins.MetaConversationItem {
event_box.button_release_event.connect((event_button) => { event_box.button_release_event.connect((event_button) => {
if (event_button.button == 1) { if (event_button.button == 1) {
try{ try{
AppInfo.launch_default_for_uri("file://" + file_transfer.get_uri(), null); AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null);
} catch (Error err) { } catch (Error err) {
print("Tried to open " + file_transfer.get_uri()); print("Tried to open " + file_transfer.get_file().get_path());
} }
} }
return false; return false;

View file

@ -41,7 +41,7 @@ public class ImageDisplay : Plugins.MetaConversationItem {
Image image = new Image() { halign=Align.START, visible = true }; Image image = new Image() { halign=Align.START, visible = true };
Gdk.Pixbuf pixbuf; Gdk.Pixbuf pixbuf;
try { try {
pixbuf = new Gdk.Pixbuf.from_file(file_transfer.get_uri()); pixbuf = new Gdk.Pixbuf.from_file(file_transfer.get_file().get_path());
} catch (Error error) { } catch (Error error) {
return null; return null;
} }
@ -65,16 +65,7 @@ public class ImageDisplay : Plugins.MetaConversationItem {
Label url_label = builder.get_object("url_label") as Label; Label url_label = builder.get_object("url_label") as Label;
Util.force_color(url_label, "#eee"); Util.force_color(url_label, "#eee");
file_transfer.notify["info"].connect_after(() => { update_info(url_label, file_transfer.info); }); update_info(url_label, file_transfer.file_name);
update_info(url_label, file_transfer.info);
Image copy_image = builder.get_object("copy_image") as Image;
Util.force_css(copy_image, "*:not(:hover) { color: #eee; }");
Button copy_button = builder.get_object("copy_button") as Button;
Util.force_css(copy_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }");
copy_button.clicked.connect(() => {
if (file_transfer.info != null) Clipboard.get_default(Gdk.Display.get_default()).set_text(file_transfer.info, file_transfer.info.length);
});
Image open_image = builder.get_object("open_image") as Image; Image open_image = builder.get_object("open_image") as Image;
Util.force_css(open_image, "*:not(:hover) { color: #eee; }"); Util.force_css(open_image, "*:not(:hover) { color: #eee; }");
@ -82,9 +73,9 @@ public class ImageDisplay : Plugins.MetaConversationItem {
Util.force_css(open_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); Util.force_css(open_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }");
open_button.clicked.connect(() => { open_button.clicked.connect(() => {
try{ try{
AppInfo.launch_default_for_uri(file_transfer.info, null); AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null);
} catch (Error err) { } catch (Error err) {
print("Tried to open " + file_transfer.info); print("Tried to open file://" + file_transfer.get_file().get_path() + " " + err.message + "\n");
} }
}); });

View file

@ -19,11 +19,12 @@ public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags)
} }
} }
public static uint8[] encrypt_file(string uri, Key[] keys, EncryptFlags flags) throws GLib.Error { public static uint8[] encrypt_file(string uri, Key[] keys, EncryptFlags flags, string file_name) throws GLib.Error {
global_mutex.lock(); global_mutex.lock();
try { try {
initialize(); initialize();
Data plain_data = Data.create_from_file(uri); Data plain_data = Data.create_from_file(uri);
plain_data.set_file_name(file_name);
Context context = Context.create(); Context context = Context.create();
context.set_armor(true); context.set_armor(true);
Data enc_data = context.op_encrypt(keys, flags, plain_data); Data enc_data = context.op_encrypt(keys, flags, plain_data);
@ -46,14 +47,20 @@ public static string decrypt(string encr) throws GLib.Error {
} }
} }
public static uint8[] decrypt_data(uint8[] data) throws GLib.Error { public class DecryptedData {
public uint8[] data { get; set; }
public string filename { get; set; }
}
public static DecryptedData decrypt_data(uint8[] data) throws GLib.Error {
global_mutex.lock(); global_mutex.lock();
try { try {
initialize(); initialize();
Data enc_data = Data.create_from_memory(data, false); Data enc_data = Data.create_from_memory(data, false);
Context context = Context.create(); Context context = Context.create();
Data dec_data = context.op_decrypt(enc_data); Data dec_data = context.op_decrypt(enc_data);
return get_uint8_from_data(dec_data); DecryptResult* dec_res = context.op_decrypt_result();
return new DecryptedData() { data=get_uint8_from_data(dec_data), filename=dec_res->file_name};
} finally { } finally {
global_mutex.unlock(); global_mutex.unlock();
} }

View file

@ -59,7 +59,7 @@ namespace GPG {
string unsupported_algorithm; string unsupported_algorithm;
bool wrong_key_usage; bool wrong_key_usage;
Recipient recipients; Recipient recipients;
string filename; string file_name;
} }
[CCode (cname = "struct _gpgme_recipient")] [CCode (cname = "struct _gpgme_recipient")]
@ -481,7 +481,9 @@ namespace GPG {
public long seek(long offset, int whence=0); public long seek(long offset, int whence=0);
public DataEncoding *get_encoding(); public GPGError.Error set_file_name(string file_name);
public DataEncoding* get_encoding();
public GPGError.Error set_encoding(DataEncoding enc); public GPGError.Error set_encoding(DataEncoding enc);
} }

View file

@ -20,10 +20,8 @@ public class FileProvider : Dino.FileProvider, Object {
this.url_regex = new Regex("""^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»]))$"""); this.url_regex = new Regex("""^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»]))$""");
this.file_ext_regex = new Regex("""\.(png|jpg|jpeg|svg|gif|pgp)$"""); this.file_ext_regex = new Regex("""\.(png|jpg|jpeg|svg|gif|pgp)$""");
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_out_message);
stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this));
stream_interactor.get_module(Manager.IDENTITY).uploaded.connect((file_transfer, url) => { stream_interactor.get_module(Manager.IDENTITY).uploaded.connect((file_transfer, url) => {
file_transfer.info = url;
ignore_once.add(url); ignore_once.add(url);
}); });
} }
@ -56,14 +54,6 @@ public class FileProvider : Dino.FileProvider, Object {
} }
} }
public void check_out_message(Message message, Conversation conversation) {
if (ignore_once.remove(message.body)) return;
if (message.body.length < 5) return;
if (!url_regex.match(message.body)) return;
if (!file_ext_regex.match(message.body)) return;
download_url(message, conversation);
}
private async bool download_url(Message message, Conversation conversation) { private async bool download_url(Message message, Conversation conversation) {
bool success = false; bool success = false;
var session = new Soup.Session(); var session = new Soup.Session();
@ -101,7 +91,7 @@ public class FileProvider : Dino.FileProvider, Object {
file_transfer.size = int.parse(content_length); file_transfer.size = int.parse(content_length);
file_transfer.state = FileTransfer.State.NOT_STARTED; file_transfer.state = FileTransfer.State.NOT_STARTED;
file_transfer.provider = 0; file_transfer.provider = 0;
file_transfer.info = message.body; file_transfer.info = message.id.to_string();
file_incoming(file_transfer); file_incoming(file_transfer);
success = true; success = true;
Idle.add((owned)callback); Idle.add((owned)callback);

View file

@ -35,7 +35,11 @@ public class Manager : StreamInteractionModule, FileSender, Object {
stream_interactor.module_manager.get_module(file_transfer.account, UploadStreamModule.IDENTITY).upload(stream, file_transfer.input_stream, file_transfer.server_file_name, file_transfer.size, file_transfer.mime_type, stream_interactor.module_manager.get_module(file_transfer.account, UploadStreamModule.IDENTITY).upload(stream, file_transfer.input_stream, file_transfer.server_file_name, file_transfer.size, file_transfer.mime_type,
(stream, url_down) => { (stream, url_down) => {
uploaded(file_transfer, url_down); uploaded(file_transfer, url_down);
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(url_down, conversation); file_transfer.info = url_down;
Entities.Message message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(url_down, conversation);
message.encryption = Encryption.NONE;
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(message, conversation);
file_transfer.info = message.id.to_string();
}, },
(stream, error_str) => { (stream, error_str) => {
print(@"Failed getting upload url + $error_str\n"); print(@"Failed getting upload url + $error_str\n");
@ -97,11 +101,9 @@ public class FileMessageFilterDisplay : Plugins.MessageDisplayProvider, Object {
} }
private bool message_is_file(Database db, Entities.Message message) { private bool message_is_file(Database db, Entities.Message message) {
Qlite.QueryBuilder builder = db.file_transfer.select() Qlite.QueryBuilder builder = db.file_transfer.select().with(db.file_transfer.info, "=", message.id.to_string());
.with(db.file_transfer.info, "=", message.body) Qlite.QueryBuilder builder2 = db.file_transfer.select().with(db.file_transfer.info, "=", message.body);
.with(db.file_transfer.account_id, "=", message.account.id) return builder.count() > 0 || builder2.count() > 0;
.with(db.file_transfer.counterpart_id, "=", db.get_jid_id(message.counterpart));
return builder.count() > 0;
} }
} }

View file

@ -17,10 +17,12 @@ public class InFileProcessor : IncommingFileProcessor, Object {
data.append_vals(buf, (uint) len); data.append_vals(buf, (uint) len);
} while(len > 0); } while(len > 0);
uint8[] clear_data = GPGHelper.decrypt_data(data.data); GPGHelper.DecryptedData clear_data = GPGHelper.decrypt_data(data.data);
file_transfer.input_stream = new MemoryInputStream.from_data(clear_data, GLib.free); file_transfer.input_stream = new MemoryInputStream.from_data(clear_data.data, GLib.free);
file_transfer.encryption = Encryption.PGP; file_transfer.encryption = Encryption.PGP;
if (file_transfer.file_name.has_suffix(".pgp")) { if (clear_data.filename != null && clear_data.filename != "") {
file_transfer.file_name = clear_data.filename;
} else if (file_transfer.file_name.has_suffix(".pgp")) {
file_transfer.file_name = file_transfer.file_name.substring(0, file_transfer.file_name.length - 4); file_transfer.file_name = file_transfer.file_name.substring(0, file_transfer.file_name.length - 4);
} }
} catch (Error e) { } catch (Error e) {

View file

@ -15,10 +15,10 @@ public class OutFileProcessor : OutgoingFileProcessor, Object {
} }
public void process(Conversation conversation, FileTransfer file_transfer) { public void process(Conversation conversation, FileTransfer file_transfer) {
string uri = file_transfer.get_uri(); string path = file_transfer.get_file().get_path();
try { try {
GPG.Key[] keys = stream_interactor.get_module(Manager.IDENTITY).get_key_fprs(conversation); GPG.Key[] keys = stream_interactor.get_module(Manager.IDENTITY).get_key_fprs(conversation);
uint8[] enc_content = GPGHelper.encrypt_file(uri, keys, GPG.EncryptFlags.ALWAYS_TRUST); uint8[] enc_content = GPGHelper.encrypt_file(path, keys, GPG.EncryptFlags.ALWAYS_TRUST, file_transfer.file_name);
file_transfer.input_stream = new MemoryInputStream.from_data(enc_content, GLib.free); file_transfer.input_stream = new MemoryInputStream.from_data(enc_content, GLib.free);
file_transfer.encryption = Encryption.PGP; file_transfer.encryption = Encryption.PGP;
file_transfer.server_file_name = Xmpp.random_uuid() + ".pgp"; file_transfer.server_file_name = Xmpp.random_uuid() + ".pgp";