1 /* Copyright (C) 2006 - 2011 Thomas Gahr <thomas.gahr@physik.uni-muenchen.de>
2    Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
3 
4    This file is part of the Trojita Qt IMAP e-mail client,
5    http://trojita.flaska.net/
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of
10    the License or (at your option) version 3 or any later version
11    accepted by the membership of KDE e.V. (or its successor approved
12    by the membership of KDE e.V.), which shall act as a proxy
13    defined in Section 14 of version 3 of the license.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 
25 #include <QApplication>
26 #include <QBuffer>
27 #include <QCheckBox>
28 #include <QDir>
29 #include <QFontDatabase>
30 #include <QGridLayout>
31 #include <QIcon>
32 #include <QProcess>
33 #include <QSettings>
34 
35 #include "configure.cmake.h"
36 #include "Util.h"
37 #include "Window.h"
38 
39 namespace Gui {
40 
41 namespace Util {
42 
43 /** @short Path to the "package data directory"
44 
45 This path shall contain various files (like the localization data).  In case we're running without being installed
46 (or on some funny^Hnon-X11 platform), this function returns an empty QString.  Please also note that the returned
47 value might contain data for a completely different version of Trojita.
48 */
pkgDataDir()49 QString pkgDataDir()
50 {
51 #ifdef PKGDATADIR
52     return QStringLiteral(PKGDATADIR);
53 #else
54     return QString();
55 #endif
56 }
57 
58 /** @short Ask for something and provide a facility to not ask again
59 
60 Check settings whether an option is already set to ignore this question. If not, ask the user and remember whether
61 she wants to be asked again.
62 */
askForSomethingUnlessTold(const QString & title,const QString & message,const QString & settingsName,QMessageBox::StandardButtons buttons,QWidget * parent,QSettings * settings)63 int askForSomethingUnlessTold(const QString &title, const QString &message, const QString &settingsName,
64                               QMessageBox::StandardButtons buttons, QWidget *parent, QSettings *settings)
65 {
66     int saved = settings->value(settingsName, -1).toInt();
67     if (saved != -1) {
68         // This means that we're not supposed to ask again
69         return saved;
70     }
71 
72     QMessageBox box(QMessageBox::Question, title, message, QMessageBox::NoButton, parent);
73     box.setStandardButtons(buttons);
74     QCheckBox *checkbox = new QCheckBox(Gui::MainWindow::tr("Don't ask again"), &box);
75     QGridLayout *layout = qobject_cast<QGridLayout*>(box.layout());
76     Q_ASSERT(layout);
77     layout->addWidget(checkbox, 1, 1);
78     int res = box.exec();
79     if (checkbox->isChecked())
80         settings->setValue(settingsName, res);
81     return res;
82 }
83 
84 /** @short Return image data from the specified filename as a self-contained URL of the data: scheme
85 
86 The image is resized and always returned in the PNG format.
87 */
resizedImageAsDataUrl(const QString & fileName,const int extent)88 QString resizedImageAsDataUrl(const QString &fileName, const int extent)
89 {
90     QByteArray bdata;
91     QBuffer buf(&bdata);
92     buf.open(QIODevice::WriteOnly);
93     QIcon(fileName).pixmap(extent).toImage().save(&buf, "png");
94     return QLatin1String("data:image/png;base64,") + QString::fromUtf8(bdata.toBase64());
95 }
96 
97 } // namespace Util
98 
99 } // namespace Gui
100 
101 
102