QTfrontend/chatwidget.cpp
changeset 2845 19db164dd20d
parent 2779 e1ae0019d43f
child 2846 1cb8b4c425ed
equal deleted inserted replaced
2844:cea15ef417ea 2845:19db164dd20d
    22 #include <QAction>
    22 #include <QAction>
    23 #include <QApplication>
    23 #include <QApplication>
    24 #include <QTextDocument>
    24 #include <QTextDocument>
    25 #include <QDir>
    25 #include <QDir>
    26 #include <QSettings>
    26 #include <QSettings>
       
    27 #include <QFile>
       
    28 #include <QTextStream>
    27 
    29 
    28 #include "hwconsts.h"
    30 #include "hwconsts.h"
    29 #include "SDLs.h"
    31 #include "SDLs.h"
    30 #include "gameuiconfig.h"
    32 #include "gameuiconfig.h"
    31 #include "chatwidget.h"
    33 #include "chatwidget.h"
    85 	connect(acKick, SIGNAL(triggered(bool)), this, SLOT(onKick()));
    87 	connect(acKick, SIGNAL(triggered(bool)), this, SLOT(onKick()));
    86 	acBan = new QAction(QAction::tr("Ban"), chatNicks);
    88 	acBan = new QAction(QAction::tr("Ban"), chatNicks);
    87 	connect(acBan, SIGNAL(triggered(bool)), this, SLOT(onBan()));
    89 	connect(acBan, SIGNAL(triggered(bool)), this, SLOT(onBan()));
    88 	acFollow = new QAction(QAction::tr("Follow"), chatNicks);
    90 	acFollow = new QAction(QAction::tr("Follow"), chatNicks);
    89 	connect(acFollow, SIGNAL(triggered(bool)), this, SLOT(onFollow()));
    91 	connect(acFollow, SIGNAL(triggered(bool)), this, SLOT(onFollow()));
       
    92 	acIgnore = new QAction(QAction::tr("Ignore"), chatNicks);
       
    93 	connect(acIgnore, SIGNAL(triggered(bool)), this, SLOT(onIgnore()));
       
    94 	acFriend = new QAction(QAction::tr("Friend"), chatNicks);
       
    95 	connect(acFriend, SIGNAL(triggered(bool)), this, SLOT(onFriend()));
    90 
    96 
    91 	chatNicks->insertAction(0, acInfo);
    97 	chatNicks->insertAction(0, acInfo);
    92 	chatNicks->insertAction(0, acFollow);
    98 	chatNicks->insertAction(0, acFollow);
       
    99 	chatNicks->insertAction(0, acIgnore);
       
   100 	chatNicks->insertAction(0, acFriend);
       
   101 	
       
   102 	showReady = false;
       
   103 }
       
   104 
       
   105 void HWChatWidget::loadList(QStringList & list, const QString & file)
       
   106 {
       
   107 	list.clear();
       
   108 	QFile txt((cfgdir->absolutePath() + "/" + file).toLocal8Bit().constData());
       
   109 	if(!txt.open(QIODevice::ReadOnly))
       
   110 		return;
       
   111 	QTextStream stream(&txt);
       
   112 	stream.setCodec("UTF-8");
       
   113 
       
   114 	while(!stream.atEnd())
       
   115 	{
       
   116 		QString str = stream.readLine();
       
   117 		if(str.startsWith(";") || str.length() == 0)
       
   118 			continue;
       
   119 		list << str.trimmed();
       
   120 	}
       
   121 	list.removeDuplicates();
       
   122 	txt.close();
       
   123 }
       
   124 
       
   125 void HWChatWidget::saveList(QStringList & list, const QString & file)
       
   126 {
       
   127 	QFile txt((cfgdir->absolutePath() + "/" + file).toLocal8Bit().constData());
       
   128 	if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate))
       
   129 		return;
       
   130 	QTextStream stream(&txt);
       
   131 	stream.setCodec("UTF-8");
       
   132 
       
   133 	stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl;
       
   134 	for(int i = 0; i < list.size(); i++)
       
   135 		stream << list[i] << endl;
       
   136 	txt.close();
       
   137 }
       
   138 
       
   139 void HWChatWidget::updateIcon(QListWidgetItem *item)
       
   140 {
       
   141 	QString nick = item->text();
       
   142 
       
   143 	if(ignoreList.contains(nick, Qt::CaseInsensitive))
       
   144 	{
       
   145 		item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_ignore_on" : ":/res/chat_ignore_off") : ":/res/chat_ignore.png"));
       
   146 		item->setForeground(Qt::gray);
       
   147 	}
       
   148 	else if(friendsList.contains(nick, Qt::CaseInsensitive))
       
   149 	{
       
   150 		item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_friend_on" : ":/res/chat_friend_off") : ":/res/chat_friend.png"));
       
   151 		item->setForeground(Qt::green);
       
   152 	}
       
   153 	else
       
   154 	{
       
   155 		item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_default_on" : ":/res/chat_default_off") : ":/res/chat_default.png"));
       
   156 		item->setForeground(QBrush(QColor(0xff, 0xcc, 0x00)));
       
   157 	}
       
   158 }
       
   159 
       
   160 void HWChatWidget::updateIcons()
       
   161 {
       
   162 	for(int i = 0; i < chatNicks->count(); i++)
       
   163 		updateIcon(chatNicks->item(i));
       
   164 }
       
   165 
       
   166 void HWChatWidget::loadLists(const QString & nick)
       
   167 {
       
   168 	loadList(ignoreList, nick.toLower() + "_ignore.txt");
       
   169 	loadList(friendsList, nick.toLower() + "_friends.txt");
       
   170 	updateIcons();
       
   171 }
       
   172 
       
   173 void HWChatWidget::saveLists(const QString & nick)
       
   174 {
       
   175 	saveList(ignoreList, nick.toLower() + "_ignore.txt");
       
   176 	saveList(friendsList, nick.toLower() + "_friends.txt");
    93 }
   177 }
    94 
   178 
    95 void HWChatWidget::returnPressed()
   179 void HWChatWidget::returnPressed()
    96 {
   180 {
    97 	emit chatLine(chatEditLine->text());
   181 	emit chatLine(chatEditLine->text());
    98 	chatEditLine->clear();
   182 	chatEditLine->clear();
    99 }
   183 }
   100 
   184 #include <QMessageBox>
   101 void HWChatWidget::onChatString(const QString& str)
   185 void HWChatWidget::onChatString(const QString& str)
   102 {
   186 {
   103 	if (chatStrings.size() > 250)
   187 	if (chatStrings.size() > 250)
   104 		chatStrings.removeFirst();
   188 		chatStrings.removeFirst();
   105 
   189 
   106 	QString formattedStr = Qt::escape(str.mid(1));
   190 	QString formattedStr = Qt::escape(str.mid(1));
       
   191 	QStringList parts = formattedStr.split(QRegExp("\\W+"), QString::SkipEmptyParts);
       
   192 
       
   193 	if (!formattedStr.startsWith(" ***")) // don't ignore status messages
       
   194 	{
       
   195 		if (formattedStr.startsWith(" *")) // emote
       
   196 			parts[0] = parts[1];
       
   197 		if(parts.size() > 0 && ignoreList.contains(parts[0], Qt::CaseInsensitive))
       
   198 			return;
       
   199 	}
       
   200 
       
   201 	QString color("");
       
   202 	bool isFriend = friendsList.contains(parts[0], Qt::CaseInsensitive);
       
   203 	
   107 	if (str.startsWith("\x03"))
   204 	if (str.startsWith("\x03"))
   108 		formattedStr = QString("<font color=grey>%1</font>").arg(formattedStr);
   205 		color = QString("#c0c0c0");
   109 	else if (str.startsWith("\x02"))
   206 	else if (str.startsWith("\x02"))
   110 		formattedStr = QString("<font color=magenta>%1</font>").arg(formattedStr);
   207 		color = QString(isFriend ? "#00ff00" : "#ff00ff");
   111 
   208 	else if (isFriend)
       
   209 		color = QString("#00c000");
       
   210 
       
   211 	if(color.compare("") != 0)
       
   212 		formattedStr = QString("<font color=\"%2\">%1</font>").arg(formattedStr).arg(color);
   112 
   213 
   113 	chatStrings.append(formattedStr);
   214 	chatStrings.append(formattedStr);
   114 
   215 
   115 	chatText->setHtml(chatStrings.join("<br>"));
   216 	chatText->setHtml(chatStrings.join("<br>"));
   116 
   217 
   130 }
   231 }
   131 
   232 
   132 void HWChatWidget::nickAdded(const QString& nick, bool notifyNick)
   233 void HWChatWidget::nickAdded(const QString& nick, bool notifyNick)
   133 {
   234 {
   134 	QListWidgetItem * item = new QListWidgetItem(nick);
   235 	QListWidgetItem * item = new QListWidgetItem(nick);
   135 	item->setIcon(QIcon(":/res/hh_small.png"));
   236 	updateIcon(item);
   136 	chatNicks->addItem(item);
   237 	chatNicks->addItem(item);
   137 
   238 
   138     if(notifyNick && notify && gameSettings->value("audio/frontendsound", true).toBool()) {
   239     if(notifyNick && notify && gameSettings->value("audio/frontendsound", true).toBool()) {
   139        Mix_PlayChannel(-1, sound[rand()%4], 0);
   240        Mix_PlayChannel(-1, sound[rand()%4], 0);
   140     }
   241     }
   182 	QListWidgetItem * curritem = chatNicks->currentItem();
   283 	QListWidgetItem * curritem = chatNicks->currentItem();
   183 	if (curritem)
   284 	if (curritem)
   184 		emit follow(curritem->text());
   285 		emit follow(curritem->text());
   185 }
   286 }
   186 
   287 
       
   288 void HWChatWidget::onIgnore()
       
   289 {
       
   290 	QListWidgetItem * curritem = chatNicks->currentItem();
       
   291 	if(!curritem)
       
   292 		return;
       
   293 
       
   294 	if(ignoreList.contains(curritem->text(), Qt::CaseInsensitive)) // already on list - remove him
       
   295 	{
       
   296 		ignoreList.removeAll(curritem->text().toLower());
       
   297 		onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your ignore list").arg('\x03').arg(curritem->text()));
       
   298 	}
       
   299 	else // not on list - add
       
   300 	{
       
   301 		ignoreList << curritem->text().toLower();
       
   302 		onChatString(HWChatWidget::tr("%1 *** %2 has been added to your ignore list").arg('\x03').arg(curritem->text()));
       
   303 	}
       
   304 	updateIcon(curritem);
       
   305 }
       
   306 
       
   307 void HWChatWidget::onFriend()
       
   308 {
       
   309 	QListWidgetItem * curritem = chatNicks->currentItem();
       
   310 	if(!curritem)
       
   311 		return;
       
   312 
       
   313 	if(friendsList.contains(curritem->text(), Qt::CaseInsensitive)) // already on list - remove him
       
   314 	{
       
   315 		friendsList.removeAll(curritem->text().toLower());
       
   316 		onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your friends list").arg('\x03').arg(curritem->text()));
       
   317 	}
       
   318 	else // not on list - add
       
   319 	{
       
   320 		friendsList << curritem->text().toLower();
       
   321 		onChatString(HWChatWidget::tr("%1 *** %2 has been added to your friends list").arg('\x03').arg(curritem->text()));
       
   322 	}
       
   323 	updateIcon(curritem);
       
   324 }
       
   325 
   187 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)
   326 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)
   188 {
   327 {
   189 	if (item) onFollow();
   328 	if (item) onFollow();
       
   329 }
       
   330 
       
   331 void HWChatWidget::setShowReady(bool s)
       
   332 {
       
   333 	showReady = s;
   190 }
   334 }
   191 
   335 
   192 void HWChatWidget::setReadyStatus(const QString & nick, bool isReady)
   336 void HWChatWidget::setReadyStatus(const QString & nick, bool isReady)
   193 {
   337 {
   194 	QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
   338 	QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
   196 	{
   340 	{
   197 		qWarning("Bug: cannot find user in chat");
   341 		qWarning("Bug: cannot find user in chat");
   198 		return;
   342 		return;
   199 	}
   343 	}
   200 
   344 
   201 	if(isReady)
   345 	/*if(isReady)
   202 		items[0]->setIcon(QIcon(":/res/lightbulb_on.png"));
   346 		items[0]->setIcon(QIcon(":/res/lightbulb_on.png"));
   203 	else
   347 	else
   204 		items[0]->setIcon(QIcon(":/res/lightbulb_off.png"));
   348 		items[0]->setIcon(QIcon(":/res/lightbulb_off.png"));*/
       
   349 
       
   350 	items[0]->setData(Qt::UserRole, isReady); // bulb status
       
   351 	updateIcon(items[0]);
       
   352 
       
   353 	// ensure we're still showing the status bulbs
       
   354 	showReady = true;
   205 }
   355 }
   206 
   356 
   207 void HWChatWidget::adminAccess(bool b)
   357 void HWChatWidget::adminAccess(bool b)
   208 {
   358 {
   209 	chatNicks->removeAction(acKick);
   359 	chatNicks->removeAction(acKick);