//============================================================================= // // File : listwindow.cpp // Creation date : Thu Oct 7 2001 13:27:55 CEST by Szymon Stefanek // // This file is part of the KVirc irc client distribution // Copyright (C) 2001-2008 Szymon Stefanek (pragma at kvirc dot net) // // This program is FREE software. You can redistribute it and/or // modify it under the lists of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your opinion) any later version. // // This program is distributed in the HOPE that it will be USEFUL, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, write to the Free Software Foundation, // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // //============================================================================= #include "listwindow.h" #include "kvi_debug.h" #include "kvi_iconmanager.h" #include "kvi_ircview.h" #include "kvi_out.h" #include "kvi_options.h" #include "kvi_locale.h" #include "kvi_out.h" #include "kvi_mirccntrl.h" #include "kvi_themedlabel.h" #include "kvi_options.h" #include "kvi_ircconnection.h" #include "kvi_ircconnection.h" #include "kvi_qstring.h" #include "kvi_qcstring.h" #include "kvi_topicw.h" #include "kvi_config.h" #include "kvi_filedialog.h" #include "kvi_msgbox.h" #include "kvi_fileextensions.h" #include "kvi_tal_hbox.h" #include "kvi_tal_tooltip.h" #include #include #include #include #include #include extern KviPointerList * g_pListWindowList; KviChannelTreeWidgetItemData::KviChannelTreeWidgetItemData(const QString &szChan,const QString &szUsers,const QString &szTopic) { m_szChan = szChan; m_szUsers = szUsers; m_szTopic = szTopic; } KviChannelTreeWidgetItemData::~KviChannelTreeWidgetItemData() { } KviChannelTreeWidgetItem::KviChannelTreeWidgetItem(KviTalTreeWidget * v,KviChannelTreeWidgetItemData * pData) : KviTalTreeWidgetItem(v) { setText(0, pData->m_szChan); setText(1, pData->m_szUsers); setText(2, pData->m_szTopic); } KviChannelTreeWidgetItem::~KviChannelTreeWidgetItem() { } int KviChannelTreeWidgetItem::width ( const QFontMetrics & fm, const KviTalTreeWidget * , int column ) const { if(column==2) return fm.width(KviMircCntrl::stripControlBytes(text(column))); else return fm.width(text(column)); } bool KviChannelTreeWidgetItem::operator< ( const KviTalTreeWidgetItem & other ) const { int sortCol = treeWidget()->sortColumn(); switch (sortCol) { case 0: //channel return text(sortCol).toUpper() < other.text(sortCol).toUpper(); break; case 1: //users return text(sortCol).toInt() < other.text(sortCol).toInt(); break; case 2: default: //topic return KviMircCntrl::stripControlBytes(text(sortCol)).toUpper() < KviMircCntrl::stripControlBytes(other.text(sortCol)).toUpper(); break; } } #define KVI_LABEL_DEF_BACK 100 #define KVI_LABEL_DEF_FORE 101 void KviChannelTreeWidgetItemDelegate::paint( QPainter * p, const QStyleOptionViewItem & option, const QModelIndex & index ) const { if (option.state & QStyle::State_Selected) p->fillRect(option.rect, option.palette.brush( QPalette::Highlight ) ); //reset the color p->setPen( option.palette.text().color() ); switch (index.column()) { case 0: //channel p->drawText(option.rect, index.data().toString()); break; case 1: //users p->drawText(option.rect, Qt::AlignHCenter, index.data().toString()); break; case 2: default: //topic KviTopicWidget::paintColoredText(p,index.data().toString(),option.palette,option.rect); break; } } KviListWindow::KviListWindow(KviFrame * lpFrm,KviConsole * lpConsole) : KviWindow(KVI_WINDOW_TYPE_LIST,lpFrm,"list",lpConsole) , KviExternalServerDataParser() { g_pListWindowList->append(this); m_pFlushTimer = 0; m_pItemList = new KviPointerList; m_pItemList->setAutoDelete(false); m_pSplitter = new QSplitter(Qt::Horizontal,this); m_pSplitter->setObjectName("splitter"); m_pTopSplitter = new QSplitter(Qt::Horizontal,this); m_pTopSplitter->setObjectName("top_splitter"); m_pVertSplitter = new QSplitter(Qt::Vertical,m_pSplitter); m_pVertSplitter->setObjectName("vsplitter"); KviTalHBox * box = new KviTalHBox(m_pTopSplitter); box->setSpacing(1); box->setMargin(0); m_pOpenButton = new QToolButton(box); m_pOpenButton->setIcon(QIcon(*(g_pIconManager->getBigIcon(KVI_BIGICON_OPEN)))); connect(m_pOpenButton,SIGNAL(clicked()),this,SLOT(importList())); m_pSaveButton = new QToolButton(box); m_pSaveButton->setIcon(QIcon(*(g_pIconManager->getBigIcon(KVI_BIGICON_SAVE)))); connect(m_pSaveButton,SIGNAL(clicked()),this,SLOT(exportList())); m_pRequestButton = new QToolButton(box); m_pRequestButton->setObjectName("request_button"); m_pRequestButton->setIconSize(QSize(16,16)); m_pRequestButton->setIcon(QIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_LIST)))); connect(m_pRequestButton,SIGNAL(clicked()),this,SLOT(requestList())); KviTalToolTip::add(m_pRequestButton,__tr2qs("Request List")); m_pStopListDownloadButton = new QToolButton(box); m_pStopListDownloadButton->setObjectName("stoplistdownload_button"); m_pStopListDownloadButton->setIconSize(QSize(16,16)); m_pStopListDownloadButton->setIcon(QIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_NICKNAMEPROBLEM)))); connect(m_pStopListDownloadButton,SIGNAL(clicked()),this,SLOT(stoplistdownload())); KviTalToolTip::add(m_pStopListDownloadButton,__tr2qs("Stop list download")); m_pParamsEdit = new QLineEdit(box); box->setStretchFactor(m_pParamsEdit,1); KviTalToolTip::add(m_pParamsEdit,__tr2qs("
/LIST command parameters:
Many servers accept special parameters that " \ "allow you to filter the returned entries.
" \ "Commonly, masked channel names (*kvirc*) are accepted as parameters, as well as strings " \ "like c<n or c>n where n is the minimum or maximum of users on the channel.
")); m_pInfoLabel = new KviThemedLabel(m_pTopSplitter,"info_label"); m_pTreeWidget = new KviTalTreeWidget(m_pVertSplitter); m_pTreeWidget->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection); m_pTreeWidget->setItemDelegate(new KviChannelTreeWidgetItemDelegate(m_pTreeWidget)); QStringList columnLabels; columnLabels.append(__tr2qs("Channel")); columnLabels.append(__tr2qs("Users")); columnLabels.append(__tr2qs("Topic")); m_pTreeWidget->setColumnCount(3); m_pTreeWidget->setHeaderLabels(columnLabels); m_pTreeWidget->setAllColumnsShowFocus(TRUE); m_pTreeWidget->setSortingEnabled(TRUE); m_pTreeWidget->sortItems(0,Qt::AscendingOrder); connect(m_pTreeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),this,SLOT(itemDoubleClicked(QTreeWidgetItem *, int))); m_pIrcView = new KviIrcView(m_pVertSplitter,lpFrm,this); m_pConsole->context()->setListWindowPointer(this); connect(m_pConsole->context(),SIGNAL(stateChanged()), this,SLOT(connectionStateChange())); connectionStateChange(); } KviListWindow::~KviListWindow() { g_pListWindowList->removeRef(this); m_pConsole->context()->setListWindowPointer(0); if(m_pFlushTimer)delete m_pFlushTimer; m_pItemList->setAutoDelete(true); delete m_pItemList; } void KviListWindow::getBaseLogFileName(QString &buffer) { buffer.sprintf("LIST_%d",context()->id()); } void KviListWindow::requestList() { if(m_pConsole->isConnected()) { KviStr parms = m_pParamsEdit->text(); if(parms.isEmpty())m_pConsole->connection()->sendFmtData("list"); else m_pConsole->connection()->sendFmtData("list %s",m_pConsole->connection()->encodeText(parms.ptr()).data()); outputNoFmt(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Sent list request, waiting for reply...")); m_pRequestButton->setEnabled(false); } else { outputNoFmt(KVI_OUT_SYSTEMERROR,__tr2qs("Cannot request list: No active connection")); } } void KviListWindow::stoplistdownload() { if(m_pConsole->isConnected()) { m_pConsole->connection()->sendFmtData("list stoplistdownloadnow"); outputNoFmt(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Stopping the list download...")); //G&N mar 2005 } else { outputNoFmt(KVI_OUT_SYSTEMERROR,__tr2qs("Cannot stop list download, no active connection.")); } } void KviListWindow::connectionStateChange() { KviIrcContext::State st = m_pConsole->context()->state(); m_pRequestButton->setEnabled(st == KviIrcContext::Connected); if(st == KviIrcContext::Connected) { QString tmp; KviQString::sprintf(tmp, __tr2qs("Connected to %s (%s)"), m_pConsole->connection()->currentServerName().toUtf8().data(), m_pConsole->currentNetworkName().toUtf8().data()); m_pInfoLabel->setText(tmp); } else { m_pInfoLabel->setText(__tr2qs("List cannot be requested: Not connected to a server")); } } QPixmap * KviListWindow::myIconPtr() { return g_pIconManager->getSmallIcon(KVI_SMALLICON_LIST); } void KviListWindow::resizeEvent(QResizeEvent *) { int hght = m_pTopSplitter->sizeHint().height(); m_pTopSplitter->setGeometry(0,0,width(),hght); m_pSplitter->setGeometry(0,hght,width(),height() - hght); } QSize KviListWindow::sizeHint() const { return m_pSplitter->sizeHint(); } void KviListWindow::fillCaptionBuffers() { KviQString::sprintf(m_szPlainTextCaption,__tr2qs("Channel List [IRC Context %u]"),m_pConsole->context()->id()); KviQString::sprintf(m_szHtmlActiveCaption, __tr2qs("Channel List " \ "[IRC Context %u]"), KVI_OPTION_COLOR(KviOption_colorCaptionTextActive).name().toAscii().data(), KVI_OPTION_COLOR(KviOption_colorCaptionTextActive2).name().toAscii().data(), m_pConsole->context()->id()); KviQString::sprintf(m_szHtmlInactiveCaption, __tr2qs("Channel list " \ "[IRC Context %u]"), KVI_OPTION_COLOR(KviOption_colorCaptionTextInactive).name().toAscii().data(), KVI_OPTION_COLOR(KviOption_colorCaptionTextInactive2).name().toAscii().data(), m_pConsole->context()->id()); } void KviListWindow::die() { close(); } void KviListWindow::exportList() { if(!m_pTreeWidget->topLevelItemCount()) { KviMessageBox::warning(__tr2qs("You cannot export an empty list")); return; } QString szFile; if(connection()) { QString szDate = QDateTime::currentDateTime().toString("d MMM yyyy hh-mm"); KviQString::sprintf(szFile,__tr2qs("Channel list for %Q - %Q"), &(connection()->networkName()),&(szDate)); } else { szFile = __tr2qs("Channel list"); } if(KviFileDialog::askForSaveFileName(szFile,__tr2qs("Choose filename"),szFile, __tr2qs("Configuration files (*.kvc)"),false, false,true,this)) { if(QFileInfo(szFile).completeSuffix()!="kvc") szFile.append(".kvc"); KviConfig cfg(szFile,KviConfig::Write); cfg.clear(); KviChannelTreeWidgetItem *it; for (int i=0;itopLevelItemCount();i++) { it=(KviChannelTreeWidgetItem *)m_pTreeWidget->topLevelItem(i); cfg.setGroup(it->text(0)); // Write properties cfg.writeEntry("topic",it->text(2)); cfg.writeEntry("users",it->text(1)); } } } void KviListWindow::importList() { QString szFile; if(KviFileDialog::askForOpenFileName(szFile,__tr2qs("Choose filename"),QString(),KVI_FILTER_CONFIG,false, false,this)) { if(m_pConsole->isConnected()) { m_pConsole->connection()->sendFmtData("list stoplistdownloadnow"); outputNoFmt(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Stopping the list download...")); //G&N mar 2005 } m_pItemList->clear(); KviConfig cfg(szFile,KviConfig::Read); KviConfigIterator it(*cfg.dict()); while(it.current()) { cfg.setGroup(it.currentKey()); m_pItemList->append( new KviChannelTreeWidgetItemData( it.currentKey(), cfg.readQStringEntry("users","0"), cfg.readQStringEntry("topic","") ) ); ++it; } flush(); } } void KviListWindow::control(int message) { switch(message) { case EXTERNAL_SERVER_DATA_PARSER_CONTROL_RESET: reset(); break; case EXTERNAL_SERVER_DATA_PARSER_CONTROL_STARTOFDATA: startOfList(); break; case EXTERNAL_SERVER_DATA_PARSER_CONTROL_ENDOFDATA: endOfList(); break; } } void KviListWindow::reset() { outputNoFmt(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Reset")); } void KviListWindow::endOfList() { if(m_pFlushTimer) { delete m_pFlushTimer; m_pFlushTimer = 0; } m_pRequestButton->setEnabled(true); outputNoFmt(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Channels list download finished")); flush(); // give it the last kick } void KviListWindow::startOfList() { m_pItemList->setAutoDelete(true); m_pItemList->clear(); m_pItemList->setAutoDelete(false); m_pTreeWidget->clear(); m_pRequestButton->setEnabled(false); } void KviListWindow::processData(KviIrcMessage *msg) { if(!m_pFlushTimer) { m_pFlushTimer = new QTimer(this); connect(m_pFlushTimer,SIGNAL(timeout()),this,SLOT(flush())); m_pFlushTimer->start(1000); m_pRequestButton->setEnabled(false); } QString sz2 = msg->connection()->decodeText(msg->safeParam(2)); m_pItemList->append( new KviChannelTreeWidgetItemData( msg->connection()->decodeText(msg->safeParam(1)), msg->connection()->decodeText(msg->safeParam(2)), msg->connection()->decodeText(msg->safeTrailing())) ); if(_OUTPUT_VERBOSE) { QString zzz = msg->connection()->decodeText(msg->allParams()); output(KVI_OUT_LIST,__tr2qs("Processing list: %Q"),&zzz); } } void KviListWindow::flush() { m_pTreeWidget->setUpdatesEnabled(true); /* for v_scroolbar */ while(KviChannelTreeWidgetItemData * d = m_pItemList->first()) { (void)new KviChannelTreeWidgetItem(m_pTreeWidget,d); m_pItemList->removeFirst(); } m_pTreeWidget->setUpdatesEnabled(true); m_pTreeWidget->viewport()->update(); } void KviListWindow::itemDoubleClicked(KviTalTreeWidgetItem *it, int) { QString sz = it->text(0); if(sz.isEmpty())return; if(!connection())return; KviQCString dat = connection()->encodeText(sz); if(!dat.data())return; m_pConsole->connection()->sendFmtData("join %s",dat.data()); } // //#warning "Load & save properties of this kind of window" //void KviListWindow::saveProperties() //{ // KviWindowProperty p; // p.rect = externalGeometry(); // p.isDocked = isAttacched(); // QValueList l(m_pSplitter->sizes()); // if(l.count() >= 1)p.splitWidth1 = *(l.at(0)); // if(l.count() >= 2)p.splitWidth2 = *(l.at(1)); // p.timestamp = m_pView->timestamp(); // p.imagesVisible = m_pView->imagesVisible(); // p.isMaximized = isAttacched() && isMaximized(); // p.topSplitWidth1 = 0; // p.topSplitWidth2 = 0; // p.topSplitWidth3 = 0; // g_pOptions->m_pWinPropertiesList->setProperty(caption(),&p); //} // //void KviListWindow::setProperties(KviWindowProperty *p) //{ // QValueList l; // l.append(p->splitWidth1); // l.append(p->splitWidth2); // m_pVertSplitter->setSizes(l); // m_pIrcView->setTimestamp(p->timestamp); // m_pIrcView->setShowImages(p->imagesVisible); //} void KviListWindow::applyOptions() { m_pIrcView->applyOptions(); KviWindow::applyOptions(); } #ifndef COMPILE_USE_STANDALONE_MOC_SOURCES #include "listwindow.moc" #endif //!COMPILE_USE_STANDALONE_MOC_SOURCES