Class: DirBrowser

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/gswax/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = Settings.brains_dir) ⇒ DirBrowser

Returns a new instance of DirBrowser.



13
14
15
16
17
18
19
20
21
22
# File 'lib/gswax/browser.rb', line 13

def initialize(path = Settings.brains_dir)		
	@okfiles = [/.mp3/, /.flac/, /.ogg/, /.wav/]
	@selected = []
	@listview = ListView.new		# def in shared.rb
	@listview.add_observer(self, :on_list_signals)
	
	init_ui
	pathscan(path)
	update_ui
end

Instance Method Details

#add_btns_active(boolean) ⇒ Object



215
216
217
218
# File 'lib/gswax/browser.rb', line 215

def add_btns_active(boolean)
	@append_btn.sensitive = boolean
	@prepend_btn.sensitive = boolean
end

#close_windowObject



220
221
222
# File 'lib/gswax/browser.rb', line 220

def close_window
	@win.destroy
end

#get_image(path) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gswax/browser.rb', line 188

def get_image(path)
	if path
		image_files = []
		Dir.entries(path).each{|entry|
			image_files << entry if entry.downcase.include?(".jpg") || entry.downcase.include?(".png") || entry.downcase.include?(".gif")
		}
		if image_files[0]
			@image_file = path + File::Separator + image_files[0]
		else
			@image_file = Settings.brains_dir + File::Separator + "images" + File::Separator + "no_cover.png"
		end
	else
		@image_file = Settings.brains_dir + File::Separator + "images" + File::Separator + "no_cover.png"
	end
end

#init_uiObject



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
# File 'lib/gswax/browser.rb', line 24

def init_ui
	@win = Gtk::Window.new
	@win.set_size_request(750, 450)
	@win.icon = Gdk::Pixbuf.new File.join(DIR, '../static/gshoes-icon.png')
	@win.title = 'Directory Browser'
	@win.signal_connect("destroy"){changed; notify_observers("BROWSER_CLOSED")}
		@main = Gtk::HBox.new(false, 5)
			@leftalign = Gtk::Alignment.new(0.5, 0, 0, 0)
				@left = Gtk::VBox.new(false, 2)
				@left.set_size_request(200, 450)
				
					current_btn_frame = Gtk::Frame.new()
						@current_dir_btn = Gtk::EventBox.new()
						@current_dir_btn.set_size_request(200, 200)
							@img = Gtk::Image.new()
						@current_dir_btn.add(@img)
						@current_dir_btn.signal_connect("button_press_event"){
							@listview.list_selection.select_all
						}
					current_btn_frame.add(@current_dir_btn)
			
					up_btn_frame = Gtk::Frame.new("/../")
					up_btn_frame.label_xalign = 0.05
						@up_dir_btn = Gtk::EventBox.new()
							@current_dir_text = Gtk::Label.new()
							@current_dir_text.width_chars = (26)
							@current_dir_text.set_wrap(true)
							@current_dir_text.justify = Gtk::JUSTIFY_CENTER
							@current_dir_text.ypad = 5
						@up_dir_btn.add(@current_dir_text)
						@up_dir_btn.signal_connect("button_press_event"){up_one_dir}
					up_btn_frame.add(@up_dir_btn)
				
					add_btns_box = Gtk::HBox.new(true, 2)
						@append_btn = Gtk::Button.new("list <<")
						@append_btn.signal_connect("clicked"){notify_add_selected}
						@prepend_btn = Gtk::Button.new(">> list")
						@prepend_btn.signal_connect("clicked"){notify_add_selected("prepend")}
						@append_btn.sensitive = false
						@prepend_btn.sensitive = false
					add_btns_box.pack_start(@append_btn, true, true, 2)
					add_btns_box.pack_start(@prepend_btn, true, true, 2)
		
				@left.pack_start(current_btn_frame, false, false, 10)
				@left.pack_start(up_btn_frame, false, false, 10)
				@left.pack_end(add_btns_box, false, false, 10)
			
			@leftalign.add(@left)
			
			@rightalign = Gtk::Alignment.new(0, 0, 1, 0)
				@right = Gtk::VBox.new(false, 2)
					@rightpane = Gtk::ScrolledWindow.new
					@rightpane.set_size_request(500, 450)
						horizontal = @rightpane.hadjustment
						vertical = @rightpane.vadjustment
						viewport = Gtk::Viewport.new(horizontal, vertical)
					@rightpane.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS)
					@rightpane.add_with_viewport(@listview.list)
				@right.pack_start(@rightpane, true, true, 2)
			@rightalign = Gtk::Alignment.new(0, 0, 1, 0)
			@rightalign.add(@right)
			
		@main.pack_start(@leftalign, true, true, 2)
		@main.pack_start(@rightalign, true, true, 2)
	
	@win.add(@main)
	@win.show_all
end

#notify_add_selected(position = "append") ⇒ Object



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
# File 'lib/gswax/browser.rb', line 128

def notify_add_selected(position = "append")
	dirs = []
	files = []
	@listview.list_selection.selected_each{|mod, path, iter|
		dirs << iter[1] ? File.directory?(iter[1]) : files << iter[1]
	}
	
	files.each{|file| @selected << file} if files[0]		
	
	if dirs[0]
		dirs.each{|dir| 
			Find.find(dir){|item|
			@okfiles.each{|ok| @selected << item if item.downcase =~ ok}
			}	
		}
	end
	
	if position == "prepend"
		@selected.reverse!.insert(0, "PREPEND")
	else
		@selected.insert(0, "APPEND")
	end
	
	changed; notify_observers(@selected)
	@selected = []
	@listview.list_selection.unselect_all
	add_btns_active(false)
end

#on_list_signals(*signal) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/gswax/browser.rb', line 108

def on_list_signals(*signal)
	case signal[0]
		when "changed"
			add_btns_active(true)
		when "row-activated"
			right_select(signal[2])
			add_btns_active(false)
	end
end

#pathscan(path) ⇒ Object



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
# File 'lib/gswax/browser.rb', line 157

def pathscan(path)
	if path
		if File.directory?(path)
			dirs = []
			files = []
			Dir.open(path){|dir|
				for entry in dir
					next if entry == '.'
					next if entry == '..'
					unless entry[0] == '.'
						item = path + File::Separator + entry
						if File.directory?(item)
							dirs << item unless File.basename(item)[0] == "."
						else
							@okfiles.each{|ok| files << item if item.downcase =~ ok}
						end
					end
				end
			}
			@dirs = dirs.sort
			@files = files.sort
			@current_dir = path
			@left_width = 200
			get_image(@current_dir)
		end
	else
		@current_dir = Settings.brains_dir
		@image_file = Settings.brains_dir + File::Separator + "images" + File::Separator + "no_cover.png"
	end
end

#right_select(path) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/gswax/browser.rb', line 118

def right_select(path)
	iter = @listview.store.get_iter(path)
	entry = iter[1]
	if @dirs.include?(entry)
		update_dir(entry)
	elsif @files.include?(entry)
		changed; notify_observers(["PLAY_NOW", entry])
	end
end

#up_one_dirObject



204
205
206
207
208
# File 'lib/gswax/browser.rb', line 204

def up_one_dir
	newpath = @current_dir.split(File::Separator)[0..-2].join(File::Separator)
	update_dir(newpath)
	add_btns_active(false)
end

#update_dir(path) ⇒ Object



210
211
212
213
# File 'lib/gswax/browser.rb', line 210

def update_dir(path)
	pathscan(path)
	update_ui
end

#update_uiObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gswax/browser.rb', line 93

def update_ui
	##left side
	@current_dir_btn.remove(@img)
	pbuf = Gdk::Pixbuf.new(@image_file, 200, 200)
	@img = Gtk::Image.new(pbuf)
	@current_dir_btn.add(@img)
	@current_dir_text.text = @current_dir
	@left.show_all
	
	##right side
	@listview.store.clear
	@dirs.each{|dir| @listview.add_to_list(dir)} if @dirs[0]
	@files.each{|file| @listview.add_to_list(file)} if @files[0]
end