forked from adiesner/GarminPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigManager.cpp
More file actions
172 lines (145 loc) · 5.21 KB
/
configManager.cpp
File metadata and controls
172 lines (145 loc) · 5.21 KB
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* GarminPlugin
* Copyright (C) Andreas Diesner 2010 <andreas.diesner [AT] gmx [DOT] de>
*
* GarminPlugin is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarminPlugin is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "configManager.h"
#include <stdlib.h>
#include <sys/stat.h>
#include "log.h"
ConfigManager::ConfigManager() : configuration(NULL), createdNew(false) {
}
ConfigManager::~ConfigManager() {
Log::dbg("ConfigManager destructor");
if (this->configuration != NULL) {
delete this->configuration;
}
}
void ConfigManager::readConfiguration() {
string homeDir = getenv ("HOME");
this->configurationFile = homeDir + "/.config/garminplugin/garminplugin.xml";
try
{
this->configuration = new TiXmlDocument(this->configurationFile );
if (this->configuration->LoadFile())
{
return;
}
}
catch(ticpp::Exception& ex)
{
if (Log::enabledInfo()) Log::info("Failed reading configuration from "+this->configurationFile);
}
this->configurationFile = homeDir + "/.garminplugin.xml";
try
{
this->configuration = new TiXmlDocument(this->configurationFile );
if (this->configuration->LoadFile())
{
return;
}
}
catch(ticpp::Exception& ex)
{
if (Log::enabledInfo()) Log::info("Failed reading configuration from "+this->configurationFile);
}
configuration = createNewConfiguration();
}
TiXmlDocument * ConfigManager::getConfiguration() {
return this->configuration;
}
TiXmlDocument * ConfigManager::createNewConfiguration() {
if (Log::enabledDbg()) Log::dbg("Creating new initial configuration");
createdNew = true;
string homeDir = getenv ("HOME");
string storagePath = homeDir + "/.config";
struct stat st;
if(stat(storagePath.c_str(),&st) == 0) {
// directory exists
storagePath += "/garminplugin";
if(stat(storagePath.c_str(),&st) == 0) {
// directory already exists
storagePath += "/";
} else {
if(mkdir(storagePath.c_str(), 0755) == -1)
{
if (Log::enabledErr()) Log::err("Failed to create directory "+storagePath);
storagePath = homeDir+"/.";
} else {
storagePath += "/";
}
}
} else {
storagePath = homeDir+"/.";
}
string configFile = storagePath + "garminplugin.xml";
TiXmlDocument * doc = new TiXmlDocument();
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "no" );
doc->LinkEndChild( decl );
/* <GarminPlugin logfile="" level="ERROR">
<Devices>
<Device>
<Name>My Oregon 300</Name>
<StoragePath>/tmp</StoragePath>
<StorageCommand></StorageCommand>
<FitnessDataPath useDriver="false"></FitnessDataPath>
</Device>
</Devices>
</GarminPlugin> */
TiXmlElement * plugin = new TiXmlElement( "GarminPlugin" );
plugin->SetAttribute("logfile", "");
plugin->SetAttribute("level", "ERROR");
doc->LinkEndChild( plugin );
TiXmlElement * devices = new TiXmlElement( "Devices" );
plugin->LinkEndChild( devices );
TiXmlElement * device = new TiXmlElement( "Device" );
devices->LinkEndChild( device );
TiXmlElement * name = new TiXmlElement( "Name" );
name->LinkEndChild(new TiXmlText("Home Directory "+homeDir));
device->LinkEndChild( name );
TiXmlElement * storePath = new TiXmlElement( "StoragePath" );
storePath->LinkEndChild(new TiXmlText(homeDir));
device->LinkEndChild( storePath );
TiXmlElement * storageCmd = new TiXmlElement( "StorageCommand" );
storageCmd->LinkEndChild(new TiXmlText(""));
device->LinkEndChild( storageCmd );
TiXmlElement * fitnessPath = new TiXmlElement( "FitnessDataPath" );
fitnessPath->LinkEndChild(new TiXmlText(""));
device->LinkEndChild( fitnessPath );
/*
<Settings>
<ForerunnerTools enabled ="true"/>
</Settings>
*/
TiXmlElement * settings = new TiXmlElement( "Settings" );
plugin->LinkEndChild( settings );
TiXmlElement * forerunnertools = new TiXmlElement( "ForerunnerTools" );
settings->LinkEndChild( forerunnertools );
forerunnertools->SetAttribute("enabled", "true");
try {
doc->SaveFile(configFile);
configurationFile = configFile;
}
catch(ticpp::Exception& ex)
{
if (Log::enabledErr()) Log::err("Failed storing initial configuration to "+configFile);
}
return doc;
}
MessageBox * ConfigManager::getMessage() {
if (!this->createdNew) return NULL;
return new MessageBox(Question, "A new configuration was created at "+this->configurationFile, BUTTON_OK, BUTTON_OK, NULL);
}