File indexing completed on 2024-06-23 05:13:38

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/command_p.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "command.h"
0013 #include "view/keylistcontroller.h"
0014 
0015 #include <Libkleo/KeyListModel>
0016 
0017 #include <KLocalizedString>
0018 #include <KMessageBox>
0019 
0020 #include <QAbstractItemView>
0021 #include <QPointer>
0022 
0023 #include <gpgme++/key.h>
0024 
0025 #include <algorithm>
0026 #include <iterator>
0027 
0028 class Kleo::Command::Private
0029 {
0030     friend class ::Kleo::Command;
0031 
0032 protected:
0033     Command *const q;
0034 
0035 public:
0036     explicit Private(Command *qq);
0037     explicit Private(Command *qq, KeyListController *controller);
0038     explicit Private(Command *qq, QWidget *parent);
0039     virtual ~Private();
0040 
0041     QAbstractItemView *view() const
0042     {
0043         return view_;
0044     }
0045     QWidget *parentWidgetOrView() const
0046     {
0047         if (parentWidget_) {
0048             return parentWidget_;
0049         } else {
0050             return view_;
0051         }
0052     }
0053     WId parentWId() const
0054     {
0055         return parentWId_;
0056     }
0057     GpgME::Key key() const
0058     {
0059         return keys_.empty() ? GpgME::Key{} : keys_.front();
0060     }
0061     std::vector<GpgME::Key> keys() const
0062     {
0063         return keys_;
0064     }
0065 
0066     void finished()
0067     {
0068         Q_EMIT q->finished();
0069         doFinish();
0070         if (autoDelete) {
0071             q->deleteLater();
0072         }
0073     }
0074 
0075     void canceled()
0076     {
0077         Q_EMIT q->canceled();
0078         finished();
0079     }
0080 
0081     void error(const QString &text, const QString &caption = QString(), KMessageBox::Options options = KMessageBox::Notify) const
0082     {
0083         if (parentWId_) {
0084             KMessageBox::errorWId(parentWId_, text, caption, options);
0085         } else {
0086             KMessageBox::error(parentWidgetOrView(), text, caption, options);
0087         }
0088     }
0089     void success(const QString &text, const QString &caption = {}, KMessageBox::Options options = KMessageBox::Notify) const
0090     {
0091         static const QString noDontShowAgainName{};
0092         const QString title = caption.isEmpty() ? i18nc("@title:window", "Success") : caption;
0093         if (parentWId_) {
0094             KMessageBox::informationWId(parentWId_, text, title, noDontShowAgainName, options);
0095         } else {
0096             KMessageBox::information(parentWidgetOrView(), text, title, noDontShowAgainName, options);
0097         }
0098     }
0099     void information(const QString &text,
0100                      const QString &caption = QString(),
0101                      const QString &dontShowAgainName = QString(),
0102                      KMessageBox::Options options = KMessageBox::Notify) const
0103     {
0104         if (parentWId_) {
0105             KMessageBox::informationWId(parentWId_, text, caption, dontShowAgainName, options);
0106         } else {
0107             KMessageBox::information(parentWidgetOrView(), text, caption, dontShowAgainName, options);
0108         }
0109     }
0110     void informationList(const QString &text,
0111                          const QStringList &strlist,
0112                          const QString &title = {},
0113                          const QString &dontShowAgainName = {},
0114                          KMessageBox::Options options = KMessageBox::Notify) const
0115     {
0116         if (parentWId_) {
0117             KMessageBox::informationListWId(parentWId_, text, strlist, title, dontShowAgainName, options);
0118         } else {
0119             KMessageBox::informationList(parentWidgetOrView(), text, strlist, title, dontShowAgainName, options);
0120         }
0121     }
0122 
0123     void applyWindowID(QWidget *w) const
0124     {
0125         q->applyWindowID(w);
0126     }
0127 
0128 private:
0129     virtual void doFinish()
0130     {
0131     }
0132 
0133 private:
0134     bool autoDelete : 1;
0135     bool warnWhenRunningAtShutdown : 1;
0136     std::vector<GpgME::Key> keys_;
0137     QPointer<QAbstractItemView> view_;
0138     QPointer<QWidget> parentWidget_;
0139     WId parentWId_ = 0;
0140     QPointer<KeyListController> controller_;
0141 };