move some functions to TagEditor class / outside

This commit is contained in:
Andrzej Rybczak
2009-03-04 22:47:48 +01:00
parent 31dcb27789
commit 8f092d5490
8 changed files with 184 additions and 171 deletions

View File

@@ -239,6 +239,35 @@ bool Keypressed(int in, const int *key)
return in == key[0] || in == key[1];
}
#ifdef HAVE_TAGLIB_H
string FindSharedDir(Menu<Song> *menu)
{
SongList list;
for (size_t i = 0; i < menu->Size(); i++)
list.push_back(&menu->at(i));
return FindSharedDir(list);
}
string FindSharedDir(const SongList &v)
{
string result;
if (!v.empty())
{
result = v.front()->GetFile();
for (SongList::const_iterator it = v.begin()+1; it != v.end(); it++)
{
int i = 1;
while (result.substr(0, i) == (*it)->GetFile().substr(0, i))
i++;
result = result.substr(0, i);
}
size_t slash = result.rfind("/");
result = slash != string::npos ? result.substr(0, slash) : "/";
}
return result;
}
#endif // HAVE_TAGLIB_H
string FindSharedDir(const string &one, const string &two)
{
if (one == two)

View File

@@ -55,6 +55,10 @@ void UpdateSongList(Menu<MPD::Song> *);
bool Keypressed(int, const int *);
#ifdef HAVE_TAGLIB_H
std::string FindSharedDir(Menu<MPD::Song> *);
std::string FindSharedDir(const MPD::SongList &);
#endif // HAVE_TAGLIB_H
std::string FindSharedDir(const std::string &, const std::string &);
std::string GetLineValue(std::string &, char = '"', char = '"', bool = 0);

View File

@@ -145,6 +145,39 @@ mpd_TagItems IntoTagItem(char c)
}
}
#ifdef HAVE_TAGLIB_H
MPD::Song::SetFunction IntoSetFunction(mpd_TagItems tag)
{
switch (tag)
{
case MPD_TAG_ITEM_ARTIST:
return &MPD::Song::SetArtist;
case MPD_TAG_ITEM_ALBUM:
return &MPD::Song::SetAlbum;
case MPD_TAG_ITEM_TITLE:
return &MPD::Song::SetTitle;
case MPD_TAG_ITEM_TRACK:
return &MPD::Song::SetTrack;
case MPD_TAG_ITEM_GENRE:
return &MPD::Song::SetGenre;
case MPD_TAG_ITEM_DATE:
return &MPD::Song::SetYear;
case MPD_TAG_ITEM_COMPOSER:
return &MPD::Song::SetComposer;
case MPD_TAG_ITEM_PERFORMER:
return &MPD::Song::SetPerformer;
case MPD_TAG_ITEM_COMMENT:
return &MPD::Song::SetComment;
case MPD_TAG_ITEM_DISC:
return &MPD::Song::SetDisc;
case MPD_TAG_ITEM_FILENAME:
return &MPD::Song::SetNewName;
default:
return 0;
}
}
#endif // HAVE_TAGLIB_H
void EscapeUnallowedChars(std::string &s)
{
for (std::string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); it++)

View File

@@ -24,7 +24,7 @@
#include <string>
#include "window.h"
#include "libmpdclient.h"
#include "song.h"
void ToLower(std::string &);
@@ -40,6 +40,10 @@ Color IntoColor(const std::string &);
mpd_TagItems IntoTagItem(char);
#ifdef HAVE_TAGLIB_H
MPD::Song::SetFunction IntoSetFunction(mpd_TagItems);
#endif // HAVE_TAGLIB_H
void EscapeUnallowedChars(std::string &);
void EscapeHtml(std::string &s);

View File

@@ -27,6 +27,7 @@
#include <sstream>
#include "charset.h"
#include "misc.h"
#include "song.h"
using MPD::Song;

View File

@@ -23,7 +23,6 @@
#include <string>
#include "misc.h"
#include "libmpdclient.h"
namespace MPD

View File

@@ -287,6 +287,9 @@ bool TinyTagEditor::GetTags()
TagEditor *myTagEditor = new TagEditor;
const string TagEditor::PatternsFile = config_dir + "patterns.list";
vector<string> TagEditor::Patterns;
const size_t TagEditor::MiddleColumnWidth = 26;
size_t TagEditor::LeftColumnWidth;
size_t TagEditor::MiddleColumnStartX;
@@ -1046,186 +1049,122 @@ std::string TagEditor::TagToString(const MPD::Song &s, void *data)
return result.empty() ? Config.empty_tag : result;
}
namespace
void TagEditor::GetPatternList()
{
const string patterns_list_file = config_dir + "patterns.list";
vector<string> patterns_list;
void GetPatternList()
if (Patterns.empty())
{
if (patterns_list.empty())
std::ifstream input(PatternsFile.c_str());
if (input.is_open())
{
std::ifstream input(patterns_list_file.c_str());
if (input.is_open())
string line;
while (getline(input, line))
{
string line;
while (getline(input, line))
{
if (!line.empty())
patterns_list.push_back(line);
}
input.close();
if (!line.empty())
Patterns.push_back(line);
}
input.close();
}
}
void SavePatternList()
{
std::ofstream output(patterns_list_file.c_str());
if (output.is_open())
{
for (vector<string>::const_iterator it = patterns_list.begin(); it != patterns_list.end() && it != patterns_list.begin()+30; it++)
output << *it << std::endl;
output.close();
}
}
Song::SetFunction IntoSetFunction(char c)
{
switch (c)
{
case 'a':
return &Song::SetArtist;
case 't':
return &Song::SetTitle;
case 'b':
return &Song::SetAlbum;
case 'y':
return &Song::SetYear;
case 'n':
return &Song::SetTrack;
case 'g':
return &Song::SetGenre;
case 'c':
return &Song::SetComposer;
case 'p':
return &Song::SetPerformer;
case 'd':
return &Song::SetDisc;
case 'C':
return &Song::SetComment;
default:
return NULL;
}
}
string GenerateFilename(const Song &s, string &pattern)
{
string result = s.toString(pattern);
EscapeUnallowedChars(result);
return result;
}
string ParseFilename(Song &s, string mask, bool preview)
{
std::ostringstream result;
vector<string> separators;
vector< std::pair<char, string> > tags;
string file = s.GetName().substr(0, s.GetName().rfind("."));
try
{
for (size_t i = mask.find("%"); i != string::npos; i = mask.find("%"))
{
tags.push_back(make_pair(mask.at(i+1), ""));
mask = mask.substr(i+2);
i = mask.find("%");
if (!mask.empty())
separators.push_back(mask.substr(0, i));
}
int i = 0;
for (vector<string>::const_iterator it = separators.begin(); it != separators.end(); it++, i++)
{
int j = file.find(*it);
tags.at(i).second = file.substr(0, j);
file = file.substr(j+it->length());
}
if (!file.empty())
tags.at(i).second = file;
}
catch (std::out_of_range)
{
return "Error while parsing filename!";
}
for (vector< std::pair<char, string> >::iterator it = tags.begin(); it != tags.end(); it++)
{
for (string::iterator j = it->second.begin(); j != it->second.end(); j++)
if (*j == '_')
*j = ' ';
if (!preview)
{
Song::SetFunction set = IntoSetFunction(it->first);
if (set)
(s.*set)(it->second);
}
else
result << "%" << it->first << ": " << it->second << "\n";
}
return result.str();
}
}
Song::SetFunction IntoSetFunction(mpd_TagItems tag)
void TagEditor::SavePatternList()
{
switch (tag)
std::ofstream output(PatternsFile.c_str());
if (output.is_open())
{
case MPD_TAG_ITEM_ARTIST:
for (vector<string>::const_iterator it = Patterns.begin(); it != Patterns.end() && it != Patterns.begin()+30; it++)
output << *it << std::endl;
output.close();
}
}
Song::SetFunction TagEditor::IntoSetFunction(char c)
{
switch (c)
{
case 'a':
return &Song::SetArtist;
case MPD_TAG_ITEM_ALBUM:
return &Song::SetAlbum;
case MPD_TAG_ITEM_TITLE:
case 't':
return &Song::SetTitle;
case MPD_TAG_ITEM_TRACK:
return &Song::SetTrack;
case MPD_TAG_ITEM_GENRE:
return &Song::SetGenre;
case MPD_TAG_ITEM_DATE:
case 'b':
return &Song::SetAlbum;
case 'y':
return &Song::SetYear;
case MPD_TAG_ITEM_COMPOSER:
case 'n':
return &Song::SetTrack;
case 'g':
return &Song::SetGenre;
case 'c':
return &Song::SetComposer;
case MPD_TAG_ITEM_PERFORMER:
case 'p':
return &Song::SetPerformer;
case MPD_TAG_ITEM_COMMENT:
return &Song::SetComment;
case MPD_TAG_ITEM_DISC:
case 'd':
return &Song::SetDisc;
case MPD_TAG_ITEM_FILENAME:
return &Song::SetNewName;
case 'C':
return &Song::SetComment;
default:
return NULL;
}
}
string FindSharedDir(Menu<Song> *menu)
string TagEditor::GenerateFilename(const Song &s, string &pattern)
{
SongList list;
for (size_t i = 0; i < menu->Size(); i++)
list.push_back(&menu->at(i));
return FindSharedDir(list);
}
string FindSharedDir(const SongList &v)
{
string result;
if (!v.empty())
{
result = v.front()->GetFile();
for (SongList::const_iterator it = v.begin()+1; it != v.end(); it++)
{
int i = 1;
while (result.substr(0, i) == (*it)->GetFile().substr(0, i))
i++;
result = result.substr(0, i);
}
size_t slash = result.rfind("/");
result = slash != string::npos ? result.substr(0, slash) : "/";
}
string result = s.toString(pattern);
EscapeUnallowedChars(result);
return result;
}
void DealWithFilenames(SongList &v)
string TagEditor::ParseFilename(Song &s, string mask, bool preview)
{
std::ostringstream result;
vector<string> separators;
vector< std::pair<char, string> > tags;
string file = s.GetName().substr(0, s.GetName().rfind("."));
try
{
for (size_t i = mask.find("%"); i != string::npos; i = mask.find("%"))
{
tags.push_back(make_pair(mask.at(i+1), ""));
mask = mask.substr(i+2);
i = mask.find("%");
if (!mask.empty())
separators.push_back(mask.substr(0, i));
}
int i = 0;
for (vector<string>::const_iterator it = separators.begin(); it != separators.end(); it++, i++)
{
int j = file.find(*it);
tags.at(i).second = file.substr(0, j);
file = file.substr(j+it->length());
}
if (!file.empty())
tags.at(i).second = file;
}
catch (std::out_of_range)
{
return "Error while parsing filename!";
}
for (vector< std::pair<char, string> >::iterator it = tags.begin(); it != tags.end(); it++)
{
for (string::iterator j = it->second.begin(); j != it->second.end(); j++)
if (*j == '_')
*j = ' ';
if (!preview)
{
Song::SetFunction set = IntoSetFunction(it->first);
if (set)
(s.*set)(it->second);
}
else
result << "%" << it->first << ": " << it->second << "\n";
}
return result.str();
}
void TagEditor::DealWithFilenames(SongList &v)
{
int width = 30;
int height = 6;
@@ -1296,20 +1235,20 @@ void DealWithFilenames(SongList &v)
Main->SetTimeout(ncmpcpp_window_timeout);
Main->SetItemDisplayer(Display::Generic);
if (!patterns_list.empty())
Config.pattern = patterns_list.front();
if (!Patterns.empty())
Config.pattern = Patterns.front();
Main->AddOption("Pattern: " + Config.pattern);
Main->AddOption("Preview");
Main->AddOption("Legend");
Main->AddSeparator();
Main->AddOption("Proceed");
Main->AddOption("Cancel");
if (!patterns_list.empty())
if (!Patterns.empty())
{
Main->AddSeparator();
Main->AddOption("Recent patterns", 1, 1);
Main->AddSeparator();
for (vector<string>::const_iterator it = patterns_list.begin(); it != patterns_list.end(); it++)
for (vector<string>::const_iterator it = Patterns.begin(); it != Patterns.end(); it++)
Main->AddOption(*it);
}
@@ -1406,15 +1345,15 @@ void DealWithFilenames(SongList &v)
}
else
{
for (size_t i = 0; i < patterns_list.size(); i++)
for (size_t i = 0; i < Patterns.size(); i++)
{
if (patterns_list[i] == Config.pattern)
if (Patterns[i] == Config.pattern)
{
patterns_list.erase(patterns_list.begin()+i);
Patterns.erase(Patterns.begin()+i);
i--;
}
}
patterns_list.insert(patterns_list.begin(), Config.pattern);
Patterns.insert(Patterns.begin(), Config.pattern);
}
ShowMessage("Operation finished!");
if (preview)

View File

@@ -105,11 +105,22 @@ class TagEditor : public Screen<Window>
static void LowerAllLetters(MPD::Song &);
static void GetTagList(TagLib::StringList &, const std::string &);
static void GetPatternList();
static void SavePatternList();
static MPD::Song::SetFunction IntoSetFunction(char);
static std::string GenerateFilename(const MPD::Song &, std::string &);
static std::string ParseFilename(MPD::Song &, std::string, bool);
static void DealWithFilenames(MPD::SongList &);
static std::string TagToString(const MPD::Song &, void *);
std::string itsBrowsedDir;
std::string itsHighlightedDir;
static const std::string PatternsFile;
static std::vector<std::string> Patterns;
static const size_t MiddleColumnWidth;
static size_t LeftColumnWidth;
static size_t MiddleColumnStartX;
@@ -119,13 +130,6 @@ class TagEditor : public Screen<Window>
extern TagEditor *myTagEditor;
std::string FindSharedDir(Menu<MPD::Song> *);
std::string FindSharedDir(const MPD::SongList &);
MPD::Song::SetFunction IntoSetFunction(mpd_TagItems);
void DealWithFilenames(MPD::SongList &);
#endif
#endif