File: AutoSaveTimer.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (98 lines) | stat: -rw-r--r-- 2,278 bytes parent folder | download | duplicates (3)
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
#include "AutoSaveTimer.h"

#include "ipreferencesystem.h"
#include "iautosaver.h"
#include "registry/registry.h"
#include "i18n.h"

namespace map
{

namespace
{
    constexpr const char* const RKEY_AUTOSAVE_INTERVAL = "user/ui/map/autoSaveInterval";
    constexpr const char* const RKEY_AUTOSAVE_ENABLED = "user/ui/map/autoSaveEnabled";
}

AutoSaveTimer::AutoSaveTimer() :
    _enabled(false),
    _interval(5 * 60)
{}

AutoSaveTimer::~AutoSaveTimer()
{
    _enabled = false;
    stopTimer();

    // Destroy the timer
    _timer.reset();
}

void AutoSaveTimer::initialise()
{
    // Add a page to the given group
    IPreferencePage& page = GlobalPreferenceSystem().getPage(_("Settings/Autosave"));

    // Add the checkboxes and connect them with the registry key
    page.appendCheckBox(_("Enable Autosave"), RKEY_AUTOSAVE_ENABLED);
    page.appendSlider(_("Autosave Interval (in minutes)"), RKEY_AUTOSAVE_INTERVAL, 1, 61, 1, 1);

    _timer.reset(new wxTimer(this));

    Bind(wxEVT_TIMER, &AutoSaveTimer::onIntervalReached, this);

    GlobalRegistry().signalForKey(RKEY_AUTOSAVE_INTERVAL).connect(
        sigc::mem_fun(this, &AutoSaveTimer::registryKeyChanged)
    );
    GlobalRegistry().signalForKey(RKEY_AUTOSAVE_ENABLED).connect(
        sigc::mem_fun(this, &AutoSaveTimer::registryKeyChanged)
    );

    // Refresh all values from the registry right now (this might also start the timer)
    registryKeyChanged();
}

void AutoSaveTimer::registryKeyChanged()
{
    // Stop the current timer
    stopTimer();

    _enabled = registry::getValue<bool>(RKEY_AUTOSAVE_ENABLED);
    _interval = registry::getValue<int>(RKEY_AUTOSAVE_INTERVAL) * 60;

    // Start the timer with the new interval
    if (_enabled)
    {
        startTimer();
    }
}

void AutoSaveTimer::startTimer()
{
    if (!_timer) return;

    _timer->Start(static_cast<int>(_interval * 1000));
}

void AutoSaveTimer::stopTimer()
{
    if (!_timer) return;

    _timer->Stop();
}

void AutoSaveTimer::onIntervalReached(wxTimerEvent& ev)
{
    if (_enabled && GlobalAutoSaver().runAutosaveCheck())
    {
        // Stop the timer before saving
        stopTimer();

        GlobalAutoSaver().performAutosave();

        // Re-start the timer after saving has finished
        startTimer();
    }
}

}