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