File indexing completed on 2024-04-21 04:44:06
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 #include "utils.h" 0019 0020 #ifdef Q_OS_WIN 0021 #include <windows.h> 0022 #include <windowsx.h> 0023 #include <shellapi.h> 0024 #include <winuser.h> 0025 #endif 0026 0027 #include <QRegExp> 0028 #include <QMutex> 0029 #include <QTextDocument> 0030 #include <QTextDocumentFragment> 0031 #include <QBuffer> 0032 0033 using namespace Snore; 0034 0035 Utils::Utils(QObject *parent): 0036 QObject(parent) 0037 { 0038 0039 } 0040 0041 Utils::~Utils() 0042 { 0043 0044 } 0045 0046 void Utils::bringWindowToFront(const QWindow *window, bool focus) 0047 { 0048 #ifdef Q_OS_WIN 0049 bringWindowToFront((HWND)window->winId(), focus); 0050 #else 0051 // TODO: implement 0052 Q_UNUSED(window); 0053 Q_UNUSED(focus); 0054 #endif 0055 } 0056 0057 #ifdef Q_OS_WIN 0058 void Utils::bringWindowToFront(HWND wid, bool focus) 0059 { 0060 int active = attatchToActiveProcess(); 0061 SetForegroundWindow(wid); 0062 if (focus) { 0063 SetFocus(wid); 0064 } 0065 detatchActiveProcess(active); 0066 } 0067 #endif 0068 0069 void Utils::raiseWindowToFront(const QWindow *window) 0070 { 0071 // Looks like qt is handling it on linux. 0072 #ifdef Q_OS_WIN 0073 int active = attatchToActiveProcess(); 0074 SetWindowPos((HWND)window->winId(), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); 0075 detatchActiveProcess(active); 0076 #else 0077 Q_UNUSED(window); 0078 #endif 0079 } 0080 0081 #define HTML_REPLACE(STRING, PATTERN){\ 0082 static QRegExp regexp(QLatin1String(PATTERN));\ 0083 STRING = STRING.replace(regexp, QStringLiteral("\\1"));\ 0084 }\ 0085 0086 QString Utils::normalizeMarkup(QString string, MarkupFlags tags) 0087 { 0088 static QMutex mutex; 0089 if (tags == AllMarkup) { 0090 return string; 0091 } else if (tags == NoMarkup) { 0092 return QTextDocumentFragment::fromHtml(string).toPlainText(); 0093 } 0094 0095 QMutexLocker lock(&mutex); 0096 if (~tags & Utils::Break) { 0097 static QRegExp br(QLatin1String("<br>")); 0098 string = string.replace(br, QStringLiteral("\n")); 0099 } 0100 if (~tags & Utils::Href) { 0101 HTML_REPLACE(string, "<a href=.*>([^<]*)</a>"); 0102 } 0103 if (~tags & Utils::Italic) { 0104 HTML_REPLACE(string, "<i>([^<]*)</i>"); 0105 } 0106 if (~tags & Utils::Bold) { 0107 HTML_REPLACE(string, "<b>([^<]*)</b>"); 0108 } 0109 if (~tags & Utils::Underline) { 0110 HTML_REPLACE(string, "<u>([^<]*)</u>"); 0111 } 0112 if (~tags & Utils::Font) { 0113 HTML_REPLACE(string, "<font.*>([^<]*)</font>"); 0114 } 0115 return string; 0116 } 0117 0118 QByteArray Utils::dataFromImage(const QImage &image) 0119 { 0120 QByteArray data; 0121 QBuffer buffer(&data); 0122 buffer.open(QBuffer::WriteOnly); 0123 image.save(&buffer, "PNG"); 0124 return data; 0125 } 0126 0127 #ifdef Q_OS_WIN 0128 int Utils::attatchToActiveProcess() 0129 { 0130 int idActive = GetWindowThreadProcessId(GetForegroundWindow(), NULL); 0131 return AttachThreadInput(GetCurrentThreadId(), idActive, TRUE) ? idActive : -1; 0132 } 0133 0134 void Utils::detatchActiveProcess(int idActive) 0135 { 0136 if (idActive != -1) { 0137 AttachThreadInput(GetCurrentThreadId(), idActive, FALSE); 0138 } 0139 } 0140 0141 #endif