2 ******************************************************************************
4 * @file uavconfiginfo.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup CorePlugin Core Plugin
10 * @brief The Core GCS plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 \class Core::UAVConfigInfo
31 \brief The Config Info is a helper-class to handle version changes in GCS
34 The UAVConfigInfo provides version-information for the configuration-data
35 and callback functions to ask the user how to handle incompatble
36 (old or newer) configurations.
38 When the config is created from a \l{QSettings} instance, an UAVConfigInfo
39 object is passed to the factory-method. With the version-data it can decide whether
40 the presented config-data is compatible to the current implementation. It may
41 migrate old data to the current format or abort the import.
43 When the config is written to the \l{QSettings} instance, an UAVConfigInfo object
44 is passed to the writer-function. The version of the config-format should
45 be written to the UAVConfigInfo object. This version will be passed to
46 factory-method when creating the config-object from this configuration.
49 Typically a plugin can handle version-changes like this:
51 MyGadgetConfiguration::MyGadgetConfiguration(QString classId, QSettings* qSettings, UAVConfigInfo *configInfo, QObject *parent) :
52 IUAVGadgetConfiguration(classId, parent)
57 if ( configInfo->version() == UAVConfigVersion() )
58 configInfo->setVersion("1.0.0");
60 if ( !configInfo->standardVersionHandlingOK(CURRENT_VERSION))
63 ... read the config ...
66 void MyGadgetConfiguration::saveConfig(QSettings* qSettings, Core::UAVConfigInfo *configInfo) const {
68 configInfo->setVersion(CURRENT_VERSION);
70 ... write the config ...
76 \section1 Version Conventions
78 The Version numbers are in the form "major.minor.patch" (e.g. "3.1.4") with the
81 \o major: Differences in this number indicate completely incompatible formats. The
82 config can't be imported.
83 \o minor: Differences in this number indicate backwards compatible formats. Old
84 configs can be imported or will be automatically migrated by the new program
85 but configs written by this plugin can't be reasonably read by old versions of
87 \o patch: Differences in this number indicate backwards and forward compatible formats.
88 Configs written by this plugin can be read by old versions of the plugin. Old configs
89 are extended by defaults by the new plugin.
92 All parts (major, minor, patch) must be numeric values.
94 \section1 Utility Functions
96 \fn bool UAVConfigInfo::standardVersionHandlingOK(UAVConfigVersion programVersion)
97 \brief Default version handling.
99 With this function the plugin can test compatiblility of the current version
100 with the imported version. If there are differences, the user is asked whether
101 he or she wants to import the settings or abort the import.
103 Returns true when the import should be done, false otherwise.
107 #include "uavconfiginfo.h"
108 #include <QMessageBox>
110 #define VERSION_DEFAULT "0.0.0"
112 using namespace Core
;
114 UAVConfigInfo::UAVConfigInfo(QObject
*parent
) :
116 m_version(VERSION_DEFAULT
),
118 m_nameOfConfigurable("")
121 UAVConfigInfo::UAVConfigInfo(QSettings
*qs
, QObject
*parent
) :
123 m_version(VERSION_DEFAULT
)
128 UAVConfigInfo::UAVConfigInfo(UAVConfigVersion version
, QString nameOfConfigurable
, QObject
*parent
) :
132 m_nameOfConfigurable(nameOfConfigurable
)
135 UAVConfigInfo::UAVConfigInfo(IUAVGadgetConfiguration
*config
, QObject
*parent
) :
138 m_locked
= config
->locked();
139 m_nameOfConfigurable
= config
->classId() + "-" + config
->name();
142 void UAVConfigInfo::save(QSettings
*qs
)
144 qs
->beginGroup("configInfo");
145 qs
->setValue("version", m_version
.toString());
146 qs
->setValue("locked", m_locked
);
150 void UAVConfigInfo::read(QSettings
*qs
)
152 qs
->beginGroup("configInfo");
153 m_version
= UAVConfigVersion(qs
->value("version", VERSION_DEFAULT
).toString());
154 m_locked
= qs
->value("locked", false).toBool();
158 bool UAVConfigInfo::askToAbort(int compat
, QString message
)
162 msgBox
.setInformativeText(tr("Do you want to continue the import?"));
163 msgBox
.setStandardButtons(QMessageBox::Ok
| QMessageBox::Cancel
);
165 int result
= QMessageBox::Abort
;
168 case FullyCompatible
:
171 case MinorLossOfConfiguration
:
172 msgBox
.setText(tr("INFO: ") + message
+ tr(" Some of the configured features might not be supported by your version of the plugin. You might want to upgrade the plugin."));
173 msgBox
.setDefaultButton(QMessageBox::Ok
);
174 result
= msgBox
.exec();
177 case MissingConfiguration
:
178 msgBox
.setText(tr("WARNING: ") + message
+ tr(" Some configuration is missing in the imported config and will be replaced by default settings."));
179 msgBox
.setDefaultButton(QMessageBox::Ok
);
180 result
= msgBox
.exec();
183 case MajorLossOfConfiguration
:
184 msgBox
.setText(tr("ERROR: ") + message
+ tr(" Major features can't be imported by your version of the plugin. You should upgrade the plugin to import these settings."));
185 msgBox
.setDefaultButton(QMessageBox::Cancel
);
186 result
= msgBox
.exec();
190 msgBox
.setText("ERROR: " + message
+ tr(" The imported settings are not compatible with this plugin and won't be imported!"));
191 msgBox
.setInformativeText(tr(""));
192 msgBox
.setStandardButtons(QMessageBox::Ok
);
197 msgBox
.setText("INTERNAL ERROR: " + message
+ tr(" Unknown compatibility level: %1").arg(compat
));
199 if (result
== QMessageBox::Ok
) {
206 void UAVConfigInfo::notify(QString message
)
210 msgBox
.setText(message
);
214 int UAVConfigInfo::checkCompatibilityWith(UAVConfigVersion programVersion
)
216 if (m_version
.majorNr
!= programVersion
.majorNr
) {
217 return NotCompatible
;
219 if (m_version
.minorNr
< programVersion
.minorNr
) {
220 return MissingConfiguration
;
222 if (m_version
.minorNr
> programVersion
.minorNr
) {
223 return MajorLossOfConfiguration
;
225 if (m_version
.patchNr
> programVersion
.patchNr
) {
226 return MinorLossOfConfiguration
;
229 return FullyCompatible
;
232 bool UAVConfigInfo::standardVersionHandlingOK(UAVConfigVersion programVersion
)
235 checkCompatibilityWith(programVersion
),
236 "(" + m_nameOfConfigurable
+ ")");
239 UAVConfigVersion::UAVConfigVersion(int majorNum
, int minorNum
, int patchNum
)
245 UAVConfigVersion::UAVConfigVersion(QString versionString
)
251 end
= versionString
.indexOf(".", begin
);
252 majorNr
= versionString
.mid(begin
, end
- begin
).toInt();
255 end
= versionString
.indexOf(".", begin
);
256 minorNr
= versionString
.mid(begin
, end
- begin
).toInt();
259 patchNr
= versionString
.mid(begin
).toInt();
262 QString
UAVConfigVersion::toString() const
264 return QString("%1.%2.%3").arg(majorNr
).arg(minorNr
).arg(patchNr
);
267 bool UAVConfigVersion::operator==(const UAVConfigVersion
&other
)
269 return toString() == other
.toString();