~ubuntu-branches/ubuntu/saucy/padre/saucy-proposed

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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package Padre::Wx::VCS;

use 5.008;
use strict;
use warnings;
use Padre::Role::Task     ();
use Padre::Wx::Role::View ();
use Padre::Wx             ();
use Padre::Wx::FBP::VCS   ();
use Padre::Task::VCS      ();
use Padre::Logger;

our $VERSION = '0.92';
our @ISA     = qw{
	Padre::Role::Task
	Padre::Wx::Role::View
	Padre::Wx::FBP::VCS
};

use constant {
	RED        => Wx::Colour->new('red'),
	DARK_GREEN => Wx::Colour->new( 0x00, 0x90, 0x00 ),
	BLUE       => Wx::Colour->new('blue'),
	GRAY       => Wx::Colour->new('gray'),
	BLACK      => Wx::Colour->new('black'),
};

# Constructor
sub new {
	my $class = shift;
	my $main  = shift;
	my $panel = shift || $main->right;
	my $self  = $class->SUPER::new($panel);

	# Set the bitmap button icons
	$self->{add}->SetBitmapLabel( Padre::Wx::Icon::find('actions/list-add') );
	$self->{delete}->SetBitmapLabel( Padre::Wx::Icon::find('actions/list-remove') );
	$self->{update}->SetBitmapLabel( Padre::Wx::Icon::find('actions/stock_update-data') );
	$self->{commit}->SetBitmapLabel( Padre::Wx::Icon::find('actions/document-save') );
	$self->{revert}->SetBitmapLabel( Padre::Wx::Icon::find('actions/edit-undo') );
	$self->{refresh}->SetBitmapLabel( Padre::Wx::Icon::find('actions/view-refresh') );

	# Set up column sorting
	$self->{sort_column} = 0;
	$self->{sort_desc}   = 1;

	# Setup columns
	my @column_headers = (
		Wx::gettext('Status'),
		Wx::gettext('Path'),
		Wx::gettext('Author'),
		Wx::gettext('Revision'),
	);
	my $index = 0;
	for my $column_header (@column_headers) {
		$self->{list}->InsertColumn( $index++, $column_header );
	}

	# Column ascending/descending image
	my $images = Wx::ImageList->new( 16, 16 );
	$self->{images} = {
		asc => $images->Add(
			Wx::ArtProvider::GetBitmap(
				'wxART_GO_UP',
				'wxART_OTHER_C',
				[ 16, 16 ],
			),
		),
		desc => $images->Add(
			Wx::ArtProvider::GetBitmap(
				'wxART_GO_DOWN',
				'wxART_OTHER_C',
				[ 16, 16 ],
			),
		),
	};
	$self->{list}->AssignImageList( $images, Wx::IMAGE_LIST_SMALL );

	# Tidy the list
	Padre::Util::tidy_list( $self->{list} );

	# Update the checkboxes with their corresponding values in the
	# configuration
	my $config = $main->config;
	$self->{show_normal}->SetValue( $config->vcs_normal_shown );
	$self->{show_unversioned}->SetValue( $config->vcs_unversioned_shown );
	$self->{show_ignored}->SetValue( $config->vcs_ignored_shown );

	# Hide vcs command buttons at startup
	$self->{commit}->Hide;
	$self->{add}->Hide;
	$self->{delete}->Hide;
	$self->{revert}->Hide;
	$self->{update}->Hide;

	return $self;
}





######################################################################
# Padre::Wx::Role::View Methods

sub view_panel {
	return 'right';
}

sub view_label {
	shift->gettext_label(@_);
}

sub view_close {
	$_[0]->main->show_vcs(0);
}

sub view_start {
}

sub view_stop {
	my $self = shift;

	# Clear out any state and tasks
	$self->task_reset;
	$self->clear;

	return;
}

#####################################################################
# Event Handlers

sub on_refresh_click {
	$_[0]->main->vcs->refresh( $_[0]->current );
}

#####################################################################
# General Methods

sub gettext_label {
	Wx::gettext('Version Control');
}

# Clear everything...
sub clear {
	my $self = shift;

	$self->{list}->DeleteAllItems;
	$self->_show_command_bar(0);

	return;
}

# Nothing to implement here
sub relocale {
	return;
}

sub refresh {
	my $self    = shift;
	my $current = shift or return;
	my $command = shift || Padre::Task::VCS::VCS_STATUS;

	my $document = $current->document;

	# Abort any in-flight checks
	$self->task_reset;

	# Flush old results
	$self->clear;

	# Do not display anything where there is no open documents
	return unless $document;

	# Shortcut if there is nothing in the document to do
	if ( $document->is_unused ) {
		$self->{status}->SetValue( Wx::gettext('Current file is not saved in a version control system') );
		return;
	}

	# Retrieve project version control system
	my $vcs = $document->project->vcs;

	# No version control system?
	unless ($vcs) {
		$self->{status}->SetValue( Wx::gettext('Current file is not in a version control system') );
		return;
	}

	# Not supported VCS check
	if ( $vcs ne Padre::Constant::SUBVERSION and $vcs ne Padre::Constant::GIT ) {
		$self->{status}->SetValue( sprintf( Wx::gettext('%s version control is not currently available'), $vcs ) );
		return;
	}


	# Start a background VCS status task
	$self->task_request(
		task     => 'Padre::Task::VCS',
		command  => $command,
		document => $document,
	);

	return 1;
}

sub task_finish {
	my $self = shift;
	my $task = shift;
	$self->{model} = Params::Util::_ARRAY0( $task->{model} ) or return;
	$self->{vcs} = $task->{vcs} or return;

	$self->render;
}

sub render {
	my $self = shift;

	# Clear if needed. Please note that this is needed
	# for sorting
	$self->clear;

	return unless $self->{model};

	# Subversion status codes
	my %SVN_STATUS = (
		' ' => { name => Wx::gettext('Normal') },
		'A' => { name => Wx::gettext('Added') },
		'D' => { name => Wx::gettext('Deleted') },
		'M' => { name => Wx::gettext('Modified') },
		'C' => { name => Wx::gettext('Conflicted') },
		'I' => { name => Wx::gettext('Ignored') },
		'?' => { name => Wx::gettext('Unversioned') },
		'!' => { name => Wx::gettext('Missing') },
		'~' => { name => Wx::gettext('Obstructed') }
	);

	# GIT status code
	my %GIT_STATUS = (
		' ' => { name => Wx::gettext('Unmodified') },
		'M' => { name => Wx::gettext('Modified') },
		'A' => { name => Wx::gettext('Added') },
		'D' => { name => Wx::gettext('Deleted') },
		'R' => { name => Wx::gettext('Renamed') },
		'C' => { name => Wx::gettext('Copied') },
		'U' => { name => Wx::gettext('Updated but unmerged') },
		'?' => { name => Wx::gettext('Unversioned') },
	);

	my %vcs_status = $self->{vcs} eq Padre::Constant::SUBVERSION ? %SVN_STATUS : %GIT_STATUS;

	# Add a zero count key for VCS status hash
	$vcs_status{$_}->{count} = 0 for keys %vcs_status;

	# Retrieve the state of the checkboxes
	my $show_normal      = $self->{show_normal}->IsChecked      ? 1 : 0;
	my $show_unversioned = $self->{show_unversioned}->IsChecked ? 1 : 0;
	my $show_ignored     = $self->{show_ignored}->IsChecked     ? 1 : 0;

	my $index = 0;
	my $list  = $self->{list};

	$self->_sort_model(%vcs_status);
	my $model = $self->{model};

	my $model_index = 0;
	for my $rec (@$model) {
		my $status      = $rec->{status};
		my $path_status = $vcs_status{$status};
		if ( defined $path_status ) {

			if ( $show_normal or $status ne ' ' ) {

				if ( $show_unversioned or $status ne '?' ) {
					if ( $show_ignored or $status ne 'I' ) {

						# Add a version control path to the list
						$list->InsertImageStringItem( $index, $path_status->{name}, -1 );
						$list->SetItemData( $index, $model_index );
						$list->SetItem( $index, 1, $rec->{path} );
						my $color;
						if ( $status eq ' ' ) {
							$color = DARK_GREEN;
						} elsif ( $status eq 'A' or $status eq 'D' ) {
							$color = RED;
						} elsif ( $status eq 'M' ) {
							$color = BLUE;
						} elsif ( $status eq 'I' ) {
							$color = GRAY;
						} else {
							$color = BLACK;
						}
						$list->SetItemTextColour( $index, $color );

						$list->SetItem( $index,   2, $rec->{author} );
						$list->SetItem( $index++, 3, $rec->{revision} );
					}
				}
			}

		}
		$path_status->{count}++;
		$model_index++;
	}

	# Select the first item
	if ( $list->GetItemCount > 0 ) {
		$list->SetItemState( 0, Wx::LIST_STATE_SELECTED, Wx::LIST_STATE_SELECTED );
	}

	# Show Subversion statistics
	my $message = '';
	for my $status ( sort keys %vcs_status ) {
		my $vcs_status_obj = $vcs_status{$status};
		next if $vcs_status_obj->{count} == 0;
		if ( length($message) > 0 ) {
			$message .= Wx::gettext(', ');
		}
		$message .= sprintf( '%s=%d', $vcs_status_obj->{name}, $vcs_status_obj->{count} );
	}
	$self->{status}->SetValue($message);

	$self->_show_command_bar( $list->GetItemCount > 0 )
		if $self->main->config->vcs_enable_command_bar;

	# Update the list sort image
	$self->set_icon_image( $self->{sort_column}, $self->{sort_desc} );

	# Tidy the list
	Padre::Util::tidy_list($list);

	return 1;
}

sub _show_command_bar {
	my ( $self, $shown ) = @_;

	$self->{commit}->Show($shown);
	$self->{add}->Show($shown);
	$self->{delete}->Show($shown);
	$self->{revert}->Show($shown);
	$self->{update}->Show($shown);
	$self->Layout;
}

sub _sort_model {
	my ( $self, %vcs_status ) = @_;

	my @model = @{ $self->{model} };
	if ( $self->{sort_column} == 0 ) {

		# Sort by status
		@model = sort { $vcs_status{ $a->{status} }{name} cmp $vcs_status{ $b->{status} }{name} } @model;

	} elsif ( $self->{sort_column} == 1 ) {

		# Sort by path
		@model = sort { $a->{path} cmp $b->{path} } @model;
	} elsif ( $self->{sort_column} == 2 ) {

		# Sort by author
		@model = sort { $a->{author} cmp $b->{author} } @model;
	} elsif ( $self->{sort_column} == 3 ) {

		# Sort by revision
		@model = sort { $a->{revision} cmp $b->{revision} } @model;

	}

	if ( $self->{sort_desc} ) {

		# reverse the sorting
		@model = reverse @model;
	}

	$self->{model} = \@model;
}

# Called when a version control list column is clicked
sub on_list_column_click {
	my ( $self, $event ) = @_;

	my $column   = $event->GetColumn;
	my $prevcol  = $self->{sort_column};
	my $reversed = $self->{sort_desc};
	$reversed = $column == $prevcol ? !$reversed : 0;
	$self->{sort_column} = $column;
	$self->{sort_desc}   = $reversed;

	# Reset the previous column sort image
	$self->set_icon_image( $prevcol, -1 );

	$self->render;

	return;
}

sub set_icon_image {
	my ( $self, $column, $image_index ) = @_;

	my $item = Wx::ListItem->new;
	$item->SetMask(Wx::LIST_MASK_IMAGE);
	$item->SetImage($image_index);
	$self->{list}->SetColumn( $column, $item );

	return;
}

sub on_list_item_activated {
	my ( $self, $event ) = @_;

	my $main     = $self->main;
	my $rec      = $self->{model}->[ $self->{list}->GetItemData( $event->GetIndex ) ] or return;
	my $filename = $rec->{fullpath};
	eval {

		# Try to open the file now
		if ( my $id = $main->editor_of_file($filename) ) {
			my $page = $main->notebook->GetPage($id);
			$page->SetFocus;
		} else {
			$main->setup_editor($filename);
		}

		# Select the next difference after opening the file
		Wx::Event::EVT_IDLE(
			$main,
			sub {
				$main->{diff}->select_next_difference;
				Wx::Event::EVT_IDLE( $main, undef );
			},
		) if $main->config->feature_document_diffs;

	};
	$main->error( Wx::gettext('Error while trying to perform Padre action') ) if $@;
}

# Called when "Show normal" checkbox is clicked
sub on_show_normal_click {
	my ( $self, $event ) = @_;

	# Save to configuration
	my $config = $self->main->config;
	$config->apply( vcs_normal_shown => $event->IsChecked ? 1 : 0 );
	$config->write;

	# refresh list
	$self->render;
}

# Called when "Show unversioned" checkbox is clicked
sub on_show_unversioned_click {
	my ( $self, $event ) = @_;

	# Save to configuration
	my $config = $self->main->config;
	$config->apply( vcs_unversioned_shown => $event->IsChecked ? 1 : 0 );
	$config->write;

	# refresh list
	$self->render;
}

# Called when "Show ignored" checkbox is clicked
sub on_show_ignored_click {
	my ( $self, $event ) = @_;

	# Save to configuration
	my $config = $self->main->config;
	$config->apply( vcs_ignored_shown => $event->IsChecked ? 1 : 0 );
	$config->write;

	# refresh list
	$self->render;
}

# Called when "Commit" button is clicked
sub on_commit_click {
	my $self = shift;
	my $main = $self->main;

	return
		unless $main->yes_no(
		Wx::gettext("Do you want to commit?"),
		Wx::gettext('Commit file/directory to repository?')
		);

	$main->vcs->refresh( $self->current, Padre::Task::VCS::VCS_COMMIT );
}

# Called when "Add" button is clicked
sub on_add_click {
	my $self = shift;

	my $main           = $self->main;
	my $list           = $self->{list};
	my $selected_index = $list->GetNextItem( -1, Wx::LIST_NEXT_ALL, Wx::LIST_STATE_SELECTED );
	return if $selected_index == -1;
	my $rec = $self->{model}->[ $list->GetItemData($selected_index) ] or return;
	my $filename = $rec->{fullpath};

	return
		unless $main->yes_no(
		sprintf( Wx::gettext("Do you want to add '%s' to your repository"), $filename ),
		Wx::gettext('Add file to repository?')
		);

	$main->vcs->refresh( $self->current, Padre::Task::VCS::VCS_ADD );
}

# Called when "Delete" checkbox is clicked
sub on_delete_click {
	my $self           = shift;
	my $main           = $self->main;
	my $list           = $self->{list};
	my $selected_index = $list->GetNextItem( -1, Wx::LIST_NEXT_ALL, Wx::LIST_STATE_SELECTED );
	return if $selected_index == -1;
	my $rec = $self->{model}->[ $list->GetItemData($selected_index) ] or return;
	my $filename = $rec->{fullpath};

	return
		unless $main->yes_no(
		sprintf( Wx::gettext("Do you want to delete '%s' from your repository"), $filename ),
		Wx::gettext('Delete file from repository??')
		);

	$main->vcs->refresh( $self->current, Padre::Task::VCS::VCS_DELETE );
}

# Called when "Update" button is clicked
sub on_update_click {
	my $self = shift;
	my $main = $self->main;

	$main->vcs->refresh( $main->current, Padre::Task::VCS::VCS_UPDATE );
}

# Called when "Revert" button is clicked
sub on_revert_click {
	my $self           = shift;
	my $main           = $self->main;
	my $list           = $self->{list};
	my $selected_index = $list->GetNextItem( -1, Wx::LIST_NEXT_ALL, Wx::LIST_STATE_SELECTED );
	return if $selected_index == -1;
	my $rec = $self->{model}->[ $list->GetItemData($selected_index) ] or return;
	my $filename = $rec->{fullpath};

	return
		unless $main->yes_no(
		sprintf( Wx::gettext("Do you want to revert changes to '%s'"), $filename ),
		Wx::gettext('Revert changes?')
		);

	$main->vcs->refresh( $self->current, Padre::Task::VCS::VCS_REVERT );
}

1;

# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.