summaryrefslogtreecommitdiffstats
path: root/konversation/src/nick.cpp
blob: dbb98f1e859acfccb714e3ef09f33c7357ec2548 (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
/*
  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.
*/

/*
  begin:     Fri Jan 25 2002
  copyright: (C) 2002 by Dario Abatianni
  email:     eisfuchs@tigress.com
*/

#include "nick.h"
#include "addressbook.h"

#include <tqtextstream.h>

#include <kdebug.h>
#include <tdelistview.h>
#include <tdelocale.h>
#include <tdeabc/phonenumber.h>

#include "konversationapplication.h"

Nick::Nick(TDEListView *listView,
const ChannelNickPtr& channelnick)
    : TQObject (),
    TDEListViewItem (listView, listView->lastItem(), TQString(),
                   channelnick->getNickname(), channelnick->getHostmask())
{
    m_channelnickptr = channelnick;

    Q_ASSERT(channelnick);
    if(!channelnick) return;

    m_flags = 0;
    m_height = height();

    refresh();

    connect(this, TQT_SIGNAL(refreshed()), listView, TQT_SLOT(startResortTimer()));
    connect(getChannelNick(), TQT_SIGNAL(channelNickChanged()), TQT_SLOT(refresh()));
    connect(getChannelNick()->getNickInfo(), TQT_SIGNAL(nickInfoChanged()), TQT_SLOT(refresh()));
}

Nick::~Nick()
{
}

ChannelNickPtr Nick::getChannelNick() const
{
    Q_ASSERT(m_channelnickptr);
    return m_channelnickptr;
}

void Nick::refresh()
{
    int flags = 0;
    NickInfo* nickInfo = getChannelNick()->getNickInfo();
    bool away = false;

    if ( nickInfo )
        away = nickInfo->isAway();

    if(away)
        flags=1;

    Images* images = KonversationApplication::instance()->images();
    TQPixmap icon;

    if ( getChannelNick()->isOwner() )
    {
        flags += 64;
        icon = images->getNickIcon( Images::Owner, away );
    }
    else if ( getChannelNick()->isAdmin() )
    {
        flags += 128;
        icon = images->getNickIcon( Images::Admin, away );
    }
    else if ( getChannelNick()->isOp() )
    {
        flags += 32;
        icon = images->getNickIcon( Images::Op, away );
    }
    else if ( getChannelNick()->isHalfOp() )
    {
        flags += 16;
        icon = images->getNickIcon( Images::HalfOp, away );
    }
    else if ( getChannelNick()->hasVoice() )
    {
        flags += 8;
        icon = images->getNickIcon( Images::Voice, away );
    }
    else
    {
        flags += 4;
        icon = images->getNickIcon( Images::Normal, away );
    }

    setPixmap( 0, icon );

    TDEABC::Picture pic = nickInfo->getAddressee().photo();

    if(!pic.isIntern())
    {
        pic = nickInfo->getAddressee().logo();
    }

    if(pic.isIntern())
    {
        TQPixmap qpixmap(pic.data().scaleHeight(m_height));
        setPixmap(1,qpixmap);
    }

    TQString newtext1 = calculateLabel1();
    if(newtext1 != text(1))
    {
        setText(1, newtext1);
        flags += 2;
    }

    setText(2, calculateLabel2());
    repaint();

    if(m_flags != flags)
    {
        m_flags = flags;
        emit refreshed();                         // Resort nick list
    }
}

TQString Nick::calculateLabel1()
{
    NickInfoPtr nickinfo = getChannelNick()->getNickInfo();
    TDEABC::Addressee addressee = nickinfo->getAddressee();

    if(!addressee.realName().isEmpty())           //if no addressee, realName will be empty
    {
        return nickinfo->getNickname() + " (" + addressee.realName() + ')';
    }
    else if(Preferences::showRealNames() && !nickinfo->getRealName().isEmpty())
    {
        return nickinfo->getNickname() + " (" + nickinfo->getRealName() + ')';
    }

    return nickinfo->getNickname();
}

TQString Nick::calculateLabel2()
{
    return getChannelNick()->getNickInfo()->getHostmask();
}

int Nick::compare(TQListViewItem* item,int col,bool ascending) const
{
    Nick* otherItem = static_cast<Nick*>(item);

    if(Preferences::sortByActivity())
    {
        uint thisRecentActivity = getChannelNick()->recentActivity();
        uint otherRecentActivity = otherItem->getChannelNick()->recentActivity();
        if(thisRecentActivity > otherRecentActivity)
        {
            return -1;
        }
        if(thisRecentActivity < otherRecentActivity)
        {
            return 1;
        }
        uint thisTimestamp = getChannelNick()->timeStamp();
        uint otherTimestamp = otherItem->getChannelNick()->timeStamp();
        if(thisTimestamp > otherTimestamp)
        {
            return -1;
        }
        if(thisTimestamp < otherTimestamp)
        {
            return 1;
        }
    }

    if(Preferences::sortByStatus())
    {
        int thisFlags = getSortingValue();
        int otherFlags = otherItem->getSortingValue();

        if(thisFlags > otherFlags)
        {
            return 1;
        }
        if(thisFlags < otherFlags)
        {
            return -1;
        }
    }

    TQString thisKey;
    TQString otherKey;

    if(col > 1) //the reason we need this: enabling hostnames adds another column
    {
        if(Preferences::sortCaseInsensitive())
        {
            thisKey = thisKey.lower();
            otherKey = otherKey.lower();
        }
        else
        {
            thisKey = key(col, ascending);
            otherKey = otherItem->key(col, ascending);
        }
    }
    else if(col == 1)
    {
        if(Preferences::sortCaseInsensitive())
        {
            thisKey = getChannelNick()->loweredNickname();
            otherKey = otherItem->getChannelNick()->loweredNickname();
        }
        else
        {
            thisKey = key(col, ascending);
            otherKey = otherItem->key(col, ascending);
        }
    }

    return thisKey.compare(otherKey);
}

void Nick::paintCell(TQPainter * p, const TQColorGroup & cg, int column, int width, int align )
{
    TQColorGroup cg2 = cg;
    NickInfo* nickInfo = getChannelNick()->getNickInfo();

    if(nickInfo->isAway())
    {
        cg2.setColor(TQColorGroup::Text, kapp->palette(listView()).disabled().text());
    }

    TDEListViewItem::paintCell(p,cg2,column,width,align);
}

int Nick::getSortingValue() const
{
    int flags;
    TQString sortingOrder = Preferences::sortOrder();

    if(getChannelNick()->isOwner())       flags=sortingOrder.find('q');
    else if(getChannelNick()->isAdmin())  flags=sortingOrder.find('p');
    else if(getChannelNick()->isOp() )    flags=sortingOrder.find('o');
    else if(getChannelNick()->isHalfOp()) flags=sortingOrder.find('h');
    else if(getChannelNick()->hasVoice()) flags=sortingOrder.find('v');
    else                                  flags=sortingOrder.find('-');

    return flags;
}

#include "nick.moc"