• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kdeui

kmessagebox.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; version 2
00007     of the License.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qcheckbox.h>
00021 #include <qguardedptr.h>
00022 #include <qhbox.h>
00023 #include <qlabel.h>
00024 #include <qlineedit.h>
00025 #include <qmessagebox.h>
00026 #include <qstringlist.h>
00027 #include <qvbox.h>
00028 #include <qvgroupbox.h>
00029 #include <qstylesheet.h>
00030 #include <qsimplerichtext.h>
00031 #include <qpushbutton.h>
00032 #include <qlayout.h>
00033 
00034 #include <kapplication.h>
00035 #include <kconfig.h>
00036 #include <kdebug.h>
00037 #include <kdialogbase.h>
00038 #include <kguiitem.h>
00039 #include <klistbox.h>
00040 #include <klocale.h>
00041 #include <kmessagebox.h>
00042 #include <knotifyclient.h>
00043 #include <kstdguiitem.h>
00044 #include <kactivelabel.h>
00045 #include <kiconloader.h>
00046 #include <kglobalsettings.h>
00047 
00048 #ifdef Q_WS_X11
00049 #include <X11/Xlib.h>
00050 #endif
00051 
00061 static bool KMessageBox_queue = false;
00062 
00063 static QPixmap themedMessageBoxIcon(QMessageBox::Icon icon)
00064 {
00065     QString icon_name;
00066 
00067     switch(icon)
00068     {
00069     case QMessageBox::NoIcon:
00070         return QPixmap();
00071         break;
00072     case QMessageBox::Information:
00073         icon_name = "messagebox_info";
00074         break;
00075     case QMessageBox::Warning:
00076         icon_name = "messagebox_warning";
00077         break;
00078     case QMessageBox::Critical:
00079         icon_name = "messagebox_critical";
00080         break;
00081     default:
00082         break;
00083     }
00084 
00085    QPixmap ret = KGlobal::iconLoader()->loadIcon(icon_name, KIcon::NoGroup, KIcon::SizeMedium, KIcon::DefaultState, 0, true);
00086 
00087    if (ret.isNull())
00088        return QMessageBox::standardIcon(icon);
00089    else
00090        return ret;
00091 }
00092 
00093 static void sendNotification( QString message,
00094                               const QStringList& strlist,
00095                               QMessageBox::Icon icon,
00096                               WId parent_id )
00097 {
00098     // create the message for KNotify
00099     QString messageType;
00100     switch ( icon )
00101     {
00102         case QMessageBox::Warning:
00103             messageType = "messageWarning";
00104             break;
00105         case QMessageBox::Critical:
00106             messageType = "messageCritical";
00107             break;
00108         case QMessageBox::Question:
00109             messageType = "messageQuestion";
00110             break;
00111         default:
00112             messageType = "messageInformation";
00113             break;
00114     }
00115 
00116     if ( !strlist.isEmpty() )
00117     {
00118         for ( QStringList::ConstIterator it = strlist.begin(); it != strlist.end(); ++it )
00119             message += "\n" + *it;
00120     }
00121 
00122     if ( !message.isEmpty() )
00123         KNotifyClient::event( (int)parent_id, messageType, message );
00124 }
00125 
00126 static QString qrichtextify( const QString& text )
00127 {
00128   if ( text.isEmpty() || text[0] == '<' )
00129     return text;
00130 
00131   QStringList lines = QStringList::split('\n', text);
00132   for(QStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
00133   {
00134     *it = QStyleSheet::convertFromPlainText( *it, QStyleSheetItem::WhiteSpaceNormal );
00135   }
00136 
00137   return lines.join(QString::null);
00138 }
00139 
00140 int KMessageBox::createKMessageBox(KDialogBase *dialog, QMessageBox::Icon icon,
00141                              const QString &text, const QStringList &strlist,
00142                              const QString &ask, bool *checkboxReturn,
00143                              int options, const QString &details)
00144 {
00145     return createKMessageBox(dialog, themedMessageBoxIcon(icon), text, strlist,
00146                       ask, checkboxReturn, options, details, icon);
00147 }
00148 
00149 int KMessageBox::createKMessageBox(KDialogBase *dialog, QPixmap icon,
00150                              const QString &text, const QStringList &strlist,
00151                              const QString &ask, bool *checkboxReturn, int options,
00152                              const QString &details, QMessageBox::Icon notifyType)
00153 {
00154     QVBox *topcontents = new QVBox (dialog);
00155     topcontents->setSpacing(KDialog::spacingHint()*2);
00156     topcontents->setMargin(KDialog::marginHint());
00157 
00158     QWidget *contents = new QWidget(topcontents);
00159     QHBoxLayout * lay = new QHBoxLayout(contents);
00160     lay->setSpacing(KDialog::spacingHint());
00161 
00162     QLabel *label1 = new QLabel( contents);
00163 
00164     if (!icon.isNull())
00165        label1->setPixmap(icon);
00166 
00167     lay->addWidget( label1, 0, Qt::AlignCenter );
00168     lay->addSpacing(KDialog::spacingHint());
00169     // Enforce <p>text</p> otherwise the word-wrap doesn't work well
00170     QString qt_text = qrichtextify( text );
00171 
00172     int pref_width = 0;
00173     int pref_height = 0;
00174     // Calculate a proper size for the text.
00175     {
00176        QSimpleRichText rt(qt_text, dialog->font());
00177        QRect d = KGlobalSettings::desktopGeometry(dialog);
00178 
00179        pref_width = d.width() / 3;
00180        rt.setWidth(pref_width);
00181        int used_width = rt.widthUsed();
00182        pref_height = rt.height();
00183        if (3*pref_height > 2*d.height())
00184        {
00185           // Very high dialog.. make it wider
00186           pref_width = d.width() / 2;
00187           rt.setWidth(pref_width);
00188           used_width = rt.widthUsed();
00189           pref_height = rt.height();
00190        }
00191        if (used_width <= pref_width)
00192        {
00193           while(true)
00194           {
00195              int new_width = (used_width * 9) / 10;
00196              rt.setWidth(new_width);
00197              int new_height = rt.height();
00198              if (new_height > pref_height)
00199                 break;
00200              used_width = rt.widthUsed();
00201              if (used_width > new_width)
00202                 break;
00203           }
00204           pref_width = used_width;
00205        }
00206        else
00207        {
00208           if (used_width > (pref_width *2))
00209              pref_width = pref_width *2;
00210           else
00211              pref_width = used_width;
00212        }
00213     }
00214     KActiveLabel *label2 = new KActiveLabel( qt_text, contents );
00215     if (!(options & KMessageBox::AllowLink))
00216     {
00217        QObject::disconnect(label2, SIGNAL(linkClicked(const QString &)),
00218                   label2, SLOT(openLink(const QString &)));
00219     }
00220 
00221     // We add 10 pixels extra to compensate for some KActiveLabel margins.
00222     // TODO: find out why this is 10.
00223     label2->setFixedSize(QSize(pref_width+10, pref_height));
00224     lay->addWidget( label2 );
00225     lay->addStretch();
00226 
00227     KListBox *listbox = 0;
00228     if (!strlist.isEmpty())
00229     {
00230        listbox=new KListBox( topcontents );
00231        listbox->insertStringList( strlist );
00232        listbox->setSelectionMode( QListBox::NoSelection );
00233        topcontents->setStretchFactor(listbox, 1);
00234     }
00235 
00236     QGuardedPtr<QCheckBox> checkbox = 0;
00237     if (!ask.isEmpty())
00238     {
00239        checkbox = new QCheckBox(ask, topcontents);
00240        if (checkboxReturn)
00241          checkbox->setChecked(*checkboxReturn);
00242     }
00243 
00244     if (!details.isEmpty())
00245     {
00246        QVGroupBox *detailsGroup = new QVGroupBox( i18n("Details"), dialog);
00247        if ( details.length() < 512 ) {
00248          KActiveLabel *label3 = new KActiveLabel(qrichtextify(details),
00249                                                  detailsGroup);
00250          label3->setMinimumSize(label3->sizeHint());
00251          if (!(options & KMessageBox::AllowLink))
00252          {
00253            QObject::disconnect(label3, SIGNAL(linkClicked(const QString &)),
00254                                label3, SLOT(openLink(const QString &)));
00255          }
00256        } else {
00257          QTextEdit* te = new QTextEdit(details, QString::null, detailsGroup);
00258          te->setReadOnly( true );
00259          te->setMinimumHeight( te->fontMetrics().lineSpacing() * 11 );
00260        }
00261        dialog->setDetailsWidget(detailsGroup);
00262     }
00263 
00264     dialog->setMainWidget(topcontents);
00265     dialog->enableButtonSeparator(false);
00266     if (!listbox)
00267        dialog->disableResize();
00268 
00269     const KDialogBase::ButtonCode buttons[] = {
00270         KDialogBase::Help,
00271         KDialogBase::Default,
00272         KDialogBase::Ok,
00273         KDialogBase::Apply,
00274         KDialogBase::Try,
00275         KDialogBase::Cancel,
00276         KDialogBase::Close,
00277         KDialogBase::User1,
00278         KDialogBase::User2,
00279         KDialogBase::User3,
00280         KDialogBase::No,
00281         KDialogBase::Yes,
00282         KDialogBase::Details };
00283     for( unsigned int i = 0;
00284      i < sizeof( buttons )/sizeof( buttons[ 0 ] );
00285      ++i )
00286     if( QPushButton* btn = dialog->actionButton( buttons[ i ] ))
00287         if( btn->isDefault())
00288         btn->setFocus();
00289 
00290     if ( (options & KMessageBox::Notify) )
00291         sendNotification( text, strlist, notifyType, dialog->topLevelWidget()->winId());
00292 
00293     if (KMessageBox_queue)
00294     {
00295        KDialogQueue::queueDialog(dialog);
00296        return KMessageBox::Cancel; // We have to return something.
00297     }
00298 
00299     if ( (options & KMessageBox::NoExec) )
00300     {
00301        return KMessageBox::Cancel; // We have to return something.
00302     }
00303 
00304     // We use a QGuardedPtr because the dialog may get deleted
00305     // during exec() if the parent of the dialog gets deleted.
00306     // In that case the guarded ptr will reset to 0.
00307     QGuardedPtr<KDialogBase> guardedDialog = dialog;
00308 
00309     int result = guardedDialog->exec();
00310     if (checkbox && checkboxReturn)
00311        *checkboxReturn = checkbox->isChecked();
00312     delete (KDialogBase *) guardedDialog;
00313     return result;
00314 }
00315 
00316 int
00317 KMessageBox::questionYesNo(QWidget *parent, const QString &text,
00318                            const QString &caption,
00319                            const KGuiItem &buttonYes,
00320                            const KGuiItem &buttonNo,
00321                            const QString &dontAskAgainName,
00322                            int options)
00323 {
00324    return questionYesNoList(parent, text, QStringList(), caption,
00325                             buttonYes, buttonNo, dontAskAgainName, options);
00326 }
00327 
00328 int
00329 KMessageBox::questionYesNoWId(WId parent_id, const QString &text,
00330                            const QString &caption,
00331                            const KGuiItem &buttonYes,
00332                            const KGuiItem &buttonNo,
00333                            const QString &dontAskAgainName,
00334                            int options)
00335 {
00336    return questionYesNoListWId(parent_id, text, QStringList(), caption,
00337                             buttonYes, buttonNo, dontAskAgainName, options);
00338 }
00339 
00340 bool
00341 KMessageBox::shouldBeShownYesNo(const QString &dontShowAgainName,
00342                                 ButtonCode &result)
00343 {
00344     if ( dontShowAgainName.isEmpty() ) return true;
00345     QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00346     KConfig *config = againConfig ? againConfig : KGlobal::config();
00347     KConfigGroupSaver saver( config, grpNotifMsgs );
00348     QString dontAsk = config->readEntry(dontShowAgainName).lower();
00349     if (dontAsk == "yes") {
00350         result = Yes;
00351         return false;
00352     }
00353     if (dontAsk == "no") {
00354         result = No;
00355         return false;
00356     }
00357     return true;
00358 }
00359 
00360 bool
00361 KMessageBox::shouldBeShownContinue(const QString &dontShowAgainName)
00362 {
00363     if ( dontShowAgainName.isEmpty() ) return true;
00364     QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00365     KConfig *config = againConfig ? againConfig : KGlobal::config();
00366     KConfigGroupSaver saver( config, grpNotifMsgs );
00367     return config->readBoolEntry(dontShowAgainName,  true);
00368 }
00369 
00370 void
00371 KMessageBox::saveDontShowAgainYesNo(const QString &dontShowAgainName,
00372                                     ButtonCode result)
00373 {
00374     if ( dontShowAgainName.isEmpty() ) return;
00375     QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00376     KConfig *config = againConfig ? againConfig : KGlobal::config();
00377     KConfigGroupSaver saver( config, grpNotifMsgs );
00378     config->writeEntry( dontShowAgainName, result==Yes ? "yes" : "no", true, (dontShowAgainName[0] == ':'));
00379     config->sync();
00380 }
00381 
00382 void
00383 KMessageBox::saveDontShowAgainContinue(const QString &dontShowAgainName)
00384 {
00385     if ( dontShowAgainName.isEmpty() ) return;
00386     QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00387     KConfig *config = againConfig ? againConfig : KGlobal::config();
00388     KConfigGroupSaver saver( config, grpNotifMsgs );
00389     config->writeEntry( dontShowAgainName, false, true, (dontShowAgainName[0] == ':'));
00390     config->sync();
00391 }
00392 
00393 KConfig* KMessageBox::againConfig = NULL;
00394 void
00395 KMessageBox::setDontShowAskAgainConfig(KConfig* cfg)
00396 {
00397   againConfig = cfg;
00398 }
00399 
00400 int
00401 KMessageBox::questionYesNoList(QWidget *parent, const QString &text,
00402                            const QStringList &strlist,
00403                            const QString &caption,
00404                            const KGuiItem &buttonYes,
00405                            const KGuiItem &buttonNo,
00406                            const QString &dontAskAgainName,
00407                            int options)
00408 { // in order to avoid code duplication, convert to WId, it will be converted back
00409     return questionYesNoListWId( parent ? parent->winId() : 0, text, strlist,
00410         caption, buttonYes, buttonNo, dontAskAgainName, options );
00411 }
00412 
00413 int
00414 KMessageBox::questionYesNoListWId(WId parent_id, const QString &text,
00415                            const QStringList &strlist,
00416                            const QString &caption,
00417                            const KGuiItem &buttonYes,
00418                            const KGuiItem &buttonNo,
00419                            const QString &dontAskAgainName,
00420                            int options)
00421 {
00422     ButtonCode res;
00423     if ( !shouldBeShownYesNo(dontAskAgainName, res) )
00424         return res;
00425 
00426     QWidget* parent = QWidget::find( parent_id );
00427     KDialogBase *dialog= new KDialogBase(
00428                        caption.isEmpty() ? i18n("Question") : caption,
00429                        KDialogBase::Yes | KDialogBase::No,
00430                        KDialogBase::Yes, KDialogBase::No,
00431                        parent, "questionYesNo", true, true,
00432                        buttonYes, buttonNo);
00433     if( options & PlainCaption )
00434         dialog->setPlainCaption( caption );
00435 #ifdef Q_WS_X11
00436     if( parent == NULL && parent_id )
00437         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00438 #endif
00439 
00440     bool checkboxResult = false;
00441     int result = createKMessageBox(dialog, QMessageBox::Information, text, strlist,
00442                        dontAskAgainName.isEmpty() ? QString::null : i18n("Do not ask again"),
00443                        &checkboxResult, options);
00444     res = (result==KDialogBase::Yes ? Yes : No);
00445 
00446     if (checkboxResult)
00447         saveDontShowAgainYesNo(dontAskAgainName, res);
00448     return res;
00449 }
00450 
00451 int
00452 KMessageBox::questionYesNoCancel(QWidget *parent,
00453                           const QString &text,
00454                           const QString &caption,
00455                           const KGuiItem &buttonYes,
00456                           const KGuiItem &buttonNo,
00457                           const QString &dontAskAgainName,
00458                           int options)
00459 {
00460     return questionYesNoCancelWId( parent ? parent->winId() : 0, text, caption, buttonYes, buttonNo,
00461         dontAskAgainName, options );
00462 }
00463 
00464 int
00465 KMessageBox::questionYesNoCancelWId(WId parent_id,
00466                           const QString &text,
00467                           const QString &caption,
00468                           const KGuiItem &buttonYes,
00469                           const KGuiItem &buttonNo,
00470                           const QString &dontAskAgainName,
00471                           int options)
00472 {
00473     ButtonCode res;
00474     if ( !shouldBeShownYesNo(dontAskAgainName, res) )
00475         return res;
00476 
00477     QWidget* parent = QWidget::find( parent_id );
00478     KDialogBase *dialog= new KDialogBase(
00479                        caption.isEmpty() ? i18n("Question") : caption,
00480                        KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
00481                        KDialogBase::Yes, KDialogBase::Cancel,
00482                        parent, "questionYesNoCancel", true, true,
00483                        buttonYes, buttonNo);
00484     if( options & PlainCaption )
00485         dialog->setPlainCaption( caption );
00486 #ifdef Q_WS_X11
00487     if( parent == NULL && parent_id )
00488         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00489 #endif
00490 
00491     bool checkboxResult = false;
00492     int result = createKMessageBox(dialog, QMessageBox::Information,
00493                        text, QStringList(),
00494                        dontAskAgainName.isEmpty() ? QString::null : i18n("Do not ask again"),
00495                        &checkboxResult, options);
00496     if ( result==KDialogBase::Cancel ) return Cancel;
00497     res = (result==KDialogBase::Yes ? Yes : No);
00498 
00499     if (checkboxResult)
00500         saveDontShowAgainYesNo(dontAskAgainName, res);
00501     return res;
00502 }
00503 
00504 int
00505 KMessageBox::warningYesNo(QWidget *parent, const QString &text,
00506                           const QString &caption,
00507                           const KGuiItem &buttonYes,
00508                           const KGuiItem &buttonNo,
00509                           const QString &dontAskAgainName,
00510                           int options)
00511 {
00512    return warningYesNoList(parent, text, QStringList(), caption,
00513                        buttonYes, buttonNo, dontAskAgainName, options);
00514 }
00515 
00516 int
00517 KMessageBox::warningYesNoWId(WId parent_id, const QString &text,
00518                           const QString &caption,
00519                           const KGuiItem &buttonYes,
00520                           const KGuiItem &buttonNo,
00521                           const QString &dontAskAgainName,
00522                           int options)
00523 {
00524    return warningYesNoListWId(parent_id, text, QStringList(), caption,
00525                        buttonYes, buttonNo, dontAskAgainName, options);
00526 }
00527 
00528 int
00529 KMessageBox::warningYesNoList(QWidget *parent, const QString &text,
00530                               const QStringList &strlist,
00531                               const QString &caption,
00532                               const KGuiItem &buttonYes,
00533                               const KGuiItem &buttonNo,
00534                               const QString &dontAskAgainName,
00535                               int options)
00536 {
00537     return warningYesNoListWId( parent ? parent->winId() : 0, text, strlist, caption,
00538         buttonYes, buttonNo, dontAskAgainName, options );
00539 }
00540 
00541 int
00542 KMessageBox::warningYesNoListWId(WId parent_id, const QString &text,
00543                               const QStringList &strlist,
00544                               const QString &caption,
00545                               const KGuiItem &buttonYes,
00546                               const KGuiItem &buttonNo,
00547                               const QString &dontAskAgainName,
00548                               int options)
00549 {
00550     // warningYesNo and warningYesNoList are always "dangerous"
00551     // ### Remove this line for KDE 4, when the 'options' default parameter
00552     // takes effects.
00553     options |= Dangerous;
00554 
00555     ButtonCode res;
00556     if ( !shouldBeShownYesNo(dontAskAgainName, res) )
00557         return res;
00558 
00559     QWidget* parent = QWidget::find( parent_id );
00560     KDialogBase *dialog= new KDialogBase(
00561                        caption.isEmpty() ? i18n("Warning") : caption,
00562                        KDialogBase::Yes | KDialogBase::No,
00563                        KDialogBase::No, KDialogBase::No,
00564                        parent, "warningYesNoList", true, true,
00565                        buttonYes, buttonNo);
00566     if( options & PlainCaption )
00567         dialog->setPlainCaption( caption );
00568 #ifdef Q_WS_X11
00569     if( parent == NULL && parent_id )
00570         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00571 #endif
00572 
00573     bool checkboxResult = false;
00574     int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist,
00575                        dontAskAgainName.isEmpty() ? QString::null : i18n("Do not ask again"),
00576                        &checkboxResult, options);
00577     res = (result==KDialogBase::Yes ? Yes : No);
00578 
00579     if (checkboxResult)
00580         saveDontShowAgainYesNo(dontAskAgainName, res);
00581     return res;
00582 }
00583 
00584 int
00585 KMessageBox::warningContinueCancel(QWidget *parent,
00586                                    const QString &text,
00587                                    const QString &caption,
00588                                    const KGuiItem &buttonContinue,
00589                                    const QString &dontAskAgainName,
00590                                    int options)
00591 {
00592    return warningContinueCancelList(parent, text, QStringList(), caption,
00593                                 buttonContinue, dontAskAgainName, options);
00594 }
00595 
00596 int
00597 KMessageBox::warningContinueCancelWId(WId parent_id,
00598                                    const QString &text,
00599                                    const QString &caption,
00600                                    const KGuiItem &buttonContinue,
00601                                    const QString &dontAskAgainName,
00602                                    int options)
00603 {
00604    return warningContinueCancelListWId(parent_id, text, QStringList(), caption,
00605                                 buttonContinue, dontAskAgainName, options);
00606 }
00607 
00608 int
00609 KMessageBox::warningContinueCancelList(QWidget *parent, const QString &text,
00610                              const QStringList &strlist,
00611                              const QString &caption,
00612                              const KGuiItem &buttonContinue,
00613                              const QString &dontAskAgainName,
00614                              int options)
00615 {
00616     return warningContinueCancelListWId( parent ? parent->winId() : 0, text, strlist,
00617         caption, buttonContinue, dontAskAgainName, options );
00618 }
00619 
00620 int
00621 KMessageBox::warningContinueCancelListWId(WId parent_id, const QString &text,
00622                              const QStringList &strlist,
00623                              const QString &caption,
00624                              const KGuiItem &buttonContinue,
00625                              const QString &dontAskAgainName,
00626                              int options)
00627 {
00628     if ( !shouldBeShownContinue(dontAskAgainName) )
00629         return Continue;
00630 
00631     QWidget* parent = QWidget::find( parent_id );
00632     KDialogBase *dialog= new KDialogBase(
00633                        caption.isEmpty() ? i18n("Warning") : caption,
00634                        KDialogBase::Yes | KDialogBase::No,
00635                        KDialogBase::Yes, KDialogBase::No,
00636                        parent, "warningYesNo", true, true,
00637                        buttonContinue, KStdGuiItem::cancel() );
00638     if( options & PlainCaption )
00639         dialog->setPlainCaption( caption );
00640 #ifdef Q_WS_X11
00641     if( parent == NULL && parent_id )
00642         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00643 #endif
00644 
00645     bool checkboxResult = false;
00646     int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist,
00647                        dontAskAgainName.isEmpty() ? QString::null : i18n("Do not ask again"),
00648                        &checkboxResult, options);
00649 
00650     if ( result==KDialogBase::No )
00651         return Cancel;
00652     if (checkboxResult)
00653         saveDontShowAgainContinue(dontAskAgainName);
00654     return Continue;
00655 }
00656 
00657 int
00658 KMessageBox::warningYesNoCancel(QWidget *parent, const QString &text,
00659                                 const QString &caption,
00660                                 const KGuiItem &buttonYes,
00661                                 const KGuiItem &buttonNo,
00662                                 const QString &dontAskAgainName,
00663                                 int options)
00664 {
00665    return warningYesNoCancelList(parent, text, QStringList(), caption,
00666                       buttonYes, buttonNo, dontAskAgainName, options);
00667 }
00668 
00669 int
00670 KMessageBox::warningYesNoCancelWId(WId parent_id, const QString &text,
00671                                 const QString &caption,
00672                                 const KGuiItem &buttonYes,
00673                                 const KGuiItem &buttonNo,
00674                                 const QString &dontAskAgainName,
00675                                 int options)
00676 {
00677    return warningYesNoCancelListWId(parent_id, text, QStringList(), caption,
00678                       buttonYes, buttonNo, dontAskAgainName, options);
00679 }
00680 
00681 int
00682 KMessageBox::warningYesNoCancelList(QWidget *parent, const QString &text,
00683                                     const QStringList &strlist,
00684                                     const QString &caption,
00685                                     const KGuiItem &buttonYes,
00686                                     const KGuiItem &buttonNo,
00687                                     const QString &dontAskAgainName,
00688                                     int options)
00689 {
00690     return warningYesNoCancelListWId( parent ? parent->winId() : 0, text, strlist,
00691         caption, buttonYes, buttonNo, dontAskAgainName, options );
00692 }
00693 
00694 int
00695 KMessageBox::warningYesNoCancelListWId(WId parent_id, const QString &text,
00696                                     const QStringList &strlist,
00697                                     const QString &caption,
00698                                     const KGuiItem &buttonYes,
00699                                     const KGuiItem &buttonNo,
00700                                     const QString &dontAskAgainName,
00701                                     int options)
00702 {
00703     ButtonCode res;
00704     if ( !shouldBeShownYesNo(dontAskAgainName, res) )
00705         return res;
00706 
00707     QWidget* parent = QWidget::find( parent_id );
00708     KDialogBase *dialog= new KDialogBase(
00709                        caption.isEmpty() ? i18n("Warning") : caption,
00710                        KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
00711                        KDialogBase::Yes, KDialogBase::Cancel,
00712                        parent, "warningYesNoCancel", true, true,
00713                        buttonYes, buttonNo);
00714     if( options & PlainCaption )
00715         dialog->setPlainCaption( caption );
00716 #ifdef Q_WS_X11
00717     if( parent == NULL && parent_id )
00718         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00719 #endif
00720 
00721     bool checkboxResult = false;
00722     int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist,
00723                        dontAskAgainName.isEmpty() ? QString::null : i18n("Do not ask again"),
00724                        &checkboxResult, options);
00725     if ( result==KDialogBase::Cancel ) return Cancel;
00726     res = (result==KDialogBase::Yes ? Yes : No);
00727 
00728     if (checkboxResult)
00729         saveDontShowAgainYesNo(dontAskAgainName, res);
00730     return res;
00731 }
00732 
00733 void
00734 KMessageBox::error(QWidget *parent,  const QString &text,
00735                    const QString &caption, int options)
00736 {
00737     return errorListWId( parent ? parent->winId() : 0, text, QStringList(), caption, options );
00738 }
00739 
00740 void
00741 KMessageBox::errorWId(WId parent_id, const QString &text,
00742                       const QString &caption, int options)
00743 {
00744     errorListWId( parent_id, text, QStringList(), caption, options );
00745 }
00746 
00747 void
00748 KMessageBox::errorList(QWidget *parent, const QString &text, const QStringList &strlist,
00749                        const QString &caption, int options)
00750 {
00751     return errorListWId( parent ? parent->winId() : 0, text, strlist, caption, options );
00752 }
00753 
00754 void
00755 KMessageBox::errorListWId(WId parent_id,  const QString &text, const QStringList &strlist,
00756                    const QString &caption, int options)
00757 {
00758     QWidget* parent = QWidget::find( parent_id );
00759     KDialogBase *dialog= new KDialogBase(
00760                        caption.isEmpty() ? i18n("Error") : caption,
00761                        KDialogBase::Yes,
00762                        KDialogBase::Yes, KDialogBase::Yes,
00763                        parent, "error", true, true,
00764                        KStdGuiItem::ok() );
00765     if( options & PlainCaption )
00766         dialog->setPlainCaption( caption );
00767 #ifdef Q_WS_X11
00768     if( parent == NULL && parent_id )
00769         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00770 #endif
00771 
00772     createKMessageBox(dialog, QMessageBox::Critical, text, strlist, QString::null, 0, options);
00773 }
00774 
00775 void
00776 KMessageBox::detailedError(QWidget *parent,  const QString &text,
00777                    const QString &details,
00778                    const QString &caption, int options)
00779 {
00780     return detailedErrorWId( parent ? parent->winId() : 0, text, details, caption, options );
00781 }
00782 
00783 void
00784 KMessageBox::detailedErrorWId(WId parent_id,  const QString &text,
00785                    const QString &details,
00786                    const QString &caption, int options)
00787 {
00788     QWidget* parent = QWidget::find( parent_id );
00789     KDialogBase *dialog= new KDialogBase(
00790                        caption.isEmpty() ? i18n("Error") : caption,
00791                        KDialogBase::Yes | KDialogBase::Details,
00792                        KDialogBase::Yes, KDialogBase::Yes,
00793                        parent, "error", true, true,
00794                        KStdGuiItem::ok() );
00795     if( options & PlainCaption )
00796         dialog->setPlainCaption( caption );
00797 #ifdef Q_WS_X11
00798     if( parent == NULL && parent_id )
00799         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00800 #endif
00801 
00802     createKMessageBox(dialog, QMessageBox::Critical, text, QStringList(), QString::null, 0, options, details);
00803 }
00804 
00805 void
00806 KMessageBox::queuedDetailedError(QWidget *parent,  const QString &text,
00807                    const QString &details,
00808                    const QString &caption)
00809 {
00810     return queuedDetailedErrorWId( parent ? parent->winId() : 0, text, details, caption );
00811 }
00812 
00813 void
00814 KMessageBox::queuedDetailedErrorWId(WId parent_id,  const QString &text,
00815                    const QString &details,
00816                    const QString &caption)
00817 {
00818    KMessageBox_queue = true;
00819    (void) detailedErrorWId(parent_id, text, details, caption);
00820    KMessageBox_queue = false;
00821 }
00822 
00823 
00824 void
00825 KMessageBox::sorry(QWidget *parent, const QString &text,
00826                    const QString &caption, int options)
00827 {
00828     return sorryWId( parent ? parent->winId() : 0, text, caption, options );
00829 }
00830 
00831 void
00832 KMessageBox::sorryWId(WId parent_id, const QString &text,
00833                    const QString &caption, int options)
00834 {
00835     QWidget* parent = QWidget::find( parent_id );
00836     KDialogBase *dialog= new KDialogBase(
00837                        caption.isEmpty() ? i18n("Sorry") : caption,
00838                        KDialogBase::Yes,
00839                        KDialogBase::Yes, KDialogBase::Yes,
00840                        parent, "sorry", true, true,
00841                        KStdGuiItem::ok() );
00842     if( options & PlainCaption )
00843         dialog->setPlainCaption( caption );
00844 #ifdef Q_WS_X11
00845     if( parent == NULL && parent_id )
00846         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00847 #endif
00848 
00849     createKMessageBox(dialog, QMessageBox::Warning, text, QStringList(), QString::null, 0, options);
00850 }
00851 
00852 void
00853 KMessageBox::detailedSorry(QWidget *parent, const QString &text,
00854                    const QString &details,
00855                    const QString &caption, int options)
00856 {
00857     return detailedSorryWId( parent ? parent->winId() : 0, text, details, caption, options );
00858 }
00859 
00860 void
00861 KMessageBox::detailedSorryWId(WId parent_id, const QString &text,
00862                    const QString &details,
00863                    const QString &caption, int options)
00864 {
00865     QWidget* parent = QWidget::find( parent_id );
00866     KDialogBase *dialog= new KDialogBase(
00867                        caption.isEmpty() ? i18n("Sorry") : caption,
00868                        KDialogBase::Yes | KDialogBase::Details,
00869                        KDialogBase::Yes, KDialogBase::Yes,
00870                        parent, "sorry", true, true,
00871                        KStdGuiItem::ok() );
00872     if( options & PlainCaption )
00873         dialog->setPlainCaption( caption );
00874 #ifdef Q_WS_X11
00875     if( parent == NULL && parent_id )
00876         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00877 #endif
00878 
00879     createKMessageBox(dialog, QMessageBox::Warning, text, QStringList(), QString::null, 0, options, details);
00880 }
00881 
00882 void
00883 KMessageBox::information(QWidget *parent,const QString &text,
00884              const QString &caption, const QString &dontShowAgainName, int options)
00885 {
00886   informationList(parent, text, QStringList(), caption, dontShowAgainName, options);
00887 }
00888 
00889 void
00890 KMessageBox::informationWId(WId parent_id,const QString &text,
00891              const QString &caption, const QString &dontShowAgainName, int options)
00892 {
00893   informationListWId(parent_id, text, QStringList(), caption, dontShowAgainName, options);
00894 }
00895 
00896 void
00897 KMessageBox::informationList(QWidget *parent,const QString &text, const QStringList & strlist,
00898                          const QString &caption, const QString &dontShowAgainName, int options)
00899 {
00900     return informationListWId( parent ? parent->winId() : 0, text, strlist, caption,
00901         dontShowAgainName, options );
00902 }
00903 
00904 void
00905 KMessageBox::informationListWId(WId parent_id,const QString &text, const QStringList & strlist,
00906                          const QString &caption, const QString &dontShowAgainName, int options)
00907 {
00908     if ( !shouldBeShownContinue(dontShowAgainName) )
00909         return;
00910 
00911     QWidget* parent = QWidget::find( parent_id );
00912     KDialogBase *dialog= new KDialogBase(
00913                        caption.isEmpty() ? i18n("Information") : caption,
00914                        KDialogBase::Yes,
00915                        KDialogBase::Yes, KDialogBase::Yes,
00916                        parent, "information", true, true,
00917                        KStdGuiItem::ok() );
00918     if( options & PlainCaption )
00919         dialog->setPlainCaption( caption );
00920 #ifdef Q_WS_X11
00921     if( parent == NULL && parent_id )
00922         XSetTransientForHint( qt_xdisplay(), dialog->winId(), parent_id );
00923 #endif
00924 
00925     bool checkboxResult = false;
00926 
00927     createKMessageBox(dialog, QMessageBox::Information, text, strlist,
00928         dontShowAgainName.isEmpty() ? QString::null : i18n("Do not show this message again"),
00929                 &checkboxResult, options);
00930 
00931     if (checkboxResult)
00932         saveDontShowAgainContinue(dontShowAgainName);
00933 }
00934 
00935 void
00936 KMessageBox::enableAllMessages()
00937 {
00938    KConfig *config = againConfig ? againConfig : KGlobal::config();
00939    QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00940    if (!config->hasGroup(grpNotifMsgs))
00941       return;
00942 
00943    KConfigGroupSaver saver( config, grpNotifMsgs );
00944 
00945    typedef QMap<QString, QString> configMap;
00946 
00947    configMap map = config->entryMap(grpNotifMsgs);
00948 
00949    configMap::Iterator it;
00950    for (it = map.begin(); it != map.end(); ++it)
00951       config->deleteEntry( it.key() );
00952    config->sync();
00953 }
00954 
00955 void
00956 KMessageBox::enableMessage(const QString &dontShowAgainName)
00957 {
00958    KConfig *config = againConfig ? againConfig : KGlobal::config();
00959    QString grpNotifMsgs = QString::fromLatin1("Notification Messages");
00960    if (!config->hasGroup(grpNotifMsgs))
00961       return;
00962 
00963    KConfigGroupSaver saver( config, grpNotifMsgs );
00964 
00965    config->deleteEntry(dontShowAgainName);
00966    config->sync();
00967 }
00968 
00969 void
00970 KMessageBox::about(QWidget *parent, const QString &text,
00971                    const QString &caption, int options)
00972 {
00973     QString _caption = caption;
00974     if (_caption.isEmpty())
00975         _caption = i18n("About %1").arg(kapp->caption());
00976 
00977     KDialogBase *dialog = new KDialogBase(
00978                                 caption,
00979                                 KDialogBase::Yes,
00980                                 KDialogBase::Yes, KDialogBase::Yes,
00981                                 parent, "about", true, true,
00982                                 KStdGuiItem::ok() );
00983     
00984     QPixmap ret = KApplication::kApplication()->icon();
00985     if (ret.isNull())
00986         ret = QMessageBox::standardIcon(QMessageBox::Information);
00987     dialog->setIcon(ret);
00988 
00989     createKMessageBox(dialog, ret, text, QStringList(), QString::null, 0, options);
00990     
00991     return;
00992 }
00993 
00994 int KMessageBox::messageBox( QWidget *parent, DialogType type, const QString &text,
00995                              const QString &caption, const KGuiItem &buttonYes,
00996                              const KGuiItem &buttonNo, const QString &dontShowAskAgainName,
00997                              int options )
00998 {
00999     return messageBoxWId( parent ? parent->winId() : 0, type, text, caption,
01000         buttonYes, buttonNo, dontShowAskAgainName, options );
01001 }
01002 
01003 int KMessageBox::messageBox( QWidget *parent, DialogType type, const QString &text,
01004                              const QString &caption, const KGuiItem &buttonYes,
01005                              const KGuiItem &buttonNo, int options )
01006 {
01007     return messageBoxWId( parent ? parent->winId() : 0, type, text, caption,
01008         buttonYes, buttonNo, QString::null, options );
01009 }
01010 
01011 int KMessageBox::messageBoxWId( WId parent_id, DialogType type, const QString &text,
01012                              const QString &caption, const KGuiItem &buttonYes,
01013                              const KGuiItem &buttonNo, const QString &dontShow,
01014                              int options )
01015 {
01016     switch (type) {
01017         case QuestionYesNo:
01018             return KMessageBox::questionYesNoWId( parent_id,
01019                                                text, caption, buttonYes, buttonNo, dontShow, options );
01020         case QuestionYesNoCancel:
01021             return KMessageBox::questionYesNoCancelWId( parent_id,
01022                                                text, caption, buttonYes, buttonNo, dontShow, options );
01023         case WarningYesNo:
01024             return KMessageBox::warningYesNoWId( parent_id,
01025                                               text, caption, buttonYes, buttonNo, dontShow, options );
01026         case WarningContinueCancel:
01027             return KMessageBox::warningContinueCancelWId( parent_id,
01028                                               text, caption, buttonYes.text(), dontShow, options );
01029         case WarningYesNoCancel:
01030             return KMessageBox::warningYesNoCancelWId( parent_id,
01031                                               text, caption, buttonYes, buttonNo, dontShow, options );
01032         case Information:
01033             KMessageBox::informationWId( parent_id,
01034                                       text, caption, dontShow, options );
01035             return KMessageBox::Ok;
01036 
01037         case Error:
01038             KMessageBox::errorWId( parent_id, text, caption, options );
01039             return KMessageBox::Ok;
01040 
01041         case Sorry:
01042             KMessageBox::sorryWId( parent_id, text, caption, options );
01043             return KMessageBox::Ok;
01044     }
01045     return KMessageBox::Cancel;
01046 }
01047 
01048 void KMessageBox::queuedMessageBox( QWidget *parent, DialogType type, const QString &text, const QString &caption, int options )
01049 {
01050     return queuedMessageBoxWId( parent ? parent->winId() : 0, type, text, caption, options );
01051 }
01052 
01053 void KMessageBox::queuedMessageBoxWId( WId parent_id, DialogType type, const QString &text, const QString &caption, int options )
01054 {
01055    KMessageBox_queue = true;
01056    (void) messageBoxWId(parent_id, type, text, caption, KStdGuiItem::yes(),
01057                      KStdGuiItem::no(), QString::null, options);
01058    KMessageBox_queue = false;
01059 }
01060 
01061 void KMessageBox::queuedMessageBox( QWidget *parent, DialogType type, const QString &text, const QString &caption )
01062 {
01063     return queuedMessageBoxWId( parent ? parent->winId() : 0, type, text, caption );
01064 }
01065 
01066 void KMessageBox::queuedMessageBoxWId( WId parent_id, DialogType type, const QString &text, const QString &caption )
01067 {
01068    KMessageBox_queue = true;
01069    (void) messageBoxWId(parent_id, type, text, caption);
01070    KMessageBox_queue = false;
01071 }

kdeui

Skip menu "kdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal