Exemple #1
0
/* Parse part of a line in the xdg-user-dir config file.
 * i.e. XDG_PICTURES_DIR="$HOME/Pictures"
 *                        ^             ^
 *
 * The calling function is responsible for freeing all elements of out_ptr as
 * well as out_ptr itself.
 *
 * [in] xdg_dirs - array of xdg directories to look for
 * [in] num_dirs - number of elements in xdg_dirs
 * [out] out_ptr - an array of the xdg directories names
 */
HRESULT XDG_UserDirLookup(const char * const *xdg_dirs, const unsigned int num_dirs, char *** out_ptr)
{
    FILE *file;
    char **out;
    char *home_dir, *config_file;
    char buffer[512];
    int len;
    unsigned int i;
    HRESULT hr;

    *out_ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_dirs * sizeof(char *));
    out = *out_ptr;
    if (!out)
        return E_OUTOFMEMORY;

    home_dir = getenv("HOME");
    if (!home_dir)
    {
        hr = E_FAIL;
        goto xdg_user_dir_lookup_error;
    }

    hr = get_xdg_config_file(home_dir, &config_file);
    if (FAILED(hr))
        goto xdg_user_dir_lookup_error;

    file = fopen(config_file, "r");
    HeapFree(GetProcessHeap(), 0, config_file);
    if (!file)
    {
        hr = E_HANDLE;
        goto xdg_user_dir_lookup_error;
    }

    while (fgets(buffer, sizeof(buffer), file))
    {
        int idx;
        char *p;

        /* Remove newline at end */
        len = strlen(buffer);
        if (len > 0 && buffer[len-1] == '\n')
            buffer[len-1] = 0;

        /* Parse the key */
        p = buffer;
        idx = parse_config1(xdg_dirs, num_dirs, &p);
        if (idx < 0)
            continue;
        if (out[idx])
            continue;

        /* Parse the value */
        hr = parse_config2(p, home_dir, &out[idx]);
        if (FAILED(hr))
        {
            if (hr == E_OUTOFMEMORY)
            {
                fclose(file);
                goto xdg_user_dir_lookup_error;
            }
            continue;
        }
    }
    fclose (file);
    hr = S_OK;

    /* Remove entries for directories that do not exist */
    for (i = 0; i <  num_dirs; i++)
    {
        struct stat statFolder;

        if (!out[i])
            continue;
        if (!stat(out[i], &statFolder) && S_ISDIR(statFolder.st_mode))
            continue;
        HeapFree(GetProcessHeap(), 0, out[i]);
        out[i] = NULL;
    }

xdg_user_dir_lookup_error:
    if (FAILED(hr))
    {
        for (i = 0; i < num_dirs; i++) HeapFree(GetProcessHeap(), 0, out[i]);
        HeapFree(GetProcessHeap(), 0, *out_ptr);
    }
    return hr;
}
Exemple #2
0
/* Create the ardesia bar window. */
GtkWidget *
create_bar_window (CommandLine *commandline,
                   GtkWidget   *parent)
{
  GtkWidget *bar_window = (GtkWidget *) NULL;
  BarData *bar_data = (BarData *) NULL;
  GError *error = (GError *) NULL;
  gchar *file = UI_FILE;
  gint x = 0;
  gint y = 0;
  gint width = 0;
  gint height = 0;


  /* Set up style for ardesia */
  gchar* gtkcss_file = get_xdg_config_file("ardesia/gtk.css");
  if (gtkcss_file)
    {
      GtkCssProvider *css = gtk_css_provider_new ();
      gtk_css_provider_load_from_path (css, gtkcss_file, NULL);
      g_free (gtkcss_file);
      gtk_style_context_add_provider_for_screen (gdk_screen_get_default(),
                                                 GTK_STYLE_PROVIDER(css),
                                                 GTK_STYLE_PROVIDER_PRIORITY_USER);
    }
    
  bar_gtk_builder = gtk_builder_new ();

  if (commandline->position>2)
    {
      /* North or south. */
      file = UI_HOR_FILE;
    }
  else
    {

      /* East or west. */
      if (gdk_screen_height () < 720)
        {
          /* 
           * The bar is too long and then I use an horizontal layout;
           * this is done to have the full bar for net book and screen
           * with low vertical resolution.
           */
          file = UI_HOR_FILE;
          commandline->position=NORTH;
        }

    }


  /* Load the bar_gtk_builder file with the definition of the ardesia bar gui. */
  gtk_builder_add_from_file (bar_gtk_builder, file, &error);
  if (error)
    {
      g_warning ("Failed to load builder file: %s", error->message);
      g_error_free (error);
      g_object_unref (bar_gtk_builder);
      bar_gtk_builder = NULL;
      return bar_window;
    }

  bar_data = init_bar_data ();

  bar_window = GTK_WIDGET (gtk_builder_get_object (bar_gtk_builder, BAR_WIDGET_NAME));
  gtk_widget_set_name (bar_window, BAR_WIDGET_NAME);
    
  /* Connect all the callback from bar_gtk_builder xml file. */
  gtk_builder_connect_signals (bar_gtk_builder, (gpointer) bar_data);

  //gtk_window_set_transient_for (GTK_WINDOW (bar_window), GTK_WINDOW (parent));

  if (commandline->decorated)
    {
      gtk_window_set_decorated (GTK_WINDOW (bar_window), TRUE);
    }

  gtk_window_get_size (GTK_WINDOW (bar_window) , &width, &height);

  /* x and y will be the bar left corner coordinates. */
  calculate_initial_position (bar_window,
                              &x,
                              &y,
                              width,
                              height,
                              commandline->position);

  /* The position is calculated respect the top left corner
   * and then I set the north west gravity. 
   */
  gtk_window_set_gravity (GTK_WINDOW (bar_window), GDK_GRAVITY_NORTH_WEST);

  /* Move the window in the desired position. */
  gtk_window_move (GTK_WINDOW (bar_window), x, y);


  return bar_window;
}