00001 #include <opie2/oresource.h>
00002 #include <qwhatsthis.h>
00003 #include <qhbox.h>
00004 #include <qdict.h>
00005
00006 #include "ircchanneltab.h"
00007 #include "ircservertab.h"
00008 #include "ircmessageparser.h"
00009
00010 #include <opie2/odebug.h>
00011 QDict<QString> IRCChannelTab::m_queuedMessages (17);
00012
00013 IRCChannelTab::IRCChannelTab(IRCChannel *channel, IRCServerTab *parentTab, MainWindow *mainWindow, QWidget *parent, const char *name, WFlags f) : IRCTab(parent, name, f) {
00014 m_mainWindow = mainWindow;
00015 m_parentTab = parentTab;
00016 m_channel = channel;
00017 m_description->setText(tr("Talking on channel") + " <b>" + channel->channelname() + "</b>");
00018 QHBox *hbox = new QHBox(this);
00019 m_textview = new QTextView(hbox);
00020 m_textview->setHScrollBarMode(QScrollView::AlwaysOff);
00021 m_textview->setVScrollBarMode(QScrollView::AlwaysOn);
00022 m_listVisible = TRUE;
00023 m_listButton = new QPushButton(">", m_textview);
00024 m_listButton->setFlat( true );
00025 m_textview->setCornerWidget(m_listButton);
00026 m_textview->setTextFormat(RichText);
00027 QWhatsThis::add(m_textview, tr("Channel discussion"));
00028 connect(m_listButton, SIGNAL(clicked()), this, SLOT(toggleList()));
00029 m_list = new IRCChannelList(m_channel, hbox);
00030 m_list->update();
00031 m_list->setMaximumWidth(LISTWIDTH);
00032 m_field = new IRCHistoryLineEdit(this);
00033 connect(m_field, SIGNAL(nextTab()), this, SIGNAL(nextTab()));
00034 connect(m_field, SIGNAL(prevTab()), this, SIGNAL(prevTab()));
00035 connect(m_field, SIGNAL(closeTab()), this, SLOT(remove()));
00036 connect(this, SIGNAL(editFocus()), m_field, SLOT(setEditFocus()));
00037
00038 QWhatsThis::add(m_field, tr("Type your message here to participate in the channel discussion"));
00039 m_popup = new QPopupMenu(m_list);
00040 m_lines = 0;
00041
00042 QPEApplication::setStylusOperation(m_list->viewport(), QPEApplication::RightOnHold);
00043 connect(m_list, SIGNAL(mouseButtonPressed(int,QListBoxItem*,const QPoint&)), this, SLOT(mouseButtonPressed(int,QListBoxItem*,const QPoint&)));
00044
00045
00046 m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/query",Opie::Core::OResource::SmallIcon),
00047 tr("Query"), this, SLOT(popupQuery()));
00048 m_popup->insertSeparator();
00049 m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/ping",Opie::Core::OResource::SmallIcon),
00050 tr("Ping"), this, SLOT(popupPing()));
00051 m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/version",Opie::Core::OResource::SmallIcon),
00052 tr("Version"), this, SLOT(popupVersion()));
00053 m_popup->insertItem(Opie::Core::OResource::loadPixmap("opieirc/whois",Opie::Core::OResource::SmallIcon),
00054 tr("Whois"), this, SLOT(popupWhois()));
00055
00056 connect(m_mainWindow, SIGNAL(updateScroll()), this, SLOT(scrolling()));
00057 m_layout->add(hbox);
00058 hbox->show();
00059 m_layout->add(m_field);
00060 m_field->setFocus();
00061 m_field->setActiveWindow();
00062
00063 connect(m_field, SIGNAL(returnPressed()), this, SLOT(processCommand()));
00064 connect(m_list, SIGNAL(doubleClicked ( QListBoxItem * ) ), this, SLOT(popupQuery( QListBoxItem * ) ));
00065 settingsChanged();
00066
00067 if(m_queuedMessages[m_channel->channelname()]) {
00068 appendText(*m_queuedMessages[m_channel->channelname()]);
00069 delete m_queuedMessages[m_channel->channelname()];
00070 m_queuedMessages.remove(m_channel->channelname());
00071 }
00072 }
00073
00074 void IRCChannelTab::scrolling(){
00075 m_textview->ensureVisible(0, m_textview->contentsHeight());
00076 }
00077
00078 void IRCChannelTab::appendText(QString text) {
00079
00080 QString txt = m_textview->text() + IRCTab::appendTimestamp( text );
00081 if (m_maxLines > 0 && m_lines >= m_maxLines) {
00082 int firstBreak = txt.find('\n');
00083 if (firstBreak != -1) {
00084 txt = "<qt bgcolor=\"" + m_backgroundColor + "\"/>" + txt.right(txt.length() - (firstBreak + 1));
00085 }
00086 } else {
00087 m_lines++;
00088 }
00089 m_textview->ensureVisible(0, m_textview->contentsHeight());
00090 m_textview->setText(txt);
00091 m_textview->ensureVisible(0, m_textview->contentsHeight());
00092
00093 int p1, p2;
00094 if ( text.contains( IRCMessageParser::tr("Received a CTCP PING from ") ) )
00095 emit ping( title() );
00096 else if ( (p1 = text.find("ping", 0, false) )!= -1 && (p2 = text.find( m_parentTab->server()->nick(), 0,false )) != -1 ) {
00097 int col = text.findRev("color", -1, false);
00098 if ( col < p2 )
00099 emit ping( title() );
00100
00101 }
00102
00103 emit changed(this);
00104 }
00105
00106 IRCChannelTab::~IRCChannelTab() {
00107 m_parentTab->removeChannelTab(this);
00108 }
00109
00110 void IRCChannelTab::processCommand() {
00111 QString text = m_field->text();
00112 if (text.length()>0) {
00113 if (session()->isSessionActive()) {
00114 if (text.startsWith("/") && !text.startsWith("//")) {
00115
00116 m_parentTab->executeCommand(this, text);;
00117 } else {
00118 if (text.startsWith("//"))
00119 text = text.right(text.length()-1);
00120 session()->sendMessage(m_channel, m_field->text());
00121 appendText("<font color=\"" + m_textColor + "\"><</font><font color=\"" + m_selfColor + "\">"+m_parentTab->server()->nick()+"</font><font color=\"" + m_textColor + "\">> "+IRCOutput::toHTML(m_field->text())+"</font><br>");
00122 }
00123 } else {
00124 appendText("<font color=\"" + m_errorColor + "\">"+tr("Disconnected")+"</font><br>");
00125 }
00126 }
00127 m_field->clear();
00128 }
00129
00130 void IRCChannelTab::settingsChanged() {
00131 m_textview->setText("<qt bgcolor=\"" + m_backgroundColor + "\"/>");
00132 m_lines = 0;
00133 }
00134
00135 void IRCChannelTab::toggleList() {
00136 if (m_listVisible) {
00137 m_list->setMaximumWidth(0);
00138 m_listButton->setText("<");
00139 } else {
00140 m_list->setMaximumWidth(LISTWIDTH);
00141 m_listButton->setText(">");
00142 }
00143 m_listVisible = !m_listVisible;
00144 }
00145
00146 void IRCChannelTab::mouseButtonPressed(int mouse, QListBoxItem *, const QPoint &point) {
00147 switch (mouse) {
00148 case 1:
00149 break;
00150 case 2:
00151 m_popup->popup(point);
00152 break;
00153 };
00154 }
00155
00156 void IRCChannelTab::popupQuery( QListBoxItem *item) {
00157 if (item) {
00158 IRCPerson *person = session()->getPerson(item->text());
00159 if (person) {
00160 IRCQueryTab *tab = m_parentTab->getTabForQuery(person);
00161 if (!tab) {
00162 tab = new IRCQueryTab(person, m_parentTab, m_mainWindow, (QWidget *)parent());
00163 m_parentTab->addQueryTab(tab);
00164 m_mainWindow->addTab(tab);
00165 }
00166 }
00167 }
00168 }
00169
00170 void IRCChannelTab::popupQuery() {
00171 if ( m_list->currentItem() != -1 )
00172 popupQuery( m_list->item(m_list->currentItem()));
00173 }
00174
00175 void IRCChannelTab::popupPing() {
00176 if(m_list->currentItem() != -1)
00177 m_parentTab->session()->sendCTCPPing(m_list->text(m_list->currentItem()));
00178 }
00179
00180 void IRCChannelTab::popupVersion() {
00181 if(m_list->currentItem() != -1)
00182 m_parentTab->session()->sendCTCPRequest(m_list->text(m_list->currentItem()), "VERSION", "");
00183 }
00184
00185 void IRCChannelTab::popupWhois() {
00186 if(m_list->currentItem() != -1)
00187 m_parentTab->session()->whois(m_list->text(m_list->currentItem()));
00188 }
00189
00190 QString IRCChannelTab::title() {
00191 if(!m_channel->channelname().startsWith("&"))
00192 return m_channel->channelname();
00193
00194 return "&" + m_channel->channelname();
00195 }
00196
00197 IRCSession *IRCChannelTab::session() {
00198 return m_parentTab->session();
00199 }
00200
00201 void IRCChannelTab::remove() {
00202 if (session()->isSessionActive()) {
00203 session()->part(m_channel);
00204 } else {
00205 m_mainWindow->killTab(this);
00206 }
00207 }
00208
00209 void IRCChannelTab::enqueue(const QString &channel, const QString &message) {
00210 if (m_queuedMessages.count() == (m_queuedMessages.size() - 1) )
00211
00212 return;
00213 m_queuedMessages.insert(channel, new QString(message));
00214 }
00215
00216 IRCChannel *IRCChannelTab::channel() {
00217 return m_channel;
00218 }
00219
00220 IRCChannelList *IRCChannelTab::list() {
00221 return m_list;
00222 }
00223