summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/meanwhile/meanwhileaccount.cpp
blob: f96b10f85114cbae0e1dcca3eb48026d3ee4a380 (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
/*
    meanwhileaccount.cpp - a meanwhile account

    Copyright (c) 2003-2004 by Sivaram Gottimukkala  <suppandi@gmail.com>
    Copyright (c) 2005      by Jeremy Kerr <jk@ozlabs.org>

    Kopete    (c) 2002-2004 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; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/
#include <signal.h>
#include "meanwhileprotocol.h"
#include "meanwhileplugin.h"
#include "meanwhileaccount.h"
#include "meanwhilecontact.h"
#include "meanwhilesession.h"
#include "kopetechatsession.h"
#include "kopetecontactlist.h"
#include "kopetepassword.h"

#include <kaction.h>
#include <kpopupmenu.h>
#include <klocale.h>
#include <kconfigbase.h>
#include "kopeteaway.h"
#include <kinputdialog.h>
#include <kmessagebox.h>
#include <tqdict.h>

MeanwhileAccount::MeanwhileAccount(
                        MeanwhileProtocol *tqparent,
                        const TQString &accountID,
                        const char *name)
    : Kopete::PasswordedAccount(tqparent, accountID, 0, name)
{
    HERE;
    m_meanwhileId = accountID;
    m_session = 0L;
    setMyself(new MeanwhileContact(m_meanwhileId, m_meanwhileId, this,
                Kopete::ContactList::self()->myself()));
    setOnlineStatus(tqparent->statusOffline);
    infoPlugin = new MeanwhilePlugin();
}

MeanwhileAccount::~MeanwhileAccount()
{
    if (m_session)
        delete m_session;
}

void MeanwhileAccount::setPlugin(MeanwhilePlugin *plugin)
{
    delete infoPlugin;
    infoPlugin = plugin;
}

bool MeanwhileAccount::createContact(
                        const TQString & contactId ,
                        Kopete::MetaContact * parentContact)
{
    MeanwhileContact* newContact = new MeanwhileContact(contactId,
            parentContact->displayName(), this, parentContact);

    MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());

    if ((newContact != 0L) && (m_session != 0L)
        && (myself()->onlinetqStatus() !=
                p->statusOffline))
        m_session->addContact(newContact);

    return newContact != 0L;
}

void MeanwhileAccount::connectWithPassword(const TQString &password)
{
    if (password.isEmpty()) {
        disconnect(Kopete::Account::Manual);
        return;
    }

    if (m_session == 0L) {
        m_session = new MeanwhileSession(this);
        if (m_session == 0L) {
            mwDebug() << "session creation failed" << endl;
            return;
        }

        TQObject::connect(m_session,
                TQT_SIGNAL(sessionStateChange(Kopete::OnlineStatus)),
                this, TQT_SLOT(slotSessionStateChange(Kopete::OnlineStatus)));
        TQObject::connect(m_session,
                TQT_SIGNAL(serverNotification(const TQString &)),
                this, TQT_SLOT(slotServerNotification(const TQString&)));

    }

    if (m_session == 0L) {
        mwDebug() << "No memory for session" << endl;
        return;
    }

    if (!m_session->isConnected() && !m_session->isConnecting())
        m_session->connect(password);

    m_session->setStatus(initialtqStatus());
}

void MeanwhileAccount::disconnect()
{
    disconnect(Manual);
}

void MeanwhileAccount::disconnect(Kopete::Account::DisconnectReason reason)
{
    if (m_session == 0L)
        return;

    MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());
    setAllContactstqStatus(p->statusOffline);
    disconnected(reason);
    emit isConnectedChanged();

    delete m_session;
    m_session = 0L;
}

KActionMenu * MeanwhileAccount::actionMenu()
{
    KActionMenu *menu = Kopete::Account::actionMenu();

    menu->popupMenu()->insertSeparator();

#if 0
    menu->insert(new KAction(i18n("&Change tqStatus Message"), TQString(), 0,
                this, TQT_SLOT(meanwhileChangetqStatus()), this,
                "meanwhileChangetqStatus"));
    //infoPlugin->addCustomMenus(theMenu);
#endif
    return menu;
}

TQString MeanwhileAccount::getServerName()
{
    return configGroup()->readEntry("Server");
}

int MeanwhileAccount::getServerPort()
{
    return configGroup()->readNumEntry("Port");
}

void MeanwhileAccount::setServerName(const TQString &server)
{
    configGroup()->writeEntry("Server", server);
}

void MeanwhileAccount::setServerPort(int port)
{
    configGroup()->writeEntry("Port", port);
}

void MeanwhileAccount::setClientID(int client, int major, int minor)
{
    configGroup()->writeEntry("clientID", client);
    configGroup()->writeEntry("clientVersionMajor", major);
    configGroup()->writeEntry("clientVersionMinor", minor);
}

void MeanwhileAccount::resetClientID()
{
    configGroup()->deleteEntry("clientID");
    configGroup()->deleteEntry("clientVersionMajor");
    configGroup()->deleteEntry("clientVersionMinor");
}

bool MeanwhileAccount::getClientIDParams(int *clientID,
	int *verMajor, int *verMinor)
{
    bool custom_id = configGroup()->hasKey("clientID");

    MeanwhileSession::getDefaultClientIDParams(clientID, verMajor, verMinor);

    if (custom_id) {
	*clientID = configGroup()->readUnsignedNumEntry("clientID", *clientID);
	*verMajor = configGroup()->readUnsignedNumEntry("clientVersionMajor",
		    *verMinor);
	*verMinor = configGroup()->readUnsignedNumEntry("clientVersionMinor",
		    *verMinor);
    }

    return custom_id;
}

void MeanwhileAccount::slotServerNotification(const TQString &mesg)
{
    KMessageBox::queuedMessageBox(0, KMessageBox::Error , mesg,
            i18n("Meanwhile Plugin: Message from server"), KMessageBox::Notify);
}

TQString MeanwhileAccount::meanwhileId() const
{
    return m_meanwhileId;
}

void MeanwhileAccount::setAway(bool away, const TQString &reason)
{
    MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());
    setOnlineStatus(away ? p->statusIdle : p->statusOnline, reason);
}

void MeanwhileAccount::setOnlineStatus(const Kopete::OnlineStatus &status,
        const TQString &reason)
{
    HERE;
    Kopete::OnlineStatus oldstatus = myself()->onlinetqStatus();

    mwDebug() << "From: " << oldstatus.description() << "(" <<
        oldstatus.internalStatus() << "):" << oldstatus.isDefinitelyOnline()
                                              << endl;
    mwDebug() << "To:   " << status.description() << "(" <<
        status.internalStatus() << "):" << status.isDefinitelyOnline() << endl;

    if (oldstatus == status)
        return;

    if (!oldstatus.isDefinitelyOnline() && status.isDefinitelyOnline()) {
        connect();

    } else if (oldstatus.isDefinitelyOnline() && !status.isDefinitelyOnline()) {
        disconnect(Kopete::Account::Manual);

    } else if (m_session)
        /* todo: check session state? */
        m_session->setStatus(status, reason);

    else
        mwDebug() << "Trying to set status, but no session exists" << endl;

    /* we should set this on the callback below */
    //myself()->setOnlineStatus(status);
}

void MeanwhileAccount::syncContactsToServer()
{
    if (m_session != 0L)
        m_session->syncContactsToServer();
}

void MeanwhileAccount::slotSessionStateChange(Kopete::OnlineStatus status)
{
    HERE;
    Kopete::OnlineStatus oldstatus = myself()->onlinetqStatus();
    myself()->setOnlineStatus(status);

    if (status.isDefinitelyOnline() != oldstatus.isDefinitelyOnline()) {
        if (status.isDefinitelyOnline())
            m_session->addContacts(contacts());
        emit isConnectedChanged();
    }
}

MeanwhileSession *MeanwhileAccount::session()
{
    return m_session;
}

#include "meanwhileaccount.moc"