Skip to content

Commit

Permalink
Allow canceling of pronunciation index generation
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed Aug 25, 2024
1 parent 55ef3cc commit 0e95864
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 29 deletions.
4 changes: 3 additions & 1 deletion include/jppron/jppron.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "utils/util.h"
#include "jppron/jppron_objects.h"
#include "objects/dict.h"
#include <stdbool.h>
#include <stdatomic.h>

void free_pronfile(Pronfile pronfile[static 1]);
void free_pronfile_buffer(Pronfile *pronfiles);
Expand All @@ -14,6 +16,6 @@ DEFINE_DROP_FUNC(Pronfile *, free_pronfile_buffer)
_deallocator_(free_pronfile_buffer) // TODO: Check why this gives compiler errors
Pronfile *get_pronfiles_for(Word word);

void jppron_create_index(const char *audio_folders_path);
void jppron_create_index(const char *audio_folders_path, atomic_bool *cancel_flag);

#endif // JPPRON_H
88 changes: 71 additions & 17 deletions src/frontends/gtk3popup/dp-preferences-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct _DpPreferencesWindow {

GtkWidget *dictionaries_path_chooser;
GtkWidget *dictpopup_create_button;
GThread *create_thread;
GThread *dictpopup_create_thread;
atomic_bool cancel_dictpopup_create;

GtkWidget *nuke_whitespace_switch;
Expand All @@ -25,7 +25,11 @@ struct _DpPreferencesWindow {
GtkWidget *anki_fields_list;

GtkWidget *pronounce_on_startup_switch;

GtkWidget *pronunciation_path_chooser;
GtkWidget *pron_index_generate_button;
GThread *pron_index_generate_thread;
atomic_bool cancel_pron_index_generate;
};

G_DEFINE_TYPE(DpPreferencesWindow, dp_preferences_window, GTK_TYPE_WINDOW)
Expand All @@ -50,15 +54,56 @@ static void on_preferences_close_button_clicked(GtkButton *button, DpPreferences
gtk_widget_destroy(GTK_WIDGET(self));
}

static void on_pronunciation_create_button_clicked(GtkButton *button, DpPreferencesWindow *self) {
static void cancel_pron_index_generation(DpPreferencesWindow *self) {
atomic_store(&self->cancel_pron_index_generate, true);
gtk_button_set_label(GTK_BUTTON(self->dictpopup_create_button), "Generate Index");
}

static gboolean pron_index_generation_thread_finished(gpointer user_data) {
DpPreferencesWindow *self = DP_PREFERENCES_WINDOW(user_data);

self->pron_index_generate_thread = NULL;
gtk_button_set_label(GTK_BUTTON(self->pron_index_generate_button), "Generate Index");

return G_SOURCE_REMOVE;
}

static gpointer pron_index_generation_thread_func(gpointer user_data) {
DpPreferencesWindow *self = DP_PREFERENCES_WINDOW(user_data);
char *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(self->pronunciation_path_chooser));

jppron_create_index(path);
if (path)
jppron_create_index(path, &self->cancel_pron_index_generate);

g_free(path);

gdk_threads_add_idle(pron_index_generation_thread_finished, self);

return NULL;
}

static bool on_db_exist(void *voidarg) {
static void start_pron_index_generation(DpPreferencesWindow *self) {
atomic_store(&self->cancel_pron_index_generate, false);
self->pron_index_generate_thread =
g_thread_new("pron_index_generation_thread", pron_index_generation_thread_func, self);
gtk_button_set_label(GTK_BUTTON(self->pron_index_generate_button), "Stop");
}

static void on_pron_index_generate_button_clicked(GtkButton *button, DpPreferencesWindow *self) {
char *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(self->pronunciation_path_chooser));

if (self->pron_index_generate_thread) {
cancel_pron_index_generation(self);
} else {
start_pron_index_generation(self);
}

g_free(path);
}

/* --------- dictpopup db generation ---------- */

static bool overwrite_dictpopup_db_dialog(void *voidarg) {
DpPreferencesWindow *pref_window = voidarg;
gboolean overwrite = FALSE;

Expand All @@ -84,10 +129,10 @@ static void cancel_dictpopup_create(DpPreferencesWindow *self) {
gtk_button_set_label(GTK_BUTTON(self->dictpopup_create_button), "Generate Index");
}

static gboolean create_thread_finished(gpointer user_data) {
static gboolean dictpopup_create_thread_finished(gpointer user_data) {
DpPreferencesWindow *self = DP_PREFERENCES_WINDOW(user_data);

self->create_thread = NULL;
self->dictpopup_create_thread = NULL;
gtk_button_set_label(GTK_BUTTON(self->dictpopup_create_button), "Generate Index");

dp_preferences_window_update_dict_order(self);
Expand All @@ -100,23 +145,24 @@ static gpointer dictpopup_create_thread_func(gpointer user_data) {
char *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(self->dictionaries_path_chooser));

if (path)
dictpopup_create(path, on_db_exist, self, &self->cancel_dictpopup_create);
dictpopup_create(path, overwrite_dictpopup_db_dialog, self, &self->cancel_dictpopup_create);

g_free(path);

gdk_threads_add_idle(create_thread_finished, self);
gdk_threads_add_idle(dictpopup_create_thread_finished, self);

return NULL;
}

static void start_dictpopup_create(DpPreferencesWindow *self) {
self->cancel_dictpopup_create = FALSE;
self->create_thread = g_thread_new("create_thread", dictpopup_create_thread_func, self);
atomic_store(&self->cancel_dictpopup_create, false);
self->dictpopup_create_thread =
g_thread_new("create_thread", dictpopup_create_thread_func, self);
gtk_button_set_label(GTK_BUTTON(self->dictpopup_create_button), "Stop");
}

static void on_dictpopup_create_button_clicked(GtkButton *button, DpPreferencesWindow *self) {
if (self->create_thread) {
if (self->dictpopup_create_thread) {
cancel_dictpopup_create(self);
} else {
start_dictpopup_create(self);
Expand All @@ -134,9 +180,14 @@ static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer use
static void dp_preferences_window_dispose(GObject *object) {
DpPreferencesWindow *self = DP_PREFERENCES_WINDOW(object);

if (self->create_thread) {
if (self->dictpopup_create_thread) {
atomic_store(&self->cancel_dictpopup_create, true);
g_thread_join(self->create_thread);
g_thread_join(self->dictpopup_create_thread);
}

if (self->pron_index_generate_thread) {
atomic_store(&self->cancel_pron_index_generate, true);
g_thread_join(self->pron_index_generate_thread);
}

g_clear_object(&self->settings);
Expand Down Expand Up @@ -230,11 +281,13 @@ static void dp_preferences_window_class_init(DpPreferencesWindowClass *klass) {
pronounce_on_startup_switch);
gtk_widget_class_bind_template_child(widget_class, DpPreferencesWindow,
pronunciation_path_chooser);
gtk_widget_class_bind_template_child(widget_class, DpPreferencesWindow,
pron_index_generate_button);

/* CALLBACKS */
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(klass),
on_preferences_close_button_clicked);
gtk_widget_class_bind_template_callback(widget_class, on_pronunciation_create_button_clicked);
gtk_widget_class_bind_template_callback(widget_class, on_pron_index_generate_button_clicked);
gtk_widget_class_bind_template_callback(widget_class, on_dictpopup_create_button_clicked);
}

Expand Down Expand Up @@ -442,7 +495,7 @@ static void destroy_widget(GtkWidget *widget, void *data) {
gtk_widget_destroy(widget);
}

static void list_box_insert_msg(GtkListBox *list_box, const char* msg) {
static void list_box_insert_msg(GtkListBox *list_box, const char *msg) {
GtkWidget *row = gtk_list_box_row_new();
GtkWidget *label = gtk_label_new(msg);
gtk_container_add(GTK_CONTAINER(row), label);
Expand All @@ -469,8 +522,9 @@ void dp_preferences_window_update_dict_order(DpPreferencesWindow *self) {

_drop_(s8_buf_free) s8Buf dict_names = db_get_dictnames(db);
db_close(db);
if(s8_buf_size(dict_names) <= 0) {
list_box_insert_msg(GTK_LIST_BOX(self->dict_order_listbox), "No dictionaries found in database");
if (s8_buf_size(dict_names) <= 0) {
list_box_insert_msg(GTK_LIST_BOX(self->dict_order_listbox),
"No dictionaries found in database");
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/frontends/gtk3popup/dp-preferences-window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,13 @@
</packing>
</child>
<child>
<object class="GtkButton" id="pronunciation_create_button">
<object class="GtkButton" id="pron_index_generate_button">
<property name="label" translatable="yes">Generate Index</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="halign">end</property>
<property name="valign">center</property>
<signal name="clicked" handler="on_pronunciation_create_button_clicked" swapped="no"/>
<signal name="clicked" handler="on_pron_index_generate_button_clicked" swapped="no"/>
</object>
</child>
</object>
Expand Down
35 changes: 26 additions & 9 deletions src/jppron/jppron.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "jppron/jppron_database.h"

#include "deinflector/kata2hira.h"
#include "platformdep/audio.h"
#include "platformdep/file_operations.h"
#include "utils/messages.h"
#include "utils/util.h"
Expand Down Expand Up @@ -147,14 +146,32 @@ Pronfile *get_pronfiles_for(Word word) {
return pronfiles;
}

void jppron_create_index(const char *audio_folders_path) {
_drop_(frees8) s8 dbpath = get_default_database_path();
void jppron_create_index(const char *audio_folders_path, atomic_bool *cancel_flag) {
if (!audio_folders_path) {
dbg("No audio index path provided");
return;
}

_drop_(frees8) s8 dbpth = get_default_database_path();

dbg("Indexing pronunciation files..");
jdb_remove(dbpth);
createdir(dbpth);

_drop_(jppron_close_db) PronDatabase *db = jppron_open_db(false);

_drop_(closedir) DIR *audio_dir = opendir(audio_folders_path);
err_ret_on(!audio_dir, "Failed to open audio directory '%s': %s", audio_folders_path,
strerror(errno));

struct dirent *entry;
while ((entry = readdir(audio_dir)) != NULL && !atomic_load(cancel_flag)) {
s8 fn = fromcstr_(entry->d_name);

if (s8equals(fn, S(".")) || s8equals(fn, S("..")))
continue;

if (audio_folders_path) {
msg("Indexing files..");
jppron_create_database(audio_folders_path, dbpath); // TODO: エラー処理
msg("Index completed.");
} else {
err("No path provided.");
process_audio_subdirectory(audio_folders_path, entry->d_name, db);
}
dbg("Done with pronunciation index.");
}

0 comments on commit 0e95864

Please sign in to comment.