1 /* patch-plugin.c (C) 2002 Johannes Schmid
2 * 2005 Massimo Cora' [ported to the new plugin architetture]
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Library General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <libanjuta/anjuta-launcher.h>
20 #include <libanjuta/anjuta-debug.h>
23 #include "patch-plugin.h"
25 #define GLADE_FILE PACKAGE_DATA_DIR"/glade/patch-plugin.glade"
27 static void patch_level_changed (GtkAdjustment
*adj
);
29 static void on_ok_clicked (GtkButton
*button
, PatchPlugin
* p_plugin
);
30 static void on_cancel_clicked (GtkButton
*button
, PatchPlugin
* plugin
);
32 static void on_msg_arrived (AnjutaLauncher
*launcher
,
33 AnjutaLauncherOutputType output_type
,
34 const gchar
* line
, gpointer data
);
35 static void on_msg_buffer (IAnjutaMessageView
*view
, const gchar
*line
,
38 static void on_patch_terminated (AnjutaLauncher
*launcher
,
39 gint child_pid
, gint status
, gulong time_taken
,
43 static gint patch_level
= 0;
47 patch_level_changed (GtkAdjustment
*adj
)
49 patch_level
= (gint
) adj
->value
;
53 patch_show_gui (PatchPlugin
*plugin
)
61 gxml
= glade_xml_new(GLADE_FILE
, "patch_dialog", NULL
);
63 plugin
->dialog
= glade_xml_get_widget(gxml
, "patch_dialog");
64 plugin
->output_label
= glade_xml_get_widget(gxml
, "output");
66 table
= glade_xml_get_widget(gxml
, "patch_table");
68 plugin
->file_chooser
= gtk_file_chooser_button_new(_("File/Directory to patch"),
69 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
);
70 plugin
->patch_chooser
= gtk_file_chooser_button_new(_("Patch file"),
71 GTK_FILE_CHOOSER_ACTION_OPEN
);
73 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(plugin
->file_chooser
),
75 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(plugin
->patch_chooser
),
79 gtk_table_attach_defaults(GTK_TABLE(table
), plugin
->file_chooser
, 1, 2, 0, 1);
80 gtk_table_attach_defaults(GTK_TABLE(table
), plugin
->patch_chooser
, 1, 2, 1, 2);
82 scale
= glade_xml_get_widget(gxml
, "patch_level_scale");
83 adj
= gtk_range_get_adjustment(GTK_RANGE(scale
));
84 g_signal_connect (G_OBJECT(adj
), "value_changed",
85 GTK_SIGNAL_FUNC (patch_level_changed
), NULL
);
87 plugin
->patch_button
= glade_xml_get_widget(gxml
, "patch_button");
88 plugin
->cancel_button
= glade_xml_get_widget(gxml
, "cancel_button");
89 plugin
->dry_run_check
= glade_xml_get_widget(gxml
, "dryrun");
91 g_signal_connect (G_OBJECT (plugin
->patch_button
), "clicked",
92 G_CALLBACK(on_ok_clicked
), plugin
);
93 g_signal_connect (G_OBJECT (plugin
->cancel_button
), "clicked",
94 G_CALLBACK(on_cancel_clicked
), plugin
);
96 plugin
->executing
= FALSE
;
97 gtk_widget_show_all (plugin
->dialog
);
102 on_ok_clicked (GtkButton
*button
, PatchPlugin
* p_plugin
)
104 const gchar
* directory
;
105 const gchar
* patch_file
;
106 GString
* command
= g_string_new (NULL
);
108 IAnjutaMessageManager
*mesg_manager
;
110 g_return_if_fail (p_plugin
!= NULL
);
112 mesg_manager
= anjuta_shell_get_interface
113 (ANJUTA_PLUGIN (p_plugin
)->shell
, IAnjutaMessageManager
, NULL
);
115 g_return_if_fail (mesg_manager
!= NULL
);
117 p_plugin
->mesg_view
=
118 ianjuta_message_manager_add_view (mesg_manager
, _("Patch"),
121 ianjuta_message_manager_set_current_view (mesg_manager
, p_plugin
->mesg_view
, NULL
);
123 g_signal_connect (G_OBJECT (p_plugin
->mesg_view
), "buffer-flushed",
124 G_CALLBACK (on_msg_buffer
), p_plugin
);
126 directory
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(p_plugin
->file_chooser
));
127 patch_file
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(p_plugin
->patch_chooser
));
129 if (!g_file_test (directory
, G_FILE_TEST_IS_DIR
))
131 g_string_free (command
, TRUE
);
132 anjuta_util_dialog_error(GTK_WINDOW(p_plugin
->dialog
),
133 _("Please select the directory where the patch should be applied"));
137 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(p_plugin
->dry_run_check
)))
138 g_string_sprintf (command
, "patch --dry-run -d %s -p %d -f -i %s",
139 directory
, patch_level
, patch_file
);
141 g_string_sprintf (command
, "patch -d %s -p %d -f -i %s",
142 directory
, patch_level
, patch_file
);
144 message
= g_strdup_printf (_("Patching %s using %s\n"),
145 directory
, patch_file
);
147 ianjuta_message_view_append (p_plugin
->mesg_view
,
148 IANJUTA_MESSAGE_VIEW_TYPE_NORMAL
,
151 ianjuta_message_view_append (p_plugin
->mesg_view
,
152 IANJUTA_MESSAGE_VIEW_TYPE_NORMAL
,
153 _("Patching...\n"), "", NULL
);
155 g_signal_connect (p_plugin
->launcher
, "child-exited",
156 G_CALLBACK (on_patch_terminated
), p_plugin
);
158 if (!anjuta_launcher_is_busy (p_plugin
->launcher
))
160 anjuta_launcher_execute (p_plugin
->launcher
, command
->str
,
161 (AnjutaLauncherOutputCallback
)on_msg_arrived
, p_plugin
);
162 p_plugin
->executing
= TRUE
;
163 gtk_label_set_text(GTK_LABEL(p_plugin
->output_label
), "Patching...");
164 gtk_widget_set_sensitive(p_plugin
->patch_button
, FALSE
);
167 anjuta_util_dialog_error(GTK_WINDOW(p_plugin
->dialog
),
168 _("There are unfinished jobs, please wait until they are finished"));
169 g_string_free(command
, TRUE
);
173 on_cancel_clicked (GtkButton
*button
, PatchPlugin
* plugin
)
175 if (plugin
->executing
== TRUE
)
177 anjuta_launcher_reset(plugin
->launcher
);
179 gtk_widget_hide (GTK_WIDGET(plugin
->dialog
));
180 gtk_widget_destroy (GTK_WIDGET(plugin
->dialog
));
184 on_msg_arrived (AnjutaLauncher
*launcher
,
185 AnjutaLauncherOutputType output_type
,
186 const gchar
* line
, gpointer data
)
188 g_return_if_fail (line
!= NULL
);
190 PatchPlugin
* p_plugin
= ANJUTA_PLUGIN_PATCH (data
);
191 ianjuta_message_view_buffer_append (p_plugin
->mesg_view
, line
, NULL
);
195 on_patch_terminated (AnjutaLauncher
*launcher
,
196 gint child_pid
, gint status
, gulong time_taken
,
199 g_return_if_fail (launcher
!= NULL
);
201 PatchPlugin
* plugin
= ANJUTA_PLUGIN_PATCH (data
);
203 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher
),
204 G_CALLBACK (on_patch_terminated
),
209 gtk_label_set_text(GTK_LABEL(plugin
->output_label
),
210 _("Patch failed.\nPlease review the failure messages.\n"
211 "Examine and remove any rejected files.\n"));
212 gtk_widget_set_sensitive(plugin
->patch_button
, TRUE
);
216 gtk_label_set_text(GTK_LABEL(plugin
->output_label
), "Patching complete");
217 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(plugin
->dry_run_check
)))
219 gtk_widget_hide (plugin
->dialog
);
220 gtk_widget_destroy (plugin
->dialog
);
223 gtk_widget_set_sensitive(plugin
->patch_button
, TRUE
);
226 plugin
->executing
= FALSE
;
227 plugin
->mesg_view
= NULL
;
231 on_msg_buffer (IAnjutaMessageView
*view
, const gchar
*line
,
234 ianjuta_message_view_append (view
, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL
, line
, "", NULL
);