From 453443d2dcdb1f7b86c8c8312a50e8399ca2b877 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Mon, 28 Sep 2020 20:27:45 +1000
Subject: Fix for Haiku


diff --git a/3rdparty/sonnet/src/plugins/hunspell/hunspellclient.cpp b/3rdparty/sonnet/src/plugins/hunspell/hunspellclient.cpp
index e9767a9..78694a9 100644
--- a/3rdparty/sonnet/src/plugins/hunspell/hunspellclient.cpp
+++ b/3rdparty/sonnet/src/plugins/hunspell/hunspellclient.cpp
@@ -56,6 +56,8 @@ HunspellClient::HunspellClient(QObject *parent)
 
 #ifdef Q_OS_MAC
     directories << QLatin1String("/System/Library/Spelling/");
+#elif defined(Q_OS_HAIKU)
+	directories << QLatin1String("/system/data/hunspell/") << QLatin1String("/system/non-packaged/data/hunspell/") << QLatin1String("/boot/home/config/non-packaged/data/hunspell/");
 #else
     directories << QLatin1String("/usr/share/hunspell/") << QLatin1String("/usr/local/share/hunspell/") << QLatin1String("/usr/share/myspell/") << QLatin1String("/usr/share/myspell/dicts/") << QLatin1String("/usr/local/share/mozilla-dicts/");
 #endif
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b686d38..b3be740 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -504,6 +504,16 @@ elseif (APPLE)
 	)
 
 	set_source_files_properties(resources/icons/otter-browser.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
+elseif (HAIKU)
+	set(otter_src
+		${otter_src}
+		src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
+		src/modules/platforms/haiku/HaikuPlatformStyle.cpp
+		3rdparty/libmimeapps/ConfigReader.cpp
+		3rdparty/libmimeapps/DesktopEntry.cpp
+		3rdparty/libmimeapps/Index.cpp
+		3rdparty/libmimeapps/Tools.cpp
+	)
 elseif (UNIX)
 	find_package(Qt5DBus 5.6.0 QUIET)
 
@@ -554,6 +564,10 @@ elseif (UNIX)
 		target_link_libraries(otter-browser Qt5::DBus)
 	endif ()
 
+	if (HAIKU)
+		target_link_libraries(otter-browser -lbe)
+	endif ()
+
 	if (ENABLE_CRASHREPORTS)
 		target_link_libraries(otter-browser -lpthread)
 	endif ()
diff --git a/src/core/Application.cpp b/src/core/Application.cpp
index 13f34bc..1b041e2 100644
--- a/src/core/Application.cpp
+++ b/src/core/Application.cpp
@@ -46,6 +46,8 @@
 #include "../modules/platforms/windows/WindowsPlatformIntegration.h"
 #elif defined(Q_OS_MAC)
 #include "../modules/platforms/mac/MacPlatformIntegration.h"
+#elif defined(Q_OS_HAIKU)
+#include "../modules/platforms/haiku/HaikuPlatformIntegration.h"
 #elif defined(Q_OS_UNIX)
 #include "../modules/platforms/freedesktoporg/FreeDesktopOrgPlatformIntegration.h"
 #endif
@@ -318,7 +320,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv), Act
 	Console::createInstance();
 
 	SettingsManager::createInstance(profilePath);
-
+#ifndef Q_OS_HAIKU
 	if (!isReadOnly && !m_isFirstRun && !QFileInfo(profilePath).isWritable())
 	{
 		QMessageBox::warning(nullptr, tr("Warning"), tr("Profile directory (%1) is not writable, application will be running in read-only mode.").arg(profilePath), QMessageBox::Close);
@@ -386,7 +388,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv), Act
 			}
 		}
 	}
-
+#endif
 	SessionsManager::createInstance(profilePath, cachePath, isPrivate, isReadOnly);
 
 	if (!isReadOnly && !Migrator::run())
@@ -454,6 +456,8 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv), Act
 	m_platformIntegration = new WindowsPlatformIntegration(this);
 #elif defined(Q_OS_MAC)
 	m_platformIntegration = new MacPlatformIntegration(this);
+#elif defined(Q_OS_HAIKU)
+	m_platformIntegration = new HaikuPlatformIntegration(this);
 #elif defined(Q_OS_UNIX)
 	m_platformIntegration = new FreeDesktopOrgPlatformIntegration(this);
 #endif
diff --git a/src/core/SettingsManager.cpp b/src/core/SettingsManager.cpp
index 43aa107..490a905 100644
--- a/src/core/SettingsManager.cpp
+++ b/src/core/SettingsManager.cpp
@@ -80,7 +80,7 @@ void SettingsManager::createInstance(const QString &path)
 	registerOption(Browser_EnableSingleKeyShortcutsOption, BooleanType, true);
 	registerOption(Browser_EnableSpellCheckOption, BooleanType, true);
 	registerOption(Browser_EnableTrayIconOption, BooleanType, true);
-	registerOption(Browser_HomePageOption, StringType, QString());
+	registerOption(Browser_HomePageOption, StringType, QLatin1String("https://haiku-os.org"));
 	registerOption(Browser_InactiveTabTimeUntilSuspendOption, IntegerType, -1);
 	registerOption(Browser_KeyboardShortcutsProfilesOrderOption, ListType, QStringList(QLatin1String("default")));
 	registerOption(Browser_LocaleOption, StringType, QLatin1String("system"));
@@ -108,19 +108,19 @@ void SettingsManager::createInstance(const QString &path)
 	registerOption(Choices_WarnQuitOption, EnumerationType, QLatin1String("noWarn"), {QLatin1String("alwaysWarn"), QLatin1String("warnOpenTabs"), QLatin1String("noWarn")});
 	registerOption(Choices_WarnQuitTransfersOption, BooleanType, true);
 	registerOption(Content_BackgroundColorOption, ColorType, QColor(0xFF, 0xFF, 0xFF));
-	registerOption(Content_CursiveFontOption, FontType, QLatin1String("Comic Sans MS"));
+	registerOption(Content_CursiveFontOption, FontType, QLatin1String("Noto Serif"));
 	registerOption(Content_DefaultCharacterEncodingOption, StringType, QLatin1String("auto"));
 	registerOption(Content_DefaultFixedFontSizeOption, IntegerType, 16);
 	registerOption(Content_DefaultFontSizeOption, IntegerType, 16);
 	registerOption(Content_DefaultZoomOption, IntegerType, 100);
-	registerOption(Content_FantasyFontOption, FontType, QLatin1String("Impact"));
-	registerOption(Content_FixedFontOption, FontType, QLatin1String("DejaVu Sans Mono"));
+	registerOption(Content_FantasyFontOption, FontType, QLatin1String("Noto Serif"));
+	registerOption(Content_FixedFontOption, FontType, QLatin1String("Noto Mono"));
 	registerOption(Content_LinkColorOption, ColorType, QColor(0, 0, 0xEE));
 	registerOption(Content_MinimumFontSizeOption, IntegerType, -1);
 	registerOption(Content_PageReloadTimeOption, IntegerType, -1);
-	registerOption(Content_SansSerifFontOption, FontType, QLatin1String("DejaVu Sans"));
-	registerOption(Content_SerifFontOption, FontType, QLatin1String("DejaVu Serif"));
-	registerOption(Content_StandardFontOption, FontType, QLatin1String("DejaVu Serif"));
+	registerOption(Content_SansSerifFontOption, FontType, QLatin1String("Noto Sans"));
+	registerOption(Content_SerifFontOption, FontType, QLatin1String("Noto Serif"));
+	registerOption(Content_StandardFontOption, FontType, QLatin1String("Noto Serif"));
 	registerOption(Content_TextColorOption, ColorType, QColor(0, 0, 0));
 	registerOption(Content_UserStyleSheetOption, PathType, QString());
 	registerOption(Content_VisitedLinkColorOption, ColorType, QColor(0x55, 0x1A, 0x8B));
@@ -156,7 +156,7 @@ void SettingsManager::createInstance(const QString &path)
 	registerOption(Interface_TabCrashingActionOption, EnumerationType, QLatin1String("ask"), {QLatin1String("ask"), QLatin1String("close"), QLatin1String("reload")});
 	registerOption(Interface_UseFancyDateTimeFormatOption, BooleanType, true);
 	registerOption(Interface_UseNativeNotificationsOption, BooleanType, true);
-	registerOption(Interface_UseSystemIconThemeOption, BooleanType, false);
+	registerOption(Interface_UseSystemIconThemeOption, BooleanType, true);
 	registerOption(Interface_WidgetStyleOption, StringType, QString());
 	registerOption(Network_AcceptLanguageOption, StringType, QLatin1String("system,*;q=0.9"));
 	registerOption(Network_CookiesKeepModeOption, EnumerationType, QLatin1String("keepUntilExpires"), {QLatin1String("keepUntilExpires"), QLatin1String("keepUntilExit"), QLatin1String("ask")});
diff --git a/src/main.cpp b/src/main.cpp
index bec0f9c..885b439 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -37,6 +37,10 @@
 #include <QtCore/QUrl>
 #include <QtWidgets/QMessageBox>
 
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+
 using namespace Otter;
 
 #if !defined(Q_OS_WIN32)
diff --git a/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp b/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
new file mode 100644
index 0000000..40e0c3b
--- /dev/null
+++ b/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
@@ -0,0 +1,174 @@
+/**************************************************************************
+* Otter Browser: Web browser controlled by the user, not vice-versa.
+* Copyright (C) 2019 Gerasim Troeglazov <3dEyes@gmail.com>
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+**************************************************************************/
+
+#include "HaikuPlatformIntegration.h"
+#include "HaikuPlatformStyle.h"
+#include "../../../core/Application.h"
+#include "../../../core/NotificationsManager.h"
+#include "../../../core/SettingsManager.h"
+#include "../../../core/TransfersManager.h"
+#include "../../../core/Utils.h"
+#include "../../../../3rdparty/libmimeapps/DesktopEntry.h"
+#include "../../../../3rdparty/libmimeapps/Index.h"
+
+#include <QtConcurrent/QtConcurrent>
+#include <QtGui/QDesktopServices>
+#include <QtGui/QIcon>
+#include <QtGui/QRgb>
+
+#include <Bitmap.h>
+#include <Entry.h>
+#include <Path.h>
+#include <Message.h>
+#include <MimeType.h>
+#include <NodeInfo.h>
+#include <Notification.h>
+#include <String.h>
+
+namespace Otter
+{
+
+HaikuPlatformIntegration::HaikuPlatformIntegration(QObject *parent) : PlatformIntegration(parent)
+{
+}
+
+HaikuPlatformIntegration::~HaikuPlatformIntegration()
+{
+}
+
+void HaikuPlatformIntegration::runApplication(const QString &command, const QUrl &url) const
+{
+	if (command.isEmpty())
+		QDesktopServices::openUrl(url);
+
+	std::vector<std::string> fileNames;
+	fileNames.push_back((url.isLocalFile() ? QDir::toNativeSeparators(url.toLocalFile()) : url.url()).toStdString());
+
+	const std::vector<std::string> parsed(LibMimeApps::DesktopEntry::parseExec(command.toStdString(), \
+		fileNames, LibMimeApps::DesktopEntry::ParseOptions::NecessarilyUseUrl));
+
+	if (parsed.size() < 1)
+		return;
+
+	QStringList arguments;
+	arguments.reserve(static_cast<int>(parsed.size()));
+
+	for (std::vector<std::string>::size_type i = 1; i < parsed.size(); ++i)
+		arguments.append(QString::fromStdString(parsed.at(i)));
+
+	QProcess::startDetached(QString::fromStdString(parsed.at(0)), arguments);
+}
+
+void HaikuPlatformIntegration::showNotification(Notification *notification)
+{
+	const Notification::Message message(notification->getMessage());
+	
+	QFileInfo appFileInfo(QCoreApplication::applicationFilePath());
+	BString stitle((const char *)(message.getTitle().toUtf8()));
+	BString smessage((const char *)(message.message.toUtf8()));
+	BString smessageId((const char *)(appFileInfo.fileName().toUtf8()));
+	BString group((const char*)(appFileInfo.baseName().toUtf8()));
+
+	BFile file(appFileInfo.filePath().toUtf8(), O_RDONLY);
+	BNodeInfo nodeInfo(&file);
+	BRect rect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON -1);
+	BBitmap bitmap(rect, B_RGBA32);
+
+	BNotification haikuNotification(B_INFORMATION_NOTIFICATION);
+	haikuNotification.SetGroup(group);
+	haikuNotification.SetTitle(stitle);
+	haikuNotification.SetMessageID(smessageId);
+	haikuNotification.SetContent(smessage);
+
+	if (nodeInfo.GetTrackerIcon(&bitmap, B_LARGE_ICON) == B_NO_ERROR)
+		haikuNotification.SetIcon(&bitmap);
+
+	const int visibilityDuration(SettingsManager::getOption(SettingsManager::Interface_NotificationVisibilityDurationOption).toInt());
+	
+	haikuNotification.Send((visibilityDuration <= 0) ? 3000000 : visibilityDuration * 1000000);
+}
+
+void HaikuPlatformIntegration::updateTransfersProgress()
+{
+}
+
+void HaikuPlatformIntegration::setTransfersProgress(qint64 bytesTotal, qint64 bytesReceived, qint64 transferAmount)
+{
+}
+
+Style* HaikuPlatformIntegration::createStyle(const QString &name) const
+{
+	return nullptr;
+}
+
+QVector<ApplicationInformation> HaikuPlatformIntegration::getApplicationsForMimeType(const QMimeType &mimeType)
+{
+	QVector<ApplicationInformation> result;
+	
+	BMimeType type(mimeType.name().toLatin1());
+	BMessage applications;
+	if (type.GetSupportingApps(&applications) != B_OK)
+		return result;
+
+	ApplicationInformation application;
+	application.command = QString("/bin/open");
+	application.name = QString("Default application");
+	application.icon = QIcon::fromTheme("application-x-executable");
+	result.append(application);
+
+	const char* signature;
+	int32 i = 0;
+	while (applications.FindString("applications", i, &signature) == B_OK) {
+		char name[B_FILE_NAME_LENGTH];
+		BMimeType applicationType(signature);
+		entry_ref app_ref;
+		applicationType.GetAppHint(&app_ref);
+		icon_size iconSize = B_MINI_ICON;
+		BRect iconRect(0, 0, iconSize - 1, iconSize - 1);
+		BBitmap* appIcon = new BBitmap(iconRect, 0, B_RGBA32);
+		if (applicationType.GetIcon(appIcon, iconSize) < B_OK) {
+			delete appIcon;
+			appIcon = NULL;
+		}		
+		BPath path(&app_ref);
+		if (applicationType.GetShortDescription(name) == B_OK) {
+			ApplicationInformation application;
+			application.command = QString(path.Path());
+			application.name = QString(name);
+			if (appIcon != NULL) {
+				QImage image(iconSize, iconSize, QImage::Format_ARGB32);
+				memcpy((uchar*)image.bits(), (uchar*)appIcon->Bits(), iconSize * iconSize * 4);
+				QPixmap pixmap = QPixmap::fromImage(image);
+				application.icon = QIcon(pixmap);
+				delete appIcon;
+			}
+			result.append(application);
+		}
+		i++;
+	}
+
+	return result;
+}
+
+bool HaikuPlatformIntegration::canShowNotifications() const
+{
+	return true;
+}
+
+}
diff --git a/src/modules/platforms/haiku/HaikuPlatformIntegration.h b/src/modules/platforms/haiku/HaikuPlatformIntegration.h
new file mode 100644
index 0000000..f49bb32
--- /dev/null
+++ b/src/modules/platforms/haiku/HaikuPlatformIntegration.h
@@ -0,0 +1,53 @@
+/**************************************************************************
+* Otter Browser: Web browser controlled by the user, not vice-versa.
+* Copyright (C) 2019 Gerasim Troeglazov <3dEyes@gmail.com>
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+**************************************************************************/
+
+#ifndef OTTER_HAIKUPLATFORMINTEGRATION_H
+#define OTTER_HAIKUPLATFORMINTEGRATION_H
+
+#include "../../../core/PlatformIntegration.h"
+
+namespace Otter
+{
+
+class HaikuPlatformIntegration final : public PlatformIntegration
+{
+	Q_OBJECT
+
+public:
+	explicit HaikuPlatformIntegration(QObject *parent);
+	~HaikuPlatformIntegration();
+
+	void runApplication(const QString &command, const QUrl &url = {}) const override;
+	Style* createStyle(const QString &name) const override;
+	QVector<ApplicationInformation> getApplicationsForMimeType(const QMimeType &mimeType) override;
+	bool canShowNotifications() const override;
+
+public slots:
+	void showNotification(Notification *notification) override;
+
+protected:
+	void setTransfersProgress(qint64 bytesTotal, qint64 bytesReceived, qint64 transferAmount);
+
+protected slots:
+	void updateTransfersProgress();
+};
+
+}
+
+#endif
diff --git a/src/modules/platforms/haiku/HaikuPlatformStyle.cpp b/src/modules/platforms/haiku/HaikuPlatformStyle.cpp
new file mode 100644
index 0000000..2258226
--- /dev/null
+++ b/src/modules/platforms/haiku/HaikuPlatformStyle.cpp
@@ -0,0 +1,46 @@
+/**************************************************************************
+* Otter Browser: Web browser controlled by the user, not vice-versa.
+* Copyright (C) 2019 Gerasim Troeglazov <3dEyes@gmail.com>
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+**************************************************************************/
+
+#include "HaikuPlatformStyle.h"
+#include "../../../core/ThemesManager.h"
+#include "../../../ui/MainWindow.h"
+#include "../../../ui/ToolBarWidget.h"
+
+#include <QtCore/QProcess>
+#include <QtGui/QPainter>
+#include <QtWidgets/QStyleOption>
+
+namespace Otter
+{
+
+HaikuPlatformStyle::HaikuPlatformStyle(const QString &name) : Style(name)
+{
+}
+
+void HaikuPlatformStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
+{
+	Style::drawControl(element, option, painter, widget);
+}
+
+int HaikuPlatformStyle::getExtraStyleHint(Style::ExtraStyleHint hint) const
+{
+	return Style::getExtraStyleHint(hint);
+}
+
+}
diff --git a/src/modules/platforms/haiku/HaikuPlatformStyle.h b/src/modules/platforms/haiku/HaikuPlatformStyle.h
new file mode 100644
index 0000000..8b50d01
--- /dev/null
+++ b/src/modules/platforms/haiku/HaikuPlatformStyle.h
@@ -0,0 +1,39 @@
+/**************************************************************************
+* Otter Browser: Web browser controlled by the user, not vice-versa.
+* Copyright (C) 2019 Gerasim Troeglazov <3dEyes@gmail.com>
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+**************************************************************************/
+
+#ifndef OTTER_HAIKUPLATFORMSTYLE_H
+#define OTTER_HAIKUPLATFORMSTYLE_H
+
+#include "../../../ui/Style.h"
+
+namespace Otter
+{
+
+class HaikuPlatformStyle final : public Style
+{
+	Q_OBJECT
+public:
+	explicit HaikuPlatformStyle(const QString &name);
+	void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
+	int getExtraStyleHint(ExtraStyleHint hint) const override;
+};
+
+}
+
+#endif
-- 
2.28.0


From 1a7d99ff0bad3b901764c9f8f122d4402d1d1622 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 30 Dec 2020 22:34:52 +1000
Subject: Fix notification api


diff --git a/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp b/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
index 40e0c3b..898ac7b 100644
--- a/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
+++ b/src/modules/platforms/haiku/HaikuPlatformIntegration.cpp
@@ -77,11 +77,24 @@ void HaikuPlatformIntegration::runApplication(const QString &command, const QUrl
 
 void HaikuPlatformIntegration::showNotification(Notification *notification)
 {
-	const Notification::Message message(notification->getMessage());
+	QString title;
+
+	switch (notification->getLevel())
+	{
+		case Notification::ErrorLevel:
+			title = "Error";
+			break;
+		case Notification::WarningLevel:
+			title = "Warning";
+			break;
+		default:
+			title = "Information";
+			break;
+	}
 	
 	QFileInfo appFileInfo(QCoreApplication::applicationFilePath());
-	BString stitle((const char *)(message.getTitle().toUtf8()));
-	BString smessage((const char *)(message.message.toUtf8()));
+	BString stitle((const char *)(title.toUtf8()));
+	BString smessage((const char *)(notification->getMessage().toUtf8()));
 	BString smessageId((const char *)(appFileInfo.fileName().toUtf8()));
 	BString group((const char*)(appFileInfo.baseName().toUtf8()));
 
-- 
2.28.0

