1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
using Gee;
using Gdk;
using Gtk;
using Pango;
using Dino.Entities;
namespace Dino.Ui {
public class FileMetaItem : ConversationSummary.ContentMetaItem {
private StreamInteractor stream_interactor;
private FileItem file_item;
private FileTransfer file_transfer;
public FileMetaItem(ContentItem content_item, StreamInteractor stream_interactor) {
base(content_item);
this.stream_interactor = stream_interactor;
this.file_item = content_item as FileItem;
this.file_transfer = file_item.file_transfer;
}
public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) {
return new FileWidget(stream_interactor, file_transfer);
}
public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) {
if (file_transfer.provider != 0 || file_transfer.info == null) return null;
Gee.List<Plugins.MessageAction> actions = new ArrayList<Plugins.MessageAction>();
if (stream_interactor.get_module(ContentItemStore.IDENTITY).get_message_id_for_content_item(file_item.conversation, content_item) != null) {
Plugins.MessageAction reply_action = new Plugins.MessageAction();
reply_action.icon_name = "mail-reply-sender-symbolic";
reply_action.callback = (button, content_meta_item_activated, widget) => {
GLib.Application.get_default().activate_action("quote", new GLib.Variant.tuple(new GLib.Variant[] { new GLib.Variant.int32(file_item.conversation.id), new GLib.Variant.int32(content_item.id) }));
};
actions.add(reply_action);
Plugins.MessageAction action2 = new Plugins.MessageAction();
action2.icon_name = "dino-emoticon-add-symbolic";
EmojiChooser chooser = new EmojiChooser();
chooser.emoji_picked.connect((emoji) => {
stream_interactor.get_module(Reactions.IDENTITY).add_reaction(file_item.conversation, content_item, emoji);
});
action2.popover = chooser;
actions.add(action2);
}
return actions;
}
}
public class FileWidget : SizeRequestBox {
enum State {
IMAGE,
DEFAULT
}
private StreamInteractor stream_interactor;
private FileTransfer file_transfer;
public FileTransfer.State file_transfer_state { get; set; }
public string file_transfer_mime_type { get; set; }
private State? state = null;
private FileDefaultWidgetController default_widget_controller;
private Widget? content = null;
construct {
margin_top = 4;
size_request_mode = SizeRequestMode.HEIGHT_FOR_WIDTH;
}
public FileWidget(StreamInteractor stream_interactor, FileTransfer file_transfer) {
this.stream_interactor = stream_interactor;
this.file_transfer = file_transfer;
update_widget.begin();
// size_allocate.connect((allocation) => {
// if (allocation.height > parent.get_allocated_height()) {
// Idle.add(() => { parent.queue_resize(); return false; });
// }
// });
file_transfer.bind_property("state", this, "file-transfer-state");
file_transfer.bind_property("mime-type", this, "file-transfer-mime-type");
this.notify["file-transfer-state"].connect(update_widget);
this.notify["file-transfer-mime-type"].connect(update_widget);
}
private async void update_widget() {
if (show_image() && state != State.IMAGE) {
var content_bak = content;
FileImageWidget file_image_widget = null;
try {
file_image_widget = new FileImageWidget();
yield file_image_widget.load_from_file(file_transfer.get_file(), file_transfer.file_name);
// If the widget changed in the meanwhile, stop
if (content != content_bak) return;
if (content != null) this.remove(content);
content = file_image_widget;
state = State.IMAGE;
this.append(content);
return;
} catch (Error e) { }
}
if (state != State.DEFAULT) {
if (content != null) this.remove(content);
FileDefaultWidget default_file_widget = new FileDefaultWidget();
default_widget_controller = new FileDefaultWidgetController(default_file_widget);
default_widget_controller.set_file_transfer(file_transfer, stream_interactor);
content = default_file_widget;
this.state = State.DEFAULT;
this.append(content);
}
}
private bool show_image() {
if (file_transfer.mime_type == null) return false;
if (file_transfer.state != FileTransfer.State.COMPLETE &&
!(file_transfer.direction == FileTransfer.DIRECTION_SENT && file_transfer.state == FileTransfer.State.IN_PROGRESS)) {
return false;
}
foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) {
foreach (string mime_type in pixbuf_format.get_mime_types()) {
if (mime_type == file_transfer.mime_type) {
return true;
}
}
}
return false;
}
}
public class FileDefaultWidgetController : Object {
private FileDefaultWidget widget;
private FileTransfer? file_transfer;
public string file_transfer_path { get; set; }
public string file_transfer_state { get; set; }
public string file_transfer_mime_type { get; set; }
private StreamInteractor? stream_interactor;
private string file_uri;
private string file_name;
private FileTransfer.State state;
public FileDefaultWidgetController(FileDefaultWidget widget) {
this.widget = widget;
widget.clicked.connect(on_clicked);
widget.open_file.connect(open_file);
widget.save_file_as.connect(save_file);
widget.cancel_download.connect(cancel_download);
}
public void set_file_transfer(FileTransfer file_transfer, StreamInteractor stream_interactor) {
this.file_transfer = file_transfer;
this.stream_interactor = stream_interactor;
widget.name_label.label = file_name = file_transfer.file_name;
file_transfer.bind_property("path", this, "file-transfer-path");
file_transfer.bind_property("state", this, "file-transfer-state");
file_transfer.bind_property("mime-type", this, "file-transfer-mime-type");
this.notify["file-transfer-path"].connect(update_file_info);
this.notify["file-transfer-state"].connect(update_file_info);
this.notify["file-transfer-mime-type"].connect(update_file_info);
update_file_info();
}
public void set_file(File file, string file_name, string? mime_type) {
file_uri = file.get_uri();
state = FileTransfer.State.COMPLETE;
widget.name_label.label = this.file_name = file_name;
widget.update_file_info(mime_type, state, -1);
}
private void update_file_info() {
file_uri = file_transfer.get_file().get_uri();
state = file_transfer.state;
widget.update_file_info(file_transfer.mime_type, file_transfer.state, file_transfer.size);
}
private void open_file() {
try{
AppInfo.launch_default_for_uri(file_uri, null);
} catch (Error err) {
warning("Failed to open %s - %s", file_uri, err.message);
}
}
private void save_file() {
var save_dialog = new FileChooserNative(_("Save as…"), widget.get_root() as Gtk.Window, FileChooserAction.SAVE, null, null);
save_dialog.set_modal(true);
save_dialog.set_current_name(file_name);
save_dialog.response.connect(() => {
try{
GLib.File.new_for_uri(file_uri).copy(save_dialog.get_file(), GLib.FileCopyFlags.OVERWRITE, null);
} catch (Error err) {
warning("Failed copy file %s - %s", file_uri, err.message);
}
});
save_dialog.show();
}
private void cancel_download() {
file_transfer.cancellable.cancel();
}
private void on_clicked() {
switch (state) {
case FileTransfer.State.COMPLETE:
open_file();
break;
case FileTransfer.State.NOT_STARTED:
assert(stream_interactor != null && file_transfer != null);
stream_interactor.get_module(FileManager.IDENTITY).download_file.begin(file_transfer);
break;
default:
// Clicking doesn't do anything in FAILED and IN_PROGRESS states
break;
}
}
}
}
|