summaryrefslogtreecommitdiff
path: root/lib/LDFM/MainController.pm
blob: 31b45da0b2cb812b012e781808c16336a782a93a (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
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
package LDFM::MainController; 
use utf8;
use warnings;
use strict;
use base 'Gtk2::GladeXML::Simple';
use Gtk2::Ex::Simple::List;
use LDFM::DialogController;
use Readonly;
use Path::Class;
use File::MMagic;
 
our $VERSION = '0.01';
 
sub new {
    my ($class,%params)=@_;
 
    my $glade_file=dir($params{glade_dir})->file('ldfm-main.glade');
    my $self=$class->SUPER::new($glade_file);
 
    $self->{glade_dir}=$params{glade_dir};
 
    # inizializzo il sistema di riconoscimento tipi file 
    $self->{filetype}=File::MMagic->new('/usr/share/file/magic.mime');
 
    # 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,$self->{filetype}->checktype_filename($item),-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_files_from_list {
    my ($self,$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 get_selected_files {
    my ($self)=@_;
    my $side=$self->get_selected_side();
 
    return $self->get_files_from_list($side);
}
 
sub mkdir {
    my ($self)=@_;
 
    my $side=$self->get_selected_side();
    my $path=$self->{"cur_${side}_path"};
 
    my $dialog=LDFM::DialogController->new(
        glade_dir => $self->{glade_dir},
        domanda   => "Nuova directory in $path:",
    );
    my $response=$dialog->run();
 
    if ($response) {
        warn "Creerei la dir $response in $path\n";
    }
 
    return;
}
 
sub delete {
    my ($self)=@_;
 
    my @files=$self->get_selected_files();
    my $num_files=scalar @files;
 
    my $dialog=Gtk2::MessageDialog->new(
        $self->{main_window},
        'modal',
        'warning',
        'yes-no',
        "Cancellare veramente i $num_files file?",
    );
    my $response=$dialog->run();
    $dialog->destroy();
    return unless $response eq 'yes';
 
    for my $file (@files) {
        if (-d $file) {
            warn "Cancellerei la directory $file\n";
        }
        else {
            warn "Cancellerei il file $file\n";
        }
    }
 
    return;
}
 
sub rename {
    my ($self)=@_;
 
    my @files=$self->get_selected_files();
 
    if (@files==1) {
        my $dialog=LDFM::DialogController->new(
            glade_dir => $self->{glade_dir},
            domanda   => "Rinomino $files[0] in:",
        );
        my $response=$dialog->run();
 
        if ($response) {
            warn "Rinominerei $files[0] in $response\n";
        }
    }
    else {
        warn "Troppa roba selezionata\n";
    }
 
    return;
}
 
sub copy_lr {
    my ($self)=@_;
 
    my @files=$self->get_files_from_list('left');
    my $destpath=$self->{cur_right_path};
 
    $self->copy_files(@files,$destpath);
 
    return;
}
 
sub copy_rl {
    my ($self)=@_;
 
    my @files=$self->get_files_from_list('right');
    my $destpath=$self->{cur_left_path};
 
    $self->copy_files(@files,$destpath);
 
    return;
}
 
sub copy_files {
    my $self=shift;
    my $destdir=pop;
    my @files=@_;
 
    for my $file (@files) {
        warn "Copierei $file in $destdir\n";
    }
 
    return;
}
 
sub move_lr {
    my ($self)=@_;
 
    my @files=$self->get_files_from_list('left');
    my $destpath=$self->{cur_right_path};
 
    $self->move_files(@files,$destpath);
 
    return;
}
 
sub move_rl {
    my ($self)=@_;
 
    my @files=$self->get_files_from_list('right');
    my $destpath=$self->{cur_left_path};
 
    $self->move_files(@files,$destpath);
 
    return;
}
 
sub move_files {
    my $self=shift;
    my $destdir=pop;
    my @files=@_;
 
    for my $file (@files) {
        warn "Sposterei $file in $destdir\n";
    }
 
    return;
}
 
sub quit {
    Gtk2->main_quit;
}
 
1;