00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kdxsbutton.h"
00022
00023 #include "knewstuff2/dxs/dxs.h"
00024
00025 #include "knewstuff2/core/entry.h"
00026 #include "knewstuff2/core/category.h"
00027
00028 #include "downloaddialog.h"
00029 #include "kdxsrating.h"
00030 #include "kdxscomment.h"
00031 #include "kdxscomments.h"
00032 #include "kdxschanges.h"
00033 #include "kdxstranslation.h"
00034
00035 #include <QtGui/QLayout>
00036 #include <QtXml/qdom.h>
00037 #include <QtGui/QToolButton>
00038
00039 #include <ktoolinvocation.h>
00040 #include <kmessagebox.h>
00041 #include <kdebug.h>
00042 #include <klocale.h>
00043 #include <kcursor.h>
00044 #include <krun.h>
00045
00046 #include <kmenu.h>
00047 #include <kiconloader.h>
00048 #include <kapplication.h>
00049 #include <kprocess.h>
00050 #include <kpassworddialog.h>
00051
00052 using namespace KNS;
00053
00054 KDXSButton::KDXSButton(QWidget *parent)
00055 : QToolButton(parent), d(0)
00056 {
00057 m_entry = 0;
00058 m_provider = 0;
00059 m_dxs = 0;
00060 m_engine = 0;
00061
00062
00063
00064
00065 m_p = new KMenu(this);
00066 action_install = m_p->addAction(SmallIcon("get-hot-new-stuff"),
00067 i18n("Install"));
00068 action_uninstall = m_p->addAction(i18n("Uninstall"));
00069 action_comments = m_p->addAction(SmallIcon("help-about"),
00070 i18n("Comments"));
00071 action_changes = m_p->addAction(SmallIcon("help-about"),
00072 i18n("Changelog"));
00073
00074 m_history = new KMenu(this);
00075 m_history->setTitle(i18n("Switch version"));
00076
00077
00078
00079
00080
00081 action_historysub = m_p->addMenu(m_history);
00082
00083 m_p->addSeparator();
00084 action_info = m_p->addAction(i18n("Provider information"));
00085
00086 m_contact = new KMenu(this);
00087 m_contact->setIcon(SmallIcon("mail-message-new"));
00088 m_contact->setTitle(i18n("Contact author"));
00089
00090 KMenu *pcollab = new KMenu(this);
00091 pcollab->setTitle(i18n("Collaboration"));
00092
00093 action_collabrating = pcollab->addAction(i18n("Add Rating"));
00094 action_collabcomment = pcollab->addAction(i18n("Add Comment"));
00095 action_collabtranslation = pcollab->addAction(i18n("Translate"));
00096 action_collabsubscribe = pcollab->addAction(i18n("Subscribe"));
00097 action_collabremoval = pcollab->addAction(i18n("Report bad entry"));
00098 pcollab->addMenu(m_contact);
00099
00100 m_p->addSeparator();
00101 action_collaboratesub = m_p->addMenu(pcollab);
00102
00103 connect(this, SIGNAL(clicked()), SLOT(slotClicked()));
00104
00105 connect(m_p, SIGNAL(triggered(QAction*)), SLOT(slotTriggered(QAction*)));
00106
00107 connect(m_contact, SIGNAL(triggered(QAction*)), SLOT(slotTriggered(QAction*)));
00108 connect(pcollab, SIGNAL(triggered(QAction*)), SLOT(slotTriggered(QAction*)));
00109
00110
00111
00112
00113
00114 setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00115 setPopupMode(QToolButton::MenuButtonPopup);
00116 setMenu(m_p);
00117
00118 setEnabled(false);
00119 show();
00120 }
00121
00122 KDXSButton::~KDXSButton()
00123 {
00124 }
00125
00126 void KDXSButton::setEntry(Entry *e)
00127 {
00128 m_entry = e;
00129
00130 if (m_engine) setEnabled(true);
00131
00132 Entry::Status status = e->status();
00133 switch (status) {
00134 case Entry::Installed:
00135 setText(i18n("Uninstall"));
00136 action_install->setVisible(false);
00137 action_uninstall->setVisible(true);
00138 break;
00139 case Entry::Updateable:
00140 setText(i18n("Update"));
00141 action_uninstall->setVisible(false);
00142 action_install->setVisible(true);
00143 break;
00144 case Entry::Deleted:
00146 setText(i18n("Install"));
00147 action_uninstall->setVisible(false);
00148 action_install->setVisible(true);
00149 break;
00150 default:
00151 setText(i18n("Install"));
00152 action_uninstall->setVisible(false);
00153 action_install->setVisible(true);
00154 }
00155
00156 Author author = e->author();
00157 if (!author.email().isEmpty()) {
00158 m_contact->setEnabled(true);
00159 action_contactbymail = m_contact->addAction(SmallIcon("mail-message-new"),
00160 i18n("Send Mail"));
00161 } else
00162 m_contact->setEnabled(false);
00163 if (!author.jabber().isEmpty()) {
00164 action_contactbyjabber = m_contact->addAction(i18n("Contact on Jabber"));
00165 }
00166 }
00167
00168 void KDXSButton::setProvider(const KNS::Provider *provider)
00169 {
00170 m_provider = provider;
00171
00172 if (!provider) return;
00173
00174
00175
00176 if (provider->webService().isValid()) {
00177
00178 action_collabrating->setEnabled(true);
00179 action_collabcomment->setEnabled(true);
00180 action_collabtranslation->setEnabled(true);
00181 action_collabsubscribe->setEnabled(true);
00182 action_collabremoval->setEnabled(true);
00183
00184 action_comments->setEnabled(true);
00185 action_changes->setEnabled(true);
00186
00187 m_history->setEnabled(true);
00188 } else {
00189 action_collabrating->setEnabled(false);
00190 action_collabcomment->setEnabled(false);
00191 action_collabtranslation->setEnabled(false);
00192 action_collabsubscribe->setEnabled(false);
00193 action_collabremoval->setEnabled(false);
00194
00195 action_comments->setEnabled(false);
00196 action_changes->setEnabled(false);
00197
00198 m_history->setEnabled(false);
00199 }
00200 }
00201
00202 void KDXSButton::setEngine(DxsEngine *engine)
00203 {
00204 m_engine = engine;
00205
00206 if (m_entry) setEnabled(true);
00207
00208 m_dxs = new KNS::Dxs(m_engine, NULL);
00209 m_dxs->setEndpoint(KUrl("http://new.kstuff.org/cgi-bin/hotstuff-dxs"));
00210
00211
00212
00213
00214 connect(m_dxs,
00215 SIGNAL(signalInfo(QString, QString, QString)),
00216 SLOT(slotInfo(QString, QString, QString)));
00217 connect(m_dxs,
00218 SIGNAL(signalCategories(QList<KNS::Category*>)),
00219 SLOT(slotCategories(QList<KNS::Category*>)));
00220 connect(m_dxs,
00221 SIGNAL(signalEntries(KNS::Entry::List, Feed *)),
00222 SLOT(slotEntries(KNS::Entry::List, Feed *)));
00223 connect(m_dxs,
00224 SIGNAL(signalComments(QStringList)),
00225 SLOT(slotComments(QStringList)));
00226 connect(m_dxs,
00227 SIGNAL(signalChanges(QStringList)),
00228 SLOT(slotChanges(QStringList)));
00229 connect(m_dxs,
00230 SIGNAL(signalHistory(QStringList)),
00231 SLOT(slotHistory(QStringList)));
00232 connect(m_dxs,
00233 SIGNAL(signalRemoval(bool)),
00234 SLOT(slotRemoval(bool)));
00235 connect(m_dxs,
00236 SIGNAL(signalSubscription(bool)),
00237 SLOT(slotSubscription(bool)));
00238 connect(m_dxs,
00239 SIGNAL(signalComment(bool)),
00240 SLOT(slotComment(bool)));
00241 connect(m_dxs,
00242 SIGNAL(signalRating(bool)),
00243 SLOT(slotRating(bool)));
00244 connect(m_dxs,
00245 SIGNAL(signalFault()),
00246 SLOT(slotFault()));
00247 connect(m_dxs,
00248 SIGNAL(signalError()),
00249 SLOT(slotError()));
00250 }
00251
00252 void KDXSButton::slotInfo(QString provider, QString server, QString version)
00253 {
00254 QString infostring = i18n("Server: %1", server);
00255 infostring += '\n' + i18n("Provider: %1", provider);
00256 infostring += '\n' + i18n("Version: %1", version);
00257
00258 KMessageBox::information(this,
00259 infostring,
00260 i18n("Provider information"));
00261 }
00262
00263 void KDXSButton::slotCategories(QList<KNS::Category*> categories)
00264 {
00265 for (QList<KNS::Category*>::Iterator it = categories.begin(); it != categories.end(); it++) {
00266 KNS::Category *category = (*it);
00267
00268 }
00269 }
00270
00271 void KDXSButton::slotEntries(KNS::Entry::List entries, Feed * feed)
00272 {
00273 for (KNS::Entry::List::Iterator it = entries.begin(); it != entries.end(); it++) {
00274 KNS::Entry *entry = (*it);
00275
00276 }
00277 }
00278
00279 void KDXSButton::slotComments(QStringList comments)
00280 {
00281 KDXSComments commentsdlg(this);
00282
00283 for (QStringList::Iterator it = comments.begin(); it != comments.end(); it++) {
00284
00285 commentsdlg.addComment("foo", (*it));
00286 }
00287
00288 commentsdlg.exec();
00289 }
00290
00291 void KDXSButton::slotChanges(QStringList changes)
00292 {
00293 KDXSChanges changesdlg(this);
00294
00295 for (QStringList::Iterator it = changes.begin(); it != changes.end(); it++) {
00296
00297 changesdlg.addChangelog("v???", (*it));
00298 }
00299
00300 changesdlg.exec();
00301 }
00302
00303 void KDXSButton::slotHistory(QStringList entries)
00304 {
00305 m_history->clear();
00306
00307 int i = 0;
00308 for (QStringList::Iterator it = entries.begin(); it != entries.end(); it++) {
00309
00310
00311
00312
00313
00314 i++;
00315 }
00316
00317 if (entries.size() == 0) {
00318
00319
00320
00321 }
00322
00323 m_history->setCursor(Qt::ArrowCursor);
00324 }
00325
00326 void KDXSButton::slotRemoval(bool success)
00327 {
00328 if (success) {
00329 KMessageBox::information(this,
00330 i18n("The removal request was successfully registered."),
00331 i18n("Removal of entry"));
00332 } else {
00333 KMessageBox::error(this,
00334 i18n("The removal request failed."),
00335 i18n("Removal of entry"));
00336 }
00337 }
00338
00339 void KDXSButton::slotSubscription(bool success)
00340 {
00341 if (success) {
00342 KMessageBox::information(this,
00343 i18n("The subscription was successfully completed."),
00344 i18n("Subscription to entry"));
00345 } else {
00346 KMessageBox::error(this,
00347 i18n("The subscription request failed."),
00348 i18n("Subscription to entry"));
00349 }
00350 }
00351
00352 void KDXSButton::slotRating(bool success)
00353 {
00354 if (success) {
00355 KMessageBox::information(this,
00356 i18n("The rating was submitted successfully."),
00357 i18n("Rating for entry"));
00358 } else {
00359 KMessageBox::error(this,
00360 i18n("The rating could not be submitted."),
00361 i18n("Rating for entry"));
00362 }
00363 }
00364
00365 void KDXSButton::slotComment(bool success)
00366 {
00367 if (success) {
00368 KMessageBox::information(this,
00369 i18n("The comment was submitted successfully."),
00370 i18n("Comment on entry"));
00371 } else {
00372 KMessageBox::error(this,
00373 i18n("The comment could not be submitted."),
00374 i18n("Comment on entry"));
00375 }
00376 }
00377
00378 void KDXSButton::slotFault()
00379 {
00380 KMessageBox::error(this,
00381 i18n("A protocol fault has occurred. The request has failed."),
00382 i18n("Desktop Exchange Service"));
00383 }
00384
00385 void KDXSButton::slotError()
00386 {
00387 KMessageBox::error(this,
00388 i18n("A network error has occurred. The request has failed."),
00389 i18n("Desktop Exchange Service"));
00390 }
00391
00392 void KDXSButton::slotVersionsActivated(int id)
00393 {
00394 int version = id - historyslots;
00395
00396 Q_UNUSED(version);
00397
00398 }
00399
00400 void KDXSButton::slotTriggered(QAction *action)
00401 {
00402 int ret;
00403
00404 if (action == action_info) {
00405
00406 if (m_provider->webService().isValid()) {
00407 m_dxs->call_info();
00408 } else {
00409 slotInfo(m_provider->name().representation(),
00410 QString(),
00411 QString());
00412 }
00413 }
00414 if (action == action_comments) {
00415 m_dxs->call_comments(0);
00416 }
00417 if (action == action_changes) {
00418 m_dxs->call_changes(2);
00419 }
00420 if (action == action_contactbymail) {
00421 QString address = m_entry->author().email();
00422 KToolInvocation::invokeMailer(address, i18n("KNewStuff contributions"), "");
00423 }
00424 if (action == action_contactbyjabber) {
00425 new KRun(KUrl(QLatin1String("xmpp:") + m_entry->author().jabber()), this);
00426 }
00427 if (action == action_collabtranslation) {
00428 if (!authenticate())
00429 return;
00430 KDXSTranslation translation(this);
00431 ret = translation.exec();
00432 if (ret == QDialog::Accepted) {
00433
00434
00435
00436
00437
00438 }
00439 }
00440 if (action == action_collabremoval) {
00441 if (authenticate())
00442 m_dxs->call_removal(0);
00443 }
00444 if (action == action_collabsubscribe) {
00445 if (authenticate())
00446 m_dxs->call_subscription(0, true);
00447 }
00448 if (action == action_uninstall) {
00449 if (m_engine->uninstall(m_entry)) {
00450 setText(i18n("Install"));
00451 action_uninstall->setVisible(false);
00452 action_install->setVisible(true);
00453 }
00454 }
00455 if (action == action_install) {
00456 connect(m_engine,
00457 SIGNAL(signalPayloadLoaded(KUrl)),
00458 SLOT(slotPayloadLoaded(KUrl)));
00459 connect(m_engine,
00460 SIGNAL(signalPayloadFailed(KNS::Entry *)),
00461 SLOT(slotPayloadFailed(KNS::Entry *)));
00462
00463 m_engine->downloadPayload(m_entry);
00464 }
00465 if (action == action_collabcomment) {
00466 if (!authenticate())
00467 return;
00468 KDXSComment comment(this);
00469 ret = comment.exec();
00470 if (ret == QDialog::Accepted) {
00471 QString s = comment.comment();
00472 if (!s.isEmpty()) {
00473 m_dxs->call_comment(0, s);
00474 }
00475 }
00476 }
00477 if (action == action_collabrating) {
00478 if (!authenticate())
00479 return;
00480 KDXSRating rating(this);
00481 ret = rating.exec();
00482 if (ret == QDialog::Accepted) {
00483 int r = rating.rating();
00484 if (r >= 0) {
00485 m_dxs->call_rating(0, r);
00486 }
00487 }
00488 }
00489 }
00490
00491 void KDXSButton::slotVersionsHighlighted(int id)
00492 {
00493
00494
00495 if (id == historyinactive) {
00496
00497 m_history->setCursor(QCursor(Qt::WaitCursor));
00498
00499
00500 m_dxs->call_history(0);
00501
00502 }
00503 }
00504
00505 void KDXSButton::slotClicked()
00506 {
00507 if (action_install->isVisible())
00508 slotTriggered(action_install);
00509 else
00510 slotTriggered(action_uninstall);
00511 }
00512
00513 bool KDXSButton::authenticate()
00514 {
00515 if ((!m_username.isEmpty()) && (!m_password.isEmpty())) return true;
00516
00517 KPasswordDialog dlg(this);
00518 dlg.setPrompt(i18n("This operation requires authentication."));
00519 int ret = dlg.exec();
00520 if (ret == QDialog::Accepted) {
00521 m_username = dlg.username();
00522 m_password = dlg.password();
00523
00524 return true;
00525 }
00526
00527 return false;
00528 }
00529
00530 void KDXSButton::slotPayloadLoaded(KUrl url)
00531 {
00532
00533
00534 Entry::Status status = m_entry->status();
00535 if (status == Entry::Installed) {
00536 setText(i18n("Uninstall"));
00537 action_install->setVisible(false);
00538 action_uninstall->setVisible(true);
00539 } else {
00540 setText(i18n("Install"));
00541 action_uninstall->setVisible(false);
00542 action_install->setVisible(true);
00543 }
00544
00545 m_engine->install(url.pathOrUrl());
00546 }
00547
00548 void KDXSButton::slotPayloadFailed(KNS::Entry *)
00549 {
00550
00551 }
00552
00553 #include "kdxsbutton.moc"