2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "serviceviewer.h"
23 #include <KMessageBox>
24 #include <KStringHandler>
26 #include <Plasma/DataEngine>
27 #include <Plasma/Service>
28 #include <Plasma/ServiceJob>
30 ServiceViewer::ServiceViewer(Plasma::DataEngine
*engine
, const QString
&source
, QWidget
*parent
)
37 setAttribute(Qt::WA_DeleteOnClose
);
38 QWidget
* mainWidget
= new QWidget(this);
39 setMainWidget(mainWidget
);
41 m_operationStatus
->hide();
43 setButtons(KDialog::Close
| KDialog::User1
);
44 setButtonText(KDialog::User1
, i18n("Start Operation"));
45 connect(this, SIGNAL(user1Clicked()), this, SLOT(startOperation()));
46 enableButton(KDialog::User1
, false);
48 connect(m_operations
, SIGNAL(currentIndexChanged(QString
)),
49 this, SLOT(operationSelected(QString
)));
51 QString engineName
= i18nc("Plasma engine with unknown name", "Unknown");
52 QString serviceName
= i18nc("Plasma service with unknown name", "Unknown");
55 engineName
= KStringHandler::capwords(m_engine
->name());
56 m_service
= m_engine
->serviceForSource(m_source
);
58 serviceName
= m_service
->name();
60 connect(m_service
, SIGNAL(operationsChanged()), this, SLOT(updateOperations()));
61 connect(m_service
, SIGNAL(finished(Plasma::ServiceJob
*)), this,
62 SLOT(operationResult(Plasma::ServiceJob
*)));
63 connect(m_engine
, SIGNAL(destroyed(QObject
*)), this, SLOT(engineDestroyed()));
66 setWindowTitle(i18nc("%1 is a Plasma service name", "%1 Service Explorer", serviceName
));
68 QString title
= i18nc("Source: name of the data, Service: writes data instead of fetching", "DataEngine: <b>%1</b>; Source: <b>%2</b>; Service <b>%3</b>", engineName
, m_source
, serviceName
);
69 m_title
->setText(title
);
70 m_operations
->setFocus();
73 ServiceViewer::~ServiceViewer()
79 void ServiceViewer::updateOperations()
87 m_operations
->clear();
88 m_operationDescription
->clear();
91 QStringList operations
= m_service
->operationNames();
93 if (!operations
.isEmpty()) {
96 foreach (const QString
& operation
, operations
) {
97 m_operations
->addItem(operation
);
102 m_operations
->setEnabled(enable
);
103 m_operationsLabel
->setEnabled(enable
);
104 m_operationDescription
->setEnabled(enable
);
107 void ServiceViewer::startOperation()
113 QString operation
= m_operations
->currentText();
114 KConfigGroup desc
= m_service
->operationDescription(operation
);
115 for (int i
= 0; i
< m_operationDescription
->rowCount(); ++i
) {
116 QTableWidgetItem
*item
= m_operationDescription
->item(i
, 1);
117 QString value
= item
->text();
119 if (value
.isEmpty()) {
123 item
= m_operationDescription
->item(i
, 0);
124 QString key
= item
->text();
125 desc
.writeEntry(key
, value
);
129 m_service
->startOperationCall(desc
);
132 void ServiceViewer::operationSelected(const QString
&operation
)
138 enableButton(KDialog::User1
, true);
140 headers
<< i18n("Key") << i18n("Value");
141 m_operationDescription
->setHorizontalHeaderLabels(headers
);
143 KConfigGroup desc
= m_service
->operationDescription(operation
);
145 QStringList keys
= desc
.keyList();
146 m_operationDescription
->setRowCount(keys
.count());
147 foreach (const QString
&key
, keys
) {
148 QTableWidgetItem
*item
= new QTableWidgetItem(key
);
149 item
->setFlags(Qt::ItemIsSelectable
| Qt::ItemIsEnabled
);
150 m_operationDescription
->setItem(i
, 0, item
);
152 item
= new QTableWidgetItem(desc
.readEntry(key
, QString()));
153 m_operationDescription
->setItem(i
, 1, item
);
159 void ServiceViewer::operationResult(Plasma::ServiceJob
*job
)
168 KMessageBox::information(this,
169 i18n("%1 operation with destination %2 failed. "
170 "The error was:<p><b>%3</b>", job
->operationName(), job
->destination(),
171 QString::number(job
->error()) + ": " + job
->errorString()),
172 i18n("Operation Result"));
174 QString result
= job
->result().toString();
175 if (result
.isEmpty()) {
176 result
= i18n("No response from job!");
179 KMessageBox::information(this,
180 i18n("%1 operation with destination %2 returned successfully. "
181 "The result was:<p><b>%3</b>", job
->operationName(),
182 job
->destination(), result
),
183 i18n("Operation Result"));
186 kDebug() << "operation results are in!";
189 void ServiceViewer::engineDestroyed()
197 void ServiceViewer::updateJobCount(int numberOfJobs
)
199 m_operationCount
+= numberOfJobs
;
201 if (m_operationCount
< 1) {
202 m_operationCount
= 0;
203 m_operationStatus
->hide();
205 m_operationStatus
->setText(i18np("One active operation ...", "%1 operations active ...", m_operationCount
));
206 m_operationStatus
->show();
210 #include "serviceviewer.moc"