File: git-pane.c

package info (click to toggle)
anjuta 2%3A3.34.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 72,440 kB
  • sloc: ansic: 207,444; sh: 47,499; cpp: 11,461; makefile: 3,586; yacc: 2,821; perl: 2,094; lex: 1,546; xml: 904; python: 149; sql: 99; javascript: 51; java: 10
file content (330 lines) | stat: -rw-r--r-- 8,906 bytes parent folder | download | duplicates (5)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * git-shell-test
 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
 * 
 * git-shell-test 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 3 of the License, or
 * (at your option) any later version.
 * 
 * git-shell-test 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "git-pane.h"



G_DEFINE_ABSTRACT_TYPE (GitPane, git_pane, ANJUTA_TYPE_DOCK_PANE);

static void
git_pane_init (GitPane *object)
{
	/* TODO: Add initialization code here */
}

static void
git_pane_finalize (GObject *object)
{
	/* TODO: Add deinitalization code here */

	G_OBJECT_CLASS (git_pane_parent_class)->finalize (object);
}

static void
git_pane_class_init (GitPaneClass *klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);
#if 0
	AnjutaDockPaneClass* parent_class = ANJUTA_DOCK_PANE_CLASS (klass);
#endif

	object_class->finalize = git_pane_finalize;
}

void
git_pane_remove_from_dock (GitPane *self)
{
	Git *plugin;

	plugin = ANJUTA_PLUGIN_GIT (anjuta_dock_pane_get_plugin (ANJUTA_DOCK_PANE (self)));

	anjuta_dock_remove_pane (ANJUTA_DOCK (plugin->dock), 
	                         ANJUTA_DOCK_PANE (self));
}

void
git_pane_popup_menu (GitPane *self, const gchar *menu_name, guint button, 
                     guint32 time)
{
	gchar *path;
	AnjutaPlugin *plugin;
	AnjutaUI *ui;
	GtkMenu *menu;

	path = g_strconcat ("/", menu_name, NULL);
	plugin = anjuta_dock_pane_get_plugin (ANJUTA_DOCK_PANE (self));
	ui = anjuta_shell_get_ui (plugin->shell, NULL);
	menu = GTK_MENU (gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui),
	                                            path));

	g_free (path);
	gtk_menu_popup (menu, NULL, NULL, NULL, NULL, button, time);
}

static void
on_message_view_destroyed (Git* plugin, gpointer destroyed_view)
{
	plugin->message_view = NULL;
}

void
git_pane_create_message_view (Git *plugin)
{
	IAnjutaMessageManager *message_manager; 
		
		
	message_manager = anjuta_shell_get_interface  (ANJUTA_PLUGIN (plugin)->shell,	
												   IAnjutaMessageManager, NULL);
	plugin->message_view = ianjuta_message_manager_get_view_by_name (message_manager, 
																	 _("Git"), 
																	 NULL);
	if (!plugin->message_view)
	{
		plugin->message_view = ianjuta_message_manager_add_view (message_manager, 
																 _("Git"), 
																 ICON_FILE, 
																 NULL);
		g_object_weak_ref (G_OBJECT (plugin->message_view), 
						   (GWeakNotify) on_message_view_destroyed, plugin);
	}
	
	ianjuta_message_view_clear (plugin->message_view, NULL);
	ianjuta_message_manager_set_current_view (message_manager, 
											  plugin->message_view, 
											  NULL);
}

void 
git_pane_on_command_info_arrived (AnjutaCommand *command, Git *plugin)
{
	GQueue *info;
	gchar *message;
	
	info = git_command_get_info_queue (GIT_COMMAND (command));
	
	while (g_queue_peek_head (info))
	{
		message = g_queue_pop_head (info);
		ianjuta_message_view_append (plugin->message_view, 
								     IANJUTA_MESSAGE_VIEW_TYPE_NORMAL,
									 message, "", NULL);
		g_free (message);
	}
}

void
git_pane_set_log_view_column_label (GtkTextBuffer *buffer, 
                                    GtkTextIter *location,
                                    GtkTextMark *mark,
                                    GtkLabel *column_label)
{
	gint column;
	gchar *text;

	column = gtk_text_iter_get_line_offset (location) + 1;
	text = g_strdup_printf (_("Column %i"), column);

	gtk_label_set_text (column_label, text);

	g_free (text);
}

gchar *
git_pane_get_log_from_text_view (GtkTextView *text_view)
{
	GtkTextBuffer *log_buffer;
	GtkTextIter start_iter;
	GtkTextIter end_iter;
	gchar *log;

	log_buffer = gtk_text_view_get_buffer(text_view);
	
	gtk_text_buffer_get_start_iter(log_buffer, &start_iter);
	gtk_text_buffer_get_end_iter(log_buffer, &end_iter) ;

	log = gtk_text_buffer_get_text(log_buffer, &start_iter, &end_iter, FALSE);
	
	return log;
}

gboolean
git_pane_check_input (GtkWidget *parent, GtkWidget *widget, const gchar *input,
                      const gchar *error_message)
{
	gboolean ret;
	GtkWidget *dialog;

	ret = FALSE;

	if (input)
	{
		if (strlen (input) > 0)
			ret = TRUE;
	}

	if (!ret)
	{
		dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
										 GTK_DIALOG_DESTROY_WITH_PARENT,
										 GTK_MESSAGE_WARNING,
										 GTK_BUTTONS_OK,
		                                 "%s",
										 error_message);

		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);

		gtk_window_set_focus (GTK_WINDOW (parent), widget);
	}


	return ret;
}

static void
message_dialog (GtkMessageType message_type, const gchar *message, Git *plugin)
{
	const gchar *dialog_title;
	GtkWidget *image;
	GtkWidget *dialog;
	GtkWidget *close_button;
	GtkWidget *content_area;
	GtkWidget *hbox;
	GtkWidget *scrolled_window;
	GtkWidget *text_view;
	GtkTextBuffer *text_buffer;

	dialog_title = NULL;
	image = gtk_image_new ();

	switch (message_type)
	{
		case GTK_MESSAGE_ERROR:
			gtk_image_set_from_icon_name (GTK_IMAGE (image), 
			                              GTK_STOCK_DIALOG_ERROR, 
			                              GTK_ICON_SIZE_DIALOG);
			dialog_title = _("Git Error");
			break;
		case GTK_MESSAGE_WARNING:
			gtk_image_set_from_icon_name (GTK_IMAGE (image), 
			                              GTK_STOCK_DIALOG_WARNING,
			                              GTK_ICON_SIZE_DIALOG);
			dialog_title = _("Git Warning");
			break;
		default:
			break;
	}
		

	dialog = gtk_dialog_new_with_buttons (dialog_title,
	                                      GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),
	                                      GTK_DIALOG_DESTROY_WITH_PARENT,
	                                      NULL, NULL);

	close_button = gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CLOSE,
	                                      GTK_RESPONSE_CLOSE);
	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	text_view = gtk_text_view_new ();
	text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));

	gtk_window_set_default_size (GTK_WINDOW (dialog), 550, 200);
	
	gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
	                                     GTK_SHADOW_IN);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
	                                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
	gtk_text_buffer_set_text (text_buffer, message, strlen (message));

	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (hbox), scrolled_window, TRUE, TRUE, 0);

	gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);

	gtk_widget_grab_default (close_button);
	gtk_widget_grab_focus (close_button);

	g_signal_connect (G_OBJECT (dialog), "response",
	                  G_CALLBACK (gtk_widget_destroy),
	                  NULL);

	gtk_widget_show_all (dialog);
	
}

void
git_pane_report_errors (AnjutaCommand *command, guint return_code, Git *plugin)
{
	gchar *message;
	
	/* In some cases, git might report errors yet still indicate success.
	 * When this happens, use a warning dialog instead of an error, so the user
	 * knows that something actually happened. */
	message = anjuta_command_get_error_message (command);
	
	if (message)
	{
		if (return_code != 0)
			message_dialog (GTK_MESSAGE_ERROR, message, plugin);
		else
			message_dialog (GTK_MESSAGE_WARNING, message, plugin);
		
		g_free (message);
	}
}

void
git_pane_send_raw_output_to_editor (AnjutaCommand *command, 
                                    IAnjutaEditor *editor)
{
	GQueue *output;
	gchar *line;

	output = git_raw_output_command_get_output (GIT_RAW_OUTPUT_COMMAND (command));

	while (g_queue_peek_head (output))
	{
		line = g_queue_pop_head (output);
		ianjuta_editor_append (editor, line, strlen (line), NULL);
		g_free (line);
	}
}

void
git_pane_send_raw_output_to_string (AnjutaCommand *command, GString *string)
{
	GQueue *output;
	gchar *line;

	output = git_raw_output_command_get_output (GIT_RAW_OUTPUT_COMMAND (command));

	while (g_queue_peek_head (output))
	{
		line = g_queue_pop_head (output);
		g_string_append (string, line);
		g_free (line);
	}
}