From 8caa95de7f923eafc31520184e614128b7bb542a Mon Sep 17 00:00:00 2001
From: Michael Carlberg <c@rlberg.se>
Date: Wed, 22 Jun 2016 19:07:57 +0200
Subject: [PATCH] fix: Initialize token labels in constructor

---
 src/modules/battery.cpp | 25 ++++++++++++-------------
 src/modules/network.cpp | 12 ++++++------
 2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp
index 988d6f5d..fe5e3fc4 100644
--- a/src/modules/battery.cpp
+++ b/src/modules/battery.cpp
@@ -44,15 +44,21 @@ BatteryModule::BatteryModule(std::string name_) : InotifyModule(name_)
   if (this->formatter->has(TAG_RAMP_CAPACITY))
     this->ramp_capacity = drawtypes::get_config_ramp(
       name(), get_tag_name(TAG_RAMP_CAPACITY));
-  if (this->formatter->has(TAG_LABEL_CHARGING, FORMAT_CHARGING))
+  if (this->formatter->has(TAG_LABEL_CHARGING, FORMAT_CHARGING)) {
     this->label_charging = drawtypes::get_optional_config_label(
-      name(), get_tag_name(TAG_LABEL_CHARGING), "%percentage%");
-  if (this->formatter->has(TAG_LABEL_DISCHARGING, FORMAT_DISCHARGING))
+        name(), get_tag_name(TAG_LABEL_CHARGING), "%percentage%");
+    this->label_charging_tokenized = this->label_charging->clone();
+  }
+  if (this->formatter->has(TAG_LABEL_DISCHARGING, FORMAT_DISCHARGING)) {
     this->label_discharging = drawtypes::get_optional_config_label(
-      name(), get_tag_name(TAG_LABEL_DISCHARGING), "%percentage%");
-  if (this->formatter->has(TAG_LABEL_FULL, FORMAT_FULL))
+        name(), get_tag_name(TAG_LABEL_DISCHARGING), "%percentage%");
+    this->label_discharging_tokenized = this->label_discharging->clone();
+  }
+  if (this->formatter->has(TAG_LABEL_FULL, FORMAT_FULL)) {
     this->label_full = drawtypes::get_optional_config_label(
-      name(), get_tag_name(TAG_LABEL_FULL), "%percentage%");
+        name(), get_tag_name(TAG_LABEL_FULL), "%percentage%");
+    this->label_full_tokenized = this->label_full->clone();
+  }
 
   this->path_capacity = string::replace(PATH_BATTERY_CAPACITY, "%battery%", this->battery);
   this->path_adapter = string::replace(PATH_ADAPTER_STATUS, "%adapter%", this->adapter);
@@ -142,13 +148,6 @@ bool BatteryModule::on_event(InotifyEvent *event)
   if (this->state == state && this->percentage == percentage)
     return false;
 
-  if (!this->label_charging_tokenized)
-    this->label_charging_tokenized = this->label_charging->clone();
-  if (!this->label_discharging_tokenized)
-    this->label_discharging_tokenized = this->label_discharging->clone();
-  if (!this->label_full_tokenized)
-    this->label_full_tokenized = this->label_full->clone();
-
   this->label_charging_tokenized->text = this->label_charging->text;
   this->label_charging_tokenized->replace_token("%percentage%", IntToStr(percentage) + "%");
 
diff --git a/src/modules/network.cpp b/src/modules/network.cpp
index aaeed702..6cbd2302 100644
--- a/src/modules/network.cpp
+++ b/src/modules/network.cpp
@@ -35,8 +35,10 @@ NetworkModule::NetworkModule(std::string name_) : TimerModule(name_, 1s)
   // Create elements for format-connected
   if (this->formatter->has(TAG_RAMP_SIGNAL, FORMAT_CONNECTED))
     this->ramp_signal = drawtypes::get_config_ramp(name(), get_tag_name(TAG_RAMP_SIGNAL));
-  if (this->formatter->has(TAG_LABEL_CONNECTED, FORMAT_CONNECTED))
+  if (this->formatter->has(TAG_LABEL_CONNECTED, FORMAT_CONNECTED)) {
     this->label_connected = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_CONNECTED), DEFAULT_LABEL_CONNECTED);
+    this->label_connected_tokenized = this->label_connected->clone();
+  }
 
   // Create elements for format-disconnected
   if (this->formatter->has(TAG_LABEL_DISCONNECTED, FORMAT_DISCONNECTED)) {
@@ -48,8 +50,10 @@ NetworkModule::NetworkModule(std::string name_) : TimerModule(name_, 1s)
   if (this->ping_nth_update > 0) {
     this->formatter->add(FORMAT_PACKETLOSS, DEFAULT_FORMAT_PACKETLOSS, { TAG_ANIMATION_PACKETLOSS, TAG_LABEL_PACKETLOSS, TAG_LABEL_CONNECTED });
 
-    if (this->formatter->has(TAG_LABEL_PACKETLOSS, FORMAT_PACKETLOSS))
+    if (this->formatter->has(TAG_LABEL_PACKETLOSS, FORMAT_PACKETLOSS)) {
       this->label_packetloss = drawtypes::get_optional_config_label(name(), get_tag_name(TAG_LABEL_PACKETLOSS), DEFAULT_LABEL_PACKETLOSS);
+      this->label_packetloss_tokenized = this->label_packetloss->clone();
+    }
     if (this->formatter->has(TAG_ANIMATION_PACKETLOSS, FORMAT_PACKETLOSS))
       this->animation_packetloss = drawtypes::get_config_animation(name(), get_tag_name(TAG_ANIMATION_PACKETLOSS));
   }
@@ -151,16 +155,12 @@ bool NetworkModule::update()
     };
 
     if (this->label_connected) {
-      if (!this->label_connected_tokenized)
-        this->label_connected_tokenized = this->label_connected->clone();
       this->label_connected_tokenized->text = this->label_connected->text;
 
       replace_tokens(this->label_connected_tokenized);
     }
 
     if (this->label_packetloss) {
-      if (!this->label_packetloss_tokenized)
-        this->label_packetloss_tokenized = this->label_packetloss->clone();
       this->label_packetloss_tokenized->text = this->label_packetloss->text;
 
       replace_tokens(this->label_packetloss_tokenized);