Diffstat (limited to 'noncore/unsupported/mail2/libmail/imapbase.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/mail2/libmail/imapbase.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/noncore/unsupported/mail2/libmail/imapbase.cpp b/noncore/unsupported/mail2/libmail/imapbase.cpp new file mode 100644 index 0000000..9a2ba47 --- a/dev/null +++ b/noncore/unsupported/mail2/libmail/imapbase.cpp | |||
@@ -0,0 +1,110 @@ | |||
1 | #include <qsocket.h> | ||
2 | #include <qtimer.h> | ||
3 | |||
4 | #include "imapbase.h" | ||
5 | |||
6 | IMAPBase::IMAPBase(const Account &account) | ||
7 | : QObject(), _account(account) | ||
8 | { | ||
9 | _connected = false; | ||
10 | _writingAllowed = false; | ||
11 | _socket = new QSocket(this); | ||
12 | |||
13 | connect(_socket, SIGNAL(readyRead()), SLOT(slotDataAvailiable())); | ||
14 | connect(_socket, SIGNAL(hostFound()), SLOT(slotHostFound())); | ||
15 | connect(_socket, SIGNAL(connected()), SLOT(slotConnected())); | ||
16 | connect(_socket, SIGNAL(connectionClosed()), SLOT(slotDisconnected())); | ||
17 | connect(_socket, SIGNAL(error(int)), SLOT(slotError(int))); | ||
18 | |||
19 | QTimer *commandTimer = new QTimer(this); | ||
20 | commandTimer->start(200); | ||
21 | connect(commandTimer, SIGNAL(timeout()), SLOT(writeCommands())); | ||
22 | } | ||
23 | |||
24 | void IMAPBase::sendCommand(const QString &command) | ||
25 | { | ||
26 | if (!_connected) makeConnect(); | ||
27 | _commandQueue.append(command); | ||
28 | } | ||
29 | |||
30 | void IMAPBase::disconnect() | ||
31 | { | ||
32 | _connected = false; | ||
33 | delete _socket; | ||
34 | emit disconnected(); | ||
35 | } | ||
36 | |||
37 | void IMAPBase::makeConnect() | ||
38 | { | ||
39 | emit lookingUpHost(); | ||
40 | if (_socket == NULL) _socket = new QSocket(this); | ||
41 | |||
42 | Q_UINT16 port = _account.imapPort().toUInt(); | ||
43 | _socket->connectToHost(_account.imapServer(), port); | ||
44 | } | ||
45 | |||
46 | void IMAPBase::writeCommands() | ||
47 | { | ||
48 | if (!_connected) return; | ||
49 | if (_commandQueue.isEmpty()) return; | ||
50 | if (!_writingAllowed) return; | ||
51 | |||
52 | QStringList::Iterator it; | ||
53 | for (it = _commandQueue.begin(); it != _commandQueue.end(); it++) { | ||
54 | if (!(*it).isEmpty() && _writingAllowed) { | ||
55 | #ifndef QT_NO_DEBUG | ||
56 | qDebug("IMAP > " + (*it).stripWhiteSpace()); | ||
57 | #endif | ||
58 | _socket->writeBlock((*it).latin1(), (*it).length()); | ||
59 | _writingAllowed = false; | ||
60 | _commandQueue.remove(it); | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void IMAPBase::slotError(int err) | ||
67 | { | ||
68 | if (err == QSocket::ErrConnectionRefused) { | ||
69 | emit error(IMAPErrConnectionRefused); | ||
70 | } else if (err == QSocket::ErrHostNotFound) { | ||
71 | emit error(IMAPErrHostNotFound); | ||
72 | } else if (err == QSocket::ErrSocketRead) { | ||
73 | emit error(IMAPErrSocketRead); | ||
74 | } else { | ||
75 | emit error(IMAPErrUnknownError); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | void IMAPBase::slotHostFound() | ||
80 | { | ||
81 | emit hostFound(); | ||
82 | } | ||
83 | |||
84 | void IMAPBase::slotConnected() | ||
85 | { | ||
86 | _connected = true; | ||
87 | emit connected(); | ||
88 | } | ||
89 | |||
90 | void IMAPBase::slotDisconnected() | ||
91 | { | ||
92 | _connected = false; | ||
93 | emit disconnected(); | ||
94 | } | ||
95 | |||
96 | void IMAPBase::slotDataAvailiable() | ||
97 | { | ||
98 | while (_socket->canReadLine()) { | ||
99 | _data += _socket->readLine(); | ||
100 | if (_socket->atEnd()) { | ||
101 | #ifndef QT_NO_DEBUG | ||
102 | qDebug("IMAP < " + _data.stripWhiteSpace()); | ||
103 | #endif | ||
104 | emit dataReceived(_data); | ||
105 | _writingAllowed = true; | ||
106 | _data = QString(0); | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | |||