From 8caf659bb8a4c0a052da30e0c424af42e75af676 Mon Sep 17 00:00:00 2001
From: ababi <albert.babi@savoirfairelinux.com>
Date: Mon, 25 Jan 2021 18:14:39 +0100
Subject: [PATCH] misc: add start on login support for linux

Gitlab: #160

Change-Id: I166988985e4a2c9b1d06d21ba0a1394159478172
---
 CMakeLists.txt | 15 ++++++++-
 src/utils.cpp  | 84 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8d1b19fc9..3f9055e5b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,7 +13,8 @@ set(CMAKE_AUTOUIC ON)
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
 set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
+set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}
+    -DJAMI_DATA_DIR='"'${CMAKE_INSTALL_PREFIX}/share/jami-qt'"')
 
 set(QML_RESOURCES ${PROJECT_SOURCE_DIR}/resources.qrc)
 set(QML_RESOURCES_QML ${PROJECT_SOURCE_DIR}/qml.qrc)
@@ -230,6 +231,18 @@ install(TARGETS jami-qt
 install(FILES ${PROJECT_SOURCE_DIR}/jami-qt.desktop
     DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications)
 
+# install .desktop in the jami-qt data dir, so that it can be copied to the
+# autostart dir by the client
+install(FILES ${PROJECT_SOURCE_DIR}/jami-qt.desktop
+   DESTINATION
+      ${CMAKE_INSTALL_PREFIX}/share/jami-qt
+   PERMISSIONS
+      WORLD_READ
+      OWNER_WRITE
+      OWNER_READ
+      GROUP_READ
+)
+
 # logos
 install(FILES images/jami.svg
     DESTINATION ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps)
diff --git a/src/utils.cpp b/src/utils.cpp
index 1dc04fada..9305f3706 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -69,7 +69,72 @@ Utils::CreateStartupLink(const std::wstring& wstrAppName)
 
     return Utils::CreateLink(programPath.c_str(), linkPath.c_str());
 #else
-    return true;
+
+    QString desktopPath;
+    /* cmake should set JAMI_DATA_DIR, otherwise it checks the following dirs
+     *  - /usr/<data dir>
+     *  - /usr/local/<data dir>
+     *  - default install data dir
+     */
+
+#ifdef JAMI_DATA_DIR
+    desktopPath = JAMI_DATA_DIR;
+    desktopPath = desktopPath + "/jami-qt.desktop";
+#else
+    QString dataDir = "share/jami-qt/";
+    QStringList paths = { "/usr/" + dataDir + "jami-qt.desktop",
+                          "/usr/local/" + dataDir + "jami-qt.desktop",
+                          QDir::currentPath() + "/../../install/"
+                          + dataDir + "jami-qt/jami-qt.desktop" };
+    for (QString filename : paths) {
+        if (QFile::exists(filename)) {
+            desktopPath = filename;
+            break;
+        }
+    }
+#endif
+
+    if (desktopPath.isEmpty() || !(QFile::exists(desktopPath))) {
+        qDebug() << "Cannot locate .desktop file";
+        return false;
+    }
+
+    qDebug() << "Setting autostart link from " << desktopPath;
+
+    QString symlink = QStandardPaths::locate(QStandardPaths::ConfigLocation,
+                                             "autostart/jami-qt.desktop");
+    if (!symlink.isEmpty()) {
+        QFileInfo symlinkInfo(symlink);
+        if (symlinkInfo.isSymLink()) {
+            if (symlinkInfo.symLinkTarget() == desktopPath) {
+                qDebug() << symlink << "already points to" << desktopPath;
+                return true;
+            } else {
+                qDebug() << symlink << "exists but does not point to " << desktopPath;
+                QFile::remove(symlink);
+            }
+        }
+    } else {
+        QString autoStartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+                + "/autostart";
+
+        if (!QDir(autoStartDir).exists()) {
+            if (QDir().mkdir(autoStartDir)) {
+                qDebug() << "Created autostart directory: " << autoStartDir;
+            } else {
+                qWarning() << "Cannot create autostart directory: " << autoStartDir;
+                return false;
+            }
+        }
+        symlink = autoStartDir + "/jami-qt.desktop";
+    }
+
+    QFile srcFile (desktopPath);
+
+    bool result = srcFile.link(symlink);
+    qDebug() << symlink << (result ? "->" + desktopPath + " created successfully"
+                                   : "cannot be created");
+    return result;
 #endif
 }
 
@@ -116,6 +181,20 @@ Utils::DeleteStartupLink(const std::wstring& wstrAppName)
     linkPath += std::wstring(TEXT("\\") + wstrAppName + TEXT(".lnk"));
 
     DeleteFile(linkPath.c_str());
+
+#else
+    QString symlink = QStandardPaths::locate(QStandardPaths::ConfigLocation,
+                                             "autostart/jami-qt.desktop");
+    if (!symlink.isEmpty()) {
+        try {
+            QFile::remove(symlink);
+            qDebug() << "Autostart disabled," << symlink << "removed";
+        } catch (...) {
+            qDebug() << "Could not remove" << symlink;
+        }
+    } else {
+        qDebug() << "jami-qt.desktop symlink does not exist";
+    }
 #endif
 }
 
@@ -130,7 +209,8 @@ Utils::CheckStartupLink(const std::wstring& wstrAppName)
     linkPath += std::wstring(TEXT("\\") + wstrAppName + TEXT(".lnk"));
     return PathFileExists(linkPath.c_str());
 #else
-    return true;
+    return (!QStandardPaths::locate(QStandardPaths::ConfigLocation,
+                                    "autostart/jami-qt.desktop").isEmpty());
 #endif
 }
 
-- 
GitLab