some refactoring, prepare to moving to qt5 style logging

This commit is contained in:
arcan1s 2015-08-28 00:15:25 +03:00
parent 8228d1d06b
commit 466faf53fa
8 changed files with 59 additions and 130 deletions

3
.gitmodules vendored
View File

@ -1,6 +1,3 @@
[submodule "sources/3rdparty/pdebug"]
path = sources/3rdparty/pdebug
url = https://github.com/arcan1s/qtadds-pdebug.git
[submodule "sources/3rdparty/task"]
path = sources/3rdparty/task
url = https://github.com/arcan1s/qtadds-taskadds-qprocess.git

@ -1 +0,0 @@
Subproject commit 75e4a0df83478802bba53345486b42c56713fe09

View File

@ -79,11 +79,6 @@ void AWKeys::initKeys(const QString currentPattern)
{
if (debug) qDebug() << PDEBUG;
// clear
keys.clear();
foundBars.clear();
foundKeys.clear();
// init
pattern = currentPattern;
// update network and hdd list
@ -133,28 +128,6 @@ void AWKeys::setWrapNewLines(const bool wrap)
}
QString AWKeys::parsePattern() const
{
if (debug) qDebug() << PDEBUG;
if (keys.isEmpty()) return pattern;
QString parsed = pattern;
parsed.replace(QString("$$"), QString("$\\$\\"));
foreach(QString key, foundLambdas)
parsed.replace(QString("${{%1}}").arg(key), calculateLambda(key));
foreach(QString key, foundKeys)
parsed.replace(QString("$%1").arg(key), htmlValue(key));
foreach(QString bar, foundBars)
parsed.replace(QString("$%1").arg(bar),
graphicalItems->itemByTag(bar)->image(valueByKey(bar).toFloat()));
parsed.replace(QString("$\\$\\"), QString("$$"));
// wrap new lines if required
if (wrapNewLines) parsed.replace(QString("\n"), QString("<br>"));
return parsed;
}
QSize AWKeys::toolTipSize() const
{
if (debug) qDebug() << PDEBUG;
@ -202,13 +175,12 @@ QStringList AWKeys::dictKeys(const bool sorted, const QString regexp) const
// uptime
allKeys.append(QString("uptime"));
allKeys.append(QString("cuptime"));
// cpuclock
for (int i=numberCpus()-1; i>=0; i--)
// cpuclock & cpu
for (int i=QThread::idealThreadCount()-1; i>=0; i--) {
allKeys.append(QString("cpucl%1").arg(i));
allKeys.append(QString("cpucl"));
// cpu
for (int i=numberCpus()-1; i>=0; i--)
allKeys.append(QString("cpu%1").arg(i));
}
allKeys.append(QString("cpucl"));
allKeys.append(QString("cpu"));
// temperature
for (int i=tempDevices.count()-1; i>=0; i--)
@ -366,9 +338,6 @@ void AWKeys::setDataBySource(const QString sourceName, const QVariantMap data,
if (sourceName == QString("update")) return emit(needToBeUpdated());
// checking
if (keys.isEmpty()) return;
// regular expressions
QRegExp cpuRegExp = QRegExp(QString("cpu/cpu.*/TotalLoad"));
QRegExp cpuclRegExp = QRegExp(QString("cpu/cpu.*/clock"));
@ -738,14 +707,7 @@ void AWKeys::editItem(const QString type)
if (debug) qDebug() << PDEBUG;
if (type == QString("graphicalitem")) {
QStringList bars;
bars.append(keys.filter((QRegExp(QString("^cpu(?!cl).*")))));
bars.append(keys.filter((QRegExp(QString("^gpu$")))));
bars.append(keys.filter((QRegExp(QString("^mem$")))));
bars.append(keys.filter((QRegExp(QString("^swap$")))));
bars.append(keys.filter((QRegExp(QString("^hdd[0-9].*")))));
bars.append(keys.filter((QRegExp(QString("^bat.*")))));
graphicalItems->setConfigArgs(bars);
graphicalItems->setConfigArgs(dictKeys(true, QString("^(cpu(?!cl).*|gpu$|mem$|swap$|hdd[0-9].*|bat.*)")));
return graphicalItems->editItems();
} else if (type == QString("extquotes")) {
return extQuotes->editItems();
@ -813,7 +775,6 @@ void AWKeys::reinitKeys()
{
if (debug) qDebug() << PDEBUG;
keys = dictKeys();
foundBars = findGraphicalItems();
foundKeys = findKeys();
foundLambdas = findLambdas();
@ -865,44 +826,41 @@ void AWKeys::addKeyToCache(const QString type, const QString key)
}
QString AWKeys::calculateLambda(QString key) const
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Lambda key" << key;
QScriptEngine engine;
foreach(QString lambdaKey, foundKeys)
key.replace(QString("$%1").arg(lambdaKey), values[lambdaKey]);
if (debug) qDebug() << PDEBUG << ":" << "Expression" << key;
QScriptValue result = engine.evaluate(key);
if (engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
if (debug) qDebug() << PDEBUG << ":" << "Uncaught exception at line"
<< line << ":" << result.toString();
return QString();
} else
return result.toString();
}
QString AWKeys::htmlValue(QString key) const
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Requested key" << key;
QString value = values[key];
if (!key.startsWith(QString("custom")))
value.replace(QString(" "), QString("&nbsp;"));
return value;
}
int AWKeys::numberCpus() const
QString AWKeys::parsePattern() const
{
if (debug) qDebug() << PDEBUG;
return QThread::idealThreadCount();
QString parsed = pattern;
parsed.replace(QString("$$"), QString("$\\$\\"));
foreach(QString key, foundLambdas)
parsed.replace(QString("${{%1}}").arg(key), [this](QString key) {
QScriptEngine engine;
foreach(QString lambdaKey, foundKeys)
key.replace(QString("$%1").arg(lambdaKey), values[lambdaKey]);
if (debug) qDebug() << PDEBUG << ":" << "Expression" << key;
QScriptValue result = engine.evaluate(key);
if (engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
if (debug) qDebug() << PDEBUG << ":" << "Uncaught exception at line"
<< line << ":" << result.toString();
return QString();
} else
return result.toString();
}(key));
foreach(QString key, foundKeys)
parsed.replace(QString("$%1").arg(key), [](QString key, QString value) {
if (!key.startsWith(QString("custom")))
value.replace(QString(" "), QString("&nbsp;"));
return value;
}(key, values[key]));
foreach(QString bar, foundBars)
parsed.replace(QString("$%1").arg(bar),
graphicalItems->itemByTag(bar)->image(valueByKey(bar).toFloat()));
parsed.replace(QString("$\\$\\"), QString("$$"));
// wrap new lines if required
if (wrapNewLines) parsed.replace(QString("\n"), QString("<br>"));
return parsed;
}
@ -955,7 +913,7 @@ QStringList AWKeys::findKeys() const
if (debug) qDebug() << PDEBUG;
QStringList selectedKeys;
foreach(QString key, keys) {
foreach(QString key, dictKeys()) {
if (key.startsWith(QString("bar"))) continue;
if (pattern.contains(QString("$%1").arg(key))) {
if (debug) qDebug() << PDEBUG << ":" << "Found key" << key;

View File

@ -48,7 +48,6 @@ public:
Q_INVOKABLE void setPopupEnabled(const bool popup = false);
Q_INVOKABLE void setTranslateStrings(const bool translate = false);
Q_INVOKABLE void setWrapNewLines(const bool wrap = false);
Q_INVOKABLE QString parsePattern() const;
Q_INVOKABLE QSize toolTipSize() const;
// keys
Q_INVOKABLE void addDevice(const QString source);
@ -78,9 +77,7 @@ private slots:
private:
// methods
void addKeyToCache(const QString type, const QString key = QString(""));
QString calculateLambda(QString key) const;
QString htmlValue(QString key) const;
int numberCpus() const;
QString parsePattern() const;
float temperature(const float temp, const QString units) const;
// find methods
QStringList findGraphicalItems() const;
@ -98,7 +95,7 @@ private:
ExtItemAggregator<ExtUpgrade> *extUpgrade;
ExtItemAggregator<ExtWeather> *extWeather;
QString pattern;
QStringList foundBars, foundKeys, foundLambdas, keys;
QStringList foundBars, foundKeys, foundLambdas;
QStringList timeKeys = QString(TIME_KEYS).split(QChar(','));
QHash<QString, QString> values;
QStringList diskDevices, hddDevices, mountDevices, networkDevices, tempDevices;

View File

@ -50,9 +50,9 @@ DPAdds::DPAdds(QObject *parent)
QString debugEnv = environment.value(QString("DEBUG"), QString("no"));
debug = (debugEnv == QString("yes"));
connect(KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)), this, SLOT(changeDesktop(int)));
connect(KWindowSystem::self(), SIGNAL(windowAdded(WId)), this, SLOT(changeWindowList(WId)));
connect(KWindowSystem::self(), SIGNAL(windowRemoved(WId)), this, SLOT(changeWindowList(WId)));
connect(KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)), this, SIGNAL(desktopChanged()));
connect(KWindowSystem::self(), SIGNAL(windowAdded(WId)), this, SIGNAL(windowListChanged()));
connect(KWindowSystem::self(), SIGNAL(windowRemoved(WId)), this, SIGNAL(windowListChanged()));
}
@ -400,24 +400,6 @@ void DPAdds::setCurrentDesktop(const int desktop) const
}
void DPAdds::changeDesktop(const int desktop) const
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Desktop" << desktop;
emit(desktopChanged());
}
void DPAdds::changeWindowList(const WId window) const
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Window" << window;
emit(windowListChanged());
}
DPAdds::DesktopWindowsInfo DPAdds::getInfoByDesktop(const int desktop) const
{
if (debug) qDebug() << PDEBUG;

View File

@ -71,10 +71,6 @@ public slots:
Q_INVOKABLE static void sendNotification(const QString eventId, const QString message);
Q_INVOKABLE void setCurrentDesktop(const int desktop) const;
private slots:
void changeDesktop(const int desktop) const;
void changeWindowList(const WId window) const;
private:
DesktopWindowsInfo getInfoByDesktop(const int desktop) const;
QList<Plasma::Containment *> getPanels() const;

View File

@ -59,10 +59,10 @@ ExtendedSysMon::ExtendedSysMon(QObject* parent, const QVariantList &args)
setMinimumPollingInterval(333);
readConfiguration();
externalQuotes = new ExtItemAggregator<ExtQuotes>(nullptr, QString("quotes"), debug);
externalScripts = new ExtItemAggregator<ExtScript>(nullptr, QString("scripts"), debug);
externalUpgrade = new ExtItemAggregator<ExtUpgrade>(nullptr, QString("upgrade"), debug);
externalWeather = new ExtItemAggregator<ExtWeather>(nullptr, QString("weather"), debug);
extQuotes = new ExtItemAggregator<ExtQuotes>(nullptr, QString("quotes"), debug);
extScripts = new ExtItemAggregator<ExtScript>(nullptr, QString("scripts"), debug);
extUpgrade = new ExtItemAggregator<ExtUpgrade>(nullptr, QString("upgrade"), debug);
extWeather = new ExtItemAggregator<ExtWeather>(nullptr, QString("weather"), debug);
}
@ -70,10 +70,10 @@ ExtendedSysMon::~ExtendedSysMon()
{
if (debug) qDebug() << PDEBUG;
delete externalQuotes;
delete externalScripts;
delete externalUpgrade;
delete externalWeather;
delete extQuotes;
delete extScripts;
delete extUpgrade;
delete extWeather;
}
@ -579,7 +579,7 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source)
QVariantHash battery = getBattery(configuration[QString("ACPIPATH")]);
foreach(QString key, battery.keys()) setData(source, key, battery[key]);
} else if (source == QString("custom")) {
foreach(ExtScript *script, externalScripts->items()) {
foreach(ExtScript *script, extScripts->items()) {
QVariantHash data = script->run();
foreach(QString key, data.keys()) setData(source, key, data[key]);
}
@ -599,7 +599,7 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source)
} else if (source == QString("netdev")) {
setData(source, QString("value"), getNetworkDevice());
} else if (source == QString("pkg")) {
foreach(ExtUpgrade *upgrade, externalUpgrade->items()) {
foreach(ExtUpgrade *upgrade, extUpgrade->items()) {
QVariantHash data = upgrade->run();
foreach(QString key, data.keys()) setData(source, key, data[key]);
}
@ -613,14 +613,14 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source)
QVariantHash ps = getPsStats();
foreach(QString key, ps.keys()) setData(source, key, ps[key]);
} else if (source == QString("quotes")) {
foreach(ExtQuotes *quote, externalQuotes->items()) {
foreach(ExtQuotes *quote, extQuotes->items()) {
QVariantHash data = quote->run();
foreach(QString key, data.keys()) setData(source, key, data[key]);
}
} else if (source == QString("update")) {
setData(source, QString("value"), true);
} else if (source == QString("weather")) {
foreach(ExtWeather *weather, externalWeather->items()) {
foreach(ExtWeather *weather, extWeather->items()) {
QVariantHash data = weather->run();
foreach(QString key, data.keys()) setData(source, key, data[key]);
}

View File

@ -59,10 +59,10 @@ protected:
private:
// configuration
QHash<QString, QString> configuration;
ExtItemAggregator<ExtQuotes> *externalQuotes;
ExtItemAggregator<ExtScript> *externalScripts;
ExtItemAggregator<ExtUpgrade> *externalUpgrade;
ExtItemAggregator<ExtWeather> *externalWeather;
ExtItemAggregator<ExtQuotes> *extQuotes;
ExtItemAggregator<ExtScript> *extScripts;
ExtItemAggregator<ExtUpgrade> *extUpgrade;
ExtItemAggregator<ExtWeather> *extWeather;
bool debug;
// reread configuration
QStringList getAllHdd() const;