mirror of
https://gitlab.cvh-server.de/skrause/flashcards.git
synced 2025-12-12 09:01:37 +01:00
formatting, small changes
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <glib/gi18n.h>
|
||||
|
||||
#include "create-category.h"
|
||||
|
||||
struct _FlashcardsCreateCategoryDialog
|
||||
{
|
||||
struct _FlashcardsCreateCategoryDialog {
|
||||
AdwAlertDialog parent;
|
||||
|
||||
GtkEditable *entry;
|
||||
@@ -16,41 +15,30 @@ struct _FlashcardsCreateCategoryDialog
|
||||
|
||||
G_DEFINE_TYPE(FlashcardsCreateCategoryDialog, flashcards_create_category_dialog, ADW_TYPE_ALERT_DIALOG);
|
||||
|
||||
static void
|
||||
flashcards_create_category_dialog_init(FlashcardsCreateCategoryDialog *self)
|
||||
{
|
||||
static void flashcards_create_category_dialog_init(FlashcardsCreateCategoryDialog *self) {
|
||||
gtk_widget_init_template(GTK_WIDGET(self));
|
||||
}
|
||||
|
||||
static void
|
||||
on_response(__attribute__((unused)) AdwAlertDialog *dialog,
|
||||
gchar *response,
|
||||
gpointer user_data)
|
||||
{
|
||||
static void on_response(__attribute__((unused)) AdwAlertDialog *dialog, gchar *response, gpointer user_data) {
|
||||
FlashcardsCreateCategoryDialog *self = user_data;
|
||||
|
||||
const gchar *category = gtk_editable_get_text(self->entry);
|
||||
|
||||
if (strcmp(response, "add") == 0 && strlen(category) > 0)
|
||||
{
|
||||
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");
|
||||
static void flashcards_create_category_dialog_class_init(FlashcardsCreateCategoryDialogClass *klass) {
|
||||
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass), "/li/sopht/flashcards/create-category.ui");
|
||||
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsCreateCategoryDialog, entry);
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), FlashcardsCreateCategoryDialog, entry);
|
||||
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_response);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(klass), on_response);
|
||||
}
|
||||
|
||||
FlashcardsCreateCategoryDialog *
|
||||
flashcards_create_category_dialog_new(FlashcardsAppWindow *win)
|
||||
{
|
||||
FlashcardsCreateCategoryDialog *self = g_object_new(FLASHCARDS_CREATE_CATEGORY_DIALOG_TYPE, NULL);
|
||||
FlashcardsCreateCategoryDialog *flashcards_create_category_dialog_new(FlashcardsAppWindow *win) {
|
||||
FlashcardsCreateCategoryDialog *self = g_object_new(FLASHCARDS_CREATE_CATEGORY_DIALOG_TYPE, nullptr);
|
||||
self->win = win;
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#ifndef __CREATECATEGORYDIALOG_H
|
||||
#define __CREATECATEGORYDIALOG_H
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.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)
|
||||
G_DECLARE_FINAL_TYPE(FlashcardsCreateCategoryDialog, flashcards_create_category_dialog, FLASHCARDS,
|
||||
CREATE_CATEGORY_DIALOG, AdwAlertDialog)
|
||||
|
||||
FlashcardsCreateCategoryDialog *flashcards_create_category_dialog_new(FlashcardsAppWindow *win);
|
||||
|
||||
|
||||
176
src/database.c
176
src/database.c
@@ -2,51 +2,40 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
sqlite3 *database_connect(const char *path)
|
||||
{
|
||||
int rc;
|
||||
gchar *file;
|
||||
sqlite3 *database_connect(const char *path) {
|
||||
sqlite3 *db;
|
||||
|
||||
file = g_build_filename(path, "cards.db", NULL);
|
||||
gchar *file = g_build_filename(path, "cards.db", NULL);
|
||||
int rc = sqlite3_open(file, &db);
|
||||
|
||||
rc = sqlite3_open(file, &db);
|
||||
|
||||
if (rc)
|
||||
{
|
||||
if (rc) {
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Opened database successfully\n");
|
||||
return nullptr;
|
||||
}
|
||||
fprintf(stdout, "Opened database successfully\n");
|
||||
g_free(file);
|
||||
|
||||
database_create_tables(db);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
void database_close(sqlite3 *db)
|
||||
{
|
||||
sqlite3_close(db);
|
||||
}
|
||||
void database_close(sqlite3 *db) { sqlite3_close(db); }
|
||||
|
||||
void database_create_tables(sqlite3 *db)
|
||||
{
|
||||
int rc;
|
||||
void database_create_tables(sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
const char *sql;
|
||||
|
||||
sql = "CREATE TABLE IF NOT EXISTS `cards` ("
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`task` TEXT NOT NULL,"
|
||||
"`solution` TEXT NOT NULL"
|
||||
")";
|
||||
const char *sql = "CREATE TABLE IF NOT EXISTS `cards` ("
|
||||
"`id` INTEGER NOT NULL,"
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`task` TEXT NOT NULL,"
|
||||
"`solution` TEXT NOT NULL,"
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -59,10 +48,9 @@ void database_create_tables(sqlite3 *db)
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -70,75 +58,86 @@ void database_create_tables(sqlite3 *db)
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void database_save_category(sqlite3 *db, const char *c)
|
||||
{
|
||||
int rc;
|
||||
void database_save_category(sqlite3 *db, const char *c) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf(stdout, "%s\n", c);
|
||||
rc = sqlite3_prepare_v2(db, "INSERT INTO categories (name) VALUES(?)", -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, "INSERT INTO categories (name) VALUES(?)", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK)
|
||||
{
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_text(stmt, 1, c, -1, SQLITE_STATIC);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
GArray *database_load_categories(sqlite3 *db)
|
||||
{
|
||||
GArray *categories;
|
||||
int rc;
|
||||
GArray *database_load_categories(sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
categories = g_array_new(TRUE, FALSE, sizeof(category));
|
||||
GArray *categories = g_array_new(TRUE, FALSE, sizeof(category));
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM categories", -1, &stmt, nullptr);
|
||||
|
||||
rc = sqlite3_prepare_v2(db, "SELECT * FROM categories", -1, &stmt, 0);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
{
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
char *name = strdup((const char *)sqlite3_column_text(stmt, 1));
|
||||
|
||||
category c = {id, name};
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
category c;
|
||||
c.id = sqlite3_column_int(stmt, 0);
|
||||
c.name = strdup((char *) sqlite3_column_text(stmt, 1));
|
||||
g_array_append_val(categories, c);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
void database_save_card(sqlite3 *db, card c)
|
||||
{
|
||||
int rc;
|
||||
void database_delete_category(sqlite3 *db, const int id) {
|
||||
// delete all cards in the category
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "DELETE FROM cards WHERE category = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
// delete the category
|
||||
rc = sqlite3_prepare_v2(db, "DELETE FROM categories WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void database_save_card(sqlite3 *db, card c) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf(stdout, "%s: %s\n", c.task, c.solution);
|
||||
rc = sqlite3_prepare_v2(db, "INSERT INTO cards VALUES(?, ?, ?)", -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, "INSERT INTO cards VALUES(?, ?, ?)", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK)
|
||||
{
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 0, c.category);
|
||||
sqlite3_bind_text(stmt, 1, c.task, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, c.solution, -1, SQLITE_STATIC);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -146,11 +145,40 @@ void database_save_card(sqlite3 *db, card c)
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
GArray *database_load_cards(sqlite3 *db)
|
||||
{
|
||||
GArray *cards;
|
||||
GArray *database_load_cards(sqlite3 *db, int category) {
|
||||
GArray *cards = g_array_new(TRUE, FALSE, sizeof(card));
|
||||
|
||||
cards = g_array_new(TRUE, FALSE, sizeof(card));
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM cards WHERE category = ?", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, category);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
card c;
|
||||
c.id = sqlite3_column_int(stmt, 0);
|
||||
c.category = sqlite3_column_int(stmt, 1);
|
||||
c.task = strdup((char *) sqlite3_column_text(stmt, 2));
|
||||
c.solution = strdup((char *) sqlite3_column_text(stmt, 3));
|
||||
|
||||
g_array_append_val(cards, c);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return cards;
|
||||
}
|
||||
}
|
||||
|
||||
void database_delete_card(sqlite3 *db, const int id) {
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "DELETE FROM cards WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
#ifndef DATABASE_H
|
||||
#define DATABASE_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
typedef struct category
|
||||
{
|
||||
typedef struct category {
|
||||
int id;
|
||||
char *name;
|
||||
} category;
|
||||
|
||||
typedef struct card
|
||||
{
|
||||
typedef struct card {
|
||||
int id;
|
||||
int category;
|
||||
char *task;
|
||||
char *solution;
|
||||
@@ -24,6 +26,11 @@ void database_save_category(sqlite3 *db, const char *c);
|
||||
|
||||
GArray *database_load_categories(sqlite3 *db);
|
||||
|
||||
void database_delete_category(sqlite3 *db, int id);
|
||||
|
||||
void database_save_card(sqlite3 *db, card c);
|
||||
|
||||
GArray *database_load_cards(sqlite3 *db);
|
||||
GArray *database_load_cards(sqlite3 *db, int category);
|
||||
|
||||
void database_delete_card(sqlite3 *db, int id);
|
||||
#endif /* DATABASE_H */
|
||||
|
||||
@@ -1,105 +1,70 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <glib/gi18n.h>
|
||||
|
||||
#include "flashcardsapp.h"
|
||||
#include "flashcardsappwin.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
struct _FlashcardsApp
|
||||
{
|
||||
AdwApplication parent;
|
||||
struct _FlashcardsApp {
|
||||
AdwApplication parent;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(FlashcardsApp, flashcards_app, ADW_TYPE_APPLICATION);
|
||||
|
||||
static void
|
||||
flashcards_app_quit(
|
||||
__attribute__((unused)) GSimpleAction *action,
|
||||
__attribute__((unused)) 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_quit(__attribute__((unused)) GSimpleAction *action,
|
||||
__attribute__((unused)) 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(
|
||||
__attribute__((unused)) GSimpleAction *action,
|
||||
__attribute__((unused)) GVariant *parameter,
|
||||
gpointer user_data)
|
||||
{
|
||||
FlashcardsApp *self = user_data;
|
||||
GtkWindow *window;
|
||||
AdwDialog *dialog;
|
||||
static void flashcards_app_about(__attribute__((unused)) GSimpleAction *action,
|
||||
__attribute__((unused)) GVariant *parameter, gpointer user_data) {
|
||||
FlashcardsApp *self = user_data;
|
||||
|
||||
g_assert(FLASHCARDS_IS_APP(self));
|
||||
g_assert(FLASHCARDS_IS_APP(self));
|
||||
|
||||
window = gtk_application_get_active_window(GTK_APPLICATION(self));
|
||||
GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self));
|
||||
|
||||
dialog = adw_about_dialog_new_from_appdata("/li/sopht/flashcards/appdata", NULL);
|
||||
adw_dialog_present(dialog, GTK_WIDGET(window));
|
||||
AdwDialog *dialog = adw_about_dialog_new_from_appdata("/li/sopht/flashcards/appdata", nullptr);
|
||||
adw_dialog_present(dialog, GTK_WIDGET(window));
|
||||
}
|
||||
|
||||
static const GActionEntry app_actions[] = {
|
||||
{"quit", flashcards_app_quit, NULL, NULL, NULL, {0, 0, 0}},
|
||||
{"about", flashcards_app_about, NULL, NULL, NULL, {0, 0, 0}},
|
||||
{"quit", flashcards_app_quit, nullptr, nullptr, nullptr, {0, 0, 0}},
|
||||
{"about", flashcards_app_about, nullptr, nullptr, nullptr, {0, 0, 0}},
|
||||
};
|
||||
|
||||
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 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", nullptr});
|
||||
}
|
||||
|
||||
static void
|
||||
flashcards_app_activate(GApplication *app)
|
||||
{
|
||||
FlashcardsAppWindow *win;
|
||||
win = flashcards_app_window_new(FLASHCARDS_APP(app));
|
||||
gtk_window_present(GTK_WINDOW(win));
|
||||
static void flashcards_app_activate(GApplication *app) {
|
||||
FlashcardsAppWindow *win = flashcards_app_window_new(FLASHCARDS_APP(app));
|
||||
gtk_window_present(GTK_WINDOW(win));
|
||||
}
|
||||
|
||||
static void
|
||||
flashcards_app_open(
|
||||
GApplication *app,
|
||||
__attribute__((unused)) GFile **files,
|
||||
__attribute__((unused)) int n_files,
|
||||
__attribute__((unused)) const char *hint)
|
||||
{
|
||||
GList *windows;
|
||||
FlashcardsAppWindow *win;
|
||||
static void flashcards_app_open(GApplication *app, __attribute__((unused)) GFile **files,
|
||||
__attribute__((unused)) int n_files, __attribute__((unused)) const char *hint) {
|
||||
FlashcardsAppWindow *win;
|
||||
|
||||
windows = gtk_application_get_windows(GTK_APPLICATION(app));
|
||||
if (windows)
|
||||
win = FLASHCARDS_APP_WINDOW(windows->data);
|
||||
else
|
||||
win = flashcards_app_window_new(FLASHCARDS_APP(app));
|
||||
GList *windows = gtk_application_get_windows(GTK_APPLICATION(app));
|
||||
if (windows)
|
||||
win = FLASHCARDS_APP_WINDOW(windows->data);
|
||||
else
|
||||
win = flashcards_app_window_new(FLASHCARDS_APP(app));
|
||||
|
||||
gtk_window_present(GTK_WINDOW(win));
|
||||
gtk_window_present(GTK_WINDOW(win));
|
||||
}
|
||||
|
||||
static void
|
||||
flashcards_app_class_init(FlashcardsAppClass *class)
|
||||
{
|
||||
G_APPLICATION_CLASS(class)->activate = flashcards_app_activate;
|
||||
G_APPLICATION_CLASS(class)->open = flashcards_app_open;
|
||||
static void flashcards_app_class_init(FlashcardsAppClass *klass) {
|
||||
G_APPLICATION_CLASS(klass)->activate = flashcards_app_activate;
|
||||
G_APPLICATION_CLASS(klass)->open = flashcards_app_open;
|
||||
}
|
||||
|
||||
FlashcardsApp *
|
||||
flashcards_app_new(void)
|
||||
{
|
||||
return g_object_new(FLASHCARDS_APP_TYPE,
|
||||
"application-id", "li.sopht.Flashcards",
|
||||
"flags", G_APPLICATION_DEFAULT_FLAGS,
|
||||
NULL);
|
||||
FlashcardsApp *flashcards_app_new(void) {
|
||||
return g_object_new(FLASHCARDS_APP_TYPE, "application-id", "li.sopht.Flashcards", "flags",
|
||||
G_APPLICATION_DEFAULT_FLAGS, NULL);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#ifndef __FLASHCARDSAPP_H
|
||||
#define __FLASHCARDSAPP_H
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#define FLASHCARDS_APP_TYPE (flashcards_app_get_type ())
|
||||
G_DECLARE_FINAL_TYPE (FlashcardsApp, flashcards_app, FLASHCARDS, APP, AdwApplication)
|
||||
#define FLASHCARDS_APP_TYPE (flashcards_app_get_type())
|
||||
G_DECLARE_FINAL_TYPE(FlashcardsApp, flashcards_app, FLASHCARDS, APP, AdwApplication)
|
||||
|
||||
FlashcardsApp *flashcards_app_new (void);
|
||||
FlashcardsApp *flashcards_app_new(void);
|
||||
|
||||
#endif /* __FLASHCARDSAPP_H */
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <glib/gi18n.h>
|
||||
|
||||
#include "create-category.h"
|
||||
#include "flashcardsapp.h"
|
||||
#include "flashcardsappwin.h"
|
||||
#include "create-category.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
struct _FlashcardsAppWindow
|
||||
{
|
||||
struct _FlashcardsAppWindow {
|
||||
AdwApplicationWindow parent;
|
||||
|
||||
sqlite3 *db;
|
||||
GArray *categories;
|
||||
GQueue *cards;
|
||||
card *current_card;
|
||||
|
||||
AdwWindowTitle *title;
|
||||
|
||||
@@ -23,7 +24,8 @@ struct _FlashcardsAppWindow
|
||||
AdwNavigationPage *content;
|
||||
|
||||
AdwViewStack *main_view;
|
||||
GtkWidget *placeholder;
|
||||
GtkWidget *placeholder_category;
|
||||
GtkWidget *placeholder_card;
|
||||
GtkWidget *flashcard;
|
||||
|
||||
GtkListBox *topics;
|
||||
@@ -33,66 +35,17 @@ struct _FlashcardsAppWindow
|
||||
|
||||
G_DEFINE_TYPE(FlashcardsAppWindow, flashcards_app_window, ADW_TYPE_APPLICATION_WINDOW);
|
||||
|
||||
static void on_category_selected(__attribute__((unused)) GtkListBox *box, GtkListBoxRow *row, gpointer user_data)
|
||||
{
|
||||
FlashcardsAppWindow *win;
|
||||
int id;
|
||||
category c;
|
||||
|
||||
win = user_data;
|
||||
|
||||
adw_navigation_split_view_set_show_content(ADW_NAVIGATION_SPLIT_VIEW(win->split_view), TRUE);
|
||||
|
||||
id = gtk_list_box_row_get_index(row);
|
||||
printf("%d\n", id);
|
||||
|
||||
c = g_array_index(win->categories, category, id);
|
||||
adw_window_title_set_subtitle(ADW_WINDOW_TITLE(win->title), c.name);
|
||||
|
||||
adw_view_stack_set_visible_child(win->main_view, win->flashcard);
|
||||
}
|
||||
|
||||
static void on_add_category(__attribute__((unused)) GtkButton *self,
|
||||
gpointer user_data)
|
||||
{
|
||||
FlashcardsCreateCategoryDialog *dialog;
|
||||
|
||||
dialog = flashcards_create_category_dialog_new(user_data);
|
||||
adw_dialog_present(ADW_DIALOG(dialog), GTK_WIDGET(user_data));
|
||||
}
|
||||
|
||||
static void on_delete_category(__attribute__((unused)) GtkButton *self,
|
||||
gpointer user_data)
|
||||
{
|
||||
FlashcardsAppWindow *win;
|
||||
|
||||
win = user_data;
|
||||
|
||||
printf("Delete category\n");
|
||||
gtk_list_box_unselect_all(win->topics);
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder);
|
||||
}
|
||||
|
||||
static void load_categories(FlashcardsAppWindow *win)
|
||||
{
|
||||
GArray *categories;
|
||||
|
||||
static void load_categories(FlashcardsAppWindow *win) {
|
||||
gtk_list_box_remove_all(win->topics);
|
||||
|
||||
categories = database_load_categories(win->db);
|
||||
win->categories = categories;
|
||||
win->categories = database_load_categories(win->db);
|
||||
|
||||
for (guint i = 0; i < categories->len; i++)
|
||||
{
|
||||
GtkWidget *child;
|
||||
GtkWidget *label;
|
||||
category c;
|
||||
|
||||
c = g_array_index(categories, category, i);
|
||||
for (guint i = 0; i < win->categories->len; i++) {
|
||||
category c = g_array_index(win->categories, category, i);
|
||||
printf("%d: %s\n", c.id, c.name);
|
||||
|
||||
child = gtk_list_box_row_new();
|
||||
label = gtk_label_new(c.name);
|
||||
GtkWidget *child = gtk_list_box_row_new();
|
||||
GtkWidget *label = gtk_label_new(c.name);
|
||||
gtk_widget_set_halign(label, GTK_ALIGN_START);
|
||||
gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_MIDDLE);
|
||||
gtk_widget_set_tooltip_text(label, c.name);
|
||||
@@ -102,33 +55,102 @@ static void load_categories(FlashcardsAppWindow *win)
|
||||
}
|
||||
}
|
||||
|
||||
void flashcards_app_window_test(FlashcardsAppWindow *win, const gchar *test)
|
||||
{
|
||||
static void load_cards(FlashcardsAppWindow *win, category c) {
|
||||
GArray *card_array = database_load_cards(win->db, c.id);
|
||||
|
||||
win->cards = g_queue_new();
|
||||
for (guint i = 0; i < card_array->len; i++) {
|
||||
g_queue_push_tail(win->cards, &g_array_index(card_array, card, i));
|
||||
;
|
||||
}
|
||||
|
||||
printf("Flashcards: %d\n", win->cards->length);
|
||||
if (win->cards->length <= 0) {
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder_card);
|
||||
return;
|
||||
}
|
||||
|
||||
win->current_card = g_queue_pop_head(win->cards);
|
||||
}
|
||||
|
||||
static void on_select_category(__attribute__((unused)) GtkListBox *box, GtkListBoxRow *row, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
|
||||
adw_navigation_split_view_set_show_content(ADW_NAVIGATION_SPLIT_VIEW(win->split_view), TRUE);
|
||||
|
||||
int id = gtk_list_box_row_get_index(row);
|
||||
|
||||
category c = g_array_index(win->categories, category, id);
|
||||
printf("%d: %d\n", id, c.id);
|
||||
adw_window_title_set_subtitle(ADW_WINDOW_TITLE(win->title), c.name);
|
||||
|
||||
adw_view_stack_set_visible_child(win->main_view, win->flashcard);
|
||||
|
||||
load_cards(win, c);
|
||||
}
|
||||
|
||||
static void on_add_category(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
FlashcardsCreateCategoryDialog *dialog = flashcards_create_category_dialog_new(user_data);
|
||||
adw_dialog_present(ADW_DIALOG(dialog), GTK_WIDGET(user_data));
|
||||
}
|
||||
|
||||
static void on_delete_category(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
database_delete_category(win->db, 1);
|
||||
|
||||
printf("Delete category\n");
|
||||
gtk_list_box_unselect_all(win->topics);
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder_category);
|
||||
}
|
||||
|
||||
static void on_add_card(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
/*FlashcardsAppWindow *win = user_data;
|
||||
FlashcardsCreateCardDialog *dialog = flashcards_create_card_dialog_new(win);
|
||||
adw_dialog_present(ADW_DIALOG(dialog), GTK_WIDGET(win));*/
|
||||
}
|
||||
|
||||
static void on_delete_card(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
/*FlashcardsAppWindow *win = user_data;
|
||||
database_delete_card(win->db, 1);
|
||||
printf("Delete card\n");*/
|
||||
}
|
||||
|
||||
static void on_answer_easy(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder_category);
|
||||
}
|
||||
|
||||
static void on_answer_medium(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder_category);
|
||||
}
|
||||
|
||||
static void on_answer_hard(__attribute__((unused)) GtkButton *self, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
adw_view_stack_set_visible_child(win->main_view, win->placeholder_category);
|
||||
}
|
||||
|
||||
void flashcards_app_window_test(FlashcardsAppWindow *win, const gchar *test) {
|
||||
GtkWidget *child;
|
||||
|
||||
database_save_category(win->db, test);
|
||||
load_categories(win);
|
||||
}
|
||||
|
||||
static void on_flashcards_app_window_show(GtkWidget *self,
|
||||
gpointer user_data)
|
||||
{
|
||||
FlashcardsAppWindow *win;
|
||||
win = user_data;
|
||||
static void on_flashcards_app_window_show(GtkWidget *self, gpointer user_data) {
|
||||
FlashcardsAppWindow *win = user_data;
|
||||
printf("Show\n");
|
||||
|
||||
load_categories(win);
|
||||
gtk_list_box_unselect_all(win->topics);
|
||||
}
|
||||
|
||||
static void flashcards_app_window_init(FlashcardsAppWindow *win)
|
||||
{
|
||||
gtk_widget_init_template(GTK_WIDGET(win));
|
||||
win->db = database_connect(g_get_user_data_dir());
|
||||
static void flashcards_app_window_init(FlashcardsAppWindow *self) {
|
||||
gtk_widget_init_template(GTK_WIDGET(self));
|
||||
self->db = database_connect(g_get_user_data_dir());
|
||||
}
|
||||
|
||||
static void flashcards_app_window_class_init(FlashcardsAppWindowClass *class)
|
||||
{
|
||||
static void 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, title);
|
||||
@@ -138,19 +160,25 @@ static void flashcards_app_window_class_init(FlashcardsAppWindowClass *class)
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, content);
|
||||
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, main_view);
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, placeholder);
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, placeholder_category);
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, placeholder_card);
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, flashcard);
|
||||
|
||||
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, topics);
|
||||
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_category_selected);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_flashcards_app_window_show);
|
||||
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_select_category);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_add_category);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_delete_category);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_flashcards_app_window_show);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_add_card);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_delete_card);
|
||||
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_answer_easy);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_answer_medium);
|
||||
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_answer_hard);
|
||||
}
|
||||
|
||||
FlashcardsAppWindow *
|
||||
flashcards_app_window_new(FlashcardsApp *app)
|
||||
{
|
||||
FlashcardsAppWindow *flashcards_app_window_new(FlashcardsApp *app) {
|
||||
return g_object_new(FLASHCARDS_APP_WINDOW_TYPE, "application", app, NULL);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __FLASHCARDSAPPWIN_H
|
||||
#define __FLASHCARDSAPPWIN_H
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include "flashcardsapp.h"
|
||||
|
||||
#define FLASHCARDS_APP_WINDOW_TYPE (flashcards_app_window_get_type())
|
||||
|
||||
13
src/main.c
13
src/main.c
@@ -1,16 +1,15 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <glib/gi18n.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "flashcardsapp.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
|
||||
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
|
||||
textdomain(GETTEXT_PACKAGE);
|
||||
int main(int argc, char *argv[]) {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user