-
Notifications
You must be signed in to change notification settings - Fork 91
Services Discovery Functionality #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheJayas
wants to merge
1
commit into
OpenPrinting:master
Choose a base branch
from
TheJayas:ws_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import dbus | ||
import dbus.mainloop.glib | ||
import gi | ||
from gi.repository import GLib | ||
gi.require_version('Gtk', '3.0') | ||
from gi.repository import Gtk | ||
import avahi | ||
|
||
SERVICE_TYPES = [ | ||
"_http._tcp", "_https._tcp" | ||
# "_ipp._tcp", "_ipps-system._tcp", | ||
# "_nvstream._tcp", "_nvstream_dbd._tcp", "_airplay._tcp", "_raop._tcp" | ||
] | ||
|
||
class AvahiServiceBrowser: | ||
def __init__(self): | ||
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | ||
self.bus = dbus.SystemBus() | ||
self.discovered_services = [] # List to store unique discovered services | ||
|
||
def get_all_discovered_services(self): | ||
return self.discovered_services | ||
|
||
def on_service_found(self, interface, protocol, name, stype, domain, adminurl=None): | ||
# Check for duplicates before adding | ||
if not any(service['name'] == name and service['link'] == adminurl for service in self.discovered_services): | ||
print(f"Service found: {name}, type: {stype}, domain: {domain}, admin URL: {adminurl}") | ||
service_info = { | ||
"name": name, | ||
"link": adminurl | ||
} | ||
self.discovered_services.append(service_info) | ||
|
||
def resolve_service(self, interface, protocol, name, stype, domain, flags): | ||
server = self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER) | ||
|
||
try: | ||
# Resolve the service to obtain its host and port | ||
resolved = server.ResolveService( | ||
interface, protocol, name, stype, domain, | ||
avahi.PROTO_UNSPEC, dbus.UInt32(0), | ||
dbus_interface=avahi.DBUS_INTERFACE_SERVER | ||
) | ||
|
||
# Construct the admin URL | ||
host = resolved[5] # The resolved hostname | ||
port = resolved[8] # The resolved port | ||
adminurl = f"http://{host}:{port}" | ||
|
||
# Pass all information to `on_service_found` | ||
self.on_service_found(interface, protocol, name, stype, domain, adminurl) | ||
|
||
except dbus.DBusException as e: | ||
print(f"Failed to resolve service {name}: {e}") | ||
|
||
def get_services(self, service_type): | ||
try: | ||
server = dbus.Interface( | ||
self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), | ||
avahi.DBUS_INTERFACE_SERVER | ||
) | ||
sbrowser = dbus.Interface( | ||
self.bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew( | ||
avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, service_type, 'local', dbus.UInt32(0))), | ||
avahi.DBUS_INTERFACE_SERVICE_BROWSER | ||
) | ||
# Connect ItemNew to resolve service when a new item is found | ||
sbrowser.connect_to_signal("ItemNew", self.resolve_service) | ||
print(f"Started service browser for: {service_type}") | ||
|
||
except dbus.DBusException as e: | ||
print(f"DBusException: {e}") | ||
|
||
def run(self): | ||
for service_type in SERVICE_TYPES: | ||
self.get_services(service_type) | ||
loop = GLib.MainLoop() | ||
loop.run() | ||
|
||
if __name__ == "__main__": | ||
browser = AvahiServiceBrowser() | ||
browser.run() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<interface> | ||
<requires lib="gtk+" version="3.0"/> | ||
<object class="GtkDialog" id="AvahiWarningDialog"> | ||
<property name="title" translatable="yes">Warning</property> | ||
<property name="modal">True</property> | ||
<property name="window_position">center-on-parent</property> | ||
<property name="default_width">350</property> | ||
<property name="default_height">150</property> | ||
|
||
<child internal-child="vbox"> | ||
<object class="GtkBox" id="dialog-vbox"> | ||
<property name="visible">True</property> | ||
<property name="orientation">vertical</property> | ||
<property name="spacing">6</property> | ||
|
||
<child> | ||
<object class="GtkImage" id="warning-icon"> | ||
<property name="icon_name">dialog-warning</property> | ||
<property name="visible">True</property> | ||
<property name="pixel_size">48</property> | ||
</object> | ||
</child> | ||
|
||
<child> | ||
<object class="GtkLabel" id="warning-message"> | ||
<property name="label" translatable="yes"> | ||
Avahi module is not installed. | ||
Service discovery will not work. | ||
</property> | ||
<property name="visible">True</property> | ||
<property name="wrap">True</property> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
|
||
<child internal-child="action_area"> | ||
<object class="GtkButtonBox" id="dialog-action_area"> | ||
<property name="visible">True</property> | ||
<property name="layout_style">end</property> | ||
<child> | ||
<object class="GtkButton" id="ok-button"> | ||
<property name="label" translatable="yes">OK</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">True</property> | ||
<signal name="clicked" handler="on_avahi_warning_close" swapped="no"/> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</interface> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<interface> | ||
<requires lib="gtk+" version="3.0"/> | ||
|
||
<object class="GtkDialog" id="DiscoveredPrintersDialog"> | ||
<property name="title" translatable="yes">Discovered Printers</property> | ||
<property name="modal">True</property> | ||
<property name="window_position">center-on-parent</property> | ||
<property name="default_width">400</property> | ||
<property name="default_height">300</property> | ||
|
||
<!-- VBox for the entire dialog content --> | ||
<child internal-child="vbox"> | ||
<object class="GtkBox" id="dialog-vbox"> | ||
<property name="visible">True</property> | ||
<property name="orientation">vertical</property> | ||
<property name="spacing">6</property> | ||
|
||
<!-- Scrollable area for the list of printers --> | ||
<child> | ||
<object class="GtkScrolledWindow" id="printer-scrolled-window"> | ||
<property name="visible">True</property> | ||
<property name="hexpand">True</property> | ||
<property name="vexpand">True</property> | ||
<child> | ||
<!-- FlowBox for single-column printer items --> | ||
<object class="GtkFlowBox" id="printers-flowbox"> | ||
<property name="visible">True</property> | ||
<property name="max_children_per_line">1</property> | ||
<property name="selection_mode">none</property> | ||
<property name="hexpand">True</property> | ||
<property name="vexpand">True</property> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
|
||
<!-- OK Button --> | ||
<child internal-child="action_area"> | ||
<object class="GtkButtonBox" id="dialog-action_area"> | ||
<property name="visible">True</property> | ||
<property name="layout_style">end</property> | ||
<child> | ||
<object class="GtkButton" id="buttonOk"> | ||
<property name="label" translatable="yes">_OK</property> | ||
<property name="visible">True</property> | ||
<signal name="clicked" handler="on_discovered_printers_dialog_close"/> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</interface> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<interface> | ||
<requires lib="gtk+" version="3.0"/> | ||
|
||
<object class="GtkBox" id="printer-item"> | ||
<property name="orientation">vertical</property> | ||
<property name="spacing">6</property> | ||
<property name="hexpand">True</property> | ||
<property name="visible">True</property> | ||
|
||
<!-- Horizontal box containing printer icon and details --> | ||
<child> | ||
<object class="GtkBox"> | ||
<property name="orientation">horizontal</property> | ||
<property name="spacing">10</property> | ||
<property name="hexpand">True</property> | ||
|
||
<!-- Printer icon --> | ||
<child> | ||
<object class="GtkImage" id="printer-icon"> | ||
<property name="icon_name">printer-symbolic</property> | ||
<property name="visible">True</property> | ||
<property name="pixel_size">48</property> | ||
</object> | ||
</child> | ||
|
||
<!-- Printer name label --> | ||
<child> | ||
<object class="GtkLabel" id="printer-name"> | ||
<property name="label">Printer Name</property> | ||
<property name="visible">True</property> | ||
<property name="hexpand">True</property> | ||
</object> | ||
</child> | ||
|
||
<!-- Open link button --> | ||
<child> | ||
<object class="GtkButton" id="open-link-button"> | ||
<property name="label" translatable="yes">Open Link</property> | ||
<property name="visible">True</property> | ||
<signal name="clicked" handler="on_open_link_clicked"/> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
|
||
<!-- Separator line --> | ||
<child> | ||
<object class="GtkSeparator" id="printer-item-separator"> | ||
<property name="orientation">horizontal</property> | ||
<property name="visible">True</property> | ||
<property name="margin_top">5</property> | ||
<property name="margin_bottom">5</property> | ||
</object> | ||
</child> | ||
</object> | ||
</interface> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these three lines be guarded by AVAHI_AVAILABLE as well? We call objects from Avahi at the method
run()
.