mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-25 16:07:19 +00:00
248 lines
6.5 KiB
C++
248 lines
6.5 KiB
C++
/***************************************************************************
|
|
* This file is part of awesome-widgets *
|
|
* *
|
|
* awesome-widgets is free software: you can redistribute it and/or *
|
|
* modify it under the terms of the GNU General Public License as *
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
* License, or (at your option) any later version. *
|
|
* *
|
|
* awesome-widgets is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
|
|
***************************************************************************/
|
|
|
|
#include "abstractextitem.h"
|
|
|
|
#include <QDebug>
|
|
#include <QDir>
|
|
#include <QSettings>
|
|
#include <QStandardPaths>
|
|
#include <QTime>
|
|
|
|
#include <pdebug/pdebug.h>
|
|
|
|
#include "version.h"
|
|
|
|
|
|
AbstractExtItem::AbstractExtItem(QWidget *parent, const QString desktopName,
|
|
const QStringList directories, const bool debugCmd)
|
|
: QDialog(parent),
|
|
m_fileName(desktopName),
|
|
m_dirs(directories),
|
|
debug(debugCmd)
|
|
{
|
|
m_name = m_fileName;
|
|
}
|
|
|
|
|
|
AbstractExtItem::~AbstractExtItem()
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
}
|
|
|
|
|
|
template <class T>
|
|
T *AbstractExtItem::copy(const QString fileName, const int number)
|
|
{
|
|
Q_UNUSED(fileName)
|
|
Q_UNUSED(number)
|
|
|
|
// an analog of pure virtual method
|
|
return new T();
|
|
}
|
|
|
|
|
|
|
|
int AbstractExtItem::apiVersion() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_apiVersion;
|
|
}
|
|
|
|
|
|
QString AbstractExtItem::comment() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_comment;
|
|
}
|
|
|
|
|
|
QStringList AbstractExtItem::directories() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_dirs;
|
|
}
|
|
|
|
|
|
QString AbstractExtItem::fileName() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_fileName;
|
|
}
|
|
|
|
|
|
int AbstractExtItem::interval() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_interval;
|
|
}
|
|
|
|
|
|
bool AbstractExtItem::isActive() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_active;
|
|
}
|
|
|
|
|
|
QString AbstractExtItem::name() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_name;
|
|
}
|
|
|
|
|
|
int AbstractExtItem::number() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
return m_number;
|
|
}
|
|
|
|
|
|
QString AbstractExtItem::tag(const QString _type) const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Tag type" << _type;
|
|
|
|
return QString("%1%2").arg(_type).arg(m_number);
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setApiVersion(const int _apiVersion)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Version" << _apiVersion;
|
|
|
|
m_apiVersion = _apiVersion;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setActive(const bool _state)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "State" << _state;
|
|
|
|
m_active = _state;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setComment(const QString _comment)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Comment" << _comment;
|
|
|
|
m_comment = _comment;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setInterval(const int _interval)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Interval" << _interval;
|
|
if (_interval <= 0) return;
|
|
|
|
m_interval = _interval;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setName(const QString _name)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Name" << _name;
|
|
|
|
m_name = _name;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::setNumber(int _number)
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Number" << _number;
|
|
if (_number == -1) {
|
|
if (debug) qDebug() << PDEBUG << ":" << "Number is empty, generate new one";
|
|
qsrand(QTime::currentTime().msec());
|
|
_number = qrand() % 1000;
|
|
if (debug) qDebug() << PDEBUG << ":" << "Generated number is" << _number;
|
|
}
|
|
|
|
m_number = _number;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::readConfiguration()
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
for (int i=m_dirs.count()-1; i>=0; i--) {
|
|
if (!QDir(m_dirs[i]).entryList(QDir::Files).contains(m_fileName)) continue;
|
|
QSettings settings(QString("%1/%2").arg(m_dirs[i]).arg(m_fileName), QSettings::IniFormat);
|
|
|
|
settings.beginGroup(QString("Desktop Entry"));
|
|
setName(settings.value(QString("Name"), m_name).toString());
|
|
setComment(settings.value(QString("Comment"), m_comment).toString());
|
|
setApiVersion(settings.value(QString("X-AW-ApiVersion"), m_apiVersion).toInt());
|
|
setActive(settings.value(QString("X-AW-Active"), QVariant(m_active)).toString() == QString("true"));
|
|
setInterval(settings.value(QString("X-AW-Interval"), m_interval).toInt());
|
|
setNumber(settings.value(QString("X-AW-Number"), m_number).toInt());
|
|
settings.endGroup();
|
|
}
|
|
}
|
|
|
|
|
|
bool AbstractExtItem::tryDelete() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
for (int i=0; i<m_dirs.count(); i++)
|
|
if (debug) qDebug() << PDEBUG << ":" << "Remove file" << QString("%1/%2").arg(m_dirs[i]).arg(m_fileName) <<
|
|
QFile::remove(QString("%1/%2").arg(m_dirs[i]).arg(m_fileName));
|
|
|
|
// check if exists
|
|
for (int i=0; i<m_dirs.count(); i++)
|
|
if (QFile::exists(QString("%1/%2").arg(m_dirs[i]).arg(m_fileName))) return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
void AbstractExtItem::writeConfiguration() const
|
|
{
|
|
if (debug) qDebug() << PDEBUG;
|
|
|
|
QSettings settings(QString("%1/%2").arg(m_dirs[0]).arg(m_fileName), QSettings::IniFormat);
|
|
if (debug) qDebug() << PDEBUG << ":" << "Configuration file" << settings.fileName();
|
|
|
|
settings.beginGroup(QString("Desktop Entry"));
|
|
settings.setValue(QString("Encoding"), QString("UTF-8"));
|
|
settings.setValue(QString("Name"), m_name);
|
|
settings.setValue(QString("Comment"), m_comment);
|
|
settings.setValue(QString("X-AW-ApiVersion"), m_apiVersion);
|
|
settings.setValue(QString("X-AW-Active"), QVariant(m_active).toString());
|
|
settings.setValue(QString("X-AW-Interval"), m_interval);
|
|
settings.setValue(QString("X-AW-Number"), m_number);
|
|
settings.endGroup();
|
|
|
|
settings.sync();
|
|
}
|