WasteBasket.lua 4.05 KB
Newer Older
Ole Tange's avatar
Ole Tange committed
1
2
3
4
5
6
7
8
9
--[[
INSTALLATION (create directories if they donot exist):
- put the file in the VLC subdir /lua/extensions, by default:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
* Windows (current user): %APPDATA%\VLC\lua\extensions\
* Linux (all users): /usr/share/vlc/lua/extensions/
* Linux (current user): ~/.local/share/vlc/lua/extensions/
* Mac OS X (all users): /Applications/VLC.app/Contents/MacOS/share/lua/extensions/
- Restart VLC.
10
11
- The extension can then be found in the menu:
    View > Move current playing file into wastebasket
Ole Tange's avatar
Ole Tange committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
]]--

--[[ Extension description ]]

function descriptor()
  return { title = "Wastebasket" ;
    version = "0.9" ;
    author = "Mark Morschhäuser/Ole Tange" ;
    shortdesc = "Move current playing file into wastebasket";
    description = "<h1>Wastebasket</h1>"
    .. "When you're playing a file, use Wastebasket to "
    .. "easily move this file to a .waste-dir with one click. "
    .. "<br>This will NOT change your playlist, it will move the file itself. "
    .. "<br>Wastebasket will search for a dir called .waste "
    .. "in the dir of the file and all parent dirs of that.";
Ole Tange's avatar
Ole Tange committed
27
    url = "https://gitlab.com/ole.tange/tangetools/tree/master/wastebasket"
Ole Tange's avatar
Ole Tange committed
28
29
30
31
32
33
34
  }
end

--[[ Hooks ]]

-- Activation hook
function activate()
Ole Tange's avatar
Ole Tange committed
35
  local filename,dst,wdir = filename_dst_wastedir()
Ole Tange's avatar
Ole Tange committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  if(directory_exists(wdir)) then
    d = vlc.dialog("Wastebasket")
    d:add_label("Move <b>".. filename .. "</b> to <b>" .. wdir .. "</b>?")
    d:add_button("Move", delete)
    d:add_button("Cancel", close)
    d:show()
  else
    d = vlc.dialog("Wastebasket - no dir found")
    d:add_label(".waste is not found anywhere in parent dirs")
    d:add_button("Cancel", close)
    d:show()
  end
  vlc.msg.dbg("[Wastebasket] Activated")
end

Ole Tange's avatar
Ole Tange committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function filename_dst_wastedir()
  -- get the current playing file
  local item = vlc.input.item()
  -- extract its URI
  local uri = item:uri()
  -- decode %foo stuff from the URI
  local filename = vlc.strings.decode_uri(uri)
  -- remove 'file://' prefix which is 7 chars long
  filename = string.sub(filename,8)

  -- find .waste in parent dirs
  local wdir = wastedir(dirname(filename))
  return filename,wdir .. "/" .. basename(filename),wdir
end

Ole Tange's avatar
Ole Tange committed
66
67
68
69
function wastedir(dir)
  -- recursively search for .waste in parent dir

  vlc.msg.dbg("[Wastebasket/wastedir] Looking at " .. dir)
Ole Tange's avatar
Ole Tange committed
70
  local wdir = dir .. "/" .. ".waste"
Ole Tange's avatar
Ole Tange committed
71
72
73
74
75
  if directory_exists(wdir) then
     vlc.msg.dbg("[Wastebasket/wastedir] Found wastedir: " .. wdir)
     return wdir
  end
  -- try the parent dir
Ole Tange's avatar
Ole Tange committed
76
  local parent = dirname(dir)
77
78
79
80
81
  if(parent == dir) then
    -- we have reached root (/)
    -- return wdir (which does not exist)
    return wdir
  end
Ole Tange's avatar
Ole Tange committed
82
83
84
85
86
87
88
89
90
91
  vlc.msg.dbg("[Wastebasket/wastedir] parent " .. parent)
  if directory_exists(parent) then
    return wastedir(parent)
  else
    return parent
  end
end

function directory_exists(dir)
  -- Simple checker if dir exists
92
93
94
  -- shell quote the dirname
  dir, _ = dir:gsub("([\002-\009\011-\026\\#?`(){}%[%]^*<>=~|; \"!$&'\130-\255])", "\\%1")
  dir, _ = dir:gsub("\n", "'\n'")
Ole Tange's avatar
Ole Tange committed
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
  return os.execute("cd " .. dir)
end

function deactivate()
  -- Deactivation hook
  vlc.msg.dbg("[Wastebasket] Deactivated")
  vlc.deactivate()
end

function close()
  deactivate()
end

--- Function equivalent to basename in POSIX systems
--@param str the path string
function basename(str)
  local name = string.gsub(str, "(.*/)(.*)", "%2")
  return name
end

function dirname(str)
  local name = string.gsub(str, "(.*)/(.*)", "%1")
  return name
end

function delete()
Ole Tange's avatar
Ole Tange committed
121
  local filename,dst,wdir = filename_dst_wastedir()
Ole Tange's avatar
Ole Tange committed
122
123
  if(directory_exists(wdir)) then
    vlc.msg.dbg("[Wastebasket]: Move to " .. dst)
Ole Tange's avatar
Ole Tange committed
124
    local retval, err = os.rename(filename,dst)
Ole Tange's avatar
Ole Tange committed
125
    if(retval == nil) then
Ole Tange's avatar
Ole Tange committed
126
      -- error handling; if moving failed, print why
Ole Tange's avatar
Ole Tange committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      vlc.msg.dbg("[Wastebasket] error: " .. err)
    end
  else
    d = vlc.dialog("Wastebasket - no dir found")
    d:add_label(".waste is not found anywhere in parent dirs")
    d:add_button("Cancel", close)
    d:show()
  end
  close()
end

-- This empty function is there, because vlc pested me otherwise
function meta_changed()
end