summaryrefslogtreecommitdiffstats
blob: 2ca77e7ba21950b70305c44591da82161d3717ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2012 Maciej Niedzielski
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2014-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/Controllers/Highlighting/HighlightManager.h>

#include <cassert>
#include <sstream>

#include <boost/algorithm/string.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/regex.hpp>
#include <boost/serialization/vector.hpp>

#include <Swiften/Base/Log.h>

#include <Swift/Controllers/Highlighting/HighlightConfiguration.h>
#include <Swift/Controllers/Highlighting/Highlighter.h>
#include <Swift/Controllers/SettingConstants.h>
#include <Swift/Controllers/Settings/SettingsProvider.h>

namespace Swift {

HighlightManager::HighlightManager(SettingsProvider* settings)
    : settings_(settings)
    , storingSettings_(false) {
    highlightConfiguration_ = std::make_shared<HighlightConfiguration>();
    loadSettings();
    handleSettingChangedConnection_ = settings_->onSettingChanged.connect(boost::bind(&HighlightManager::handleSettingChanged, this, _1));
}

void HighlightManager::handleSettingChanged(const std::string& settingPath) {
    if (!storingSettings_ && SettingConstants::HIGHLIGHT_RULES.getKey() == settingPath) {
        loadSettings();
    }
}

HighlightConfiguration HighlightManager::getDefaultConfig() {
    HighlightConfiguration defaultConfiguration;
    defaultConfiguration.playSoundOnIncomingDirectMessages = true;
    defaultConfiguration.showNotificationOnIncomingDirectMessages = true;
    defaultConfiguration.ownMentionAction.setSoundFilePath(std::string("/sounds/message-received.wav"));
    defaultConfiguration.ownMentionAction.setSystemNotificationEnabled(true);
    return defaultConfiguration;
}

void HighlightManager::storeSettings() {
    storingSettings_ = true;    // don't reload settings while saving
    settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES_V2, highlightConfigurationToString(*highlightConfiguration_));
    storingSettings_ = false;
}

void HighlightManager::loadSettings() {
    std::string configString = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES_V2);
    *highlightConfiguration_ = highlightConfigurationFromString(configString);
}

Highlighter* HighlightManager::createHighlighter(NickResolver* nickResolver) {
    return new Highlighter(this, nickResolver);
}

void HighlightManager::resetToDefaultConfiguration() {
    *highlightConfiguration_ = getDefaultConfig();
}

HighlightConfiguration HighlightManager::highlightConfigurationFromString(const std::string& dataString) {
    std::stringstream stream;
    stream << dataString;

    HighlightConfiguration configuration;
    try {
        boost::archive::text_iarchive archive(stream);
        archive >> configuration;
    }
    catch (boost::archive::archive_exception&) {
        configuration = getDefaultConfig();
        SWIFT_LOG(warning) << "Failed to load highlight configuration. Will use default configuration instead." << std::endl;
    }
    return configuration;
}

std::string HighlightManager::highlightConfigurationToString(const HighlightConfiguration& configuration) {
    std::stringstream stream;
    boost::archive::text_oarchive archive(stream);
    archive & configuration;
    return stream.str();
}

}