Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ nobase_pkgdata_DATA= \
gui.py \
gtkinklevel.py \
installpackage.py \
ipp_pd_async.py \
jobviewer.py \
killtimer.py \
monitor.py \
Expand All @@ -135,6 +136,8 @@ nobase_pkgdata_DATA= \
userdefault.py \
ui/AboutDialog.ui \
ui/ConnectDialog.ui \
ui/DiscoveredPrintersDialog.ui \
ui/PrinterItem.ui \
ui/ConnectingDialog.ui \
ui/InstallDialog.ui \
ui/JobsWindow.ui \
Expand Down
82 changes: 82 additions & 0 deletions ipp_pd_async.py
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
Copy link
Member

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?

Copy link
Author

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


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()
55 changes: 53 additions & 2 deletions system-config-printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 ()
Expand Down Expand Up @@ -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 ()

Expand Down Expand Up @@ -2278,4 +2329,4 @@ def main(show_jobs):
if opt == "--embedded":
PlugWindowId = int(optarg)

main(show_jobs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks this one is unneeded.

main(show_jobs)
55 changes: 55 additions & 0 deletions ui/DiscoveredPrintersDialog.ui
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>
57 changes: 57 additions & 0 deletions ui/PrinterItem.ui
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>
15 changes: 15 additions & 0 deletions ui/PrintersWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnDiscoverPrinters">
<property name="label" translatable="yes">_Discover Printers</property>
<property name="use_underline">True</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="clicked" handler="on_discover_printers_button_click" swapped="no"/>
</object>

<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnConnectNoService">
<property name="label" translatable="yes">Connect</property>
Expand Down