Android release v6.7_preview1
[xcsoar.git] / src / StatusMessage.cpp
blobb1556f9045cd4430c8d6a3e0cb3d10ffd2aae14d
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "StatusMessage.hpp"
25 #include "Profile/Profile.hpp"
26 #include "LocalPath.hpp"
27 #include "Util/EscapeBackslash.hpp"
28 #include "Util/StringUtil.hpp"
29 #include "Util/NumberParser.hpp"
30 #include "IO/ConfiguredFile.hpp"
32 #include <stdio.h>
33 #include <memory>
35 static constexpr StatusMessage default_status_messages[] = {
36 #include "Status_defaults.cpp"
37 { NULL }
40 StatusMessageList::StatusMessageList()
41 :old_delay(2000)
43 // DEFAULT - 0 is loaded as default, and assumed to exist
44 StatusMessage &first = list.append();
45 first.key = _T("DEFAULT");
46 first.visible = true;
47 first.sound = _T("IDR_WAV_DRIP");
48 first.delay_ms = 2500; // 2.5 s
50 // Load up other defaults - allow overwrite in config file
51 const StatusMessage *src = &default_status_messages[0];
52 while (src->key != NULL)
53 list.append(*src++);
56 void
57 StatusMessageList::LoadFile()
59 std::unique_ptr<TLineReader> reader(OpenConfiguredTextFile(ProfileKeys::StatusFile));
60 if (reader)
61 LoadFile(*reader);
64 static bool
65 parse_assignment(TCHAR *buffer, const TCHAR *&key, const TCHAR *&value)
67 TCHAR *separator = _tcschr(buffer, '=');
68 if (separator == NULL || separator == buffer)
69 return false;
71 *separator = _T('\0');
73 key = buffer;
74 value = separator + 1;
76 return true;
79 void
80 StatusMessageList::LoadFile(TLineReader &reader)
82 // Init first entry
83 StatusMessage current;
84 current.Clear();
86 /* Read from the file */
87 TCHAR *buffer;
88 const TCHAR *key, *value;
89 while ((buffer = reader.ReadLine()) != NULL) {
90 // Check valid line? If not valid, assume next record (primative, but works ok!)
91 if (*buffer == _T('#') || !parse_assignment(buffer, key, value)) {
92 // Global counter (only if the last entry had some data)
93 if (!current.IsEmpty()) {
94 list.append(current);
95 current.Clear();
97 if (list.full())
98 break;
100 } else {
101 if (_tcscmp(key, _T("key")) == 0) {
102 if (current.key == NULL)
103 current.key = UnescapeBackslash(value);
104 } else if (_tcscmp(key, _T("sound")) == 0) {
105 if (current.sound == NULL)
106 current.sound = UnescapeBackslash(value);
107 } else if (_tcscmp(key, _T("delay")) == 0) {
108 TCHAR *endptr;
109 unsigned ms = ParseUnsigned(value, &endptr);
110 if (endptr > value)
111 current.delay_ms = ms;
112 } else if (_tcscmp(key, _T("hide")) == 0) {
113 if (_tcscmp(value, _T("yes")) == 0)
114 current.visible = false;
119 if (!current.IsEmpty())
120 list.append(current);
123 void
124 StatusMessageList::Startup(bool first)
126 if (first) {
127 // NOTE: Must show errors AFTER all windows ready
128 old_delay = list[0].delay_ms;
129 list[0].delay_ms = 20000; // 20 seconds
130 } else {
131 list[0].delay_ms = old_delay;
135 const StatusMessage *
136 StatusMessageList::Find(const TCHAR *key) const
138 for (int i = list.size() - 1; i > 0; i--)
139 if (_tcscmp(key, list[i].key) == 0)
140 return &list[i];
142 return NULL;