File: chatrooms.cpp

package info (click to toggle)
museek%2B 1%3A0.2%2Bsvn20100315.r1208-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 7,628 kB
  • sloc: cpp: 28,853; python: 28,014; ansic: 538; makefile: 122; sh: 117
file content (230 lines) | stat: -rw-r--r-- 7,492 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* museeq - a Qt client to museekd
 *
 * Copyright (C) 2003-2004 Hyriand <hyriand@thegraveyard.org>
 * Copyright 2008 little blue poney <lbponey@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "museeq.h"
#include "publicchat.h"
#include "chatrooms.h"
#include "roomlist.h"
#include "chatroom.h"
#include "images.h"

ChatRooms::ChatRooms(QWidget* parent, const char* name)
          : TabWidget(parent, name) {

	mRoomList = new RoomList(this);
	addTab(mRoomList, tr("*Rooms*"));

	connect(museeq, SIGNAL(disconnected()), SLOT(clear()));

	connect(museeq, SIGNAL(joinedRoom(const QString&, const NRoom&, const QString&, const QStringList&)), SLOT(joined(const QString&, const NRoom&, const QString&, const QStringList&)));
	connect(museeq, SIGNAL(leftRoom(const QString&)), SLOT(left(const QString&)));
	connect(museeq, SIGNAL(saidChatroom(const QString&, const QString&, const QString&)), SLOT(append(const QString&, const QString&, const QString&)));
	connect(museeq, SIGNAL(userJoinedRoom(const QString&, const QString&, const NUserData&)), SLOT(userJoined(const QString&, const QString&, const NUserData&)));
	connect(museeq, SIGNAL(userLeftRoom(const QString&, const QString&)), SLOT(userLeft(const QString&, const QString&)));
	connect(museeq, SIGNAL(roomTickers(const QString&, const NTickers&)), SLOT(setTickers(const QString&, const NTickers&)));
	connect(museeq, SIGNAL(roomsTickers(const NTickerMap&)), SLOT(setTickersForAllRooms(const NTickerMap&)));
	connect(museeq, SIGNAL(roomTickerSet(const QString&, const QString&, const QString&)), SLOT(setTicker(const QString&, const QString&, const QString&)));
	connect(museeq, SIGNAL(askedPublicChat()), SLOT(askedPublicChat()));
	connect(this, SIGNAL(currentChanged(QWidget*)), SLOT(doCurrentChanged(QWidget*)));
}

void ChatRooms::clear() {
	while(count() > 1)
		delete widget(1);
}

void ChatRooms::askedPublicChat() {
	for(int ix = 1; ix < count(); ++ix) {
		PublicChat* _room = dynamic_cast<PublicChat*>(widget(ix));
		if(_room)
			return; // Already exists, nothing to do
	}

	PublicChat* _room = new PublicChat(this);
	addTab(_room, tr("*Public Chat*"));
	setCurrentWidget(_room);
	connect(_room, SIGNAL(highlight(int, QWidget*)), this, SIGNAL(highlight(int)));
	connect(_room, SIGNAL(highlight(int, QWidget*)), this, SLOT(setHighlight(int, QWidget*)));
}

void ChatRooms::joined(const QString& room, const NRoom& r, const QString& owner, const QStringList& ops) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->setUsers(r);
			_room->setOperators(ops);
			_room->setOwner(owner);
			return;
		}
	}

	ChatRoom* _room = new ChatRoom(room, this, false);
	addTab(_room, room);
	setCurrentWidget(_room);
	connect(_room, SIGNAL(highlight(int, QWidget*)), this, SIGNAL(highlight(int)));
	_room->setUsers(r);
    _room->setOperators(ops);
    _room->setOwner(owner);
	connect(_room, SIGNAL(highlight(int, QWidget*)), this, SLOT(setHighlight(int, QWidget*)));
}

void ChatRooms::left(const QString& room) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			delete _room;
			return;
		}
	}
}

void ChatRooms::append(const QString& room, const QString& user, const QString& line) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->append(user, line);
			return;
		}
	}
}

void ChatRooms::userJoined(const QString& room, const QString& user, const NUserData& data) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->userJoined(user, data);
			return;
		}
	}
}

void ChatRooms::userLeft(const QString& room, const QString& user) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->userLeft(user);
			return;
		}
	}
}

void ChatRooms::closeCurrent() {
	ChatRoom* room = dynamic_cast<ChatRoom*>(currentWidget());
	if (room)
        museeq->leaveRoom(room->room());
    else {
        PublicChat* pchat = dynamic_cast<PublicChat*>(currentWidget());
        if (pchat) {
            museeq->stopPublicChat();
            delete pchat;
        }
    }
}

void ChatRooms::setTickers(const QString& room, const NTickers& tickers) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->setUserTicker(tickers);
			break;
		}
	}
}

void ChatRooms::setTickersForAllRooms(const NTickerMap& tickers) {
    NTickerMap::const_iterator it = tickers.begin();
	for(; it != tickers.end(); ++it)
		setTickers(it.key(), it.value());
}

void ChatRooms::setTicker(const QString& room, const QString& user, const QString& message) {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if(_room && _room->room() == room) {
			_room->setUserTicker(user, message);
			break;
		}
	}
}
void ChatRooms::updateTickers() {
	for(int ix = 1; ix < count(); ++ix) {
		ChatRoom* _room = dynamic_cast<ChatRoom*>(widget(ix));
		if (_room)
            _room->updateTickers(museeq->mTickerLength);
	}
}

void ChatRooms::doCurrentChanged(QWidget* widget) {
	selected(widget);
}

void ChatRooms::setHighlight(int highlight, QWidget* chatwidget) {

	ChatRoom * uw = dynamic_cast<ChatRoom*>(chatwidget);
	PublicChat * pc = dynamic_cast<PublicChat*>(chatwidget);

    if (!uw && !pc)
        return;

	int pos;
	int tabHighlighted;
	if (uw) {
        pos = indexOf(uw);
        tabHighlighted = uw->highlighted();
	}
	else {
	    pos = indexOf(pc);
        tabHighlighted = pc->highlighted();
	}

	if(( currentIndex() != pos) && highlight > tabHighlighted )
	{
        if (uw)
            uw->setHighlighted(highlight);
        else
            pc->setHighlighted(highlight);

		if (highlight > 0)// Icon on tab
			tabBar()->setTabIcon(pos, QIcon(IMG("new-element")));
		if (highlight > 1)// Red tab
			tabBar()->setTabTextColor(pos, QColor(255, 0, 0));

	} else if (highlight == 0){
		tabBar()->setTabTextColor(pos, tabBar()->palette().buttonText().color());
		tabBar()->setTabIcon(pos, QIcon());
	}
}

void ChatRooms::selected(QWidget* chatwidget) {
	if (currentIndex() == 0)
		return;
	ChatRoom * uw = dynamic_cast<ChatRoom*>(chatwidget);
	if(uw && uw->highlighted() != 0) {
		uw->setHighlighted(0);
		setHighlight(uw->highlighted(), uw );
	}
	else if (!uw) {
        PublicChat * pc = dynamic_cast<PublicChat*>(chatwidget);
        if(pc && pc->highlighted() != 0) {
            pc->setHighlighted(0);
            setHighlight(pc->highlighted(), pc );
        }
	}
}