diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 25441c041..3e04b2904 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -196,57 +196,9 @@ public: QStringSetting cachePath = {"/cache/path", ""}; /// Timeout buttons - QStringSetting timeoutDurationPerUnit1 = { - "/timeouts/timeoutDurationPerUnit1", "1"}; - QStringSetting timeoutDurationUnit1 = {"/timeouts/timeoutDurationUnit1", - "s"}; - IntSetting timeoutDurationInSec1 = {"/timeouts/timeoutDurationInSec1", 1}; - QStringSetting timeoutDurationPerUnit2 = { - "/timeouts/timeoutDurationPerUnit2", "30"}; - QStringSetting timeoutDurationUnit2 = {"/timeouts/timeoutDurationUnit2", - "s"}; - IntSetting timeoutDurationInSec2 = {"/timeouts/timeoutDurationInSec2", 30}; - - QStringSetting timeoutDurationPerUnit3 = { - "/timeouts/timeoutDurationPerUnit3", "1"}; - QStringSetting timeoutDurationUnit3 = {"/timeouts/timeoutDurationUnit3", - "m"}; - IntSetting timeoutDurationInSec3 = {"/timeouts/timeoutDurationInSec3", 60}; - - QStringSetting timeoutDurationPerUnit4 = { - "/timeouts/timeoutDurationPerUnit4", "5"}; - QStringSetting timeoutDurationUnit4 = {"/timeouts/timeoutDurationUnit4", - "s"}; - IntSetting timeoutDurationInSec4 = {"/timeouts/timeoutDurationInSec4", 300}; - - QStringSetting timeoutDurationPerUnit5 = { - "/timeouts/timeoutDurationPerUnit5", "30"}; - QStringSetting timeoutDurationUnit5 = {"/timeouts/timeoutDurationUnit5", - "m"}; - IntSetting timeoutDurationInSec5 = {"/timeouts/timeoutDurationInSec5", - 1800}; - - QStringSetting timeoutDurationPerUnit6 = { - "/timeouts/timeoutDurationPerUnit6", "1"}; - QStringSetting timeoutDurationUnit6 = {"/timeouts/timeoutDurationUnit6", - "h"}; - IntSetting timeoutDurationInSec6 = {"/timeouts/timeoutDurationInSec6", - 3600}; - - QStringSetting timeoutDurationPerUnit7 = { - "/timeouts/timeoutDurationPerUnit7", "1"}; - QStringSetting timeoutDurationUnit7 = {"/timeouts/timeoutDurationUnit7", - "d"}; - IntSetting timeoutDurationInSec7 = {"/timeouts/timeoutDurationInSec7", - 86400}; - - QStringSetting timeoutDurationPerUnit8 = { - "/timeouts/timeoutDurationPerUnit8", "1"}; - QStringSetting timeoutDurationUnit8 = {"/timeouts/timeoutDurationUnit8", - "w"}; - IntSetting timeoutDurationInSec8 = {"/timeouts/timeoutDurationInSec8", - 604800}; + ChatterinoSetting> timeoutDurationsPerUnit = { "/timeouts/timeoutDurationsPerUnit", { "1", "30", "1", "5", "30", "1", "1", "1" }}; + ChatterinoSetting> timeoutDurationUnits = { "/timeouts/timeoutDurationUnits", { "s", "s", "m", "m", "m", "h", "d", "w" }}; private: void updateModerationActions(); diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 9fd1671ce..cc461d993 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -539,38 +539,35 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget() addButton(Unban, "unban", getApp()->resources->buttons.unban); - addTimeouts("Timeouts", - {{getSettings()->timeoutDurationPerUnit1.getValue() + - getSettings()->timeoutDurationUnit1.getValue(), - getSettings()->timeoutDurationInSec1.getValue()}, + std::vector durationsPerUnit = + getSettings()->timeoutDurationsPerUnit; - {getSettings()->timeoutDurationPerUnit2.getValue() + - getSettings()->timeoutDurationUnit2.getValue(), - getSettings()->timeoutDurationInSec2.getValue()}, + std::vector durationUnits = getSettings()->timeoutDurationUnits; - {getSettings()->timeoutDurationPerUnit3.getValue() + - getSettings()->timeoutDurationUnit3.getValue(), - getSettings()->timeoutDurationInSec3.getValue()}, + addTimeouts( + "Timeouts", + {{durationsPerUnit[0] + durationUnits[0], + calculateTimeoutDuration(durationsPerUnit[0], durationUnits[0])}, - {getSettings()->timeoutDurationPerUnit4.getValue() + - getSettings()->timeoutDurationUnit4.getValue(), - getSettings()->timeoutDurationInSec4.getValue()}, + {durationsPerUnit[1] + durationUnits[1], + calculateTimeoutDuration(durationsPerUnit[1], durationUnits[1])}, - {getSettings()->timeoutDurationPerUnit5.getValue() + - getSettings()->timeoutDurationUnit5.getValue(), - getSettings()->timeoutDurationInSec5.getValue()}, + {durationsPerUnit[2] + durationUnits[2], + calculateTimeoutDuration(durationsPerUnit[2], durationUnits[2])}, - {getSettings()->timeoutDurationPerUnit6.getValue() + - getSettings()->timeoutDurationUnit6.getValue(), - getSettings()->timeoutDurationInSec6.getValue()}, + {durationsPerUnit[3] + durationUnits[3], + calculateTimeoutDuration(durationsPerUnit[3], durationUnits[3])}, - {getSettings()->timeoutDurationPerUnit7.getValue() + - getSettings()->timeoutDurationUnit7.getValue(), - getSettings()->timeoutDurationInSec7.getValue()}, + {durationsPerUnit[4] + durationUnits[4], + calculateTimeoutDuration(durationsPerUnit[4], durationUnits[4])}, - {getSettings()->timeoutDurationPerUnit8.getValue() + - getSettings()->timeoutDurationUnit8.getValue(), - getSettings()->timeoutDurationInSec8.getValue()}}); + {durationsPerUnit[5] + durationUnits[5], + calculateTimeoutDuration(durationsPerUnit[5], durationUnits[5])}, + + {durationsPerUnit[6] + durationUnits[6], + calculateTimeoutDuration(durationsPerUnit[6], durationUnits[6])}, + {durationsPerUnit[7] + durationUnits[7], + calculateTimeoutDuration(durationsPerUnit[7], durationUnits[7])}}); addButton(Ban, "ban", getApp()->resources->buttons.ban); } @@ -587,13 +584,15 @@ void UserInfoPopup::TimeoutWidget::paintEvent(QPaintEvent *) void UserInfoPopup::fillLatestMessages() { - LimitedQueueSnapshot snapshot = this->channel_->getMessageSnapshot(); + LimitedQueueSnapshot snapshot = + this->channel_->getMessageSnapshot(); ChannelPtr channelPtr(new Channel("search", Channel::Type::None)); for (size_t i = 0; i < snapshot.size(); i++) { MessagePtr message = snapshot[i]; - if (message->loginName.compare(this->userName_, Qt::CaseInsensitive) == 0 - && !message->flags.has(MessageFlag::Whisper)) + if (message->loginName.compare(this->userName_, Qt::CaseInsensitive) == + 0 && + !message->flags.has(MessageFlag::Whisper)) { channelPtr->addMessage(message); } @@ -602,4 +601,34 @@ void UserInfoPopup::fillLatestMessages() this->latestMessages_->setChannel(channelPtr); } +int UserInfoPopup::calculateTimeoutDuration(const QString &durationPerUnit, + const QString &unit) +{ + int valueInUnit = durationPerUnit.toInt(); + int valueInSec = 0; + + if (unit == "s") + { + valueInSec = valueInUnit; + } + else if (unit == "m") + { + valueInSec = valueInUnit * 60; + } + else if (unit == "h") + { + valueInSec = valueInUnit * 60 * 60; + } + else if (unit == "d") + { + valueInSec = valueInUnit * 24 * 60 * 60; + } + else if (unit == "w") + { + valueInSec = valueInUnit * 7 * 24 * 60 * 60; + } + + return valueInSec; +} + } // namespace chatterino diff --git a/src/widgets/dialogs/UserInfoPopup.hpp b/src/widgets/dialogs/UserInfoPopup.hpp index 4a07f2f9d..68e6a26a2 100644 --- a/src/widgets/dialogs/UserInfoPopup.hpp +++ b/src/widgets/dialogs/UserInfoPopup.hpp @@ -34,6 +34,8 @@ private: bool isMod_; bool isBroadcaster_; + static int calculateTimeoutDuration(const QString &durationPerUnit, const QString &unit); + QString userName_; QString userId_; ChannelPtr channel_; diff --git a/src/widgets/settingspages/AdvancedPage.cpp b/src/widgets/settingspages/AdvancedPage.cpp index 98f36f20f..414089a2c 100644 --- a/src/widgets/settingspages/AdvancedPage.cpp +++ b/src/widgets/settingspages/AdvancedPage.cpp @@ -81,41 +81,15 @@ AdvancedPage::AdvancedPage() units.append("d"); units.append("w"); - QStringList initDurationsPerUnit; - initDurationsPerUnit = QStringList(); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit1)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit2)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit3)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit4)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit5)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit6)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit7)); - initDurationsPerUnit.append( - QString(getSettings()->timeoutDurationPerUnit8)); + std::vector durationsPerUnit = + getSettings()->timeoutDurationsPerUnit; + std::vector::iterator itDurationPerUnit; + itDurationPerUnit = durationsPerUnit.begin(); - QStringList::iterator itDurationPerUnit; - itDurationPerUnit = initDurationsPerUnit.begin(); - - QStringList initUnits; - initUnits = QStringList(); - initUnits.append(QString(getSettings()->timeoutDurationUnit1)); - initUnits.append(QString(getSettings()->timeoutDurationUnit2)); - initUnits.append(QString(getSettings()->timeoutDurationUnit3)); - initUnits.append(QString(getSettings()->timeoutDurationUnit4)); - initUnits.append(QString(getSettings()->timeoutDurationUnit5)); - initUnits.append(QString(getSettings()->timeoutDurationUnit6)); - initUnits.append(QString(getSettings()->timeoutDurationUnit7)); - initUnits.append(QString(getSettings()->timeoutDurationUnit8)); - - QStringList::iterator itUnit; - itUnit = initUnits.begin(); + std::vector durationUnits = + getSettings()->timeoutDurationUnits; + std::vector::iterator itUnit; + itUnit = durationUnits.begin(); for (int i = 0; i < 8; i++) { @@ -172,7 +146,6 @@ AdvancedPage::AdvancedPage() &QComboBox::currentTextChanged, this, &AdvancedPage::timeoutUnitChanged); - auto secondsLabel = timeout.emplace(); timeout->addStretch(); } timeout->setContentsMargins(40, 0, 0, 0); @@ -201,28 +174,15 @@ void AdvancedPage::timeoutDurationChanged(const QString &newDuration) QString unit = cbUnit->currentText(); int valueInUnit = newDuration.toInt(); - int valueInSec; - if (unit == "s") - { - valueInSec = valueInUnit; - } - else if (unit == "m") - { - valueInSec = valueInUnit * 60; - } - else if (unit == "h") - { - valueInSec = valueInUnit * 60 * 60; - } - else if (unit == "d") + // safety mechanism for setting days and weeks + if (unit == "d") { if (valueInUnit > 14) { durationPerUnit->setText("14"); return; } - valueInSec = valueInUnit * 24 * 60 * 60; } else if (unit == "w") { @@ -231,44 +191,12 @@ void AdvancedPage::timeoutDurationChanged(const QString &newDuration) durationPerUnit->setText("2"); return; } - valueInSec = valueInUnit * 7 * 24 * 60 * 60; } - switch (index) - { - case 0: - getSettings()->timeoutDurationPerUnit1 = newDuration; - getSettings()->timeoutDurationInSec1 = valueInSec; - break; - case 1: - getSettings()->timeoutDurationPerUnit2 = newDuration; - getSettings()->timeoutDurationInSec2 = valueInSec; - break; - case 2: - getSettings()->timeoutDurationPerUnit3 = newDuration; - getSettings()->timeoutDurationInSec3 = valueInSec; - break; - case 3: - getSettings()->timeoutDurationPerUnit4 = newDuration; - getSettings()->timeoutDurationInSec4 = valueInSec; - break; - case 4: - getSettings()->timeoutDurationPerUnit5 = newDuration; - getSettings()->timeoutDurationInSec5 = valueInSec; - break; - case 5: - getSettings()->timeoutDurationPerUnit6 = newDuration; - getSettings()->timeoutDurationInSec6 = valueInSec; - break; - case 6: - getSettings()->timeoutDurationPerUnit7 = newDuration; - getSettings()->timeoutDurationInSec7 = valueInSec; - break; - case 7: - getSettings()->timeoutDurationPerUnit8 = newDuration; - getSettings()->timeoutDurationInSec8 = valueInSec; - break; - } + std::vector durationsPerUnit = + getSettings()->timeoutDurationsPerUnit; + durationsPerUnit[index] = newDuration; + getSettings()->timeoutDurationsPerUnit = durationsPerUnit; } void AdvancedPage::timeoutUnitChanged(const QString &newUnit) @@ -276,38 +204,15 @@ void AdvancedPage::timeoutUnitChanged(const QString &newUnit) QObject *sender = QObject::sender(); int index = sender->objectName().toInt(); - switch (index) - { - case 0: - getSettings()->timeoutDurationUnit1 = newUnit; - break; - case 1: - getSettings()->timeoutDurationUnit2 = newUnit; - break; - case 2: - getSettings()->timeoutDurationUnit3 = newUnit; - break; - case 3: - getSettings()->timeoutDurationUnit4 = newUnit; - break; - case 4: - getSettings()->timeoutDurationUnit5 = newUnit; - break; - case 5: - getSettings()->timeoutDurationUnit6 = newUnit; - break; - case 6: - getSettings()->timeoutDurationUnit7 = newUnit; - break; - case 7: - getSettings()->timeoutDurationUnit8 = newUnit; - break; - } - itDurationInput = durationInputs.begin() + index; QLineEdit *durationPerUnit = *itDurationInput; + // safety mechanism for changing units (i.e. to days or weeks) AdvancedPage::timeoutDurationChanged(durationPerUnit->text()); + + std::vector durationUnits = getSettings()->timeoutDurationUnits; + durationUnits[index] = newUnit; + getSettings()->timeoutDurationUnits = durationUnits; } } // namespace chatterino