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
|
using Dino.Entities;
namespace Dino {
public extern unowned string get_version();
public string get_short_version() {
string version = get_version();
if (!version.contains("~")) return version;
return version.split("~")[0] + "+";
}
public interface Application : GLib.Application {
public abstract Database db { get; set; }
public abstract Dino.Entities.Settings settings { get; set; }
public abstract StreamInteractor stream_interactor { get; set; }
public abstract Plugins.Registry plugin_registry { get; set; }
public abstract SearchPathGenerator? search_path_generator { get; set; }
internal static string print_xmpp;
private const OptionEntry[] options = {
{ "print-xmpp", 0, 0, OptionArg.STRING, ref print_xmpp, "Print XMPP stanzas identified by DESC to stderr", "DESC" },
{ null }
};
public abstract void handle_uri(string jid, string query, Gee.Map<string, string> options);
public void init() throws Error {
if (DirUtils.create_with_parents(get_storage_dir(), 0700) == -1) {
throw new Error(-1, 0, "Could not create storage dir \"%s\": %s", get_storage_dir(), FileUtils.error_from_errno(errno).to_string());
}
this.db = new Database(Path.build_filename(get_storage_dir(), "dino.db"));
this.settings = new Dino.Entities.Settings.from_db(db);
this.stream_interactor = new StreamInteractor(db);
MessageProcessor.start(stream_interactor, db);
MessageStorage.start(stream_interactor, db);
PresenceManager.start(stream_interactor);
CounterpartInteractionManager.start(stream_interactor);
BlockingManager.start(stream_interactor);
Calls.start(stream_interactor, db);
ConversationManager.start(stream_interactor, db);
OccupantIdStore.start(stream_interactor, db);
MucManager.start(stream_interactor, db);
AvatarManager.start(stream_interactor, db);
RosterManager.start(stream_interactor, db);
FileManager.start(stream_interactor, db);
FileManager2.start(stream_interactor, db);
CallStore.start(stream_interactor, db);
ContentItemStore.start(stream_interactor, db);
ChatInteraction.start(stream_interactor);
NotificationEvents.start(stream_interactor);
SearchProcessor.start(stream_interactor, db);
Register.start(stream_interactor, db);
EntityInfo.start(stream_interactor, db, settings);
MessageCorrection.start(stream_interactor, db);
FileTransferStorage.start(stream_interactor, db);
Reactions.start(stream_interactor, db);
Replies.start(stream_interactor, db);
FallbackBody.start(stream_interactor, db);
ContactModels.start(stream_interactor);
MessageDeletion.start(stream_interactor, db);
StatelessFileSharing.start(stream_interactor, db);
create_actions();
startup.connect(() => {
stream_interactor.connection_manager.log_options = print_xmpp;
restore();
});
shutdown.connect(() => {
stream_interactor.connection_manager.make_offline_all();
});
open.connect((files, hint) => {
if (files.length != 1) {
warning("Can't handle more than one URI at once.");
return;
}
File file = files[0];
if (!file.has_uri_scheme("xmpp")) {
warning("xmpp:-URI expected");
return;
}
string uri = file.get_uri();
if (!uri.contains(":")) {
warning("Invalid URI");
return;
}
string r = uri.split(":", 2)[1];
string[] m = r.split("?", 2);
string jid = m[0];
while (jid[0] == '/') {
jid = jid.substring(1);
}
jid = Uri.unescape_string(jid);
try {
jid = Xmpp.Jid.from_string(jid).to_string();
} catch (Xmpp.InvalidJidError e) {
warning("Received invalid jid in xmpp:-URI: %s", e.message);
}
string query = "message";
Gee.Map<string, string> options = new Gee.HashMap<string, string>();
if (m.length == 2) {
string[] cmds = m[1].split(";");
query = cmds[0];
for (int i = 1; i < cmds.length; ++i) {
string[] opt = cmds[i].split("=", 2);
options[Uri.unescape_string(opt[0])] = opt.length == 2 ? Uri.unescape_string(opt[1]) : "";
}
}
activate();
handle_uri(jid, query, options);
});
add_main_option_entries(options);
}
public static string get_storage_dir() {
return Path.build_filename(Environment.get_user_data_dir(), "dino");
}
public static unowned Application get_default() {
return (Dino.Application) GLib.Application.get_default();
}
public void create_actions() {
SimpleAction accept_subscription_action = new SimpleAction("accept-subscription", VariantType.INT32);
accept_subscription_action.activate.connect((variant) => {
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(variant.get_int32());
if (conversation == null) return;
stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(conversation.account, conversation.counterpart);
stream_interactor.get_module(PresenceManager.IDENTITY).request_subscription(conversation.account, conversation.counterpart);
});
add_action(accept_subscription_action);
SimpleAction open_file_action = new SimpleAction("file_open_externally", VariantType.INT32);
open_file_action.activate.connect((variant) => {
int file_transfer_id = variant.get_int32();
FileTransfer file_transfer = stream_interactor.get_module(FileTransferStorage.IDENTITY).get_file_by_id(file_transfer_id, null);
try{
AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null);
} catch (Error err) {
warning("Failed to open %s - %s", file_transfer.get_file().get_uri(), err.message);
}
});
add_action(open_file_action);
SimpleAction download_file_action = new SimpleAction("file_start_download", VariantType.INT32);
download_file_action.activate.connect((variant) => {
int file_transfer_id = variant.get_int32();
FileTransfer file_transfer = stream_interactor.get_module(FileTransferStorage.IDENTITY).get_file_by_id(file_transfer_id, null);
stream_interactor.get_module(FileManager.IDENTITY).download_file.begin(file_transfer);
});
add_action(download_file_action);
SimpleAction cancel_file_action = new SimpleAction("file_cancel_download", VariantType.INT32);
cancel_file_action.activate.connect((variant) => {
int file_transfer_id = variant.get_int32();
FileTransfer file_transfer = stream_interactor.get_module(FileTransferStorage.IDENTITY).get_file_by_id(file_transfer_id, null);
file_transfer.cancellable.cancel();
});
add_action(cancel_file_action);
}
protected void add_connection(Account account) {
if ((get_flags() & ApplicationFlags.IS_SERVICE) == ApplicationFlags.IS_SERVICE) hold();
stream_interactor.connect_account(account);
}
protected void remove_connection(Account account) {
if ((get_flags() & ApplicationFlags.IS_SERVICE) == ApplicationFlags.IS_SERVICE) release();
stream_interactor.disconnect_account.begin(account);
}
private void restore() {
foreach (Account account in db.get_accounts()) {
if (account.enabled) add_connection(account);
}
}
}
}
|