Skip to content

Commit

Permalink
Add option to synchronize during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Aug 15, 2023
1 parent a2ac044 commit 8d6c28b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions includes/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ void cb_manage_hosts();
void cb_connect_host(GtkAction * action);
void cb_local_computer();
void cb_act_as_server();
void cb_sync_on_startup();

#endif /* __CALLBACKS_H__ */
2 changes: 2 additions & 0 deletions includes/syncmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ void sync_manager_clear_entries(void);
void sync_manager_show(GtkWidget *parent);
gint sync_manager_count_entries(void);

void sync_manager_update_on_startup(void);

#endif /* __SYNCMANAGER_H__ */
3 changes: 2 additions & 1 deletion includes/uidefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#define SYNC_MANAGER_MENU_ITEMS \
"<separator/>" \
"<menuitem name=\"SyncManager\" action=\"SyncManagerAction\" " \
"always-show-image=\"true\"/>"
"always-show-image=\"true\"/>" \
"<menuitem name=\"SyncOnStartup\" action=\"SyncOnStartupAction\"/>"
#define SYNC_MANAGER_TOOL_ITEMS \
"<toolitem name=\"SyncManager\" action=\"SyncManagerAction\"/>"

Expand Down
18 changes: 18 additions & 0 deletions shell/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ void cb_sync_manager()
sync_manager_show(shell->window);
}

void cb_sync_on_startup()
{
gboolean setting = shell_action_get_active("SyncOnStartupAction");
GKeyFile *key_file = g_key_file_new();

gchar *conf_path = g_build_filename(g_get_user_config_dir(), "hardinfo",
"settings.ini", NULL);

g_key_file_load_from_file(
key_file, conf_path,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
g_key_file_set_boolean(key_file, "Sync", "OnStartup", setting);
g_key_file_save_to_file(key_file, conf_path, NULL);

g_free(conf_path);
g_key_file_free(key_file);
}

void cb_open_web_page()
{
uri_open("http://www.hardinfo.org");
Expand Down
4 changes: 4 additions & 0 deletions shell/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ static GtkToggleActionEntry toggle_entries[] = {
N_("_Toolbar"), NULL,
NULL,
G_CALLBACK(cb_toolbar)},
{"SyncOnStartupAction", NULL,
N_("Synchronize on startup"), NULL,
NULL,
G_CALLBACK(cb_sync_on_startup)},
};

/* Implement a handler for GtkUIManager's "add_widget" signal. The UI manager
Expand Down
25 changes: 25 additions & 0 deletions shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,29 @@ select_first_tree_item(gpointer data)
return FALSE;
}

static void
check_for_updates(void)
{
GKeyFile *key_file = g_key_file_new();

gchar *conf_path = g_build_filename(g_get_user_config_dir(), "hardinfo",
"settings.ini", NULL);

g_key_file_load_from_file(
key_file, conf_path,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);

gboolean setting = g_key_file_get_boolean(key_file, "Sync", "OnStartup", NULL);
shell_action_set_active("SyncOnStartupAction", setting);

g_free(conf_path);
g_key_file_free(key_file);

if (setting) {
sync_manager_update_on_startup();
}
}

gboolean hardinfo_link(const gchar *uri) {
/* Clicked link events pass through here on their
* way to the default handler (xdg-open).
Expand Down Expand Up @@ -792,6 +815,8 @@ void shell_init(GSList * modules)

shell->tree->modules = modules ? modules : modules_load_all();

check_for_updates();

g_slist_foreach(shell->tree->modules, shell_add_modules_to_gui, shell->tree);
gtk_tree_view_expand_all(GTK_TREE_VIEW(shell->tree->view));

Expand Down
59 changes: 51 additions & 8 deletions shell/syncmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,24 @@ static SoupURI *sync_manager_get_proxy(void)
return soup_uri_new(conf);
}

static void sync_dialog_start_sync(SyncDialog *sd)
static void ensure_soup_session(void)
{
gint nactions;
SyncNetAction *actions;

if (!session) {
SoupURI *proxy = sync_manager_get_proxy();

session = soup_session_new_with_options(
SOUP_SESSION_TIMEOUT, 10, SOUP_SESSION_PROXY_URI, proxy, NULL);
/* Crashes if we unref the proxy? O_o */
/*if (proxy)
g_object_unref(proxy); */
}
}

static void sync_dialog_start_sync(SyncDialog *sd)
{
gint nactions;
SyncNetAction *actions;

ensure_soup_session();

loop = g_main_loop_new(NULL, TRUE);
loop = g_main_loop_new(NULL, FALSE);

gtk_widget_hide(sd->button_sync);
gtk_widget_hide(sd->button_priv_policy);
Expand Down Expand Up @@ -647,4 +649,45 @@ static void sync_dialog_destroy(SyncDialog *sd)
sync_dialog_netarea_destroy(sd->sna);
g_free(sd);
}

static gboolean sync_one(gpointer data)
{
SyncNetAction *sna = data;

if (sna->entry->generate_contents_for_upload)
goto out;

DEBUG("Syncronizing: %s", sna->entry->name);

gchar *msg = g_strdup_printf(_("Synchronizing: %s"), _(sna->entry->name));
shell_status_update(msg);
shell_status_pulse();
g_free(msg);

send_request_for_net_action(sna);

out:
g_main_loop_unref(loop);
idle_free(sna);

return FALSE;
}

void sync_manager_update_on_startup(void)
{
GSList *entry;

ensure_soup_session();

loop = g_main_loop_new(NULL, FALSE);

for (entry = entries; entry; entry = entry->next) {
SyncNetAction *action = g_new0(SyncNetAction, 1);

action->entry = entry->data;
loop = g_main_loop_ref(loop);

g_idle_add(sync_one, action);
}
}
#endif /* HAS_LIBSOUP */

0 comments on commit 8d6c28b

Please sign in to comment.