summaryrefslogtreecommitdiffstats
path: root/konversation/src/linkaddressbook/addressbook.cpp
blob: 06fe4415546fa5536bba84830a037d836f31240b (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
  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.
*/

/*
  This class gives function that interact with kaddressbook.
  begin:     Fri 2004-07-23
  copyright: (C) 2004 by John Tapsell
  email:     john@geola.co.uk
*/

#include "addressbook.h"
#include "../viewcontainer.h"
#include "../konversationmainwindow.h"
#include "../server.h"
#include "../channel.h"
#include "../konversationapplication.h"

#include <tqstringlist.h>
#include "tqwidget.h"

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeapplication.h>
#include <dcopclient.h>
#include <twin.h>


namespace Konversation
{

    Addressbook *Addressbook::m_instance=0L;

    Addressbook::Addressbook() : DCOPObject( "KIMIface")
    {
        addressBook = KABC::StdAddressBook::self(true);
        m_ticket=NULL;
    }
    Addressbook::~Addressbook()
    {
        if (m_instance == this)
            sd.setObject(m_instance, 0, false);
    }

    Addressbook *Addressbook::self()
    {
        if (!m_instance) { sd.setObject(m_instance, new Addressbook()); }
        return m_instance;
    }

    TQStringList Addressbook::allContacts()
    {
        TQStringList contactUIDS;
        for( KABC::AddressBook::Iterator it = addressBook->begin(); it != addressBook->end(); ++it )
            if(hasAnyNicks(*it)) contactUIDS.append((*it).uid());
        return contactUIDS;
    }
    //Produces a string list of all the irc nicks that are known.
    TQStringList Addressbook::allContactsNicks()
    {
        TQStringList contacts;
        for( KABC::AddressBook::Iterator it = addressBook->begin(); it != addressBook->end(); ++it )
            contacts += TQStringList::split( TQChar( 0xE000 ), (*it).custom("messaging/irc", "All") );
        return contacts;
    }

    TQStringList Addressbook::onlineContacts()
    {
        TQStringList contactUIDS;
        for( KABC::AddressBook::Iterator it = addressBook->begin(); it != addressBook->end(); ++it )
            if(isOnline(*it)) contactUIDS.append((*it).uid());

        return contactUIDS;
    }
    TQStringList Addressbook::reachableContacts()
    {
        return onlineContacts();
    }
    TQStringList Addressbook::fileTransferContacts()
    {
        return onlineContacts();
    }
    bool Addressbook::isPresent(const TQString &uid)
    {
        return hasAnyNicks(addressBook->findByUid(uid));
    }
    TQString Addressbook::displayName(const TQString &uid)
    {
        return getBestNick(addressBook->findByUid(uid));
    }
    TQString Addressbook::presenceString(const TQString &uid)
    {
        if(uid.isEmpty())
        {
            kdDebug() << "Addressbook::presenceString() called with an empty uid" << endl;
            return TQString("Error");
        }
        switch( presenceStatus(uid))
        {
            case 0:
                return "";
            case 1:
                return i18n("Offline");
            case 2:
                return i18n("Connecting");        //Shouldn't happen - not supported.
            case 3:
                return i18n("Away");
            case 4:
                return i18n("Online");
        }
        return TQString("Error");
    }
    int Addressbook::presenceStatus(const TQString &uid)
    {
        return presenceStatusByAddressee(addressBook->findByUid(uid));
    }

    bool Addressbook::canReceiveFiles(const TQString &uid)
    {
        if(uid.isEmpty())
        {
            kdDebug() << "Addressbook::canReceiveFiles() called with empty uid" << endl;
            return false;
        }
        int presence = presenceStatus(uid);

        return (presence == 4) || (presence == 3);
    }
    bool Addressbook::canRespond(const TQString &uid)
    {
        if(uid.isEmpty())
        {
            kdDebug() << "Addressbook::canRespond called with empty uid" << endl;
            return false;
        }
        //this should return false if they are offline.
        int result = presenceStatus(uid);
        if(result == 3 || result == 4) return true;
        return false;
    }
    TQString Addressbook::locate(const TQString &contactId, const TQString &protocol)
    {
        if(contactId.isEmpty())
        {
            kdDebug() << "Addressbook::locate called with empty contactId" << endl;
            return TQString();
        }
        if(protocol != "messaging/irc")
            return TQString();

        return getKABCAddresseeFromNick(contactId).uid();
    }
    TQPixmap Addressbook::icon(const TQString &uid)
    {

        Images* icons = KonversationApplication::instance()->images();
        TQIconSet currentIcon;
        if(!isPresent(uid))
            return TQPixmap();

        switch(presenceStatus(uid))
        {
            case 0:                               //Unknown
            case 1:                               //Offline
            case 2:                               //connecting - invalid for us?
                currentIcon = icons->getKimproxyOffline();
                break;
            case 3:                               //Away
                currentIcon = icons->getKimproxyAway();
                break;
            case 4:                               //Online
                currentIcon = icons->getKimproxyOnline();
                break;
            default:
                //error
                kdDebug() << "Unknown status " << uid << endl;
                return TQPixmap();
        }

        TQPixmap joinedIcon = currentIcon.pixmap(TQIconSet::Automatic, TQIconSet::Active, TQIconSet::On);
        return joinedIcon;
    }
    TQString Addressbook::context(const TQString &uid)
    {
        if(uid.isEmpty())
        {
            kdDebug() << "Addressbook::contact called with empty uid" << endl;
            return TQString();
        }
        TQString context;
        return context;
    }
    TQStringList Addressbook::protocols()
    {
        TQStringList protocols;
        protocols.append("messaging/irc");
        return protocols;
    }

    // ACTORS
    /**
     * Send a single message to the specified addressee
     * Any response will be handled by the IM client as a normal
     * conversation.
     * @param uid the KABC uid you want to chat with.
     * @param message the message to send them.
     */
    void Addressbook::messageContact( const TQString &uid, const TQString& message )
    {
        if(uid.isEmpty())
        {
            focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation for instant messaging, but did not specify any contact to send the message to.  This is probably a bug in the other application."));
            return;
        }
        KABC::Addressee addressee = addressBook->findByUid(uid);
        if(addressee.isEmpty())
        {
            focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation for instant messaging, but Konversation could not find the specified contact in the TDE address book."));
            return;
        }
        NickInfoPtr nickInfo = getNickInfo(addressee);
        if(!nickInfo)
        {
            TQString user = addressee.fullEmail();
            if(!user.isEmpty()) user = " (" + user + ')';
            focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation for instant messaging, but the requested user%1 is not online.").arg(user));
            return;
        }

        nickInfo->getServer()->dcopSay(nickInfo->getNickname(), message);
    }

    /**
     * Open a chat to a contact, and optionally set some initial text
     */
    void Addressbook::messageNewContact( const TQString &contactId, const TQString &/*protocol*/ ) {
    if(contactId.isEmpty() )
    {
        kdDebug() << "Addressbook::messageNewContact called with empty contactid" << endl;
        focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation for instant messaging, but did not specify any contact to send the message to.  This is probably a bug in the other application."));
        return;
    }
    messageContact(contactId, TQString());
}

/**
 * Start a chat session with the specified addressee
 * @param uid the KABC uid you want to chat with.
 */
void Addressbook::chatWithContact( const TQString &uid )
{
    if(uid.isEmpty())
    {
        kdDebug() << "Addressbook::chatWithContact called with empty uid" << endl;
        focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation for instant messaging, but did not specify any contact to send the message to.  This is probably a bug in the other application."));
        return;
    }
    messageContact(uid, TQString());
}

/**
 * Send the file to the contact
 * @param uid the KABC uid you are sending to.
 * @param sourceURL a KURL to send.
 * @param altFileName an alternate filename describing the file
 * @param fileSize file size in bytes
 */
void Addressbook::sendFile(const TQString &uid, const KURL &sourceURL, const TQString &altFileName, uint fileSize)
{
    if(uid.isEmpty())
    {
        focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation to send a file to a contact, but did not specify any contact to send the file to.  This is probably a bug in the other application."));
        return;
    }
    KABC::Addressee addressee = addressBook->findByUid(uid);
    if(addressee.isEmpty())
    {
        focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation to send a file to a contact, but Konversation could not find the specified contact in the TDE address book."));
        return;
    }
    NickInfoPtr nickInfo = getNickInfo(addressee);
    if(!nickInfo)
    {
        TQString user = addressee.fullEmail();
        if(!user.isEmpty()) user = " (" + user + ')';
        focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation to send a file to a contact, but the requested user%1 is not currently online.").arg(user));
        return;
    }
    nickInfo->getServer()->addDccSend(nickInfo->getNickname(), sourceURL, altFileName, fileSize);
    TQWidget *widget = nickInfo->getServer()->getViewContainer()->getWindow();
    KWin::demandAttention(widget->winId());       //If activeWindow request is denied, at least demand attention!
    KWin::activateWindow(widget->winId());        //May or may not work, depending on focus stealing prevention.

}

// MUTATORS
// Contact list
/**
 * Add a contact to the contact list
 * @param contactId the protocol specific identifier for the contact, eg UIN for ICQ, screenname for AIM, nick for IRC.
 * @param protocolId the protocol, eg one of "AIMProtocol", "MSNProtocol", "ICTQProtocol", ...
 * @return whether the add succeeded.  False may signal already present, protocol not supported, or add operation not supported.
 */
bool Addressbook::addContact( const TQString &/*contactId*/, const TQString &/*protocolId*/ ) {
focusAndShowErrorMessage(i18n("Another TDE application tried to use Konversation to add a contact.  Konversation does support this."));
return false;
//Nicks are auto added if they are put in the addressbook - I don' think there is anything useful I can do.
}

void Addressbook::emitContactPresenceChanged(const TQString &uid, int presence)
{
    if(uid.isEmpty())
    {
        //This warning below is annoying.  FIXME - disabled because it's too verbose
        //		kdDebug() << "Addressbook::emitContactPresenceChanged was called with empty uid" << endl;
        return;
    }
    Q_ASSERT(kapp->dcopClient());
    emit contactPresenceChanged(uid, kapp->dcopClient()->appId(), presence);
    //	kdDebug() << "Presence changed for uid " << uid << " to " << presence << endl;
}

void Addressbook::emitContactPresenceChanged(const TQString &uid)
{
    if(uid.isEmpty())
    {
        kdDebug() << "Addressbook::emitContactPresenceChanged was called with empty uid" << endl;
        return;
    };

    emitContactPresenceChanged(uid, presenceStatus(uid));
}

}                                                 //NAMESPACE

#include "addressbook.moc"