1 /**************************************************************************/
2 /* Klavaro - a flexible touch typing tutor */
3 /* Copyright (C) from 2005 until 2008 Felipe Castro */
4 /* Copyright (C) from 2009 until 2019 The Free Software Foundation */
5 /* */
6 /* This program is free software, licensed under the terms of the GNU */
7 /* General Public License as published by the Free Software Foundation, */
8 /* either version 3 of the License, or (at your option) any later */
9 /* version. You should have received a copy of the GNU General Public */
10 /* License along with this program. If not, */
11 /* see <https://www.gnu.org/licenses/>. */
12 /**************************************************************************/
13
14 #include <sys/stat.h>
15 #include <string.h>
16 #include <glib.h>
17 #include <glib/gstdio.h>
18 #include <gtk/gtk.h>
19
20 #include "auxiliar.h"
21 #include "main.h"
22
23 extern GtkBuilder *gui;
24
25 GObject *
get_obj(gchar * name)26 get_obj (gchar *name)
27 {
28 GObject *obj;
29 obj = gtk_builder_get_object (gui, name);
30 if (obj == NULL)
31 g_warning ("Object not found: %s", name);
32 return (obj);
33 }
34
35 GtkWidget *
get_wg(gchar * name)36 get_wg (gchar *name)
37 {
38 GObject *obj;
39 obj = gtk_builder_get_object (gui, name);
40 if (obj == NULL)
41 g_warning ("Widget not found: %s", name);
42 return (GTK_WIDGET (obj));
43 }
44
45 GtkWindow *
get_win(gchar * name)46 get_win (gchar *name)
47 {
48 GObject *obj;
49 obj = gtk_builder_get_object (gui, name);
50 if (obj == NULL)
51 g_warning ("Window not found: %s", name);
52 return (GTK_WINDOW (obj));
53 }
54
55 /* Set an image widget with the name of the file provided in the data dir
56 */
57 void
set_pixmap(gchar * widget,gchar * image)58 set_pixmap (gchar *widget, gchar *image)
59 {
60 gchar *tmp;
61 GtkImage *img;
62
63 tmp = g_build_filename (main_path_data (), image, NULL);
64 img = GTK_IMAGE (get_wg (widget));
65 gtk_image_set_from_file (img, tmp);
66 g_free (tmp);
67 }
68
69 /* Search for the user directory and create it if not found
70 */
71 void
assert_user_dir()72 assert_user_dir ()
73 {
74 GDir *dh;
75
76 dh = g_dir_open (main_path_user (), 0, NULL);
77 if (dh == NULL)
78 {
79 g_message ("creating an empty user folder:\n %s", main_path_user ());
80 g_mkdir (main_path_user (), DIR_PERM);
81 dh = g_dir_open (main_path_user (), 0, NULL);
82 if (dh == NULL)
83 g_error ("could not creat a user folder, so we must quit!");
84 }
85 g_dir_close (dh);
86 }
87
88 /* Compare two strings, so that it applies to other sorting functions.
89 */
90 gint
compare_string_function(gconstpointer a,gconstpointer b)91 compare_string_function (gconstpointer a, gconstpointer b)
92 {
93 return (strcasecmp (a, b));
94 }
95