summaryrefslogtreecommitdiffstats
path: root/src/message-stack.cpp
blob: cebd5f23c6412571a54c04cd6d024904a1603926 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * MessageStack - manages stack of active status messages
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2004 MenTaLguY
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <glib.h>
#include <cstring>
#include "message-stack.h"

namespace Inkscape {

MessageStack::MessageStack()
: _messages(nullptr), _next_id(1)
{
}

MessageStack::~MessageStack()
{
    while (_messages) {
        _messages = _discard(_messages);
    }
}

MessageId MessageStack::push(MessageType type, gchar const *message) {
    return _push(type, 0, message);
}

MessageId MessageStack::pushF(MessageType type, gchar const *format, ...)
{
    va_list args;
    va_start(args, format);
    MessageId id=pushVF(type, format, args);
    va_end(args);
    return id;
}

MessageId MessageStack::pushVF(MessageType type, gchar const *format, va_list args)
{
    MessageId id;
    gchar *message=g_strdup_vprintf(format, args);
    id = push(type, message);
    g_free(message);
    return id;
}

void MessageStack::cancel(MessageId id) {
    Message **ref;
    for ( ref = &_messages ; *ref ; ref = &(*ref)->next ) {
        if ( (*ref)->id == id ) {
            *ref = _discard(*ref);
            _emitChanged();
            break;
        }
    }
}

MessageId MessageStack::flash(MessageType type, Glib::ustring const &message)
{
    MessageId id = flash( type, message.c_str() );
    return id;
}

MessageId MessageStack::flash(MessageType type, gchar const *message) {
    switch (type) {
    case INFORMATION_MESSAGE: // stay rather long so as to seem permanent, but eventually disappear
        return _push(type, 6000 + 80*strlen(message), message);
        break;
    case ERROR_MESSAGE: // pretty important stuff, but temporary
        return _push(type, 4000 + 60*strlen(message), message);
        break;
    case WARNING_MESSAGE: // a bit less important than error
        return _push(type, 2000 + 40*strlen(message), message);
        break;
    case IMMEDIATE_MESSAGE: // same length as normal, higher priority
        return _push(type, 1000 + 20*strlen(message), message);
        break;
    case NORMAL_MESSAGE: // something ephemeral
    default:
        return _push(type, 1000 + 20*strlen(message), message);
        break;
    }
}

MessageId MessageStack::flashF(MessageType type, gchar const *format, ...) {
    va_list args;
    va_start(args, format);
    MessageId id = flashVF(type, format, args);
    va_end(args);
    return id;
}

MessageId MessageStack::flashVF(MessageType type, gchar const *format, va_list args)
{
    gchar *message=g_strdup_vprintf(format, args);
    MessageId id = flash(type, message);
    g_free(message);
    return id;
}

MessageId MessageStack::_push(MessageType type, guint lifetime, gchar const *message)
{
    Message *m=new Message;
    MessageId id=_next_id++;

    m->stack = this;
    m->id = id;
    m->type = type;
    m->message = g_strdup(message);

    if (lifetime) {
        m->timeout_id = g_timeout_add(lifetime, &MessageStack::_timeout, m);
    } else {
        m->timeout_id = 0;
    }

    m->next = _messages;
    _messages = m;

    _emitChanged();

    return id;
}

MessageStack::Message *MessageStack::_discard(MessageStack::Message *m)
{
    Message *next=m->next;
    if (m->timeout_id) {
        g_source_remove(m->timeout_id);
        m->timeout_id = 0;
    }
    g_free(m->message);
    m->message = nullptr;
    m->stack = nullptr;
    delete m;
    return next;
}

void MessageStack::_emitChanged() {
    if (_messages) {
        _changed_signal.emit(_messages->type, _messages->message);
    } else {
        _changed_signal.emit(NORMAL_MESSAGE, nullptr);
    }
}

gboolean MessageStack::_timeout(gpointer data) {
    Message *m=reinterpret_cast<Message *>(data);
    m->timeout_id = 0;
    m->stack->cancel(m->id);
    return FALSE;
}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :