-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportexport.h
173 lines (149 loc) · 4.2 KB
/
importexport.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
char *on_select_file(const char *filetype, const char *title, int save_dialog)
{
GtkWidget *dialog;
GtkFileChooserAction action = save_dialog ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;
dialog = gtk_file_chooser_dialog_new(title,
NULL,
action,
"_Cancel",
GTK_RESPONSE_CANCEL,
save_dialog ? "_Save" : "_Open",
GTK_RESPONSE_ACCEPT,
NULL);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
window_set_icon(GTK_WINDOW(dialog), program_icon);
GtkFileFilter *filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(filter, filetype);
gtk_file_filter_set_name(filter, "SysLinux Customizer Entry (*.sce)");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
res = gtk_dialog_run(GTK_DIALOG(dialog));
char *filename = NULL;
if (res == GTK_RESPONSE_ACCEPT)
{
GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
if (save_dialog)
{
filename = gtk_file_chooser_get_filename(chooser);
}
else
{
filename = g_strdup(gtk_file_chooser_get_filename(chooser));
}
}
gtk_widget_destroy(dialog);
return filename;
}
void on_importentry(void)
{
char *filename = on_select_file("*.sce", "Select a SysLinux Customizer Entry (.sce)", 0);
if (!filename)
{
g_printerr("\033[1;31mERROR\033[0m: Selected file is NULL\n");
reset(window, 1);
return;
}
FILE *sourceFile = fopen(filename, "r");
g_free(filename);
if (!sourceFile)
{
g_printerr("\033[1;31mERROR\033[0m: Unable to open selected file for reading\n");
return;
}
FILE *destinationFile = fopen(syslinuxcfg, "a");
if (!destinationFile)
{
fclose(sourceFile);
g_printerr("\033[1;31mERROR\033[0m: Unable to open destination file for appending\n");
return;
}
char buffer[1024];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0)
{
fwrite(buffer, 1, bytesRead, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
}
void on_exportentry(void)
{
GtkWidget *focused_widget = gtk_window_get_focus(GTK_WINDOW(window));
if (focused_widget != NULL)
{
char *filename = on_select_file("*.sce", "Select a SysLinux Customizer Entry (.sce)", 1);
if (!filename)
{
g_printerr("\033[1;31mERROR\033[0m: Selected file is NULL\n");
reset(window, 1);
return;
}
syslinuxcfg = filename;
const gchar *widget_name = gtk_widget_get_name(focused_widget);
exportcontext = atoi(widget_name);
saveconfig(1);
reset(window, 1);
}
else {}
}
void on_importexport(GtkMenuItem *menu_item, gpointer user_data)
{
int exporting = GPOINTER_TO_INT(user_data);
g_print("%d", exporting);
char *filename = on_select_file("*.cfg", "Select a SysLinux Config file (.cfg)", exporting);
if (!filename)
{
g_printerr("\033[1;31mERROR\033[0m: Selected file is NULL\n");
reset(window, 1);
return;
}
if (exporting == 1)
{
FILE *sourceFile = fopen(syslinuxcfg, "r");
FILE *destinationFile = fopen(filename, "w");
if (sourceFile == NULL || destinationFile == NULL)
{
printf("Error opening files.\n");
return;
}
char buffer[MAX_ENTRIES];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0)
{
fwrite(buffer, 1, bytesRead, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
printf("Data copied from %s to %s.\n", syslinuxcfg, filename);
}
else if (exporting == 0)
{
GtkWidget *warning = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_MODAL,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK_CANCEL,
"Are you sure you want to proceed with data copying? The current configuration will be overwritten.");
gint response = gtk_dialog_run(GTK_DIALOG(warning));
if (response == GTK_RESPONSE_OK)
{
FILE *sourceFile = fopen(filename, "r");
FILE *destinationFile = fopen(syslinuxcfg, "w");
if (sourceFile == NULL || destinationFile == NULL)
{
printf("Error opening files.\n");
return;
}
char buffer[MAX_ENTRIES];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0)
{
fwrite(buffer, 1, bytesRead, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
printf("Data copied from %s to %s.\n", filename, syslinuxcfg);
reset(window, 1);
}
gtk_widget_destroy(warning);
}
}