00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "serviceviewer.h"
00021
00022 #include <KDebug>
00023 #include <KMessageBox>
00024 #include <KStringHandler>
00025
00026 #include <Plasma/DataEngine>
00027 #include <Plasma/Service>
00028 #include <Plasma/ServiceJob>
00029
00030 ServiceViewer::ServiceViewer(Plasma::DataEngine *engine, const QString &source, QWidget *parent)
00031 : KDialog(parent),
00032 m_engine(engine),
00033 m_service(0),
00034 m_source(source),
00035 m_operationCount(0)
00036 {
00037 setAttribute(Qt::WA_DeleteOnClose);
00038 QWidget* mainWidget = new QWidget(this);
00039 setMainWidget(mainWidget);
00040 setupUi(mainWidget);
00041 m_operationStatus->hide();
00042
00043 setButtons(KDialog::Close | KDialog::User1);
00044 setButtonText(KDialog::User1, i18n("Start Operation"));
00045 connect(this, SIGNAL(user1Clicked()), this, SLOT(startOperation()));
00046 enableButton(KDialog::User1, false);
00047
00048 connect(m_operations, SIGNAL(currentIndexChanged(QString)),
00049 this, SLOT(operationSelected(QString)));
00050
00051 QString engineName = i18nc("Plasma engine with unknown name", "Unknown");
00052 QString serviceName = i18nc("Plasma service with unknown name", "Unknown");
00053
00054 if (m_engine) {
00055 engineName = KStringHandler::capwords(m_engine->name());
00056 m_service = m_engine->serviceForSource(m_source);
00057 Q_ASSERT(m_service);
00058 serviceName = m_service->name();
00059 updateOperations();
00060 connect(m_service, SIGNAL(operationsChanged()), this, SLOT(updateOperations()));
00061 connect(m_service, SIGNAL(finished(Plasma::ServiceJob*)), this,
00062 SLOT(operationResult(Plasma::ServiceJob*)));
00063 connect(m_engine, SIGNAL(destroyed(QObject*)), this, SLOT(engineDestroyed()));
00064 }
00065
00066 setWindowTitle(i18nc("%1 is a Plasma service name", "%1 Service Explorer", serviceName));
00067
00068 QString title = i18n("DataEngine: <b>%1</b>; Source: <b>%2</b>; Service <b>%3</b>", engineName, m_source, serviceName);
00069 m_title->setText(title);
00070 m_operations->setFocus();
00071 }
00072
00073 ServiceViewer::~ServiceViewer()
00074 {
00075 delete m_service;
00076 m_engine = 0;
00077 }
00078
00079 void ServiceViewer::updateOperations()
00080 {
00081 if (!m_engine) {
00082 return;
00083 }
00084
00085 bool enable = false;
00086
00087 m_operations->clear();
00088 m_operationDescription->clear();
00089
00090 if (m_service) {
00091 QStringList operations = m_service->operationNames();
00092
00093 if (!operations.isEmpty()) {
00094 enable = true;
00095
00096 foreach (const QString& operation, operations) {
00097 m_operations->addItem(operation);
00098 }
00099 }
00100 }
00101
00102 m_operations->setEnabled(enable);
00103 m_operationsLabel->setEnabled(enable);
00104 m_operationDescription->setEnabled(enable);
00105 }
00106
00107 void ServiceViewer::startOperation()
00108 {
00109 if (!m_service) {
00110 return;
00111 }
00112
00113 QString operation = m_operations->currentText();
00114 KConfigGroup desc = m_service->operationDescription(operation);
00115 for (int i = 0; i < m_operationDescription->rowCount(); ++i) {
00116 QTableWidgetItem *item = m_operationDescription->item(i, 1);
00117 QString value = item->text();
00118
00119 if (value.isEmpty()) {
00120 continue;
00121 }
00122
00123 item = m_operationDescription->item(i, 0);
00124 QString key = item->text();
00125 desc.writeEntry(key, value);
00126 }
00127
00128 updateJobCount(1);
00129 m_service->startOperationCall(desc);
00130 }
00131
00132 void ServiceViewer::operationSelected(const QString &operation)
00133 {
00134 if (!m_service) {
00135 return;
00136 }
00137
00138 enableButton(KDialog::User1, true);
00139 QStringList headers;
00140 headers << i18n("Key") << i18n("Value");
00141 m_operationDescription->setHorizontalHeaderLabels(headers);
00142
00143 KConfigGroup desc = m_service->operationDescription(operation);
00144 int i = 0;
00145 QStringList keys = desc.keyList();
00146 m_operationDescription->setRowCount(keys.count());
00147 foreach (const QString &key, keys) {
00148 QTableWidgetItem *item = new QTableWidgetItem(key);
00149 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
00150 m_operationDescription->setItem(i, 0, item);
00151
00152 item = new QTableWidgetItem(desc.readEntry(key, QString()));
00153 m_operationDescription->setItem(i, 1, item);
00154
00155 ++i;
00156 }
00157 }
00158
00159 void ServiceViewer::operationResult(Plasma::ServiceJob *job)
00160 {
00161 if (!m_service) {
00162 return;
00163 }
00164
00165 updateJobCount(-1);
00166
00167 if (job->error()) {
00168 KMessageBox::information(this,
00169 i18n("%1 operation with destination %2 failed. "
00170 "The error was:<p><b>%3</b>", job->operationName(), job->destination(),
00171 QString::number(job->error()) + ": " + job->errorString()),
00172 i18n("Operation Result"));
00173 } else {
00174 QString result = job->result().toString();
00175 if (result.isEmpty()) {
00176 result = i18n("No response from job!");
00177 }
00178
00179 KMessageBox::information(this,
00180 i18n("%1 operation with destination %2 returned successfully. "
00181 "The result was:<p><b>%3</b>", job->operationName(),
00182 job->destination(), result),
00183 i18n("Operation Result"));
00184 }
00185
00186 kDebug() << "operation results are in!";
00187 }
00188
00189 void ServiceViewer::engineDestroyed()
00190 {
00191 m_service = 0;
00192 m_engine = 0;
00193 hide();
00194 deleteLater();
00195 }
00196
00197 void ServiceViewer::updateJobCount(int numberOfJobs)
00198 {
00199 m_operationCount += numberOfJobs;
00200
00201 if (m_operationCount < 1) {
00202 m_operationCount = 0;
00203 m_operationStatus->hide();
00204 } else {
00205 m_operationStatus->setText(i18np("One active operation ...", "%1 operations active ...", m_operationCount));
00206 m_operationStatus->show();
00207 }
00208 }
00209
00210 #include "serviceviewer.moc"
00211