blob: 51281053489f970c9073a497f8cdebff0588186f [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
21#include <boost/filesystem.hpp>
22#include "logging.h"
23#include "exception.h"
24#endif
25
26namespace fs = boost::filesystem;
27using namespace ndn;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070028using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070029
30INIT_LOGGER("ContactPanel");
31
Yingdi Yu9236c432013-10-18 11:29:25 -070032ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yu3b318c12013-10-15 17:54:00 -070033 : QDialog(parent)
34 , ui(new Ui::ContactPanel)
Yingdi Yu9236c432013-10-18 11:29:25 -070035 , m_contactManager(contactManager)
Yingdi Yu3b318c12013-10-15 17:54:00 -070036 , m_contactListModel(new QStringListModel)
Yingdi Yu9236c432013-10-18 11:29:25 -070037 , m_addContactPanel(new AddContactPanel(contactManager))
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070038 , m_setAliasDialog(new SetAliasDialog(contactManager))
39 , m_menuInvite(new QAction("&Invite", this))
40 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yu847aa862013-10-09 16:35:53 -070041{
Yingdi Yu3b318c12013-10-15 17:54:00 -070042
Yingdi Yu847aa862013-10-09 16:35:53 -070043 ui->setupUi(this);
Yingdi Yu40eca752013-10-10 15:00:58 -070044
Yingdi Yu3b318c12013-10-15 17:54:00 -070045 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 Yu4685b1b2013-10-18 17:05:02 -070051 m_profileEditor = new ProfileEditor(m_contactManager);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070052
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070053 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070054 ui->ContactList->setModel(m_contactListModel);
55
56 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yu0269c872013-10-16 13:16:49 -070057
Yingdi Yu40eca752013-10-10 15:00:58 -070058 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
59 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070060 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
61 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu3b318c12013-10-15 17:54:00 -070062 connect(ui->EditProfileButton, SIGNAL(clicked()),
63 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070064
65 connect(ui->AddContactButton, SIGNAL(clicked()),
66 this, SLOT(openAddContactPanel()));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070067
68 connect(m_addContactPanel, SIGNAL(newContactAdded()),
69 this, SLOT(refreshContactList()));
70 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
71 this, SLOT(refreshContactList()));
72
73
74
Yingdi Yu847aa862013-10-09 16:35:53 -070075}
76
77ContactPanel::~ContactPanel()
78{
79 delete ui;
Yingdi Yu40eca752013-10-10 15:00:58 -070080 delete m_contactListModel;
Yingdi Yu0269c872013-10-16 13:16:49 -070081 delete m_profileEditor;
82 delete m_addContactPanel;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070083
84 delete m_menuInvite;
Yingdi Yu847aa862013-10-09 16:35:53 -070085}
86
Yingdi Yu40eca752013-10-10 15:00:58 -070087void
88ContactPanel::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 Yu2ac40fb2013-10-21 13:38:38 -070093 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 Yu40eca752013-10-10 15:00:58 -0700112}
Yingdi Yu847aa862013-10-09 16:35:53 -0700113
Yingdi Yu3b318c12013-10-15 17:54:00 -0700114void
115ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700116{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700117
Yingdi Yu0269c872013-10-16 13:16:49 -0700118void
119ContactPanel::openAddContactPanel()
120{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700121
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700122void
123ContactPanel::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
133void
134ContactPanel::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
145void
146ContactPanel::openSetAliasDialog()
147{
148 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact);
149 m_setAliasDialog->show();
150}
151
Yingdi Yu847aa862013-10-09 16:35:53 -0700152#if WAF
153#include "contactpanel.moc"
154#include "contactpanel.cpp.moc"
155#endif