1# -*- python-mode -*-
2# -*- coding: UTF-8 -*-
3
4## Copyright (C) 2012-2013  Daniel Pavel
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License along
17## with this program; if not, write to the Free Software Foundation, Inc.,
18## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20from __future__ import absolute_import, division, print_function, unicode_literals
21
22from gi.repository import Gtk, Gdk
23
24# from logging import getLogger
25# _log = getLogger(__name__)
26# del getLogger
27
28
29from solaar.i18n import _
30
31#
32#
33#
34
35def make(name, label, function, stock_id=None, *args):
36	action = Gtk.Action(name, label, label, None)
37	action.set_icon_name(name)
38	if stock_id is not None:
39		action.set_stock_id(stock_id)
40	if function:
41		action.connect('activate', function, *args)
42	return action
43
44
45def make_toggle(name, label, function, stock_id=None, *args):
46	action = Gtk.ToggleAction(name, label, label, None)
47	action.set_icon_name(name)
48	if stock_id is not None:
49		action.set_stock_id(stock_id)
50	action.connect('activate', function, *args)
51	return action
52
53#
54#
55#
56
57# def _toggle_notifications(action):
58# 	if action.get_active():
59# 		notify.init('Solaar')
60# 	else:
61# 		notify.uninit()
62# 	action.set_sensitive(notify.available)
63# toggle_notifications = make_toggle('notifications', 'Notifications', _toggle_notifications)
64
65
66from .about import show_window as _show_about_window
67from solaar import NAME
68about = make('help-about', _("About") + ' ' + NAME, _show_about_window, stock_id=Gtk.STOCK_ABOUT)
69
70#
71#
72#
73
74from . import pair_window
75def pair(window, receiver):
76	assert receiver
77	assert receiver.kind is None
78
79	pair_dialog = pair_window.create(receiver)
80	pair_dialog.set_transient_for(window)
81	pair_dialog.set_destroy_with_parent(True)
82	pair_dialog.set_modal(True)
83	pair_dialog.set_type_hint(Gdk.WindowTypeHint.DIALOG)
84	pair_dialog.set_position(Gtk.WindowPosition.CENTER)
85	pair_dialog.present()
86
87
88from ..ui import error_dialog
89def unpair(window, device):
90	assert device
91	assert device.kind is not None
92
93	qdialog = Gtk.MessageDialog(window, 0,
94								Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE,
95								_("Unpair") + ' ' + device.name + ' ?')
96	qdialog.set_icon_name('remove')
97	qdialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
98	qdialog.add_button(_("Unpair"), Gtk.ResponseType.ACCEPT)
99	choice = qdialog.run()
100	qdialog.destroy()
101	if choice == Gtk.ResponseType.ACCEPT:
102		receiver = device.receiver
103		assert receiver
104		device_number = device.number
105
106		try:
107			del receiver[device_number]
108		except:
109			# _log.exception("unpairing %s", device)
110			error_dialog('unpair', device)
111