1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Andrzej Rybczak                            *
3  *   andrzej@rybczak.net                                                   *
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  ***************************************************************************/
20 
21 #include <boost/date_time/posix_time/posix_time.hpp>
22 #include <iomanip>
23 
24 #include "global.h"
25 #include "helpers.h"
26 #include "screens/server_info.h"
27 #include "statusbar.h"
28 #include "screens/screen_switcher.h"
29 
30 using Global::MainHeight;
31 using Global::MainStartY;
32 
33 ServerInfo *myServerInfo;
34 
ServerInfo()35 ServerInfo::ServerInfo()
36 : m_timer(boost::posix_time::from_time_t(0))
37 {
38 	SetDimensions();
39 	w = NC::Scrollpad((COLS-m_width)/2, (MainHeight-m_height)/2+MainStartY, m_width, m_height, "MPD server info", Config.main_color, Config.window_border);
40 }
41 
switchTo()42 void ServerInfo::switchTo()
43 {
44 	using Global::myScreen;
45 	if (myScreen != this)
46 	{
47 		SwitchTo::execute(this);
48 
49 		m_url_handlers.clear();
50 		std::copy(
51 			std::make_move_iterator(Mpd.GetURLHandlers()),
52 			std::make_move_iterator(MPD::StringIterator()),
53 			std::back_inserter(m_url_handlers)
54 		);
55 
56 		m_tag_types.clear();
57 		std::copy(
58 			std::make_move_iterator(Mpd.GetTagTypes()),
59 			std::make_move_iterator(MPD::StringIterator()),
60 			std::back_inserter(m_tag_types)
61 		);
62 	}
63 	else
64 		switchToPreviousScreen();
65 }
66 
resize()67 void ServerInfo::resize()
68 {
69 	SetDimensions();
70 	w.resize(m_width, m_height);
71 	w.moveTo((COLS-m_width)/2, (MainHeight-m_height)/2+MainStartY);
72 	if (previousScreen() && previousScreen()->hasToBeResized) // resize background window
73 	{
74 		previousScreen()->resize();
75 		previousScreen()->refresh();
76 	}
77 	hasToBeResized = 0;
78 }
79 
title()80 std::wstring ServerInfo::title()
81 {
82 	return previousScreen()->title();
83 }
84 
update()85 void ServerInfo::update()
86 {
87 	if (Global::Timer - m_timer < boost::posix_time::seconds(1))
88 		return;
89 	m_timer = Global::Timer;
90 
91 	MPD::Statistics stats = Mpd.getStatistics();
92 	if (stats.empty())
93 		return;
94 
95 	w.clear();
96 
97 	w << NC::Format::Bold << "Version: " << NC::Format::NoBold << "0." << Mpd.Version() << ".*\n";
98 	w << NC::Format::Bold << "Uptime: " << NC::Format::NoBold;
99 	ShowTime(w, stats.uptime(), 1);
100 	w << '\n';
101 	w << NC::Format::Bold << "Time playing: " << NC::Format::NoBold << MPD::Song::ShowTime(stats.playTime()) << '\n';
102 	w << '\n';
103 	w << NC::Format::Bold << "Total playtime: " << NC::Format::NoBold;
104 	ShowTime(w, stats.dbPlayTime(), 1);
105 	w << '\n';
106 	w << NC::Format::Bold << "Artist names: " << NC::Format::NoBold << stats.artists() << '\n';
107 	w << NC::Format::Bold << "Album names: " << NC::Format::NoBold << stats.albums() << '\n';
108 	w << NC::Format::Bold << "Songs in database: " << NC::Format::NoBold << stats.songs() << '\n';
109 	w << '\n';
110 	w << NC::Format::Bold << "Last DB update: " << NC::Format::NoBold << Timestamp(stats.dbUpdateTime()) << '\n';
111 	w << '\n';
112 	w << NC::Format::Bold << "URL Handlers:" << NC::Format::NoBold;
113 	for (auto it = m_url_handlers.begin(); it != m_url_handlers.end(); ++it)
114 		w << (it != m_url_handlers.begin() ? ", " : " ") << *it;
115 	w << "\n\n";
116 	w << NC::Format::Bold << "Tag Types:" << NC::Format::NoBold;
117 	for (auto it = m_tag_types.begin(); it != m_tag_types.end(); ++it)
118 		w << (it != m_tag_types.begin() ? ", " : " ") << *it;
119 
120 	w.flush();
121 	w.refresh();
122 }
123 
SetDimensions()124 void ServerInfo::SetDimensions()
125 {
126 	m_width = COLS*0.6;
127 	m_height = std::min(size_t(LINES*0.7), MainHeight);
128 }
129 
130