Esempio n. 1
0
void KviModeEditor::commit()
{
	if(!m_pChannel)
	{
		emit done();
		return;
	}

	QMap<char, QString> szPlusModes;
	QMap<char, QString> szMinusModes;

	QCheckBox * pCheckBox;
	QLineEdit * pLineEdit;
	char cMode = 0;

	QList<char> modes = m_pCheckBoxes.keys();

	for(int i = 0; i < modes.count(); ++i)
	{
		cMode = modes.at(i);
		pCheckBox = m_pCheckBoxes.value(cMode);
		if(!pCheckBox)
			continue; // wtf!?

		pLineEdit = m_pLineEdits.value(cMode);
		if(pLineEdit)
		{
			// mode with parameter (with an exception)
			if(pCheckBox->isChecked())
			{
				// mode is checked
				if(!m_pChannel->hasChannelMode(cMode) || (pLineEdit->text().trimmed() != m_pChannel->channelModeParam(cMode)))
				{
					// mode was not checked before, or the parameter has changed
					szPlusModes.insert(cMode, pLineEdit->text().trimmed());
				}
			}
			else
			{
				// mode is not checked
				if(m_pChannel->hasChannelMode(cMode))
				{
					// but it was checked before

					// checks if this specific mode does not need a parameter when set
					if(modeNeedsParameterOnlyWhenSet(cMode))
					{
						szMinusModes.insert(cMode, QString());
					}
					else
					{
						szMinusModes.insert(cMode, m_pChannel->channelModeParam(cMode));
					}
				}
			}
		}
		else
		{
			//mode without parameter
			if(pCheckBox->isChecked())
			{
				if(!m_pChannel->plainChannelMode().contains(cMode))
					szPlusModes.insert(cMode, QString());
			}
			else
			{
				if(m_pChannel->plainChannelMode().contains(cMode))
					szMinusModes.insert(cMode, QString());
			}
		}
	}

	// now flush out mode changes
	int iModesPerLine = 3; // a good default
	KviIrcConnectionServerInfo * pServerInfo = nullptr;
	if(m_pChannel)
		pServerInfo = m_pChannel->serverInfo();
	if(pServerInfo)
	{
		iModesPerLine = pServerInfo->maxModeChanges();
		if(iModesPerLine < 1)
			iModesPerLine = 1;
	}

	QString szModes;
	QStringList szParameters;
	int iModes = 0;

	QMap<char, QString>::const_iterator iter = szMinusModes.constBegin();
	while(iter != szMinusModes.constEnd())
	{
		if(iter == szMinusModes.constBegin())
			szModes.append("-");
		szModes.append(iter.key());
		szParameters.append(iter.value());
		++iModes;
		++iter;

		//time to commit?
		if(iModes == iModesPerLine)
		{
			QString szCommitModes = szModes;
			if(iter == szMinusModes.constEnd())
				szModes.clear();
			else
				szModes = "-";
			if(szParameters.count())
			{
				szCommitModes.append(QChar(' '));
				szCommitModes.append(szParameters.join(QString(" ")));
				szParameters.clear();
			}
			iModes = 0;

			emit setMode(szCommitModes);
		}
	}

	iter = szPlusModes.constBegin();
	while(iter != szPlusModes.constEnd())
	{
		if(iter == szPlusModes.constBegin())
			szModes.append("+");
		szModes.append(iter.key());
		szParameters.append(iter.value());
		++iModes;
		++iter;

		//time to commit? this should be an ==, but includes the minus sign so "+aaa" = 4 chars
		if(iModes == iModesPerLine)
		{
			QString szCommitModes = szModes;
			if(iter == szPlusModes.constEnd())
				szModes.clear();
			else
				szModes = "+";
			if(szParameters.count())
			{
				szCommitModes.append(QChar(' '));
				szCommitModes.append(szParameters.join(QString(" ")));
				szParameters.clear();
			}
			iModes = 0;

			emit setMode(szCommitModes);
		}
	}

	if(iModes)
	{
		QString szCommitModes = szModes;
		szModes.clear();
		if(szParameters.count())
		{
			szCommitModes.append(QChar(' '));
			szCommitModes.append(szParameters.join(QString(" ")));
			szParameters.clear();
		}
		emit setMode(szCommitModes);
	}

	emit done();
}
Esempio n. 2
0
void KviModeWidget::editorReturnPressed()
{
	QMap<char, QString> szPlusModes;
	QMap<char, QString> szMinusModes;

	QString szTmpMode;
	m_pChannel->getChannelModeStringWithEmbeddedParams(szTmpMode);
	QStringList szOldModes=szTmpMode.split(QChar(' '), QString::SkipEmptyParts);
	QStringList szNewModes=text().split(QChar(' '), QString::SkipEmptyParts);

	//add new modes and modified ones
	for(int i=0; i<szNewModes.count(); ++i)
	{
		QString szSubstring=szNewModes.at(i);
		if(i)
		{
			// not first part: mode with parameter
			if(szSubstring.size() < 3)
				continue;
			if(szSubstring.at(1) != QChar(':'))
				continue;
			char cMode = szSubstring.at(0).unicode();
			szSubstring.remove(0,2);

			if(!m_pChannel->hasChannelMode(cMode) ||
				(szSubstring != m_pChannel->channelModeParam(cMode)))
			{
				// mode was not set before, or the parameter has changed
				szPlusModes.insert(cMode, szSubstring);
			}
		} else {
			// first part: parameterless modes
			QString szCurModes = szOldModes.count() ? szOldModes.at(0) : "";
			for(int j=0; j<szSubstring.length(); ++j)
			{
				char cMode = szSubstring.at(j).unicode();
				if(!szCurModes.contains(cMode))
				{
					// was not set, has to be inserted
					szPlusModes.insert(cMode,QString());
				}
			}
		}
	}

	// check for any mode that has been unset
	for(int i=0; i<szOldModes.count(); ++i)
	{
		QString szSubstring=szOldModes.at(i);
		if(i)
		{
			// not first part: mode with parameter
			if(szSubstring.size() < 3)
				continue;
			if(szSubstring.at(1) != QChar(':'))
				continue;
			char cMode = szSubstring.at(0).unicode();
			szSubstring.remove(0,2);

			// we skip parameterless modes (j=0)
			bool bStillSet=false;
			for(int j=1; j<szNewModes.length(); ++j)
			{
				if(szNewModes.at(j).at(0) == cMode)
					bStillSet=true;
			}
			if(!bStillSet)
			{
				// checks if this specific mode does not need a parameter when set
				if(modeNeedsParameterOnlyWhenSet(cMode))
				{
					szMinusModes.insert(cMode,QString());
				} else {
					szMinusModes.insert(cMode, szSubstring);
				}
			}
		} else {
			// first part: parameterless modes
			QString szNewParameterLessModes = szNewModes.count() ? szNewModes.at(0) : "";
			for(int j=0; j<szSubstring.length(); ++j)
			{
				char cMode = szSubstring.at(j).unicode();
				if(!szNewParameterLessModes.contains(cMode))
				{
					// was set, has to be unset
					szMinusModes.insert(cMode,QString());
				}
			}
		}
	}

	// now flush out mode changes
	int iModesPerLine=3; // a good default
	KviIrcConnectionServerInfo * pServerInfo = 0;
	if(m_pChannel) pServerInfo = m_pChannel->serverInfo();
	if(pServerInfo)
	{
		iModesPerLine = pServerInfo->maxModeChanges();
		if(iModesPerLine < 1) iModesPerLine = 1;
	}

	QString szModes;
	QStringList szParameters;
	int iModes=0;

	QMap<char, QString>::const_iterator iter = szMinusModes.constBegin();
	while (iter != szMinusModes.constEnd())
	{
		if(iter == szMinusModes.constBegin())
			szModes.append("-");
		szModes.append(iter.key());
		szParameters.append(iter.value());
		++iModes;
		++iter;

		//time to commit?
		if(iModes == iModesPerLine)
		{
			QString szCommitModes = szModes;
			if(iter == szMinusModes.constEnd())
				szModes.clear();
			else
				szModes="-";
			if(szParameters.count())
			{
				szCommitModes.append(QChar(' '));
				szCommitModes.append(szParameters.join(QString(" ")));
				szParameters.clear();
			}
			iModes=0;

			emit setMode(szCommitModes);
		}
	}

	iter = szPlusModes.constBegin();
	while (iter != szPlusModes.constEnd())
	{
		if(iter == szPlusModes.constBegin())
			szModes.append("+");
		szModes.append(iter.key());
		szParameters.append(iter.value());
		++iModes;
		++iter;

		//time to commit? this should be an ==, but includes the minus sign so "+aaa" = 4 chars
		if(iModes == iModesPerLine)
		{
			QString szCommitModes = szModes;
			if(iter == szPlusModes.constEnd())
				szModes.clear();
			else
				szModes="+";
			if(szParameters.count())
			{
				szCommitModes.append(QChar(' '));
				szCommitModes.append(szParameters.join(QString(" ")));
				szParameters.clear();
			}
			iModes=0;

			emit setMode(szCommitModes);
		}
	}

	if(iModes)
	{
		QString szCommitModes = szModes;
		szModes.clear();
		if(szParameters.count())
		{
			szCommitModes.append(QChar(' '));
			szCommitModes.append(szParameters.join(QString(" ")));
			szParameters.clear();
		}
		emit setMode(szCommitModes);
	}

	reset();
}