Forwardport r1147052:
[kdepim.git] / knotes / knotehostdlg.cpp
blobda4dd9b9bd561312aff402f521e2dd28f16ee952
1 /*******************************************************************
2 KNotes -- Notes for the KDE project
4 Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
5 2004, Michael Brade <brade@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
31 *******************************************************************/
33 #include "knotehostdlg.h"
34 #include "knotesglobalconfig.h"
36 #include <kconfig.h>
37 #include <kdebug.h>
38 #include <kglobal.h>
39 #include <khistorycombobox.h>
40 #include <klocale.h>
41 #include <kstandarddirs.h>
42 #include <kvbox.h>
43 #include <dnssd/servicemodel.h>
44 #include <dnssd/servicebrowser.h>
46 #include <QLabel>
47 #include <QString>
48 #include <QLineEdit>
49 #include <QTableView>
50 #include <QSortFilterProxyModel>
53 KNoteHostDlg::KNoteHostDlg( const QString &caption, QWidget *parent )
54 : KDialog( parent )
56 setCaption( caption );
57 setButtons( Ok|Cancel );
58 KVBox *page = new KVBox( this );
59 setMainWidget( page );
60 ( void ) new QLabel( i18n("Select recipient:"), page );
62 m_servicesView = new QTableView( page );
63 m_servicesView->setShowGrid( false );
64 DNSSD::ServiceModel* mdl = new DNSSD::ServiceModel( new DNSSD::ServiceBrowser( "_knotes._tcp", true ) );
65 mdl->setParent( m_servicesView );
66 m_servicesView->setModel( mdl );
67 m_servicesView->setSelectionBehavior( QAbstractItemView::SelectRows );
68 m_servicesView->hideColumn( DNSSD::ServiceModel::Port );
69 connect( m_servicesView->selectionModel(), SIGNAL( currentRowChanged( const QModelIndex&, const QModelIndex& ) ),
70 SLOT( serviceSelected( const QModelIndex& ) ) );
71 connect( m_servicesView, SIGNAL( activated( const QModelIndex& ) ),
72 SLOT( serviceSelected( const QModelIndex& ) ) );
73 connect( m_servicesView, SIGNAL( clicked( const QModelIndex& ) ),
74 SLOT( serviceSelected( const QModelIndex& ) ) );
76 ( void ) new QLabel( i18n("Hostname or IP address:"), page );
78 m_hostCombo = new KHistoryComboBox( true, page );
79 m_hostCombo->setMinimumWidth( fontMetrics().maxWidth() * 15 );
80 m_hostCombo->setDuplicatesEnabled( false );
82 // Read known hosts from configfile
83 m_hostCombo->setHistoryItems( KNotesGlobalConfig::knownHosts(), true );
84 m_hostCombo->setFocus();
85 connect( m_hostCombo->lineEdit(), SIGNAL( textChanged ( const QString & ) ),
86 this, SLOT( slotTextChanged( const QString & ) ) );
87 slotTextChanged( m_hostCombo->lineEdit()->text() );
90 KNoteHostDlg::~KNoteHostDlg()
92 if ( result() == Accepted ) {
93 m_hostCombo->addToHistory( m_hostCombo->currentText().trimmed() );
96 // Write known hosts to configfile
97 KNotesGlobalConfig::setKnownHosts( m_hostCombo->historyItems() );
98 KNotesGlobalConfig::self()->writeConfig();
101 void KNoteHostDlg::slotTextChanged( const QString &text )
103 enableButton( Ok, !text.isEmpty() );
106 void KNoteHostDlg::serviceSelected( const QModelIndex& idx )
108 DNSSD::RemoteService::Ptr srv=idx.data( DNSSD::ServiceModel::ServicePtrRole ).value<DNSSD::RemoteService::Ptr>();
109 m_hostCombo->lineEdit()->setText( srv->hostName() + ":" + QString::number( srv->port() ) );
112 QString KNoteHostDlg::host() const
114 return m_hostCombo->currentText().section( ':', 0, 0 );
117 quint16 KNoteHostDlg::port() const
119 return m_hostCombo->currentText().section( ':', 1 ).toUShort();
122 #include "knotehostdlg.moc"