|
| 1 | +#include "AutoStart-FreeBSD.h" |
| 2 | +#include "LogManager.h" |
| 3 | +#include "filesystem.h" |
| 4 | + |
| 5 | +#include <fstream> |
| 6 | +#include <iostream> |
| 7 | +#include <unistd.h> |
| 8 | +#include <sstream> |
| 9 | + |
| 10 | +/*-----------------------------------------------------*\ |
| 11 | +| FreeBSD AutoStart Implementation | |
| 12 | +| Public Methods | |
| 13 | +\*-----------------------------------------------------*/ |
| 14 | + |
| 15 | +AutoStart::AutoStart(std::string name) |
| 16 | +{ |
| 17 | + InitAutoStart(name); |
| 18 | +} |
| 19 | + |
| 20 | +bool AutoStart::DisableAutoStart() |
| 21 | +{ |
| 22 | + std::error_code autostart_file_remove_errcode; |
| 23 | + bool success = false; |
| 24 | + |
| 25 | + /*-------------------------------------------------*\ |
| 26 | + | Check if the filename is valid | |
| 27 | + \*-------------------------------------------------*/ |
| 28 | + if(autostart_file != "") |
| 29 | + { |
| 30 | + /*---------------------------------------------*\ |
| 31 | + | If file doesn't exist, disable is successful | |
| 32 | + \*---------------------------------------------*/ |
| 33 | + if(!filesystem::exists(autostart_file)) |
| 34 | + { |
| 35 | + success = true; |
| 36 | + } |
| 37 | + /*---------------------------------------------*\ |
| 38 | + | Otherwise, delete the file | |
| 39 | + \*---------------------------------------------*/ |
| 40 | + else |
| 41 | + { |
| 42 | + success = filesystem::remove(autostart_file, autostart_file_remove_errcode); |
| 43 | + |
| 44 | + if(!success) |
| 45 | + { |
| 46 | + LOG_ERROR("[AutoStart] An error occurred removing the auto start file."); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + LOG_ERROR("Could not establish correct autostart file path."); |
| 53 | + } |
| 54 | + |
| 55 | + return(success); |
| 56 | +} |
| 57 | + |
| 58 | +bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info) |
| 59 | +{ |
| 60 | + bool success = false; |
| 61 | + |
| 62 | + /*-------------------------------------------------*\ |
| 63 | + | Check if the filename is valid | |
| 64 | + \*-------------------------------------------------*/ |
| 65 | + if(autostart_file != "") |
| 66 | + { |
| 67 | + std::ofstream autostart_file_stream(autostart_file, std::ios::out | std::ios::trunc); |
| 68 | + |
| 69 | + /*---------------------------------------------*\ |
| 70 | + | Error out if the file could not be opened | |
| 71 | + \*---------------------------------------------*/ |
| 72 | + if(!autostart_file_stream) |
| 73 | + { |
| 74 | + LOG_ERROR("Could not open %s for writing.", autostart_file.c_str()); |
| 75 | + success = false; |
| 76 | + } |
| 77 | + /*---------------------------------------------*\ |
| 78 | + | Otherwise, write the file | |
| 79 | + \*---------------------------------------------*/ |
| 80 | + else |
| 81 | + { |
| 82 | + //autostart_file_stream << desktop_file; |
| 83 | + autostart_file_stream.close(); |
| 84 | + success = !autostart_file_stream.fail(); |
| 85 | + |
| 86 | + if (!success) |
| 87 | + { |
| 88 | + LOG_ERROR("An error occurred writing the auto start file."); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + LOG_ERROR("Could not establish correct autostart file path."); |
| 95 | + } |
| 96 | + |
| 97 | + return(success); |
| 98 | +} |
| 99 | + |
| 100 | +bool AutoStart::IsAutoStartEnabled() |
| 101 | +{ |
| 102 | + /*-------------------------------------------------*\ |
| 103 | + | Check if the filename is valid | |
| 104 | + \*-------------------------------------------------*/ |
| 105 | + if(autostart_file != "") |
| 106 | + { |
| 107 | + return(filesystem::exists(autostart_file)); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + return(false); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +std::string AutoStart::GetExePath() |
| 116 | +{ |
| 117 | + /*-------------------------------------------------*\ |
| 118 | + | Create the OpenRGB executable path | |
| 119 | + \*-------------------------------------------------*/ |
| 120 | + char exepath[ PATH_MAX ]; |
| 121 | + |
| 122 | + ssize_t count = readlink("/proc/self/exe", exepath, PATH_MAX); |
| 123 | + |
| 124 | + return(std::string(exepath, (count > 0) ? count : 0)); |
| 125 | +} |
| 126 | + |
| 127 | +void AutoStart::InitAutoStart(std::string name) |
| 128 | +{ |
| 129 | + std::string autostart_dir; |
| 130 | + |
| 131 | + autostart_name = name; |
| 132 | + |
| 133 | + /*-------------------------------------------------*\ |
| 134 | + | Get home and config paths | |
| 135 | + \*-------------------------------------------------*/ |
| 136 | + const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); |
| 137 | + const char *home = getenv("HOME"); |
| 138 | + |
| 139 | + /*-------------------------------------------------*\ |
| 140 | + | Determine where the autostart .desktop files are | |
| 141 | + | kept | |
| 142 | + \*-------------------------------------------------*/ |
| 143 | + if(xdg_config_home != NULL) |
| 144 | + { |
| 145 | + autostart_dir = xdg_config_home; |
| 146 | + autostart_dir = autostart_dir + "/autostart/"; |
| 147 | + } |
| 148 | + else if(home != NULL) |
| 149 | + { |
| 150 | + autostart_dir = home; |
| 151 | + autostart_dir = autostart_dir + "/.config/autostart/"; |
| 152 | + } |
| 153 | + |
| 154 | + /*-------------------------------------------------*\ |
| 155 | + | Check if the filename is valid | |
| 156 | + \*-------------------------------------------------*/ |
| 157 | + if(autostart_dir != "") |
| 158 | + { |
| 159 | + std::error_code ec; |
| 160 | + |
| 161 | + bool success = true; |
| 162 | + |
| 163 | + if(!filesystem::exists(autostart_dir)) |
| 164 | + { |
| 165 | + success = filesystem::create_directories(autostart_dir, ec); |
| 166 | + } |
| 167 | + |
| 168 | + if(success) |
| 169 | + { |
| 170 | + autostart_file = autostart_dir + autostart_name + ".desktop"; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
0 commit comments