summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-console/dialer.cpp
blob: 8883b8347e506cf3f3ab18595c71e1078309d0cb (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "dialer.h"
#include "io_modem.h"

/* QT */
#include <qlayout.h>
#include <qprogressbar.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qapp.h>
#include <qtimer.h>
#include <qmessagebox.h>

/* STD */
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

// State machine:          | When an error occurs, we don't have to
//                         | reset everything.
// (init) <------+         | But if the user wants to reset,
//   |           |         | we stop dialing immediately.
//   v           |         |
// (options) ----+         | Following the state machine is necessary
//   |       \             | to get determinable results.
//   v        ^            |
// (dial) ----+            |
//   |        ^            |
//   v        |            |
// (online) --+            |
//   |                     |
//   v                     |


// from atconfigdialog
//initStringLine->setText( config.readEntry("InitString", MODEM_DEFAULT_INIT_STRING ) );
//resetStringLine->setText( config.readEntry("ResetString", MODEM_DEFAULT_RESET_STRING ) );
//dialPref1Line->setText( config.readEntry("DialPrefix1", MODEM_DEFAULT_DIAL_PREFIX1 ) );
//dialSuf1Line->setText( config.readEntry("DialSuffix1", MODEM_DEFAULT_DIAL_SUFFIX1 ) );
//dialPref2Line->setText( config.readEntry("DialPrefix2", MODEM_DEFAULT_DIAL_PREFIX1 ) );
//dialSuf2Line->setText( config.readEntry("DialSuffix2", MODEM_DEFAULT_DIAL_SUFFIX1 ) );
//dialPref3Line->setText( config.readEntry("DialPrefix3", MODEM_DEFAULT_DIAL_PREFIX1 ) );
//dialSuf3Line->setText( config.readEntry("DialSuffix3", MODEM_DEFAULT_DIAL_SUFFIX1 ) );
//connectLine->setText( config.readEntry("DefaultConnect" MODEM_DEFAULT_CONNECT_STRING ) );
//hangupLine->setText( config.readEntry("HangupString", MODEM_DEFAULT_HANGUP_STRING ) );

// from modemconfigwidget
//int rad_flow = prof.readNumEntry("Flow");
//int rad_parity = prof.readNumEntry("Parity");
//int speed = prof.readNumEntry("Speed");
//QString number = prof.readEntry("Number");

Dialer::Dialer(const Profile& profile, int fd, QWidget *parent, const char *name)
: QDialog(parent, name, true), m_fd(fd), m_profile(profile)
{
	QVBoxLayout *vbox;
	QLabel *desc;


	usercancel = 0;
	cleanshutdown = 0;


	desc = new QLabel(QObject::tr("Dialing number: %1").arg(m_profile.readEntry("Number")), this);
	progress = new QProgressBar(this);
	status = new QLabel("", this);
	status->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	cancel = new QPushButton(QObject::tr("Cancel"), this);

	vbox = new QVBoxLayout(this, 2);
	vbox->add(desc);
	vbox->add(progress);
	vbox->add(status);
	vbox->add(cancel);

	connect(cancel, SIGNAL(clicked()), SLOT(slotCancel()));

	show();

	QTimer::singleShot(500, this, SLOT(slotAutostart()));
}

Dialer::~Dialer()
{
}

void Dialer::setHangupOnly()
{
	state = state_cancel;
	usercancel = 1;
        send( m_profile.readEntry("HangupString", MODEM_DEFAULT_HANGUP_STRING )+"\r" );
}

void Dialer::slotCancel()
{
	if(state != state_online)
	{
		usercancel = 1;
		reset();
	}
	else {
            accept();
        }
}

void Dialer::reset()
{
	switchState(state_cancel);
}

void Dialer::slotAutostart()
{
	//state = state_preinit;
	dial(m_profile.readEntry("Number"));
}

void Dialer::dial(const QString& number)
{
	while(state != state_online)
	{
		if(!usercancel)
		{
			state = state_preinit;
			trydial(number);
		}
		else break;
	}

	if(usercancel)
	{
		// modem hangup
		trydial(QString::null);
		reject();
	}
}

void Dialer::trydial(const QString& number)
{
	if(state != state_cancel) switchState(state_preinit);
	if(cleanshutdown)
	{
            send(m_profile.readEntry("HangupString", MODEM_DEFAULT_HANGUP_STRING ) + "\r");
	}

	if(state != state_cancel)
	{
		switchState(state_init);
		send(m_profile.readEntry("InitString",MODEM_DEFAULT_INIT_STRING ) + "\r");
		QString response2 = receive();
		if(!response2.contains("\nOK\r"))
			reset();
	}

/*	if(state != state_cancel)
	{
		switchState(state_options);

                owarn << "ATM3l3" << oendl; 
		send("ATM3L3\r");
		QString response3 = receive();
		if(!response3.contains("\nOK\r"))
			reset();
	}
*/

	if(state != state_cancel)
	{
		switchState(state_dialtone);

		send("ATX1\r");
		QString response4 = receive();
		if(!response4.contains("\nOK\r"))
			reset();
	}

	if(state != state_cancel)
	{
		switchState(state_dialing);

//		send(QString("ATDT %1\r").arg(number));
		send(QString("%1 %2\r").arg(m_profile.readEntry("DialPrefix1", MODEM_DEFAULT_DIAL_PREFIX1 ))
                     .arg(number));

		QString response5 = receive();
		if(!response5.contains("CONNECT") )
		{
			if(response5.contains("BUSY"))
				switchState(state_dialing);
			else
			{
				QMessageBox::warning(this,
					QObject::tr("Failure"),
					QObject::tr("<qt>Dialing the number failed.</qt>"));
				slotCancel();
			}
		}
	}


	if(state != state_cancel)
	{
            state = state_online;
            slotCancel();
	}
}

void Dialer::send(const QString& msg)
{
	QString m = msg;
	int bytes;
	QString termination;


/*	termination = "\r";
	//termination = m_profile.readEntry("Termination");
	if(termination == "\n") m = m + "\n";
	else if(termination == "\r") m = m + "\r";
	else m = m + "\r\n";
*/
        m = m.replace(QRegExp("\n"), "\r");

	bytes = ::write(m_fd, m.local8Bit(), strlen(m.local8Bit()));
	if(bytes < 0)
	{
		reset();
	}
}

QString Dialer::receive()
{
	QString buf;
	char buffer[1024];
	int ret;
	int counter = 0;

	while(1)
	{
		ret = ::read(m_fd, buffer, sizeof(buffer));

		if(ret > 0)
		{
			for(int i = 0; i < ret; i++)
				buffer[i] = buffer[i] & 0x7F;
			buffer[ret] = 0;
			buf.append(QString(buffer));
			if(buf.contains("OK") || buf.contains("ERROR") || buf.contains("CONNECT") || (buf.contains("BUSY")))
			{
				cleanshutdown = 1;
				return buf;
			}else if (buf.contains("NO CARRIER") || buf.contains("NO DIALTONE")  ) {
                            cleanshutdown = 1;
                            return QString::null;
                        }
		}
		else if(ret < 0)
		{
			if(errno != EAGAIN) reset();
			else if(!(counter++ % 100)) qApp->processEvents();
		}
		else if(!(counter++ % 100)) qApp->processEvents();

		if(usercancel) return QString::null;
	}

	cleanshutdown = 1;
	return QString::null;
}

void Dialer::switchState(int newstate)
{
	int oldstate = state;
	state = newstate;

	switch(state)
	{
		case state_cancel:
			status->setText(QObject::tr("Cancelling..."));
			progress->setProgress(0);
			break;
		case state_preinit:
			status->setText(QObject::tr("Searching modem"));
			progress->setProgress(10);
			break;
		case state_init:
			status->setText(QObject::tr("Initializing..."));
			progress->setProgress(20);
			break;
		case state_options:
			status->setText(QObject::tr("Reset speakers"));
			progress->setProgress(30);
			break;
		case state_dialtone:
			status->setText(QObject::tr("Turning off dialtone"));
			progress->setProgress(40);
			break;
		case state_dialing:
			if(oldstate != state_dialing) status->setText(QObject::tr("Dial number"));
			else status->setText(QObject::tr("Line busy, redialing number"));
			progress->setProgress(50);
			break;
		case state_online:
			status->setText(QObject::tr("Connection established"));
			progress->setProgress(100);
			cancel->setText(QObject::tr("Dismiss"));
			break;
	}
}