Heraia  0.1.8
heraia_io.c
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  heraia_io.c
4  heraia_io.c - input and output functions for heraia
5 
6  (C) Copyright 2005 - 2011 Olivier Delhomme
7  e-mail : heraia@delhomme.org
8  URL : http://heraia.tuxfamily.org
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2, or (at your option)
13  any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /**
24  * @file heraia_io.c
25  * Here I want to see everything that deals with I/O, files, disk and so on.
26  */
27 #include <libheraia.h>
28 
29 static GtkBuilder *load_xml_if_it_exists(char *file_to_load);
30 
31 
32 /**
33  * Loads the file 'filename' to analyse and populates the
34  * corresponfing structure 'main_struct' as needed thus
35  * main_struct and filename must NOT be NULL pointers
36  * @param main_struct : main structure (it must not be NULL)
37  * @param filename : filename of the file to load (it must not be NULL)
38  * @return TRUE if everything went ok, FALSE otherwise
39  */
40 gboolean load_file_to_analyse(heraia_struct_t *main_struct, gchar *filename)
41 {
42  struct stat *stat_buf = NULL;
43  gboolean success = FALSE;
44  doc_t* doc = NULL;
45 
46  g_return_val_if_fail(filename != NULL, FALSE);
47  g_return_val_if_fail(main_struct != NULL, FALSE);
48 
49  stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
50  stat(filename, stat_buf);
51 
52  log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("filename to load : %s"), filename);
53 
54  if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size > 0)
55  {
56 
57  doc = heraia_hex_document_new(main_struct, filename); /* Adds an new hexdocument */
58 
59  if (doc != NULL)
60  {
61  add_new_tab_in_main_window(main_struct, doc);
62 
63  log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Hexwidget : %p"), doc->hex_widget);
64 
65  success = TRUE;
66 
67  /* updating the window name */
68  update_main_window_name(main_struct);
69 
70  /* Showing all the widgets (in the menu and such) */
71  grey_main_widgets(main_struct->xmls->main, FALSE);
72 
73  log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("file %s loaded !"), filename);
74  }
75  else
76  {
77  log_message(main_struct, G_LOG_LEVEL_ERROR, Q_("Error while trying to load file %s"), filename);
78  success = FALSE;
79  }
80 
81  }
82  else
83  {
84  if (S_ISREG(stat_buf->st_mode))
85  {
86  log_message(main_struct, G_LOG_LEVEL_WARNING, Q_("The file %s is empty !"), filename);
87  }
88  else
89  {
90  log_message(main_struct, G_LOG_LEVEL_WARNING, Q_("The file %s does not exist !"), filename);
91  }
92  success = FALSE;
93  }
94 
95  g_free(stat_buf);
96 
97  return success;
98 }
99 
100 
101 /**
102  * Checks if file_to_load exists and is valid and if possible, loads it
103  * in the xml structure
104  * @param file_to_load : a filename of a possibly existing GtkBuilder file
105  * @return returns the GtkBuilder XML structure if any, NULL otherwise
106  */
107 static GtkBuilder *load_xml_if_it_exists(gchar *file_to_load)
108 {
109  struct stat *stat_buf;
110  GtkBuilder *xml = NULL;
111 
112  stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
113 
114  stat(file_to_load, stat_buf);
115  if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
116  {
117  GError* error = NULL;
118  xml = gtk_builder_new ();
119 
120  if (!gtk_builder_add_from_file(xml, file_to_load, &error))
121  {
122  g_warning (Q_("Couldn't load builder file: %s"), error->message);
123  g_error_free (error);
124  }
125  }
126  else
127  {
128  xml = NULL;
129  }
130 
131  g_free(stat_buf);
132 
133  return xml;
134 }
135 
136 
137 /**
138  * loads the GtkBuilder xml file ('filename') that describes an interface,
139  * tries all the paths defined in the location_list and put the definition
140  * in the 'xml' variable. A frontend to load_xml_if_it_exists function
141  * @param location_list : a Glist containing paths where we might found the file
142  * @param filename : GtkBuilder filename that we want to load (possibly)
143  * @return returns the GtkBuilder XML structure if any, NULL otherwise
144  */
145 GtkBuilder *load_xml_file(GList *location_list, gchar *filename)
146 {
147  gchar *file_to_load = NULL;
148  GList *list = g_list_first(location_list);
149  GtkBuilder *xml = NULL;
150 
151  while (list != NULL && xml == NULL)
152  {
153  file_to_load = g_build_filename((gchar *) list->data, filename, NULL);
154 
155  xml = load_xml_if_it_exists(file_to_load);
156 
157  if (xml == NULL)
158  {
159  list = list->next;
160  }
161  g_free(file_to_load);
162  }
163 
164  return xml;
165 }
166 
167 
168 /**
169  * Load the preference file from the disk
170  * @param main_struct : main structure
171  * @return TRUE if everything went ok, FALSE otherwise
172  */
174 {
175  if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
176  {
177  return g_key_file_load_from_file(prefs->file, prefs->filename, G_KEY_FILE_KEEP_COMMENTS & G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
178  }
179  else
180  {
181  return FALSE;
182  }
183 }
184 
185 
186 /**
187  * Saves the preferences to the file preferences
188  * @param prefs : preferences (from prefs_t structure)
189  * @return TRUE if everything went ok, FALSE otherwise
190  */
192 {
193  gsize length = 0;
194  gchar *contents = NULL;
195  gboolean result = FALSE;
196 
197  if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
198  {
199  contents = g_key_file_to_data(prefs->file, &length, NULL);
200  result = g_file_set_contents(prefs->filename, contents, length, NULL);
201  g_free(contents);
202  }
203 
204  return result;
205 }
This is the main structure.
Definition: libheraia.h:332
void add_new_tab_in_main_window(heraia_struct_t *main_struct, doc_t *doc)
Adds a new tab to the main window in file's notebook.
Definition: heraia_ui.c:2547
void log_message(heraia_struct_t *main_struct, GLogLevelFlags log_level, const char *format,...)
A function that helps logging a message a the specified level.
Definition: log.c:195
GtkBuilder * load_xml_file(GList *location_list, gchar *filename)
loads the GtkBuilder xml file ('filename') that describes an interface, tries all the paths defined i...
Definition: heraia_io.c:145
gchar * filename
user preference file file name
Definition: libheraia.h:282
Proposal for a structure that will group all informations about a single document.
Definition: libheraia.h:293
void grey_main_widgets(GtkBuilder *xml, gboolean greyed)
Hides or grey all widgets that needs an open file when boolean greyed is TRUE.
Definition: heraia_ui.c:1601
xml_t * xmls
All the xmls used in the program, loaded at running time.
Definition: libheraia.h:337
Data type related to preferences.
Definition: libheraia.h:280
gboolean load_preference_file(prefs_t *prefs)
Load the preference file from the disk.
Definition: heraia_io.c:173
GKeyFile * file
preference file contents
Definition: libheraia.h:284
static GtkBuilder * load_xml_if_it_exists(char *file_to_load)
gboolean save_preferences_to_file(prefs_t *prefs)
Saves the preferences to the file preferences.
Definition: heraia_io.c:191
doc_t * heraia_hex_document_new(heraia_struct_t *main_struct, char *filename)
Removes the old document if it exists and adds a new one from the filename 'filename'.
GtkWidget * hex_widget
hexwidget corresponding to the document
Definition: libheraia.h:296
void update_main_window_name(heraia_struct_t *main_struct)
Update main window heraia's name to reflect the current edited file.
Definition: heraia_ui.c:1434
GtkBuilder * main
the main interface xml description
Definition: libheraia.h:222
This file contains all the definitions and includes all other .h files.
gboolean load_file_to_analyse(heraia_struct_t *main_struct, gchar *filename)
Loads the file 'filename' to analyse and populates the corresponfing structure 'main_struct' as neede...
Definition: heraia_io.c:40