From 15b3192d83def40ce226db04197dd6d08e9ad7e9 Mon Sep 17 00:00:00 2001 From: Jasper Huijsmans Date: Thu, 27 Dec 2007 22:36:41 +0100 Subject: [PATCH 1/4] Refactoring dnd on config dialog. Add separate source file and header. No functional change. --- plugins/launcher/Makefile.am | 4 +- plugins/launcher/launcher-dialog.c | 102 +----------------------- plugins/launcher/launcher-dialog.h | 2 + plugins/launcher/launcher-dnd.c | 148 ++++++++++++++++++++++++++++++++++++ plugins/launcher/launcher-dnd.h | 29 +++++++ 5 files changed, 187 insertions(+), 98 deletions(-) diff --git a/plugins/launcher/Makefile.am b/plugins/launcher/Makefile.am index 1d6fc62..27260ac 100644 --- a/plugins/launcher/Makefile.am +++ b/plugins/launcher/Makefile.am @@ -21,7 +21,9 @@ liblauncher_la_SOURCES = \ launcher-exec.h \ launcher-exec.c \ launcher-dialog.h \ - launcher-dialog.c + launcher-dialog.c \ + launcher-dnd.h \ + launcher-dnd.c liblauncher_la_CFLAGS = \ $(GTK_CFLAGS) \ diff --git a/plugins/launcher/launcher-dialog.c b/plugins/launcher/launcher-dialog.c index 196ca41..2bb03fd 100644 --- a/plugins/launcher/launcher-dialog.c +++ b/plugins/launcher/launcher-dialog.c @@ -34,7 +34,7 @@ #include "launcher.h" #include "launcher-dialog.h" - +#include "launcher-dnd.h" enum { @@ -42,8 +42,6 @@ enum COLUMN_NAME }; -typedef struct _LauncherDialog LauncherDialog; - struct _LauncherDialog { LauncherPlugin *launcher; @@ -89,8 +87,6 @@ struct _LauncherDialog **/ static void launcher_dialog_g_list_swap (GList *li_a, GList *li_b); -static gboolean launcher_dialog_read_desktop_file (const gchar *file, - LauncherEntry *entry); static void launcher_dialog_tree_drag_data_received (GtkWidget *widget, GdkDragContext *context, gint x, @@ -147,93 +143,6 @@ launcher_dialog_g_list_swap (GList *li_a, } -static gboolean -launcher_dialog_read_desktop_file (const gchar *path, - LauncherEntry *entry) -{ - XfceRc *rc = NULL; - const gchar *value = NULL; - const gchar *p; - - /* we only support .desktop files */ - if (G_UNLIKELY (g_str_has_suffix (path, ".desktop") == FALSE || - g_path_is_absolute (path) == FALSE)) - return FALSE; - - /* open de .desktop file */ - rc = xfce_rc_simple_open (path, TRUE); - if (G_UNLIKELY (rc == NULL)) - return FALSE; - - /* set the desktop entry group */ - xfce_rc_set_group (rc, "Desktop Entry"); - - /* name */ - value = xfce_rc_read_entry (rc, "Name", NULL); - if (G_LIKELY (value != NULL)) - { - g_free (entry->name); - entry->name = g_strdup (value); - } - - /* comment */ - value = xfce_rc_read_entry (rc, "Comment", NULL); - if (G_LIKELY (value != NULL)) - { - g_free (entry->comment); - entry->comment = g_strdup (value); - } - - /* icon */ - value = xfce_rc_read_entry_untranslated (rc, "Icon", NULL); - if (G_LIKELY (value != NULL)) - { - g_free (entry->icon); - - /* get rid of extensions in non-absolute names */ - if (G_UNLIKELY (g_path_is_absolute (value) == FALSE) && - ((p = g_strrstr (value, ".")) && strlen (p) < 6)) - entry->icon = g_strndup (value, p-value); - else - entry->icon = g_strdup (value); - } - - /* exec */ - value = xfce_rc_read_entry_untranslated (rc, "Exec", NULL); - if (G_LIKELY (value != NULL)) - { - g_free (entry->exec); - - /* expand variables and store */ - entry->exec = value ? xfce_expand_variables (value, NULL) : NULL; - } - - /* working directory */ - value = xfce_rc_read_entry_untranslated (rc, "Path", NULL); - if (G_UNLIKELY (value != NULL)) - { - g_free (entry->path); - - /* expand variables and store */ - entry->path = value ? xfce_expand_variables (value, NULL) : NULL; - } - - /* terminal */ - entry->terminal = xfce_rc_read_bool_entry (rc, "Terminal", FALSE); - -#ifdef HAVE_LIBSTARTUP_NOTIFICATION - /* startup notification */ - entry->startup = xfce_rc_read_bool_entry (rc, "StartupNotify", FALSE); -#endif - - /* release rc file */ - xfce_rc_close (rc); - - return TRUE; -} - - - static void launcher_dialog_tree_drag_data_received (GtkWidget *widget, GdkDragContext *context, @@ -309,8 +218,7 @@ launcher_dialog_tree_drag_data_received (GtkWidget *widget, /* create new entry */ entry = launcher_entry_new (); - /* try to parse desktop file */ - if (G_LIKELY (launcher_dialog_read_desktop_file (file, entry) == TRUE)) + if (G_LIKELY (launcher_dnd_update_entry (file, entry, ld)) == TRUE) { /* insert new row in store */ if (insert_before) @@ -395,7 +303,7 @@ launcher_dialog_frame_drag_data_received (GtkWidget *widget, file = (gchar *) li->data; /* try to update the current entry settings */ - if (G_LIKELY (launcher_dialog_read_desktop_file (file, ld->entry) == TRUE)) + if (G_LIKELY (launcher_dnd_update_entry (file, ld->entry, ld)) == TRUE) { /* update the widgets */ launcher_dialog_update_entries (ld); @@ -1453,7 +1361,7 @@ launcher_dialog_response (GtkWidget *dialog, gtk_list_store_clear (ld->store); g_object_unref (G_OBJECT (ld->store)); - /* the launcher dialog dataS */ + /* the launcher dialog data */ g_object_set_data (G_OBJECT (launcher->panel_plugin), I_("launcher-dialog"), NULL); /* destroy the dialog */ @@ -1532,7 +1440,7 @@ launcher_dialog_show (LauncherPlugin *launcher) gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); /* connect dialog to plugin, so we can destroy it when plugin is closed */ - g_object_set_data (G_OBJECT (ld->launcher->panel_plugin), "dialog", dialog); + g_object_set_data (G_OBJECT (ld->launcher->panel_plugin), I_("launcher-dialog"), dialog); dialog_vbox = GTK_DIALOG (dialog)->vbox; diff --git a/plugins/launcher/launcher-dialog.h b/plugins/launcher/launcher-dialog.h index d7dcc25..95ad118 100644 --- a/plugins/launcher/launcher-dialog.h +++ b/plugins/launcher/launcher-dialog.h @@ -21,6 +21,8 @@ #ifndef __XFCE_PANEL_LAUNCHER_DIALOG_H__ #define __XFCE_PANEL_LAUNCHER_DIALOG_H__ +typedef struct _LauncherDialog LauncherDialog; + void launcher_dialog_show (LauncherPlugin *launcher) G_GNUC_INTERNAL; #endif /* !__XFCE_PANEL_LAUNCHER_DIALOG_H__ */ diff --git a/plugins/launcher/launcher-dnd.c b/plugins/launcher/launcher-dnd.c new file mode 100644 index 0000000..ced1e81 --- /dev/null +++ b/plugins/launcher/launcher-dnd.c @@ -0,0 +1,148 @@ +/* $Id$ + * + * Copyright (c) 2007 Jasper Huijsmans + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include "launcher.h" +#include "launcher-dnd.h" + +/* + * Inline file type tests + */ + +/* .desktop files */ +static inline gboolean +launcher_dnd_test_desktop_file (const char *file) +{ + return (g_str_has_suffix (file, ".desktop")); +} + +/* + * Update LauncherEntry + */ + +/* .desktop files */ +static gboolean +launcher_dnd_handle_desktop_file (const char *file, + LauncherEntry *entry) +{ + XfceRc *rc = NULL; + const gchar *value = NULL; + const gchar *p; + + rc = xfce_rc_simple_open (file, TRUE); + if (G_UNLIKELY (rc == NULL)) + return FALSE; + + /* set the desktop entry group */ + xfce_rc_set_group (rc, "Desktop Entry"); + + /* name */ + value = xfce_rc_read_entry (rc, "Name", NULL); + if (G_LIKELY (value != NULL)) + { + g_free (entry->name); + entry->name = g_strdup (value); + } + + /* comment */ + value = xfce_rc_read_entry (rc, "Comment", NULL); + if (G_LIKELY (value != NULL)) + { + g_free (entry->comment); + entry->comment = g_strdup (value); + } + + /* icon */ + value = xfce_rc_read_entry_untranslated (rc, "Icon", NULL); + if (G_LIKELY (value != NULL)) + { + g_free (entry->icon); + + /* get rid of extensions in non-absolute names + * (we only care about png and svg)*/ + if (G_UNLIKELY (g_path_is_absolute (value) == FALSE) && + (p = g_strrstr (value, ".")) != NULL && + (g_ascii_strcasecmp("png", (p+1)) == 0 || + g_ascii_strcasecmp("svg", (p+1)) == 0)) + { + entry->icon = g_strndup (value, p-value); + } + else + { + entry->icon = g_strdup (value); + } + } + + /* exec */ + value = xfce_rc_read_entry_untranslated (rc, "Exec", NULL); + if (G_LIKELY (value != NULL)) + { + g_free (entry->exec); + + /* expand variables and store */ + entry->exec = xfce_expand_variables (value, NULL); + } + + /* working directory */ + value = xfce_rc_read_entry_untranslated (rc, "Path", NULL); + if (G_UNLIKELY (value != NULL)) + { + g_free (entry->path); + + /* expand variables and store */ + entry->path = xfce_expand_variables (value, NULL); + } + + /* terminal */ + entry->terminal = xfce_rc_read_bool_entry (rc, "Terminal", FALSE); + +#ifdef HAVE_LIBSTARTUP_NOTIFICATION + /* startup notification */ + entry->startup = xfce_rc_read_bool_entry (rc, "StartupNotify", FALSE); +#endif + + /* release rc file */ + xfce_rc_close (rc); + + return TRUE; +} + +/* + * Public Interface + */ +gboolean +launcher_dnd_update_entry (const char *file, + LauncherEntry *entry, + LauncherDialog *ld) +{ + gboolean entry_created = FALSE; + + DBG("filename: %s", file); + + if (launcher_dnd_test_desktop_file (file)) + { + /* .desktop file */ + entry_created = launcher_dnd_handle_desktop_file (file, entry); + + DBG("create launcher from desktop file: %s\n", entry_created ? "success" : "failed"); + } + return entry_created; +} + + diff --git a/plugins/launcher/launcher-dnd.h b/plugins/launcher/launcher-dnd.h new file mode 100644 index 0000000..53a32c1 --- /dev/null +++ b/plugins/launcher/launcher-dnd.h @@ -0,0 +1,29 @@ +/* $Id$ + * + * Copyright (c) 2007 Jasper Huijsmans + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __LAUNCHER_DND_H__ +#define __LAUNCHER_DND_H_ + +#include "launcher-dialog.h" + +gboolean launcher_dnd_update_entry (const char *file, + LauncherEntry *entry, + LauncherDialog *ld); +#endif /* __LAUNCHER_DND_H_ */ + -- 1.5.3.7