File indexing completed on 2025-02-02 05:43:20

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "dialoghandler.hpp"
0010 
0011 // Kasten core
0012 #include <Kasten/AbstractDocument>
0013 // KF
0014 #include <KMessageBox>
0015 #include <KLocalizedString>
0016 // Qt
0017 #include <QUrl>
0018 
0019 namespace Kasten {
0020 
0021 DialogHandler::DialogHandler(QWidget* widget)
0022     : mWidget(widget)
0023 {}
0024 
0025 DialogHandler::~DialogHandler() = default;
0026 
0027 void DialogHandler::setWidget(QWidget* widget) { mWidget = widget; }
0028 
0029 Answer DialogHandler::queryOverwrite(const QUrl& url, const QString& title) const
0030 {
0031     const QString message =
0032         xi18nc("@info",
0033                "There is already a file at<nl/><filename>%1</filename>.<nl/>"
0034                "Overwrite?", url.url());
0035     const int answer = KMessageBox::warningTwoActionsCancel(mWidget,
0036                                                             message, title,
0037                                                             KStandardGuiItem::overwrite(),
0038                                                             KStandardGuiItem::back());
0039 
0040     return (answer == KMessageBox::PrimaryAction) ?   Overwrite :
0041            (answer == KMessageBox::SecondaryAction) ? PreviousQuestion :
0042                                                       Cancel;
0043 }
0044 
0045 Answer DialogHandler::queryDiscardOnReload(const AbstractDocument* document, const QString& title) const
0046 {
0047     const QString message = xi18nc("@info \"%title\" has been modified.",
0048                                    "There are unsaved modifications to <filename>%1</filename>. "
0049                                    "They will be lost if you reload the document.<nl/>"
0050                                    "Do you want to discard them?", document->title());
0051 
0052     const int answer = KMessageBox::warningContinueCancel(mWidget, message, title,
0053                                                           KStandardGuiItem::discard());
0054 
0055     return (answer == KMessageBox::Cancel) ? Cancel : Discard;
0056 }
0057 
0058 Answer DialogHandler::querySaveDiscard(const AbstractDocument* document, const QString& title) const
0059 {
0060     const QString message = xi18nc("@info \"%title\" has been modified.",
0061                                    "<filename>%1</filename> has been modified.<nl/>"
0062                                    "Do you want to save your changes or discard them?", document->title());
0063 
0064     const int answer = KMessageBox::warningTwoActionsCancel(mWidget,
0065                                                             message, title,
0066                                                             KStandardGuiItem::save(), KStandardGuiItem::discard());
0067 
0068     return (answer == KMessageBox::PrimaryAction) ?   Save :
0069            (answer == KMessageBox::SecondaryAction) ? Discard :
0070                                                       Cancel;
0071 }
0072 
0073 Answer DialogHandler::queryDiscard(const AbstractDocument* document, const QString& title) const
0074 {
0075     const QString message = xi18nc("@info \"%title\" has been modified.",
0076                                    "<filename>%1</filename> has been modified.<nl/>"
0077                                    "Do you want to discard your changes?", document->title());
0078 
0079     const int answer = KMessageBox::warningContinueCancel(mWidget, message, title,
0080                                                           KStandardGuiItem::discard());
0081 
0082     return (answer == KMessageBox::Cancel) ? Cancel : Discard;
0083 }
0084 
0085 }