summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/smpppdcs/smpppdcsprefsimpl.cpp
blob: 4dbedc549d64d7108b8187e155b22ad0acc9a0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
    smpppdcsprefsimpl.cpp
 
    Copyright (c) 2004-2006 by Heiko Schaefer        <heiko@rangun.de>
 
    Kopete    (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
 
    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; version 2 of the License.               *
    *                                                                       *
    *************************************************************************
*/

#include <arpa/inet.h>
#include <netdb.h>

#include <tqradiobutton.h>

#include <kstandarddirs.h>
#include <kapplication.h>
#include <kpushbutton.h>
#include <kresolver.h>
#include <knuminput.h>
#include <klocale.h>
#include <kdebug.h>

#include "kopetepluginmanager.h"

#include "../smpppdcsplugin.h"

#include "smpppdlocationwidget.h"
#include "smpppdcsprefsimpl.h"
#include "smpppdsearcher.h"

SMPPPDCSPrefs::SMPPPDCSPrefs(TQWidget* parent, const char* name, WFlags fl)
	: SMPPPDCSPrefsBase(parent, name, fl), m_plugin(NULL), m_scanProgressDlg(NULL), m_curSearcher(NULL) {

	// search for our main-plugin instance
	Kopete::Plugin * p = Kopete::PluginManager::self()->plugin("kopete_smpppdcs");
	if(p) {
		m_plugin = static_cast<SMPPPDCSPlugin *>(p);
	}

    // signals and slots connections
    connect(useNetstat, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(disableSMPPPDSettings()));
    connect(useSmpppd,  TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(enableSMPPPDSettings()));
    connect(autoCSTest, TQT_SIGNAL(clicked()),     this, TQT_SLOT(determineCSType()));
	
	if(m_plugin) {
		connect((TQObject *)SMPPPDLocation->server, TQT_SIGNAL(textChanged(const TQString&)),
				m_plugin, TQT_SLOT(smpppdServerChanged(const TQString&)));
	}

    // if netstat is NOT available, disable the option and set to SMPPPD
    if(KStandardDirs::findExe("netstat") == TQString::null) {
        autoCSTest->setEnabled(FALSE);
        useNetstat->setEnabled(FALSE);
        useNetstat->setChecked(FALSE);
        useSmpppd->setChecked(TRUE);
    }
}

SMPPPDCSPrefs::~SMPPPDCSPrefs() {
    delete m_scanProgressDlg;
}

void SMPPPDCSPrefs::determineCSType() {

    // while we search, we'll disable the button
    autoCSTest->setEnabled(false);
    //kapp->processEvents();

    /* broadcast network for a smpppd.
       If one is available set to smpppd method */

    SMPPPDSearcher searcher;
    m_curSearcher = &searcher;

    connect(&searcher, TQT_SIGNAL(smpppdFound(const TQString&)), this, TQT_SLOT(smpppdFound(const TQString&)));
    connect(&searcher, TQT_SIGNAL(smpppdNotFound()), this, TQT_SLOT(smpppdNotFound()));
    connect(&searcher, TQT_SIGNAL(scanStarted(uint)), this, TQT_SLOT(scanStarted(uint)));
    connect(&searcher, TQT_SIGNAL(scanProgress(uint)), this, TQT_SLOT(scanProgress(uint)));
    connect(&searcher, TQT_SIGNAL(scanFinished()), this, TQT_SLOT(scanFinished()));

    searcher.searchNetwork();
    m_curSearcher = NULL;
}

void SMPPPDCSPrefs::scanStarted(uint total) {
    kdDebug(14312) << k_funcinfo << "Scanning for a SMPPPD started. Will scan " << total << " IPs" << endl;

    // setup the scanProgress Dialog
    if(!m_scanProgressDlg) {
        m_scanProgressDlg = new KProgressDialog(this, 0, i18n("Searching"), i18n("Searching for a SMPPPD on the local network..."), TRUE);
        m_scanProgressDlg->setAutoClose(TRUE);
        m_scanProgressDlg->setAllowCancel(TRUE);
        m_scanProgressDlg->setMinimumDuration(2000);

        connect(m_scanProgressDlg, TQT_SIGNAL(cancelClicked()), this, TQT_SLOT(cancelScanning()));
    }
    m_scanProgressDlg->progressBar()->setTotalSteps(total);
    m_scanProgressDlg->progressBar()->setProgress(0);
    m_scanProgressDlg->show();
}

void SMPPPDCSPrefs::scanProgress(uint cur) {
    m_scanProgressDlg->progressBar()->setProgress(cur);
    kapp->processEvents();
}

void SMPPPDCSPrefs::cancelScanning() {
    kdDebug(14312) << k_funcinfo << endl;
    Q_ASSERT(m_curSearcher);
    m_curSearcher->cancelSearch();
}

void SMPPPDCSPrefs::smpppdFound(const TQString& host) {
    kdDebug(14312) << k_funcinfo << endl;

    TQString myHost = host;

    // try to get the domain name
    struct in_addr addr;
    if(inet_aton(host.ascii(), &addr)) {
        struct hostent * hostEnt = gethostbyaddr(&addr.s_addr, sizeof(addr.s_addr), AF_INET);
        if(hostEnt) {
            myHost = hostEnt->h_name;
        } else {
#ifndef NDEBUG
            switch(h_errno) {
            case HOST_NOT_FOUND:
                kdDebug(14312) << k_funcinfo << "No such host is known in the database." << endl;
                break;
            case TRY_AGAIN:
                kdDebug(14312) << k_funcinfo << "Couldn't contact DNS server." << endl;
                break;
            case NO_RECOVERY:
                kdDebug(14312) << k_funcinfo << "A non-recoverable error occurred." << endl;
                break;
            case NO_ADDRESS:
                kdDebug(14312) << k_funcinfo << "The host database contains an entry for the name, but it doesn't have an associated Internet address." << endl;
                break;
            }
#endif

        }
    }

    SMPPPDLocation->setServer(myHost);
    useNetstat->setChecked(false);
    useSmpppd->setChecked(true);
    autoCSTest->setEnabled(true);
}

void SMPPPDCSPrefs::smpppdNotFound() {
    kdDebug(14312) << k_funcinfo << endl;
    useNetstat->setChecked(true);
    useSmpppd->setChecked(false);
    autoCSTest->setEnabled(true);
}

#include "smpppdcsprefsimpl.moc"