1# =============================================================================================================
2# directory listing related functions
3# =============================================================================================================
4
5use strict;
6use warnings;
7
8#--------------------------------------------------------------------------------------------------------------
9# List Directory
10#--------------------------------------------------------------------------------------------------------------
11
12sub ls_dir
13{
14	&plog(3, "sub ls_dir");
15	&prep_globals;
16	&hlist_clear;
17
18	if($main::LISTING)
19	{
20		&plog(0, "sub ls_dir: Allready preforming a list, aborting new list attempt");
21		return 0;
22	}
23
24	$main::STOP = 0;
25	$main::LISTING = 1;
26	chdir $main::dir;	# shift directory, not just list ;)
27	$main::dir = cwd();
28
29        my @file_list = ();
30        my @dirlist = &dir_filtered($main::dir);
31
32	&ls_dir_print("..");
33
34        if($main::recr)
35        {
36		&plog(4, "sub ls_dir: recursive mode");
37		@main::find_arr = ();
38	        find(\&find_fix, "$main::dir");
39		&ls_dir_find_fix;
40                $main::LISTING = 0;
41                $main::FIRST_DIR_LISTED = 0;
42	        return;
43        }
44        else
45        {
46		&plog(4, "sub ls_dir: non recursive mode");
47        	for(@dirlist)
48        	{
49			if($_ eq "..")
50			{
51				next;
52			}
53                	if(-d $_)
54                	{
55	                	&ls_dir_print($_);	# print directorys 1st
56                        }
57                        else
58                        {
59                        	push @file_list, $_;	# push files to array
60                        }
61                }
62                for(@file_list)
63                {
64                	if($main::STOP == 1)
65			{
66				return 0;
67			}
68                	&ls_dir_print($_);		# then print the file array after all dirs have been printed
69                }
70                $main::LISTING = 0;
71                $main::FIRST_DIR_LISTED = 0;
72                return;
73        }
74}
75
76#--------------------------------------------------------------------------------------------------------------
77# ls_dir_print
78#--------------------------------------------------------------------------------------------------------------
79
80sub ls_dir_find_fix
81{
82	# this sub should recieve an array of files from find_fix
83
84	my @list = @main::find_arr;
85	my $d = cwd();
86	my $file = "";
87	my $dir = "";
88
89	&plog(3, "sub ls_dir_find_fix:");
90
91	for $file(@main::find_arr)
92	{
93		&plog(4, "sub ls_dir_find_fix: list line \"$file\"");
94		$file =~ m/^(.*)\/(.*?)$/;
95		$dir = $1;
96		$file = $2;
97		&plog(4, "sub ls_dir_find_fix: dir = \"$dir\"");
98		&plog(4, "sub ls_dir_find_fix: file = \"$file\"");
99
100		chdir $dir;	# change to dir containing file
101		&ls_dir_print($file);
102		chdir $d;	# change back to dir sub started in
103	}
104	&plog(3, "sub find_fix_process: done");
105	return 1;
106}
107
108
109# this function is only called from ls_dir & ls_dir_find_fix
110
111sub ls_dir_print
112{
113	my $file = shift;
114
115	if($main::STOP == 1)
116	{
117		&plog(4, "sub ls_dir_print: \$main::STOP = \"$main::STOP\" - not printing");
118		return 0;
119	}
120	&plog(3, "sub ls_dir_print: \"$file\"");
121
122	$main::hlist_cwd = cwd;
123
124        my $tag = "";
125        my $art = "";
126        my $tit = "";
127        my $tra = "";
128        my $alb = "";
129        my $com = "";
130        my $gen = "";
131        my $year = "";
132	my $d = cwd();
133
134        if(!$file || $file eq "" || $file eq ".")
135        {
136		&plog(4, "sub ls_dir_print: \$file = \"$file\" not printing");
137        	return;
138        }
139
140	if($file eq "..")
141	{
142		&nf_print("..", "..");
143		return;
144	}
145
146	# recursive padding
147
148	# when doing recursive, pad new dir with a blank line
149	# since when doing recursive we step through each directory while listing,
150	# we simply print cwd for a full dir path.
151
152	if(-d $file && $main::recr)
153	{
154		&nf_print(" ", "<MSG>");
155		&nf_print("$d/$file", "$d/$file");
156		return;
157	}
158
159	# if is mp3 & id3_mode enabled then grab id3 tags
160	if($file =~ /.*\.mp3$/i && $main::id3_mode == 1)
161	{
162		&plog(4, "sub ls_dir_print: if is mp3 & id3_mode enabled then grab id3 tags");
163		($tag, $art, $tit, $tra, $alb, $gen, $year, $com) = &get_tags($file);
164
165       		if ($tag eq "id3v1")
166       		{
167			&plog(4, "sub ls_dir_print: got id3 tags, printing");
168			&nf_print($file, $file, $art, $tit, $tra, $alb, $com, $gen, $year);
169			return;
170		}
171	}
172
173	&plog(4, "sub ls_dir_print: doesnt meet any special conditions, just print it");
174	&nf_print($file, $file);
175}
176
177#--------------------------------------------------------------------------------------------------------------
178# Dir Dialog
179#--------------------------------------------------------------------------------------------------------------
180
181sub dir_dialog
182{
183	&plog(3, "sub dir_dialog");
184	my $old_dir = $main::dir;
185
186	my $dd_dir = $main::mw->chooseDirectory
187	(
188		-initialdir=>$main::dir,
189		-title=>"Choose a directory"
190	);
191
192	if($dd_dir)
193	{
194		$main::dir = $dd_dir;
195                chdir $main::dir;
196		&ls_dir;
197	}
198	else
199	{
200		$main::dir = $old_dir;
201	}
202}
203
204#--------------------------------------------------------------------------------------------------------------
205# fn_readdir, also removes . and .. from listing which nf needs
206#--------------------------------------------------------------------------------------------------------------
207
208sub fn_readdir
209{
210	&plog(3, "sub fn_readdir");
211	my $dir = shift;
212	my @dirlist = ();
213	my @dirlist_clean = ();
214	my @d = ();
215
216	opendir(DIR, "$dir") or die "can't open dir \"$dir\": $!";
217	@dirlist = CORE::readdir(DIR);
218	closedir DIR;
219
220	# -- make sure we dont have . and .. in array --
221	for my $item(@dirlist)
222	{
223		if($item eq "." || $item eq "..")
224		{
225			next;
226		}
227		push @dirlist_clean, $item;
228	}
229
230	@dirlist = &ci_sort(@dirlist);  # sort array
231	return @dirlist;
232}
233
234sub dir_filtered
235{
236	my $dir = shift;
237	my @d = ();
238	my @dirlist = &fn_readdir($dir);
239
240	&plog(3, "sub dir_filtered \"$dir\"");
241
242	for my $i(@dirlist)
243	{
244		if(-d $i && (!$main::LISTING && !$main::proc_dirs))	# ta tibbsy for help
245 		{
246			&plog(4, "sub dir_filtered: $i is dir, didnt pass");
247 			next;
248 		}
249		if($main::FILTER)
250		{
251			if(&match_filter($i) == 1)	# apply listing filter
252			{
253				&plog(4, "sub dir_filtered: $i passed");
254				push @d, $i;
255				next;
256			}
257			else
258			{
259				&plog(4, "sub dir_filtered: $i didnt pass");
260				next;
261			}
262		}
263		else
264		{
265			push @d, $i;
266			next;
267		}
268	}
269	return @d;
270}
271
2721;
273