1 /*
2  * %kadu copyright begin%
3  * Copyright 2012 Wojciech Treter (juzefwt@gmail.com)
4  * Copyright 2012, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
5  * Copyright 2011, 2012, 2013, 2014, 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
6  * %kadu copyright end%
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "infos_dialog.h"
23 
24 #include "infos.h"
25 
26 #include "chat/chat-manager.h"
27 #include "chat/chat-storage.h"
28 #include "chat/type/chat-type-contact.h"
29 #include "configuration/config-file-variant-wrapper.h"
30 #include "contacts/contact-manager.h"
31 #include "gui/actions/base-action-context.h"
32 #include "gui/menu/menu-inventory.h"
33 #include "model/roles.h"
34 #include "os/generic/window-geometry-manager.h"
35 #include "status/status-type-data.h"
36 #include "status/status-type-manager.h"
37 #include "debug.h"
38 
39 #include <QtCore/QStringList>
40 #include <QtNetwork/QHostAddress>
41 #include <QtWidgets/QApplication>
42 #include <QtWidgets/QDialogButtonBox>
43 #include <QtWidgets/QMenu>
44 #include <QtWidgets/QPushButton>
45 #include <QtWidgets/QTreeWidget>
46 #include <QtWidgets/QVBoxLayout>
47 
InfosDialog(const LastSeen & lastSeen,QWidget * parent)48 InfosDialog::InfosDialog(const LastSeen &lastSeen, QWidget *parent) :
49 	QDialog(parent), m_lastSeen{lastSeen}
50 {
51 	setAttribute(Qt::WA_DeleteOnClose);
52 	setWindowTitle(tr("Buddies Information"));
53 }
54 
~InfosDialog()55 InfosDialog::~InfosDialog()
56 {
57 }
58 
setChatManager(ChatManager * chatManager)59 void InfosDialog::setChatManager(ChatManager *chatManager)
60 {
61 	m_chatManager = chatManager;
62 }
63 
setChatStorage(ChatStorage * chatStorage)64 void InfosDialog::setChatStorage(ChatStorage *chatStorage)
65 {
66 	m_chatStorage = chatStorage;
67 }
68 
setConfiguration(Configuration * configuration)69 void InfosDialog::setConfiguration(Configuration *configuration)
70 {
71 	m_configuration = configuration;
72 }
73 
setContactManager(ContactManager * contactManager)74 void InfosDialog::setContactManager(ContactManager *contactManager)
75 {
76 	m_contactManager = contactManager;
77 }
78 
setMenuInventory(MenuInventory * menuInventory)79 void InfosDialog::setMenuInventory(MenuInventory *menuInventory)
80 {
81 	m_menuInventory = menuInventory;
82 }
83 
setStatusTypeManager(StatusTypeManager * statusTypeManager)84 void InfosDialog::setStatusTypeManager(StatusTypeManager *statusTypeManager)
85 {
86 	m_statusTypeManager = statusTypeManager;
87 }
88 
init()89 void InfosDialog::init()
90 {
91 	QVBoxLayout *layout = new QVBoxLayout(this);
92 
93 	ListView = new QTreeWidget(this);
94 	ListView->setAllColumnsShowFocus(true);
95 	ListView->setColumnCount(7);
96 	ListView->setContextMenuPolicy(Qt::CustomContextMenu);
97 	ListView->setRootIsDecorated(false);
98 	ListView->setSelectionMode(QAbstractItemView::SingleSelection);
99 	ListView->setSortingEnabled(true);
100 
101 	connect(ListView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
102 
103 	QStringList labels;
104 	labels << tr("Buddy")
105 			<< tr("Protocol")
106 			<< tr("Username")
107 			<< tr("Nick")
108 			<< tr("Description")
109 			<< tr("State")
110 			<< tr("Last time seen on");
111 	ListView->setHeaderLabels(labels);
112 
113 	foreach (const Contact &contact, m_contactManager->items())
114 	{
115 		if (contact.isAnonymous())
116 			continue;
117 
118 		QString desc;
119 		if (!contact.currentStatus().description().isEmpty())
120 			desc = contact.currentStatus().description();
121 		desc.replace('\n', ' ');
122 
123 		QStringList labels;
124 		labels << contact.display(true)
125 				<< contact.contactAccount().protocolName()
126 				<< contact.id()
127 				<< contact.ownerBuddy().nickName()
128 				<< desc
129 				<< m_statusTypeManager->statusTypeData(contact.currentStatus().type()).name()
130 				<< m_lastSeen[qMakePair(contact.contactAccount().protocolName(), contact.id())];
131 
132 		QTreeWidgetItem *item = new QTreeWidgetItem(labels);
133 		item->setData(0, ContactRole, contact);
134 		ListView->addTopLevelItem(item);
135 	}
136 	ListView->sortItems(0, Qt::AscendingOrder);
137 
138 	QDialogButtonBox *buttons = new QDialogButtonBox(this);
139 
140 	QPushButton *closeButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogCloseButton), tr("&Close"), this);
141 
142 	buttons->addButton(closeButton, QDialogButtonBox::RejectRole);
143 
144 	layout->addWidget(ListView);
145 	layout->addSpacing(16);
146 	layout->addWidget(buttons);
147 
148 	connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
149 
150 	new WindowGeometryManager(new ConfigFileVariantWrapper(m_configuration, "LastSeen", "LastSeenWidgetGeometry"), QRect(0, 0, 800, 300), this);
151 }
152 
customContextMenuRequested(const QPoint & point)153 void InfosDialog::customContextMenuRequested(const QPoint &point)
154 {
155 	Q_UNUSED(point);
156 
157 	QList<QTreeWidgetItem *> selectedItems = ListView->selectedItems();
158 	if (1 != selectedItems.count())
159 		return;
160 
161 	QTreeWidgetItem *selectedItem = selectedItems.at(0);
162 	if (!selectedItem)
163 		return;
164 
165 	Contact contact = selectedItem->data(0, ContactRole).value<Contact>();
166 	if (!contact)
167 		return;
168 
169 	BaseActionContext actionContext{this};
170 	actionContext.setBuddies(BuddySet(contact.ownerBuddy()));
171 	actionContext.setChat(ChatTypeContact::findChat(m_chatManager, m_chatStorage, contact, ActionCreateAndAdd));
172 	actionContext.setContacts(ContactSet(contact));
173 	actionContext.setRoles(RoleSet() << ContactRole);
174 
175 	QScopedPointer<QMenu> menu(new QMenu());
176 	m_menuInventory->menu("buddy-list")->attachToMenu(menu.data());
177 	m_menuInventory->menu("buddy-list")->applyTo(menu.data(), &actionContext);
178 	m_menuInventory->menu("buddy-list")->update();
179 	menu->exec(QCursor::pos());
180 }
181 
182 #include "moc_infos_dialog.cpp"
183