new feature: allow for files and directories deletion in browser

This commit is contained in:
Andrzej Rybczak
2009-05-23 17:09:41 +02:00
parent ecd4c8cc17
commit 8f5df28e62
7 changed files with 115 additions and 1 deletions

View File

@@ -465,6 +465,41 @@ void Browser::GetDirectory(string dir, string subdir)
w->Highlight(highlightme);
}
void Browser::ClearDirectory(const std::string &path) const
{
DIR *dir = opendir(path.c_str());
if (!dir)
return;
dirent *file;
struct stat file_stat;
string full_path;
// omit . and ..
for (int i = 0; i < 2; i++)
{
file = readdir(dir);
if (!file)
{
closedir(dir);
return;
}
}
while ((file = readdir(dir)))
{
full_path = path;
if (*full_path.rbegin() != '/')
full_path += '/';
full_path += file->d_name;
lstat(full_path.c_str(), &file_stat);
if (S_ISDIR(file_stat.st_mode))
ClearDirectory(full_path);
remove(full_path.c_str());
}
closedir(dir);
}
void Browser::ChangeBrowseMode()
{
if (Mpd->GetHostname()[0] != '/')

View File

@@ -52,6 +52,7 @@ class Browser : public Screen< Menu<MPD::Item> >
void LocateSong(const MPD::Song &);
void GetDirectory(std::string, std::string = "/");
void ClearDirectory(const std::string &) const;
void ChangeBrowseMode();
void UpdateItemList();

View File

@@ -18,6 +18,7 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <cerrno>
#include <clocale>
#include <csignal>
@@ -513,7 +514,56 @@ int main(int argc, char *argv[])
}
else if (myScreen == myBrowser && !myBrowser->Main()->Empty() && myBrowser->Main()->Current().type != itPlaylist)
{
// delete song/dir implementation here
MPD::Item &item = myBrowser->Main()->Current();
if (item.type == itSong && !Config.allow_physical_files_deletion)
{
ShowMessage("Deleting files is disabled by default, see man page for more details");
continue;
}
if (item.type == itDirectory && (item.song || !Config.allow_physical_directories_deletion)) // [..]
{
ShowMessage("Deleting directories is disabled by default, see man page for more details");
continue;
}
LockStatusbar();
Statusbar() << "Delete " << (item.type == itSong ? "file" : "directory") << ' ' << (item.type == itSong ? item.song->GetName() : item.name) << " ? [y/n] ";
curs_set(1);
int in = 0;
do
{
TraceMpdStatus();
wFooter->ReadKey(in);
}
while (in != 'y' && in != 'n');
if (in == 'y')
{
ShowMessage("Deleting...");
string path;
if (!Config.local_browser)
path = Config.mpd_music_dir;
path += item.type == itSong ? item.song->GetFile() : item.name;
if (item.type == itDirectory)
myBrowser->ClearDirectory(path);
if (remove(path.c_str()) == 0)
{
ShowMessage("%s has been deleted!", item.type == itSong ? "File" : "Directory");
if (!Config.local_browser)
Mpd->UpdateDirectory(myBrowser->CurrentDir());
else
myBrowser->GetDirectory(myBrowser->CurrentDir());
}
else
ShowMessage("Deletion failed: %s", strerror(errno));
}
else
ShowMessage("Aborted!");
curs_set(0);
UnlockStatusbar();
}
else if (myScreen->ActiveWindow() == myPlaylistEditor->Content && !myPlaylistEditor->Content->Empty())
{

View File

@@ -278,6 +278,8 @@ void DefaultConfiguration(ncmpcpp_config &conf)
conf.block_search_constraints_change = true;
conf.use_console_editor = false;
conf.use_cyclic_scrolling = false;
conf.allow_physical_files_deletion = false;
conf.allow_physical_directories_deletion = false;
conf.set_window_title = true;
conf.mpd_port = 6600;
conf.mpd_connection_timeout = 15;
@@ -669,6 +671,14 @@ void ReadConfiguration(ncmpcpp_config &conf)
{
conf.block_search_constraints_change = v == "yes";
}
else if (cl.find("allow_physical_files_deletion") != string::npos)
{
conf.allow_physical_files_deletion = v == "yes";
}
else if (cl.find("allow_physical_directories_deletion") != string::npos)
{
conf.allow_physical_directories_deletion = v == "yes";
}
else if (cl.find("enable_window_title") != string::npos)
{
conf.set_window_title = v == "yes";

View File

@@ -170,6 +170,8 @@ struct ncmpcpp_config
bool block_search_constraints_change;
bool use_console_editor;
bool use_cyclic_scrolling;
bool allow_physical_files_deletion;
bool allow_physical_directories_deletion;
int mpd_port;
int mpd_connection_timeout;