1
0
mirror of https://gitlab.cvh-server.de/skrause/flashcards.git synced 2025-12-12 09:01:37 +01:00

replace deprecated classes, update code, fix errors, add missing stuff

This commit is contained in:
2025-02-05 20:52:07 +01:00
parent 81e353b449
commit b4d8f9d096
31 changed files with 767 additions and 183 deletions

56
src/create-category.c Normal file
View File

@@ -0,0 +1,56 @@
#include <gtk/gtk.h>
#include <adwaita.h>
#include <glib/gi18n.h>
#include "create-category.h"
struct _FlashcardsCreateCategoryDialog
{
AdwAlertDialog parent;
GtkEditable *entry;
FlashcardsAppWindow *win;
};
G_DEFINE_TYPE(FlashcardsCreateCategoryDialog, flashcards_create_category_dialog, ADW_TYPE_ALERT_DIALOG);
static void
flashcards_create_category_dialog_init(FlashcardsCreateCategoryDialog *self)
{
gtk_widget_init_template(GTK_WIDGET(self));
}
static void
on_response(AdwAlertDialog *dialog,
gchar *response,
gpointer user_data)
{
FlashcardsCreateCategoryDialog *self = FLASHCARDS_CREATE_CATEGORY_DIALOG(user_data);
const gchar *category = gtk_editable_get_text(self->entry);
if (strcmp(response, "add") == 0 && strlen(category) > 0)
{
flashcards_app_window_test(self->win, category);
}
}
static void
flashcards_create_category_dialog_class_init(FlashcardsCreateCategoryDialogClass *class)
{
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class), "/li/sopht/flashcards/create-category.ui");
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsCreateCategoryDialog, entry);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_response);
}
FlashcardsCreateCategoryDialog *
flashcards_create_category_dialog_new(FlashcardsAppWindow *win)
{
FlashcardsCreateCategoryDialog *self = g_object_new(FLASHCARDS_CREATE_CATEGORY_DIALOG_TYPE, NULL);
self->win = win;
return self;
}

14
src/create-category.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef __CREATECATEGORYDIALOG_H
#define __CREATECATEGORYDIALOG_H
#include <gtk/gtk.h>
#include <adwaita.h>
#include "flashcardsapp.h"
#include "flashcardsappwin.h"
#define FLASHCARDS_CREATE_CATEGORY_DIALOG_TYPE (flashcards_create_category_dialog_get_type())
G_DECLARE_FINAL_TYPE(FlashcardsCreateCategoryDialog, flashcards_create_category_dialog, FLASHCARDS, CREATE_CATEGORY_DIALOG, AdwAlertDialog)
FlashcardsCreateCategoryDialog *flashcards_create_category_dialog_new(FlashcardsAppWindow *win);
#endif /* __CREATECATEGORYDIALOG_H */

View File

@@ -69,8 +69,29 @@ void database_create_tables()
sqlite3_finalize(stmt);
}
void database_save_category()
void database_save_category(const char *c)
{
int rc;
sqlite3_stmt *stmt;
fprintf(stdout, "%s\n", c);
rc = sqlite3_prepare_v2(db, "INSERT INTO categories (name) VALUES(?)", -1, &stmt, 0);
if (rc == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, c, -1, SQLITE_STATIC);
}
else
{
printf("Failed to execute statement: %s\n", sqlite3_errmsg(db));
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_OK)
{
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
}
GArray *database_load_categories()

View File

@@ -1,6 +1,8 @@
#include <gtk/gtk.h>
#include <adwaita.h>
#include <glib/gi18n.h>
#include "flashcardsapp.h"
#include "flashcardsappwin.h"
@@ -13,9 +15,56 @@ struct _FlashcardsApp
G_DEFINE_TYPE(FlashcardsApp, flashcards_app, ADW_TYPE_APPLICATION);
static void
flashcards_app_quit_action(GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
FlashcardsApp *self = user_data;
g_assert(FLASHCARDS_IS_APP(self));
g_application_quit(G_APPLICATION(self));
}
static void
flashcards_app_about_action(GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
static const char *developers[] = {"Sophie Krause", NULL};
FlashcardsApp *self = user_data;
GtkWindow *window = NULL;
g_assert(FLASHCARDS_IS_APP(self));
window = gtk_application_get_active_window(GTK_APPLICATION(self));
adw_show_about_dialog(GTK_WIDGET(window),
"application-name", _("Flashcards"),
"application-icon", "li.sopht.Flashcards",
"developer-name", "Sophie Krause",
"translator-credits", "Sophie Krause",
"version", "1.0.0",
"developers", developers,
"copyright", "© 2025 Sophie Krause",
"license-type", GTK_LICENSE_MIT_X11,
NULL);
}
static const GActionEntry app_actions[] = {
{"quit", flashcards_app_quit_action},
{"about", flashcards_app_about_action},
};
static void
flashcards_app_init(FlashcardsApp *app)
{
g_action_map_add_action_entries(G_ACTION_MAP(app),
app_actions,
G_N_ELEMENTS(app_actions),
app);
gtk_application_set_accels_for_action(GTK_APPLICATION(app),
"app.quit",
(const char *[]){"<primary>q", NULL});
}
static void
@@ -59,7 +108,7 @@ FlashcardsApp *
flashcards_app_new(void)
{
return g_object_new(FLASHCARDS_APP_TYPE,
"application-id", "li.sopht.flashcards",
"flags", G_APPLICATION_HANDLES_OPEN,
"application-id", "li.sopht.Flashcards",
"flags", G_APPLICATION_DEFAULT_FLAGS,
NULL);
}

View File

@@ -5,6 +5,7 @@
#include "flashcardsapp.h"
#include "flashcardsappwin.h"
#include "create-category.h"
#include "database.h"
@@ -14,25 +15,17 @@ struct _FlashcardsAppWindow
GArray *categories;
AdwLeaflet *leaflet;
GtkButton *leaflet_previous;
AdwNavigationSplitView *split_view;
AdwNavigationPage *sidebar;
AdwNavigationPage *content;
AdwDialog *create_category_dialog;
GtkListBox *topics;
};
G_DEFINE_TYPE(FlashcardsAppWindow, flashcards_app_window, ADW_TYPE_APPLICATION_WINDOW);
static void
show_about (GtkWindow *win)
{
adw_show_about_window (win,
"application-name", _("Flashcards"),
"application-icon", "org.gnome.Builder",
"developer-name", "Tobias Krause",
"version", "1.0",
"license-type", GTK_LICENSE_MIT_X11,
NULL);
}
static void
on_category_selected(GtkListBox *box, GtkListBoxRow *row, gpointer user_data)
{
@@ -42,21 +35,28 @@ on_category_selected(GtkListBox *box, GtkListBoxRow *row, gpointer user_data)
}
printf("Test\n");
FlashcardsAppWindow *win = FLASHCARDS_APP_WINDOW(gtk_widget_get_root(GTK_WIDGET(box)));
adw_leaflet_navigate(win->leaflet, ADW_NAVIGATION_DIRECTION_FORWARD);
adw_navigation_split_view_set_show_content(ADW_NAVIGATION_SPLIT_VIEW(win->split_view), TRUE);
int id = gtk_list_box_row_get_index(gtk_list_box_get_selected_row(win->topics));
printf("%d\n", id);
show_about (GTK_WINDOW(gtk_widget_get_root (GTK_WIDGET (box))));
}
static void
on_navigate_back(GtkButton *button, gpointer user_data)
on_add_category(GtkButton *self,
gpointer user_data)
{
printf("Test2\n");
FlashcardsAppWindow *win = FLASHCARDS_APP_WINDOW(gtk_widget_get_root(GTK_WIDGET(button)));
gtk_list_box_unselect_all(win->topics);
adw_leaflet_navigate(win->leaflet, ADW_NAVIGATION_DIRECTION_BACK);
FlashcardsCreateCategoryDialog *dialog = flashcards_create_category_dialog_new(user_data);
adw_dialog_present(ADW_DIALOG(dialog), GTK_WIDGET(user_data));
}
void flashcards_app_window_test(FlashcardsAppWindow *win, const gchar *test)
{
database_save_category(test);
GtkWidget *child = adw_action_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(child), test);
gtk_list_box_append(GTK_LIST_BOX(win->topics), child);
}
static void
@@ -87,12 +87,14 @@ flashcards_app_window_class_init(FlashcardsAppWindowClass *class)
{
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class), "/li/sopht/flashcards/window.ui");
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, leaflet);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, leaflet_previous);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, split_view);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, sidebar);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, content);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, topics);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_navigate_back);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_category_selected);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_add_category);
}
FlashcardsAppWindow *

View File

@@ -11,5 +11,6 @@ G_DECLARE_FINAL_TYPE(FlashcardsAppWindow, flashcards_app_window, FLASHCARDS, APP
FlashcardsAppWindow *flashcards_app_window_new(FlashcardsApp *app);
void flashcards_app_window_open(FlashcardsAppWindow *win,
GFile *file);
void flashcards_app_window_test(FlashcardsAppWindow *win, const gchar *test);
#endif /* __FLASHCARDSAPPWIN_H */

View File

@@ -1,4 +1,4 @@
#define GETTEXT_PACKAGE "flashcards"
#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
@@ -6,12 +6,11 @@
#include "flashcardsapp.h"
int
main (int argc, char *argv[])
int main(int argc, char *argv[])
{
bindtextdomain (GETTEXT_PACKAGE, "/usr/local/share/locale");
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
return g_application_run (G_APPLICATION (flashcards_app_new ()), argc, argv);
return g_application_run(G_APPLICATION(flashcards_app_new()), argc, argv);
}

View File

@@ -1,5 +1,6 @@
sourcefiles = files('main.c',
'flashcardsapp.c',
'flashcardsappwin.c',
'create-category.c',
'database.c'
)