Skip to content

Commit d895b16

Browse files
committed
Custom layouts, disconnect client by hand
1 parent 36b95b2 commit d895b16

File tree

7 files changed

+131
-154
lines changed

7 files changed

+131
-154
lines changed

Server/include/Config.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,42 @@ class profile {
5050

5151
class id {
5252
public:
53-
int current_profile = 0;
54-
bool locked = false;
5553
string ID;
54+
string nickname;
5655
string config_file;
56+
sockinfo sock;
5757
json config;
5858
vector<profile> profiles;
59+
int current_profile = 0;
60+
bool locked = false;
5961
id() {}
6062
id(string in_ID) : ID(in_ID) {};
6163
id(const id& ID) {
6264
this->ID = ID.ID;
65+
this->current_profile = ID.current_profile;
66+
this->nickname = ID.nickname;
67+
this->sock = ID.sock;
6368
this->config_file = ID.config_file;
69+
this->config = ID.config;
6470
this->profiles = ID.profiles;
6571
}
6672
id(const id* ID) {
6773
this->ID = ID->ID;
74+
this->current_profile = ID->current_profile;
75+
this->nickname = ID->nickname;
76+
this->sock = ID->sock;
6877
this->config_file = ID->config_file;
78+
this->config = ID->config;
6979
this->profiles = ID->profiles;
7080
}
71-
bool operator==(const id& other) const {
72-
return this->ID == other.ID;
73-
}
74-
id operator=(const id& other) {
75-
this->config_file = other.config_file;
76-
this->profiles = other.profiles;
77-
return this;
78-
}
7981
};
8082

81-
extern vector<id> ids;
83+
extern unordered_map<string, id> ids;
8284

8385
extern bool ids_locked;
86+
extern bool button_cleared;
8487

8588
extern void clear_button(int profile, int page, int button);
86-
extern void configure_id(id& ID);
87-
extern void reconfigure(id& ID);
89+
extern int configure_id(id& ID);
90+
extern int reconfigure(id& ID);
8891
extern void write_config(vector<string> args, size_t arg_size);

Server/include/GUI.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ extern bool should_draw_button_properties;
5252
extern bool clear_dialog_shown;
5353

5454
extern int button_properties_to_draw;
55-
extern int selected_id;
5655

57-
extern std::string selected_id_id;
56+
extern std::string selected_id;
5857

5958
extern int gui_init();
6059
extern int tray_init();

Server/include/Header.h

-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,3 @@ using std::unordered_map;
3030
using std::to_string;
3131

3232
extern bool done;
33-
extern bool button_cleared;
34-
35-
extern int selected_id;
36-
extern int connected_devices;

Server/include/Server.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct sockinfo {
2121
struct addrinfo* result = NULL;
2222
};
2323

24-
extern unordered_map<string, sockinfo> sock_map;
24+
extern int connected_devices;
2525

2626
extern int server_init();
2727

@@ -33,5 +33,5 @@ extern int accept_socket(sockinfo& sock, char* idbuf, bool& is_valid);
3333
extern bool is_id(string message);
3434

3535
extern void begin_accept_cycle(sockinfo& sock);
36-
extern void comm();
36+
extern void comm(string ID);
3737
extern void parse_message(string& message, id& ID);

Server/src/Config.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "../include/Header.h"
22
#include "../include/Config.h"
3+
#include "../include/GUI.h"
34

45
bool button_cleared = false;
56
bool ids_locked = false;
67

7-
vector<id> ids = {};
8+
unordered_map<string, id> ids = {};
89

910
void read_config(json& config, id& ID) {
11+
if (config[config.begin().key()].contains("nickname")) ID.nickname = config[config.begin().key()]["nickname"].get<string>();
1012
if (config[config.begin().key()].contains("profiles")) {
1113
ID.profiles.clear();
1214
int profile_count = 0;
@@ -70,7 +72,7 @@ void read_config(json& config, id& ID) {
7072
}
7173
}
7274

73-
void configure_id(id& ID) {
75+
int configure_id(id& ID) {
7476
ID.config_file = getenv("USERPROFILE");
7577
ID.config_file += "\\AppData\\Roaming\\NexusShell\\";
7678
if (!exists(ID.config_file)) create_directory(ID.config_file);
@@ -82,20 +84,16 @@ void configure_id(id& ID) {
8284
if (reader.peek() != ifstream::traits_type::eof()) {
8385
try {
8486
json config = json::parse(reader);
85-
cerr << "here\n";
8687
reader.close();
8788
read_config(config, ID);
8889
reconfigure(ID);
89-
cerr << "configured\n";
90-
return;
90+
cerr << "Configured " << ID.nickname << "\n";
91+
return 0;
9192
} catch (...) {
92-
cerr << "invalid config!\nclearing...\n"; // TODO: Don't clear invalid configs
93-
//string to_write = "{\"" + ID.ID + "\": {}}";
94-
//json empty_config = json::parse(to_write);
95-
//ofstream writer(ID.config_file);
96-
//writer << empty_config.dump(4);
97-
//writer.close();
98-
//reconfigure(ID);
93+
cerr << "invalid config!\n";
94+
send(ID.sock.ClientSocket, "killreason: invalid config", strlen("killreason: invalid config") + 1, 0);
95+
closesocket(ID.sock.ClientSocket);
96+
return 1;
9997
}
10098
}
10199
reader.close();
@@ -112,19 +110,22 @@ void configure_id(id& ID) {
112110
reconfigure(ID);
113111
}
114112

115-
void reconfigure(id& ID) {
113+
int reconfigure(id& ID) {
116114
if (exists(ID.config_file)) {
117115
ifstream reader(ID.config_file);
118116
json config;
119117
try {
120118
config = json::parse(reader);
121119
} catch (...) {
122120
cerr << "Invalid Config\n";
121+
send(ID.sock.ClientSocket, "killreason: invalid config", strlen("killreason: invalid config") + 1, 0);
122+
closesocket(ID.sock.ClientSocket);
123123
reader.close();
124-
return; // TODO: Change all functions that oculd fail to int and return 1
124+
return 1; // TODO: Change all functions that oculd fail to int and return 1
125125
}
126126
reader.close();
127-
ID.config = config;
127+
ID.config = config; // TODO: use instead;
128+
if (config[config.begin().key()].contains("nickname")) config[config.begin().key()].erase("nickname");
128129
for (int i = 0; i < ID.profiles.size(); ++i) {
129130
json& profile_store = config[config.begin().key()]["profiles"][to_string(i)];
130131
if (profile_store.contains("pages")) {
@@ -149,8 +150,7 @@ void reconfigure(id& ID) {
149150
}
150151
}
151152
string configuration = "cfg" + config.dump();
152-
sock_map[ID.ID].iSendResult = send(sock_map[ID.ID].ClientSocket, configuration.c_str(), configuration.length(), 0);
153-
cerr << "here: " << sock_map[ID.ID].iSendResult << "\n";
153+
ID.sock.iSendResult = send(ID.sock.ClientSocket, configuration.c_str(), configuration.length(), 0);
154154
}
155155
}
156156

@@ -162,14 +162,14 @@ void clear_button(int profile, int page, int button) {
162162
return;
163163
}
164164
nxsh_config += "\\";
165-
nxsh_config += CURRENT_ID.ID;
165+
nxsh_config += selected_id;
166166
nxsh_config += ".json";
167167
json to_remove;
168168
ifstream reader(nxsh_config);
169169
if (reader && reader.peek() != ifstream::traits_type::eof()) to_remove = json::parse(reader); // FIXME: no if (reader
170170
reader.close();
171-
if (to_remove[CURRENT_ID.ID]["profiles"][to_string(profile)]["pages"][to_string(page)]["buttons"].contains(to_string(button))) {
172-
to_remove[CURRENT_ID.ID]["profiles"][to_string(profile)]["pages"][to_string(page)]["buttons"].erase(to_string(button));
171+
if (to_remove[selected_id]["profiles"][to_string(profile)]["pages"][to_string(page)]["buttons"].contains(to_string(button))) {
172+
to_remove[selected_id]["profiles"][to_string(profile)]["pages"][to_string(page)]["buttons"].erase(to_string(button));
173173
ofstream writer(nxsh_config);
174174
writer << to_remove.dump(4);
175175
writer.close();

0 commit comments

Comments
 (0)