add option -d in order to indicate the path to configuration files
[zik.git] / data / tree.rb
blob7374f81079147693f723ac5d838a906a66b29264
1 =begin
2 Copyright 2007-2008 Vincent Carmona
4 This file is part of ZiK.
6     ZiK is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
11     ZiK is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
16     You should have received a copy of the GNU General Public License
17     along with ZiK; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 =end
20 #**********************************
21 #-----Tree explorer----------------
22 #**********************************
24 ####################Voir /usr/share/doc/libgtk2-ruby/examples/gtk-demo/main.rb
26 class Tree < Gtk::TreeStore
28 def refresh(directory,ext)
29         printf 'Refreshing treeview...'
30         self.clear
31         extension=''
32         ext.each{|ext,show|
33                 extension+=ext+',' if show
34         }
35         extension.chop!
36         l=directory.length
37         unless directory[0].nil? or extension.empty?
38                 for i in 0..l-1
39                         songa=[]
40                         rpath=directory[i]
41                         lr=rpath.split('/').length
42                         songa=Dir.glob(File.join(rpath,"**","*.{"+extension+"}"),File::FNM_CASEFOLD).sort
43 #Root directory
44                         root=self.append(nil)
45                         root[0]=rpath
46                         dirl=rpath#last directory scanned
47                         child_rec=[]#keep record of children
48                         songa.each{|song|
49                                 ext=File.extname(song)
50                                 name=File.basename(song,ext)
51                                 dir=File.dirname(song)
52                                 if dir==rpath
53 #Songs in root directory
54                                         child= self.append(root)
55                                         child[0]=name
56                                         child[1]=ext
57                                         child[2]=rpath
58                                 else
59                                         dira=[]
60                                         dira=dir.split('/');la=dira.length
61                                         dirla=dirl.split('/')
62                                         unless dira[lr]==dirla[lr]
63 #sub-directories of root directory
64                                         child=self.append(root)
65                                         child[0]=dira[lr]
66                                         child_rec[0]=child
67 #                                       child[2]=dir
68                                         end
69                                         for j in lr+1..la-1
70                                                 unless dira[j]==dirla[j]
71 #all sub-directories of first children directories
72                                                         child=self.append(child_rec[j-lr-1])
73                                                         child[0]=dira[j]
74                                                         child_rec[j-lr]=child
75 #                                                       child[2]=dir
76                                                 end
77                                         end
78 #Songs in other directories
79                                         child=self.append(child_rec[la-1-lr])
80                                         child[0]=name
81                                         child[1]=ext
82                                         child[2]=dir
83                                 end
84                                 dirl=dir
85                                 }
86                 end
87         end
88         puts 'done.'
89         return songa
90 end
92 end