open-chat-with.cpp 7.01 KB
Newer Older
1 2
/*
 * %kadu copyright begin%
Rafał Malinowski's avatar
Rafał Malinowski committed
3
 * Copyright 2009, 2010, 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
Piotr Galiszewski's avatar
Piotr Galiszewski committed
4
 * Copyright 2009, 2010 Wojciech Treter (juzefwt@gmail.com)
Rafał Malinowski's avatar
Rafał Malinowski committed
5
 * Copyright 2010, 2011 Piotr Dąbrowski (ultr@ultr.pl)
6 7
 * Copyright 2009, 2010, 2011, 2012 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
 * Copyright 2010, 2011, 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * %kadu copyright end%
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
Rafał Malinowski's avatar
Rafał Malinowski committed
23

24
#include <QtDeclarative/QDeclarativeContext>
25
#include <QtDeclarative/QDeclarativeView>
26 27 28 29
#include <QtWidgets/QApplication>
#include <QtWidgets/QDesktopWidget>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QGraphicsObject>
Rafał Malinowski's avatar
Rafał Malinowski committed
30
#include <QtGui/QKeyEvent>
31 32 33 34
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStyle>
#include <QtWidgets/QVBoxLayout>
Rafał Malinowski's avatar
Rafał Malinowski committed
35

36
#include "buddies/buddy-manager.h"
37
#include "buddies/buddy-set.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
38
#include "buddies/model/buddy-list-model.h"
39
#include "chat/chat-manager.h"
40
#include "chat/type/chat-type-contact.h"
41
#include "configuration/configuration.h"
42
#include "configuration/configuration-api.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
43
#include "contacts/contact.h"
44
#include "core/core.h"
45
#include "gui/widgets/chat-widget/chat-widget-manager.h"
46
#include "gui/widgets/filtered-tree-view.h"
47
#include "gui/widgets/line-edit-with-clear-button.h"
48
#include "misc/paths-provider.h"
49
#include "model/model-chain.h"
50
#include "model/roles.h"
51
#include "talkable/model/talkable-proxy-model.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
52

53
#include "activate.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
54 55 56 57 58
#include "debug.h"

#include "open-chat-with-contact-list-runner.h"
#include "open-chat-with-runner-manager.h"

59
#include "open-chat-with.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
60

61 62 63 64 65 66 67 68 69 70 71
OpenChatWith *OpenChatWith::Instance = 0;

OpenChatWith * OpenChatWith::instance()
{
	if (!Instance)
		Instance = new OpenChatWith();

	return Instance;
}

OpenChatWith::OpenChatWith() :
72
		QWidget(0, Qt::Window), DesktopAwareObject(this)
Rafał Malinowski's avatar
Rafał Malinowski committed
73 74
{
	kdebugf();
75

76
	setWindowRole("kadu-open-chat-with");
77

Rafał Malinowski's avatar
Rafał Malinowski committed
78 79
	setWindowTitle(tr("Open chat with..."));
	setAttribute(Qt::WA_DeleteOnClose);
80

81
	QRect availableGeometry = qApp->desktop()->availableGeometry(this);
82 83
	int width = static_cast<int>(0.25f * availableGeometry.width());
	int height = static_cast<int>(0.6f * availableGeometry.height());
84
	setGeometry(availableGeometry.center().x() - width / 2, availableGeometry.center().y() - height / 2, width, height);
85

Rafał Malinowski's avatar
Rafał Malinowski committed
86
	MainLayout = new QVBoxLayout(this);
87

88 89 90 91 92 93
	QWidget *idWidget = new QWidget(this);

	QHBoxLayout *idLayout = new QHBoxLayout(idWidget);
	idLayout->setMargin(0);
	idLayout->addWidget(new QLabel(tr("User name:"), idWidget));

94
	ContactID = new LineEditWithClearButton(this);
95
	ContactID->installEventFilter(this);
Rafał Malinowski's avatar
Rafał Malinowski committed
96
	connect(ContactID, SIGNAL(textChanged(const QString &)), this, SLOT(inputChanged(const QString &)));
97
	idLayout->addWidget(ContactID);
98

99
	MainLayout->addWidget(idWidget);
100

101
	BuddiesView = new QDeclarativeView();
Rafał Malinowski's avatar
Rafał Malinowski committed
102

103 104 105 106
	Chain = new ModelChain(this);
	ListModel = new BuddyListModel(Chain);
	Chain->setBaseModel(ListModel);
	Chain->addProxyModel(new TalkableProxyModel(Chain));
107

108
	QDeclarativeContext *declarativeContext = BuddiesView->rootContext();
109
	declarativeContext->setContextProperty("buddies", Chain->lastModel());
110

111 112
	BuddiesView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	BuddiesView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
113
	BuddiesView->setSource(QUrl(PathsProvider::instance()->dataPath() + "qml/openChatWith.qml"));
114

115 116
	connect(BuddiesView->rootObject(), SIGNAL(itemActivated(int)), this, SLOT(itemActivated(int)));

117
	MainLayout->addWidget(BuddiesView);
118

119
	QDialogButtonBox *buttons = new QDialogButtonBox(Qt::Horizontal, this);
Rafał Malinowski's avatar
Rafał Malinowski committed
120

121 122 123 124
	QPushButton *okButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogOkButton), tr("&Ok"), this);
	buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
	QPushButton *cancelButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogCancelButton), tr("&Cancel"), this);
	buttons->addButton(cancelButton, QDialogButtonBox::RejectRole);
Rafał Malinowski's avatar
Rafał Malinowski committed
125

126 127
	connect(okButton, SIGNAL(clicked(bool)), this, SLOT(inputAccepted()));
	connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(close()));
128

129
	MainLayout->addSpacing(16);
Rafał Malinowski's avatar
Rafał Malinowski committed
130
	MainLayout->addWidget(buttons);
131

Rafał Malinowski's avatar
Rafał Malinowski committed
132 133
	OpenChatRunner = new OpenChatWithContactListRunner();
	OpenChatWithRunnerManager::instance()->registerRunner(OpenChatRunner);
134 135

	inputChanged(QString());
Rafał Malinowski's avatar
Rafał Malinowski committed
136 137 138 139 140
}

OpenChatWith::~OpenChatWith()
{
	OpenChatWithRunnerManager::instance()->unregisterRunner(OpenChatRunner);
141
	Instance = 0;
142

143 144 145 146 147 148 149
	delete OpenChatRunner;
	OpenChatRunner = 0;
}

bool OpenChatWith::eventFilter(QObject *obj, QEvent *e)
{
	if (obj == ContactID && e->type() == QEvent::KeyPress)
150
	{
151
		if (static_cast<QKeyEvent *>(e)->modifiers() == Qt::NoModifier)
152
		{
153 154 155 156 157
			int key = static_cast<QKeyEvent *>(e)->key();
			if (key == Qt::Key_Down ||
					key == Qt::Key_Up ||
					key == Qt::Key_PageDown ||
					key == Qt::Key_PageUp ||
158 159
					key == Qt::Key_Left ||
					key == Qt::Key_Right)
160
			{
161
				QCoreApplication::sendEvent(BuddiesView, e);
Piotr Henryk Dabrowski's avatar
Piotr Henryk Dabrowski committed
162
				return true;
163
			}
164
		}
165
	}
166 167

	return false;
Rafał Malinowski's avatar
Rafał Malinowski committed
168 169 170 171 172
}

void OpenChatWith::keyPressEvent(QKeyEvent *e)
{
	kdebugf();
173

Rafał Malinowski's avatar
Rafał Malinowski committed
174 175 176
	switch (e->key())
	{
		case Qt::Key_Enter:
177
		case Qt::Key_Return:
178
			e->accept();
179
			inputAccepted();
180
			return;
181 182
			break;
		case Qt::Key_Escape:
183
			e->accept();
184
			close();
185
			return;
186
			break;
Rafał Malinowski's avatar
Rafał Malinowski committed
187
	}
188

189
	if (FilteredTreeView::shouldEventGoToFilter(e))
190 191 192 193 194 195 196 197 198
	{
		ContactID->setText(e->text());
		ContactID->setFocus(Qt::OtherFocusReason);
		e->accept();
		return;
	}

	QWidget::keyPressEvent(e);

Rafał Malinowski's avatar
Rafał Malinowski committed
199 200 201 202 203 204
	kdebugf2();
}

void OpenChatWith::inputChanged(const QString &text)
{
	kdebugf();
205

206
	BuddyList matchingContacts = text.isEmpty()
207
			? BuddyManager::instance()->items().toList()
208
			: OpenChatWithRunnerManager::instance()->matchingContacts(text);
209

210
	ListModel->setBuddyList(matchingContacts);
211 212
	if (BuddiesView->rootObject()->property("currentIndex").toInt() < 0)
		BuddiesView->rootObject()->setProperty("currentIndex", 0);
Rafał Malinowski's avatar
Rafał Malinowski committed
213 214
}

215 216 217 218 219 220 221 222 223 224
void OpenChatWith::itemActivated(int index)
{
	QModelIndex modelIndex = Chain->lastModel()->index(index, 0);
	if (!modelIndex.isValid())
		return;

	Contact contact = modelIndex.data(ContactRole).value<Contact>();
	if (!contact)
		return;

225
	Chat chat = ChatTypeContact::findChat(contact, ActionCreateAndAdd);
226 227 228
	if (!chat)
		return;

229
	Core::instance()->chatWidgetManager()->openChat(chat, OpenChatActivation::Activate);
230
	deleteLater();
231 232
}

Rafał Malinowski's avatar
Rafał Malinowski committed
233 234
void OpenChatWith::inputAccepted()
{
235
	itemActivated(BuddiesView->rootObject()->property("currentIndex").toInt());
Rafał Malinowski's avatar
Rafał Malinowski committed
236
}
237 238 239 240 241 242 243

void OpenChatWith::show()
{
  	if (!isVisible())
		QWidget::show();
	else
		_activateWindow(this);
244
}
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
245 246

#include "moc_open-chat-with.cpp"