~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/irc/ui/channellist.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    channellist.cpp - IRC Channel Search Widget
 
3
 
 
4
    Copyright (c) 2004      by Jason Keirstead <jason@keirstead.org>
 
5
    Copyright (c) 2004-2007 by Michel Hermier <michel.hermier@gmail.com>
 
6
 
 
7
    Kopete    (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
 
8
 
 
9
    *************************************************************************
 
10
    *                                                                       *
 
11
    * This program is free software; you can redistribute it and/or modify  *
 
12
    * it under the terms of the GNU General Public License as published by  *
 
13
    * the Free Software Foundation; either version 2 of the License, or     *
 
14
    * (at your option) any later version.                                   *
 
15
    *                                                                       *
 
16
    *************************************************************************
 
17
*/
 
18
 
 
19
#include "channellist.h"
 
20
 
 
21
#include "kircclient.h"
 
22
 
 
23
#include <klocale.h>
 
24
#include <kmessagebox.h>
 
25
 
 
26
#include <qvariant.h>
 
27
#include <qlabel.h>
 
28
#include <qpainter.h>
 
29
#include <qapplication.h>
 
30
#include <qstyle.h>
 
31
#include <qlineedit.h>
 
32
#include <qpushbutton.h>
 
33
 
 
34
#include <Q3HBoxLayout>
 
35
#include <QPixmap>
 
36
#include <Q3VBoxLayout>
 
37
#include <k3listview.h>
 
38
#include <qlayout.h>
 
39
 
 
40
#include <qtimer.h>
 
41
#include <qspinbox.h>
 
42
#include <Q3Header>
 
43
 
 
44
#include <kdebug.h>
 
45
 
 
46
 
 
47
class ChannelListItem : public K3ListViewItem
 
48
{
 
49
        public:
 
50
                ChannelListItem( K3ListView *parent, QString arg1, QString arg2, QString arg3 );
 
51
                virtual int compare( Q3ListViewItem *i, int col, bool ascending ) const;
 
52
                //virtual void paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align );
 
53
 
 
54
        private:
 
55
                K3ListView *parentList;
 
56
};
 
57
 
 
58
ChannelListItem::ChannelListItem( K3ListView *parent, QString arg1, QString arg2, QString arg3 ) :
 
59
        K3ListViewItem( parent, parent->lastItem() ), parentList( parent )
 
60
{
 
61
        setText(0, arg1);
 
62
        setText(1, arg2);
 
63
        setText(2, arg3);
 
64
}
 
65
 
 
66
int ChannelListItem::compare( Q3ListViewItem *i, int col, bool ascending ) const
 
67
{
 
68
        if( col == 1 )
 
69
        {
 
70
                if( text(1).toUInt() < i->text(1).toUInt() )
 
71
                        return -1;
 
72
                else if ( text(1).toUInt() == i->text(1).toUInt() )
 
73
                        return 0;
 
74
                else
 
75
                        return 1;
 
76
        }
 
77
        else
 
78
                return Q3ListViewItem::compare( i, col, ascending );
 
79
}
 
80
 
 
81
#if 0
 
82
void ChannelListItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align )
 
83
{
 
84
        QPixmap back( width, height() );
 
85
        QPainter paint( &back );
 
86
        //K3ListViewItem::paintCell( &paint, cg, column, width, align );
 
87
        // PASTED FROM KLISTVIEWITEM:
 
88
        // set the alternate cell background colour if necessary
 
89
        QColorGroup _cg = cg;
 
90
        if (isAlternate())
 
91
                if (listView()->viewport()->backgroundMode()==Qt::FixedColor)
 
92
                        _cg.setColor(QColorGroup::Background, static_cast< K3ListView* >(listView())->alternateBackground());
 
93
                else
 
94
                        _cg.setColor(QColorGroup::Base, static_cast< K3ListView* >(listView())->alternateBackground());
 
95
        // PASTED FROM QLISTVIEWITEM
 
96
        {
 
97
                QPainter *p = &paint;
 
98
 
 
99
                Q3ListView *lv = listView();
 
100
                if ( !lv )
 
101
                        return;
 
102
                QFontMetrics fm( p->fontMetrics() );
 
103
 
 
104
                // any text we render is done by the Components, not by this class, so make sure we've nothing to write
 
105
                QString t;
 
106
 
 
107
                // removed text truncating code from Qt - we do that differently, further on
 
108
 
 
109
                int marg = lv->itemMargin();
 
110
                int r = marg;
 
111
        //      const QPixmap * icon = pixmap( column );
 
112
 
 
113
                const BackgroundMode bgmode = lv->viewport()->backgroundMode();
 
114
                const QColorGroup::ColorRole crole = QPalette::backgroundRoleFromMode( bgmode );
 
115
 
 
116
                if ( _cg.brush( crole ) != lv->colorGroup().brush( crole ) )
 
117
                        p->fillRect( 0, 0, width, height(), _cg.brush( crole ) );
 
118
                else
 
119
                {
 
120
                        // all copied from QListView::paintEmptyArea
 
121
 
 
122
                        //lv->paintEmptyArea( p, QRect( 0, 0, width, height() ) );
 
123
                        QStyleOption opt( lv->sortColumn(), 0 ); // ### hack; in 3.1, add a property in QListView and QHeader
 
124
                        QStyle::SFlags how = QStyle::Style_Default;
 
125
                        if ( lv->isEnabled() )
 
126
                                how |= QStyle::Style_Enabled;
 
127
 
 
128
                        lv->style().drawComplexControl( QStyle::CC_ListView,
 
129
                                                p, lv, QRect( 0, 0, width, height() ), lv->colorGroup(),
 
130
                                                how, QStyle::SC_ListView, QStyle::SC_None,
 
131
                                                opt );
 
132
                }
 
133
 
 
134
 
 
135
 
 
136
                if ( isSelected() &&
 
137
                (column == 0 || lv->allColumnsShowFocus()) ) {
 
138
                        p->fillRect( r - marg, 0, width - r + marg, height(),
 
139
                                        _cg.brush( QColorGroup::Highlight ) );
 
140
        // removed text pen setting code from Qt
 
141
                }
 
142
 
 
143
                // removed icon drawing code from Qt
 
144
 
 
145
                // draw the tree gubbins
 
146
                if ( multiLinesEnabled() && column == 0 && isOpen() && childCount() ) {
 
147
                        int textheight = fm.size( align, t ).height() + 2 * lv->itemMargin();
 
148
                        textheight = qMax( textheight, QApplication::globalStrut().height() );
 
149
                        if ( textheight % 2 > 0 )
 
150
                                textheight++;
 
151
                        if ( textheight < height() ) {
 
152
                                int w = lv->treeStepSize() / 2;
 
153
                                lv->style().drawComplexControl( QStyle::CC_ListView, p, lv,
 
154
                                                                QRect( 0, textheight, w + 1, height() - textheight + 1 ), _cg,
 
155
                                                                lv->isEnabled() ? QStyle::Style_Enabled : QStyle::Style_Default,
 
156
                                                                QStyle::SC_ListViewExpand,
 
157
                                                                (uint)QStyle::SC_All, QStyleOption( this ) );
 
158
                        }
 
159
                }
 
160
        }
 
161
        // END OF PASTE
 
162
 
 
163
 
 
164
        //do you see a better way to tell the TextComponent we are selected ?  - Olivier 2004-09-02
 
165
        if ( isSelected() )
 
166
                _cg.setColor(QColorGroup::Text , _cg.highlightedText() );
 
167
 
 
168
        Q3SimpleRichText myrichtext( text(column), paint.font() );
 
169
        myrichtext.draw(  &paint, 0, 0, paint.window(), _cg );
 
170
 
 
171
        paint.end();
 
172
        p->drawPixmap( 0, 0, back );
 
173
}
 
174
#endif
 
175
 
 
176
ChannelList::ChannelList( QWidget* parent, KIRC::Client *client )
 
177
    : QWidget( parent ), m_client( client )
 
178
{
 
179
        ChannelListLayout = new Q3VBoxLayout( this, 11, 6, "ChannelListLayout");
 
180
 
 
181
        layout72_2 = new Q3HBoxLayout( 0, 0, 6, "layout72_2");
 
182
 
 
183
        textLabel1_2 = new QLabel( this, "textLabel1_2" );
 
184
        layout72_2->addWidget( textLabel1_2 );
 
185
 
 
186
        channelSearch = new QLineEdit( this, "channelSearch" );
 
187
        layout72_2->addWidget( channelSearch );
 
188
 
 
189
        numUsers = new QSpinBox( 0, 32767, 1, this, "num_users" );
 
190
        numUsers->setSuffix( i18n(" members") );
 
191
        layout72_2->addWidget( numUsers );
 
192
 
 
193
        mSearchButton = new QPushButton( this, "mSearchButton" );
 
194
        layout72_2->addWidget( mSearchButton );
 
195
        ChannelListLayout->addLayout( layout72_2 );
 
196
 
 
197
        mChannelList = new K3ListView( this );
 
198
        mChannelList->addColumn( i18n( "Channel" ) );
 
199
        mChannelList->addColumn( i18n( "Users" ) );
 
200
        //mChannelList->header()->setResizeEnabled( false, mChannelList->header()->count() - 1 );
 
201
        mChannelList->addColumn( i18n( "Topic" ) );
 
202
        mChannelList->setAllColumnsShowFocus( true );
 
203
        mChannelList->setShowSortIndicator( true );
 
204
 
 
205
        ChannelListLayout->addWidget( mChannelList );
 
206
 
 
207
        //clearWState( WState_Polished );
 
208
 
 
209
        textLabel1_2->setText( i18n( "Search for:" ) );
 
210
        textLabel1_2->setToolTip( i18n( "You may search for channels on the IRC server, using a text string entered here." ) );
 
211
        numUsers->setToolTip( i18n( "Channels returned must have at least this many members." ) );
 
212
        numUsers->setWhatsThis( i18n( "Channels returned must have at least this many members." ) );
 
213
        textLabel1_2->setWhatsThis( i18n( "You may search for channels on the IRC server, using a text string entered here.  For instance, you may type 'linux' to find channels that have something to do with Linux." ) );
 
214
        channelSearch->setToolTip( i18n( "You may search for channels on the IRC server for a text string entered here." ) );
 
215
        channelSearch->setWhatsThis( i18n( "You may search for channels on the IRC server, using a text string entered here.  For instance, you may type 'linux' to find channels that have something to do with Linux." ) );
 
216
        mSearchButton->setText( i18n( "S&earch" ) );
 
217
        mSearchButton->setToolTip( i18n( "Perform a channel search." ) );
 
218
        mSearchButton->setWhatsThis( i18n( "Perform a channel search.  Please be patient, as this can be slow, depending on the total number of channels on the server." ) );
 
219
 
 
220
        mChannelList->setToolTip( i18n( "Double click on a channel to select it." ) );
 
221
        mChannelList->header()->setLabel( 0, i18n( "Channel" ) );
 
222
        mChannelList->header()->setLabel( 1, i18n( "Users" ) );
 
223
        mChannelList->header()->setLabel( 2, i18n( "Topic" ) );
 
224
 
 
225
        // signals and slots connections
 
226
        connect( mChannelList, SIGNAL(doubleClicked(Q3ListViewItem*)),
 
227
                this, SLOT(slotItemDoubleClicked(Q3ListViewItem*)) );
 
228
 
 
229
        connect( mSearchButton, SIGNAL(clicked()), this, SLOT(search()) );
 
230
 
 
231
        connect( mChannelList, SIGNAL(selectionChanged(Q3ListViewItem*)), this,
 
232
                SLOT(slotItemSelected(Q3ListViewItem*)) );
 
233
 
 
234
        /*
 
235
        connect( m_engine, SIGNAL(incomingListedChan(QString,uint,QString)),
 
236
                this, SLOT(slotChannelListed(QString,uint,QString)) );
 
237
 
 
238
        connect( m_engine, SIGNAL(incomingEndOfList()), this, SLOT(slotListEnd()) );
 
239
 
 
240
        connect( m_engine, SIGNAL(connectedToServer()), this, SLOT(reset()) );
 
241
        connect( m_engine, SIGNAL(disconnected()), this, SLOT(slotDisconnected()) );
 
242
        */
 
243
 
 
244
        show();
 
245
}
 
246
 
 
247
void ChannelList::slotItemDoubleClicked( Q3ListViewItem *i )
 
248
{
 
249
        emit channelDoubleClicked( i->text(0) );
 
250
}
 
251
 
 
252
void ChannelList::slotItemSelected( Q3ListViewItem *i )
 
253
{
 
254
        emit channelSelected( i->text(0) );
 
255
}
 
256
 
 
257
void ChannelList::reset()
 
258
{
 
259
        channelCache.clear();
 
260
        clear();
 
261
}
 
262
 
 
263
void ChannelList::clear()
 
264
{
 
265
        mChannelList->clear();
 
266
        channelSearch->clear();
 
267
        channelSearch->setFocus();
 
268
}
 
269
 
 
270
void ChannelList::search()
 
271
{
 
272
        kDebug(14120) ;
 
273
 
 
274
        if( m_client->connectionState() == KIRC::Socket::Open || !channelCache.isEmpty() )
 
275
        {
 
276
                mChannelList->clear();
 
277
                mChannelList->setSorting( -1 );
 
278
                mSearchButton->setEnabled(false);
 
279
                mSearch = channelSearch->text();
 
280
                mSearching = true;
 
281
                mUsers = numUsers->value();
 
282
 
 
283
                if( channelCache.isEmpty() ) {
 
284
                        // FIXME
 
285
                        //m_client->list();
 
286
                }
 
287
                else
 
288
                {
 
289
                        cacheIterator = channelCache.begin();
 
290
                        slotSearchCache();
 
291
                }
 
292
        }
 
293
        else
 
294
        {
 
295
                KMessageBox::queuedMessageBox(
 
296
                        this, KMessageBox::Error,
 
297
                        i18n("You must be connected to the IRC server to perform a channel listing."),
 
298
                        i18n("Not Connected"), 0
 
299
                );
 
300
        }
 
301
}
 
302
 
 
303
void ChannelList::slotChannelListed( const QString &channel, uint users, const QString &topic )
 
304
{
 
305
        checkSearchResult( channel, users, topic );
 
306
        channelCache.insert( channel, QPair< uint, QString >( users, topic ) );
 
307
}
 
308
 
 
309
void ChannelList::checkSearchResult( const QString &channel, uint users, const QString &topic )
 
310
{
 
311
        if( ( mUsers == 0 || mUsers <= users ) &&
 
312
                ( mSearch.isEmpty() || channel.contains( mSearch, false ) || topic.contains( mSearch, false ) )
 
313
        )
 
314
        {
 
315
                //new ChannelListItem( mChannelList, channel, QString::number(users), topic );
 
316
        }
 
317
}
 
318
 
 
319
void ChannelList::slotSearchCache()
 
320
{
 
321
        if( cacheIterator != channelCache.end() )
 
322
        {
 
323
                checkSearchResult( cacheIterator.key(), cacheIterator.data().first, cacheIterator.data().second );
 
324
                ++cacheIterator;
 
325
                QTimer::singleShot( 0, this, SLOT(slotSearchCache()) );
 
326
        }
 
327
        else
 
328
        {
 
329
                slotListEnd();
 
330
        }
 
331
}
 
332
 
 
333
void ChannelList::slotDisconnected()
 
334
{
 
335
        KMessageBox::queuedMessageBox(
 
336
                this, KMessageBox::Error, i18n("You have been disconnected from the IRC server."),
 
337
                i18n("Disconnected"), 0
 
338
        );
 
339
 
 
340
        slotListEnd();
 
341
}
 
342
 
 
343
void ChannelList::slotListEnd()
 
344
{
 
345
        mChannelList->setSorting(0, true);
 
346
        mSearchButton->setEnabled(true);
 
347
        mSearching = false;
 
348
}
 
349
 
 
350
#include "channellist.moc"