1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//===========================================
// Lumina-Desktop source code
// Copyright (c) 2017, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "PianoBarProcess.h"
#include <QTime>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QApplication>
#include <LUtils.h>
PianoBarProcess::PianoBarProcess(QWidget *parent) : QObject(parent){
setupProcess();
saveTimer = new QTimer(this);
saveTimer->setInterval(100); //1/10 second (just enough to change a few settings at once before dumping to disk)
saveTimer->setSingleShot(true);
connect(saveTimer, SIGNAL(timeout()), this, SLOT(saveSettingsFile()) );
if( !loadSettings() ){ GenerateSettings(); }
}
PianoBarProcess::~PianoBarProcess(){
if(PROC->state()!=QProcess::NotRunning){
PROC->kill();
}
}
// ===== PUBLIC ======
//Interaction functions
bool PianoBarProcess::isSetup(){ //email/password already saved for use or not
return !(settingValue("user").isEmpty() || settingValue("password").isEmpty());
}
void PianoBarProcess::setLogin(QString email, QString pass){
setSettingValue("user",email);
setSettingValue("password",pass);
}
QString PianoBarProcess::email(){
return settingValue("user");
}
QString PianoBarProcess::password(){
return settingValue("password");
}
void PianoBarProcess::closePianoBar(){ //"q"
sendToProcess("q");
}
QString PianoBarProcess::currentStation(){ return cstation; }
QStringList PianoBarProcess::stations(){ return stationList; }
void PianoBarProcess::setCurrentStation(QString station){
cstation = station;
sendToProcess("s");
}
void PianoBarProcess::deleteCurrentStation(){ //"d" -> "y"
if(cstation == "QuickMix" || cstation=="Thumbprint Radio"){ return; } //cannot delete these stations - provided by Pandora itself
sendToProcess("d"); //delete current station
sendToProcess("y",true); //yes, we want to delete it
//Now need to automatically change to another station
setCurrentStation("QuickMix"); //this is always a valid station
}
//void PianoBarProcess::createNewStation(); //"c"
void PianoBarProcess::createStationFromCurrentSong(){ //"v" -> "s"
sendToProcess("v");
sendToProcess("s",true);
}
void PianoBarProcess::createStationFromCurrentArtist(){ //"v" -> "a"
sendToProcess("v");
sendToProcess("a",true);
}
//Settings Manipulation
QString PianoBarProcess::audioQuality(){ // "audio_quality" = [low, medium, high]
return settingValue("audio_quality");
}
void PianoBarProcess::setAudioQuality(QString val){ // [low, medium, high]
setSettingValue("audio_quality",val);
}
QString PianoBarProcess::autostartStation(){ //"autostart_station" = ID
return settingValue("autostart_station");
}
void PianoBarProcess::setAutostartStation(QString id){
setSettingValue("autostart_station", id);
}
QString PianoBarProcess::proxy(){ //"proxy" = URL (example: "http://USER:PASSWORD@HOST:PORT/" )
return settingValue("proxy");
}
void PianoBarProcess::setProxy(QString url){
setSettingValue("proxy",url);
}
QString PianoBarProcess::controlProxy(){ //"control_proxy" = URL (example: "http://USER:PASSWORD@HOST:PORT/" )
return settingValue("control_proxy");
}
void PianoBarProcess::setControlProxy(QString url){
setSettingValue("control_proxy", url);
}
// ====== PUBLIC SLOTS ======
void PianoBarProcess::play(){
if(PROC->state() == QProcess::NotRunning){
PROC->start();
}else{
sendToProcess("P");
}
}
// ====== PRIVATE ======
void PianoBarProcess::GenerateSettings(){
currentSettings << "audio_quality = medium";
currentSettings << "autoselect = 1"; //automatically select the last item in a list (station selection only)
currentSettings << "format_list_song = %r::::%t::::%a"; //[rating, title, artist]
currentSettings << "format_nowplaying_song = %r::::%t::::%a::::%l::::%u::::%s"; // [rating, title, artist, album, details url, station (if not quickmix)]
currentSettings << "format_nowplaying_station = %n::::%i"; //[name, id]
saveSettingsFile(); //save this to disk *now* - needed before starting the pianobar process
}
bool PianoBarProcess::loadSettings(){
currentSettings.clear();
QFile file(settingsPath);
if(!file.exists()){ return false; }
if(file.open(QIODevice::ReadOnly)){
QTextStream in(&file);
currentSettings = in.readAll().split("\n");
file.close();
return true;
}
return false;
}
QString PianoBarProcess::settingValue(QString var){
for(int i=0; i<currentSettings.length(); i++){
if(currentSettings[i].startsWith(var+" = ")){ return currentSettings[i].section(" = ", 1,-1); }
}
return "";
}
void PianoBarProcess::setSettingValue(QString var,QString val){
bool changed = false;
for(int i=0; i<currentSettings.length() && !changed; i++){
if(currentSettings[i].startsWith(var+" = ")){ currentSettings[i] = var+" = "+val; changed = true; }
}
if(!changed){ currentSettings << var+" = "+val; }
saveTimer->start(); //save this to disk in a moment
}
void PianoBarProcess::saveSettingsFile(){
//Ensure the parent directory exists first
QDir dir(settingsPath.section("/",0,-2));
if(!dir.exists()){ dir.mkpath(dir.absolutePath()); }
//Now save the settings
QFile file(settingsPath);
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
QTextStream out(&file);
out << currentSettings.join("\n");
file.close();
}
}
void PianoBarProcess::setupProcess(){
PROC = new QProcess(this);
//Ensure this process always points to the right configuration directory/files
QString configdir = getenv("XDG_CONFIG_HOME");
if(configdir.isEmpty()){ configdir = QDir::homePath()+"/.config/lumina-desktop"; }
else{ configdir.append("/lumina-desktop"); }
QProcessEnvironment penv;
penv.insert("XDG_CONFIG_HOME",configdir);
settingsPath = configdir+"/pianobar/config";
PROC->setProcessEnvironment(penv);
//Now setup the rest of the process
PROC->setProcessChannelMode(QProcess::MergedChannels);
QString bin = "pianobar";
LUtils::isValidBinary(bin); //will change "bin" to the full path
PROC->setProgram(bin);
connect(PROC, SIGNAL(readyRead()), this, SLOT(ProcUpdate()) );
}
void PianoBarProcess::sendToProcess(QString txt, bool withreturn){
if(PROC->state()==QProcess::Running){
if(withreturn){ PROC->write( QString(txt+"\r\n").toLocal8Bit() ); }
else{ PROC->write( QString(txt).toLocal8Bit() ); }
}
}
// ====== PRIVATE SLOTS ======
void PianoBarProcess::ProcUpdate(){
QString tmp = QString(PROC->readAllStandardOutput()).replace("\r","\n").remove("\u001B[2K");
QStringList info = tmp.split("\n",QString::SkipEmptyParts);
//NOTE: Need to have a cache of info lines which can carry over between updates as needed (for questions, etc)
//qDebug() << "Got Update:" << info;
for(int i=0; i<info.length(); i++){
//First handle any pending cache of listing lines
if((info[i].startsWith("\t")||info[i].startsWith(" ")) && info[i].contains(")")){
if(info[i].simplified().startsWith("0) ")){ infoList.clear(); }
infoList << info[i].section(") ",1,-1).simplified();
continue; //done handling this line
}else if(!info[i].startsWith("[?]") && !infoList.isEmpty()){
emit NewList(infoList);
infoList.clear();
}
//Now parse the lines for messages/etc
if(info[i].startsWith("|>")){
//Now playing line (station, or song)
QStringList data = info[i].section(">",1,-1).simplified().split("::::"); //Make sure to chop the line prefix off first
if(data.length()==2){ //station
cstation = data[0]; //save the name for later
emit NowPlayingStation(data[0], data[1]);
if(stationList.isEmpty()){
//Need to prompt to list all the available stations
sendToProcess("s",true);//line return cancels the prompt
//sendToProcess("",true); //empty line - cancels the prompt
}
//Automatically save this station for autostart next time (make toggle-able later)
if(data[1]!=autostartStation()){ setAutostartStation(data[1]); }
}else if(data.length()==6){ //song
emit NowPlayingSong( data[0]=="<3", data[1], data[2], data[3], data[4], data[5] );
}
}else if(info[i].startsWith("(i) ")){ //informational line
emit NewInformation(info[i].section(" ",1,-1));
}else if(info[i].startsWith("[?] ")){ //waiting for reply to question
qDebug() << "Got Question:" << info[i] << infoList;
if(info[i].contains("Select station:")){
qDebug() << "Change to Station:" << cstation;
stationList = infoList; //save this list for later
infoList.clear();
emit StationListChanged(stationList);
//Find the station number which corresponds to the cstation variable/name
for(int i=0; i<stationList.length(); i++){
if(stationList[i].endsWith(cstation) ){
qDebug() << "Activate Station:" << stationList[i];
sendToProcess(QString::number(i), true);
break;
}else if(i==stationList.length()-1){
qDebug() << "Activate Last Station:" << stationList[i];
sendToProcess(QString::number(stationList.length()-1), true);
}
}
}
}else if(info[i].startsWith("#")){
//Time Stamp
QTime stamp = QTime::fromString(info[i].section("/",0,0).section("-",1,-1), "mm:ss");
int curS = 60*stamp.minute() + stamp.second(); //time remaining
stamp = QTime::fromString(info[i].section("/",1,-1), "mm:ss");
int totS = 60*stamp.minute() + stamp.second(); //time total
emit TimeUpdate(totS-curS, totS);
}
}
}
|