Skip to content

Commit

Permalink
Implement desktop-file-install
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Feb 10, 2023
1 parent a310f45 commit d794ed6
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 156 deletions.
4 changes: 3 additions & 1 deletion applications/applications.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TEMPLATE = subdirs

SUBDIRS = \
desktop-file-edit \
desktop-file-install \
desktop-file-validate \
gio \
launcher \
Expand Down Expand Up @@ -36,6 +37,7 @@ xdg-open.depends = system-service
gio.depends = system-service
xdg-settings.depends = system-service
xdg-icon-resource.depends = system-service
desktop-file-edit.depends =
desktop-file-edit.depends = desktop-file-edit
desktop-file-install.depends =

INSTALLS += $$SUBDIRS
4 changes: 3 additions & 1 deletion applications/desktop-file-edit/desktop-file-edit.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
edit.cpp \
main.cpp

HEADERS +=
HEADERS += \
edit.h

TARGET = desktop-file-edit
include(../../qmake/common.pri)
Expand Down
165 changes: 165 additions & 0 deletions applications/desktop-file-edit/edit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "edit.h"

QCommandLineOption setKeyOption(
"set-key",
"Set the KEY key to the value passed to the next --set-value option. A matching --set-value option is mandatory.",
"KEY"
);
QCommandLineOption setValueOption(
"set-value",
"Set the key specified with the previous --set-key option to VALUE. A matching --set-key option is mandatory.",
"VALUE"
);
QCommandLineOption setNameOption(
"set-name",
"NOT IMPLEMENTED",
"NAME"
);
QCommandLineOption copyNameToGenericNameOption(
"copy-name-to-generic-name",
"Copy the value of the Name key to the GenericName key. Note that a desktop file requires a Name key to be valid, so this option will always have an effect."
);
QCommandLineOption setGenericNameOption(
"set-generic-name",
"Set the generic name (key DisplayName) to GENERIC-NAME. If a generic name was already set, it will be overridden. Localizations of the old generic name will be removed.",
"GENERIC-NAME"
);
QCommandLineOption copyGenericNameToNameOption(
"copy-generic-name-to-name",
"NOT IMPLEMENTED"
);
QCommandLineOption setCommentOption(
"set-comment",
"NOT IMPLEMENTED",
"COMMENT"
);
QCommandLineOption setIconOption(
"set-icon",
"Set the icon (key Icon) to ICON. If an icon was already set, it will be overridden. Localizations of the old icon will be removed.",
"ICON"
);
QCommandLineOption addCategoryOption(
"add-category",
"NOT IMPLEMENTED",
"CATEGORY"
);
QCommandLineOption removeCategoryOption(
"remove-category",
"NOT IMPLEMENTED",
"CATEGORY"
);
QCommandLineOption addMimeTypeOption(
"add-mime-type",
"NOT IMPLEMENTED",
"MIME-TYPE"
);
QCommandLineOption removeMimeTypeOption(
"remove-mime-type",
"NOT IMPLEMENTED",
"MIME-TYPE"
);
QCommandLineOption addOnlyShowInOption(
"add-only-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
QCommandLineOption removeOnlyShowInOption(
"remove-only-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
QCommandLineOption addNotShowInOption(
"add-not-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
QCommandLineOption removeNotShowInOption(
"remove-not-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
QCommandLineOption removeKeyOption(
"remove-key",
"Remove the KEY key from the desktop files, if present.",
"KEY"
);

void addEditOptions(QCommandLineParser& parser){
parser.addOption(setKeyOption);
parser.addOption(setValueOption);
parser.addOption(setNameOption);
parser.addOption(copyNameToGenericNameOption);
parser.addOption(setGenericNameOption);
parser.addOption(copyGenericNameToNameOption);
parser.addOption(setCommentOption);
parser.addOption(setIconOption);
parser.addOption(addCategoryOption);
parser.addOption(removeCategoryOption);
parser.addOption(addMimeTypeOption);
parser.addOption(removeMimeTypeOption);
parser.addOption(addOnlyShowInOption);
parser.addOption(removeOnlyShowInOption);
parser.addOption(addNotShowInOption);
parser.addOption(removeNotShowInOption);
parser.addOption(removeKeyOption);
}

bool validateSetKeyValueOptions(QCommandLineParser& parser){
auto options = parser.optionNames();
if(parser.isSet(setKeyOption) || parser.isSet(setValueOption)){
for(int i=0; i< options.length(); i++){
auto option = options[i];
if(setValueOption.names().contains(option)){
qDebug() << "Option \"--set-value\" used without a prior \"--set-key\" option";
qDebug() << "Run 'desktop-file-edit --help' to see a full list of available command line options.";
return false;
}
if(setKeyOption.names().contains(option)){
option = options[++i];
if(!setValueOption.names().contains(option)){
qDebug() << "Option \"--set-key\" used without a following \"--set-value\" option";
qDebug() << "Run 'desktop-file-edit --help' to see a full list of available command line options.";
return false;
}
}
}
}
return true;
}

void applyChanges(QCommandLineParser& parser, QJsonObject reg, QString name){
int removeIndex = 0;
int keyIndex = 0;
int genericNameIndex = 0;
int iconIndex = 0;
QString key;
auto options = parser.optionNames();
for(int i=0; i< options.length(); i++){
auto option = options[i];
if(copyNameToGenericNameOption.names().contains(option)){
reg["displayName"] = name;
continue;
}
if(removeKeyOption.names().contains(option)){
reg.remove(parser.values(removeKeyOption)[removeIndex]);
removeIndex++;
continue;
}
if(setGenericNameOption.names().contains(option)){
reg["displayname"] = parser.values(setGenericNameOption)[genericNameIndex];
genericNameIndex++;
continue;
}
if(setIconOption.names().contains(option)){
reg["icon"] = parser.values(setIconOption)[iconIndex];
iconIndex++;
continue;
}
if(setKeyOption.names().contains(option)){
key = parser.values(setKeyOption)[keyIndex];
reg[key] = parser.values(setValueOption)[keyIndex];
i++;
keyIndex++;
}
}
}
12 changes: 12 additions & 0 deletions applications/desktop-file-edit/edit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#ifndef EDIT_H
#define EDIT_H

#include <QCommandLineParser>
#include <QJsonObject>

void addEditOptions(QCommandLineParser& parser);
bool validateSetKeyValueOptions(QCommandLineParser& parser);
void applyChanges(QCommandLineParser& parser, QJsonObject reg, QString name);

#endif // EDIT_H
160 changes: 6 additions & 154 deletions applications/desktop-file-edit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <liboxide.h>
#include <liboxide/applications.h>

#include "edit.h"

using namespace Oxide::Sentry;
using namespace Oxide::JSON;
using namespace Oxide::Applications;
Expand All @@ -27,132 +29,15 @@ int main(int argc, char *argv[]){
parser.applicationDescription();
parser.addHelpOption();
parser.addVersionOption();

QCommandLineOption setKeyOption(
"set-key",
"Set the KEY key to the value passed to the next --set-value option. A matching --set-value option is mandatory.",
"KEY"
);
parser.addOption(setKeyOption);
QCommandLineOption setValueOption(
"set-value",
"Set the key specified with the previous --set-key option to VALUE. A matching --set-key option is mandatory.",
"VALUE"
);
parser.addOption(setValueOption);
QCommandLineOption setNameOption(
"set-name",
"NOT IMPLEMENTED",
"NAME"
);
parser.addOption(setNameOption);
QCommandLineOption copyNameToGenericNameOption(
"copy-name-to-generic-name",
"Copy the value of the Name key to the GenericName key. Note that a desktop file requires a Name key to be valid, so this option will always have an effect."
);
parser.addOption(copyNameToGenericNameOption);
QCommandLineOption setGenericNameOption(
"set-generic-name",
"Set the generic name (key DisplayName) to GENERIC-NAME. If a generic name was already set, it will be overridden. Localizations of the old generic name will be removed.",
"GENERIC-NAME"
);
parser.addOption(setGenericNameOption);
QCommandLineOption copyGenericNameToNameOption(
"copy-generic-name-to-name",
"NOT IMPLEMENTED"
);
parser.addOption(copyGenericNameToNameOption);
QCommandLineOption setCommentOption(
"set-comment",
"NOT IMPLEMENTED",
"COMMENT"
);
parser.addOption(setCommentOption);
QCommandLineOption setIconOption(
"set-icon",
"Set the icon (key Icon) to ICON. If an icon was already set, it will be overridden. Localizations of the old icon will be removed.",
"ICON"
);
parser.addOption(setIconOption);
QCommandLineOption addCategoryOption(
"add-category",
"NOT IMPLEMENTED",
"CATEGORY"
);
parser.addOption(addCategoryOption);
QCommandLineOption removeCategoryOption(
"remove-category",
"NOT IMPLEMENTED",
"CATEGORY"
);
parser.addOption(removeCategoryOption);
QCommandLineOption addMimeTypeOption(
"add-mime-type",
"NOT IMPLEMENTED",
"MIME-TYPE"
);
parser.addOption(addMimeTypeOption);
QCommandLineOption removeMimeTypeOption(
"remove-mime-type",
"NOT IMPLEMENTED",
"MIME-TYPE"
);
parser.addOption(removeMimeTypeOption);
QCommandLineOption addOnlyShowInOption(
"add-only-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
parser.addOption(addOnlyShowInOption);
QCommandLineOption removeOnlyShowInOption(
"remove-only-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
parser.addOption(removeOnlyShowInOption);
QCommandLineOption addNotShowInOption(
"add-not-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
parser.addOption(addNotShowInOption);
QCommandLineOption removeNotShowInOption(
"remove-not-show-in",
"NOT IMPLEMENTED",
"ENVIRONMENT"
);
parser.addOption(removeNotShowInOption);
QCommandLineOption removeKeyOption(
"remove-key",
"Remove the KEY key from the desktop files, if present.",
"KEY"
);
parser.addOption(removeKeyOption);

addEditOptions(parser);
parser.addPositionalArgument("FILE", "Application registration to edit");
parser.process(app);
auto args = parser.positionalArguments();
if(args.empty() || args.length() > 1){
parser.showHelp(EXIT_FAILURE);
}
auto options = parser.optionNames();
if(parser.isSet(setKeyOption) || parser.isSet(setValueOption)){
for(int i=0; i< options.length(); i++){
auto option = options[i];
if(setValueOption.names().contains(option)){
qDebug() << "Option \"--set-value\" used without a prior \"--set-key\" option";
qDebug() << "Run 'desktop-file-edit --help' to see a full list of available command line options.";
return EXIT_FAILURE;
}
if(setKeyOption.names().contains(option)){
option = options[++i];
if(!setValueOption.names().contains(option)){
qDebug() << "Option \"--set-key\" used without a following \"--set-value\" option";
qDebug() << "Run 'desktop-file-edit --help' to see a full list of available command line options.";
return EXIT_FAILURE;
}
}
}
if(!validateSetKeyValueOptions(parser)){
return EXIT_FAILURE;
}
auto path = args.first();
QFile file(path);
Expand All @@ -173,40 +58,7 @@ int main(int argc, char *argv[]){

QFileInfo info(file);
auto name = info.baseName();
int removeIndex = 0;
int keyIndex = 0;
int genericNameIndex = 0;
int iconIndex = 0;
QString key;
for(int i=0; i< options.length(); i++){
auto option = options[i];
if(copyNameToGenericNameOption.names().contains(option)){
reg["displayName"] = name;
continue;
}
if(removeKeyOption.names().contains(option)){
reg.remove(parser.values(removeKeyOption)[removeIndex]);
removeIndex++;
continue;
}
if(setGenericNameOption.names().contains(option)){
reg["displayname"] = parser.values(setGenericNameOption)[genericNameIndex];
genericNameIndex++;
continue;
}
if(setIconOption.names().contains(option)){
reg["icon"] = parser.values(setIconOption)[iconIndex];
iconIndex++;
continue;
}
if(setKeyOption.names().contains(option)){
key = parser.values(setKeyOption)[keyIndex];
reg[key] = parser.values(setValueOption)[keyIndex];
i++;
keyIndex++;
}
}

applyChanges(parser, reg, name);
auto json = toJson(reg, QJsonDocument::Indented);
file.write(json.toUtf8());
file.close();
Expand Down
Loading

0 comments on commit d794ed6

Please sign in to comment.