Implement filtering in playlist editor
[ncmpcpp.git] / src / helpers.cpp
blob59dc74eda7dc6791357c3141e7c7c775fcc95cb0
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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 <algorithm>
22 #include <time.h>
24 #include "enums.h"
25 #include "helpers.h"
26 #include "playlist.h"
27 #include "statusbar.h"
28 #include "utility/functional.h"
30 const MPD::Song *currentSong(const BaseScreen *screen)
32 const MPD::Song *ptr = nullptr;
33 const auto *list = dynamic_cast<const SongList *>(screen->activeWindow());
34 if (list != nullptr)
36 const auto it = list->currentS();
37 if (it != list->endS())
38 ptr = it->get<Bit::Song>();
40 return ptr;
43 MPD::SongIterator getDatabaseIterator(MPD::Connection &mpd)
45 MPD::SongIterator result;
46 try
48 result = mpd.GetDirectoryRecursive("/");
50 catch (MPD::ClientError &e)
52 if (e.code() == MPD_ERROR_CLOSED)
54 // If we can't get the database, display appropriate
55 // error message and reconnect with the MPD server.
56 Statusbar::print("Unable to fetch the data, increase max_output_buffer_size in your MPD configuration file");
57 mpd.Disconnect();
58 mpd.Connect();
60 else
61 throw;
63 return result;
66 void removeSongFromPlaylist(const SongMenu &playlist, const MPD::Song &s)
68 Mpd.StartCommandsList();
69 for (auto &item : reverse_iteration(playlist))
70 if (item.value() == s)
71 Mpd.Delete(item.value().getPosition());
72 Mpd.CommitCommandsList();
75 bool addSongToPlaylist(const MPD::Song &s, bool play, int position)
77 bool result = false;
78 if (Config.space_add_mode == SpaceAddMode::AddRemove
79 && myPlaylist->checkForSong(s)
82 result = true;
83 if (play)
85 const auto begin = myPlaylist->main().beginV(), end = myPlaylist->main().endV();
86 auto it = find_map_first(begin, end, s, [](const MPD::Song &found) {
87 Mpd.PlayID(found.getID());
88 });
89 assert(it != end);
91 else
92 removeSongFromPlaylist(myPlaylist->main(), s);
93 return result;
95 int id = Mpd.AddSong(s, position);
96 if (id >= 0)
98 Statusbar::printf("Added to playlist: %s",
99 Format::stringify<char>(Config.song_status_format, &s)
101 if (play)
102 Mpd.PlayID(id);
103 result = true;
105 return result;
108 std::string timeFormat(const char *format, time_t t)
110 char result[32];
111 tm tinfo;
112 localtime_r(&t, &tinfo);
113 strftime(result, sizeof(result), format, &tinfo);
114 return result;
117 std::string Timestamp(time_t t)
119 char result[32];
120 tm info;
121 result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
122 return result;
125 std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width)
127 std::wstring s(str);
128 if (!Config.header_text_scrolling)
129 return s;
130 std::wstring result;
131 size_t len = wideLength(s);
133 if (len > width)
135 s += L" ** ";
136 len = 0;
137 auto b = s.begin(), e = s.end();
138 for (auto it = b+pos; it < e && len < width; ++it)
140 if ((len += wcwidth(*it)) > width)
141 break;
142 result += *it;
144 if (++pos >= s.length())
145 pos = 0;
146 for (; len < width; ++b)
148 if ((len += wcwidth(*b)) > width)
149 break;
150 result += *b;
153 else
154 result = s;
155 return result;
158 void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos,
159 size_t width, const std::wstring &separator)
161 const auto &s = buf.str();
162 size_t len = wideLength(s);
163 if (len > width)
165 len = 0;
166 const auto &ps = buf.properties();
167 auto p = ps.begin();
169 // load attributes from before starting pos
170 for (; p != ps.end() && p->first < start_pos; ++p)
171 w << p->second;
173 auto write_buffer = [&](size_t start) {
174 for (size_t i = start; i < s.length() && len < width; ++i)
176 for (; p != ps.end() && p->first == i; ++p)
177 w << p->second;
178 len += wcwidth(s[i]);
179 if (len > width)
180 break;
181 w << s[i];
183 for (; p != ps.end(); ++p)
184 w << p->second;
185 p = ps.begin();
188 write_buffer(start_pos);
189 size_t i = 0;
190 if (start_pos > s.length())
191 i = start_pos - s.length();
192 for (; i < separator.length() && len < width; ++i)
194 len += wcwidth(separator[i]);
195 if (len > width)
196 break;
197 w << separator[i];
199 write_buffer(0);
201 ++start_pos;
202 if (start_pos >= s.length() + separator.length())
203 start_pos = 0;
205 else
206 w << buf;