File indexing completed on 2024-12-15 04:51:47
0001 /******************************************************************* 0002 KNotes -- Notes for the KDE project 0003 0004 SPDX-FileCopyrightText: 2003 Daniel Martin <daniel.martin@pirack.com> 0005 SPDX-FileCopyrightText: 2004 Michael Brade <brade@kde.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 *******************************************************************/ 0009 0010 #include "notehostdialog.h" 0011 #include "notesharedglobalconfig.h" 0012 0013 #include <KDNSSD/ServiceBrowser> 0014 #include <KDNSSD/ServiceModel> 0015 #include <KHistoryComboBox> 0016 #include <KLocalizedString> 0017 0018 #include <QDialogButtonBox> 0019 #include <QLabel> 0020 #include <QLineEdit> 0021 #include <QPushButton> 0022 #include <QTreeView> 0023 #include <QVBoxLayout> 0024 0025 using namespace NoteShared; 0026 0027 NoteHostDialog::NoteHostDialog(const QString &caption, QWidget *parent) 0028 : QDialog(parent) 0029 , m_hostCombo(new KHistoryComboBox(true, this)) 0030 , m_servicesView(new QTreeView(this)) 0031 { 0032 setWindowTitle(caption); 0033 0034 auto mainLayout = new QVBoxLayout(this); 0035 0036 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0037 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0038 mOkButton->setDefault(true); 0039 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0040 connect(buttonBox, &QDialogButtonBox::accepted, this, &NoteHostDialog::accept); 0041 connect(buttonBox, &QDialogButtonBox::rejected, this, &NoteHostDialog::reject); 0042 0043 auto label = new QLabel(i18n("Select recipient:"), this); 0044 mainLayout->addWidget(label); 0045 0046 m_servicesView->setRootIsDecorated(false); 0047 auto mdl = new KDNSSD::ServiceModel(new KDNSSD::ServiceBrowser(QStringLiteral("_knotes._tcp"), true), this); 0048 m_servicesView->setModel(mdl); 0049 m_servicesView->setSelectionBehavior(QAbstractItemView::SelectRows); 0050 m_servicesView->hideColumn(KDNSSD::ServiceModel::Port); 0051 0052 connect(m_servicesView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &NoteHostDialog::serviceSelected); 0053 connect(m_servicesView, &QTreeView::activated, this, &NoteHostDialog::serviceSelected); 0054 connect(m_servicesView, &QTreeView::clicked, this, &NoteHostDialog::serviceSelected); 0055 connect(m_servicesView, &QTreeView::doubleClicked, this, &NoteHostDialog::slotServiceDoubleClicked); 0056 0057 mainLayout->addWidget(m_servicesView); 0058 0059 label = new QLabel(i18n("Hostname or IP address:"), this); 0060 mainLayout->addWidget(label); 0061 0062 mainLayout->addWidget(m_hostCombo); 0063 m_hostCombo->setMinimumWidth(fontMetrics().maxWidth() * 15); 0064 m_hostCombo->setDuplicatesEnabled(false); 0065 0066 // Read known hosts from configfile 0067 m_hostCombo->setHistoryItems(NoteShared::NoteSharedGlobalConfig::knownHosts(), true); 0068 m_hostCombo->setFocus(); 0069 0070 mainLayout->addWidget(buttonBox); 0071 0072 connect(m_hostCombo->lineEdit(), &QLineEdit::textChanged, this, &NoteHostDialog::slotTextChanged); 0073 slotTextChanged(m_hostCombo->lineEdit()->text()); 0074 readConfig(); 0075 } 0076 0077 NoteHostDialog::~NoteHostDialog() 0078 { 0079 if (result() == Accepted) { 0080 m_hostCombo->addToHistory(m_hostCombo->currentText().trimmed()); 0081 } 0082 0083 // Write known hosts to configfile 0084 NoteShared::NoteSharedGlobalConfig::setKnownHosts(m_hostCombo->historyItems()); 0085 NoteShared::NoteSharedGlobalConfig::setNoteHostDialogSize(size()); 0086 NoteShared::NoteSharedGlobalConfig::setNoteHostDialogSize(size()); 0087 NoteShared::NoteSharedGlobalConfig::self()->save(); 0088 } 0089 0090 void NoteHostDialog::readConfig() 0091 { 0092 const QSize size = NoteShared::NoteSharedGlobalConfig::noteHostDialogSize(); 0093 if (size.isValid()) { 0094 resize(size); 0095 } 0096 } 0097 0098 void NoteHostDialog::slotTextChanged(const QString &text) 0099 { 0100 mOkButton->setEnabled(!text.isEmpty()); 0101 } 0102 0103 void NoteHostDialog::serviceSelected(const QModelIndex &idx) 0104 { 0105 auto srv = idx.data(KDNSSD::ServiceModel::ServicePtrRole).value<KDNSSD::RemoteService::Ptr>(); 0106 m_hostCombo->lineEdit()->setText(srv->hostName() + QLatin1StringView(":") + QString::number(srv->port())); 0107 } 0108 0109 QString NoteHostDialog::host() const 0110 { 0111 return m_hostCombo->currentText().section(QLatin1Char(':'), 0, 0); 0112 } 0113 0114 quint16 NoteHostDialog::port() const 0115 { 0116 return m_hostCombo->currentText().section(QLatin1Char(':'), 1).toUShort(); 0117 } 0118 0119 void NoteHostDialog::slotServiceDoubleClicked(const QModelIndex &idx) 0120 { 0121 auto srv = idx.data(KDNSSD::ServiceModel::ServicePtrRole).value<KDNSSD::RemoteService::Ptr>(); 0122 m_hostCombo->lineEdit()->setText(srv->hostName() + QLatin1Char(':') + QString::number(srv->port())); 0123 accept(); 0124 } 0125 0126 #include "moc_notehostdialog.cpp"