1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <boost/tuple/tuple.hpp>
25 #include "configuration.h"
28 #include "utility/conversion.h"
29 #include "utility/option_parser.h"
30 #include "utility/type_conversions.h"
32 #ifdef HAVE_LANGINFO_H
33 # include <langinfo.h>
40 std::string
remove_dollar_formatting(const std::string
&s
)
43 for (size_t i
= 0; i
< s
.size(); ++i
)
53 std::pair
<std::vector
<Column
>, std::string
> generate_columns(const std::string
&format
)
55 std::vector
<Column
> result
;
58 while (!(width
= getEnclosedString(format
, '(', ')', &pos
)).empty())
61 col
.color
= stringToColor(getEnclosedString(format
, '[', ']', &pos
));
62 std::string tag_type
= getEnclosedString(format
, '{', '}', &pos
);
64 if (*width
.rbegin() == 'f')
67 width
.resize(width
.size()-1);
73 size_t tag_type_colon_pos
= tag_type
.find(':');
74 if (tag_type_colon_pos
!= std::string::npos
)
76 col
.name
= ToWString(tag_type
.substr(tag_type_colon_pos
+1));
77 tag_type
.resize(tag_type_colon_pos
);
80 if (!tag_type
.empty())
84 // extract tag types in format a|b|c etc.
86 col
.type
+= tag_type
[(++i
)++]; // nice one.
87 while (tag_type
[i
] == '|');
90 for (; i
< tag_type
.length(); ++i
)
95 col
.right_alignment
= 1;
98 col
.display_empty_tag
= 0;
104 col
.display_empty_tag
= 0;
106 col
.width
= boost::lexical_cast
<int>(width
);
107 result
.push_back(col
);
110 // calculate which column is the last one to have relative width and stretch it accordingly
113 int stretch_limit
= 0;
114 auto it
= result
.rbegin();
115 for (; it
!= result
.rend(); ++it
)
118 stretch_limit
+= it
->width
;
122 // if it's false, all columns are fixed
123 if (it
!= result
.rend())
124 it
->stretch_limit
= stretch_limit
;
127 std::string string_format
;
128 // generate format for converting tags in columns to string for Playlist::SongInColumnsToString()
129 char tag
[] = "{% }|";
131 for (auto it
= result
.begin(); it
!= result
.end(); ++it
)
133 for (std::string::const_iterator j
= it
->type
.begin(); j
!= it
->type
.end(); ++j
)
136 string_format
+= tag
;
138 *string_format
.rbegin() = ' ';
140 if (string_format
.length() == 1) // only '{'
141 string_format
+= '}';
143 *string_format
.rbegin() = '}';
145 return std::make_pair(std::move(result
), std::move(string_format
));
148 void add_slash_at_the_end(std::string
&s
)
150 if (s
.empty() || *s
.rbegin() != '/')
152 s
.resize(s
.size()+1);
157 std::string
adjust_directory(std::string
&&s
)
159 add_slash_at_the_end(s
);
164 std::string
adjust_and_validate_format(std::string
&&format
)
166 MPD::Song::validateFormat(format
);
167 format
= "{" + format
+ "}";
171 // parser worker for buffer
172 template <typename ValueT
, typename TransformT
>
173 option_parser::worker
buffer(NC::Buffer
&arg
, ValueT
&&value
, TransformT
&&map
)
175 return option_parser::worker(assign
<std::string
>(arg
, [&arg
, map
](std::string
&&s
) {
176 return map(stringToBuffer(s
));
177 }), defaults_to(arg
, map(std::forward
<ValueT
>(value
))));
182 bool Configuration::read(const std::string
&config_path
)
184 std::string mpd_host
;
186 std::string columns_format
;
188 // keep the same order of variables as in configuration file
190 p
.add("ncmpcpp_directory", assign_default
<std::string
>(
191 ncmpcpp_directory
, "~/.ncmpcpp/", adjust_directory
193 p
.add("lyrics_directory", assign_default
<std::string
>(
194 lyrics_directory
, "~/.lyrics/", adjust_directory
196 p
.add("mpd_host", assign_default
<std::string
>(
197 mpd_host
, "localhost", [](std::string
&&host
) {
198 Mpd
.SetHostname(host
);
201 p
.add("mpd_port", assign_default
<unsigned>(
202 mpd_port
, 6600, [](unsigned port
) {
206 p
.add("mpd_music_dir", assign_default
<std::string
>(
207 mpd_music_dir
, "~/music", adjust_directory
209 p
.add("mpd_connection_timeout", assign_default(
210 mpd_connection_timeout
, 5
212 p
.add("mpd_crossfade_time", assign_default(
215 p
.add("visualizer_fifo_path", assign_default(
216 visualizer_fifo_path
, "/tmp/mpd.fifo"
218 p
.add("visualizer_output_name", assign_default(
219 visualizer_output_name
, "Visualizer feed"
221 p
.add("visualizer_in_stereo", yes_no(
222 visualizer_in_stereo
, true
224 p
.add("visualizer_sync_interval", assign_default
<unsigned>(
225 visualizer_sync_interval
, 30, [](unsigned v
) {
226 return boost::posix_time::seconds(v
);
228 p
.add("visualizer_type", option_parser::worker([this](std::string
&&v
) {
230 visualizer_use_wave
= true;
231 else if (v
== "frequency")
232 visualizer_use_wave
= false;
234 throw std::runtime_error("invalid argument: " + v
);
235 }, defaults_to(visualizer_use_wave
, true)
237 p
.add("visualizer_look", assign_default
<std::string
>(
238 visualizer_chars
, "◆│", [](std::string
&&s
) {
239 auto result
= ToWString(std::move(s
));
240 typedef std::wstring::size_type size_type
;
241 boundsCheck(result
.size(), size_type(2), size_type(2));
244 p
.add("system_encoding", assign_default
<std::string
>(
245 system_encoding
, "", [](std::string
&&enc
) {
246 # ifdef HAVE_LANGINFO_H
247 // try to autodetect system encoding
250 enc
= nl_langinfo(CODESET
);
251 if (enc
== "UTF-8") // mpd uses utf-8 by default so no need to convert
254 # endif // HAVE_LANGINFO_H
257 p
.add("playlist_disable_highlight_delay", assign_default
<unsigned>(
258 playlist_disable_highlight_delay
, 5, [](unsigned v
) {
259 return boost::posix_time::seconds(v
);
261 p
.add("message_delay_time", assign_default(
262 message_delay_time
, 5
264 p
.add("song_list_format", assign_default
<std::string
>(
265 song_list_format
, "{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}", [this](std::string
&&s
) {
266 auto result
= adjust_and_validate_format(std::move(s
));
267 song_list_format_dollar_free
= remove_dollar_formatting(result
);
270 p
.add("song_status_format", assign_default
<std::string
>(
271 song_status_format
, "{{%a{ \"%b\"{ (%y)}} - }{%t}}|{%f}", [this](std::string
&&s
) {
272 auto result
= adjust_and_validate_format(std::move(s
));
273 if (result
.find("$") != std::string::npos
)
274 song_status_format_no_colors
= stringToBuffer(result
).str();
276 song_status_format_no_colors
= result
;
279 p
.add("song_library_format", assign_default
<std::string
>(
280 song_library_format
, "{%n - }{%t}|{%f}", adjust_and_validate_format
282 p
.add("tag_editor_album_format", assign_default
<std::string
>(
283 tag_editor_album_format
, "{(%y) }%b", adjust_and_validate_format
285 p
.add("browser_sort_mode", assign_default(
286 browser_sort_mode
, SortMode::Name
288 p
.add("browser_sort_format", assign_default
<std::string
>(
289 browser_sort_format
, "{%a - }{%t}|{%f} {(%l)}", adjust_and_validate_format
291 p
.add("alternative_header_first_line_format", assign_default
<std::string
>(
292 new_header_first_line
, "$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b", adjust_and_validate_format
294 p
.add("alternative_header_second_line_format", assign_default
<std::string
>(
295 new_header_second_line
, "{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}", adjust_and_validate_format
297 p
.add("now_playing_prefix", buffer(
298 now_playing_prefix
, NC::Buffer(NC::Format::Bold
), [this](NC::Buffer
&&buf
) {
299 now_playing_prefix_length
= wideLength(ToWString(buf
.str()));
302 p
.add("now_playing_suffix", buffer(
303 now_playing_suffix
, NC::Buffer(NC::Format::NoBold
), [this](NC::Buffer
&&buf
) {
304 now_playing_suffix_length
= wideLength(ToWString(buf
.str()));
307 p
.add("browser_playlist_prefix", buffer(
308 browser_playlist_prefix
, NC::Buffer(NC::Color::Red
, "playlist", NC::Color::End
, ' '), id_()
310 p
.add("selected_item_prefix", buffer(
311 selected_item_prefix
, NC::Buffer(NC::Color::Magenta
), [this](NC::Buffer
&&buf
) {
312 selected_item_prefix_length
= wideLength(ToWString(buf
.str()));
315 p
.add("selected_item_suffix", buffer(
316 selected_item_suffix
, NC::Buffer(NC::Color::End
), [this](NC::Buffer
&&buf
) {
317 selected_item_suffix_length
= wideLength(ToWString(buf
.str()));
320 p
.add("modified_item_prefix", buffer(
321 modified_item_prefix
, NC::Buffer(NC::Color::Green
, "> ", NC::Color::End
), id_()
323 p
.add("song_window_title_format", assign_default
<std::string
>(
324 song_window_title_format
, "{%a - }{%t}|{%f}", adjust_and_validate_format
326 p
.add("song_columns_list_format", assign_default
<std::string
>(
327 columns_format
, "(20)[]{a} (6f)[green]{NE} (50)[white]{t|f:Title} (20)[cyan]{b} (7f)[magenta]{l}",
328 [this](std::string
&&v
) {
329 boost::tie(columns
, song_in_columns_to_string_format
) = generate_columns(v
);
332 p
.add("execute_on_song_change", assign_default(
333 execute_on_song_change
, ""
335 p
.add("playlist_show_remaining_time", yes_no(
336 playlist_show_remaining_time
, false
338 p
.add("playlist_shorten_total_times", yes_no(
339 playlist_shorten_total_times
, false
341 p
.add("playlist_separate_albums", yes_no(
342 playlist_separate_albums
, false
344 p
.add("playlist_display_mode", assign_default(
345 playlist_display_mode
, DisplayMode::Columns
347 p
.add("browser_display_mode", assign_default(
348 browser_display_mode
, DisplayMode::Classic
350 p
.add("search_engine_display_mode", assign_default(
351 search_engine_display_mode
, DisplayMode::Classic
353 p
.add("playlist_editor_display_mode", assign_default(
354 playlist_editor_display_mode
, DisplayMode::Classic
356 p
.add("discard_colors_if_item_is_selected", yes_no(
357 discard_colors_if_item_is_selected
, true
359 p
.add("incremental_seeking", yes_no(
360 incremental_seeking
, true
362 p
.add("seek_time", assign_default(
365 p
.add("volume_change_step", assign_default(
366 volume_change_step
, 2
368 p
.add("autocenter_mode", yes_no(
369 titles_visibility
, false
371 p
.add("centered_cursor", yes_no(
372 centered_cursor
, false
374 p
.add("progressbar_look", assign_default
<std::string
>(
375 progressbar
, "=>", [](std::string
&&s
) {
376 auto result
= ToWString(std::move(s
));
377 typedef std::wstring::size_type size_type
;
378 boundsCheck(result
.size(), size_type(2), size_type(3));
379 // if two characters were specified, add third one (null)
383 p
.add("progressbar_boldness", yes_no(
384 progressbar_boldness
, true
386 p
.add("default_place_to_search_in", option_parser::worker([this](std::string
&&v
) {
389 else if (v
== "playlist")
392 throw std::runtime_error("invalid argument: " + v
);
393 }, defaults_to(search_in_db
, true)
395 p
.add("user_interface", assign_default(
396 design
, Design::Classic
398 p
.add("media_library_primary_tag", option_parser::worker([this](std::string
&&v
) {
400 media_lib_primary_tag
= MPD_TAG_ARTIST
;
401 else if (v
== "album_artist")
402 media_lib_primary_tag
= MPD_TAG_ALBUM_ARTIST
;
403 else if (v
== "date")
404 media_lib_primary_tag
= MPD_TAG_DATE
;
405 else if (v
== "genre")
406 media_lib_primary_tag
= MPD_TAG_GENRE
;
407 else if (v
== "composer")
408 media_lib_primary_tag
= MPD_TAG_COMPOSER
;
409 else if (v
== "performer")
410 media_lib_primary_tag
= MPD_TAG_PERFORMER
;
412 throw std::runtime_error("invalid argument: " + v
);
413 regex_type
|= boost::regex::icase
;
414 }, defaults_to(regex_type
, boost::regex::literal
| boost::regex::icase
)
416 p
.add("default_find_mode", option_parser::worker([this](std::string
&&v
) {
418 wrapped_search
= true;
419 else if (v
== "normal")
420 wrapped_search
= false;
422 throw std::runtime_error("invalid argument: " + v
);
423 }, defaults_to(wrapped_search
, true)
425 p
.add("default_space_mode", option_parser::worker([this](std::string
&&v
) {
427 space_selects
= false;
428 else if (v
== "select")
429 space_selects
= true;
431 throw std::runtime_error("invalid argument: " + v
);
432 }, defaults_to(wrapped_search
, true)
434 p
.add("default_tag_editor_pattern", assign_default(
437 p
.add("header_visibility", yes_no(
438 header_visibility
, true
440 p
.add("statusbar_visibility", yes_no(
441 statusbar_visibility
, true
443 p
.add("titles_visibility", yes_no(
444 titles_visibility
, true
446 p
.add("header_text_scrolling", yes_no(
447 header_text_scrolling
, true
449 p
.add("cyclic_scrolling", yes_no(
450 use_cyclic_scrolling
, false
452 p
.add("lines_scrolled", assign_default(
455 p
.add("follow_now_playing_lyrics", yes_no(
456 now_playing_lyrics
, false
458 p
.add("fetch_lyrics_for_current_song_in_background", yes_no(
459 fetch_lyrics_in_background
, false
461 p
.add("store_lyrics_in_song_dir", yes_no(
462 store_lyrics_in_song_dir
, false
464 p
.add("generate_win32_compatible_filenames", yes_no(
465 generate_win32_compatible_filenames
, true
467 p
.add("allow_for_physical_item_deletion", yes_no(
468 allow_for_physical_item_deletion
, false
470 p
.add("lastfm_preferred_language", assign_default(
471 lastfm_preferred_language
, "en"
473 p
.add("space_add_mode", assign_default(
474 space_add_mode
, SpaceAddMode::AlwaysAdd
476 p
.add("show_hidden_files_in_local_browser", yes_no(
477 local_browser_show_hidden_files
, false
479 p
.add("screen_switcher_mode", option_parser::worker([this](std::string
&&v
) {
481 screen_switcher_previous
= true;
484 screen_switcher_previous
= false;
485 boost::sregex_token_iterator
i(v
.begin(), v
.end(), boost::regex("\\w+")), j
;
488 auto screen
= stringtoStartupScreenType(*i
);
489 if (screen
!= ScreenType::Unknown
)
490 screen_sequence
.push_back(screen
);
492 throw std::runtime_error("unknown screen: " + *i
);
496 screen_switcher_previous
= false;
497 screen_sequence
= { ScreenType::Playlist
, ScreenType::Browser
};
499 p
.add("startup_screen", option_parser::worker([this](std::string
&&v
) {
500 startup_screen_type
= stringtoStartupScreenType(v
);
501 if (startup_screen_type
== ScreenType::Unknown
)
502 throw std::runtime_error("unknown screen: " + v
);
503 }, defaults_to(startup_screen_type
, ScreenType::Playlist
)
505 p
.add("locked_screen_width_part", assign_default
<double>(
506 locked_screen_width_part
, 50.0, [](double v
) {
509 p
.add("ask_for_locked_screen_width_part", yes_no(
510 ask_for_locked_screen_width_part
, true
512 p
.add("jump_to_now_playing_song_at_start", yes_no(
513 jump_to_now_playing_song_at_start
, true
515 p
.add("ask_before_clearing_playlists", yes_no(
516 ask_before_clearing_playlists
, true
518 p
.add("clock_display_seconds", yes_no(
519 clock_display_seconds
, false
521 p
.add("display_volume_level", yes_no(
522 display_volume_level
, true
524 p
.add("display_bitrate", yes_no(
525 display_bitrate
, false
527 p
.add("display_remaining_time", yes_no(
528 display_remaining_time
, false
530 p
.add("regular_expressions", option_parser::worker([this](std::string
&&v
) {
532 regex_type
= boost::regex::literal
;
533 else if (v
== "basic")
534 regex_type
= boost::regex::basic
;
535 else if (v
== "extended")
536 regex_type
= boost::regex::extended
;
538 throw std::runtime_error("invalid argument: " + v
);
539 regex_type
|= boost::regex::icase
;
540 }, defaults_to(regex_type
, boost::regex::literal
| boost::regex::icase
)
542 p
.add("ignore_leading_the", yes_no(
543 ignore_leading_the
, false
545 p
.add("block_search_constraints_change_if_items_found", yes_no(
546 block_search_constraints_change
, true
548 p
.add("mouse_support", yes_no(
551 p
.add("mouse_list_scroll_whole_page", yes_no(
552 mouse_list_scroll_whole_page
, true
554 p
.add("empty_tag_marker", assign_default(
557 p
.add("tags_separator", assign_default(
558 tags_separator
, " | "
560 p
.add("tag_editor_extended_numeration", yes_no(
561 tag_editor_extended_numeration
, false
563 p
.add("media_library_sort_by_mtime", yes_no(
564 media_library_sort_by_mtime
, false
566 p
.add("enable_window_title", yes_no(
567 set_window_title
, true
569 p
.add("search_engine_default_search_mode", assign_default
<unsigned>(
570 search_engine_default_search_mode
, 1, [](unsigned v
) {
571 boundsCheck(v
, 1u, 3u);
574 p
.add("external_editor", assign_default(
575 external_editor
, "nano"
577 p
.add("use_console_editor", yes_no(
578 use_console_editor
, true
580 p
.add("colors_enabled", yes_no(
583 p
.add("empty_tag_color", assign_default(
584 empty_tags_color
, NC::Color::Cyan
586 p
.add("header_window_color", assign_default(
587 header_color
, NC::Color::Default
589 p
.add("volume_color", assign_default(
590 volume_color
, NC::Color::Default
592 p
.add("state_line_color", assign_default(
593 state_line_color
, NC::Color::Default
595 p
.add("state_flags_color", assign_default(
596 state_flags_color
, NC::Color::Default
598 p
.add("main_window_color", assign_default(
599 main_color
, NC::Color::Yellow
601 p
.add("color1", assign_default(
602 color1
, NC::Color::White
604 p
.add("color2", assign_default(
605 color2
, NC::Color::Green
607 p
.add("main_window_highlight_color", assign_default(
608 main_highlight_color
, NC::Color::Yellow
610 p
.add("progressbar_color", assign_default(
611 progressbar_color
, NC::Color::Black
613 p
.add("progressbar_elapsed_color", assign_default(
614 progressbar_elapsed_color
, NC::Color::Green
616 p
.add("statusbar_color", assign_default(
617 statusbar_color
, NC::Color::Default
619 p
.add("alternative_ui_separator_color", assign_default(
620 alternative_ui_separator_color
, NC::Color::Black
622 p
.add("active_column_color", assign_default(
623 active_column_color
, NC::Color::Red
625 p
.add("visualizer_color", assign_default(
626 visualizer_color
, NC::Color::Yellow
628 p
.add("window_border_color", assign_default(
629 window_border
, NC::Border::Green
631 p
.add("active_window_border", assign_default(
632 active_window_border
, NC::Border::Red
635 std::ifstream
f(config_path
);