File: tsh-diff-dialog.c

package info (click to toggle)
thunar-vcs-plugin 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 4,832 kB
  • sloc: ansic: 14,489; sh: 4,174; makefile: 306
file content (363 lines) | stat: -rw-r--r-- 11,868 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*-
 * Copyright (C) 2007-2011  Peter de Ridder <peter@xfce.org>
 * Copyright (C) 2012 Stefan Sperling <stsp@stsp.name>
 *
 * 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 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
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <exo/exo.h>
#include <libxfce4util/libxfce4util.h>

#include <subversion-1/svn_client.h>
#include <subversion-1/svn_pools.h>

#include "tsh-common.h"
#include "tsh-diff-dialog.h"

static void cancel_clicked (GtkButton*, gpointer);
static void refresh_clicked (GtkButton*, gpointer);

struct _TshDiffDialog
{
  GtkDialog dialog;

  GtkWidget *text_view;
  GtkTextTag *tag_red;
  GtkTextTag *tag_green;
  GtkTextTag *tag_bold;
  GtkWidget *close;
  GtkWidget *cancel;
  GtkWidget *refresh;
  gint current_line;
  GtkWidget *depth;
  GtkWidget *notice_ancestry;
  GtkWidget *no_diff_deleted;
#if CHECK_SVN_VERSION_S(1,7)
  GtkWidget *show_copies_as_adds;
#endif
};

struct _TshDiffDialogClass
{
  GtkDialogClass dialog_class;
};

G_DEFINE_TYPE (TshDiffDialog, tsh_diff_dialog, GTK_TYPE_DIALOG)

enum {
  SIGNAL_CANCEL = 0,
  SIGNAL_REFRESH,
  SIGNAL_COUNT
};

static guint signals[SIGNAL_COUNT];

static void
tsh_diff_dialog_class_init (TshDiffDialogClass *klass)
{
  signals[SIGNAL_CANCEL] = g_signal_new("cancel-clicked",
    G_OBJECT_CLASS_TYPE (klass),
    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__VOID,
    G_TYPE_NONE, 0);
  signals[SIGNAL_REFRESH] = g_signal_new("refresh-clicked",
    G_OBJECT_CLASS_TYPE (klass),
    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__VOID,
    G_TYPE_NONE, 0);
}

static void
tsh_diff_dialog_init (TshDiffDialog *dialog)
{
  GtkWidget *text_view;
  GtkTextBuffer *text_buffer;
  GtkWidget *scroll_window;
  GtkWidget *button;
  GtkWidget *grid;
  GtkTreeModel *model;
  GtkWidget *depth;
  GtkWidget *notice_ancestry;
  GtkWidget *no_diff_deleted;
#if CHECK_SVN_VERSION_S(1,7)
  GtkWidget *show_copies_as_adds;
#endif
  GtkCellRenderer *renderer;
  GtkTreeIter iter;

  scroll_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  dialog->text_view = text_view = gtk_text_view_new ();
  gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE);
  gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text_view), FALSE);
  gtk_text_view_set_monospace (GTK_TEXT_VIEW (text_view), TRUE);
  text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(dialog->text_view));
  dialog->tag_red = gtk_text_buffer_create_tag(text_buffer, NULL,
                                               "foreground", "red", NULL);
  dialog->tag_green = gtk_text_buffer_create_tag(text_buffer, NULL,
                                                 "foreground",
                                                 "forestgreen", NULL);
  dialog->tag_bold = gtk_text_buffer_create_tag(text_buffer, NULL,
                                                "weight",
                                                PANGO_WEIGHT_BOLD, NULL);
  dialog->current_line = 0;

  gtk_container_add (GTK_CONTAINER (scroll_window), text_view);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), scroll_window, TRUE, TRUE, 0);
  gtk_widget_show (text_view);
  gtk_widget_show (scroll_window);

  gtk_window_set_title (GTK_WINDOW (dialog), _("Diff"));

  dialog->close = button = gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Close"), GTK_RESPONSE_CLOSE);
  gtk_widget_hide (button);

  dialog->cancel = button = gtk_button_new_with_mnemonic (_("_Cancel"));
  gtk_box_pack_end (GTK_BOX (exo_gtk_dialog_get_action_area (GTK_DIALOG (dialog))), button, FALSE, TRUE, 0);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cancel_clicked), dialog);
  gtk_widget_show (button);

  dialog->refresh = button = gtk_button_new_with_mnemonic (_("_Refresh"));
  gtk_box_pack_start (GTK_BOX (exo_gtk_dialog_get_action_area (GTK_DIALOG (dialog))), button, FALSE, TRUE, 0);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (refresh_clicked), dialog);
  gtk_widget_hide (button);

  grid = gtk_grid_new ();
  model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
  dialog->depth = depth = gtk_combo_box_new_with_model (model);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                        /* Translators: svn recursion selection
                         * Self means only this file/direcotry is shown
                         */
                      0, _("Self"),
                      1, svn_depth_empty,
                      -1);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                        /* Translators: svn recursion selection
                         * Immediate files means this file/direcotry and the files it contains are shown
                         */
                      0, _("Immediate files"),
                      1, svn_depth_files,
                      -1);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                        /* Translators: svn recursion selection
                         * Immediates means this file/direcotry and the subdirectories are shown
                         */
                      0, _("Immediates"),
                      1, svn_depth_immediates,
                      -1);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                        /* Translators: svn recursion selection
                         * Recursive means the list is full recursive
                         */
                      0, _("Recursive"),
                      1, svn_depth_infinity,
                      -1);

  gtk_combo_box_set_active_iter (GTK_COMBO_BOX (depth), &iter);

  g_object_unref (model);

  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (depth), renderer, TRUE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (depth), renderer, "text", 0);

  gtk_widget_set_hexpand (depth, TRUE);
  gtk_grid_attach (GTK_GRID (grid), depth, 0, 0, 1, 1);
  gtk_widget_show (depth);

  dialog->notice_ancestry = notice_ancestry = gtk_check_button_new_with_label (_("Notice ancestry"));
  gtk_widget_set_hexpand (notice_ancestry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), notice_ancestry, 1, 1, 1, 1);
  gtk_widget_show (notice_ancestry);

  dialog->no_diff_deleted = no_diff_deleted = gtk_check_button_new_with_label (_("Do not show differences for deleted files"));
  gtk_widget_set_hexpand (no_diff_deleted, TRUE);
  gtk_grid_attach (GTK_GRID (grid), no_diff_deleted, 0, 1, 1, 1);
  gtk_widget_show (no_diff_deleted);

#if CHECK_SVN_VERSION_S(1,7)
  dialog->show_copies_as_adds = show_copies_as_adds = gtk_check_button_new_with_label (_("Show copies as additions"));
  gtk_widget_set_hexpand (show_copies_as_adds, TRUE);
  gtk_grid_attach (GTK_GRID (grid), show_copies_as_adds, 1, 0, 1, 1);
  gtk_widget_show (show_copies_as_adds);
#endif

  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), grid, FALSE, FALSE, 0);
  gtk_widget_show (grid);

  gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 400);
}

GtkWidget*
tsh_diff_dialog_new (const gchar *title, GtkWindow *parent, GtkDialogFlags flags)
{
  TshDiffDialog *dialog = g_object_new (TSH_TYPE_DIFF_DIALOG, NULL);

  if(title)
    gtk_window_set_title (GTK_WINDOW(dialog), title);

  if(parent)
    gtk_window_set_transient_for (GTK_WINDOW(dialog), parent);

  if(flags & GTK_DIALOG_MODAL)
    gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);

  if(flags & GTK_DIALOG_DESTROY_WITH_PARENT)
    gtk_window_set_destroy_with_parent (GTK_WINDOW(dialog), TRUE);

  return GTK_WIDGET(dialog);
}

void
tsh_diff_dialog_add (TshDiffDialog *dialog, const char *line, gint len)
{
  GtkTextBuffer *text_buffer;
  GtkTextIter line_start;
  GtkTextTag *tag = NULL;

  g_return_if_fail (TSH_IS_DIFF_DIALOG (dialog));

  if (line[0] == '-')
    tag = dialog->tag_red;
  else if (line[0] == '+')
    tag = dialog->tag_green;
  else if (strncmp(line, "Index", 5) == 0)
    tag = dialog->tag_bold;

  text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(dialog->text_view));
  gtk_text_buffer_get_iter_at_line(text_buffer, &line_start,
                                   dialog->current_line);
  if (tag)
    gtk_text_buffer_insert_with_tags(text_buffer, &line_start, line, len,
                                     tag, NULL);
  else
    gtk_text_buffer_insert(text_buffer, &line_start, line, len);

  dialog->current_line++;
}

void
tsh_diff_dialog_done (TshDiffDialog *dialog)
{
  g_return_if_fail (TSH_IS_DIFF_DIALOG (dialog));

  gtk_widget_hide (dialog->cancel);
  gtk_widget_show (dialog->close);
  gtk_widget_show (dialog->refresh);
}

static void
cancel_clicked (GtkButton *button, gpointer user_data)
{
  TshDiffDialog *dialog = TSH_DIFF_DIALOG (user_data);

  gtk_widget_hide (dialog->cancel);
  gtk_widget_show (dialog->close);
  gtk_widget_show (dialog->refresh);

  g_signal_emit (dialog, signals[SIGNAL_CANCEL], 0);
}

static void
refresh_clicked(GtkButton *button, gpointer user_data)
{
  TshDiffDialog *dialog = TSH_DIFF_DIALOG(user_data);
  GtkTextBuffer *text_buffer;

  gtk_widget_hide(dialog->refresh);
  gtk_widget_show(dialog->cancel);

  text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(dialog->text_view));
  gtk_text_buffer_set_text(text_buffer, "", -1);

  g_signal_emit(dialog, signals[SIGNAL_REFRESH], 0);
}

svn_depth_t
tsh_diff_dialog_get_depth (TshDiffDialog *dialog)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  svn_depth_t depth;
  GValue value;

  memset(&value, 0, sizeof(GValue));

  g_return_val_if_fail (TSH_IS_DIFF_DIALOG (dialog), svn_depth_unknown);

  g_return_val_if_fail (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->depth), &iter), svn_depth_unknown);

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->depth));
  gtk_tree_model_get_value (model, &iter, 1, &value);

  depth = g_value_get_int (&value);

  g_value_unset(&value);

  return depth;
}

gboolean
tsh_diff_dialog_get_show_copies_as_adds (TshDiffDialog *dialog)
{
#if CHECK_SVN_VERSION_S(1,7)
  g_return_val_if_fail (TSH_IS_DIFF_DIALOG (dialog), FALSE);

  return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->show_copies_as_adds));
#else /* 1.6 or earlier */
  return FALSE;
#endif
}

gboolean
tsh_diff_dialog_get_no_diff_deleted (TshDiffDialog *dialog)
{
  g_return_val_if_fail (TSH_IS_DIFF_DIALOG (dialog), FALSE);

  return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->no_diff_deleted));
}

gboolean
tsh_diff_dialog_get_notice_ancestry (TshDiffDialog *dialog)
{
  g_return_val_if_fail (TSH_IS_DIFF_DIALOG (dialog), FALSE);

  return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->notice_ancestry));
}

void
tsh_diff_dialog_start (TshDiffDialog *dialog)
{
  GtkTextBuffer *text_buffer;

  text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(dialog->text_view));
  gtk_text_buffer_set_text(text_buffer, _("Loading..."), -1);
}