Skip to content

Commit

Permalink
feat: discover instances button in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 26, 2024
1 parent b23c0e0 commit 147de86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
26 changes: 26 additions & 0 deletions qtribu/gui/dlg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__(self, parent=None):
# self.lne_qchat_instance_uri.setValidator(QVAL_URL)
self.btn_rules.pressed.connect(self.show_instance_rules)
self.btn_rules.setIcon(QIcon(QgsApplication.iconPath("processingResult.svg")))
self.btn_discover.pressed.connect(self.discover_instances)
self.btn_discover.setIcon(QIcon(QgsApplication.iconPath("mIconListView.svg")))

# customization
self.btn_help.setIcon(QIcon(QgsApplication.iconPath("mActionHelpContents.svg")))
Expand Down Expand Up @@ -178,6 +180,9 @@ def load_settings(self) -> None:
self.lbl_version_saved_value.setText(settings.version)

def show_instance_rules(self) -> None:
"""
Action called when clicking on the "Instance rules" button
"""
instance_url = self.cbb_qchat_instance_uri.currentText()
try:
client = QChatApiClient(instance_url)
Expand Down Expand Up @@ -206,6 +211,27 @@ def show_instance_rules(self) -> None:
except Exception as e:
self.log(message=str(e), log_level=Qgis.Critical)

def discover_instances(self) -> None:
"""
Action called when clicking on the "Discover instances" button
"""
try:
client = QChatApiClient(self.cbb_qchat_instance_uri.currentText())
instances = client.get_registered_instances()
msg = ""
for lang, lang_instances in instances.items():
msg += f"[{lang}]:\n"
for li in lang_instances:
msg += f"- {li}\n"
msg += "\n"
QMessageBox.information(
self,
self.tr("Registered instances"),
msg,
)
except Exception as e:
self.log(message=str(e), log_level=Qgis.Critical)

def on_ring_tone_changed(self) -> None:
"""
Action called when ringtone value is changed
Expand Down
16 changes: 7 additions & 9 deletions qtribu/gui/dlg_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,6 @@
</property>
</widget>
</item>
<item>
<widget class="QgsSymbolButton" name="mSymbolButton"/>
</item>
<item>
<widget class="QComboBox" name="cbb_qchat_instance_uri">
<property name="sizePolicy">
Expand Down Expand Up @@ -348,6 +345,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_discover">
<property name="text">
<string>Discover instances</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -754,11 +758,6 @@
<extends>QToolButton</extends>
<header>qgscolorbutton.h</header>
</customwidget>
<customwidget>
<class>QgsSymbolButton</class>
<extends>QToolButton</extends>
<header>qgssymbolbutton.h</header>
</customwidget>
<customwidget>
<class>AuthoringWidget</class>
<extends>QWidget</extends>
Expand All @@ -774,7 +773,6 @@
<tabstop>btn_reset_read_history</tabstop>
<tabstop>chb_integration_news_feed</tabstop>
<tabstop>chb_license_global_accept</tabstop>
<tabstop>mSymbolButton</tabstop>
<tabstop>cbb_qchat_instance_uri</tabstop>
<tabstop>btn_rules</tabstop>
<tabstop>ckb_show_avatars</tabstop>
Expand Down
12 changes: 12 additions & 0 deletions qtribu/logic/qchat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

CONTENT_TYPE_JSON = "application/json"

INSTANCES_JSON_URL = "https://github.com/geotribu/gischat/raw/main/instances.json"


class QChatApiClient:
"""
Expand All @@ -29,6 +31,16 @@ def __init__(
self.instance_uri = instance_uri
self.qntwk = NetworkRequestsManager()

def get_registered_instances(self) -> dict[str, list[str]]:
response: QByteArray = self.qntwk.get_from_source(
headers=HEADERS,
url=INSTANCES_JSON_URL,
response_expected_content_type="text/plain; charset=utf-8",
use_cache=False,
)
data = json.loads(str(response, "UTF8"))
return data

def get_status(self) -> dict[str, Any]:
"""
Get instance status with an API call
Expand Down

0 comments on commit 147de86

Please sign in to comment.