]> gitweb.fluxo.info Git - awesompd.git/commitdiff
Implemented popup promptbox
authorAlexander Yakushev <yakushev.alex@gmail.com>
Sun, 24 Jul 2011 21:02:20 +0000 (00:02 +0300)
committerAlexander Yakushev <yakushev.alex@gmail.com>
Sun, 24 Jul 2011 21:02:20 +0000 (00:02 +0300)
awesompd.lua
jamendo.lua

index 8243542c7d1511b4bd64f834a0aab7f2830a34f5..77f129a1011219633363433286d723a9606077dc 100644 (file)
@@ -6,10 +6,11 @@
 
 require('utf8')
 require('jamendo')
-
+local beautiful = require('beautiful')
 local naughty = naughty
 local awful = awful
 
+
 -- Debug stuff
 
 local enable_dbg = false
@@ -300,23 +301,6 @@ function awesompd:command_show_menu()
           end 
 end
    
--- function awesompd:add_tracks_from_jamendo(parse_table,format)
---    if (table.getn(parse_table) > 0) then
---       local trygetlink = 
---          assert(io.popen("echo $(curl -w %{redirect_url} " .. 
---                          "'http://api.jamendo.com/get2/stream/track/redirect/" .. 
---                          "?streamencoding="..format.."&id=729304')"),'r'):read("*lines")
---       local _, _, prefix = string.find(trygetlink,"stream(%d+)\.jamendo\.com")
---       for i = 1,table.getn(parse_table) do
---          track_link = "http://stream" .. prefix .. ".jamendo.com/stream/" 
---             .. parse_table[i].id .."/".. format .."/"
---          self:command("add " .. track_link)
---          self.jamendo_list[parse_table[i].id] = 
---             parse_table[i].artist .. " - " .. parse_table[i].track
---       end
---    end
--- end
-
 function awesompd:add_jamendo_top(format)
    return function ()
              local track_table = jamendo.return_track_table()
@@ -345,13 +329,18 @@ function awesompd:get_playback_menu()
          if self.current_number ~= 1 then
             table.insert(new_menu, 
                          { "Prev: " .. 
-                           awesompd.protect_string(self.list_array[self.current_number - 1],
+                           awesompd.protect_string(jamendo.replace_link(
+                                                      self.list_array[self.current_number - 1]),
                                                    true),
-                                     self:command_prev_track(), self.ICONS.PREV })
+                        self:command_prev_track(), self.ICONS.PREV })
          end
          if self.current_number ~= table.getn(self.list_array) then
-            table.insert(new_menu, { "Next: " .. awesompd.protect_string(self.list_array[self.current_number + 1], true), 
-                                     self:command_next_track(), self.ICONS.NEXT })
+            table.insert(new_menu, 
+                         { "Next: " .. 
+                           awesompd.protect_string(jamendo.replace_link(
+                                                      self.list_array[self.current_number + 1]), 
+                                                   true), 
+                        self:command_next_track(), self.ICONS.NEXT })
          end
          table.insert(new_menu, { "Stop", self:command_stop(), self.ICONS.STOP })
       end
@@ -370,19 +359,11 @@ function awesompd:get_list_menu()
         local start_num = (self.current_number - 15 > 0) and self.current_number - 15 or 1
         local end_num = (self.current_number + 15 < total_count ) and self.current_number + 15 or total_count
         for i = start_num, end_num do
-            if (string.find(self.list_array[i],"jamendo.com")) then
-               table.insert(new_menu, { jamendo.get_name_by_link(self.list_array[i]),
-                                        self:command_play_specific(i),
-                                        self.current_number == i and 
-                                           (self.status == "Playing" and self.ICONS.PLAY or self.ICONS.PAUSE)
-                                        or nil} )
-            else 
-               table.insert(new_menu, { awesompd.protect_string(self.list_array[i], true),
-                                        self:command_play_specific(i),
-                                        self.current_number == i and 
-                                           (self.status == "Playing" and self.ICONS.PLAY or self.ICONS.PAUSE)
-                                        or nil} )
-            end           
+            table.insert(new_menu, { jamendo.replace_link(self.list_array[i]),
+                                     self:command_play_specific(i),
+                                     self.current_number == i and 
+                                        (self.status == "Playing" and self.ICONS.PLAY or self.ICONS.PAUSE)
+                                     or nil} )
         end
       end
       self.recreate_list = false
@@ -645,11 +626,7 @@ function awesompd:update_track(file)
          self:update_state(options_line)
         local new_track = track_line
         if new_track ~= self.unique_text then
-            if (string.find(new_track,"jamendo.com")) then
-               self.text = jamendo.get_name_by_link(new_track)
-            else
-               self.text = new_track
-            end
+            self.text = jamendo.replace_link(new_track)
             self.unique_text = new_track
            self.to_notify = true
            self.recreate_menu = true
@@ -721,3 +698,45 @@ function awesompd.protect_string(str, for_menu)
       return utf8replace(str, awesompd.ESCAPE_SYMBOL_MAPPING)
    end
 end
+
+-- Displays a inputbox on the screen (looks like naugty with prompt).
+-- title_text - bold text in the first line
+-- prompt_text - preceding text on the second line
+-- hook - function that will be called with input data
+function awesompd:display_inputbox(title_text, prompt_text, hook)
+   if self.inputbox then -- Inputbox already exists, do nothing
+      return
+   end
+   local width = 200
+   local height = 30
+   local border_color = beautiful.bg_focus or '#535d6c'
+   local margin = 5
+   local wbox = wibox({ name = "awmpd_ibox", height = height , width = width, 
+                        border_color = border_color, border_width = 1 })
+   self.inputbox = wbox
+   local ws = screen[mouse.screen].workarea
+
+   wbox:geometry({ x = ws.width - width - 5, y = 25 })
+   wbox.screen = mouse.screen
+   wbox.ontop = true
+
+   local exe_callback = function(s)
+                           hook(s)
+                           wbox.screen = nil
+                           self.inputbox = nil
+                        end
+   local done_callback = function()
+                            wbox.screen = nil
+                            self.inputbox = nil
+                         end
+   local wprompt = awful.widget.prompt({ layout = awful.widget.layout.horizontal.leftright })
+   local wtbox = widget({ type = "textbox" })
+   wtbox:margin({ right = margin, left = margin, bottom = margin, top = margin })
+   wtbox.text = "<b>" .. title_text .. "</b>"
+   wbox.widgets = { wtbox, wprompt, layout = awful.widget.layout.vertical.flex }
+   awful.prompt.run( { prompt = " " .. prompt_text .. ": " }, wprompt.widget, 
+                     exe_callback, nil, nil, nil, done_callback)
+end
+
+-- Use it like this
+-- self:display_inputbox("Search music on Jamendo", "Artist", print)
index 1f4a915e68f5228802624b575949c7efb7d23d04..a4c45c847b1123b8860433edae66b0d96c772b0b 100644 (file)
@@ -29,7 +29,9 @@ SEARCH_ALBUM = { display = "Album",
 SEARCH_TAG = { display = "Tag",
                value = "tag_idstr" }
 
-current_request_table = { format = FORMAT_MP3,
+current_request_table = { unit = "track", 
+                          fields = {"id", "artist_name", "name", "stream"}, 
+                          format = FORMAT_MP3,
                           order = ORDER_RATINGWEEKLY }
 
 -- Local variables
@@ -70,6 +72,18 @@ function get_name_by_link(link)
    return jamendo_list[get_id_from_link(link)]
 end
 
+-- If a track is actually a Jamendo stream, replace it with normal
+-- track name.
+function replace_link(track)
+   if string.find(track,"jamendo.com/stream") then
+      local track_name = get_name_by_link(track)
+      if track_name then
+         return track_name
+      end
+   end
+   return track
+end
+
 -- Returns table of track IDs, names and other things based on the
 -- request table.
 function return_track_table(request_table)
@@ -96,32 +110,24 @@ end
 -- table. If request_table is nil, uses current_request_table instead.
 function form_request(request_table)
    local curl_str = 'echo $(curl -w %%{redirect_url} ' ..
-      '"http://api.jamendo.com/get2/id+artist_name+name+stream/' ..
-      'track/json/track_album+album_artist/?n=100&order=%s&streamencoding=%s")'
-   if request_table then
-      local format = request_table.format or current_request_table.format
-      local order = request_table.order or current_request_table.order
-      return string.format(curl_str, order.value, format.value)
-   else
-      print("Request : " .. string.format(curl_str, 
-                           current_request_table.order.value,
-                                        current_request_table.format.value))
-      return string.format(curl_str, 
-                           current_request_table.order.value,
-                           current_request_table.format.value)
+      '"http://api.jamendo.com/get2/%s/' ..
+      '%s/json/track_album+album_artist/?n=100&order=%s&streamencoding=%s")'
+   request_table = request_table or current_request_table
+   
+   local fields = request_table.fields or current_request_table.fields
+   local field_string = ""
+   for i = 1, table.getn(fields) do
+      field_string = field_string .. fields[i] .. "+"
    end
+   field_string = string.sub(field_string,1,string.len(field_string)-1)
+   local unit = request_table.unit or current_request_table.unit
+   local format = request_table.format or current_request_table.format
+   local order = request_table.order or current_request_table.order
+   
+   print("Request : " .. string.format(curl_str, field_string, unit, order.value, format.value))
+   return string.format(curl_str, field_string, unit, order.value, format.value)
 end
 
-
-
-
-
-
-
-
-
-
-
 -- Primitive function for parsing Jamendo API JSON response.  Does not
 -- support arrays. Supports only strings and numbers as values.
 -- Provides basic safety (correctly handles special symbols like comma