Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 1 | #include "chatroom-discovery-dialog.hpp" |
| 2 | #include "ui_chatroom-discovery-dialog.h" |
| 3 | |
| 4 | namespace chronos { |
| 5 | |
| 6 | using std::vector; |
| 7 | using ndn::Name; |
| 8 | |
| 9 | ChatroomDiscoveryDialog::ChatroomDiscoveryDialog(QWidget* parent) |
| 10 | : QDialog(parent) |
| 11 | , ui(new Ui::ChatroomDiscoveryDialog) |
| 12 | , m_standardItemModel(new QStandardItemModel(0, 3, this)) |
| 13 | , m_chatroomDiscoveryViewDialog(new ChatroomDiscoveryViewDialog) |
| 14 | { |
| 15 | ui->setupUi(this); |
| 16 | |
| 17 | connect(ui->cancelButton, SIGNAL(clicked()), |
| 18 | this, SLOT(onCancelButtonClicked())); |
| 19 | connect(ui->joinButton, SIGNAL(clicked()), |
| 20 | this, SLOT(onJoinButtonClicked())); |
| 21 | connect(ui->viewButton, SIGNAL(clicked()), |
| 22 | this, SLOT(onViewButtonClicked())); |
| 23 | connect(ui->chatroomListView, SIGNAL(clicked(QModelIndex)), |
| 24 | this, SLOT(onChatroomListViewClicked(QModelIndex))); |
| 25 | connect(ui->chatroomListView, SIGNAL(doubleClicked(QModelIndex)), |
| 26 | this, SLOT(onChatroomListViewDoubleClicked(QModelIndex))); |
| 27 | |
| 28 | updateChatroomList(); |
| 29 | } |
| 30 | |
| 31 | ChatroomDiscoveryDialog::~ChatroomDiscoveryDialog() |
| 32 | { |
| 33 | delete ui; |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | ChatroomDiscoveryDialog::updateChatroomList() |
| 38 | { |
| 39 | m_standardItemModel->clear(); |
| 40 | |
| 41 | m_standardItemModel |
| 42 | ->setHorizontalHeaderItem(0, new QStandardItem(QString("Chatroom Name"))); |
| 43 | m_standardItemModel |
| 44 | ->setHorizontalHeaderItem(1, new QStandardItem(QString("Chatroom Trust Model"))); |
| 45 | m_standardItemModel |
| 46 | ->setHorizontalHeaderItem(2, new QStandardItem(QString("Participants or Contacts"))); |
| 47 | |
| 48 | QHeaderView *m_headerView = ui->chatroomListView->horizontalHeader(); |
| 49 | m_headerView->setResizeMode((QHeaderView::ResizeMode)3); |
| 50 | m_headerView->setStretchLastSection(true); |
| 51 | |
| 52 | ui->chatroomListView->setModel(m_standardItemModel); |
| 53 | |
| 54 | int i = 0; |
| 55 | for (Chatrooms::const_iterator it = m_chatrooms.begin(); |
| 56 | it != m_chatrooms.end(); ++it, ++i) { |
| 57 | QStandardItem *item = new QStandardItem(QString::fromStdString(it->first.toUri())); |
| 58 | item->setEditable(false); |
| 59 | m_standardItemModel->setItem(i, 0, item); |
| 60 | |
| 61 | if (it->second.getTrustModel() == ChatroomInfo::TRUST_MODEL_WEBOFTRUST) |
| 62 | item = new QStandardItem(QString("Web of trust")); |
| 63 | else |
| 64 | item = new QStandardItem(QString("Hierarchical")); |
| 65 | item->setEditable(false); |
| 66 | m_standardItemModel->setItem(i, 1, item); |
| 67 | |
| 68 | QString content; |
| 69 | |
| 70 | for (vector<Name>::const_iterator nameIt = it->second.getParticipants().begin(); |
| 71 | nameIt != it->second.getParticipants().end(); nameIt++) { |
| 72 | content.append(QString::fromStdString(nameIt->toUri())).append(","); |
| 73 | } |
| 74 | item = new QStandardItem(content); |
| 75 | item->setEditable(false); |
| 76 | m_standardItemModel->setItem(i, 2, item); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | ChatroomDiscoveryDialog::onDiscoverChatroomChanged(const chronos::ChatroomInfo& info, bool isAdd) |
| 82 | { |
| 83 | if (isAdd) |
| 84 | m_chatrooms[info.getName()] = info; |
| 85 | else |
| 86 | m_chatrooms.erase(info.getName()); |
| 87 | |
| 88 | updateChatroomList(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | ChatroomDiscoveryDialog::onCancelButtonClicked() |
| 93 | { |
| 94 | this->close(); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ChatroomDiscoveryDialog::onJoinButtonClicked() |
| 99 | { |
| 100 | if(ui->chatroomListView->selectionModel()->selectedRows().size() == 0) { |
| 101 | QMessageBox::information(this, tr("ChronoChat"), tr("Please select a chatroom to join")); |
| 102 | } |
| 103 | else { |
| 104 | m_selectedRow = ui->chatroomListView->selectionModel()->selectedRows()[0].row(); |
| 105 | QStandardItem* selectedChatroomName = m_standardItemModel->item(m_selectedRow, 0); |
| 106 | emit startChatroom(selectedChatroomName->text(), false); |
| 107 | |
| 108 | { |
| 109 | // Tmp disabled |
| 110 | // QStandardItem* selectedChatroomTrustModel = m_standardItemModel->item(m_selectedRow, 1); |
| 111 | // if(selectedChatroomTrustModel->text() == "Hierarchical") { |
| 112 | // emit startChatroom(selectedChatroomName->text(), false); |
| 113 | // } |
| 114 | // else if(selectedChatroomTrustModel->text() == "Web of trust") { |
| 115 | // ChatroomDiscoveryLogic::ChatroomList::const_iterator it; |
| 116 | // it = m_chatroomList->find(Name::Component(selectedChatroomName->text().toStdString())); |
| 117 | |
| 118 | // if(it->second.getContacts().size() == 0) { |
| 119 | // QMessageBox messageBox; |
| 120 | // messageBox.addButton(QMessageBox::Ok); |
| 121 | // messageBox.setIcon(QMessageBox::Information); |
| 122 | // messageBox.setText |
| 123 | // ("You do not have a contact in this chatroom. Please choose another chatroom to join"); |
| 124 | // messageBox.exec(); |
| 125 | // } |
| 126 | // else { |
| 127 | // m_sendInvitationRequestDialog->setChatroomName(selectedChatroomName->text()); |
| 128 | // m_sendInvitationRequestDialog->setContacts(it->second.getContacts()); |
| 129 | // m_sendInvitationRequestDialog->show(); |
| 130 | // } |
| 131 | // } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | ChatroomDiscoveryDialog::onChatroomListViewDoubleClicked(const QModelIndex &index) |
| 138 | { |
| 139 | onJoinButtonClicked(); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | ChatroomDiscoveryDialog::onChatroomListViewClicked(const QModelIndex &index) |
| 144 | { |
| 145 | ui->chatroomListView->selectRow(index.row()); |
| 146 | m_selectedRow = index.row(); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | ChatroomDiscoveryDialog::onViewButtonClicked() |
| 151 | { |
| 152 | if(ui->chatroomListView->selectionModel()->selectedRows().size() == 0) { |
| 153 | QMessageBox messageBox; |
| 154 | messageBox.addButton(QMessageBox::Ok); |
| 155 | messageBox.setIcon(QMessageBox::Information); |
| 156 | messageBox.setText("Please select a chatroom to view"); |
| 157 | messageBox.exec(); |
| 158 | } |
| 159 | else { |
| 160 | m_selectedRow = ui->chatroomListView->selectionModel()->selectedRows()[0].row(); |
| 161 | |
| 162 | QStandardItem* selectedChatroomName = m_standardItemModel->item(m_selectedRow); |
| 163 | m_chatroomDiscoveryViewDialog->setChatroomName(selectedChatroomName->text()); |
| 164 | |
| 165 | // QStandardItem *m_selectedTrustModel = m_standardItemModel->item(m_selectedRow, 1); |
| 166 | // m_chatroomDiscoveryViewDialog->setChatroomTrustModel(m_selectedTrustModel->text()); |
| 167 | |
| 168 | //use chatroomlist as parameter to call set participants |
| 169 | //maybe for different chatroom with different trust model??? |
| 170 | //participants can be contacts |
| 171 | |
| 172 | Chatrooms::const_iterator it = |
| 173 | m_chatrooms.find(Name::Component(selectedChatroomName->text().toStdString())); |
| 174 | |
| 175 | if (it != m_chatrooms.end()) { |
| 176 | m_chatroomDiscoveryViewDialog->setChatroomParticipants(it->second.getParticipants()); |
| 177 | m_chatroomDiscoveryViewDialog->show(); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } // namespace chronos |
| 183 | |
| 184 | |
| 185 | #if WAF |
| 186 | #include "chatroom-discovery-dialog.moc" |
| 187 | #endif |