Adding a GUI to save your packages

This commit is contained in:
Arkagedon 2022-10-26 19:09:26 +00:00
parent 817623fcba
commit a7b6b29a5d
10 changed files with 494 additions and 1 deletions

45
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
"configurations": [
{
"name": "C Linux GTK3",
"includePath": [
"${workspaceFolder}/**",
"/usr/lib/glib-2.0/include/",
"/usr/include/gtk-3.0",
"/usr/include/pango-1.0",
"/usr/include/glib-2.0",
"/usr/include/harfbuzz",
"/usr/include/fribidi",
"/usr/include/libpng16",
"/usr/include/cairo",
"/usr/include/pixman-1",
"/usr/include/gdk-pixbuf-2.0",
"/usr/include/libmount",
"/usr/include/blkid",
"/usr/include/gio-unix-2.0",
"/usr/include/atk-1.0",
"/usr/include/at-spi2-atk/2.0",
"/usr/include/at-spi-2.0",
"/usr/include/dbus-1.0",
"/usr/lib/dbus-1.0/include",
"/usr/local/include",
"/usr/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compilerArgs": [
"-Wall",
"-Wextra",
"-mtune=native",
"-no-pie",
"`pkg-config gtk+-3.0 --cflags`",
"`pkg-config --libs gtk+-3.0`",
"-lm"
]
}
],
"version": 4
}

12
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
"makefile.launchConfigurations": [
{
"cwd": "/home/arka/Documents/Dev/C/Projets/PackageSaver",
"binaryPath": "/home/arka/Documents/Dev/C/Projets/PackageSaver/PackageSaver",
"binaryArgs": []
}
],
"files.associations": {
"random": "c"
}
}

84
Makefile Normal file
View File

@ -0,0 +1,84 @@
NAME := PackageSaver
#------------------------------------------------#
# INGREDIENTS #
#------------------------------------------------#
#
# INCS header file locations
#
# SRC_DIR source directory
# SRCS source files
#
# BUILD_DIR build directory
# OBJS object files
# DEPS dependency files
#
# CC compiler
# CFLAGS compiler flags
# CPPFLAGS preprocessor flags
# LDFLAGS linker flags
# LDLIBS libraries name
INCS := include
SRC_DIR := src
SRCS := PkgSaver.c main.c
SRCS := $(SRCS:%=$(SRC_DIR)/%)
BUILD_DIR := .build
OBJS := $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
CC := gcc
CFLAGS := -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic
CPPFLAGS := $(addprefix -I,$(INCS))
#------------------------------------------------#
# UTENSILS #
#------------------------------------------------#
# RM force remove
# MAKE quietly make
# DIR_DUP duplicate directory tree
RM := rm -f
MAKE := $(MAKE) --silent --no-print-directory
DIR_DUP = mkdir -p $(@D)
#------------------------------------------------#
# RECIPES #
#------------------------------------------------#
# all default goal
# $(NAME) link .o -> archive
# $(LIBS) build libraries
# %.o compilation .c -> .o
# clean remove .o
# fclean remove .o + binary
# re remake default goal
# run run the program
# info print the default goal recipe
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $(NAME) $(CFLAGS)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
$(DIR_DUP)
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
clean:
$(RM) $(OBJS)
fclean: clean
$(RM) $(NAME)
re:
$(MAKE) fclean
$(MAKE) all
#------------------------------------------------#
# SPEC #
#------------------------------------------------#
.PHONY: clean fclean re
.SILENT:

BIN
PackageSaver Executable file

Binary file not shown.

17
include/PkgSaver.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef DEF_PKGSAVER_H
#define DEF_PKGSAVER_H
#include <gtk/gtk.h>
typedef struct _treeViewer treeViewer;
struct _treeViewer
{
GtkTreeView *treeView;
GtkTreeStore *treeStore;
};
void init_packages(treeViewer *tv);
void on_ct2_toggled (GtkCellRendererToggle *cell, gchar *path_string, gpointer user_data);
void on_destroy();
#endif

View File

@ -82,7 +82,7 @@ do
;;
#Pacman is the package manager
pacman) currPkg=$(pacman -Q | sed -n "${pkgI}p" | cut -d" " -f1)
pacman) currPkg=$(pacman -Q | sed -n "${pkgI}p" | cut -d " " -f1)
;;
esac

90
src/PkgSaver.c Normal file
View File

@ -0,0 +1,90 @@
#include "PkgSaver.h"
/**
* @brief Function to init the TreeStore content with the packages names
*
* @param tv TreeViewer which needs to be filled
*/
void init_packages(treeViewer *tv)
{
FILE *fp;
char buffer[1035];
GtkTreeIter iter;
/* Getting all the packages */
fp = popen("pacman -Q | cut -d ' ' -f1", "r");
if (fp == NULL) {
fprintf(stderr, "Failed to get packages, please open an issue on github\n" );
exit(1);
}
/* Read the packages, and adding them into the treeStore*/
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
gtk_tree_store_append (tv->treeStore, &iter, NULL);
gtk_tree_store_set(tv->treeStore, &iter, 0, buffer, -1);
gtk_tree_store_set(tv->treeStore, &iter, 1, FALSE, -1);
}
/* Closing the file */
pclose(fp);
}
/**
* @brief Function to get the value of the selected rows
*
* @param selection Selected row
*/
void on_select_changed(GtkTreeSelection *selection)
{
gchar *value;
gboolean box;
GtkTreeIter iter;
GtkTreeModel *model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), &model, &iter) == FALSE)
return;
/* Getting package column content */
gtk_tree_model_get(model, &iter, 0, &value, -1);
printf("Select signal received: package = \"%s\"; ", value);
/* Getting keeping ? column content */
gtk_tree_model_get(model, &iter, 1, &box, -1); // get column 1
printf("keeping ? = \"%d\"\n", box);
}
/**
* @brief Function to change the toggling of a row
*
* @param cell The cell rendered (useless here)
* @param path_string The path of the row we want
* @param user_data TreeViewer which needs to be changed
*/
void on_ct2_toggled (GtkCellRendererToggle *cell, gchar *path_string, gpointer user_data)
{
GtkTreeIter iter;
GtkTreeModel *model;
gboolean isKept = FALSE;
gchar *packageName;
treeViewer *tv = (treeViewer*) user_data;
model = gtk_tree_view_get_model(tv->treeView); // get the tree model
gtk_tree_model_get_iter_from_string (model, &iter, path_string); // get iter from path
gtk_tree_model_get(model, &iter, 0, &packageName, -1); //Getting the name of the package
gtk_tree_model_get(model, &iter, 1, &isKept, -1); // Getting the value of the boolean
if (isKept == FALSE) isKept = TRUE; else isKept = FALSE;
gtk_tree_store_set(tv->treeStore, &iter, 1, isKept, -1); //Changing the checkbox toggling
return;
}
void on_destroy()
{
gtk_main_quit();
}

44
src/main.c Normal file
View File

@ -0,0 +1,44 @@
#include "PkgSaver.h"
int main(int argc, char *argv[])
{
/*Initialisation of the window*/
gtk_init(&argc, &argv);
GtkBuilder *builder = gtk_builder_new_from_file("src/packageSaver.glade");
GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
gtk_builder_connect_signals(builder, NULL);
/*Initialisation of the component of the window*/
treeViewer tv;
tv.treeView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "tv1"));
tv.treeStore = GTK_TREE_STORE(gtk_builder_get_object(builder, "TreeStore"));
GtkTreeViewColumn *cx1 = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "cx1"));
GtkTreeViewColumn *cx2 = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "cx2"));
GtkCellRenderer *ct1 = GTK_CELL_RENDERER(gtk_builder_get_object(builder, "ct1"));
GtkCellRenderer *ct2 = GTK_CELL_RENDERER(gtk_builder_get_object(builder, "ct2"));
GtkTreeSelection *select = GTK_TREE_SELECTION(gtk_builder_get_object(builder, "select"));
gtk_tree_view_column_add_attribute(cx1, ct1, "text", 0); // attach renderer to column
gtk_tree_view_column_add_attribute(cx2, ct2, "active", 1); // attach renderer to column
select = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.treeView));
/*Init the content of the treestore (all the name of packages)*/
init_packages(&tv);
/*Connection all the signal*/
g_signal_connect (ct2, "toggled", G_CALLBACK (on_ct2_toggled), &tv);
gtk_widget_show_all(window);
gtk_main();
return EXIT_SUCCESS;
}
// TODO: free all when exiting
// saving the actions into a file ou clipboard
// adding a research by name feature

102
src/packageSaver.glade Normal file
View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkTreeStore" id="TreeStore">
<columns>
<!-- column-name Texte -->
<column type="gchararray"/>
<!-- column-name Bool -->
<column type="gboolean"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="resizable">False</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkScrolledWindow" id="scroll">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="min-content-width">500</property>
<property name="min-content-height">200</property>
<child>
<object class="GtkTreeView" id="tv1">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">TreeStore</property>
<property name="enable-grid-lines">horizontal</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="select">
<signal name="changed" handler="on_select_changed" swapped="no"/>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="cx1">
<property name="sizing">autosize</property>
<property name="min-width">100</property>
<property name="title" translatable="yes" context="Label of the package column">Package name</property>
<property name="expand">True</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="ct1"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="cx2">
<property name="min-width">100</property>
<property name="title" translatable="yes" context="Label of column keeping the program ?">Keeping ?</property>
<property name="expand">True</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererToggle" id="ct2"/>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkButton" id="SaveInFile">
<property name="label" translatable="yes">Save in a file</property>
<property name="width-request">250</property>
<property name="height-request">25</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<signal name="clicked" handler="on_SaveInFile_clicked" swapped="no"/>
</object>
<packing>
<property name="y">200</property>
</packing>
</child>
<child>
<object class="GtkButton" id="SaveInClip">
<property name="label" translatable="yes">Save in clip board</property>
<property name="width-request">250</property>
<property name="height-request">25</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<signal name="clicked" handler="on_SaveInClip_clicked" swapped="no"/>
</object>
<packing>
<property name="x">250</property>
<property name="y">200</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

99
src/packageSaver.glade~ Normal file
View File

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkTreeStore" id="TreeStore">
<columns>
<!-- column-name Texte -->
<column type="gchararray"/>
<!-- column-name Bool -->
<column type="gboolean"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="resizable">False</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkScrolledWindow" id="scroll">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="min-content-width">500</property>
<property name="min-content-height">200</property>
<child>
<object class="GtkTreeView" id="tv1">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">TreeStore</property>
<property name="enable-grid-lines">horizontal</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="select">
<signal name="changed" handler="on_select_changed" swapped="no"/>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="cx1">
<property name="sizing">autosize</property>
<property name="min-width">100</property>
<property name="title" translatable="yes" context="Label of the package column">Package name</property>
<property name="expand">True</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="ct1"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="cx2">
<property name="min-width">100</property>
<property name="title" translatable="yes" context="Label of column keeping the program ?">Keeping ?</property>
<property name="expand">True</property>
<property name="alignment">0.5</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkButton" id="SaveInFile">
<property name="label" translatable="yes">Save in a file</property>
<property name="width-request">250</property>
<property name="height-request">25</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<signal name="clicked" handler="on_SaveInFile_clicked" swapped="no"/>
</object>
<packing>
<property name="y">200</property>
</packing>
</child>
<child>
<object class="GtkButton" id="SaveInClip">
<property name="label" translatable="yes">Save in clip board</property>
<property name="width-request">250</property>
<property name="height-request">25</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<signal name="clicked" handler="on_SaveInClip_clicked" swapped="no"/>
</object>
<packing>
<property name="x">250</property>
<property name="y">200</property>
</packing>
</child>
</object>
</child>
</object>
</interface>