1
//=============================================================================
3
// File : kvi_modeeditor.cpp
4
// Creation date : Sat Apr 14 2001 13:52:11 by Szymon Stefanek
6
// This file is part of the KVirc irc client distribution
7
// Copyright (C) 2001-2008 Szymon Stefanek (pragma at kvirc dot net)
9
// This program is FREE software. You can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your opinion) any later version.
14
// This program is distributed in the HOPE that it will be USEFUL,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// See the GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
//=============================================================================
25
#include "kvi_modeeditor.h"
26
#include "kvi_iconmanager.h"
27
#include "kvi_locale.h"
28
#include "kvi_qstring.h"
29
#include "kvi_console.h"
30
#include "kvi_ircconnection.h"
31
#include "kvi_ircconnectionserverinfo.h"
32
#include "kvi_channel.h"
33
#include "kvi_ircconnectionuserinfo.h"
34
#include "kvi_toolwindows_container.h"
36
#include <QScrollArea>
41
//////////////////////////////////////////////////////////////////////
42
// class KviModeEditor
44
//////////////////////////////////////////////////////////////////////
46
//static char checkable_modes_table[KVI_NUM_CHECKABLE_MODES] = { 'p','s','t','n','m','i'};
48
KviModeEditor::KviModeEditor(QWidget * par,KviWindowToolPageButton* button,const char *,KviConsole * c,const QString &mode,const QString &key,const QString &limit)
49
: KviWindowToolWidget(par,button)
56
QObject * w = parent();
59
if(w->inherits("KviChannel"))
61
KviChannel *chan = ((KviChannel *)w);
62
if(!( chan->isMeHalfOp() || chan->isMeOp() || chan->isMeChanOwner() || chan->isMeChanAdmin() || chan->connection()->userInfo()->hasUserMode('o') || chan->connection()->userInfo()->hasUserMode('O') ) ) isEnabled=0;
68
QGridLayout *pMasterLayout = new QGridLayout(this);
70
setFocusPolicy(Qt::ClickFocus);
72
QScrollArea * pScrollArea = new QScrollArea(this);
73
pScrollArea->viewport()->setBackgroundRole(QPalette::Background);
74
pMasterLayout->addWidget(pScrollArea,0,0);
76
pMasterLayout->setRowStretch(1,1);
78
if(isEnabled) b = new QPushButton(__tr2qs("&Apply"),this);
79
else b = new QPushButton(__tr2qs("Close"),this);
81
pMasterLayout->addWidget(b,1,0);
82
connect(b,SIGNAL(clicked()),this,SLOT(commit()));
84
QWidget * pBackground = new QWidget(pScrollArea->viewport());
86
QGridLayout *g = new QGridLayout(pBackground);
88
QLabel * l = new QLabel("",pBackground);
89
l->setPixmap(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_MODE)));
92
l = new QLabel(__tr2qs("Channel Modes"),pBackground);
94
g->addWidget(l,0,1,1,1);
96
QFrame * f = new QFrame(pBackground);
97
f->setFrameStyle(QFrame::HLine | QFrame::Sunken);
98
g->addWidget(f,1,0,1,3);
103
m_pCheckBoxes = new KviPointerList<QCheckBox>;
104
m_pCheckBoxes->setAutoDelete(false);
107
// first che basic checkable modes pstnmi
108
QString szModes = "pstnmi";
110
while(!szModes.isEmpty())
112
QChar ccc = szModes[0];
115
KviQString::sprintf(tmp,"%c: %Q",ccc.unicode(),&(c->connection()->serverInfo()->getChannelModeDescription(ccc)));
116
QCheckBox * cb = new QCheckBox(tmp,pBackground);
117
cb->setEnabled(isEnabled);
118
m_pCheckBoxes->append(cb);
119
cb->setChecked(m_szMode.contains((char)ccc.unicode()));
121
g->addWidget(cb,i,0,1,3);
124
KviQString::sprintf(tmp,"l: %Q",&(c->connection()->serverInfo()->getChannelModeDescription('l')));
125
m_pLimitBox = new QCheckBox(tmp,pBackground);
126
m_pLimitBox->setEnabled(isEnabled);
128
g->addWidget(m_pLimitBox,i,0,1,3);
129
connect(m_pLimitBox,SIGNAL(toggled(bool)),this,SLOT(limitBoxToggled(bool)));
130
m_pLimitEdit = new QLineEdit(pBackground);
131
m_pLimitEdit->setEnabled(isEnabled);
133
g->addWidget(m_pLimitEdit,i,1,1,2);
134
if(!m_szLimit.isEmpty())
136
m_pLimitBox->setChecked(true);
137
m_pLimitEdit->setText(m_szLimit);
139
m_pLimitEdit->setEnabled(false);
142
KviQString::sprintf(tmp,"k: %Q",&(c->connection()->serverInfo()->getChannelModeDescription('k')));
143
m_pKeyBox = new QCheckBox(tmp,pBackground);
144
m_pKeyBox->setEnabled(isEnabled);
147
g->addWidget(m_pKeyBox,i,0,1,3);
148
connect(m_pKeyBox,SIGNAL(toggled(bool)),this,SLOT(keyBoxToggled(bool)));
149
m_pKeyEdit = new QLineEdit(pBackground);
150
m_pKeyEdit->setEnabled(isEnabled);
152
g->addWidget(m_pKeyEdit,i,1,1,2);
153
if(!m_szKey.isEmpty())
155
m_pKeyBox->setChecked(true);
156
m_pKeyEdit->setText(m_szKey);
158
m_pKeyEdit->setEnabled(false);
163
if(c->connection()->serverInfo())
164
szModes = c->connection()->serverInfo()->supportedChannelModes();
167
int idx = szModes.lastIndexOf(',');
168
if(idx != -1)szModes.remove(0,idx+1);
170
szModes.replace("p","");
171
szModes.replace("s","");
172
szModes.replace("t","");
173
szModes.replace("n","");
174
szModes.replace("m","");
175
szModes.replace("i","");
176
szModes.replace(",","");
177
szModes.replace("b","");
178
szModes.replace("k","");
179
szModes.replace("l","");
181
while(!szModes.isEmpty())
183
QChar ccc = szModes[0];
186
KviQString::sprintf(tmp,"%c: %Q",(char)ccc.unicode(),&(c->connection()->serverInfo()->getChannelModeDescription(ccc)));
187
QCheckBox * cb = new QCheckBox(tmp,pBackground);
188
cb->setEnabled(isEnabled);
189
m_pCheckBoxes->append(cb);
190
cb->setChecked(m_szMode.contains((char)ccc.unicode()));
192
g->addWidget(cb,i,0,1,3);
197
g->setRowStretch(i,1);
198
g->setColumnStretch(2,1);
200
pScrollArea->setWidget(pBackground);
203
KviModeEditor::~KviModeEditor()
205
delete m_pCheckBoxes;
208
void KviModeEditor::limitBoxToggled(bool bChecked)
210
m_pLimitEdit->setEnabled(bChecked);
213
void KviModeEditor::keyBoxToggled(bool bChecked)
215
m_pKeyEdit->setEnabled(bChecked);
218
void KviModeEditor::commit()
223
if(!m_szKey.isEmpty())
226
if(m_pKeyBox->isChecked())
229
QString tmp = m_pKeyEdit->text().trimmed();
236
KviStr mode(KviStr::Format,"-k %s",m_szKey.toUtf8().data()); // FIXME: assuming utf8 is wrong here!
237
emit setMode(mode.ptr());
238
mode.sprintf("+k %s",tmp.toUtf8().data());
239
emit setMode(mode.ptr());
244
KviStr mode(KviStr::Format,"-k %s",m_szKey.toUtf8().data());
245
emit setMode(mode.ptr());
248
// there was no key before
249
if(m_pKeyBox->isChecked())
251
QString tmp = m_pKeyEdit->text().trimmed();
255
KviStr mode(KviStr::Format,"+k %s",tmp.toUtf8().data());
256
emit setMode(mode.ptr());
261
if(!m_szLimit.isEmpty())
263
// had a limit before
264
if(m_pLimitBox->isChecked())
267
QString tmp = m_pLimitEdit->text().trimmed();
269
unsigned int uLimit = tmp.toUInt(&bOk);
274
// not the same limit!
275
KviStr mode(KviStr::Format,"+l %u",uLimit);
276
emit setMode(mode.ptr());
280
// no limit now! reset
281
szMinusModes.append('l');
284
// there was no limit before
285
if(m_pLimitBox->isChecked())
287
QString tmp = m_pLimitEdit->text().trimmed();
289
unsigned int uLimit = tmp.toUInt(&bOk);
292
// new limit to be set
293
KviStr mode(KviStr::Format,"+l %u",uLimit);
294
emit setMode(mode.ptr());
300
for(QCheckBox * cb = m_pCheckBoxes->first();cb;cb = m_pCheckBoxes->next())
302
QString sz = cb->text();
309
if(!m_szMode.contains((char)ccc.unicode()))szPlusModes.append((char)ccc.unicode());
311
if(m_szMode.contains((char)ccc.unicode()))szMinusModes.append((char)ccc.unicode());
318
if(szMinusModes.hasData())szModes.sprintf("-%s",szMinusModes.ptr());
319
if(szPlusModes.hasData())szModes.append(KviStr::Format,"+%s",szPlusModes.ptr());
320
if(szModes.hasData())emit setMode(szModes.ptr());
325
#ifndef COMPILE_USE_STANDALONE_MOC_SOURCES
326
#include "kvi_modeeditor.moc"
327
#endif //!COMPILE_USE_STANDALONE_MOC_SOURCES