summaryrefslogtreecommitdiff
path: root/bin/grux.rb
blob: 954ea5ffcbe0ebc2e39e17aed0797762f6bff3c7 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/env ruby -w

#$: << File.expand_path(File.dirname(FILE) + “/../lib”))

#require 'libgruxclient'
require 'gtk2'
require 'xmmsclient'
require 'xmmsclient_glib'

class Grux
end

class Grux::ClientWin < Gtk::VBox
  attr_reader :status

  def initialize( server_address )
    super()


    @server                 = Xmms::Client.new( 'Grux' )

    @status                 = Hash.new
    @status[:connected]     = self.connect( server_address )

    @statusBox              = Gtk::HBox.new
    @mainBox                = Gtk::VPaned.new
    @statusBar              = Gtk::Statusbar.new

    self.pack_start @statusBox, false
    self.pack_start @mainBox
    self.pack_end @statusBar, false


    @statusWdgts            = [
      @currentartistFrame     = Gtk::Frame.new( "Artist" ).add( @currentartistLabel = Gtk::Label.new ),
      @currenttitleFrame      = Gtk::Frame.new( "Title" ).add( @currenttitleLabel = Gtk::Label.new ),
      @currentalbumFrame      = Gtk::Frame.new( "Album" ).add( @currentalbumLabel = Gtk::Label.new )
    ]
    @statusWdgts.each { |frame| @statusBox.pack_start frame }


    @mainWdgts              = [
      @cntrlBox               = Gtk::HBox.new,
      @medialistsWin          = Gtk::Notebook.new
    ]
    @mainBox.pack1 @cntrlBox, true, false
    @mainBox.pack2 @medialistsWin, true, false


    @toolWdgts              = [
      @cntrlBtnBox            = Gtk::VButtonBox.new.set_layout_style( Gtk::ButtonBox::CENTER ),
      @toolChsr               = Gtk::Notebook.new
    ]
    @cntrlBox.pack_start @cntrlBtnBox, false
    @cntrlBox.pack_start @toolChsr


    @cntrlbtns      = [
      @playBtn        = Gtk::ToggleButton.new('_Play', true),
      @pauseBtn       = Gtk::ToggleButton.new('Pa_use', true),
      @stopBtn        = Gtk::ToggleButton.new('_Stop', true),
      @nextBtn        = Gtk::Button.new('_Next'),
      @prevBtn        = Gtk::Button.new('P_revious'),
      @rndmBtn        = Gtk::Button.new('Randomi_ze'),
      @rptBtn         = Gtk::ToggleButton.new('Repeat _All', true),
      @rpt1Btn        = Gtk::ToggleButton.new('Repeat _One', true)
    ]
    @cntrlbtns.each do |button|
      @cntrlBtnBox.pack_start button
    end


    @toolTabs                 = [
      [ @extinfoTab             = Gtk::VBox.new,
        @extinfoTabLbl          = Gtk::Label.new( "Status" ) ],
      [ @tageditorTab           = Gtk::VBox.new,
        @tageditorTabLbl        = Gtk::Label.new( "Tag Editor" ) ],
      [ @searchTab              = Gtk::VBox.new,
        @searchTabLbl           = Gtk::Label.new( "Search" ) ]
    ]
    @toolTabs.each do |content, label|
      @toolChsr.append_page( Gtk::ScrolledWindow.new.add_with_viewport( content ), label )
    end

    @searchTabItems           = [
      @searchTermEntry          = Gtk::Entry.new,
      @startsearchBtn           = Gtk::Button.new( "Search" )
    ]
    @searchTabItems.each do |wdgt|
      @searchTab.pack_start( wdgt )
    end


    @medialistTextRenderer    = Gtk::CellRendererText.new
    @medialistColumns         = [ 'id', 'artist', 'title' ]

    @medialists               = [
      [ @playlistViewLbl        = Gtk::Label.new( "Current Playlist" ),
        @playlistViewWin        = Gtk::ScrolledWindow.new,
        @playlistView           = Gtk::TreeView.new,
        @playlistStore          = Gtk::ListStore.new( String, String, String )
      ],
      [ @medialibViewLbl        = Gtk::Label.new( "Medialib" ),
        @medialibViewWin        = Gtk::ScrolledWindow.new,
        @medialibView           = Gtk::TreeView.new,
        @medialibStore          = Gtk::ListStore.new( String, String, String )
      ],
      [ @searchResultViewLbl    = Gtk::Label.new( "Search Results" ),
        @searchResultViewWin    = Gtk::ScrolledWindow.new,
        @searchResultView       = Gtk::TreeView.new,
        @searchResultStore      = Gtk::ListStore.new( String, String, String )
      ]
    ]

    @medialists.each do |label, window, view, store|
      @medialistColumns.each_with_index do |tag, index|
        view.append_column Gtk::TreeViewColumn.new( tag, @medialistTextRenderer, :text => index )
      end
      view.search_column = 2
      @medialistsWin.append_page( window.add( view ), label )
    end


    self.show_all


    @playBtnClkdHndlr = @playBtn.signal_connect :clicked do
      self.start_playback
    end
    @pauseBtnClkdHndlr = @pauseBtn.signal_connect :clicked do
      self.pause_playback
    end
    @stopBtnClkdHndlr = @stopBtn.signal_connect :clicked do
      self.stop_playback
    end
    @nextBtn.signal_connect :clicked do
      self.step_in_playlist( 1 )
    end
    @prevBtn.signal_connect :clicked do
      self.step_in_playlist( -1 )
    end
    @rndmBtn.signal_connect :clicked do
      self.randomize_playlist
    end
    @rptBtnClkdHndlr = @rptBtn.signal_connect :clicked do
      self.toggle_repeat_all
    end
    @rpt1BtnClkdHndlr = @rpt1Btn.signal_connect :clicked do
      self.toggle_repeat_one
    end

    @startsearchBtn.signal_connect :clicked do
      searchregex = Regexp.new( /#{@searchTermEntry.text}/ix )
      results = []
      @status[:medialib].each_with_index do |songinfo, id|
        songinfo && songinfo.each do |key, info|
          info.each do |src, value|
            if searchregex.match( value.to_s )
              results << id
              break
            end
          end
        end
      end
      searchResultIds, searchResult = self.xmmspls_to_lststor( results.uniq!, @searchResultStore )
      @searchResultView.model = searchResult
    end


    @playlistView.signal_connect :row_activated do |view, path, column|
      @server.playlist_set_next( path.to_str.to_i ).notifier do
        @server.playback_tickle.notifier do
          self.start_playback
          true
        end
        true
      end
    end


    @medialibView.signal_connect :row_activated do |view, path, column|
      songid = @medialibStore.get_iter( path )[0].to_i
      @server.playlist.add_entry( songid ).notifier do
        current_row = @playlistStore.append
        @server.medialib_get_info( songid ).notifier do |songinfo|
          current_row[0] = songid.to_s
          current_row[1] = songinfo[:artist] ? songinfo[:artist].first[1] : ""
          current_row[2] = songinfo[:title] ? songinfo[:title].first[1] : ""
          true
        end
        @server.playlist_set_next( current_row.to_s.to_i ).notifier do
          @server.playback_tickle.notifier do
            self.start_playback
            true
          end
          true
        end
      end
    end


    if @status[:connected]
      p "connected"
      self.resync_complete
    end
  end


  def connect( address )
    begin
      @server.connect( address )
      @server.add_to_glib_mainloop
    rescue Xmms::Client::ClientError => ex
      warn "Connection failed. Error Code:"
      warn ex
      return false
    end


    @server.broadcast_coll_changed.notifier do |*foo|
      self.handle_coll_changed foo
      true
    end
    @server.broadcast_config_value_changed.notifier do |*foo|
      self.handle_config_value_changed  foo
      true
    end
    @server.broadcast_mediainfo_reader_status.notifier do |*foo|
      self.handle_mediainfo_reader_status  foo
      true
    end
    @server.broadcast_medialib_entry_added.notifier do |*foo|
      self.handle_medialib_entry_added  foo
      true
    end
    @server.broadcast_medialib_entry_changed.notifier do |*foo|
      self.handle_medialib_entry_changed  foo
      true
    end
    @server.broadcast_playback_current_id.notifier do |id|
      self.handle_playback_current_id id
      true
    end
    @server.broadcast_playback_status.notifier do |status|
      self.handle_playback_status status
      true
    end
    @server.broadcast_playback_volume_changed.notifier do |*foo|
      self.handle_playback_volume_changed  foo
      true
    end
    @server.broadcast_playlist_changed.notifier do |*foo|
      self.handle_playlist_changed  foo
      true
    end
    @server.broadcast_playlist_current_pos.notifier do |pos|
      self.handle_playlist_current_pos pos
      true
    end
    @server.broadcast_playlist_loaded.notifier do |*foo|
      self.handle_playlist_loaded  foo
      true
    end
    @server.broadcast_quit.notifier do |*foo|
      self.handle_quit  foo
      true
    end
    true
  end


  def handle_coll_changed( *foo )
    p "Collection changed: #{foo}"
    true
  end
  def handle_config_value_changed( *foo )
    p "Config changed: #{foo}"
    true
  end
  def handle_mediainfo_reader_status( *foo )
    p "Mediainfo reader status changed: #{foo}"
    true
  end
  def handle_medialib_entry_added( *foo )
    p "Medialib entry added: #{foo}"
    true
  end
  def handle_medialib_entry_changed( *foo )
    p "Medialib entry changed: #{foo}"
    true
  end


  def handle_playback_current_id( id )
    p "Current playback ID changed: #{id}"
    songpath = Gtk::TreePath.new( @status[:playlistids].index id )

    @playlistView.scroll_to_cell( songpath, nil, true, 0.5, 0.5 )
    @playlistView.set_cursor( songpath, nil, false )

    @server.medialib_get_info( id ).notifier do |songinfo|
      @currentartistLabel.text = ""
      @currenttitleLabel.text = ""
      @currentalbumLabel.text = ""

      @extinfoTab.each do |child| @extinfoTab.remove child end

      songinfo[:artist] && songinfo[:artist].each_pair do |src, value|
        @currentartistLabel.text += @currentartistLabel.text.empty? ? value : "\n#{value}"
      end
      songinfo[:title] && songinfo[:title].each_pair do |src, value|
        @currenttitleLabel.text += @currenttitleLabel.text.empty? ? value : "\n#{value}"
      end
      songinfo[:album] && songinfo[:album].each_pair do |src, value|
        @currentalbumLabel.text += @currentalbumLabel.text.empty? ? value : "\n#{value}"
      end
      songinfo.each_key do |key|
        infos = String.new
        songinfo[key].each_pair do |src,value|
          infos.empty? || infos += "\n"
          infos += "#{value} (#{src})"
        end
        @extinfoTab.pack_end( Gtk::Frame.new( key.to_s ).add( Gtk::Label.new( infos ) ) )
      end
      @extinfoTab.show_all
      true
    end
    true
  end


  def handle_playback_status( status )
    p "Current playback status changed: #{status}"
    @playbackState = status
    true
  end


  def handle_playback_volume_changed( *foo )
    p "Playback volume changed: #{foo}"
    true
  end
  def handle_playlist_changed( *foo )
    p "Playlist changed: #{foo}"
    true
  end


  def handle_playlist_current_pos( pos )
    p "Current pos changed: #{pos}"
    #@server.playlist.entries.notifier do |playlist|
    #  @server.medialib_get_info( playlist[pos[:position]] ).notifier do |songinfo|
    #    @nextSongLbl.text = ""
    #    songinfo.each_pair do |key, info|
    #      info.each_pair do |src, value|
    #        @nextSongLbl.text += "[#{src}] #{key} = #{value}\n"
    #      end
    #    end
    #    true
    #  end
    #  true
    #end
    true
  end


  def handle_playlist_loaded( *foo )
    p "Playlist loaded: #{foo}"
    true
  end
  def handle_quit( *foo )
    p "Server quit: #{foo}"
    true
  end


  def xmmspls_to_lststor( songids, store )
    songinfos = []
    store.clear
    songids.each do |id|
      current_row = store.append
      @server.medialib_get_info( id ).notifier do |songinfo|
        songinfos[id] = songinfo
        current_row[0] = id.to_s
        current_row[1] = songinfo[:artist] ? songinfo[:artist].first[1] : ""
        current_row[2] = songinfo[:title] ? songinfo[:title].first[1] : ""
        true
      end
    end
    return [songinfos, store]
  end



  def start_playback
    @server.playback_start.notifier do |res|
      @playBtn.signal_handler_block( @playBtnClkdHndlr ) do
        @playBtn.active = true
      end
      @pauseBtn.signal_handler_block( @pauseBtnClkdHndlr ) do
        @pauseBtn.active = false
      end
      @stopBtn.signal_handler_block( @stopBtnClkdHndlr ) do
        @stopBtn.active = false
      end
      @statusBar.push( @statusBar.get_context_id( "playback" ), "Started playback" )
      true
    end
  end


  def pause_playback
    @server.playback_pause.notifier do |res|
      @playBtn.signal_handler_block( @playBtnClkdHndlr ) do
        @playBtn.active = false
      end
      @pauseBtn.signal_handler_block( @pauseBtnClkdHndlr ) do
        @pauseBtn.active = true
      end
      @stopBtn.signal_handler_block( @stopBtnClkdHndlr ) do
        @stopBtn.active = false
      end
      @statusBar.push( @statusBar.get_context_id( "playback" ), "Paused playback" )
      true
    end
  end


  def stop_playback
    @server.playback_stop.notifier do |res|
      @playBtn.signal_handler_block( @playBtnClkdHndlr ) do
        @playBtn.active = false
      end
      @pauseBtn.signal_handler_block( @pauseBtnClkdHndlr ) do
        @pauseBtn.active = false
      end
      @stopBtn.signal_handler_block( @stopBtnClkdHndlr ) do
        @stopBtn.active = true
      end
      @statusBar.push( @statusBar.get_context_id( "playback" ), "Stopped playback" )
      true
    end
  end


  def toggle_playback
    if @playbackState == 1
      @server.playback_pause.notifier do |res|
        @statusBar.push( @statusBar.get_context_id( "playback" ), "Paused playback" )
        true
      end
    elsif @playbackState == ( 0 || 2 )
      @server.playback_start.notifier do |res|
        @statusBar.push( @statusBar.get_context_id( "playback" ), "Started playback" )
        true
      end
    else
      warn "Playbackstate unknown"
      @server.playback_start.notifier do |res|
        @statusBar.push( @statusBar.get_context_id( "playback" ), "Started playback" )
        true
      end
    end
  end


  def step_in_playlist( offset = 1 )
    @server.playlist_set_next_rel( offset ).notifier do
      @server.playback_tickle.notifier do |res|
        @statusBar.push( @statusBar.get_context_id( "playback" ), "Skipped playback by #{offset} song#{offset.abs >= 1 ? "s" : ''}" )
        true
      end
      true
    end
  end


  def handle_repeat_all_state( state )
    @currentconfig[:"playlist.repeat_all"] = ( state ? "1" : "0" )
    @rptBtn.signal_handler_block( @rptBtnClkdHndlr ) do
      @rptBtn.active = state
    end
  end


  def handle_repeat_one_state( state )
    @currentconfig[:"playlist.repeat_one"] = ( state ? "1" : "0" )
    @rpt1Btn.signal_handler_block( @rpt1BtnClkdHndlr ) do
      @rpt1Btn.active = state
    end
  end


  def toggle_repeat_all
    if @currentconfig[:"playlist.repeat_all"] == "1"
      @server.config_set_value( "playlist.repeat_all", "0" ).notifier do |res|
        @server.config_get_value( "playlist.repeat_all" ).notifier do |value|
          self.handle_repeat_all_state( value == "1" ? true : false )
          true
        end
        true
      end
    elsif @currentconfig[:"playlist.repeat_all"] == "0"
      @server.config_set_value( "playlist.repeat_all", "1" ).notifier do |res|
        @server.config_get_value( "playlist.repeat_all" ).notifier do |value|
          self.handle_repeat_all_state( value == "1" ? true : false )
          true
        end
        true
      end
    else
      warn "Unknown state: #{@currentconfig[:"playlist.repeat_all"]}"
    end
    true
  end


  def toggle_repeat_one
    if @currentconfig[:"playlist.repeat_one"] == "1"
      @server.config_set_value( "playlist.repeat_one", "0" ).notifier do |res|
        @server.config_get_value( "playlist.repeat_one" ).notifier do |value|
          self.handle_repeat_one_state( value == "1" ? true : false )
          true
        end
        true
      end
    elsif @currentconfig[:"playlist.repeat_one"] == "0"
      @server.config_set_value( "playlist.repeat_one", "1" ).notifier do |res|
        @server.config_get_value( "playlist.repeat_one" ).notifier do |value|
          self.handle_repeat_one_state( value == "1" ? true : false )
          true
        end
        true
      end
    else
      warn "Unknown state: #{@currentconfig[:"playlist.repeat_one"]}"
    end
    true
  end


  def resync_complete
    @server.playlist.entries.notifier do |playlistids|
      @status[:playlistids] = playlistids
      @status[:playlist], @playlistView.model = self.xmmspls_to_lststor( playlistids, @playlistStore )
      true
    end

    @server.coll_query_ids( Xmms::Collection.universe ).notifier do |medialibids|
      @status[:medialibids] = medialibids
      @status[:medialib], @medialibView.model = self.xmmspls_to_lststor( medialibids, @medialibStore )
      true
    end

    @server.playback_current_id.notifier do |id|
      self.handle_playback_current_id id
      true
    end

    @server.config_list_values.notifier do |res|
      @currentconfig = {}
      res.each_pair do |key, value|
        @currentconfig[key] = value
      end

      @rpt1Btn.signal_handler_block( @rpt1BtnClkdHndlr ) do
        @rpt1Btn.active = if( @currentconfig[:"playlist.repeat_one"] == "1" )
                            true
                          else
                            false
                          end
      end
      @rptBtn.signal_handler_block( @rptBtnClkdHndlr ) do
        @rptBtn.active = if @currentconfig[:"playlist.repeat_all"] == "1"
                           true
                         else
                           false
                         end
      end
      true
    end

    @server.coll_list.notifier do |colls|
      colls.each do |collname|
        @server.coll_get( collname ).notifier do |coll|
          @server.coll_query_ids( coll ).notifier do |ids|
            p collname
            p ids.join( "', '" )
            true
          end
          true
        end
      end
      true
    end

    true
  end
end


class Grux::MainWin < Gtk::Window
  def initialize
    super

    @mainCont       = Gtk::VBox.new
    @btnCont        = Gtk::HBox.new
    @clntCont       = Gtk::Notebook.new

    @cnnctInput     = Gtk::ComboBoxEntry.new
    @cnnctBtn       = Gtk::Button.new "_Connect"
    @quitBtn        = Gtk::Button.new "_Quit"

    self.title      = "Grux"

    @winDstryHndlr  = self.signal_connect :destroy do
      Gtk.main_quit
    end

    @cnnctInput.append_text 'tcp://127.0.0.1:9667'

    @cnnctBtnHndlr  = @cnnctBtn.signal_connect :clicked do
      if not @cnnctInput.active_text.empty?
        server_address = @cnnctInput.active_text
      else
        server_address = ENV['XMMS_PATH']
      end

      @clntCont.append_page( Grux::ClientWin.new( server_address ), Gtk::Label.new( server_address ) )
    end
    @quitBtnHndlr   = @quitBtn.signal_connect :clicked do
      Gtk.main_quit
    end

    @btnCont.pack_start @cnnctInput
    @btnCont.pack_start @cnnctBtn
    @btnCont.pack_end @quitBtn

    @mainCont.pack_start @btnCont, false
    @mainCont.pack_end @clntCont

    self.add @mainCont
    self.show_all
  end
end


gruxWin = Grux::MainWin.new
Gtk.main


#:added
#:chain
#:id
#:laststarted
#:status
#:timesplayed
#:url