blob: e2c83592ff483c5c7e98dd07a9763ca86e849ece [file] [log] [blame]
Yingdi Yu9e0dc292013-10-10 11:39:45 -07001/* -*- 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 Yudef90612013-10-09 22:34:42 -070011#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu01a942b2013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yu01a942b2013-10-10 15:00:58 -070019
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070020#ifndef Q_MOC_RUN
Yingdi Yu100267f2013-10-21 15:01:40 -070021#include <ndn.cxx/common.h>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070022#include <boost/filesystem.hpp>
23#include "logging.h"
24#include "exception.h"
25#endif
26
27namespace fs = boost::filesystem;
28using namespace ndn;
Yingdi Yu79c25a22013-10-21 13:38:38 -070029using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070030
31INIT_LOGGER("ContactPanel");
32
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070033ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070034 : QDialog(parent)
35 , ui(new Ui::ContactPanel)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070036 , m_contactManager(contactManager)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070037 , m_contactListModel(new QStringListModel)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070038 , m_addContactPanel(new AddContactPanel(contactManager))
Yingdi Yu79c25a22013-10-21 13:38:38 -070039 , m_setAliasDialog(new SetAliasDialog(contactManager))
Yingdi Yu100267f2013-10-21 15:01:40 -070040 , m_startChatDialog(new StartChatDialog)
41 , m_menuInvite(new QAction("&Chat", this))
Yingdi Yu79c25a22013-10-21 13:38:38 -070042 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yudef90612013-10-09 22:34:42 -070043{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070044
Yingdi Yudef90612013-10-09 22:34:42 -070045 ui->setupUi(this);
Yingdi Yu01a942b2013-10-10 15:00:58 -070046
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070047 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 Yuaa8d7692013-10-18 17:05:02 -070053 m_profileEditor = new ProfileEditor(m_contactManager);
Yingdi Yuc26af3c2013-10-17 17:03:46 -070054
Yingdi Yu79c25a22013-10-21 13:38:38 -070055 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070056 ui->ContactList->setModel(m_contactListModel);
57
58 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yud59b5932013-10-16 13:16:49 -070059
Yingdi Yu01a942b2013-10-10 15:00:58 -070060 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
61 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu79c25a22013-10-21 13:38:38 -070062 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
63 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070064 connect(ui->EditProfileButton, SIGNAL(clicked()),
65 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -070066
67 connect(ui->AddContactButton, SIGNAL(clicked()),
68 this, SLOT(openAddContactPanel()));
Yingdi Yu79c25a22013-10-21 13:38:38 -070069
70 connect(m_addContactPanel, SIGNAL(newContactAdded()),
71 this, SLOT(refreshContactList()));
72 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
73 this, SLOT(refreshContactList()));
74
Yingdi Yu100267f2013-10-21 15:01:40 -070075 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
76 this, SLOT(startChatroom(const QString&, const QString&, bool)));
77
Yingdi Yu79c25a22013-10-21 13:38:38 -070078
79
Yingdi Yudef90612013-10-09 22:34:42 -070080}
81
82ContactPanel::~ContactPanel()
83{
84 delete ui;
Yingdi Yu01a942b2013-10-10 15:00:58 -070085 delete m_contactListModel;
Yingdi Yud59b5932013-10-16 13:16:49 -070086 delete m_profileEditor;
87 delete m_addContactPanel;
Yingdi Yu79c25a22013-10-21 13:38:38 -070088
89 delete m_menuInvite;
Yingdi Yudef90612013-10-09 22:34:42 -070090}
Yingdi Yu9e0dc292013-10-10 11:39:45 -070091
Yingdi Yu01a942b2013-10-10 15:00:58 -070092void
93ContactPanel::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 Yu79c25a22013-10-21 13:38:38 -070098 string alias = text.toUtf8().constData();
99
Yingdi Yu79c25a22013-10-21 13:38:38 -0700100 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 Yu100267f2013-10-21 15:01:40 -0700113
114 m_currentSelectedContactAlias = alias;
115 m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700116 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yu01a942b2013-10-10 15:00:58 -0700117}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700118
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700119void
120ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700121{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700122
Yingdi Yud59b5932013-10-16 13:16:49 -0700123void
124ContactPanel::openAddContactPanel()
125{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700126
Yingdi Yu79c25a22013-10-21 13:38:38 -0700127void
128ContactPanel::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
138void
139ContactPanel::showContextMenu(const QPoint& pos)
140{
141 QMenu menu(ui->ContactList);
142 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700143 connect(m_menuInvite, SIGNAL(triggered()),
144 this, SLOT(openStartChatDialog()));
Yingdi Yu79c25a22013-10-21 13:38:38 -0700145 menu.addAction(m_menuAlias);
146 connect(m_menuAlias, SIGNAL(triggered()),
147 this, SLOT(openSetAliasDialog()));
148 menu.exec(ui->ContactList->mapToGlobal(pos));
149
150}
151
152void
153ContactPanel::openSetAliasDialog()
154{
Yingdi Yu100267f2013-10-21 15:01:40 -0700155 m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700156 m_setAliasDialog->show();
157}
158
Yingdi Yu100267f2013-10-21 15:01:40 -0700159void
160ContactPanel::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
173void
174ContactPanel::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 Yu9e0dc292013-10-10 11:39:45 -0700182#if WAF
183#include "contactpanel.moc"
184#include "contactpanel.cpp.moc"
185#endif