29 #include <KInputDialog>
30 #include <KMessageBox>
31 #include <KNotification>
32 #include <KPushButton>
33 #include <KCompletionBox>
35 #include <TelepathyQt/AccountSet>
36 #include <TelepathyQt/AccountCapabilityFilter>
37 #include <TelepathyQt/AccountManager>
38 #include <TelepathyQt/AccountPropertyFilter>
39 #include <TelepathyQt/AndFilter>
40 #include <TelepathyQt/ChannelTypeRoomListInterface>
41 #include <TelepathyQt/PendingChannel>
42 #include <TelepathyQt/PendingReady>
43 #include <TelepathyQt/RequestableChannelClassSpec>
44 #include <TelepathyQt/RoomListChannel>
46 #include <QSortFilterProxyModel>
54 , m_recentComp(new KCompletion)
57 ui->
setupUi(joinChatRoomDialog);
58 setMainWidget(joinChatRoomDialog);
59 setWindowIcon(KIcon(QLatin1String(
"telepathy-kde")));
62 KSharedConfigPtr commonConfig = KSharedConfig::openConfig(QLatin1String(
"ktelepathyrc"));
63 m_favoriteRoomsGroup = commonConfig->group(QLatin1String(
"FavoriteRooms"));
64 m_recentRoomsGroup = commonConfig->group(QLatin1String(
"RecentChatRooms"));
66 Q_FOREACH (
const QString &key, m_recentRoomsGroup.keyList()) {
67 if (!m_recentRoomsGroup.readEntry(key, QStringList()).isEmpty()) {
68 m_recentRooms.insert(key, m_recentRoomsGroup.readEntry(key, QStringList()));
75 button(Ok)->setEnabled(
false);
83 Tp::AccountPropertyFilterPtr isOnlineFilter = Tp::AccountPropertyFilter::create();
84 isOnlineFilter->addProperty(QLatin1String(
"online"),
true);
86 Tp::AccountCapabilityFilterPtr capabilityFilter = Tp::AccountCapabilityFilter::create(
87 Tp::RequestableChannelClassSpecList() << Tp::RequestableChannelClassSpec::conferenceTextChatroom());
89 Tp::AccountFilterPtr filter = Tp::AndFilter<Tp::Account>::create((QList<Tp::AccountFilterConstPtr>() <<
96 onAccountSelectionChanged(ui->
comboBox->currentIndex());
99 m_favoritesProxyModel->setSourceModel(m_favoritesModel);
101 m_favoritesProxyModel->setDynamicSortFilter(
true);
103 ui->
listView->setModel(m_favoritesProxyModel);
107 m_recentComp->setCompletionMode(KGlobalSettings::CompletionPopup);
108 m_recentComp->setIgnoreCase(
true);
110 ui->
lineEdit->setCompletionObject(m_recentComp);
111 ui->
lineEdit->setAutoDeleteCompletionObject(
true);
119 proxyModel->setSourceModel(m_model);
120 proxyModel->setSortLocaleAware(
true);
121 proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
123 proxyModel->setDynamicSortFilter(
true);
126 ui->
treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
130 connect(ui->
lineEdit, SIGNAL(textChanged(QString)),
this, SLOT(onTextChanged(QString)));
131 connect(ui->
listView, SIGNAL(clicked(QModelIndex)),
this, SLOT(onFavoriteRoomClicked(QModelIndex)));
135 connect(ui->
recentListWidget, SIGNAL(currentTextChanged(QString)),
this, SLOT(onRecentRoomClicked()));
138 connect(ui->
queryPushButton, SIGNAL(clicked(
bool)),
this, SLOT(getRoomList()));
139 connect(ui->
stopPushButton, SIGNAL(clicked(
bool)),
this, SLOT(stopListing()));
140 connect(ui->
treeView, SIGNAL(clicked(QModelIndex)),
this, SLOT(onRoomClicked(QModelIndex)));
141 connect(ui->
filterBar, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterFixedString(QString)));
142 connect(ui->
comboBox, SIGNAL(currentIndexChanged(
int)),
this, SLOT(onAccountSelectionChanged(
int)));
143 connect(button(Ok), SIGNAL(clicked(
bool)),
this, SLOT(addRecentRoom()));
153 return ui->comboBox->currentAccount();
156 void KTp::JoinChatRoomDialog::onAccountSelectionChanged(
int newIndex)
161 if (!ui->comboBox->currentAccount()) {
165 QString accountIdentifier = ui->comboBox->currentAccount()->uniqueIdentifier();
166 m_favoritesProxyModel->setFilterFixedString(accountIdentifier);
169 m_recentComp->clear();
171 Q_FOREACH (
const QString &room, m_recentRooms.value(accountIdentifier)) {
172 m_recentComp->addItem(room);
176 ui->recentListWidget->clear();
177 ui->recentListWidget->addItems(m_recentRooms.value(accountIdentifier));
180 bool recentListNonEmpty = m_recentRooms.value(accountIdentifier).size() > 0;
182 ui->clearRecentPushButton->setEnabled(recentListNonEmpty);
183 ui->removeRecentPushButton->setEnabled(recentListNonEmpty);
186 void KTp::JoinChatRoomDialog::addFavorite()
188 if (!ui->comboBox->currentAccount()) {
193 QString favoriteHandle = ui->lineEdit->text();
194 QString favoriteAccount = ui->comboBox->currentAccount()->uniqueIdentifier();
196 if (m_favoritesModel->containsRoom(favoriteHandle, favoriteAccount)) {
197 KMessageBox::sorry(
this, i18n(
"This room is already in your favorites."));
199 QString favoriteName = KInputDialog::getText(i18n(
"Add room"), i18n(
"Name"), QString(), &ok);
202 QString key = favoriteHandle + favoriteAccount;
205 QVariantList favorite;
206 favorite.append(favoriteName);
207 favorite.append(favoriteHandle);
208 favorite.append(favoriteAccount);
209 m_favoriteRoomsGroup.writeEntry(key, favorite);
210 m_favoriteRoomsGroup.sync();
214 room.insert(QLatin1String(
"name"), favoriteName);
215 room.insert(QLatin1String(
"handle-name"), favoriteHandle);
216 room.insert(QLatin1String(
"account-identifier"), favoriteAccount);
217 m_favoritesModel->addRoom(room);
222 void KTp::JoinChatRoomDialog::removeFavorite()
225 QString favoriteAccount = ui->comboBox->currentAccount()->uniqueIdentifier();
228 QString key = favoriteHandle + favoriteAccount;
230 if(m_favoriteRoomsGroup.keyList().contains(key)) {
231 m_favoriteRoomsGroup.deleteEntry(key);
232 m_favoriteRoomsGroup.sync();
233 m_favoritesModel->removeRoom(room);
235 if (m_favoritesModel->countForAccount(favoriteAccount) == 0) {
236 ui->removeFavoritePushButton->setEnabled(
false);
241 void KTp::JoinChatRoomDialog::addRecentRoom()
243 Tp::AccountPtr account = ui->comboBox->currentAccount();
248 QString accountIdentifier = account->uniqueIdentifier();
249 QString handle = ui->lineEdit->text();
251 if (!handle.isEmpty()) {
252 if (m_recentRooms.contains(accountIdentifier)) {
253 QStringList currentList = m_recentRooms.take(accountIdentifier);
257 if (currentList.contains(handle)) {
258 currentList.move(currentList.indexOf(handle), 0);
259 m_recentRooms.insert(accountIdentifier, currentList);
262 currentList.insert(0, handle);
264 if (currentList.size() > 8) {
265 currentList.removeLast();
268 m_recentRooms.insert(accountIdentifier, currentList);
271 m_recentRooms.insert(accountIdentifier, QStringList(handle));
275 m_recentRoomsGroup.writeEntry(accountIdentifier, m_recentRooms.value(accountIdentifier));
276 m_recentRoomsGroup.sync();
280 void KTp::JoinChatRoomDialog::removeRecentRoom()
282 QString accountIdentifier = ui->comboBox->currentAccount()->uniqueIdentifier();
283 QString handle = ui->recentListWidget->currentItem()->text();
286 QStringList currentList = m_recentRooms.take(accountIdentifier);
287 currentList.removeOne(handle);
288 m_recentRooms.insert(accountIdentifier, currentList);
291 onAccountSelectionChanged(ui->comboBox->currentIndex());
294 m_recentRoomsGroup.writeEntry(accountIdentifier, m_recentRooms.value(accountIdentifier));
295 m_recentRoomsGroup.sync();
298 ui->removeRecentPushButton->setEnabled(
false);
301 void KTp::JoinChatRoomDialog::clearRecentRooms()
303 QString accountIdentifier = ui->comboBox->currentAccount()->uniqueIdentifier();
306 m_recentRooms.remove(accountIdentifier);
309 onAccountSelectionChanged(ui->comboBox->currentIndex());
312 m_recentRoomsGroup.deleteEntry(accountIdentifier);
313 m_recentRoomsGroup.sync();
316 void KTp::JoinChatRoomDialog::getRoomList()
318 Tp::AccountPtr account = ui->comboBox->currentAccount();
324 m_model->clearRoomInfoList();
328 request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(
".ChannelType"),
329 TP_QT_IFACE_CHANNEL_TYPE_ROOM_LIST);
330 request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(
".TargetHandleType"),
334 if (!ui->serverLineEdit->text().isEmpty()) {
335 request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(
".Type.RoomList.Server"),
336 ui->serverLineEdit->text());
339 m_pendingRoomListChannel = account->createAndHandleChannel(request, QDateTime::currentDateTime());
340 connect(m_pendingRoomListChannel, SIGNAL(finished(Tp::PendingOperation*)),
341 this, SLOT(onRoomListChannelReadyForHandling(Tp::PendingOperation*)));
345 void KTp::JoinChatRoomDialog::stopListing()
347 m_iface->StopListing();
350 void KTp::JoinChatRoomDialog::onRoomListChannelReadyForHandling(Tp::PendingOperation *operation)
352 if (operation->isError()) {
353 kDebug() << operation->errorName();
354 kDebug() << operation->errorMessage();
355 QString errorMsg(operation->errorName() + QLatin1String(
": ") + operation->errorMessage());
356 sendNotificationToUser(errorMsg);
358 m_roomListChannel = m_pendingRoomListChannel->channel();
360 connect(m_roomListChannel->becomeReady(),
361 SIGNAL(finished(Tp::PendingOperation*)),
362 SLOT(onRoomListChannelReady(Tp::PendingOperation*)));
366 void KTp::JoinChatRoomDialog::onRoomListChannelReady(Tp::PendingOperation *operation)
368 if (operation->isError()) {
369 kDebug() << operation->errorName();
370 kDebug() << operation->errorMessage();
371 QString errorMsg(operation->errorName() + QLatin1String(
": ") + operation->errorMessage());
372 sendNotificationToUser(errorMsg);
374 m_iface = m_roomListChannel->interface<Tp::Client::ChannelTypeRoomListInterface>();
376 m_iface->ListRooms();
378 connect(m_iface, SIGNAL(ListingRooms(
bool)), SLOT(onListing(
bool)));
379 connect(m_iface, SIGNAL(GotRooms(Tp::RoomInfoList)), SLOT(onGotRooms(Tp::RoomInfoList)));
383 void KTp::JoinChatRoomDialog::onRoomListChannelClosed(Tp::PendingOperation *operation)
385 if (operation->isError()) {
386 kDebug() << operation->errorName();
387 kDebug() << operation->errorMessage();
388 QString errorMsg(operation->errorName() + QLatin1String(
": ") + operation->errorMessage());
389 sendNotificationToUser(errorMsg);
391 ui->queryPushButton->setEnabled(
true);
392 ui->stopPushButton->setEnabled(
false);
396 void KTp::JoinChatRoomDialog::onListing(
bool isListing)
399 kDebug() <<
"listing";
400 ui->queryPushButton->setEnabled(
false);
401 ui->stopPushButton->setEnabled(
true);
403 kDebug() <<
"finished listing";
404 Tp::PendingOperation *op = m_roomListChannel->requestClose();
406 SIGNAL(finished(Tp::PendingOperation*)),
407 SLOT(onRoomListChannelClosed(Tp::PendingOperation*)));
411 void KTp::JoinChatRoomDialog::onGotRooms(Tp::RoomInfoList roomInfoList)
413 m_model->addRooms(roomInfoList);
416 void KTp::JoinChatRoomDialog::onFavoriteRoomClicked(
const QModelIndex &index)
418 if (index.isValid()) {
419 ui->removeFavoritePushButton->setEnabled(
true);
422 ui->removeFavoritePushButton->setEnabled(
false);
426 void KTp::JoinChatRoomDialog::onRecentRoomClicked()
428 ui->removeRecentPushButton->setEnabled(
true);
431 void KTp::JoinChatRoomDialog::onRoomClicked(
const QModelIndex& index)
438 return ui->lineEdit->text();
441 void KTp::JoinChatRoomDialog::onTextChanged(QString newText)
443 if (newText.isEmpty()) {
444 button(Ok)->setEnabled(
false);
445 ui->addFavoritePushButton->setEnabled(
false);
447 button(Ok)->setEnabled(
true);
448 ui->addFavoritePushButton->setEnabled(
true);
452 void KTp::JoinChatRoomDialog::sendNotificationToUser(
const QString& errorMsg)
455 KNotification *notification;
456 notification =
new KNotification(QLatin1String(
"telepathyError"),
this);
458 notification->setText(errorMsg);
459 notification->sendEvent();
462 void KTp::JoinChatRoomDialog::loadFavoriteRooms()
464 QList<QVariantMap> roomList;
466 Q_FOREACH(
const QString &key, m_favoriteRoomsGroup.keyList()) {
467 QVariantList favorite = m_favoriteRoomsGroup.readEntry(key, QVariantList());
468 QString favoriteName = favorite.at(0).toString();
469 QString favoriteHandle = favorite.at(1).toString();
470 QString favoriteAccount = favorite.at(2).toString();
473 room.insert(QLatin1String(
"name"), favoriteName);
474 room.insert(QLatin1String(
"handle-name"), favoriteHandle);
475 room.insert(QLatin1String(
"account-identifier"), favoriteAccount);
476 roomList.append(room);
479 m_favoritesModel->addRooms(roomList);