File: patch-plugin.c

package info (click to toggle)
anjuta 1.2.2-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 24,732 kB
  • ctags: 12,398
  • sloc: ansic: 82,587; cpp: 35,104; sh: 8,591; xml: 7,210; makefile: 981; python: 157
file content (235 lines) | stat: -rw-r--r-- 7,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*  patch-plugin.c (C) 2002 Johannes Schmid
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU 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 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 "../../src/anjuta.h"
#include "../../src/launcher.h"
#include "../../src/anjuta_info.h"
#include "../../src/anjuta-plugins.h"

gchar   *GetDescr       (void);
glong    GetVersion     (void);
gchar *GetMenu(void);
gboolean Init           (GModule *self, void **pUserData, AnjutaApp* p);
void     CleanUp        (GModule *self, void *pUserData, AnjutaApp* p);
void     Activate       (GModule *self, void *pUserData, AnjutaApp* p);
gchar   *GetMenuTitle   (GModule *self, void *pUserData);
gchar   *GetTooltipText (GModule *self, void *pUserData);

typedef struct _PatchPluginGUI PatchPluginGUI;

static void patch_level_changed (GtkAdjustment *adj);

static void on_ok_clicked (GtkButton *button, PatchPluginGUI* gui);
static void on_cancel_clicked (GtkButton *button, PatchPluginGUI* gui);

static void on_msg_arrived (AnjutaLauncher *launcher,
							AnjutaLauncherOutputType output_type,
							const gchar* line, gpointer data);
static void on_patch_terminated (AnjutaLauncher *launcher,
								 gint child_pid, gint status, gulong time_taken,
								 gpointer data);

struct _PatchPluginGUI
{
	GtkWidget* dialog;
	
	GtkWidget* ok_button;
	GtkWidget* cancel_button;
	
	GtkWidget* entry_patch_dir;
	GtkWidget* entry_patch_file;
	
	GtkWidget* hscale_patch_level;
};

gint patch_level = 0;

/* Get module description */
gchar *
GetDescr()
{
	return g_strdup(_("Interface to patch Projects or individual files"));
}
	/* GetModule Version hi/low word 1.02 0x10002 */
glong
GetVersion()
{
	return 0x100000;
}

gchar *GetMenu(void)
{
	return g_strdup("format");
}

gboolean
Init( GModule *self, void **pUserData, AnjutaApp* p )
{
	return TRUE ;
}

void
CleanUp( GModule *self, void *pUserData, AnjutaApp* p )
{
}

static void
patch_level_changed (GtkAdjustment *adj)
{
	patch_level = (gint) adj->value;
}

void
Activate( GModule *self, void *pUserData, AnjutaApp* p)
{
	PatchPluginGUI* gui;
	GtkObject* adj;
	GtkWidget* dir_label = gtk_label_new (_("File/Dir to patch"));
	GtkWidget* file_label = gtk_label_new (_("Patch file"));
	GtkWidget* level_label = gtk_label_new (_("Patch level (-p)"));
	GtkWidget* table = gtk_table_new (3, 2, FALSE);

	gui = g_new0 (PatchPluginGUI, 1);
	
	gui->dialog = gnome_dialog_new (_("Patch Plugin"), _("Cancel"), _("Patch"), NULL);
	gtk_window_set_transient_for (GTK_WINDOW(gui->dialog), GTK_WINDOW(p->widgets.window));
	gui->entry_patch_dir = gnome_file_entry_new ("patch-dir", 
	_("Selected directory to patch"));
	gui->entry_patch_file = gnome_file_entry_new ("patch-file",
			_("Selected patch file"));

	adj = gtk_adjustment_new (patch_level, 0, 10, 1, 1, 1);
	gtk_signal_connect (adj, "value_changed",
			    GTK_SIGNAL_FUNC (patch_level_changed), NULL);

	gui->hscale_patch_level = gtk_hscale_new (GTK_ADJUSTMENT (adj));
	gtk_scale_set_digits (GTK_SCALE (gui->hscale_patch_level), 0);
	
	gtk_table_set_row_spacings (GTK_TABLE (table), 5);
	gtk_table_set_col_spacings (GTK_TABLE (table), 10);

	gtk_table_attach_defaults (GTK_TABLE(table), dir_label, 0, 1, 0, 1);
	gtk_table_attach_defaults (GTK_TABLE(table), file_label, 0, 1, 1, 2);
	gtk_table_attach_defaults (GTK_TABLE(table), level_label, 0, 1, 2, 3);
	gtk_table_attach_defaults (GTK_TABLE(table), gui->entry_patch_dir, 1, 2, 0, 1);
	gtk_table_attach_defaults (GTK_TABLE(table), gui->entry_patch_file, 1, 2, 1, 2);
	gtk_table_attach_defaults (GTK_TABLE(table), gui->hscale_patch_level, 1, 2, 2, 3);
	
	gui->ok_button = g_list_last (GNOME_DIALOG (gui->dialog)->buttons)->data;
	gui->cancel_button = g_list_first (GNOME_DIALOG (gui->dialog)->buttons)->data;
	
	g_signal_connect (G_OBJECT (gui->ok_button), "clicked", 
			GTK_SIGNAL_FUNC(on_ok_clicked), gui);
	g_signal_connect (G_OBJECT (gui->cancel_button), "clicked", 
			GTK_SIGNAL_FUNC(on_cancel_clicked), gui);

	gtk_container_set_border_width (GTK_CONTAINER (GNOME_DIALOG (gui->dialog)->vbox), 5);
	gtk_box_pack_start_defaults (GTK_BOX (GNOME_DIALOG (gui->dialog)->vbox), table);

	gtk_widget_show_all (gui->dialog);
}

gchar
*GetMenuTitle( GModule *self, void *pUserData )
{
	return g_strdup(_("Patch plugin"));
}

gchar
*GetTooltipText( GModule *self, void *pUserData ) 
{
   return g_strdup(_("The patch plugin"));
}

static void on_ok_clicked (GtkButton *button, PatchPluginGUI* gui)
{
	const gchar* directory;
	const gchar* patch_file;
	GString* command = g_string_new (NULL);
	gchar* message;
	
	directory = gtk_entry_get_text(GTK_ENTRY(
		gnome_file_entry_gtk_entry(GNOME_FILE_ENTRY(gui->entry_patch_dir))));
	patch_file = gtk_entry_get_text(GTK_ENTRY(
		gnome_file_entry_gtk_entry(GNOME_FILE_ENTRY(gui->entry_patch_file))));

	if (!file_is_directory (directory))
	{
		g_string_free (command, TRUE);
		gnome_ok_dialog (_("Please select the directory where the patch should be applied"));
		return;
	}
	
	g_string_sprintf (command, "patch -d %s -p %d -f -i %s",
			  directory, patch_level, patch_file);
	
	message = g_strdup_printf (_("Patching %s using %s\n"), 
			  directory, patch_file);
	
	an_message_manager_show (app->messages, MESSAGE_BUILD);
	an_message_manager_append (app->messages, message, MESSAGE_BUILD);
	an_message_manager_append (app->messages,
			_("Patching...\n"), MESSAGE_BUILD);

	g_signal_connect (app->launcher, "child-exited",
					  G_CALLBACK (on_patch_terminated), NULL);
	
	if (!anjuta_launcher_is_busy (app->launcher))
		anjuta_launcher_execute (app->launcher, command->str,
								 on_msg_arrived, NULL);
	else
		gnome_ok_dialog (
			_("There are unfinished jobs, please wait until they are finished"));
	g_string_free(command, TRUE);
	
	on_cancel_clicked (GTK_BUTTON(gui->cancel_button), gui);
}

static void on_cancel_clicked (GtkButton *button, PatchPluginGUI* gui)
{
	gtk_widget_hide (gui->dialog);
	gtk_widget_destroy (gui->dialog);
	g_free(gui);
}

static void on_msg_arrived (AnjutaLauncher *launcher,
							AnjutaLauncherOutputType output_type,
							const gchar* line, gpointer data)
{
	g_return_if_fail (line != NULL);
	an_message_manager_append (app->messages, line, MESSAGE_BUILD);
}

static void on_patch_terminated (AnjutaLauncher *launcher,
								 gint child_pid, gint status, gulong time_taken,
								 gpointer data)
{
	g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
										  G_CALLBACK (on_patch_terminated),
										  data);
	if (status)
	{
		an_message_manager_append (app->messages,
			_("Patch failed.\nPlease review the failure messages.\n"
			"Examine and remove any rejected files.\n"),
			MESSAGE_BUILD);
	}
	else
	{
		an_message_manager_append (app->messages,
			_("Patch successful.\n"), MESSAGE_BUILD);
	}
}