Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contactpanel.h" |
| 12 | #include "ui_contactpanel.h" |
| 13 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QStringList> |
| 16 | #include <QItemSelectionModel> |
| 17 | #include <QModelIndex> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 18 | #include <QDir> |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 20 | #ifndef Q_MOC_RUN |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 21 | #include <ndn.cxx/common.h> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 22 | #include <boost/filesystem.hpp> |
| 23 | #include "logging.h" |
| 24 | #include "exception.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace fs = boost::filesystem; |
| 28 | using namespace ndn; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 29 | using namespace std; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 30 | |
| 31 | INIT_LOGGER("ContactPanel"); |
| 32 | |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 33 | ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 34 | : QDialog(parent) |
| 35 | , ui(new Ui::ContactPanel) |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 36 | , m_contactManager(contactManager) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 37 | , m_contactListModel(new QStringListModel) |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 38 | , m_addContactPanel(new AddContactPanel(contactManager)) |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 39 | , m_setAliasDialog(new SetAliasDialog(contactManager)) |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 40 | , m_startChatDialog(new StartChatDialog) |
| 41 | , m_menuInvite(new QAction("&Chat", this)) |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 42 | , m_menuAlias(new QAction("&Set Alias", this)) |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 45 | ui->setupUi(this); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 46 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 47 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 48 | QString path = (QDir::home().path()); |
| 49 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 50 | db.setDatabaseName(path); |
| 51 | bool ok = db.open(); |
| 52 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 53 | m_profileEditor = new ProfileEditor(m_contactManager); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 55 | refreshContactList(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 56 | ui->ContactList->setModel(m_contactListModel); |
| 57 | |
| 58 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 59 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 60 | connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 61 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 62 | connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 63 | this, SLOT(showContextMenu(const QPoint&))); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 64 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 65 | this, SLOT(openProfileEditor())); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 66 | |
| 67 | connect(ui->AddContactButton, SIGNAL(clicked()), |
| 68 | this, SLOT(openAddContactPanel())); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 69 | |
| 70 | connect(m_addContactPanel, SIGNAL(newContactAdded()), |
| 71 | this, SLOT(refreshContactList())); |
| 72 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 73 | this, SLOT(refreshContactList())); |
| 74 | |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 75 | connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)), |
| 76 | this, SLOT(startChatroom(const QString&, const QString&, bool))); |
| 77 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 78 | |
| 79 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ContactPanel::~ContactPanel() |
| 83 | { |
| 84 | delete ui; |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 85 | delete m_contactListModel; |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 86 | delete m_profileEditor; |
| 87 | delete m_addContactPanel; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 88 | |
| 89 | delete m_menuInvite; |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 92 | void |
| 93 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 94 | const QItemSelection &deselected) |
| 95 | { |
| 96 | QModelIndexList items = selected.indexes(); |
| 97 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 98 | string alias = text.toUtf8().constData(); |
| 99 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 100 | int i = 0; |
| 101 | for(; i < m_contactList.size(); i++) |
| 102 | { |
| 103 | if(alias == m_contactList[i]->getAlias()) |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | QString name = QString::fromUtf8(m_contactList[i]->getName().c_str()); |
| 108 | QString institution = QString::fromUtf8(m_contactList[i]->getInstitution().c_str()); |
| 109 | QString nameSpace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str()); |
| 110 | ui->NameData->setText(name); |
| 111 | ui->NameSpaceData->setText(nameSpace); |
| 112 | ui->InstitutionData->setText(institution); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 113 | |
| 114 | m_currentSelectedContactAlias = alias; |
| 115 | m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 116 | // _LOG_DEBUG("current Alias: " << m_currentSelectedContact); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 117 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 118 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 119 | void |
| 120 | ContactPanel::openProfileEditor() |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 121 | { m_profileEditor->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 122 | |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 123 | void |
| 124 | ContactPanel::openAddContactPanel() |
| 125 | { m_addContactPanel->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 127 | void |
| 128 | ContactPanel::refreshContactList() |
| 129 | { |
| 130 | m_contactList = m_contactManager->getContactItemList(); |
| 131 | QStringList contactNameList; |
| 132 | for(int i = 0; i < m_contactList.size(); i++) |
| 133 | contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str()); |
| 134 | |
| 135 | m_contactListModel->setStringList(contactNameList); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | ContactPanel::showContextMenu(const QPoint& pos) |
| 140 | { |
| 141 | QMenu menu(ui->ContactList); |
| 142 | menu.addAction(m_menuInvite); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 143 | connect(m_menuInvite, SIGNAL(triggered()), |
| 144 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 145 | menu.addAction(m_menuAlias); |
| 146 | connect(m_menuAlias, SIGNAL(triggered()), |
| 147 | this, SLOT(openSetAliasDialog())); |
| 148 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | ContactPanel::openSetAliasDialog() |
| 154 | { |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 155 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 156 | m_setAliasDialog->show(); |
| 157 | } |
| 158 | |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame^] | 159 | void |
| 160 | ContactPanel::openStartChatDialog() |
| 161 | { |
| 162 | TimeInterval ti = time::NowUnixTimestamp(); |
| 163 | ostringstream oss; |
| 164 | oss << ti.total_seconds(); |
| 165 | |
| 166 | Name chatroom("/ndn/broadcast/chronos"); |
| 167 | chatroom.append(string("chatroom-") + oss.str()); |
| 168 | |
| 169 | m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri()); |
| 170 | m_startChatDialog->show(); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 175 | { |
| 176 | _LOG_DEBUG("room: " << chatroom.toUtf8().constData()); |
| 177 | _LOG_DEBUG("invitee: " << invitee.toUtf8().constData()); |
| 178 | _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer); |
| 179 | } |
| 180 | |
| 181 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 182 | #if WAF |
| 183 | #include "contactpanel.moc" |
| 184 | #include "contactpanel.cpp.moc" |
| 185 | #endif |