mirror of
				https://github.com/arcan1s/awesome-widgets.git
				synced 2025-11-03 22:33:42 +00:00 
			
		
		
		
	* implement interval to extupgrade (ApiVer = 1) (fix #45)
* improve extscript logic * add cache values to both of them
This commit is contained in:
		@ -971,6 +971,7 @@ void AwesomeWidget::copyPkgCommand(const QString original)
 | 
			
		||||
    upgrade->setExecutable(originalUpgrade->executable());
 | 
			
		||||
    upgrade->setName(originalUpgrade->name());
 | 
			
		||||
    upgrade->setNull(originalUpgrade->null());
 | 
			
		||||
    upgrade->setInterval(originalUpgrade->interval());
 | 
			
		||||
    delete originalUpgrade;
 | 
			
		||||
 | 
			
		||||
    if (upgrade->showConfiguration() == 1) {
 | 
			
		||||
 | 
			
		||||
@ -1077,6 +1077,7 @@ void AWKeys::copyUpgrade(const QString original)
 | 
			
		||||
        upgrade->setExecutable(extUpgrade[originalItem]->executable());
 | 
			
		||||
        upgrade->setName(extUpgrade[originalItem]->name());
 | 
			
		||||
        upgrade->setNull(extUpgrade[originalItem]->null());
 | 
			
		||||
        upgrade->setInterval(extUpgrade[originalItem]->interval());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (upgrade->showConfiguration() == 1) {
 | 
			
		||||
 | 
			
		||||
@ -273,46 +273,45 @@ void ExtScript::readConfiguration()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ExtScript::ScriptData ExtScript::run(const int time)
 | 
			
		||||
QString ExtScript::run()
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
    if (!m_active) return value;
 | 
			
		||||
 | 
			
		||||
    ScriptData response;
 | 
			
		||||
    response.active = m_active;
 | 
			
		||||
    response.name = m_name;
 | 
			
		||||
    response.refresh = false;
 | 
			
		||||
    if (!m_active) return response;
 | 
			
		||||
    if (time != m_interval) return response;
 | 
			
		||||
    response.refresh = true;
 | 
			
		||||
    if (times == 1) {
 | 
			
		||||
        QStringList cmdList;
 | 
			
		||||
        if (!m_prefix.isEmpty())
 | 
			
		||||
            cmdList.append(m_prefix);
 | 
			
		||||
        cmdList.append(m_executable);
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "cmd" << cmdList.join(QChar(' '));
 | 
			
		||||
        TaskResult process = runTask(cmdList.join(QChar(' ')));
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode;
 | 
			
		||||
        if (process.exitCode != 0)
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error;
 | 
			
		||||
 | 
			
		||||
    QStringList cmdList;
 | 
			
		||||
    if (!m_prefix.isEmpty())
 | 
			
		||||
        cmdList.append(m_prefix);
 | 
			
		||||
    cmdList.append(m_executable);
 | 
			
		||||
    if (debug) qDebug() << PDEBUG << ":" << "cmd" << cmdList.join(QChar(' '));
 | 
			
		||||
    TaskResult process = runTask(cmdList.join(QChar(' ')));
 | 
			
		||||
    if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode;
 | 
			
		||||
    if (process.exitCode != 0)
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error;
 | 
			
		||||
 | 
			
		||||
    QString info = QString::number(process.exitCode) + QString(":") +
 | 
			
		||||
                   QTextCodec::codecForMib(106)->toUnicode(process.error).trimmed();
 | 
			
		||||
    QString qoutput = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed();
 | 
			
		||||
    switch (m_redirect) {
 | 
			
		||||
    case stdout2stderr:
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Debug" << info;
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Output" << qoutput;
 | 
			
		||||
        break;
 | 
			
		||||
    case stderr2stdout:
 | 
			
		||||
        response.output = info + QString("\t") + qoutput;
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Debug" << info;
 | 
			
		||||
        response.output = qoutput;
 | 
			
		||||
        break;
 | 
			
		||||
        QString info = QString::number(process.exitCode) + QString(":") +
 | 
			
		||||
                    QTextCodec::codecForMib(106)->toUnicode(process.error).trimmed();
 | 
			
		||||
        QString qoutput = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed();
 | 
			
		||||
        switch (m_redirect) {
 | 
			
		||||
        case stdout2stderr:
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Debug" << info;
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Output" << qoutput;
 | 
			
		||||
            break;
 | 
			
		||||
        case stderr2stdout:
 | 
			
		||||
            value = info + QString("\t") + qoutput;
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Debug" << info;
 | 
			
		||||
            value = qoutput;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return response;
 | 
			
		||||
    // update value
 | 
			
		||||
    times++;
 | 
			
		||||
    if (times >= m_interval) times = 0;
 | 
			
		||||
 | 
			
		||||
    return value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -44,12 +44,6 @@ public:
 | 
			
		||||
        nothing,
 | 
			
		||||
        stderr2stdout
 | 
			
		||||
    };
 | 
			
		||||
    typedef struct {
 | 
			
		||||
        bool active;
 | 
			
		||||
        QString name;
 | 
			
		||||
        QString output;
 | 
			
		||||
        bool refresh;
 | 
			
		||||
    } ScriptData;
 | 
			
		||||
 | 
			
		||||
    explicit ExtScript(QWidget *parent = 0, const QString scriptName = QString(),
 | 
			
		||||
                       const QStringList directories = QStringList(), const bool debugCmd = false);
 | 
			
		||||
@ -80,7 +74,7 @@ public:
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
    void readConfiguration();
 | 
			
		||||
    ScriptData run(const int time);
 | 
			
		||||
    QString run();
 | 
			
		||||
    int showConfiguration();
 | 
			
		||||
    int tryDelete();
 | 
			
		||||
    void writeConfiguration();
 | 
			
		||||
@ -100,6 +94,8 @@ private:
 | 
			
		||||
    bool m_output = true;
 | 
			
		||||
    QString m_prefix = QString("");
 | 
			
		||||
    Redirect m_redirect = nothing;
 | 
			
		||||
    int times = 0;
 | 
			
		||||
    QString value = QString();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -145,7 +145,6 @@ void ExtendedSysMon::initScripts()
 | 
			
		||||
                                     QStandardPaths::LocateDirectory);
 | 
			
		||||
#endif /* BUILD_KDE4 */
 | 
			
		||||
 | 
			
		||||
    times.clear();
 | 
			
		||||
    QStringList names;
 | 
			
		||||
    for (int i=0; i<dirs.count(); i++) {
 | 
			
		||||
        QStringList files = QDir(dirs[i]).entryList(QDir::Files, QDir::Name);
 | 
			
		||||
@ -155,7 +154,6 @@ void ExtendedSysMon::initScripts()
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Found file" << files[j] << "in" << dirs[i];
 | 
			
		||||
            names.append(files[j]);
 | 
			
		||||
            externalScripts.append(new ExtScript(0, files[j], dirs, debug));
 | 
			
		||||
            times.append(1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -666,15 +664,8 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source)
 | 
			
		||||
            setData(source, battery.keys()[i], battery[battery.keys()[i]].toInt());
 | 
			
		||||
        }
 | 
			
		||||
    } else if (source == QString("custom")) {
 | 
			
		||||
        for (int i=0; i<externalScripts.count(); i++) {
 | 
			
		||||
            ExtScript::ScriptData data = externalScripts[i]->run(times[i]);
 | 
			
		||||
            if (!data.active) return true;
 | 
			
		||||
            if (data.refresh) {
 | 
			
		||||
                times[i] = 1;
 | 
			
		||||
                setData(source, QString("custom") + QString::number(i), data.output);
 | 
			
		||||
            } else
 | 
			
		||||
                times[i]++;
 | 
			
		||||
        }
 | 
			
		||||
        for (int i=0; i<externalScripts.count(); i++)
 | 
			
		||||
            setData(source, QString("custom") + QString::number(i), externalScripts[i]->run());
 | 
			
		||||
    } else if (source == QString("desktop")) {
 | 
			
		||||
        QMap<QString, QVariant> desktop = getCurrentDesktop();
 | 
			
		||||
        for (int i=0; i<desktop.keys().count(); i++)
 | 
			
		||||
@ -694,12 +685,8 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source)
 | 
			
		||||
    } else if (source == QString("netdev")) {
 | 
			
		||||
        setData(source, QString("value"), getNetworkDevice());
 | 
			
		||||
    } else if (source == QString("pkg")) {
 | 
			
		||||
        if (pkgTimeUpdate >= SEC_IN_HOUR) {
 | 
			
		||||
            for (int i=0; i<externalUpgrade.count(); i++)
 | 
			
		||||
                setData(source, QString("pkgcount") + QString::number(i), externalUpgrade[i]->run());
 | 
			
		||||
            pkgTimeUpdate = 0;
 | 
			
		||||
        }
 | 
			
		||||
        pkgTimeUpdate++;
 | 
			
		||||
        for (int i=0; i<externalUpgrade.count(); i++)
 | 
			
		||||
            setData(source, QString("pkgcount") + QString::number(i), externalUpgrade[i]->run());
 | 
			
		||||
    } else if (source == QString("player")) {
 | 
			
		||||
        QMap<QString, QVariant> player = getPlayerInfo(configuration[QString("PLAYER")],
 | 
			
		||||
                configuration[QString("MPDADDRESS")],
 | 
			
		||||
 | 
			
		||||
@ -18,10 +18,6 @@
 | 
			
		||||
#ifndef EXTSYSMON_H
 | 
			
		||||
#define EXTSYSMON_H
 | 
			
		||||
 | 
			
		||||
#ifndef SEC_IN_HOUR
 | 
			
		||||
#define SEC_IN_HOUR 60*60
 | 
			
		||||
#endif /* SEC_IN_HOUR */
 | 
			
		||||
 | 
			
		||||
#include <Plasma/DataEngine>
 | 
			
		||||
#include <QProcess>
 | 
			
		||||
 | 
			
		||||
@ -61,10 +57,7 @@ private:
 | 
			
		||||
    QMap<QString, QString> configuration;
 | 
			
		||||
    QList<ExtScript *> externalScripts;
 | 
			
		||||
    QList<ExtUpgrade *> externalUpgrade;
 | 
			
		||||
    QList<int> times;
 | 
			
		||||
    bool debug;
 | 
			
		||||
    // FIXME dirty hack to avoid update package information every second
 | 
			
		||||
    int pkgTimeUpdate = SEC_IN_HOUR;
 | 
			
		||||
    // reread configuration
 | 
			
		||||
    QStringList allHddDevices;
 | 
			
		||||
    QString getAllHdd();
 | 
			
		||||
 | 
			
		||||
@ -82,6 +82,14 @@ QString ExtUpgrade::fileName()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int ExtUpgrade::interval()
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
 | 
			
		||||
    return m_interval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
QString ExtUpgrade::name()
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
@ -142,6 +150,15 @@ void ExtUpgrade::setExecutable(const QString _executable)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void ExtUpgrade::setInterval(const int _interval)
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
    if (debug) qDebug() << PDEBUG << ":" << "Interval" << _interval;
 | 
			
		||||
 | 
			
		||||
    m_interval = _interval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void ExtUpgrade::setName(const QString _name)
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
@ -176,6 +193,7 @@ void ExtUpgrade::readConfiguration()
 | 
			
		||||
        setExecutable(settings.value(QString("Exec"), m_executable).toString());
 | 
			
		||||
        setActive(settings.value(QString("X-AW-Active"), QVariant(m_active)).toString() == QString("true"));
 | 
			
		||||
        setNull(settings.value(QString("X-AW-Null"), m_null).toInt());
 | 
			
		||||
        setInterval(settings.value(QString("X-AW-Interval"), m_interval).toInt());
 | 
			
		||||
        settings.endGroup();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -184,16 +202,23 @@ void ExtUpgrade::readConfiguration()
 | 
			
		||||
int ExtUpgrade::run()
 | 
			
		||||
{
 | 
			
		||||
    if (debug) qDebug() << PDEBUG;
 | 
			
		||||
    if (!m_active) return 0;
 | 
			
		||||
    if (!m_active) return value;
 | 
			
		||||
 | 
			
		||||
    TaskResult process = runTask(QString("bash -c \"") + m_executable + QString("\""));
 | 
			
		||||
    if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode;
 | 
			
		||||
    if (process.exitCode != 0)
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error;
 | 
			
		||||
    if (times == 1) {
 | 
			
		||||
        TaskResult process = runTask(QString("bash -c \"") + m_executable + QString("\""));
 | 
			
		||||
        if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode;
 | 
			
		||||
        if (process.exitCode != 0)
 | 
			
		||||
            if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error;
 | 
			
		||||
 | 
			
		||||
    QString qoutput = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed();
 | 
			
		||||
        QString qoutput = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed();
 | 
			
		||||
        value = qoutput.split(QChar('\n'), QString::SkipEmptyParts).count() - m_null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (qoutput.split(QChar('\n'), QString::SkipEmptyParts).count() - m_null);
 | 
			
		||||
    // update value
 | 
			
		||||
    times++;
 | 
			
		||||
    if (times >= m_interval) times = 0;
 | 
			
		||||
 | 
			
		||||
    return value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -209,6 +234,7 @@ int ExtUpgrade::showConfiguration()
 | 
			
		||||
    else
 | 
			
		||||
        ui->checkBox_active->setCheckState(Qt::Unchecked);
 | 
			
		||||
    ui->spinBox_null->setValue(m_null);
 | 
			
		||||
    ui->spinBox_interval->setValue(m_interval);
 | 
			
		||||
 | 
			
		||||
    int ret = exec();
 | 
			
		||||
    if (ret != 1) return ret;
 | 
			
		||||
@ -218,6 +244,7 @@ int ExtUpgrade::showConfiguration()
 | 
			
		||||
    setExecutable(ui->lineEdit_command->text());
 | 
			
		||||
    setActive(ui->checkBox_active->checkState() == Qt::Checked);
 | 
			
		||||
    setNull(ui->spinBox_null->value());
 | 
			
		||||
    setInterval(ui->spinBox_interval->value());
 | 
			
		||||
 | 
			
		||||
    writeConfiguration();
 | 
			
		||||
    return ret;
 | 
			
		||||
@ -254,6 +281,7 @@ void ExtUpgrade::writeConfiguration()
 | 
			
		||||
    settings.setValue(QString("X-AW-ApiVersion"), m_apiVersion);
 | 
			
		||||
    settings.setValue(QString("X-AW-Active"), QVariant(m_active).toString());
 | 
			
		||||
    settings.setValue(QString("X-AW-Null"), m_null);
 | 
			
		||||
    settings.setValue(QString("X-AW-Interval"), m_interval);
 | 
			
		||||
    settings.endGroup();
 | 
			
		||||
 | 
			
		||||
    settings.sync();
 | 
			
		||||
 | 
			
		||||
@ -34,6 +34,7 @@ class ExtUpgrade : public QDialog
 | 
			
		||||
    Q_PROPERTY(QString executable READ executable WRITE setExecutable)
 | 
			
		||||
    Q_PROPERTY(int null READ null WRITE setNull)
 | 
			
		||||
    Q_PROPERTY(bool active READ isActive WRITE setActive)
 | 
			
		||||
    Q_PROPERTY(int interval READ interval WRITE setInterval)
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit ExtUpgrade(QWidget *parent = 0, const QString upgradeName = QString(),
 | 
			
		||||
@ -44,6 +45,7 @@ public:
 | 
			
		||||
    QString comment();
 | 
			
		||||
    QString executable();
 | 
			
		||||
    QString fileName();
 | 
			
		||||
    int interval();
 | 
			
		||||
    QString name();
 | 
			
		||||
    int null();
 | 
			
		||||
    bool isActive();
 | 
			
		||||
@ -54,6 +56,7 @@ public:
 | 
			
		||||
    void setExecutable(const QString _executable = QString("/usr/bin/true"));
 | 
			
		||||
    void setName(const QString _name = QString("none"));
 | 
			
		||||
    void setNull(const int _null = 0);
 | 
			
		||||
    void setInterval(const int _interval = 0);
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
    void readConfiguration();
 | 
			
		||||
@ -74,6 +77,9 @@ private:
 | 
			
		||||
    QString m_executable = QString("/usr/bin/true");
 | 
			
		||||
    QString m_name = QString("none");
 | 
			
		||||
    int m_null = 0;
 | 
			
		||||
    int m_interval = 3600;
 | 
			
		||||
    int times = 0;
 | 
			
		||||
    int value = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -137,6 +137,36 @@
 | 
			
		||||
     </item>
 | 
			
		||||
    </layout>
 | 
			
		||||
   </item>
 | 
			
		||||
   <item>
 | 
			
		||||
    <layout class="QHBoxLayout" name="layout_interval">
 | 
			
		||||
     <item>
 | 
			
		||||
      <widget class="QLabel" name="label_interval">
 | 
			
		||||
       <property name="text">
 | 
			
		||||
        <string>Interval</string>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="alignment">
 | 
			
		||||
        <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
 | 
			
		||||
       </property>
 | 
			
		||||
      </widget>
 | 
			
		||||
     </item>
 | 
			
		||||
     <item>
 | 
			
		||||
      <widget class="QSpinBox" name="spinBox_interval">
 | 
			
		||||
       <property name="minimum">
 | 
			
		||||
        <number>1</number>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="maximum">
 | 
			
		||||
        <number>10000</number>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="singleStep">
 | 
			
		||||
        <number>500</number>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="value">
 | 
			
		||||
        <number>3600</number>
 | 
			
		||||
       </property>
 | 
			
		||||
      </widget>
 | 
			
		||||
     </item>
 | 
			
		||||
    </layout>
 | 
			
		||||
   </item>
 | 
			
		||||
   <item>
 | 
			
		||||
    <spacer name="verticalSpacer">
 | 
			
		||||
     <property name="orientation">
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,5 @@ Exec=pacman -Qu
 | 
			
		||||
X-AW-Prefix=
 | 
			
		||||
X-AW-Active=false
 | 
			
		||||
X-AW-Null=0
 | 
			
		||||
X-AW-Interval=3600
 | 
			
		||||
X-AW-ApiVersion=1
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,5 @@ Exec=apt-show-versions -u -b
 | 
			
		||||
X-AW-Prefix=
 | 
			
		||||
X-AW-Active=false
 | 
			
		||||
X-AW-Null=0
 | 
			
		||||
X-AW-Interval=3600
 | 
			
		||||
X-AW-ApiVersion=1
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,5 @@ Exec=yum list updates
 | 
			
		||||
X-AW-Prefix=
 | 
			
		||||
X-AW-Active=false
 | 
			
		||||
X-AW-Null=3
 | 
			
		||||
X-AW-Interval=3600
 | 
			
		||||
X-AW-ApiVersion=1
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,5 @@ Exec=urpmq --auto-select
 | 
			
		||||
X-AW-Prefix=
 | 
			
		||||
X-AW-Active=false
 | 
			
		||||
X-AW-Null=0
 | 
			
		||||
X-AW-Interval=3600
 | 
			
		||||
X-AW-ApiVersion=1
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,5 @@ Exec=aptitude search '~U'
 | 
			
		||||
X-AW-Prefix=
 | 
			
		||||
X-AW-Active=false
 | 
			
		||||
X-AW-Null=0
 | 
			
		||||
X-AW-Interval=3600
 | 
			
		||||
X-AW-ApiVersion=1
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user