-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (92 loc) · 2.98 KB
/
Copy pathmain.cpp
File metadata and controls
106 lines (92 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "modules/headers/swifty.h"
#include <QApplication>
#include <QStringList>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
namespace fs = std::filesystem;
const char *homeEnv = std::getenv("HOME");
// Global variables defined in main.cpp
std::string BACKEND = "swww";
std::string DIRECTORY = "~/Pictures/Wallpapers/";
QStringList ARGS;
// Helper to clean up whitespace
std::string trim(const std::string &str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos)
return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, (last - first + 1));
}
void parseConfig(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open())
return;
std::string line;
while (std::getline(file, line)) {
line = trim(line);
if (line.empty() || line[0] == '#')
continue;
size_t sep = line.find('=');
if (sep != std::string::npos) {
std::string key = trim(line.substr(0, sep));
std::string value = trim(line.substr(sep + 1));
// 1. Handle Standard Keys (backend, dir)
if (key == "backend") {
// Strip quotes: "swww" -> swww
if (value.size() >= 2 && value.front() == '"' && value.back() == '"')
BACKEND = value.substr(1, value.size() - 2);
} else if (key == "dir") {
if (value.size() >= 2 && value.front() == '"' && value.back() == '"'){
DIRECTORY = value.substr(1, value.size() - 2);
if (DIRECTORY.at(0) == '~') {
DIRECTORY.replace(0, 1, std::string(homeEnv));
}
}
}
// 2. Handle Special Arguments Key (using <<)
else if (key == "arguments") {
ARGS.clear();
QString raw = QString::fromStdString(value);
// Split by << delimiter
QStringList parts = raw.split("<<", Qt::SkipEmptyParts);
for (QString part : parts) {
QString clean = part.trimmed();
// Remove quotes from each token: "img" -> img
if (clean.startsWith('"') && clean.endsWith('"')) {
clean = clean.mid(1, clean.length() - 2);
}
ARGS << clean;
}
}
}
}
}
int main(int argc, char *argv[]) {
if (!homeEnv)
return 1; // Safety check
fs::path configDir = fs::path(homeEnv) / ".config/swifty";
fs::path filePath = configDir / "swifty.conf";
// Create directory and default file if they don't exist
if (!fs::exists(configDir)) {
fs::create_directories(configDir);
}
if (!fs::exists(filePath)) {
std::ofstream file(filePath);
if (file.is_open()) {
file << "backend = \"swww\"\n"
<< "dir = \"~/Pictures/Wallpapers/\"\n"
<< "arguments = \"img\" << \"WALLPATH\" << \"--transition-type\" << "
"\"wipe\" << \"--transition-duration\" << \"1.5\"\n";
file.close();
}
}
parseConfig(filePath.string());
QApplication app(argc, argv);
Swifty w;
w.show();
return app.exec();
}