Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -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 | |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 11 | #include "contactpanel.h" |
| 12 | #include "ui_contactpanel.h" |
| 13 | |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QStringList> |
| 16 | #include <QItemSelectionModel> |
| 17 | #include <QModelIndex> |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 18 | #include <QDir> |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 0a6b6c5 | 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 | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 28 | using namespace std; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 29 | |
| 30 | INIT_LOGGER("ContactPanel"); |
| 31 | |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 32 | ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 33 | : QDialog(parent) |
| 34 | , ui(new Ui::ContactPanel) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | , m_contactManager(contactManager) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 36 | , m_contactListModel(new QStringListModel) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 37 | , m_addContactPanel(new AddContactPanel(contactManager)) |
Yingdi Yu | 79c25a2 | 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 | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 41 | { |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 43 | ui->setupUi(this); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 0a6b6c5 | 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 | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 51 | m_profileEditor = new ProfileEditor(m_contactManager); |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 53 | refreshContactList(); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 54 | ui->ContactList->setModel(m_contactListModel); |
| 55 | |
| 56 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 01a942b | 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 | 79c25a2 | 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 | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 62 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 63 | this, SLOT(openProfileEditor())); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 64 | |
| 65 | connect(ui->AddContactButton, SIGNAL(clicked()), |
| 66 | this, SLOT(openAddContactPanel())); |
Yingdi Yu | 79c25a2 | 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 | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ContactPanel::~ContactPanel() |
| 78 | { |
| 79 | delete ui; |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 80 | delete m_contactListModel; |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 81 | delete m_profileEditor; |
| 82 | delete m_addContactPanel; |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 83 | |
| 84 | delete m_menuInvite; |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 85 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 86 | |
Yingdi Yu | 01a942b | 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 | 79c25a2 | 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 | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 112 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 114 | void |
| 115 | ContactPanel::openProfileEditor() |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 116 | { m_profileEditor->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 118 | void |
| 119 | ContactPanel::openAddContactPanel() |
| 120 | { m_addContactPanel->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 121 | |
Yingdi Yu | 79c25a2 | 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 | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 152 | #if WAF |
| 153 | #include "contactpanel.moc" |
| 154 | #include "contactpanel.cpp.moc" |
| 155 | #endif |