mirror of
https://gitlab.cvh-server.de/skrause/flashcards.git
synced 2025-12-12 09:01:37 +01:00
Initial commit
This commit is contained in:
133
src/database.c
Normal file
133
src/database.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "database.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
sqlite3 *db;
|
||||
|
||||
void database_connect(const char *path)
|
||||
{
|
||||
// char *zErrMsg = 0;
|
||||
|
||||
int rc;
|
||||
|
||||
gchar *file = g_build_filename(path, "cards.db", NULL);
|
||||
rc = sqlite3_open(file, &db);
|
||||
|
||||
if (rc)
|
||||
{
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Opened database successfully\n");
|
||||
}
|
||||
g_free(file);
|
||||
}
|
||||
|
||||
void database_close()
|
||||
{
|
||||
sqlite3_close(db);
|
||||
}
|
||||
|
||||
void database_create_tables()
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
char *sql = "CREATE TABLE IF NOT EXISTS `cards` ("
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`task` TEXT NOT NULL,"
|
||||
"`solution` TEXT NOT NULL"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
sql = "CREATE TABLE IF NOT EXISTS `categories` ("
|
||||
"`id` INTEGER NOT NULL,"
|
||||
"`name` TEXT NOT NULL,"
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void database_save_category()
|
||||
{
|
||||
}
|
||||
|
||||
GArray *database_load_categories()
|
||||
{
|
||||
GArray *categories = g_array_new(TRUE, FALSE, sizeof(category));
|
||||
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
rc = sqlite3_prepare_v2(db, "SELECT * FROM categories", -1, &stmt, 0);
|
||||
|
||||
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);
|
||||
const unsigned char *temp_name = sqlite3_column_text(stmt, 1);
|
||||
|
||||
char *name = g_new0(char, strlen(temp_name));
|
||||
strcpy(name, temp_name);
|
||||
category c = {id, name};
|
||||
g_array_append_val(categories, c);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return categories;
|
||||
}
|
||||
|
||||
void database_save_card(card c)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf(stdout, "%s: %s\n", c.task, c.solution);
|
||||
rc = sqlite3_prepare_v2(db, "INSERT INTO cards VALUES(?, ?, ?)", -1, &stmt, 0);
|
||||
|
||||
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
|
||||
{
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
GArray *database_load_cards()
|
||||
{
|
||||
GArray *cards = g_array_new(TRUE, FALSE, sizeof(card));
|
||||
|
||||
return cards;
|
||||
}
|
||||
28
src/database.h
Normal file
28
src/database.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <glib.h>
|
||||
|
||||
typedef struct category
|
||||
{
|
||||
int id;
|
||||
char *name;
|
||||
} category;
|
||||
|
||||
typedef struct card
|
||||
{
|
||||
int category;
|
||||
char *task;
|
||||
char *solution;
|
||||
} card;
|
||||
|
||||
void database_connect(const char *path);
|
||||
|
||||
void database_close();
|
||||
|
||||
void database_create_tables();
|
||||
|
||||
void database_save_category();
|
||||
|
||||
GArray *database_load_categories();
|
||||
|
||||
void database_save_card(card c);
|
||||
|
||||
GArray *database_load_cards();
|
||||
65
src/flashcardsapp.c
Normal file
65
src/flashcardsapp.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
|
||||
#include "flashcardsapp.h"
|
||||
#include "flashcardsappwin.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
struct _FlashcardsApp
|
||||
{
|
||||
AdwApplication parent;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(FlashcardsApp, flashcards_app, ADW_TYPE_APPLICATION);
|
||||
|
||||
static void
|
||||
flashcards_app_init(FlashcardsApp *app)
|
||||
{
|
||||
}
|
||||
|
||||
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_open(GApplication *app,
|
||||
GFile **files,
|
||||
int n_files,
|
||||
const char *hint)
|
||||
{
|
||||
GList *windows;
|
||||
FlashcardsAppWindow *win;
|
||||
int i;
|
||||
|
||||
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));
|
||||
|
||||
for (i = 0; i < n_files; i++)
|
||||
flashcards_app_window_open(win, files[i]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
FlashcardsApp *
|
||||
flashcards_app_new(void)
|
||||
{
|
||||
return g_object_new(FLASHCARDS_APP_TYPE,
|
||||
"application-id", "li.sopht.flashcards",
|
||||
"flags", G_APPLICATION_HANDLES_OPEN,
|
||||
NULL);
|
||||
}
|
||||
12
src/flashcardsapp.h
Normal file
12
src/flashcardsapp.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef __FLASHCARDSAPP_H
|
||||
#define __FLASHCARDSAPP_H
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
|
||||
#define FLASHCARDS_APP_TYPE (flashcards_app_get_type ())
|
||||
G_DECLARE_FINAL_TYPE (FlashcardsApp, flashcards_app, FLASHCARDS, APP, AdwApplication)
|
||||
|
||||
FlashcardsApp *flashcards_app_new (void);
|
||||
|
||||
#endif /* __FLASHCARDSAPP_H */
|
||||
106
src/flashcardsappwin.c
Normal file
106
src/flashcardsappwin.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
|
||||
#include <glib/gi18n.h>
|
||||
|
||||
#include "flashcardsapp.h"
|
||||
#include "flashcardsappwin.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
struct _FlashcardsAppWindow
|
||||
{
|
||||
AdwApplicationWindow parent;
|
||||
|
||||
GArray *categories;
|
||||
|
||||
AdwLeaflet *leaflet;
|
||||
GtkButton *leaflet_previous;
|
||||
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)
|
||||
{
|
||||
if (row == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
printf("Test\n");
|
||||
FlashcardsAppWindow *win = FLASHCARDS_APP_WINDOW(gtk_widget_get_root(GTK_WIDGET(box)));
|
||||
adw_leaflet_navigate(win->leaflet, ADW_NAVIGATION_DIRECTION_FORWARD);
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static void
|
||||
flashcards_app_window_init(FlashcardsAppWindow *win)
|
||||
{
|
||||
gtk_widget_init_template(GTK_WIDGET(win));
|
||||
|
||||
database_connect(g_get_user_data_dir());
|
||||
database_create_tables();
|
||||
win->categories = database_load_categories();
|
||||
|
||||
GArray *categories = win->categories;
|
||||
|
||||
for (int i = 0; i < categories->len; i++)
|
||||
{
|
||||
category c = g_array_index(categories, category, i);
|
||||
printf("%d: %s\n", c.id, c.name);
|
||||
|
||||
GtkWidget *child = adw_action_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(child), c.name);
|
||||
|
||||
gtk_list_box_append(GTK_LIST_BOX(win->topics), child);
|
||||
}
|
||||
}
|
||||
|
||||
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, 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, 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);
|
||||
}
|
||||
|
||||
FlashcardsAppWindow *
|
||||
flashcards_app_window_new(FlashcardsApp *app)
|
||||
{
|
||||
return g_object_new(FLASHCARDS_APP_WINDOW_TYPE, "application", app, NULL);
|
||||
}
|
||||
|
||||
void flashcards_app_window_open(FlashcardsAppWindow *win, GFile *file)
|
||||
{
|
||||
}
|
||||
15
src/flashcardsappwin.h
Normal file
15
src/flashcardsappwin.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef __FLASHCARDSAPPWIN_H
|
||||
#define __FLASHCARDSAPPWIN_H
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <adwaita.h>
|
||||
#include "flashcardsapp.h"
|
||||
|
||||
#define FLASHCARDS_APP_WINDOW_TYPE (flashcards_app_window_get_type())
|
||||
G_DECLARE_FINAL_TYPE(FlashcardsAppWindow, flashcards_app_window, FLASHCARDS, APP_WINDOW, AdwApplicationWindow)
|
||||
|
||||
FlashcardsAppWindow *flashcards_app_window_new(FlashcardsApp *app);
|
||||
void flashcards_app_window_open(FlashcardsAppWindow *win,
|
||||
GFile *file);
|
||||
|
||||
#endif /* __FLASHCARDSAPPWIN_H */
|
||||
17
src/main.c
Normal file
17
src/main.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#define GETTEXT_PACKAGE "flashcards"
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <glib/gi18n.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "flashcardsapp.h"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
bindtextdomain (GETTEXT_PACKAGE, "/usr/local/share/locale");
|
||||
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
||||
textdomain (GETTEXT_PACKAGE);
|
||||
|
||||
return g_application_run (G_APPLICATION (flashcards_app_new ()), argc, argv);
|
||||
}
|
||||
5
src/meson.build
Normal file
5
src/meson.build
Normal file
@@ -0,0 +1,5 @@
|
||||
sourcefiles = files('main.c',
|
||||
'flashcardsapp.c',
|
||||
'flashcardsappwin.c',
|
||||
'database.c'
|
||||
)
|
||||
Reference in New Issue
Block a user