blob: e2c83592ff483c5c7e98dd07a9763ca86e849ece [file] [log] [blame]
Yingdi Yu847aa862013-10-09 16:35:53 -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
11#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu40eca752013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu3b318c12013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yu40eca752013-10-10 15:00:58 -070019
Yingdi Yu3b318c12013-10-15 17:54:00 -070020#ifndef Q_MOC_RUN
Yingdi Yu595aadc2013-10-21 15:01:40 -070021#include <ndn.cxx/common.h>
Yingdi Yu3b318c12013-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 Yu2ac40fb2013-10-21 13:38:38 -070029using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070030
31INIT_LOGGER("ContactPanel");
32
Yingdi Yu9236c432013-10-18 11:29:25 -070033ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yu3b318c12013-10-15 17:54:00 -070034 : QDialog(parent)
35 , ui(new Ui::ContactPanel)
Yingdi Yu9236c432013-10-18 11:29:25 -070036 , m_contactManager(contactManager)
Yingdi Yu3b318c12013-10-15 17:54:00 -070037 , m_contactListModel(new QStringListModel)
Yingdi Yu9236c432013-10-18 11:29:25 -070038 , m_addContactPanel(new AddContactPanel(contactManager))
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070039 , m_setAliasDialog(new SetAliasDialog(contactManager))
Yingdi Yu595aadc2013-10-21 15:01:40 -070040 , m_startChatDialog(new StartChatDialog)
41 , m_menuInvite(new QAction("&Chat", this))
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070042 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yu847aa862013-10-09 16:35:53 -070043{
Yingdi Yu3b318c12013-10-15 17:54:00 -070044
Yingdi Yu847aa862013-10-09 16:35:53 -070045 ui->setupUi(this);
Yingdi Yu40eca752013-10-10 15:00:58 -070046
Yingdi Yu3b318c12013-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 Yu4685b1b2013-10-18 17:05:02 -070053 m_profileEditor = new ProfileEditor(m_contactManager);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070054
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070055 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070056 ui->ContactList->setModel(m_contactListModel);
57
58 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yu0269c872013-10-16 13:16:49 -070059
Yingdi Yu40eca752013-10-10 15:00:58 -070060 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
61 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070062 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
63 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu3b318c12013-10-15 17:54:00 -070064 connect(ui->EditProfileButton, SIGNAL(clicked()),
65 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070066
67 connect(ui->AddContactButton, SIGNAL(clicked()),
68 this, SLOT(openAddContactPanel()));
Yingdi Yu2ac40fb2013-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 Yu595aadc2013-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 Yu2ac40fb2013-10-21 13:38:38 -070078
79
Yingdi Yu847aa862013-10-09 16:35:53 -070080}
81
82ContactPanel::~ContactPanel()
83{
84 delete ui;
Yingdi Yu40eca752013-10-10 15:00:58 -070085 delete m_contactListModel;
Yingdi Yu0269c872013-10-16 13:16:49 -070086 delete m_profileEditor;
87 delete m_addContactPanel;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070088
89 delete m_menuInvite;
Yingdi Yu847aa862013-10-09 16:35:53 -070090}
91
Yingdi Yu40eca752013-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 Yu2ac40fb2013-10-21 13:38:38 -070098 string alias = text.toUtf8().constData();
99
Yingdi Yu2ac40fb2013-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 Yu595aadc2013-10-21 15:01:40 -0700113
114 m_currentSelectedContactAlias = alias;
115 m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700116 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yu40eca752013-10-10 15:00:58 -0700117}
Yingdi Yu847aa862013-10-09 16:35:53 -0700118
Yingdi Yu3b318c12013-10-15 17:54:00 -0700119void
120ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700121{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700122
Yingdi Yu0269c872013-10-16 13:16:49 -0700123void
124ContactPanel::openAddContactPanel()
125{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700126
Yingdi Yu2ac40fb2013-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 Yu595aadc2013-10-21 15:01:40 -0700143 connect(m_menuInvite, SIGNAL(triggered()),
144 this, SLOT(openStartChatDialog()));
Yingdi Yu2ac40fb2013-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 Yu595aadc2013-10-21 15:01:40 -0700155 m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700156 m_setAliasDialog->show();
157}
158
Yingdi Yu595aadc2013-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 Yu847aa862013-10-09 16:35:53 -0700182#if WAF
183#include "contactpanel.moc"
184#include "contactpanel.cpp.moc"
185#endif