summaryrefslogtreecommitdiff
path: root/lib/LDFM/MainController.pm
blob: f365115891f15abbd8cf39ed8c01f15c4b5e852e (plain)
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
package LDFM::MainController; 
use utf8;
use warnings;
use strict;
use base 'Gtk2::GladeXML::Simple';
use Gtk2::Ex::Simple::List;
use Readonly;
use Path::Class;
 
our $VERSION = '0.01';
 
sub new {
    my ($class,%params)=@_;
 
    my $self=$class->SUPER::new($params{glade_file});
 
    # cambio le liste con qualcosa di più comodo 
    $self->simplify_list('left');
    $self->simplify_list('right');
 
    # imposto i path 
    $self->update_list_with_path('left',$ENV{HOME});
    $self->update_list_with_path('right','/');
 
    return $self;
}
 
sub simplify_list {
    my ($self,$side)=@_;
 
    my $list_name="${side}_list";
 
    $self->{$list_name}=Gtk2::Ex::Simple::List->new_from_treeview(
        $self->{$list_name},
        'File' => 'text',
        'Type' => 'text',
        'Size' => 'int',
    );
    $self->{$list_name}->get_selection->set_mode('multiple');
 
    $self->{"${side}_data"}=$self->{$list_name}->{data};
 
    return;
}
 
sub update_list_with_path {
    my ($self,$side,$path)=@_;
 
    my $newpath;
    if ($path eq '..') {
        $newpath=$self->{"cur_${side}_path"}->parent;
    }
    else {
        $newpath=dir($path)->absolute($self->{"cur_${side}_path"})->cleanup;
    }
 
    return unless -d $newpath;
 
    $self->{"cur_${side}_path"}=$newpath;
    $self->{"${side}_path"}->set_text($newpath->stringify);
 
    my @dir_list=();
    for my $item ($newpath->children(all=>1)) {
        my @row=($item->relative($newpath)->stringify);
        if ($item->isa('Class::Path::Dir')) {
            push @row,'DIR',0;
        }
        else {
            push @row,'FILE',-s $item;
        }
        push @dir_list,[@row];
    }
 
    @{$self->{"${side}_data"}}=@dir_list;
}
 
sub set_left_path {
    my ($self,$widget)=@_;
 
    $self->update_list_with_path('left',$widget->get_text());
 
    return;
}
 
sub set_right_path {
    my ($self,$widget)=@_;
 
    $self->update_list_with_path('right',$widget->get_text());
 
    return;
}
 
sub use_a_row {
    my ($self,$side,$tree_path)=@_;
 
    #my $row_ref=$widget->get_row_data_from_path($tree_path); 
    my ($row_num)=$tree_path->get_indices();
    my $row_ref=$self->{"${side}_data"}->[$row_num];
    my $relpath=$row_ref->[0];
 
    $self->update_list_with_path($side,$relpath);
}
 
sub use_left_row {
    my ($self,$widget,$tree_path)=@_;
 
    $self->use_a_row('left',$tree_path);
 
    return;
}
 
sub use_right_row {
    my ($self,$widget,$tree_path)=@_;
 
    $self->use_a_row('right',$tree_path);
 
    return;
}
 
sub get_selected_side {
    my ($self)=@_;
 
    return $self->{left_list}->has_focus()
             ||
           $self->{left_path}->has_focus()
             'left'
             'right';
}
 
sub get_selected_files {
    my ($self)=@_;
    my $side=$self->get_selected_side();
    my $path=$self->{"cur_${side}_path"};
 
    my @files=map {
        $self->{"cur_${side}_path"}->file($self->{"${side}_data"}->[$_]->[0]);
    $self->{"${side}_list"}->get_selected_indices();
 
    return @files;
}
 
sub mkdir {
    my ($self)=@_;
 
    my $side=$self->get_selected_side();
    my $path=$self->{"cur_${side}_path"};
 
    warn "Creerei una dir in $path\n";
 
    return;
}
 
sub delete {
    my ($self)=@_;
 
    my @files=$self->get_selected_files();
 
    for my $file (@files) {
        if (-d $file) {
            warn "Cancellerei la directory $file";
        }
        else {
            warn "Cancellerei il file $file";
        }
    }
 
    return;
}
 
sub rename {
    my ($self)=@_;
 
    my @files=$self->get_selected_files();
 
    if (@files==1) {
        warn "Rinominerei $files[0]\n";
    }
    else {
        warn "Troppa roba selezionata\n";
    }
 
    return;
}
 
sub quit {
    Gtk2->main_quit;
}
 
1;