def check_bluetooth_status(message, exitfunc, *args, **kwargs): try: applet = AppletService() except DBusProxyFailed as e: logging.exception(e) print("Blueman applet needs to be running") exitfunc() if "PowerManager" in applet.QueryPlugins(): if not applet.get_bluetooth_status(): d = Gtk.MessageDialog(None, type=Gtk.MessageType.ERROR) d.props.icon_name = "blueman" d.props.text = _("Bluetooth Turned Off") d.props.secondary_text = message d.add_button("Exit", Gtk.ResponseType.NO) d.add_button(_("Enable Bluetooth"), Gtk.ResponseType.YES) resp = d.run() d.destroy() if resp != Gtk.ResponseType.YES: exitfunc() else: applet.SetBluetoothStatus('(b)', True, **kwargs) if not applet.get_bluetooth_status(): print('Failed to enable bluetooth') exitfunc()
def check_bluetooth_status(message, exitfunc): try: applet = AppletService() except DBusProxyFailed as e: logging.exception(e) print("Blueman applet needs to be running") exitfunc() return if "PowerManager" not in applet.QueryPlugins(): return if not applet.GetBluetoothStatus(): d = Gtk.MessageDialog(None, type=Gtk.MessageType.ERROR, icon_name="blueman", text=_("Bluetooth Turned Off"), secondary_text=message) d.add_button(_("Exit"), Gtk.ResponseType.NO) d.add_button(_("Enable Bluetooth"), Gtk.ResponseType.YES) resp = d.run() d.destroy() if resp != Gtk.ResponseType.YES: exitfunc() return applet.SetBluetoothStatus('(b)', True) timeout = time() + 60 while applet.GetRequestStatus(): sleep(0.1) if time() > timeout: break if not applet.GetBluetoothStatus(): print('Failed to enable bluetooth') exitfunc() return config = Config("org.blueman.plugins.powermanager") if config["auto-power-on"] is None: d = Gtk.MessageDialog( None, type=Gtk.MessageType.QUESTION, icon_name="blueman", text=_("Shall bluetooth get enabled automatically?")) d.add_button(_("Yes"), Gtk.ResponseType.YES) d.add_button(_("No"), Gtk.ResponseType.NO) resp = d.run() d.destroy() config["auto-power-on"] = resp == Gtk.ResponseType.YES
def on_load(self, container: Gtk.Box) -> None: self._builder = Builder("services-transfer.ui") self.widget = self._builder.get_widget("transfer", Gtk.Widget) container.pack_start(self.widget, True, True, 0) a = AppletService() if "TransferService" in a.QueryPlugins(): self._setup_transfer() else: self.widget.props.sensitive = False self.widget.props.tooltip_text = _( "Applet's transfer service plugin is disabled")
def on_load(self, container: Gtk.Box) -> None: self.Builder = Gtk.Builder() self.Builder.set_translation_domain("blueman") self.Builder.add_from_file(UI_PATH + "/services-transfer.ui") self.widget = self.Builder.get_object("transfer") container.pack_start(self.widget, True, True, 0) a = AppletService() if "TransferService" in a.QueryPlugins(): self._setup_transfer() else: self.widget.props.sensitive = False self.widget.props.tooltip_text = _("Applet's transfer service plugin is disabled")
def on_request_menu_items( self, manager_menu: ManagerDeviceMenu, device: Device) -> List[Tuple[Gtk.MenuItem, int]]: items: List[Tuple[Gtk.MenuItem, int]] = [] appl = AppletService() self.has_dun = False serial_items: List[Gtk.MenuItem] = [] def add_menu_item(manager_menu: ManagerDeviceMenu, service: Service) -> None: if service.connected: surface = self._make_x_icon(service.icon, 16) item = create_menuitem(service.name, surface=surface) item.connect("activate", manager_menu.on_disconnect, service) items.append((item, service.priority + 100)) else: item = create_menuitem(service.name, service.icon) if service.description: item.props.tooltip_text = service.description item.connect("activate", manager_menu.on_connect, service) if isinstance(service, SerialService): serial_items.append(item) if isinstance(service, DialupNetwork): self.has_dun = True else: items.append((item, service.priority)) item.props.sensitive = service.available item.show() for service in get_services(device): add_menu_item(manager_menu, service) if isinstance(service, SerialService): for dev in rfcomm_list(): if dev["dst"] == device['Address'] and dev[ "state"] == "connected": devname = _("Serial Port %s") % "rfcomm%d" % dev["id"] surface = self._make_x_icon("modem", 16) item: Gtk.MenuItem = create_menuitem(devname, surface=surface) item.connect("activate", manager_menu.on_disconnect, service, dev["id"]) items.append((item, 120)) item.show() if isinstance(service, NetworkService) and service.connected: if "DhcpClient" in appl.QueryPlugins(): def renew(_item: Gtk.MenuItem) -> None: appl.DhcpClient('(s)', device.get_object_path()) item = create_menuitem(_("Renew IP Address"), "view-refresh") item.connect("activate", renew) item.show() items.append((item, 201)) if self.has_dun and ('PPPSupport' in appl.QueryPlugins() or 'NMDUNSupport' in appl.QueryPlugins()): def open_settings(_item: Gtk.MenuItem, device: Device) -> None: from blueman.gui.GsmSettings import GsmSettings d = GsmSettings(device['Address']) d.run() d.destroy() item = Gtk.SeparatorMenuItem() item.show() serial_items.append(item) item = create_menuitem(_("Dialup Settings"), "preferences-other") serial_items.append(item) item.show() item.connect("activate", open_settings, device) if len(serial_items) > 1: sub = Gtk.Menu() sub.show() item = create_menuitem(_("Serial Ports"), "modem") item.set_submenu(sub) item.show() items.append((item, 90)) for item in serial_items: sub.append(item) else: for item in serial_items: items.append((item, 80)) return items
def setup_network(self): self.Config = Config("org.blueman.network") nap_enable = self.Builder.get_object("nap-enable") r_dnsmasq = self.Builder.get_object("r_dnsmasq") r_dhcpd = self.Builder.get_object("r_dhcpd") r_udhcpd = self.Builder.get_object("r_udhcpd") net_ip = self.Builder.get_object("net_ip") rb_nm = self.Builder.get_object("rb_nm") rb_blueman = self.Builder.get_object("rb_blueman") rb_dun_nm = self.Builder.get_object("rb_dun_nm") rb_dun_blueman = self.Builder.get_object("rb_dun_blueman") nap_frame = self.Builder.get_object("nap_frame") warning = self.Builder.get_object("warning") if not self.Config["nap-enable"]: nap_frame.props.sensitive = False nc = NetConf.get_default() if nc.ip4_address is not None: net_ip.props.text = inet_ntoa(nc.ip4_address) nap_enable.props.active = True else: net_ip.props.text = "10.%d.%d.1" % (randint(0, 255), randint(0, 255)) if nc.get_dhcp_handler() is None: nap_frame.props.sensitive = False nap_enable.props.active = False r_dnsmasq.props.active = True self.Config["nap-enable"] = False have_dhcpd = have("dhcpd3") or have("dhcpd") have_dnsmasq = have("dnsmasq") have_udhcpd = have("udhcpd") if nc.get_dhcp_handler() == DnsMasqHandler and have_dnsmasq: r_dnsmasq.props.active = True elif nc.get_dhcp_handler() == DhcpdHandler and have_dhcpd: r_dhcpd.props.active = True elif nc.get_dhcp_handler() == UdhcpdHandler and have_udhcpd: r_udhcpd.props.active = True else: r_dnsmasq.props.active = True if not have_dnsmasq and not have_dhcpd and not have_udhcpd: nap_frame.props.sensitive = False warning.props.visible = True warning.props.sensitive = True nap_enable.props.sensitive = False self.Config["nap-enable"] = False if not have_dnsmasq: r_dnsmasq.props.sensitive = False r_dnsmasq.props.active = False if not have_dhcpd: r_dhcpd.props.sensitive = False r_dhcpd.props.active = False if not have_udhcpd: r_udhcpd.props.sensitive = False r_udhcpd.props.active = False r_dnsmasq.connect("toggled", lambda x: self.option_changed_notify("dnsmasq")) r_dhcpd.connect("toggled", lambda x: self.option_changed_notify("dhcpd")) r_udhcpd.connect("toggled", lambda x: self.option_changed_notify("udhcpd")) net_ip.connect("changed", lambda x: self.option_changed_notify("ip", False)) nap_enable.connect("toggled", lambda x: self.option_changed_notify("nap_enable")) self.Config.bind_to_widget("nap-enable", nap_enable, "active", Gio.SettingsBindFlags.GET) nap_enable.bind_property("active", nap_frame, "sensitive", 0) applet = AppletService() avail_plugins = applet.QueryAvailablePlugins() active_plugins = applet.QueryPlugins() def dun_support_toggled(rb, x): if rb.props.active and x == "nm": applet.SetPluginConfig('(sb)', "PPPSupport", False) applet.SetPluginConfig('(sb)', "NMDUNSupport", True) elif rb.props.active and x == "blueman": applet.SetPluginConfig('(sb)', "NMDUNSupport", False) applet.SetPluginConfig('(sb)', "PPPSupport", True) def pan_support_toggled(rb, x): if rb.props.active and x == "nm": applet.SetPluginConfig('(sb)', "DhcpClient", False) applet.SetPluginConfig('(sb)', "NMPANSupport", True) elif rb.props.active and x == "blueman": applet.SetPluginConfig('(sb)', "NMPANSupport", False) applet.SetPluginConfig('(sb)', "DhcpClient", True) if "PPPSupport" in active_plugins: rb_dun_blueman.props.active = True if "NMDUNSupport" in avail_plugins: rb_dun_nm.props.sensitive = True else: rb_dun_nm.props.sensitive = False rb_dun_nm.props.tooltip_text = _("Not currently supported with this setup") if "DhcpClient" in active_plugins: rb_blueman.props.active = True if "NMPANSupport" in avail_plugins: rb_nm.props.sensitive = True else: rb_nm.props.sensitive = False rb_nm.props.tooltip_text = _("Not currently supported with this setup") if "NMPANSupport" in active_plugins: rb_nm.props.active = True if "NMDUNSupport" in active_plugins: rb_dun_nm.props.active = True rb_nm.connect("toggled", pan_support_toggled, "nm") rb_blueman.connect("toggled", pan_support_toggled, "blueman") rb_dun_nm.connect("toggled", dun_support_toggled, "nm") rb_dun_blueman.connect("toggled", dun_support_toggled, "blueman")
class Blueman(Gtk.Window): def __init__(self): super(Blueman, self).__init__(title=_("Bluetooth Devices")) self._applet_sig = None self.Config = Config("org.blueman.general") self.Builder = Gtk.Builder() self.Builder.set_translation_domain("blueman") bind_textdomain_codeset("blueman", "UTF-8") self.Builder.add_from_file(UI_PATH + "/manager-main.ui") grid = self.Builder.get_object("grid") self.add(grid) self.set_name("BluemanManager") self.Plugins = PluginManager(ManagerPlugin, blueman.plugins.manager, self) self.Plugins.load_plugin() area = MessageArea() grid.attach(area, 0, 3, 1, 1) # Add margin for resize grip or it will overlap if self.get_has_resize_grip(): statusbar = self.Builder.get_object("statusbar") margin_right = statusbar.get_margin_right() statusbar.set_margin_right(margin_right + 10) def do_present(time): if self.props.visible: self.present_with_time(time) check_single_instance("blueman-manager", do_present) def on_window_delete(window, event): w, h = self.get_size() x, y = self.get_position() self.Config["window-properties"] = [w, h, x, y] Gtk.main_quit() def bt_status_changed(status): if not status: self.hide() check_bluetooth_status(_("Bluetooth needs to be turned on for the device manager to function"), lambda: Gtk.main_quit()) else: self.show() def on_applet_signal(_proxy, _sender, signal_name, params): if signal_name == 'BluetoothStatusChanged': status = params.unpack() bt_status_changed(status) def on_dbus_name_vanished(_connection, name): logging.info(name) if self._applet_sig is not None: self.Applet.disconnect(self._applet_sig) self._applet_sig = None self.hide() d = ErrorDialog( _("Connection to BlueZ failed"), _("Bluez daemon is not running, blueman-manager cannot continue.\n" "This probably means that there were no Bluetooth adapters detected " "or Bluetooth daemon was not started."), icon_name="blueman") d.run() d.destroy() # FIXME ui can handle BlueZ start/stop but we should inform user Gtk.main_quit() def on_dbus_name_appeared(_connection, name, owner): logging.info("%s %s" % (name, owner)) setup_icon_path() try: self.Applet = AppletService() except DBusProxyFailed: print("Blueman applet needs to be running") exit() if 'PowerManager' in self.Applet.QueryPlugins(): if not self.Applet.get_bluetooth_status(): bt_status_changed(False) self._applet_sig = self.Applet.connect('g-signal', on_applet_signal) self.connect("delete-event", on_window_delete) self.props.icon_name = "blueman" w, h, x, y = self.Config["window-properties"] if w and h: self.resize(w, h) if x and y: self.move(x, y) sw = self.Builder.get_object("scrollview") # Disable overlay scrolling if Gtk.get_minor_version() >= 16: sw.props.overlay_scrolling = False self.List = ManagerDeviceList(adapter=self.Config["last-adapter"], inst=self) self.List.show() sw.add(self.List) self.Toolbar = ManagerToolbar(self) self.Menu = ManagerMenu(self) self.Stats = ManagerStats(self) if self.List.is_valid_adapter(): self.List.display_known_devices(autoselect=True) self.List.connect("adapter-changed", self.on_adapter_changed) toolbar = self.Builder.get_object("toolbar") statusbar = self.Builder.get_object("statusbar") self.Config.bind_to_widget("show-toolbar", toolbar, "visible") self.Config.bind_to_widget("show-statusbar", statusbar, "visible") self.show() bluez.Manager.watch_name_owner(on_dbus_name_appeared, on_dbus_name_vanished) def on_adapter_changed(self, lst, adapter): if adapter is not None: self.List.display_known_devices(autoselect=True) def inquiry(self): def prop_changed(lst, adapter, key_value): key, value = key_value if key == "Discovering" and not value: prog.finalize() self.List.disconnect(s1) self.List.disconnect(s2) def on_progress(lst, frac): if abs(1.0 - frac) <= 0.00001: if not prog.started(): prog.start() else: prog.fraction(frac) prog = ManagerProgressbar(self, text=_("Searching")) prog.connect("cancelled", lambda x: self.List.stop_discovery()) try: self.List.discover_devices() except Exception as e: prog.finalize() MessageArea.show_message(*e_(e)) s1 = self.List.connect("discovery-progress", on_progress) s2 = self.List.connect("adapter-property-changed", prop_changed) def setup(self, device): command = "blueman-assistant --device=%s" % device['Address'] launch(command, None, False, "blueman", _("Bluetooth Assistant")) def bond(self, device): def error_handler(e): logging.exception(e) message = 'Pairing failed for:\n%s (%s)' % (device['Alias'], device['Address']) Notification('Bluetooth', message, icon_name="blueman").show() device.pair(error_handler=error_handler) def adapter_properties(self): launch("blueman-adapters", None, False, "blueman", _("Adapter Preferences")) def toggle_trust(self, device): device['Trusted'] = not device['Trusted'] def send(self, device, f=None): adapter = self.List.Adapter command = "blueman-sendto --source=%s --device=%s" % (adapter["Address"], device['Address']) launch(command, None, False, "blueman", _("File Sender")) def remove(self, device): self.List.Adapter.remove_device(device)
def on_request_menu_items(self, manager_menu: ManagerDeviceMenu, device: Device) -> List[DeviceMenuItem]: items: List[DeviceMenuItem] = [] appl = AppletService() services = get_services(device) connectable_services = [ service for service in services if service.connectable ] for service in connectable_services: item: Gtk.MenuItem = create_menuitem(service.name, service.icon) if service.description: item.props.tooltip_text = service.description item.connect( "activate", lambda _item: manager_menu.connect_service( service.device, service.uuid)) items.append( DeviceMenuItem(item, DeviceMenuItem.Group.CONNECT, service.priority)) item.props.sensitive = service.available item.show() connected_services = [ service for service in services if service.connected_instances ] for service in connected_services: for instance in service.connected_instances: surface = self._make_x_icon(service.icon, 16) item = create_menuitem(instance.name, surface=surface) item.connect( "activate", lambda _item: manager_menu.disconnect_service( service.device, service.uuid, instance.port)) items.append( DeviceMenuItem(item, DeviceMenuItem.Group.DISCONNECT, service.priority + 100)) item.show() if services: config = AutoConnectConfig() autoconnect_services = set(config["services"]) for service in services: if service.connected_instances or ( device.get_object_path(), service.uuid) in autoconnect_services: item = Gtk.CheckMenuItem(label=service.name) config.bind_to_menuitem(item, device, service.uuid) item.show() items.append( DeviceMenuItem(item, DeviceMenuItem.Group.AUTOCONNECT, service.priority)) for action, priority in set((action, service.priority) for service in services for action in service.common_actions if any(plugin in appl.QueryPlugins() for plugin in action.plugins)): item = create_menuitem(action.title, action.icon) items.append( DeviceMenuItem(item, DeviceMenuItem.Group.ACTIONS, priority + 200)) item.show() item.connect("activate", self._get_activation_handler(action.callback)) return items