Re: [Nautilus-list] [PATCH] creating launchers with Nautilus



> >>> +    launcher_dialog (path, fm_directory_view_get_containing_window
> >> (FM_DIRECTORY_VIEW (callback_data)), NULL);
> >>
> >> It's generally considered bad style to pass in a string that's "adopted" by
> >> the function you're calling. If the path is not "adopted", then you have a
> >> storage leak here, and need a g_free.
> >
> > I fixed the two others. This one, I'm not really sure of, since I'm mostly
> > a Java-programmer. What is the correct way of solving this?
>
> Adding a call to g_free (path) to get rid of the path after using it. But
> also you need to put those other files where I can see them, because this
> depends on how your "launcher_dialog" function works.

I just attached them here.

Gaute
#include <stdio.h>
#include <string.h>
#include <libgnome/libgnome.h>
#include <libgnome/gnome-desktop-item.h>
#include <libgnomeui/libgnomeui.h>
#include <libgnomeui/gnome-ditem-edit.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnome/gnome-desktop-item.h>
#include <libnautilus-private/nautilus-file-utilities.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
#include "fm-directory-view.h"
#include "nautilus-launcher-dialog.h"

char * current_path = NULL;

gboolean 
launcher_save_to_file (GtkWidget *dialog, gpointer data);

/* Tries to save the desktop file. Returns TRUE if ok, FALSE if not.
 * It will overwrite existing desktop files if the same name is given.
 */

gboolean 
launcher_save_to_file (GtkWidget *dialog, gpointer data) 
{
  GError * error;
  gboolean return_value;
  GnomeDesktopItem *item;
  char * filename;
  char * location;
  GtkDialog *error_dialog;
 
  GnomeDItemEdit *dedit = GNOME_DITEM_EDIT (data);
  error = NULL;
  item = gnome_ditem_edit_get_ditem (dedit);
  filename = gnome_ditem_edit_get_name (dedit);

  /* If we have no file name, we'll use a default */
  if (strlen(filename) < 1) {
    filename = "No name";
    gnome_desktop_item_set_string (item, GNOME_DESKTOP_ITEM_NAME, _(filename));
  }

  location = nautilus_make_path (current_path, filename);
  g_free (current_path);
  
  /* Do the actual saving */
  return_value = gnome_desktop_item_save (item, location, TRUE, &error);
  
  if (!return_value) {
    error_dialog = eel_show_error_dialog (error->message, (char *) _("Error creating launcher"), GTK_WINDOW (dialog));
    g_error_free (error);
    return FALSE;
  }
  else {
    nautilus_directory_force_reload (nautilus_directory_get (location));
    g_error_free (error);
    gtk_widget_destroy (GTK_WIDGET (dialog));
    return TRUE;
  }
}

void 
callback_handler (GtkWidget *dialog, int response, gpointer data);

void
callback_handler (GtkWidget *dialog, int response, gpointer data) 
{
  switch (response) {
  case GTK_RESPONSE_OK:
    launcher_save_to_file (dialog, data);
    break;
  case GTK_RESPONSE_CANCEL:
    gtk_widget_destroy (dialog);
    break;
  case GTK_RESPONSE_HELP: eel_show_error_dialog ("No help available yet", (char *) _("No help"), GTK_WINDOW (dialog));
    break;
  }
}

void
launcher_dialog ( char * path, GtkWindow * parent, char * old_launcher)
{
  GtkWidget *dialog;
  GnomeDItemEdit *dee;

  GnomeDesktopItem *ditem = NULL;
  GError * error = NULL;
  current_path = g_malloc(strlen(path) + 1);
  strcpy(current_path, path);
  g_free (path);
  dialog = gtk_dialog_new_with_buttons (_("Create launcher"),
					parent /* parent */,
					0 /* flags */,
					GTK_STOCK_HELP,
					GTK_RESPONSE_HELP,
					GTK_STOCK_CANCEL,
					GTK_RESPONSE_CANCEL,
					GTK_STOCK_OK,
					GTK_RESPONSE_OK,
					NULL);
  dee = GNOME_DITEM_EDIT (gnome_ditem_edit_new ());
  if (old_launcher != NULL) {
    ditem = gnome_desktop_item_new_from_uri (gnome_vfs_get_local_path_from_uri (old_launcher), GNOME_DESKTOP_ITEM_LOAD_ONLY_IF_EXISTS, &error);
    
    if (ditem == NULL) {
      eel_show_error_dialog (error->message, (char *) _("Error opening launcher"), GTK_WINDOW (parent));
      g_error_free (error);
    }
  }
  else {
    ditem = gnome_desktop_item_new ();
  }
  
  gnome_ditem_edit_set_editable (dee, TRUE);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
		      GTK_WIDGET (dee),
		      TRUE, TRUE, GNOME_PAD_SMALL);


  gnome_desktop_item_set_entry_type (ditem, GNOME_DESKTOP_ITEM_TYPE_APPLICATION);

  gnome_ditem_edit_set_ditem (dee, ditem);
	
  gnome_desktop_item_unref (ditem);
	
  g_signal_connect (G_OBJECT (dialog), "response",
		    G_CALLBACK (callback_handler),
		    dee);

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

  gtk_widget_show_all (dialog);

  gnome_ditem_edit_grab_focus (dee);
}


#include <libgnome/gnome-desktop-item.h>

G_BEGIN_DECLS

void
launcher_dialog ( char * path, GtkWindow * parent, char * old_launcher );




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]