File indexing completed on 2024-04-28 04:44:15

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 #ifndef UTILS_H
0019 #define UTILS_H
0020 
0021 #include "snore_exports.h"
0022 #include "snoreglobals.h"
0023 
0024 #include <QImage>
0025 #include <QObject>
0026 #include <QWindow>
0027 
0028 namespace Snore
0029 {
0030 class SNORE_EXPORT  Utils : public QObject
0031 {
0032     Q_OBJECT
0033 public:
0034     /**
0035      * The MarkupFlag enum.
0036      * If a flag is not present the markup key will be removed.
0037      * If any flag is present, special characters mus be html escaped.
0038      */
0039     enum MarkupFlag {
0040         /**
0041          * No markup is supported.
0042          * All markup will be removed.
0043          */
0044         NoMarkup   = 0,
0045 
0046         /**
0047          * Urls are supprotet.
0048          * &lt;a href="www.foo.bar"&gt;Foo Bar&lt;/a&gt;
0049          */
0050         Href        = 1 << 0,
0051 
0052         /**
0053          * Line breaks &lt;br&gt; are supprotet.
0054          * If the flag is not present &lt;br&gt; will be replaced by \\n
0055          */
0056         Break       = 1 << 1,
0057 
0058         /**
0059          * Bold &lt;b&gt; is supportet.
0060          */
0061         Bold        = 1 << 2,
0062 
0063         /**
0064          * Italic &lt;i&gt; is supportet.
0065          */
0066         Italic      = 1 << 3,
0067 
0068         /**
0069          * Underline &lt;u&gt; is supportet.
0070          */
0071         Underline   = 1 << 4,
0072 
0073         /**
0074          * Fonst are supportet.
0075          * &lt;font color="blue"&gt; word &lt;/font&gt;
0076          */
0077         Font        = 1 << 5,
0078 
0079         /**
0080          * All markup is supported.
0081          * No markup will be removed.
0082          */
0083         AllMarkup  = ~0
0084     };
0085 
0086     Q_DECLARE_FLAGS(MarkupFlags, MarkupFlag)
0087 
0088     Utils(QObject *parent = nullptr);
0089     ~Utils();
0090 
0091     /**
0092      * Raise a window to the front and activates it.
0093      * @param window the window to raise.
0094      * @param focus whether the window should request focus.
0095      */
0096     Q_INVOKABLE static void bringWindowToFront(const QWindow *window, bool focus);
0097 
0098 #ifdef Q_OS_WIN
0099     static void bringWindowToFront(HWND wid, bool focus);
0100 #endif
0101 
0102     /**
0103      * Raised the Window to front and don't make it active or steal focus.
0104      */
0105     Q_INVOKABLE static void raiseWindowToFront(const QWindow *window);
0106 
0107     /**
0108      * Removes unsupported markup tags from a string.
0109      */
0110     static QString normalizeMarkup(QString string, MarkupFlags tags);
0111 
0112     /**
0113      * Version number prefix for the settings.
0114      */
0115     static inline QString settingsVersionSchema()
0116     {
0117         return QStringLiteral("v1");
0118     }
0119 
0120     /**
0121      * Returns the SettingsKey formated with type and version.
0122      * @param key The key.
0123      * @param type The Type.
0124      * @param application The application's name.
0125      */
0126     static inline QString normalizeSettingsKey(const QString &key, SettingsType type, const QString &application)
0127     {
0128         if (type == LocalSetting) {
0129             return settingsVersionSchema() + QLatin1String("/LocalSettings/") + application + QLatin1Char('/') + key;
0130         } else {
0131             return settingsVersionSchema() + QLatin1String("/GlobalSettings/") +  key;
0132         }
0133     }
0134 
0135     static QByteArray dataFromImage(const QImage &image);
0136 
0137 private:
0138 #ifdef Q_OS_WIN
0139     static int attatchToActiveProcess();
0140     static void detatchActiveProcess(int idActive);
0141 #endif
0142 
0143 };
0144 
0145 }
0146 Q_DECLARE_OPERATORS_FOR_FLAGS(Snore::Utils::MarkupFlags)
0147 
0148 #endif // UTILS_H