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 |
| 21 | #include <boost/filesystem.hpp> |
| 22 | #include "logging.h" |
| 23 | #include "exception.h" |
| 24 | #endif |
| 25 | |
| 26 | namespace fs = boost::filesystem; |
| 27 | using namespace ndn; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 28 | using namespace std; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 29 | |
| 30 | INIT_LOGGER("ContactPanel"); |
| 31 | |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 32 | ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 33 | : QDialog(parent) |
| 34 | , ui(new Ui::ContactPanel) |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | , m_contactManager(contactManager) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 36 | , m_contactListModel(new QStringListModel) |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 37 | , m_addContactPanel(new AddContactPanel(contactManager)) |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 38 | , m_setAliasDialog(new SetAliasDialog(contactManager)) |
| 39 | , m_menuInvite(new QAction("&Invite", this)) |
| 40 | , m_menuAlias(new QAction("&Set Alias", this)) |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 41 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 43 | ui->setupUi(this); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 45 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 46 | QString path = (QDir::home().path()); |
| 47 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 48 | db.setDatabaseName(path); |
| 49 | bool ok = db.open(); |
| 50 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 51 | m_profileEditor = new ProfileEditor(m_contactManager); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 53 | refreshContactList(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 54 | ui->ContactList->setModel(m_contactListModel); |
| 55 | |
| 56 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 58 | connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 59 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 60 | connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 61 | this, SLOT(showContextMenu(const QPoint&))); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 62 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 63 | this, SLOT(openProfileEditor())); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 64 | |
| 65 | connect(ui->AddContactButton, SIGNAL(clicked()), |
| 66 | this, SLOT(openAddContactPanel())); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 67 | |
| 68 | connect(m_addContactPanel, SIGNAL(newContactAdded()), |
| 69 | this, SLOT(refreshContactList())); |
| 70 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 71 | this, SLOT(refreshContactList())); |
| 72 | |
| 73 | |
| 74 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ContactPanel::~ContactPanel() |
| 78 | { |
| 79 | delete ui; |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 80 | delete m_contactListModel; |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 81 | delete m_profileEditor; |
| 82 | delete m_addContactPanel; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 83 | |
| 84 | delete m_menuInvite; |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 87 | void |
| 88 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 89 | const QItemSelection &deselected) |
| 90 | { |
| 91 | QModelIndexList items = selected.indexes(); |
| 92 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 93 | string alias = text.toUtf8().constData(); |
| 94 | |
| 95 | m_currentSelectedContact = alias; |
| 96 | |
| 97 | int i = 0; |
| 98 | for(; i < m_contactList.size(); i++) |
| 99 | { |
| 100 | if(alias == m_contactList[i]->getAlias()) |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | QString name = QString::fromUtf8(m_contactList[i]->getName().c_str()); |
| 105 | QString institution = QString::fromUtf8(m_contactList[i]->getInstitution().c_str()); |
| 106 | QString nameSpace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str()); |
| 107 | ui->NameData->setText(name); |
| 108 | ui->NameSpaceData->setText(nameSpace); |
| 109 | ui->InstitutionData->setText(institution); |
| 110 | |
| 111 | // _LOG_DEBUG("current Alias: " << m_currentSelectedContact); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 112 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 114 | void |
| 115 | ContactPanel::openProfileEditor() |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 116 | { m_profileEditor->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 118 | void |
| 119 | ContactPanel::openAddContactPanel() |
| 120 | { m_addContactPanel->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 121 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 122 | void |
| 123 | ContactPanel::refreshContactList() |
| 124 | { |
| 125 | m_contactList = m_contactManager->getContactItemList(); |
| 126 | QStringList contactNameList; |
| 127 | for(int i = 0; i < m_contactList.size(); i++) |
| 128 | contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str()); |
| 129 | |
| 130 | m_contactListModel->setStringList(contactNameList); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | ContactPanel::showContextMenu(const QPoint& pos) |
| 135 | { |
| 136 | QMenu menu(ui->ContactList); |
| 137 | menu.addAction(m_menuInvite); |
| 138 | menu.addAction(m_menuAlias); |
| 139 | connect(m_menuAlias, SIGNAL(triggered()), |
| 140 | this, SLOT(openSetAliasDialog())); |
| 141 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | ContactPanel::openSetAliasDialog() |
| 147 | { |
| 148 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact); |
| 149 | m_setAliasDialog->show(); |
| 150 | } |
| 151 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 152 | #if WAF |
| 153 | #include "contactpanel.moc" |
| 154 | #include "contactpanel.cpp.moc" |
| 155 | #endif |