]> gitweb.fluxo.info Git - awesompd.git/commitdiff
Fixed bug with widget crashing after restarting awesome
authorAlexander Yakushev <yakushev.alex@gmail.com>
Fri, 10 Jun 2011 19:36:19 +0000 (22:36 +0300)
committerAlexander Yakushev <yakushev.alex@gmail.com>
Fri, 10 Jun 2011 19:36:19 +0000 (22:36 +0300)
awesompd.lua

index e0b40252fbf3589569c3abf1e705f57023fe5f60..6a452ce8c5ea72bfcb16cb44f8a27b21ade210a8 100644 (file)
@@ -65,6 +65,8 @@ function awesompd:create()
    instance.status_text = "Stopped"
    instance.to_notify = false
    instance.connected = true
+   instance.jamendo_list = {}
+
 --   instance.promptbox = {}
 --   for s = 1, screen.count() do
 --      instance.promptbox[s] = awful.widget.prompt({ layout = awful.widget.layout.horizontal.leftright })      
@@ -102,6 +104,7 @@ function awesompd:run()
 --   self:update_state()
    self:check_playlists()
    self.load_icons(self.path_to_icons)
+   self:retrieve_cache()
    self.update_widget_timer = timer({ timeout = 1 })
    self.update_widget_timer:add_signal("timeout", function () self:update_widget() end)
    self.update_widget_timer:start()
@@ -285,14 +288,14 @@ function awesompd:add_jamendo_top(prefix,format)
                                                                                 track = (_track or ""),
                                                                                 artist = (_artist or "")})
                                                               end)
-      self.jamendo_list = {}
       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[track_link] = parse_table[i].artist .. " - " .. parse_table[i].track
+         self.jamendo_list[parse_table[i].id] = parse_table[i].artist .. " - " .. parse_table[i].track
       end
       self.recreate_menu = true
       self.recreate_list = true
+      self:save_cache()
    end
 end
 
@@ -333,7 +336,7 @@ function awesompd:get_list_menu()
         for i = start_num, end_num do
             print(self.list_array[i])
             if (string.find(self.list_array[i],"jamendo.com")) then
-               table.insert(new_menu, { self.jamendo_list[(self.list_array[i])],
+               table.insert(new_menu, { self.jamendo_list[awesompd.get_id_from_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)
@@ -497,6 +500,35 @@ function awesompd:wrap_output(text)
    return '<span font="' .. self.font .. '">| ' .. text .. ' |</span>'
 end
 
+function awesompd:retrieve_cache()
+   if self.path_to_icons == "" then
+      self.filename = "jamendo_cache"
+   else
+      self.filename = self.path_to_icons .. "/jamendo_cache"
+   end
+   local bus = io.open(self.filename)
+   if bus then
+      for l in bus:lines() do
+         local _, _, id, track = string.find(l,"(%d+)-(.+)")
+         self.jamendo_list[id] = track
+      end
+   end
+end
+
+function awesompd:save_cache()
+   local bus = io.open(self.filename, "w")
+   for id,name in pairs(self.jamendo_list) do
+      bus:write(id.."-"..name.."\n")
+   end
+   bus:flush()
+   bus:close()
+end
+
+function awesompd.get_id_from_link(link)
+   local _, _, id = string.find(link,"stream/(%d+)")
+   return id
+end
+
 function awesompd.split (s,t)
    local l = {n=0}
    local f = function (s)
@@ -598,7 +630,7 @@ function awesompd:update_track()
         local new_track = awesompd.protect_string(info_ar[1])
         if new_track ~= self.unique_text then
             if (string.find(new_track,"jamendo.com")) then
-               self.text = self.jamendo_list[new_track]
+               self.text = self.jamendo_list[awesompd.get_id_from_link(new_track)]
             else
                self.text = new_track
             end