uniformize ways to convert strings into screen type

This commit is contained in:
Andrzej Rybczak
2014-08-27 08:54:02 +02:00
parent e3e6aa09cc
commit 7fdace835b
9 changed files with 155 additions and 168 deletions

View File

@@ -2213,11 +2213,13 @@ void NextScreen::run()
}
else if (!Config.screens_seq.empty())
{
auto screen = std::find(Config.screens_seq.begin(), Config.screens_seq.end(), myScreen);
if (++screen == Config.screens_seq.end())
Config.screens_seq.front()->switchTo();
auto screen_type = std::find(Config.screens_seq.begin(), Config.screens_seq.end(),
myScreen->type()
);
if (++screen_type == Config.screens_seq.end())
toScreen(Config.screens_seq.front())->switchTo();
else
(*screen)->switchTo();
toScreen(*screen_type)->switchTo();
}
}
@@ -2230,11 +2232,13 @@ void PreviousScreen::run()
}
else if (!Config.screens_seq.empty())
{
auto screen = std::find(Config.screens_seq.begin(), Config.screens_seq.end(), myScreen);
if (screen == Config.screens_seq.begin())
Config.screens_seq.back()->switchTo();
auto screen_type = std::find(Config.screens_seq.begin(), Config.screens_seq.end(),
myScreen->type()
);
if (screen_type == Config.screens_seq.begin())
toScreen(Config.screens_seq.back())->switchTo();
else
(*--screen)->switchTo();
toScreen(*--screen_type)->switchTo();
}
}

View File

@@ -18,29 +18,13 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <cassert>
#include <cstring>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include "actions.h"
#include "charset.h"
#include "cmdargs.h"
#include "config.h"
#include "mpdpp.h"
#include "settings.h"
#include "help.h"
#include "playlist.h"
#include "browser.h"
#include "search_engine.h"
#include "media_library.h"
#include "playlist_editor.h"
#include "tag_editor.h"
#include "outputs.h"
#include "visualizer.h"
#include "clock.h"
void ParseArgv(int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
@@ -122,39 +106,14 @@ void ParseArgv(int argc, char **argv)
if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--screen"))
{
if (++i == argc) {
if (++i == argc)
{
std::cerr << "No screen specified" << std::endl;
exit(1);
}
if (!strcmp(argv[i], "help"))
Config.startup_screen = myHelp;
else if (!strcmp(argv[i], "playlist"))
Config.startup_screen = myPlaylist;
else if (!strcmp(argv[i], "browser"))
Config.startup_screen = myBrowser;
else if (!strcmp(argv[i], "search-engine"))
Config.startup_screen = mySearcher;
else if (!strcmp(argv[i], "media-library"))
Config.startup_screen = myLibrary;
else if (!strcmp(argv[i], "playlist-editor"))
Config.startup_screen = myPlaylistEditor;
# ifdef HAVE_TAGLIB_H
else if (!strcmp(argv[i], "tag-editor"))
Config.startup_screen = myTagEditor;
# endif // HAVE_TAGLIB_H
# ifdef ENABLE_OUTPUTS
else if (!strcmp(argv[i], "outputs"))
Config.startup_screen = myOutputs;
# endif // ENABLE_OUTPUTS
# ifdef ENABLE_VISUALIZER
else if (!strcmp(argv[i], "visualizer"))
Config.startup_screen = myVisualizer;
# endif // ENABLE_VISUALIZER
# ifdef ENABLE_CLOCK
else if (!strcmp(argv[i], "clock"))
Config.startup_screen = myClock;
# endif // ENABLE_CLOCK
else {
Config.startup_screen_type = stringtoStartupScreenType(argv[i]);
if (Config.startup_screen_type == ScreenType::Unknown)
{
std::cerr << "Invalid screen: " << argv[i] << std::endl;
exit(1);
}

View File

@@ -206,8 +206,8 @@ int main(int argc, char **argv)
}
// go to startup screen
if (Config.startup_screen != myScreen)
Config.startup_screen->switchTo();
if (Config.startup_screen_type != myScreen->type())
toScreen(Config.startup_screen_type)->switchTo();
myBrowser->fetchSupportedExtensions();
# ifdef ENABLE_OUTPUTS

View File

@@ -18,43 +18,72 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "config.h"
#include "screen_type.h"
ScreenType stringtoStarterScreenType(const std::string &s)
#include "browser.h"
#include "clock.h"
#include "help.h"
#include "lastfm.h"
#include "lyrics.h"
#include "media_library.h"
#include "outputs.h"
#include "playlist.h"
#include "playlist_editor.h"
#include "search_engine.h"
#include "sel_items_adder.h"
#include "server_info.h"
#include "song_info.h"
#include "sort_playlist.h"
#include "tag_editor.h"
#include "tiny_tag_editor.h"
#include "visualizer.h"
ScreenType stringtoStartupScreenType(const std::string &s)
{
ScreenType result = ScreenType::Unknown;
if (s == "browser")
result = ScreenType::Browser;
# ifdef ENABLE_CLOCK
else if (s == "clock")
result = ScreenType::Clock;
# endif // ENABLE_CLOCK
else if (s == "help")
result = ScreenType::Help;
else if (s == "media_library")
result = ScreenType::MediaLibrary;
# ifdef ENABLE_OUTPUTS
else if (s == "outputs")
result = ScreenType::Outputs;
# endif // ENABLE_OUTPUTS
else if (s == "playlist")
result = ScreenType::Playlist;
else if (s == "playlist_editor")
result = ScreenType::PlaylistEditor;
else if (s == "search_engine")
result = ScreenType::SearchEngine;
# ifdef HAVE_TAGLIB_H
else if (s == "tag_editor")
result = ScreenType::TagEditor;
# endif // HAVE_TAGLIB_H
# ifdef ENABLE_VISUALIZER
else if (s == "visualizer")
result = ScreenType::Visualizer;
# endif // ENABLE_VISUALIZER
return result;
}
ScreenType stringToScreenType(const std::string &s)
{
ScreenType result = stringtoStarterScreenType(s);
ScreenType result = stringtoStartupScreenType(s);
if (result == ScreenType::Unknown)
{
if (s == "last_fm")
result = ScreenType::Lastfm;
else if (s == "lyrics")
if (s == "lyrics")
result = ScreenType::Lyrics;
# ifdef HAVE_CURL_CURL_H
else if (s == "last_fm")
result = ScreenType::Lastfm;
# endif // HAVE_CURL_CURL_H
else if (s == "selected_items_adder")
result = ScreenType::SelectedItemsAdder;
else if (s == "server_info")
@@ -63,8 +92,61 @@ ScreenType stringToScreenType(const std::string &s)
result = ScreenType::SongInfo;
else if (s == "sort_playlist_dialog")
result = ScreenType::SortPlaylistDialog;
# ifdef HAVE_TAGLIB_H
else if (s == "tiny_tag_editor")
result = ScreenType::TinyTagEditor;
# endif // HAVE_TAGLIB_H
}
return result;
}
BaseScreen *toScreen(ScreenType st)
{
switch (st)
{
case ScreenType::Browser:
return myBrowser;
# ifdef ENABLE_CLOCK
case ScreenType::Clock:
return myClock;
# endif // ENABLE_CLOCK
case ScreenType::Help:
return myHelp;
case ScreenType::Lastfm:
return myLastfm;
case ScreenType::Lyrics:
return myLyrics;
case ScreenType::MediaLibrary:
return myLibrary;
# ifdef ENABLE_OUTPUTS
case ScreenType::Outputs:
return myOutputs;
# endif // ENABLE_OUTPUTS
case ScreenType::Playlist:
return myPlaylist;
case ScreenType::PlaylistEditor:
return myPlaylistEditor;
case ScreenType::SearchEngine:
return mySearcher;
case ScreenType::SelectedItemsAdder:
return mySelectedItemsAdder;
case ScreenType::ServerInfo:
return myServerInfo;
case ScreenType::SongInfo:
return mySongInfo;
case ScreenType::SortPlaylistDialog:
return mySortPlaylistDialog;
# ifdef HAVE_TAGLIB_H
case ScreenType::TagEditor:
return myTagEditor;
case ScreenType::TinyTagEditor:
return myTinyTagEditor;
# endif // HAVE_TAGLIB_H
# ifdef ENABLE_VISUALIZER
case ScreenType::Visualizer:
return myVisualizer;
# endif // ENABLE_VISUALIZER
default:
return nullptr;
}
}

View File

@@ -22,15 +22,25 @@
#define NCMPCPP_SCREEN_TYPE_H
#include <string>
#include "config.h"
// forward declaration
struct BaseScreen;
enum class ScreenType {
Browser,
# ifdef ENABLE_CLOCK
Clock,
# endif // ENABLE_CLOCK
Help,
# ifdef HAVE_CURL_CURL_H
Lastfm,
# endif // HAVE_CURL_CURL_H
Lyrics,
MediaLibrary,
# ifdef ENABLE_OUTPUTS
Outputs,
# endif // ENABLE_OUTPUTS
Playlist,
PlaylistEditor,
SearchEngine,
@@ -38,13 +48,19 @@ enum class ScreenType {
ServerInfo,
SongInfo,
SortPlaylistDialog,
# ifdef HAVE_TAGLIB_H
TagEditor,
TinyTagEditor,
# endif // HAVE_TAGLIB_H
Unknown,
# ifdef ENABLE_VISUALIZER
Visualizer,
# endif // ENABLE_VISUALIZER
};
ScreenType stringtoStarterScreenType(const std::string &s);
ScreenType stringtoStartupScreenType(const std::string &s);
ScreenType stringToScreenType(const std::string &s);
BaseScreen *toScreen(ScreenType st);
#endif // NCMPCPP_SCREEN_TYPE_H

View File

@@ -60,43 +60,6 @@ Configuration Config;
namespace
{
ScreenRef intToScreen(int n)
{
switch (n)
{
case 1:
return myHelp;
case 2:
return myPlaylist;
case 3:
return myBrowser;
case 4:
return mySearcher;
case 5:
return myLibrary;
case 6:
return myPlaylistEditor;
# ifdef HAVE_TAGLIB_H
case 7:
return myTagEditor;
# endif // HAVE_TAGLIB_H
# ifdef ENABLE_OUTPUTS
case 8:
return myOutputs;
# endif // ENABLE_OUTPUTS
# ifdef ENABLE_VISUALIZER
case 9:
return myVisualizer;
# endif // ENABLE_VISUALIZER
# ifdef ENABLE_CLOCK
case 10:
return myClock;
# endif // ENABLE_CLOCK
default:
return ScreenRef();
}
}
std::string GetOptionName(const std::string &s)
{
size_t equal = s.find('=');
@@ -241,11 +204,11 @@ void Configuration::SetDefaults()
if (system_encoding == "UTF-8") // mpd uses utf-8 by default so no need to convert
system_encoding.clear();
# endif // HAVE_LANGINFO_H
startup_screen = myPlaylist;
startup_screen_type = ScreenType::Playlist;
browser_sort_mode = smName;
// default screens sequence
screens_seq.push_back(myPlaylist);
screens_seq.push_back(myBrowser);
screens_seq.push_back(ScreenType::Playlist);
screens_seq.push_back(ScreenType::Browser);
}
Configuration::Configuration()
@@ -625,24 +588,20 @@ void Configuration::Read()
}
else if (name == "screen_switcher_mode")
{
if (v.find("previous") != std::string::npos)
{
if (v == "previous")
screen_switcher_previous = true;
}
else if (v.find("sequence") != std::string::npos)
else
{
screen_switcher_previous = false;
screens_seq.clear();
for (std::string::const_iterator it = v.begin(); it != v.end(); )
boost::sregex_token_iterator i(v.begin(), v.end(), boost::regex("\\w+")), j;
for (; i != j; ++i)
{
while (it != v.end() && !isdigit(*it))
++it;
if (it == v.end())
break;
if (auto screen = intToScreen(atoi(&*it)))
auto screen = stringtoStartupScreenType(*i);
if (screen != ScreenType::Unknown)
screens_seq.push_back(screen);
while (it != v.end() && isdigit(*it))
++it;
else
std::cerr << "screen_switcher_mode: unknown screen: " << *i << "\n";
}
// throw away duplicates
screens_seq.unique();
@@ -650,9 +609,9 @@ void Configuration::Read()
}
else if (name == "startup_screen")
{
startup_screen = intToScreen(atoi(v.c_str()));
if (!startup_screen)
startup_screen = myPlaylist;
startup_screen_type = stringtoStartupScreenType(v);
if (startup_screen_type == ScreenType::Unknown)
startup_screen_type = ScreenType::Playlist;
}
else if (name == "autocenter_mode")
{

View File

@@ -26,6 +26,7 @@
#include <vector>
#include <mpd/client.h>
#include "actions.h"
#include "screen_type.h"
#include "strbuffer.h"
struct BaseScreen; // forward declaration for screens sequence
@@ -46,27 +47,6 @@ struct Column
bool display_empty_tag;
};
// FIXME: temporary hack
struct ScreenRef
{
ScreenRef() : m_ptr(0) { }
template <typename ScreenT>
ScreenRef(ScreenT *&ptr) : m_ptr(reinterpret_cast<BaseScreen **>(&ptr)) { }
BaseScreen &operator*() const { return **m_ptr; }
BaseScreen *operator->() const { return *m_ptr; }
bool operator==(const ScreenRef &rhs) const { return m_ptr == rhs.m_ptr; }
bool operator!=(const ScreenRef &rhs) const { return m_ptr != rhs.m_ptr; }
bool operator==(const BaseScreen *rhs) const { return *m_ptr == rhs; }
bool operator!=(const BaseScreen *rhs) const { return *m_ptr != rhs; }
operator bool() { return m_ptr != 0; }
private:
BaseScreen **m_ptr;
};
struct Configuration
{
Configuration();
@@ -208,8 +188,8 @@ struct Configuration
size_t now_playing_prefix_length;
size_t now_playing_suffix_length;
ScreenRef startup_screen;
std::list<ScreenRef> screens_seq;
ScreenType startup_screen_type;
std::list<ScreenType> screens_seq;
SortMode browser_sort_mode;