src/application-data.h File Reference

Header for the application's data types and functions. More...

#include <dbus/dbus-glib-bindings.h>
#include <gtk/gtk.h>
#include <glib.h>
#include "data-types.h"
#include "frame-common.h"
#include "frame-table.h"
#include "settings-manager.h"
Include dependency graph for application-data.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  synema_instance_t
 Data structure containing all the variables needed to run Synema. More...

Functions

void error_dialog (const gchar *,...)
 Displays an error dialog.
void toggle_panels (GtkToolButton *, gpointer)
 Toggles the visibility of the side panel This function toggles the visibility of the side panel containing the log player, or the open frames panel.
gint post_init_restore_frames ()
 Restores frames previously saved to files. Part of the app's init process.
void pre_quit_save_frames (GtkObject *, gpointer)
 Saves the frames if needed, then quits the GTK main loop.
gint recursive_remove (const gchar *)
 Recursively removes a file or folder pointed by path.
guint unique_frame_id_generator (GList *)
 Returns a number that can be used to identify a frame among a list.
synema_instance_tsynema_instance ()
 Returns a pointer to the current Synema instance.
void synema_instance_free ()
 Frees the current Synema instance.

Detailed Description

Header for the application's data types and functions.

Author:
Steve Dodier <sidnioulz@gmail.com>

This file contains data types describing a Synema instance.

Definition in file application-data.h.


Function Documentation

void error_dialog ( const gchar *  message,
  ... 
)

Displays an error dialog.

This function displays an error dialog, and takes as a parameter the message to display, in the same way as printf and affiliated functions.

Parameters:
[in] message the printf-like format of the message to display
[in] ... the parameters matching message's format

Definition at line 57 of file application-data.c.

00058 {
00059     synema_instance_t   *inst   = synema_instance ();
00060     gchar               *buffer = NULL;
00061     GtkWindow           *parent = NULL;
00062     va_list             args;
00063     
00064     parent = GTK_WINDOW (gtk_builder_get_object (inst->builder, "synema_window"));
00065 
00066     va_start (args, message);
00067     buffer = g_strdup_vprintf (message, args);
00068     va_end (args);
00069 
00070     _error_dialog (parent, buffer);
00071     g_free (buffer);
00072 }

gint post_init_restore_frames (  ) 

Restores frames previously saved to files. Part of the app's init process.

This function is called after the Synema instance has been created. It searches for previously saved frames and restores them.

Returns:
0 if restoring succeeded, another value otherwise

Definition at line 129 of file application-data.c.

00130 {
00131     synema_instance_t   *inst           = synema_instance ();
00132     frame_table_t       *tmptab         = NULL;
00133     gchar               *dirpath        = NULL;
00134     gchar               *subdirpath     = NULL;
00135     gchar               *ptr            = NULL;
00136     GError              *err            = NULL;
00137     GFile               *dirfile        = NULL;
00138     GFileEnumerator     *direnum        = NULL;
00139     GFileInfo           *current        = NULL;
00140     GFileInfo           *dirinfo        = NULL;
00141     GList               *iter           = NULL;
00142     GtkWidget           *frame_box      = NULL;
00143 
00144     frame_box = (GtkWidget *) gtk_builder_get_object (inst->builder, "notebook");
00145 
00146     dirpath = g_strdup_printf ("%s/"SAVED_FRAMES_DIR, inst->config_dir);
00147     dirfile = g_file_new_for_path (dirpath);
00148 
00149     dirinfo = g_file_query_info (dirfile, "standard::type", G_FILE_QUERY_INFO_NONE, NULL, &err);
00150     if (err) {
00151         //g_warning ("frame_table_load_from_files: Error while querying the "SAVED_FRAMES_DIR" file (%s)", err->message);
00152         // This may happen if the file doesn't exist, in which case we just don't restore any frame
00153         g_clear_error (&err);
00154         g_object_unref (dirfile);
00155         g_free (dirpath);
00156         return -1;
00157     }
00158     if (g_file_info_get_file_type (dirinfo) != G_FILE_TYPE_DIRECTORY) {
00159         // This is not an error, we just can't restore frames since there is no saved frames directory
00160         g_free (dirpath);
00161         return -1;
00162     }
00163     g_object_unref (dirinfo);
00164 
00165     direnum = g_file_enumerate_children (dirfile, "standard::type,standard::name", G_FILE_QUERY_INFO_NONE, NULL, &err);
00166     if (err) {
00167         g_warning ("post_init_restore_frames: Error while reading the "SAVED_FRAMES_DIR" directory (%s)", err->message);
00168         g_free (dirpath);
00169         g_clear_error (&err);
00170         g_object_unref (dirfile);
00171         return -1;
00172     }
00173 
00174 
00175     // Let's fetch the tabs
00176     while ((current = g_file_enumerator_next_file (direnum, NULL, &err)) != NULL) {
00177         if (err) {
00178             g_warning ("post_init_restore_frames: Error while reading a subdirectory of "SAVED_FRAMES_DIR" (%s)", err->message);
00179             g_clear_error (&err);
00180         } else {
00181             if (g_file_info_get_file_type (current) == G_FILE_TYPE_DIRECTORY) {
00182                 subdirpath = g_strdup_printf ("%s/%s", dirpath, g_file_info_get_name (current));
00183                 ptr = g_strstr_len (g_strrstr (subdirpath, "/"), -1, "__");
00184                 if (!ptr) {
00185                     g_warning ("post_init_restore_frames: Malformed directory name %s", subdirpath);
00186                 } else {
00187                     tmptab = _notebook_add_page (frame_box, ptr + 2);
00188                     if (frame_table_load_from_files (subdirpath, tmptab) != 0) {
00189                         notebook_remove_page (NULL, tmptab);
00190                     }
00191                 }
00192                 g_free (subdirpath);
00193             }
00194             g_object_unref (current);
00195         }
00196     }
00197 
00198     g_free (dirpath);
00199     g_object_unref (direnum);
00200     g_object_unref (dirfile);
00201 
00202 
00203     // Now we can sort the tabs and put them in the right order.
00204     inst->tables_list = g_list_sort (inst->tables_list, frame_table_list_cmp_by_position);
00205     if (inst->tables_list) {
00206         iter = inst->tables_list;
00207         do {
00208             tmptab = iter->data;
00209             gtk_notebook_reorder_child (GTK_NOTEBOOK (frame_box), tmptab->table_box, tmptab->position);
00210         } while ((iter = g_list_next (iter)) != NULL);
00211     }
00212 
00213 
00214     // We set the tab counter to the current number of pages to avoid having several "Unnamed X" tabs in most cases
00215     inst->labelcount = gtk_notebook_get_n_pages (GTK_NOTEBOOK (frame_box));
00216 
00217     gtk_notebook_set_current_page (GTK_NOTEBOOK (frame_box), inst->settings->last_current_tab);
00218 
00219     return 0;
00220 }

void pre_quit_save_frames ( GtkObject *  object,
gpointer  user_data 
)

Saves the frames if needed, then quits the GTK main loop.

This function saves the currently opened frames if automatic frame saving is enabled in the settings, and then calls gtk_main_quit ().

Parameters:
[in] object the object whose signal emission triggered the handler
[in] user_data an uninteresting pointer

Definition at line 224 of file application-data.c.

00225 {
00226     synema_instance_t   *inst       = synema_instance ();
00227     frame_table_t       *tab        = NULL;
00228     gchar               *path       = g_strdup_printf ("%s/"SAVED_FRAMES_DIR, inst->config_dir);
00229     GList               *iter       = inst->tables_list;
00230 
00231     inst->settings->last_current_tab = inst->current_table->position;
00232 
00233     if (recursive_remove (path))
00234         g_warning ("pre_quit_save_frames: Could not fully remove previously saved frames.");
00235 
00236     if ((iter) && (inst->settings->autosave_frames)) {
00237         do {
00238             tab = iter->data;
00239             frame_table_save_to_files (path, tab);
00240         } while ((iter = g_list_next (iter)) != NULL);
00241     }
00242 
00243     g_free (path);
00244     gtk_main_quit ();
00245 }

gint recursive_remove ( const gchar *  path  ) 

Recursively removes a file or folder pointed by path.

This function removes a file pointed by path, and if it is a folder, removes the files it contains before removing the folder.

Parameters:
[in] path the path of the file or folder to remove
Returns:
0 if no error, -1 otherwise

Definition at line 249 of file application-data.c.

00250 {
00251     gchar               *subpath    = NULL;
00252     GError              *err        = NULL;
00253     GFile               *file       = NULL;
00254     GFileEnumerator     *fileenum   = NULL;
00255     GFileInfo           *current    = NULL;
00256     GFileInfo           *fileinfo   = NULL;
00257 
00258     file = g_file_new_for_path (path);
00259 
00260     if (!g_file_query_exists (file, NULL))
00261         return 0;
00262 
00263     fileinfo = g_file_query_info (file, "standard::type", G_FILE_QUERY_INFO_NONE, NULL, &err);
00264     if (err) {
00265         g_warning ("recursive_remove: Could not get file info for %s (%s)", path, err->message);
00266         g_clear_error (&err);
00267         g_object_unref (file);
00268         return -1;
00269     } else {
00270         if (g_file_info_get_file_type (fileinfo) == G_FILE_TYPE_DIRECTORY) {
00271             fileenum = g_file_enumerate_children (file, "standard::name, standard::type", G_FILE_QUERY_INFO_NONE, NULL, &err);
00272             if (err) {
00273                 g_warning ("recursive_remove: Could not enumerate files in %s (%s)", path, err->message);
00274                 g_clear_error (&err);
00275             } else {
00276                 while ((current = g_file_enumerator_next_file (fileenum, NULL, &err)) != NULL) {
00277                     if (err) {
00278                         g_warning ("recursive_remove: Could not get next file to remove in %s (%s)", path, err->message);
00279                         g_clear_error (&err);
00280                     } else {
00281                         subpath = g_strdup_printf ("%s/%s", path, g_file_info_get_name (current));
00282                         if (recursive_remove (subpath))
00283                             g_warning ("recursive_remove: Could not remove file %s", subpath);
00284                         g_free (subpath);
00285                         g_object_unref (current);
00286                     }
00287                 }
00288             }
00289             g_object_unref (fileenum);
00290         }
00291         g_object_unref (fileinfo);
00292         if (g_remove (path)) {
00293             g_warning ("recursive_remove: Could not remove file %s (%s)", path, g_strerror (errno));
00294             g_object_unref (file);
00295             return -1;
00296         }
00297     }
00298     
00299     g_object_unref (file);
00300     return 0;
00301 }

synema_instance_t* synema_instance (  ) 

Returns a pointer to the current Synema instance.

This function returns a pointer to the current instance of Synema (it is implemented as a synema_instance_t static struct). If the instance doesn't exist, it is created beforehand.

Returns:
the current Synema instance, properly initialised

Definition at line 630 of file application-data.c.

00631 {
00632     return _synema_instance (0);
00633 }

void synema_instance_free (  ) 

Frees the current Synema instance.

Warning:
This function should not be called when the Gtk loop is running.

This function frees the whole current Synema instance, by freeing all of its members before freeing the synema_instance_t struct itself.

Definition at line 637 of file application-data.c.

00638 {
00639     synema_instance_t   *inst   = synema_instance ();
00640 
00641     settings_save (inst->settings);
00642 
00643     if (inst->tables_list) {
00644         GList *iter = inst->tables_list;
00645         do {
00646             frame_table_free (iter->data);
00647         } while ((iter = g_list_next (iter)) != NULL);
00648     }
00649     g_list_free (inst->tables_list);
00650 
00651     g_rand_free (inst->rand);
00652     machine_list_free (inst->machines_list);
00653     plugin_list_free (inst->plugins_list);
00654     g_object_unref (inst->builder);
00655     g_free (inst->settings);
00656 
00657     recursive_remove (inst->tmp_dir);
00658 
00659     g_free (inst->tmp_dir);
00660     g_free (inst->data_dir);
00661     g_free (inst->config_dir);
00662     g_free (inst->machines_dir);
00663     g_free (inst->plugins_dir);
00664     
00665 
00666     g_free (inst);
00667     _synema_instance (1); // set the instance to NULL
00668 }

void toggle_panels ( GtkToolButton *  button,
gpointer  user_data 
)

Toggles the visibility of the side panel This function toggles the visibility of the side panel containing the log player, or the open frames panel.

Parameters:
[in] button the button that was clicked to trigger the handler
[in] user_data an identifier to tell which panel to show

Definition at line 76 of file application-data.c.

00077 {
00078     synema_instance_t   *inst       = synema_instance ();
00079     gint                paneltype   = GPOINTER_TO_INT (user_data);
00080     GtkWidget           *panel      = (GtkWidget *) gtk_builder_get_object (inst->builder, "top_panel");
00081     GtkWidget           *top_box    = (GtkWidget *) gtk_builder_get_object (inst->builder, "top_box");
00082     GtkWidget           *frame_box  = (GtkWidget *) gtk_builder_get_object (inst->builder, "notebook");
00083     GtkWidget           *pn_open    = (GtkWidget *) gtk_builder_get_object (inst->builder, "open_vbox");
00084     GtkWidget           *pn_actions = (GtkWidget *) gtk_builder_get_object (inst->builder, "actions_vbox");
00085     GtkWidget           *pn_player  = (GtkWidget *) gtk_builder_get_object (inst->builder, "player_vbox");
00086 
00087 
00088     // We just show the asked panel here
00089     if (paneltype == PANEL_PLAYER) {
00090         gtk_widget_show (pn_player);
00091         gtk_widget_hide (pn_open);
00092         gtk_widget_hide (pn_actions);
00093     } else if (paneltype == PANEL_OPEN) {
00094         gtk_widget_show (pn_open);
00095         gtk_widget_hide (pn_player);
00096         gtk_widget_hide (pn_actions);
00097     } else if (paneltype == PANEL_ACTIONS) {
00098         gtk_widget_show (pn_actions);
00099         gtk_widget_hide (pn_player);
00100         gtk_widget_hide (pn_open);
00101     }
00102 
00103 
00104     // If the panel was hidden we move the necessary widgets to make it visible
00105     if (gtk_widget_get_parent (frame_box) != panel) {
00106         g_object_ref (frame_box);
00107         gtk_container_remove (GTK_CONTAINER (top_box), frame_box);
00108         gtk_container_add (GTK_CONTAINER (top_box), panel);
00109         gtk_paned_add2 (GTK_PANED (panel), frame_box);
00110         g_object_unref (frame_box);
00111         g_object_unref (panel);
00112     }
00113     // If we toggle the current panel the we must hide it
00114     else if (paneltype == inst->current_panel) {
00115         g_object_ref (panel);
00116         g_object_ref (frame_box);
00117         gtk_container_remove (GTK_CONTAINER (panel), frame_box);
00118         gtk_container_remove (GTK_CONTAINER (top_box), panel);
00119         gtk_container_add (GTK_CONTAINER (top_box), frame_box);
00120         g_object_unref (frame_box);
00121     }
00122 
00123     // We set the current panel to the last toggled one
00124     inst->current_panel = paneltype;
00125 }

guint unique_frame_id_generator ( GList *  list  ) 

Returns a number that can be used to identify a frame among a list.

This function returns a number that can be used as an identifier for a frame that is being added to a list, that must be given as a parameter. The function will pick a random number and make sure no other frame uses it.

Parameters:
[in] list a list of frames that already have an identifier
Returns:
an unique identifier for a member of the list

Definition at line 305 of file application-data.c.

00306 {
00307     synema_instance_t   *inst   = synema_instance ();
00308     frame_t             *f      = NULL;
00309     gboolean            found   = FALSE;
00310     GList               *iter   = list;
00311     guint               val;
00312 
00313     while (!found) {
00314         val = g_rand_int (inst->rand);
00315         found = TRUE;
00316 
00317         if (iter) {
00318             do {
00319                 f = iter->data;
00320                 found = (f->id != val);
00321             } while (((iter = g_list_next (iter)) != NULL) && (found));
00322         }
00323     }
00324 
00325     return val;
00326 }

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Tue Jan 12 00:48:44 2010 for ENSIBSynema by  doxygen 1.6.1