JustAudio/io/conf.cpp

151 lines
3.3 KiB
C++
Raw Normal View History

#include "io/conf.h"
MenuItem::MenuItem(const QString &label, QWidget *widget, QObject *parent) : QWidgetAction(parent)
{
str = label;
wid = widget;
}
QWidget *MenuItem::createWidget(QWidget *parent)
{
QWidget *widget = new QWidget(parent);
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(new QLabel(str, parent));
layout->addStretch();
layout->addWidget(wid, 0, Qt::AlignRight);
return widget;
}
Conf::Conf(QObject *parent) : QObject(parent)
{
QFile file(confPath(), this);
QByteArray data;
if (file.open(QFile::ReadOnly))
{
data = file.readAll();
}
file.close();
Idm idm(data, this);
idm.rdHeader();
QByteArray pathBa = idm.value(LAST_PATH);
QByteArray nextBa = idm.value(NEXT_FILE);
QByteArray volBa = idm.value(VOLUME);
if (!pathBa.isEmpty()) lastPath = pathBa;
else lastPath = QDir::homePath();
if (!nextBa.isEmpty()) nextFileState = (nextBa == "Y");
else nextFileState = true;
if (!volBa.isEmpty()) volumeValue = rdInt(volBa);
else volumeValue = 50;
}
void Conf::sync()
{
Idm idm(this);
idm.wrHeader();
idm.insert(LAST_PATH, lastPath.toUtf8());
idm.insert(VOLUME, wrInt(volumeValue));
if (nextFileState) idm.insert(NEXT_FILE, "Y");
else idm.insert(NEXT_FILE, "N");
QFile file(confPath(), this);
file.open(QFile::WriteOnly | QFile::Truncate);
file.write(idm.getBuff());
}
void Conf::populateOptionsMenu(QMenu *menu, QWidget *parent)
{
QWidget *volWidget = new QWidget(parent);
QHBoxLayout *volLayout = new QHBoxLayout(volWidget);
QSlider *volSlider = new QSlider(parent);
QCheckBox *nextBox = new QCheckBox(parent);
volSlider->setMinimum(0);
volSlider->setMaximum(100);
volSlider->setValue(getVolume());
volSlider->setOrientation(Qt::Horizontal);
volLayout->addWidget(new QLabel("-"));
volLayout->addWidget(volSlider);
volLayout->addWidget(new QLabel("+"));
nextBox->setText("");
nextBox->setChecked(nextFile());
connect(volSlider, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
connect(volSlider, SIGNAL(valueChanged(int)), this, SIGNAL(volume(int)));
connect(nextBox, SIGNAL(clicked(bool)), this, SLOT(setNextFile(bool)));
menu->addAction(new MenuItem(tr("Volume"), volWidget, this));
menu->addAction(new MenuItem(tr("Auto play next file in directory"), nextBox, this));
}
void Conf::setLastPath(const QString &path)
{
lastPath = path; sync();
}
void Conf::setNextFile(bool state)
{
nextFileState = state; sync();
}
void Conf::setVolume(int value)
{
emit volume(value);
volumeValue = value; sync();
}
QString Conf::getLastPath()
{
if (!QDir(lastPath).exists())
{
lastPath = QDir::homePath();
}
return lastPath;
}
bool Conf::nextFile()
{
return nextFileState;
}
int Conf::getVolume()
{
return volumeValue;
}
QString Conf::confPath()
{
QString path = QDir::homePath() + '/';
#ifndef Q_OS_WIN32
path.append("/.config/");
#else
path.append("AppData/Local/");
#endif
QDir dir;
path.append(QApplication::applicationName() + '/');
dir.mkpath(path);
return path + "conf.idm";
}