2
This file is part of KDE.
4
Copyright (c) 2009 Eckhart Wörner <ewoerner@kde.org>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22
#include "requestfriendshipwidget.h"
24
#include <QtGui/QGraphicsGridLayout>
25
#include <QtGui/QGraphicsLinearLayout>
27
#include <KConfigGroup>
28
#include <KIconLoader>
31
#include <Plasma/IconWidget>
32
#include <Plasma/Service>
34
#include "contactimage.h"
38
using namespace Plasma;
40
RequestFriendshipWidget::RequestFriendshipWidget(DataEngine* engine, QGraphicsWidget* parent)
45
m_updateTimer.setInterval(1000);
46
m_updateTimer.setSingleShot(true);
48
int avatarSize = KIconLoader::SizeMedium;
49
int actionSize = KIconLoader::SizeSmallMedium;
51
Label* title = new Label;
52
title->setText(i18n("<b>Add as friend</b>"));
55
m_image = new ContactImage(m_engine);
56
m_image->setMinimumHeight(avatarSize);
57
m_image->setMinimumWidth(avatarSize);
58
m_image->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
59
m_toLabel = new Label;
60
m_toEdit = new LineEdit;
62
QGraphicsGridLayout* toLayout = new QGraphicsGridLayout;
63
toLayout->setColumnFixedWidth(0, avatarSize * 1.2);
64
toLayout->addItem(m_image, 0, 0, 2, 1);
65
toLayout->addItem(m_toLabel, 0, 1);
66
toLayout->addItem(m_toEdit, 1, 1);
68
Label* bodyLabel = new Label;
69
bodyLabel->setText(i18n("Message:"));
71
Frame* bodyFrame = new Frame(this);
72
bodyFrame->setFrameShadow(Sunken);
73
bodyFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
74
m_body = new TextEdit;
75
(new QGraphicsLinearLayout(bodyFrame))->addItem(m_body);
77
Plasma::IconWidget* cancel = new Plasma::IconWidget;
78
cancel->setIcon("go-previous-view");
79
cancel->setToolTip(i18n("Back"));
80
cancel->setMinimumHeight(actionSize);
81
cancel->setMaximumHeight(actionSize);
82
cancel->setMinimumWidth(actionSize);
83
cancel->setMaximumWidth(actionSize);
85
m_submit = new Plasma::IconWidget;
86
m_submit->setIcon("dialog-ok");
87
m_submit->setToolTip(i18n("Send"));
88
m_submit->setMinimumHeight(actionSize);
89
m_submit->setMaximumHeight(actionSize);
90
m_submit->setMinimumWidth(actionSize);
91
m_submit->setMaximumWidth(actionSize);
92
m_submit->setEnabled(false);
94
QGraphicsLinearLayout* buttonLayout = new QGraphicsLinearLayout(Qt::Horizontal);
95
buttonLayout->addItem(cancel);
96
buttonLayout->addStretch();
97
buttonLayout->addItem(m_submit);
99
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical, this);
100
layout->addItem(title);
101
layout->addItem(toLayout);
102
layout->addItem(bodyLabel);
103
layout->addItem(bodyFrame);
104
layout->addItem(buttonLayout);
106
connect(m_submit, SIGNAL(clicked()), SLOT(send()));
107
connect(cancel, SIGNAL(clicked()), SIGNAL(done()));
108
connect(&m_updateTimer, SIGNAL(timeout()), SLOT(updateTo()));
109
connect(m_toEdit, SIGNAL(editingFinished()), SLOT(updateTo()));
110
connect(m_toEdit, SIGNAL(textEdited(QString)), SLOT(updateSendAction()));
111
connect(m_toEdit, SIGNAL(textEdited(QString)), SLOT(toChanged(QString)));
112
connect(m_toEdit, SIGNAL(returnPressed()), SLOT(switchToBody()));
113
connect(&m_personWatch, SIGNAL(updated()), SLOT(personUpdated()));
114
connect(m_body, SIGNAL(textChanged()), SLOT(updateSendAction()));
118
void RequestFriendshipWidget::personUpdated()
120
DataEngine::Data personData = m_personWatch.data();
121
m_toLabel->setText(personData.value("Name").toString());
122
m_image->setUrl(personData.value("AvatarUrl").toUrl());
126
void RequestFriendshipWidget::send() {
127
Service* service = m_engine->serviceForSource(personQuery(m_provider, m_id));
128
KConfigGroup cg = service->operationDescription("invite");
129
cg.writeEntry("Message", m_body->nativeWidget()->toPlainText());
130
service->startOperationCall(cg);
132
// FIXME: We do not wait for the result atm
135
m_toEdit->setText(QString());
136
m_personWatch.setId(QString());
137
m_body->setText(QString());
141
void RequestFriendshipWidget::switchToBody()
147
void RequestFriendshipWidget::toChanged(const QString& to)
152
m_updateTimer.stop();
153
m_updateTimer.start();
157
void RequestFriendshipWidget::updateSendAction()
159
m_submit->setEnabled(!m_toEdit->text().isEmpty() && !m_body->nativeWidget()->toPlainText().isEmpty());
163
void RequestFriendshipWidget::updateTo()
165
m_personWatch.setId(m_id);
169
void RequestFriendshipWidget::setId(const QString& id)
172
m_toEdit->setText(m_id);
173
m_personWatch.setId(m_id);
177
void RequestFriendshipWidget::setProvider(const QString& provider)
180
m_provider = provider;
181
m_toEdit->setText(m_id);
182
m_personWatch.setId(m_id);
183
m_personWatch.setProvider(m_provider);
187
#include "requestfriendshipwidget.moc"