File indexing completed on 2024-06-16 05:01:31

0001 /* Copyright (C) 2006 - 2011 Thomas Gahr <thomas.gahr@physik.uni-muenchen.de>
0002    Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 
0025 #include <QApplication>
0026 #include <QBuffer>
0027 #include <QCheckBox>
0028 #include <QDir>
0029 #include <QDropEvent>
0030 #include <QFontDatabase>
0031 #include <QGridLayout>
0032 #include <QIcon>
0033 #include <QMimeData>
0034 #include <QProcess>
0035 #include <QSettings>
0036 
0037 #include "configure.cmake.h"
0038 #include <Imap/Model/DragAndDrop.h>
0039 #include "Util.h"
0040 #include "Window.h"
0041 
0042 namespace {
0043 
0044 void messageBoxImpl(QWidget *parent, const QString &title, const QString &message, const QMessageBox::Icon icon)
0045 {
0046     Q_ASSERT(parent);
0047     auto box = new QMessageBox(icon, title, message, QMessageBox::Ok, parent);
0048     box->open();
0049 }
0050 
0051 }
0052 
0053 namespace Gui {
0054 
0055 namespace Util {
0056 
0057 /** @short Path to the "package data directory"
0058 
0059 This path shall contain various files (like the localization data).  In case we're running without being installed
0060 (or on some funny^Hnon-X11 platform), this function returns an empty QString.  Please also note that the returned
0061 value might contain data for a completely different version of Trojita.
0062 */
0063 QString pkgDataDir()
0064 {
0065 #ifdef PKGDATADIR
0066     return QStringLiteral(PKGDATADIR);
0067 #else
0068     return QString();
0069 #endif
0070 }
0071 
0072 /** @short Ask for something and provide a facility to not ask again
0073 
0074 Check settings whether an option is already set to ignore this question. If not, ask the user and remember whether
0075 she wants to be asked again.
0076 */
0077 int askForSomethingUnlessTold(const QString &title, const QString &message, const QString &settingsName,
0078                               QMessageBox::StandardButtons buttons, QWidget *parent, QSettings *settings)
0079 {
0080     int saved = settings->value(settingsName, -1).toInt();
0081     if (saved != -1) {
0082         // This means that we're not supposed to ask again
0083         return saved;
0084     }
0085 
0086     QMessageBox box(QMessageBox::Question, title, message, QMessageBox::NoButton, parent);
0087     box.setStandardButtons(buttons);
0088     QCheckBox *checkbox = new QCheckBox(Gui::MainWindow::tr("Don't ask again"), &box);
0089     QGridLayout *layout = qobject_cast<QGridLayout*>(box.layout());
0090     Q_ASSERT(layout);
0091     layout->addWidget(checkbox, 1, 1);
0092     int res = box.exec();
0093     if (checkbox->isChecked())
0094         settings->setValue(settingsName, res);
0095     return res;
0096 }
0097 
0098 /** @short Return image data from the specified filename as a self-contained URL of the data: scheme
0099 
0100 The image is resized and always returned in the PNG format.
0101 */
0102 QString resizedImageAsDataUrl(const QString &fileName, const int extent)
0103 {
0104     QByteArray bdata;
0105     QBuffer buf(&bdata);
0106     buf.open(QIODevice::WriteOnly);
0107     QIcon(fileName).pixmap(extent).toImage().save(&buf, "png");
0108     return QLatin1String("data:image/png;base64,") + QString::fromUtf8(bdata.toBase64());
0109 }
0110 
0111 /** @short QMessageBox::critical, but without reentering the event loop */
0112 void messageBoxCritical(QWidget *parent, const QString &title, const QString &message)
0113 {
0114     messageBoxImpl(parent, title, message, QMessageBox::Critical);
0115 }
0116 
0117 /** @short QMessageBox::warning, but without reentering the event loop */
0118 void messageBoxWarning(QWidget *parent, const QString &title, const QString &message)
0119 {
0120     messageBoxImpl(parent, title, message, QMessageBox::Warning);
0121 }
0122 
0123 /** @short Checks whether the data of a drop event is from another IMAP account, and is therefore not supported. */
0124 bool isFromDistinctImapAccount(QDropEvent* de)
0125 {
0126     if ((de->mimeData()->hasFormat(Imap::MimeTypes::xTrojitaImapPart) || de->mimeData()->hasFormat(Imap::MimeTypes::xTrojitaMessageList)) && !de->source()) {
0127         return true;
0128     }
0129     return false;
0130 }
0131 
0132 QString cssWarningBorder()
0133 {
0134     static QString border = QStringLiteral("border: 2px solid red; background-color: #E7C575; color: black; padding: 5px; margin: 5px; text-align: center;");
0135     return border;
0136 }
0137 
0138 } // namespace Util
0139 
0140 } // namespace Gui
0141 
0142