summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircchanneltab.cpp
blob: 881ded756311b7f68232273d9a878800a59f4560 (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
#include <opie2/oresource.h>
#include <qwhatsthis.h>
#include <qhbox.h>
#include <qdict.h>

#include "ircchanneltab.h"
#include "ircservertab.h"
#include "ircmessageparser.h"

#include <opie2/odebug.h>
QDict<QString> IRCChannelTab::m_queuedMessages (17);

IRCChannelTab::IRCChannelTab(IRCChannel *channel, IRCServerTab *parentTab, MainWindow *mainWindow, QWidget *parent, const char *name, WFlags f) : IRCTab(parent, name, f) {
    m_mainWindow = mainWindow;
    m_parentTab = parentTab;
    m_channel = channel;
    m_description->setText(tr("Talking on channel") + " <b>" + channel->channelname() + "</b>");
    QHBox *hbox = new QHBox(this);
    m_textview = new QTextView(hbox);
    m_textview->setHScrollBarMode(QScrollView::AlwaysOff);
    m_textview->setVScrollBarMode(QScrollView::AlwaysOn);
    m_listVisible = TRUE;
    m_listButton = new QPushButton(">", m_textview);
    m_listButton->setFlat( true );
    m_textview->setCornerWidget(m_listButton);
    m_textview->setTextFormat(RichText);
    QWhatsThis::add(m_textview, tr("Channel discussion"));
    connect(m_listButton, SIGNAL(clicked()), this, SLOT(toggleList()));
    m_list = new IRCChannelList(m_channel, hbox);
    m_list->update();
    m_list->setMaximumWidth(LISTWIDTH);
    m_field = new IRCHistoryLineEdit(this);
    connect(m_field, SIGNAL(nextTab()), this, SIGNAL(nextTab()));
    connect(m_field, SIGNAL(prevTab()), this, SIGNAL(prevTab()));
    connect(m_field, SIGNAL(closeTab()), this, SLOT(remove()));
    connect(this, SIGNAL(editFocus()), m_field, SLOT(setEditFocus()));

    QWhatsThis::add(m_field, tr("Type your message here to participate in the channel discussion"));
    m_popup = new QPopupMenu(m_list);
    m_lines = 0;
    /* Required so that embedded-style "right" clicks work */
    QPEApplication::setStylusOperation(m_list->viewport(), QPEApplication::RightOnHold);
    connect(m_list, SIGNAL(mouseButtonPressed(int,QListBoxItem*,const QPoint&)), this, SLOT(mouseButtonPressed(int,QListBoxItem*,const QPoint&)));
    /* Construct the popup menu */
    //QPopupMenu *ctcpMenu = new QPopupMenu(m_list);
    m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/query",Opie::Core::OResource::SmallIcon),
                        tr("Query"), this, SLOT(popupQuery()));
    m_popup->insertSeparator();
    m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/ping",Opie::Core::OResource::SmallIcon),
                        tr("Ping"), this, SLOT(popupPing()));
    m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/version",Opie::Core::OResource::SmallIcon),
                        tr("Version"), this, SLOT(popupVersion()));
    m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/whois",Opie::Core::OResource::SmallIcon),
                        tr("Whois"), this, SLOT(popupWhois()));
    //m_popup->insertItem(ctcpMenu, "CTCP");
    connect(m_mainWindow, SIGNAL(updateScroll()), this, SLOT(scrolling()));
    m_layout->add(hbox);
    hbox->show();
    m_layout->add(m_field);
    m_field->setFocus();
    m_field->setActiveWindow();

    connect(m_field, SIGNAL(returnPressed()), this, SLOT(processCommand()));
    connect(m_list,  SIGNAL(doubleClicked ( QListBoxItem * ) ), this, SLOT(popupQuery( QListBoxItem * ) ));
    settingsChanged();
    
    if(m_queuedMessages[m_channel->channelname()]) {
        appendText(*m_queuedMessages[m_channel->channelname()]);
        delete m_queuedMessages[m_channel->channelname()];
        m_queuedMessages.remove(m_channel->channelname());
    }
}

void IRCChannelTab::scrolling(){
  m_textview->ensureVisible(0, m_textview->contentsHeight());
}

void IRCChannelTab::appendText(QString text) {
    /* not using append because it creates layout problems */
    QString txt = m_textview->text() + IRCTab::appendTimestamp( text );
    if (m_maxLines > 0 && m_lines >= m_maxLines) {
        int firstBreak = txt.find('\n');
        if (firstBreak != -1) {
            txt = "<qt bgcolor=\"" + m_backgroundColor + "\"/>" + txt.right(txt.length() - (firstBreak + 1));
        }
    } else {
        m_lines++;
    }
    m_textview->ensureVisible(0, m_textview->contentsHeight());
    m_textview->setText(txt);
    m_textview->ensureVisible(0, m_textview->contentsHeight());

    int p1, p2;
    if ( text.contains( IRCMessageParser::tr("Received a CTCP PING from ") ) )
        emit ping( title() );
    else if ( (p1 = text.find("ping", 0, false) )!= -1 && (p2 = text.find( m_parentTab->server()->nick(), 0,false )) != -1  ) {
        int col = text.findRev("color", -1, false);
        if ( col < p2 )
            emit ping( title() );

    }

    emit changed(this);
}

IRCChannelTab::~IRCChannelTab() {
    m_parentTab->removeChannelTab(this);
}

void IRCChannelTab::processCommand() {
    QString text = m_field->text();
    if (text.length()>0) {
        if (session()->isSessionActive()) {
            if (text.startsWith("/") && !text.startsWith("//")) {
                /* Command mode */
                m_parentTab->executeCommand(this, text);;
            } else {
                if (text.startsWith("//"))
                    text = text.right(text.length()-1);
                session()->sendMessage(m_channel, m_field->text());
                appendText("<font color=\"" + m_textColor + "\">&lt;</font><font color=\"" + m_selfColor + "\">"+m_parentTab->server()->nick()+"</font><font color=\"" + m_textColor + "\">&gt; "+IRCOutput::toHTML(m_field->text())+"</font><br>");
            }
        } else {
            appendText("<font color=\"" + m_errorColor + "\">"+tr("Disconnected")+"</font><br>");
        }
    }
    m_field->clear();
}

void IRCChannelTab::settingsChanged() {
    m_textview->setText("<qt bgcolor=\"" + m_backgroundColor + "\"/>");
    m_lines = 0;
}

void IRCChannelTab::toggleList() {
    if (m_listVisible) {
        m_list->setMaximumWidth(0);
        m_listButton->setText("<");
    } else {
        m_list->setMaximumWidth(LISTWIDTH);
        m_listButton->setText(">");
    }
    m_listVisible = !m_listVisible;
}

void IRCChannelTab::mouseButtonPressed(int mouse, QListBoxItem *, const QPoint &point) {
    switch (mouse) {
        case 1:
            break;
        case 2:
            m_popup->popup(point);
            break;
    };
}

void IRCChannelTab::popupQuery( QListBoxItem *item) {
    if (item) {
        IRCPerson *person = session()->getPerson(item->text());
        if (person) {
            IRCQueryTab *tab = m_parentTab->getTabForQuery(person);
            if (!tab) {
                tab = new IRCQueryTab(person, m_parentTab, m_mainWindow, (QWidget *)parent());
                m_parentTab->addQueryTab(tab);
                m_mainWindow->addTab(tab);
            }
        }
    }
}

void IRCChannelTab::popupQuery() {
    if ( m_list->currentItem() != -1 )
        popupQuery( m_list->item(m_list->currentItem()));
}

void IRCChannelTab::popupPing() {
    if(m_list->currentItem() != -1)
        m_parentTab->session()->sendCTCPPing(m_list->text(m_list->currentItem()));
}

void IRCChannelTab::popupVersion() {
    if(m_list->currentItem() != -1)
        m_parentTab->session()->sendCTCPRequest(m_list->text(m_list->currentItem()), "VERSION", "");
}

void IRCChannelTab::popupWhois() {
    if(m_list->currentItem() != -1)
        m_parentTab->session()->whois(m_list->text(m_list->currentItem()));
}

QString IRCChannelTab::title() {
    if(!m_channel->channelname().startsWith("&"))
        return m_channel->channelname();

    return "&" + m_channel->channelname();
}

IRCSession *IRCChannelTab::session() {
    return m_parentTab->session();
}

void IRCChannelTab::remove() {
    if (session()->isSessionActive()) {
        session()->part(m_channel);
    } else {
        m_mainWindow->killTab(this);
    }
}

void IRCChannelTab::enqueue(const QString &channel, const QString &message) {
    if (m_queuedMessages.count() == (m_queuedMessages.size() - 1) )
        /* 17 messages max */
        return;
    m_queuedMessages.insert(channel, new QString(message));
}

IRCChannel *IRCChannelTab::channel() {
    return m_channel;
}

IRCChannelList *IRCChannelTab::list() {
    return m_list;
}