Skip to content

Commit

Permalink
fix #44
Browse files Browse the repository at this point in the history
- the function for check for dark.txt
- a logic that remove dark.txt with switching the theme
  • Loading branch information
NachtsternBuild authored Sep 12, 2024
1 parent c736752 commit 7c0d372
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions Anwendung/header/header_dark_theme.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
*-------------------------------------------*
* Projekt 122 *
*-------------------------------------------*
* Apache License, Version 2.0 *
*-------------------------------------------*
* *
* Programm um das installieren von *
* Custom-ROM und GSIs auf Android-Geräte *
* zu erleichtern *
* *
*-------------------------------------------*
* (C) Copyright 2024 Elias Mörz *
*-------------------------------------------*
* *
* Headerpart - dark_theme *
* *
*-------------------------------------------*
*/

/* headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "program_functions.h"

#define CONFIG_DIR "~/Downloads/ROM-Install/config"
#define CONFIG_FILE "dark.txt"

const char *current_theme = "light";

// function that create the dir
// thanks to my book for programming for linux
void create_directory_if_not_exists(const char *path)
{
char expanded_path[512];
snprintf(expanded_path, sizeof(expanded_path), "%s", path);
char *home = getenv("HOME");
if (home != NULL)
{
// use the full path
char *tilde_pos = strchr(expanded_path, '~');
if (tilde_pos != NULL)
{
memmove(expanded_path + strlen(home), tilde_pos + 1, strlen(tilde_pos));
memcpy(expanded_path, home, strlen(home));
}
}

struct stat st = {0};
if (stat(expanded_path, &st) == -1)
{
if (mkdir(expanded_path, 0700) == -1)
{
perror("Fehler beim Erstellen des Verzeichnisses");
exit(EXIT_FAILURE);
}
}
}

// function that write the dark.txt
// thanks to my book for programming for linux
void write_dark_file()
{
// use the full path
char *home = getenv("HOME");
if (home == NULL)
{
perror("Fehler beim Abrufen des Home-Verzeichnisses");
exit(EXIT_FAILURE);
}

char dir_path[512];
snprintf(dir_path, sizeof(dir_path), "%s/Downloads/ROM-Install/config", home);

// create the dir
create_directory_if_not_exists(dir_path);

char path[512];
snprintf(path, sizeof(path), "%s/%s", dir_path, CONFIG_FILE);

FILE *file = fopen(path, "w");
if (file == NULL)
{
perror("Fehler beim Öffnen der Datei zum Schreiben");
exit(EXIT_FAILURE);
}
fprintf(file, "dunkel");
fclose(file);
g_print("In die Datei '%s' geschrieben.\n", path);
}

// thanks to my book for programming for linux
// function that delete the dark.txt
void delete_dark_file()
{
// use the full path
char *home = getenv("HOME");
if (home == NULL)
{
perror("Fehler beim Abrufen des Home-Verzeichnisses");
exit(EXIT_FAILURE);
}

char dir_path[512];
snprintf(dir_path, sizeof(dir_path), "%s/Downloads/ROM-Install/config", home);

char path[512];
snprintf(path, sizeof(path), "%s/%s", dir_path, CONFIG_FILE);

// try to remove the file
if (remove(path) == 0)
{
g_print("Datei '%s' erfolgreich gelöscht.\n", path);
}
else
{
perror("Fehler beim Löschen der Datei");
}
}

// function that check if there are the dark.txt
void check_dark_file()
{
// use the full path
char *home = getenv("HOME");
if (home == NULL)
{
perror("Fehler beim Abrufen des Home-Verzeichnisses");
exit(EXIT_FAILURE);
}

char dir_path[512];
snprintf(dir_path, sizeof(dir_path), "%s/Downloads/ROM-Install/config", home);

char path[512];
snprintf(path, sizeof(path), "%s/%s", dir_path, CONFIG_FILE);

FILE *file = fopen(path, "r");
if (file != NULL)
{
g_print("Dunkelheit\n");
current_theme = "dark";
fclose(file);
}

else
{
g_print("Tag\n");
}
}

void check_dark_file_light()
{
// use the full path
char *home = getenv("HOME");
if (home == NULL)
{
perror("Fehler beim Abrufen des Home-Verzeichnisses");
exit(EXIT_FAILURE);
}

char dir_path[512];
snprintf(dir_path, sizeof(dir_path), "%s/Downloads/ROM-Install/config", home);

char path[512];
snprintf(path, sizeof(path), "%s/%s", dir_path, CONFIG_FILE);

FILE *file = fopen(path, "r");
if (file != NULL)
{
g_print("Tag\n");
current_theme = "light";
delete_dark_file();
fclose(file);
}

else
{
g_print("Dunkelheit\n");
}
}

0 comments on commit 7c0d372

Please sign in to comment.