-
Notifications
You must be signed in to change notification settings - Fork 88
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
Adding Feature of IPP Service Discoveries #378
Open
TheJayas
wants to merge
2
commits into
OpenPrinting:master
Choose a base branch
from
TheJayas:ws1
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.
+265
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,82 @@ | ||
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 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 |
---|---|---|
|
@@ -28,6 +28,9 @@ | |
import _thread | ||
import dbus | ||
import gi | ||
import webbrowser | ||
import asyncio | ||
from ipp_pd_async import AvahiServiceBrowser | ||
try: | ||
gi.require_version('Polkit', '1.0') | ||
from gi.repository import Polkit | ||
|
@@ -216,11 +219,21 @@ def __init__(self): | |
"btnAddFirstPrinter", | ||
"btnStartService", | ||
"btnConnectNoService", | ||
"btnDiscoverPrinters", | ||
"statusbarMain", | ||
"toolbar", | ||
"server_menubar_item", | ||
"printer_menubar_item", | ||
"view_discovered_printers"], | ||
"DiscoveredPrintersDialog": | ||
["DiscoveredPrintersDialog", | ||
"printers-flowbox", | ||
"buttonOk"], | ||
"PrinterItem": [ | ||
"printer-item", | ||
"printer-icon", | ||
"printer-name", | ||
"open-link-button"], | ||
"AboutDialog": | ||
["AboutDialog"], | ||
"ConnectDialog": | ||
|
@@ -516,6 +529,7 @@ def __init__(self): | |
self.dests_iconview_drag_data_get) | ||
self.btnStartService.connect ('clicked', self.on_start_service_clicked) | ||
self.btnConnectNoService.connect ('clicked', self.on_connect_activate) | ||
self.btnDiscoverPrinters.connect ('clicked', self.on_discover_printers_button_click) | ||
self.btnAddFirstPrinter.connect ('clicked', | ||
self.on_new_printer_activate) | ||
|
||
|
@@ -543,9 +557,13 @@ def __init__(self): | |
elif len (self.printers) > 1: | ||
self.PrintersWindow.set_default_size (500, 180) | ||
|
||
|
||
self.PrintersWindow.show() | ||
|
||
self.builder = Gtk.Builder() | ||
self.service_browser = AvahiServiceBrowser() | ||
self.service_browser.run() | ||
self.discovered_services = self.service_browser.get_all_discovered_services() # To store discovered services | ||
|
||
def display_properties_dialog_for (self, queue): | ||
model = self.dests_iconview.get_model () | ||
iter = model.get_iter_first () | ||
|
@@ -614,6 +632,39 @@ def dests_iconview_item_activated (self, iconview, path): | |
self.monitor.update () | ||
return | ||
|
||
def on_discover_printers_button_click(self, widget): | ||
self.builder.add_from_file("ui/DiscoveredPrintersDialog.ui") | ||
self.discovered_printers_dialog = self.builder.get_object("DiscoveredPrintersDialog") | ||
self.discovered_services = self.service_browser.get_all_discovered_services() | ||
printers = self.discovered_services | ||
printers_flowbox = self.builder.get_object("printers-flowbox") | ||
for printer in printers: | ||
item_builder = Gtk.Builder() | ||
item_builder.add_from_file("ui/PrinterItem.ui") | ||
|
||
# Configure each printer item | ||
printer_item = item_builder.get_object("printer-item") | ||
printer_name_label = item_builder.get_object("printer-name") | ||
open_link_button = item_builder.get_object("open-link-button") | ||
|
||
printer_name_label.set_text(printer["name"]) | ||
open_link_button.connect("clicked", self.on_open_link_clicked, printer["link"]) | ||
|
||
# Add the item to the flowbox | ||
printers_flowbox.add(printer_item) | ||
printer_item.show_all() | ||
|
||
# Display the dialog | ||
self.discovered_printers_dialog.set_transient_for(self.PrintersWindow) | ||
self.discovered_printers_dialog.show_all() | ||
|
||
def on_open_link_clicked(self, widget, link): | ||
# Open the provided link in the web browser | ||
webbrowser.open(link) | ||
|
||
def on_discovered_printers_dialog_close(self, widget): | ||
self.discovered_printers_dialog.hide() | ||
|
||
def on_properties_dialog_closed (self, obj): | ||
self.sensitise_main_window_widgets () | ||
|
||
|
@@ -2278,4 +2329,4 @@ def main(show_jobs): | |
if opt == "--embedded": | ||
PlugWindowId = int(optarg) | ||
|
||
main(show_jobs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks this one is unneeded. |
||
main(show_jobs) |
This file contains 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 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">Website</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> |
This file contains 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
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.
Some distros currently do not ship Avahi Python module - can you catch the exception, show the error dialog if someone clicks on "Discover Services" about "Missing Avahi module - we could not discover mDNS services." and return back to the main window?
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.
Sure , I'll do require changes