Improve detection for songs not in playlists
[ncmpcpp.git] / src / helpers.cpp
blobbfc7efc7da62675eed698c0053ec0bbfb8a902d4
1 /***************************************************************************
2 * Copyright (C) 2008-2016 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 <boost/range/adaptor/reversed.hpp>
23 #include <time.h>
25 #include "enums.h"
26 #include "helpers.h"
27 #include "screens/playlist.h"
28 #include "statusbar.h"
29 #include "utility/functional.h"
31 const MPD::Song *currentSong(const BaseScreen *screen)
33 const MPD::Song *ptr = nullptr;
34 const auto *list = dynamic_cast<const SongList *>(screen->activeWindow());
35 if (list != nullptr)
37 const auto it = list->currentS();
38 if (it != list->endS())
39 ptr = it->song();
41 return ptr;
44 MPD::SongIterator getDatabaseIterator(MPD::Connection &mpd)
46 MPD::SongIterator result;
47 try
49 result = mpd.GetDirectoryRecursive("/");
51 catch (MPD::ClientError &e)
53 if (e.code() == MPD_ERROR_CLOSED)
55 // If we can't get the database, display appropriate
56 // error message and reconnect with the MPD server.
57 Statusbar::print("Unable to fetch the data, increase max_output_buffer_size in your MPD configuration file");
58 mpd.Disconnect();
59 mpd.Connect();
61 else
62 throw;
64 return result;
67 void removeSongFromPlaylist(const SongMenu &playlist, const MPD::Song &s)
69 Mpd.StartCommandsList();
70 for (auto &item : boost::adaptors::reverse(playlist))
71 if (item.value() == s)
72 Mpd.Delete(item.value().getPosition());
73 Mpd.CommitCommandsList();
76 bool addSongToPlaylist(const MPD::Song &s, bool play, int position)
78 bool result = false;
79 if (Config.space_add_mode == SpaceAddMode::AddRemove
80 && myPlaylist->checkForSong(s)
83 result = true;
84 if (play)
86 const auto begin = myPlaylist->main().beginV(), end = myPlaylist->main().endV();
87 auto it = find_map_first(begin, end, s, [](const MPD::Song &found) {
88 Mpd.PlayID(found.getID());
89 });
90 assert(it != end);
92 else
93 removeSongFromPlaylist(myPlaylist->main(), s);
94 return result;
96 int id = Mpd.AddSong(s, position);
97 if (id >= 0)
99 Statusbar::printf("Added to playlist: %s",
100 Format::stringify<char>(Config.song_status_format, &s)
102 if (play)
103 Mpd.PlayID(id);
104 result = true;
106 return result;
109 std::string timeFormat(const char *format, time_t t)
111 char result[32];
112 tm tinfo;
113 localtime_r(&t, &tinfo);
114 strftime(result, sizeof(result), format, &tinfo);
115 return result;
118 std::string Timestamp(time_t t)
120 char result[32];
121 tm info;
122 result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
123 return result;
126 std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width)
128 std::wstring s(str);
129 if (!Config.header_text_scrolling)
130 return s;
131 std::wstring result;
132 size_t len = wideLength(s);
134 if (len > width)
136 s += L" ** ";
137 len = 0;
138 auto b = s.begin(), e = s.end();
139 for (auto it = b+pos; it < e && len < width; ++it)
141 if ((len += wcwidth(*it)) > width)
142 break;
143 result += *it;
145 if (++pos >= s.length())
146 pos = 0;
147 for (; len < width; ++b)
149 if ((len += wcwidth(*b)) > width)
150 break;
151 result += *b;
154 else
155 result = s;
156 return result;
159 void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos,
160 size_t width, const std::wstring &separator)
162 const auto &s = buf.str();
163 size_t len = wideLength(s);
164 if (len > width)
166 len = 0;
167 const auto &ps = buf.properties();
168 auto p = ps.begin();
170 // load attributes from before starting pos
171 for (; p != ps.end() && p->first < start_pos; ++p)
172 w << p->second;
174 auto write_buffer = [&](size_t start) {
175 for (size_t i = start; i < s.length() && len < width; ++i)
177 for (; p != ps.end() && p->first == i; ++p)
178 w << p->second;
179 len += wcwidth(s[i]);
180 if (len > width)
181 break;
182 w << s[i];
184 for (; p != ps.end(); ++p)
185 w << p->second;
186 p = ps.begin();
189 write_buffer(start_pos);
190 size_t i = 0;
191 if (start_pos > s.length())
192 i = start_pos - s.length();
193 for (; i < separator.length() && len < width; ++i)
195 len += wcwidth(separator[i]);
196 if (len > width)
197 break;
198 w << separator[i];
200 write_buffer(0);
202 ++start_pos;
203 if (start_pos >= s.length() + separator.length())
204 start_pos = 0;
206 else
207 w << buf;