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
|
using Gee;
using Dino.Entities;
using Xmpp;
namespace Dino {
public class ContentItemAccumulator : StreamInteractionModule, Object {
public static ModuleIdentity<ContentItemAccumulator> IDENTITY = new ModuleIdentity<ContentItemAccumulator>("content_item_accumulator");
public string id { get { return IDENTITY.id; } }
public signal void new_item();
private StreamInteractor stream_interactor;
private Gee.List<ContentFilter> filters = new ArrayList<ContentFilter>();
private HashMap<ContentItemCollection, Conversation> collection_conversations = new HashMap<ContentItemCollection, Conversation>();
public static void start(StreamInteractor stream_interactor) {
ContentItemAccumulator m = new ContentItemAccumulator(stream_interactor);
stream_interactor.add_module(m);
}
public ContentItemAccumulator(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(on_new_message);
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(on_new_message);
stream_interactor.get_module(FileManager.IDENTITY).received_file.connect(insert_file_transfer);
}
public void init(Conversation conversation, ContentItemCollection item_collection) {
collection_conversations[item_collection] = conversation;
}
public Gee.List<ContentItem> populate_latest(ContentItemCollection item_collection, Conversation conversation, int n) {
Gee.TreeSet<ContentItem> items = new Gee.TreeSet<ContentItem>(ContentItem.compare);
Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages(conversation, n);
if (messages != null) {
foreach (Entities.Message message in messages) {
items.add(new MessageItem(message, conversation));
}
}
Gee.List<FileTransfer> transfers = stream_interactor.get_module(FileManager.IDENTITY).get_latest_transfers(conversation.account, conversation.counterpart, n);
foreach (FileTransfer transfer in transfers) {
items.add(new FileItem(transfer));
}
BidirIterator<ContentItem> iter = items.bidir_iterator();
iter.last();
int i = 0;
while (i < n && iter.has_previous()) {
iter.previous();
i++;
}
Gee.List<ContentItem> ret = new ArrayList<ContentItem>();
do {
ret.add(iter.get());
} while(iter.next());
return ret;
}
public Gee.List<ContentItem> populate_before(ContentItemCollection item_collection, Conversation conversation, ContentItem item, int n) {
Gee.TreeSet<ContentItem> items = new Gee.TreeSet<ContentItem>(ContentItem.compare);
Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before_message(conversation, item.display_time, n);
if (messages != null) {
foreach (Entities.Message message in messages) {
items.add(new MessageItem(message, conversation));
}
}
Gee.List<FileTransfer> transfers = stream_interactor.get_module(FileManager.IDENTITY).get_transfers_before(conversation.account, conversation.counterpart, item.display_time, n);
foreach (FileTransfer transfer in transfers) {
items.add(new FileItem(transfer));
}
BidirIterator<ContentItem> iter = items.bidir_iterator();
iter.last();
int i = 0;
while (i < n && iter.has_previous()) {
iter.previous();
i++;
}
Gee.List<ContentItem> ret = new ArrayList<ContentItem>();
do {
ret.add(iter.get());
} while(iter.next());
return ret;
}
public void populate_after(Conversation conversation, ContentItem item, int n) {
}
public void add_filter(ContentFilter content_filter) {
filters.add(content_filter);
}
private void on_new_message(Message message, Conversation conversation) {
foreach (ContentItemCollection collection in collection_conversations.keys) {
if (collection_conversations[collection].equals(conversation)) {
MessageItem item = new MessageItem(message, conversation);
insert_item(collection, item);
}
}
}
private void insert_file_transfer(FileTransfer file_transfer) {
foreach (ContentItemCollection collection in collection_conversations.keys) {
Conversation conversation = collection_conversations[collection];
if (conversation.account.equals(file_transfer.account) && conversation.counterpart.equals_bare(file_transfer.counterpart)) {
FileItem item = new FileItem(file_transfer);
insert_item(collection, item);
}
}
}
private void insert_item(ContentItemCollection item_collection, ContentItem content_item) {
bool insert = true;
foreach (ContentFilter filter in filters) {
if (filter.discard(content_item)) {
insert = false;
}
}
if (insert) {
item_collection.insert_item(content_item);
}
}
}
public interface ContentItemCollection : Object {
public abstract void insert_item(ContentItem item);
public abstract void remove_item(ContentItem item);
}
public interface ContentFilter : Object {
public abstract bool discard(ContentItem content_item);
}
public abstract class ContentItem : Object {
public virtual string type_ { get; set; }
public virtual Jid? jid { get; set; default=null; }
public virtual DateTime? sort_time { get; set; default=null; }
public virtual double seccondary_sort_indicator { get; set; }
public virtual DateTime? display_time { get; set; default=null; }
public virtual Encryption? encryption { get; set; default=null; }
public virtual Entities.Message.Marked? mark { get; set; default=null; }
public static int compare(ContentItem a, ContentItem b) {
int res = a.sort_time.compare(b.sort_time);
if (res == 0) {
res = a.display_time.compare(b.display_time);
}
if (res == 0) {
res = a.seccondary_sort_indicator - b.seccondary_sort_indicator > 0 ? 1 : -1;
}
return res;
}
}
public class MessageItem : ContentItem {
public const string TYPE = "message";
public override string type_ { get; set; default=TYPE; }
public Message message;
public Conversation conversation;
public MessageItem(Message message, Conversation conversation) {
this.message = message;
this.conversation = conversation;
this.jid = message.from;
this.sort_time = message.local_time;
this.seccondary_sort_indicator = message.id + 0.0845;
this.display_time = message.time;
this.encryption = message.encryption;
this.mark = message.marked;
WeakRef weak_message = WeakRef(message);
message.notify["marked"].connect(() => {
Message? m = weak_message.get() as Message;
if (m == null) return;
mark = m.marked;
});
}
}
public class FileItem : ContentItem {
public const string TYPE = "file";
public override string type_ { get; set; default=TYPE; }
public FileTransfer file_transfer;
public Conversation conversation;
public FileItem(FileTransfer file_transfer) {
this.file_transfer = file_transfer;
this.jid = file_transfer.direction == FileTransfer.DIRECTION_SENT ? file_transfer.account.bare_jid.with_resource(file_transfer.account.resourcepart) : file_transfer.counterpart;
this.sort_time = file_transfer.time;
this.seccondary_sort_indicator = file_transfer.id + 0.2903;
this.display_time = file_transfer.time;
this.encryption = file_transfer.encryption;
this.mark = file_to_message_state(file_transfer.state);
file_transfer.notify["state"].connect_after(() => {
this.mark = file_to_message_state(file_transfer.state);
});
}
private Entities.Message.Marked file_to_message_state(FileTransfer.State state) {
switch (state) {
case FileTransfer.State.IN_PROCESS:
return Entities.Message.Marked.UNSENT;
case FileTransfer.State.COMPLETE:
return Entities.Message.Marked.NONE;
case FileTransfer.State.NOT_STARTED:
return Entities.Message.Marked.UNSENT;
case FileTransfer.State.FAILED:
return Entities.Message.Marked.WONTSEND;
}
assert_not_reached();
}
}
}
|