File indexing completed on 2024-05-05 05:38:34

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QDebug>
0010 #include <QStringList>
0011 #include <QUrl>
0012 
0013 #include "tasktools.h"
0014 
0015 namespace TaskManager
0016 {
0017 #define NULL_UUID "00000000-0000-0000-0000-000000000000"
0018 
0019 inline static bool isValidLauncherUrl(const QUrl &url)
0020 {
0021     if (url.isEmpty() || !url.isValid()) {
0022         return false;
0023     }
0024 
0025     if (url.scheme() == QLatin1String("preferred")) {
0026         return !defaultApplication(url).isEmpty();
0027     }
0028 
0029     if (!url.isLocalFile() && url.scheme() != QLatin1String("applications") && url.scheme() != QLatin1String("preferred")) {
0030         return false;
0031     }
0032 
0033     return true;
0034 }
0035 
0036 inline static std::pair<QUrl, QStringList> deserializeLauncher(const QString &serializedLauncher)
0037 {
0038     QStringList activities;
0039     QUrl url(serializedLauncher);
0040 
0041     // The storage format is: [list of activity ids]\nURL
0042     // The activity IDs list can not be empty, it at least needs
0043     // to contain the nulluuid.
0044     // If parsing fails, we are considering the serialized launcher
0045     // to not have the activities array -- to have the old format
0046     if (serializedLauncher.startsWith('[')) {
0047         // It seems we have the activity specifier in the launcher
0048         const auto activitiesBlockEnd = serializedLauncher.indexOf("]\n");
0049 
0050         if (activitiesBlockEnd != -1) {
0051             activities = serializedLauncher.mid(1, activitiesBlockEnd - 1).split(",", Qt::SkipEmptyParts);
0052 
0053             if (!activities.isEmpty()) {
0054                 url = QUrl(serializedLauncher.mid(activitiesBlockEnd + 2));
0055             }
0056         }
0057     }
0058 
0059     // If the activities array is empty, this means that this launcher
0060     // needs to be on all activities
0061     if (activities.isEmpty()) {
0062         activities = QStringList({NULL_UUID});
0063     }
0064 
0065     return std::make_pair(url, activities);
0066 }
0067 
0068 } // namespace TaskManager