forked from acmpesuecc/Night-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
software_manager.c
190 lines (155 loc) · 8.17 KB
/
software_manager.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <gtk/gtk.h>
#include <vte/vte.h>
#include <stdlib.h>
#include <stdio.h>
GtkWidget *package_list;
GtkWidget *details_label;
GtkWidget *terminal;
GtkWidget *toggle_terminal_button;
gboolean terminal_visible = FALSE;
void on_package_selected(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
void on_install_clicked(GtkWidget *widget, gpointer data);
void on_uninstall_clicked(GtkWidget *widget, gpointer data);
void on_toggle_terminal_clicked(GtkWidget *widget, gpointer data);
void populate_package_list(GtkListStore *list_store);
void fetch_available_packages(GtkListStore *list_store) {
FILE *fp;
char output[1024];
fp = popen("pacman -Slq", "r");
if (fp == NULL) {
perror("Failed to run pacman command");
return;
}
GtkTreeIter iter;
while (fgets(output, sizeof(output), fp) != NULL) {
output[strcspn(output, "\n")] = 0; // Remove newline
gtk_list_store_append(list_store, &iter);
gtk_list_store_set(list_store, &iter, 0, output, -1);
}
pclose(fp);
}
void on_package_selected(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data) {
GtkTreeModel *model;
GtkTreeIter iter;
char *selected_package;
model = gtk_tree_view_get_model(tree_view);
if (gtk_tree_model_get_iter(model, &iter, path)) {
gtk_tree_model_get(model, &iter, 0, &selected_package, -1);
gchar *details = g_strdup_printf("Package: %s\nVersion: 1.0.0\nSize: 10MB", selected_package);
gtk_label_set_text(GTK_LABEL(details_label), details);
g_free(details);
g_free(selected_package);
}
}
// // Install button callback
// void on_install_clicked(GtkWidget *widget, gpointer data) {
// g_print("Installing package...\n");
// const char *command = "echo 'Installing package...' && sudo pacman -S --noconfirm <package-name>\n";
// vte_terminal_feed_child(VTE_TERMINAL(terminal), command, -1);
// }
// // Uninstall button callback
// void on_uninstall_clicked(GtkWidget *widget, gpointer data) {
// g_print("Uninstalling package...\n");
// const char *command = "echo 'Uninstalling package...' && sudo pacman -R --noconfirm <package-name>\n";
// vte_terminal_feed_child(VTE_TERMINAL(terminal), command, -1);
// }
void on_install_clicked(GtkWidget *widget, gpointer data) {
g_print("Installing package...\n");
// The command that will be executed when the package is installed
const char *command[] = { "/bin/sh", "-c", "echo 'Installing package...' && sudo pacman -S --noconfirm <package-name>", NULL };
// Spawn the command in the embedded terminal
vte_terminal_spawn_async(
VTE_TERMINAL(terminal), // The VTE terminal widget
VTE_PTY_DEFAULT, // Use the default pseudo-terminal
NULL, // Working directory (NULL uses current directory)
(char **)command, // Command to execute
NULL, // Environment (NULL uses current environment)
G_SPAWN_DEFAULT, // Spawn flags
NULL, // Child setup function (not needed here)
NULL, // User data for the setup function
NULL, // Cancellable object (not needed)
-1, // Timeout for the operation (-1 means no timeout)
NULL, // GCancellable (not needed)
NULL, // Callback for process completion (not needed here)
NULL // User data for the callback
);
}
void on_uninstall_clicked(GtkWidget *widget, gpointer data) {
g_print("Uninstalling package...\n");
// Prepare the command to be executed in the terminal
const char *command[] = { "/bin/sh", "-c", "echo 'Uninstalling package...' && sudo pacman -R --noconfirm <package-name>", NULL };
// Spawn the command in the embedded terminal
vte_terminal_spawn_async(
VTE_TERMINAL(terminal), // The VTE terminal widget
VTE_PTY_DEFAULT, // Use the default pseudo-terminal
NULL, // Working directory (NULL uses current directory)
(char **)command, // Command to execute
NULL, // Environment (NULL uses current environment)
G_SPAWN_DEFAULT, // Spawn flags
NULL, // Child setup function (not needed here)
NULL, // User data for the setup function (unnecessary)
NULL, // Cancellable object (not needed)
-1, // Timeout for the operation (-1 means no timeout)
NULL, // GCancellable (not needed)
NULL, // Callback for process completion (not needed here)
NULL // User data for the callback
);
}
// Toggle terminal visibility
void on_toggle_terminal_clicked(GtkWidget *widget, gpointer data) {
terminal_visible = !terminal_visible;
if (terminal_visible) {
gtk_widget_show(terminal);
gtk_button_set_label(GTK_BUTTON(toggle_terminal_button), "Hide Terminal");
} else {
gtk_widget_hide(terminal);
gtk_button_set_label(GTK_BUTTON(toggle_terminal_button), "Show Terminal");
}
}
int main(int argc, char *argv[]) {
GtkWidget *window, *vbox, *scrolled_window, *button_box, *install_button, *uninstall_button;
GtkListStore *list_store;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Software Manager");
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(window), vbox);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
// Package list
list_store = gtk_list_store_new(1, G_TYPE_STRING);
fetch_available_packages(list_store); /
package_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store));
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Available Packages", renderer, "text", 0, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(package_list), column);
gtk_container_add(GTK_CONTAINER(scrolled_window), package_list);
g_signal_connect(package_list, "row-activated", G_CALLBACK(on_package_selected), NULL);
details_label = gtk_label_new("Select a package to view details.");
gtk_box_pack_start(GTK_BOX(vbox), details_label, FALSE, FALSE, 0);
button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 0);
install_button = gtk_button_new_with_label("Install");
g_signal_connect(install_button, "clicked", G_CALLBACK(on_install_clicked), NULL);
gtk_container_add(GTK_CONTAINER(button_box), install_button);
uninstall_button = gtk_button_new_with_label("Uninstall");
g_signal_connect(uninstall_button, "clicked", G_CALLBACK(on_uninstall_clicked), NULL);
gtk_container_add(GTK_CONTAINER(button_box), uninstall_button);
// widget for terminal
terminal = vte_terminal_new();
vte_terminal_spawn_async(VTE_TERMINAL(terminal), VTE_PTY_DEFAULT, NULL, NULL, NULL, 0, NULL, NULL, NULL, -1, NULL, NULL, NULL);
gtk_box_pack_start(GTK_BOX(vbox), terminal, TRUE, TRUE, 0);
gtk_widget_hide(terminal); // by default this is hidden
// allowing users to toggle terminal based on tastes
toggle_terminal_button = gtk_button_new_with_label("Show Terminal");
g_signal_connect(toggle_terminal_button, "clicked", G_CALLBACK(on_toggle_terminal_clicked), NULL);
gtk_box_pack_start(GTK_BOX(vbox), toggle_terminal_button, FALSE, FALSE, 0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}