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 "sendmessagewidget.h"
24
#include <QtGui/QGraphicsGridLayout>
25
#include <QtGui/QGraphicsLinearLayout>
27
#include <KConfigGroup>
28
#include <KIconLoader>
31
#include <Plasma/IconWidget>
32
#include <Plasma/Service>
33
#include <Plasma/ServiceJob>
35
#include "contactimage.h"
39
using namespace Plasma;
41
SendMessageWidget::SendMessageWidget(DataEngine* engine, QGraphicsWidget* parent)
46
m_updateTimer.setInterval(1000);
47
m_updateTimer.setSingleShot(true);
49
int avatarSize = KIconLoader::SizeMedium;
50
int actionSize = KIconLoader::SizeSmallMedium;
52
Label* title = new Label;
53
title->setText(i18n("<b>Send message</b>"));
56
m_image = new ContactImage(m_engine);
57
m_image->setMinimumHeight(avatarSize);
58
m_image->setMinimumWidth(avatarSize);
59
m_image->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60
m_toLabel = new Label;
61
m_toEdit = new LineEdit;
63
QGraphicsGridLayout* toLayout = new QGraphicsGridLayout;
64
toLayout->setColumnFixedWidth(0, avatarSize * 1.2);
65
toLayout->addItem(m_image, 0, 0, 2, 1);
66
toLayout->addItem(m_toLabel, 0, 1);
67
toLayout->addItem(m_toEdit, 1, 1);
69
Label* subjectLabel = new Label;
70
subjectLabel->setText(i18n("Subject:"));
72
m_subject = new LineEdit;
74
Label* bodyLabel = new Label;
75
bodyLabel->setText(i18n("Message:"));
77
Frame* bodyFrame = new Frame(this);
78
bodyFrame->setFrameShadow(Sunken);
79
bodyFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
80
m_body = new TextEdit;
81
(new QGraphicsLinearLayout(bodyFrame))->addItem(m_body);
83
Plasma::IconWidget* cancel = new Plasma::IconWidget;
84
cancel->setIcon("go-previous-view");
85
cancel->setToolTip(i18n("Back"));
86
cancel->setMinimumHeight(actionSize);
87
cancel->setMaximumHeight(actionSize);
88
cancel->setMinimumWidth(actionSize);
89
cancel->setMaximumWidth(actionSize);
91
m_submit = new Plasma::IconWidget;
92
m_submit->setIcon("mail-send");
93
m_submit->setToolTip(i18n("Send"));
94
m_submit->setMinimumHeight(actionSize);
95
m_submit->setMaximumHeight(actionSize);
96
m_submit->setMinimumWidth(actionSize);
97
m_submit->setMaximumWidth(actionSize);
98
m_submit->setEnabled(false);
100
QGraphicsLinearLayout* buttonLayout = new QGraphicsLinearLayout(Qt::Horizontal);
101
buttonLayout->addItem(cancel);
102
buttonLayout->addStretch();
103
buttonLayout->addItem(m_submit);
105
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical, this);
106
layout->addItem(title);
107
layout->addItem(toLayout);
108
layout->addItem(subjectLabel);
109
layout->addItem(m_subject);
110
layout->addItem(bodyLabel);
111
layout->addItem(bodyFrame);
112
layout->addItem(buttonLayout);
114
connect(m_submit, SIGNAL(clicked()), SLOT(send()));
115
connect(cancel, SIGNAL(clicked()), SIGNAL(done()));
116
connect(&m_updateTimer, SIGNAL(timeout()), SLOT(updateTo()));
117
connect(m_toEdit, SIGNAL(editingFinished()), SLOT(updateTo()));
118
connect(m_toEdit, SIGNAL(textEdited(QString)), SLOT(updateSendAction()));
119
connect(m_toEdit, SIGNAL(textEdited(QString)), SLOT(toChanged(QString)));
120
connect(m_toEdit, SIGNAL(returnPressed()), SLOT(switchToSubject()));
121
connect(&m_personWatch, SIGNAL(updated()), SLOT(personUpdated()));
122
connect(m_subject, SIGNAL(textEdited(QString)), SLOT(updateSendAction()));
123
connect(m_subject, SIGNAL(returnPressed()), SLOT(switchToBody()));
124
connect(m_body, SIGNAL(textChanged()), SLOT(updateSendAction()));
128
void SendMessageWidget::personUpdated()
130
DataEngine::Data personData = m_personWatch.data();
131
m_toLabel->setText(personData.value("Name").toString());
132
m_image->setUrl(personData.value("AvatarUrl").toUrl());
136
void SendMessageWidget::send() {
138
Service* service = m_engine->serviceForSource(personQuery(m_provider, m_id));
139
KConfigGroup cg = service->operationDescription("sendMessage");
140
cg.writeEntry("Subject", m_subject->text());
141
cg.writeEntry("Body", m_body->nativeWidget()->toPlainText());
142
ServiceJob* job = service->startOperationCall(cg);
143
connect(job, SIGNAL(finished(KJob*)), SIGNAL(endWork()));
145
// FIXME: We do not wait for the result atm
148
m_toEdit->setText(QString());
149
m_personWatch.setId(QString());
150
m_subject->setText(QString());
151
m_body->setText(QString());
155
void SendMessageWidget::switchToBody()
161
void SendMessageWidget::switchToSubject()
163
m_subject->setFocus();
167
void SendMessageWidget::toChanged(const QString& to)
172
m_updateTimer.stop();
173
m_updateTimer.start();
177
void SendMessageWidget::updateSendAction()
179
m_submit->setEnabled(!m_toEdit->text().isEmpty() && !m_subject->text().isEmpty() && !m_body->nativeWidget()->toPlainText().isEmpty());
183
void SendMessageWidget::updateTo()
185
m_personWatch.setId(m_id);
189
void SendMessageWidget::setId(const QString& id)
192
m_toEdit->setText(m_id);
193
m_personWatch.setId(m_id);
197
void SendMessageWidget::setProvider(const QString& provider)
200
m_provider = provider;
201
m_toEdit->setText(m_id);
202
m_personWatch.setId(m_id);
203
m_personWatch.setProvider(m_provider);
207
#include "sendmessagewidget.moc"