From 6d943009205788e75b387d66014d807f4bf3e212 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Thu, 12 Aug 2021 23:22:59 +1000
Subject: Fix for Haiku


diff --git a/CMakeLists.txt b/CMakeLists.txt
index d2aaadf..1200ef2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -419,7 +419,7 @@ set_package_properties(Qt5QuickWidgets PROPERTIES
         PURPOSE "Optionally used for the touch gui for Krita")
 endif()
 
-if (NOT WIN32 AND NOT APPLE AND NOT ANDROID)
+if (NOT WIN32 AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
 
     find_package(Qt5 ${MIN_QT_VERSION} REQUIRED X11Extras)
 
diff --git a/krita/main.cc b/krita/main.cc
index 74eb200..667fadf 100644
--- a/krita/main.cc
+++ b/krita/main.cc
@@ -353,6 +353,9 @@ extern "C" int main(int argc, char **argv)
             qputenv("GST_PLUGIN_SCANNER", appimageMountDir + QFile::encodeName("/usr/lib/gstreamer-1.0/gst-plugin-scanner"));
         }
     }
+#elif defined(Q_OS_HAIKU)
+	qputenv("KRITA_PLUGIN_PATH", QFile::encodeName(root + "lib"));
+    qputenv("XDG_DATA_DIRS", QFile::encodeName(root + "share") + ":" + qgetenv("XDG_DATA_DIRS"));
 #else
     qputenv("XDG_DATA_DIRS", QFile::encodeName(root + "share"));
 #endif
@@ -521,6 +524,12 @@ extern "C" int main(int argc, char **argv)
     }
 
 
+#if defined Q_OS_HAIKU
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#endif
+
 #if defined HAVE_KCRASH
     KCrash::initialize();
 #elif defined USE_DRMINGW
@@ -676,6 +685,8 @@ extern "C" int main(int argc, char **argv)
     if (logUsage) {
         KisUsageLogger::close();
     }
-
+#if defined Q_OS_HAIKU
+	kill(::getpid(), SIGKILL);
+#endif
     return state;
 }
diff --git a/krita/pics/Breeze-dark/breeze-dark-icons.qrc b/krita/pics/Breeze-dark/breeze-dark-icons.qrc
index 22a8f23..9bb0b08 100644
--- a/krita/pics/Breeze-dark/breeze-dark-icons.qrc
+++ b/krita/pics/Breeze-dark/breeze-dark-icons.qrc
@@ -13,7 +13,6 @@
         <file>dark_arrow-right.svg</file>
         <file>dark_arrow-up.svg</file>
         <file>dark_bookmarks.svg</file>
-        <file alias="dark_configure-shortcuts.svg">dark_applications-system.svg</file>
         <file>dark_configure.svg</file>
         <file alias="dark_configure-toolbars.svg">dark_configure.svg</file>
         <file>dark_dialog-cancel.svg</file>
@@ -23,6 +22,7 @@
         <file>dark_document-edit.svg</file>
         <file>dark_document-export.svg</file>
         <file>dark_document-import.svg</file>
+        <file>dark_configure-shortcuts.svg</file>
         <file>dark_document-new.svg</file>
         <file>dark_document-open-recent.svg</file>
         <file>dark_document-open.svg</file>
diff --git a/krita/pics/Breeze-light/breeze-light-icons.qrc b/krita/pics/Breeze-light/breeze-light-icons.qrc
index a7688a6..54f8da1 100644
--- a/krita/pics/Breeze-light/breeze-light-icons.qrc
+++ b/krita/pics/Breeze-light/breeze-light-icons.qrc
@@ -13,7 +13,7 @@
         <file>light_arrow-right.svg</file>
         <file>light_arrow-up.svg</file>
         <file>light_bookmarks.svg</file>
-        <file alias="light_configure-shortcuts.svg">light_applications-system.svg</file>
+        <file>light_configure-shortcuts.svg</file>
         <file>light_configure.svg</file>
         <file alias="light_configure-toolbars.svg">light_configure.svg</file>
         <file>light_dialog-cancel.svg</file>
diff --git a/libs/image/kis_image_config.cpp b/libs/image/kis_image_config.cpp
index a59517b..57e88fb 100644
--- a/libs/image/kis_image_config.cpp
+++ b/libs/image/kis_image_config.cpp
@@ -400,6 +400,8 @@ void KisImageConfig::setLazyFrameCreationEnabled(bool value)
 #elif defined Q_OS_MACOS
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#elif defined Q_OS_HAIKU
+#include <OS.h>
 #endif
 
 int KisImageConfig::totalRAM()
@@ -428,6 +430,13 @@ int KisImageConfig::totalRAM()
     if(!error) {
         totalMemory = physmem >> 20;
     }
+#elif defined Q_OS_HAIKU
+	system_info info;
+	error = get_system_info(&info) == B_OK?0:1;
+	if (!error) {
+		uint64_t size = (info.max_pages * B_PAGE_SIZE);
+    	totalMemory = size >> 20;
+	}
 #elif defined Q_OS_WIN
     MEMORYSTATUSEX status;
     status.dwLength = sizeof(status);
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt
index 4876489..55c7f58 100644
--- a/libs/ui/CMakeLists.txt
+++ b/libs/ui/CMakeLists.txt
@@ -610,7 +610,7 @@ if (HAVE_QT_MULTIMEDIA)
     target_link_libraries(kritaui Qt5::Multimedia)
 endif()
 
-if (NOT WIN32 AND NOT APPLE AND NOT ANDROID)
+if (NOT WIN32 AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
   target_link_libraries(kritaui ${X11_X11_LIB}
                                 ${X11_Xinput_LIB})
 endif()
@@ -620,11 +620,17 @@ if(APPLE)
     target_link_libraries(kritaui ${APPKIT_LIBRARY})
 endif ()
 
+if(HAIKU)
+    target_link_libraries(kritaui network)
+    target_link_libraries(kritaui expat)
+    target_link_libraries(kritaui iconv)
+    target_link_libraries(kritaui intl)
+endif ()
 
 target_link_libraries(kritaui ${OPENEXR_LIBRARIES})
 
 # Add VSync disable workaround
-if(NOT WIN32 AND NOT APPLE AND NOT ANDROID)
+if(NOT WIN32 AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
     target_link_libraries(kritaui ${CMAKE_DL_LIBS} Qt5::X11Extras)
 endif()
 
diff --git a/libs/ui/KisApplication.cpp b/libs/ui/KisApplication.cpp
index 7ca7809..9d69ec7 100644
--- a/libs/ui/KisApplication.cpp
+++ b/libs/ui/KisApplication.cpp
@@ -164,7 +164,7 @@ KisApplication::KisApplication(const QString &key, int &argc, char **argv)
     setWindowIcon(KisIconUtils::loadIcon("krita"));
 
     if (qgetenv("KRITA_NO_STYLE_OVERRIDE").isEmpty()) {
-        QStringList styles = QStringList() << "breeze" << "fusion" << "plastique";
+        QStringList styles = QStringList() << "haiku" << "breeze" << "fusion" << "plastique";
         if (!styles.contains(style()->objectName().toLower())) {
             Q_FOREACH (const QString & style, styles) {
                 if (!setStyle(style)) {
@@ -419,9 +419,10 @@ bool KisApplication::start(const KisApplicationArguments &args)
     KoHashGeneratorProvider::instance()->setGenerator("MD5", new KisMD5Generator());
 
     KConfigGroup group(KSharedConfig::openConfig(), "theme");
+#ifndef Q_OS_HAIKU
     Digikam::ThemeManager themeManager;
     themeManager.setCurrentTheme(group.readEntry("Theme", "Krita dark"));
-
+#endif
 
     ResetStarting resetStarting(d->splashScreen, args.filenames().count()); // remove the splash when done
     Q_UNUSED(resetStarting);
diff --git a/libs/ui/KisMainWindow.cpp b/libs/ui/KisMainWindow.cpp
index bb68c6c..e52b6b3 100644
--- a/libs/ui/KisMainWindow.cpp
+++ b/libs/ui/KisMainWindow.cpp
@@ -279,9 +279,9 @@ public:
     KoToolDocker *toolOptionsDocker {0};
 
     QCloseEvent *deferredClosingEvent {0};
-
+#ifndef Q_OS_HAIKU
     Digikam::ThemeManager *themeManager {0};
-
+#endif
     QScrollArea *welcomeScroller {0};
     KisWelcomePageWidget *welcomePage {0};
 
@@ -335,8 +335,9 @@ KisMainWindow::KisMainWindow(QUuid uuid)
 
     d->viewManager = new KisViewManager(this, actionCollection());
     KConfigGroup group( KSharedConfig::openConfig(), "theme");
+#ifndef Q_OS_HAIKU
     d->themeManager = new Digikam::ThemeManager(group.readEntry("Theme", "Krita dark"), this);
-
+#endif
     d->windowStateConfig = KSharedConfig::openConfig()->group("MainWindow");
 
     setStandardToolBarMenuEnabled(true);
@@ -776,8 +777,9 @@ void KisMainWindow::slotThemeChanged()
 {
     // save theme changes instantly
     KConfigGroup group( KSharedConfig::openConfig(), "theme");
+#ifndef Q_OS_HAIKU
     group.writeEntry("Theme", d->themeManager->currentThemeName());
-
+#endif
     // reload action icons!
     Q_FOREACH (QAction *action, actionCollection()->actions()) {
         KisIconUtils::updateIcon(action);
@@ -2561,7 +2563,9 @@ void KisMainWindow::configChanged()
 #endif
 
     KConfigGroup group( KSharedConfig::openConfig(), "theme");
+#ifndef Q_OS_HAIKU
     d->themeManager->setCurrentTheme(group.readEntry("Theme", "Krita dark"));
+#endif
     d->actionManager()->updateGUI();
 
     QString s = cfg.getMDIBackgroundColor();
@@ -2786,14 +2790,14 @@ void KisMainWindow::createActions()
     d->showDocumentInfo  = actionManager->createAction("file_documentinfo");
     connect(d->showDocumentInfo, SIGNAL(triggered(bool)), this, SLOT(slotDocumentInfo()));
 
-
+#ifndef Q_OS_HAIKU
     d->themeManager->setThemeMenuAction(new KActionMenu(i18nc("@action:inmenu", "&Themes"), this));
     d->themeManager->registerThemeActions(actionCollection());
     connect(d->themeManager, SIGNAL(signalThemeChanged()), this, SLOT(slotThemeChanged()));
 
 
     connect(d->themeManager, SIGNAL(signalThemeChanged()), d->welcomePage, SLOT(slotUpdateThemeColors()));
-
+#endif
     d->toggleDockers = actionManager->createAction("view_toggledockers");
     KisConfig(true).showDockers(true);
     d->toggleDockers->setChecked(true);
diff --git a/libs/ui/dialogs/kis_dlg_preferences.cc b/libs/ui/dialogs/kis_dlg_preferences.cc
index f0a1a9d..26af04c 100644
--- a/libs/ui/dialogs/kis_dlg_preferences.cc
+++ b/libs/ui/dialogs/kis_dlg_preferences.cc
@@ -1530,7 +1530,7 @@ KisDlgPreferences::KisDlgPreferences(QWidget* parent, const char* name)
     page = new KPageWidgetItem(vbox, i18n("Keyboard Shortcuts"));
     page->setObjectName("shortcuts");
     page->setHeader(i18n("Shortcuts"));
-    page->setIcon(KisIconUtils::loadIcon("document-export"));
+    page->setIcon(KisIconUtils::loadIcon("configure-shortcuts"));
     m_pages << page;
     addPage(page);
     m_shortcutSettings = new ShortcutSettingsTab(vbox);
diff --git a/libs/ui/widgets/kis_floating_message.cpp b/libs/ui/widgets/kis_floating_message.cpp
index 85714a5..7bffe86 100644
--- a/libs/ui/widgets/kis_floating_message.cpp
+++ b/libs/ui/widgets/kis_floating_message.cpp
@@ -130,6 +130,7 @@ KisFloatingMessage::KisFloatingMessage(const QString &message, QWidget *parent,
     , m_alignment(alignment)
     , widgetQueuedForDeletion(false)
 {
+#ifndef Q_OS_HAIKU
     m_icon = KisIconUtils::loadIcon("krita").pixmap(256, 256).toImage();
 
     setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip | Qt::WindowTransparentForInput);
@@ -141,6 +142,7 @@ KisFloatingMessage::KisFloatingMessage(const QString &message, QWidget *parent,
     m_timer.setSingleShot( true );
     connect(&m_timer, SIGNAL(timeout()), SLOT(startFade()));
     connect(this, SIGNAL(destroyed()), SLOT(widgetDeleted()));
+#endif
 }
 
 void KisFloatingMessage::tryOverrideMessage(const QString message,
@@ -162,6 +164,7 @@ void KisFloatingMessage::tryOverrideMessage(const QString message,
 
 void KisFloatingMessage::showMessage()
 {
+#ifndef Q_OS_HAIKU
     if (widgetQueuedForDeletion) return;
 #if QT_VERSION >= QT_VERSION_CHECK(5,13,0)
     setGeometry(determineMetrics(fontMetrics().horizontalAdvance('x')));
@@ -172,6 +175,7 @@ void KisFloatingMessage::showMessage()
 
     QWidget::setVisible(true);
     m_timer.start(m_timeout);
+#endif
 }
 
 void KisFloatingMessage::setShowOverParent(bool show)
diff --git a/libs/widgetutils/KoResourcePaths.cpp b/libs/widgetutils/KoResourcePaths.cpp
index b165d17..5a9c555 100644
--- a/libs/widgetutils/KoResourcePaths.cpp
+++ b/libs/widgetutils/KoResourcePaths.cpp
@@ -127,6 +127,8 @@ QString getInstallationPrefix() {
     appdir.setPath(correctedPath);
     appdir.cdUp();
     return appdir.canonicalPath();
+    #elif defined(Q_OS_HAIKU)
+		return qApp->applicationDirPath() + "/";
 #else
 #ifdef Q_OS_ANDROID
     // qApp->applicationDirPath() isn't writable and android system won't allow
diff --git a/plugins/dockers/CMakeLists.txt b/plugins/dockers/CMakeLists.txt
index db5e216..49b098e 100644
--- a/plugins/dockers/CMakeLists.txt
+++ b/plugins/dockers/CMakeLists.txt
@@ -14,7 +14,7 @@ add_subdirectory(compositiondocker)
 add_subdirectory(patterndocker)
 add_subdirectory(griddocker)
 add_subdirectory(arrangedocker)
-if(HAVE_OCIO)
+if(HAVE_OCIO AND NOT HAIKU)
     add_subdirectory(lut)
 endif()
 add_subdirectory(overview)
-- 
2.30.2

