Outsourced file related operations
This commit is contained in:
13
include/FileService.h
Normal file
13
include/FileService.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef FILE_SERVICES_H
|
||||
#define FILE_SERVICES_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class FileServices {
|
||||
public:
|
||||
static bool doesNonEmptyFileExist(std::string);
|
||||
static void emptyFile(std::string);
|
||||
static void createOrOverwriteFile(std::string, std::string);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -16,6 +16,6 @@ class HyprlandService {
|
||||
static void setClientFloating(Client&);
|
||||
static void setClientTiled(Client&);
|
||||
static void toggleClientFloating(Client&);
|
||||
static std::string exec(const std::string& command);
|
||||
static bool isFloatingRulePresent();
|
||||
};
|
||||
#endif
|
||||
|
||||
22
src/FileService.cpp
Normal file
22
src/FileService.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include "../include/FileService.h"
|
||||
#include "../include/Macros.h"
|
||||
#include "../include/ShellService.h"
|
||||
|
||||
bool FileServices::doesNonEmptyFileExist(std::string path) {
|
||||
std::ifstream file(path);
|
||||
return file.good() && file.peek() != std::ifstream::traits_type::eof();
|
||||
};
|
||||
|
||||
void FileServices::emptyFile(std::string path) {
|
||||
std::stringstream cmd;
|
||||
cmd << CAT_PATH << " " << NULL_PATH << " > " << path;
|
||||
ShellService::exec(cmd.str().c_str());
|
||||
};
|
||||
|
||||
void FileServices::createOrOverwriteFile(std::string path, std::string content) {
|
||||
std::stringstream cmd;
|
||||
cmd << ECHO_PATH << " " << content << " > " << path;
|
||||
ShellService::exec(cmd.str().c_str());
|
||||
};
|
||||
@@ -62,9 +62,13 @@ void HyprlandService::toggleClientFloating(Client& c) {
|
||||
|
||||
void HyprlandService::setFloatingRule(bool b) {
|
||||
if (b) {
|
||||
exec(SET_FLOATING_RULE);
|
||||
FileServices::createOrOverwriteFile(FLOATING_RULE_CONF_FILE, FLOATING_RULE);
|
||||
}
|
||||
else {
|
||||
exec(REMOVE_FLOATING_RULE);
|
||||
FileServices::emptyFile(FLOATING_RULE_CONF_FILE);
|
||||
}
|
||||
};
|
||||
|
||||
bool HyprlandService::isFloatingRulePresent() {
|
||||
return FileServices::doesNonEmptyFileExist(FLOATING_RULE_CONF_FILE);
|
||||
};
|
||||
Reference in New Issue
Block a user