1 /*************************************************************************/ 2 /* Copyright (C) 2012-2013 matias <mati86dl@gmail.com> */ 3 /* */ 4 /* This program is free software: you can redistribute it and/or modify */ 5 /* it under the terms of the GNU General Public License as published by */ 6 /* the Free Software Foundation, either version 3 of the License, or */ 7 /* (at your option) any later version. */ 8 /* */ 9 /* This program is distributed in the hope that it will be useful, */ 10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 12 /* GNU General Public License for more details. */ 13 /* */ 14 /* You should have received a copy of the GNU General Public License */ 15 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 /*************************************************************************/ 17 18 #include "pragha-simple-async.h" 19 20 #include "pragha-statusbar.h" 21 22 struct _AsyncSimple { 23 gpointer userdata; 24 gpointer finished_data; 25 GThreadFunc func_w; 26 GSourceFunc func_f; 27 }; 28 29 /* Generic function to set a message when finished the async operation. 30 * You need set 'pragha_async_set_idle_message' as finish_func 31 * and then return a 'const gchar *' on worker_func. */ 32 33 gboolean 34 pragha_async_set_idle_message (gpointer user_data) 35 { 36 PraghaStatusbar *statusbar; 37 38 const gchar *message = user_data; 39 40 if (message == NULL) 41 return FALSE; 42 43 statusbar = pragha_statusbar_get (); 44 pragha_statusbar_set_misc_text(statusbar, message); pragha_sidebar_attach_plugin(PraghaSidebar * sidebar,GtkWidget * widget,GtkWidget * title,GtkMenu * popup_menu)45 g_object_unref(G_OBJECT(statusbar)); 46 47 return FALSE; 48 } 49 50 /* Launch a asynchronous operation (worker_func), and when finished use another 51 * function (finish_func) in the main loop using the information returned by 52 * the asynchronous operation. */ 53 54 static gboolean 55 pragha_async_finished(gpointer data) 56 { 57 AsyncSimple *as = data; 58 59 as->func_f(as->finished_data); 60 g_slice_free(AsyncSimple, as); 61 62 return FALSE; 63 } 64 65 static gpointer 66 pragha_async_worker(gpointer data) 67 { 68 AsyncSimple *as = data; 69 pragha_sidebar_remove_plugin(PraghaSidebar * sidebar,GtkWidget * widget)70 as->finished_data = as->func_w(as->userdata); 71 72 g_idle_add_full(G_PRIORITY_HIGH_IDLE, pragha_async_finished, as, NULL); 73 74 return NULL; 75 } 76 77 void 78 pragha_async_launch (GThreadFunc worker_func, GSourceFunc finish_func, gpointer user_data) 79 { 80 AsyncSimple *as; 81 82 as = g_slice_new0(AsyncSimple); 83 as->func_w = worker_func; 84 as->func_f = finish_func; 85 as->userdata = user_data; 86 as->finished_data = NULL; 87 88 g_thread_unref(g_thread_new("Launch async", pragha_async_worker, as)); 89 } 90 91 GThread * 92 pragha_async_launch_full (GThreadFunc worker_func, GSourceFunc finish_func, gpointer userdata) 93 { 94 AsyncSimple *as; pragha_sidebar_get_n_panes(PraghaSidebar * sidebar)95 96 as = g_slice_new0(AsyncSimple); 97 as->func_w = worker_func; 98 as->func_f = finish_func; 99 as->userdata = userdata; 100 as->finished_data = NULL; pragha_sidebar_style_position(PraghaSidebar * sidebar,GtkPositionType position)101 102 return g_thread_new("Launch async", pragha_async_worker, as); 103 } 104