1# Copyright (C) 2017 Philipp Hörist <philipp AT hoerist.com>
2#
3# This file is part of Gajim.
4#
5# Gajim is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published
7# by the Free Software Foundation; version 3 only.
8#
9# Gajim is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
16
17from gi.repository import Gtk
18from gi.repository import Gdk
19
20from gajim.common import app
21from gajim.common import helpers
22from gajim.common.app import interface
23from gajim.common.exceptions import GajimGeneralException
24from gajim import dialogs
25
26from gajim.gui.dialogs import ShortcutsWindow
27from gajim.gui.single_message import SingleMessageWindow
28from gajim.gui.about import AboutDialog
29from gajim.gui.history import HistoryWindow
30from gajim.gui.discovery import ServiceDiscoveryWindow
31from gajim.gui.util import open_window
32from gajim.gui.util import get_app_window
33
34# General Actions
35
36def on_add_contact_jid(_action, param):
37    contact_jid = param.get_string()
38    open_window('AddNewContactWindow', account=None, contact_jid=contact_jid)
39
40
41# Application Menu Actions
42
43
44def on_preferences(_action, _param):
45    open_window('Preferences')
46
47
48def on_plugins(_action, _param):
49    open_window('PluginsWindow')
50
51
52def on_accounts(_action, param):
53    window = open_window('AccountsWindow')
54    account = param.get_string()
55    if account:
56        window.select_account(account)
57
58
59def on_history_manager(_action, _param):
60    open_window('HistoryManager')
61
62
63def on_bookmarks(_action, param):
64    account = param.get_string()
65    open_window('Bookmarks', account=account)
66
67
68def on_quit(_action, _param):
69    interface.roster.on_quit_request()
70
71
72def on_new_chat(_action, param):
73    window = open_window('StartChatDialog')
74    search_text = param.get_string()
75    if search_text:
76        window.set_search_text(search_text)
77
78
79# Accounts Actions
80
81
82def on_profile(_action, param):
83    account = param.get_string()
84    open_window('ProfileWindow', account=account)
85
86
87def on_send_server_message(_action, param):
88    account = param.get_string()
89    server = app.settings.get_account_setting(account, 'hostname')
90    server += '/announce/online'
91    SingleMessageWindow(account, server, 'send')
92
93
94def on_service_disco(_action, param):
95    account = param.get_string()
96    server_jid = app.settings.get_account_setting(account, 'hostname')
97    if server_jid in interface.instances[account]['disco']:
98        interface.instances[account]['disco'][server_jid].\
99            window.present()
100    else:
101        try:
102            # Object will add itself to the window dict
103            ServiceDiscoveryWindow(account, address_entry=True)
104        except GajimGeneralException:
105            pass
106
107def on_create_gc(_action, param):
108    account = param.get_string()
109    open_window('CreateGroupchatWindow', account=account or None)
110
111
112def on_add_contact(_action, param):
113    account, contact_jid = param.get_strv()
114    if not contact_jid:
115        contact_jid = None
116    open_window('AddNewContactWindow', account=account, contact_jid=contact_jid)
117
118
119def on_single_message(_action, param):
120    account = param.get_string()
121    open_window('SingleMessageWindow', account=account, action='send')
122
123
124def on_merge_accounts(action, param):
125    action.set_state(param)
126    value = param.get_boolean()
127    app.settings.set('mergeaccounts', value)
128    # Do not merge accounts if only one active
129    if len(app.connections) >= 2:
130        app.interface.roster.regroup = value
131    else:
132        app.interface.roster.regroup = False
133    app.interface.roster.setup_and_draw_roster()
134
135
136def on_add_account(action, _param):
137    open_window('AccountWizard')
138
139
140def on_import_contacts(_action, param):
141    account = param.get_string()
142    if 'import_contacts' in app.interface.instances:
143        app.interface.instances['import_contacts'].dialog.present()
144    else:
145        app.interface.instances['import_contacts'] = \
146            dialogs.SynchroniseSelectAccountDialog(account)
147
148
149# Advanced Actions
150
151def on_pep_config(_action, param):
152    account = param.get_string()
153    open_window('PEPConfig', account=account)
154
155
156def on_mam_preferences(_action, param):
157    account = param.get_string()
158    open_window('MamPreferences', account=account)
159
160
161def on_blocking_list(_action, param):
162    account = param.get_string()
163    open_window('BlockingList', account=account)
164
165
166def on_history_sync(_action, param):
167    account = param.get_string()
168    open_window('HistorySyncAssistant',
169                account=account,
170                parent=interface.roster.window)
171
172
173def on_server_info(_action, param):
174    account = param.get_string()
175    open_window('ServerInfo', account=account)
176
177
178def on_xml_console(_action, _param):
179    open_window('XMLConsoleWindow')
180
181
182def on_manage_proxies(_action, _param):
183    open_window('ManageProxies')
184
185
186# Admin Actions
187
188
189def on_set_motd(_action, param):
190    account = param.get_string()
191    server = app.settings.get_account_setting(account, 'hostname')
192    server += '/announce/motd'
193    SingleMessageWindow(account, server, 'send')
194
195
196def on_update_motd(_action, param):
197    account = param.get_string()
198    server = app.settings.get_account_setting(account, 'hostname')
199    server += '/announce/motd/update'
200    SingleMessageWindow(account, server, 'send')
201
202
203def on_delete_motd(_action, param):
204    account = param.get_string()
205    app.connections[account].get_module('Announce').delete_motd()
206
207# Help Actions
208
209
210def on_contents(_action, _param):
211    helpers.open_uri('https://dev.gajim.org/gajim/gajim/wikis')
212
213
214def on_faq(_action, _param):
215    helpers.open_uri('https://dev.gajim.org/gajim/gajim/wikis/help/gajimfaq')
216
217
218def on_keyboard_shortcuts(_action, _param):
219    ShortcutsWindow()
220
221
222def on_features(_action, _param):
223    open_window('Features')
224
225
226def on_about(_action, _param):
227    AboutDialog()
228
229# View Actions
230
231
232def on_file_transfers(_action, _param):
233    if interface.instances['file_transfers']. \
234            window.get_property('visible'):
235        interface.instances['file_transfers'].window.present()
236    else:
237        interface.instances['file_transfers'].window.show_all()
238
239
240def on_history(action, param):
241    on_browse_history(action, param)
242
243
244def on_open_event(_action, param):
245    dict_ = param.unpack()
246    app.interface.handle_event(
247        dict_['account'], dict_['jid'], dict_['type_'])
248
249
250def on_remove_event(_action, param):
251    dict_ = param.unpack()
252    account, jid, type_ = dict_['account'], dict_['jid'], dict_['type_']
253    event = app.events.get_first_event(account, jid, type_)
254    app.events.remove_events(account, jid, event)
255    win = app.interface.msg_win_mgr.get_window(jid, account)
256    if win:
257        win.redraw_tab(win.get_control(jid, account))
258        win.show_title()
259
260# Other Actions
261
262def toggle_ipython(_action, _param):
263    """
264    Show/hide the ipython window
265    """
266    win = app.ipython_window
267    if win and win.window.is_visible():
268        win.present()
269    else:
270        app.interface.create_ipython_window()
271
272
273def show_next_pending_event(_action, _param):
274    """
275    Show the window(s) with next pending event in tabbed/group chats
276    """
277    if app.events.get_nb_events():
278        account, jid, event = app.events.get_first_systray_event()
279        if not event:
280            return
281        app.interface.handle_event(account, jid, event.type_)
282
283
284def open_mail(_action, param):
285    uri = param.get_string()
286    if not uri.startswith('mailto:'):
287        uri = 'mailto:%s' % uri
288    helpers.open_uri(uri)
289
290
291def open_link(_action, param):
292    account, uri = param.get_strv()
293    helpers.open_uri(uri, account=account)
294
295
296def copy_text(_action, param):
297    text = param.get_string()
298    clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
299    clip.set_text(text, -1)
300
301
302def start_chat(_action, param):
303    account, jid = param.get_strv()
304    app.interface.new_chat_from_jid(account, jid)
305
306
307def on_browse_history(_action, param):
308    jid, account = None, None
309    if param is not None:
310        dict_ = param.unpack()
311        jid = dict_.get('jid')
312        account = dict_.get('account')
313
314    window = get_app_window(HistoryWindow)
315    if window is None:
316        HistoryWindow(jid, account)
317    else:
318        window.present()
319        if jid is not None and account is not None:
320            window.open_history(jid, account)
321
322
323def on_groupchat_join(_action, param):
324    account, jid = param.get_strv()
325    open_window('GroupchatJoin', account=account, jid=jid)
326