Update debian/control to point to garage.
[weightgraph] / weightgraph / settings.cpp
1 #include "settings.h"
2 #include <QDir>
3
4 Settings *Settings::singleton = new Settings();
5 QSettings Settings::s(Settings::settingsfile(), QSettings::IniFormat);
6 const QString Settings::weightUnitKey = "WeightUnit";
7 const QString Settings::goalWeightMinKey = "GoalWeightMin";
8 const QString Settings::goalWeightMaxKey = "GoalWeightMax";
9 const QString Settings::grabZoomKeysKey = "GrabZoomKeys";
10 const QString Settings::graphSettingsGroupSuffix = "Graph";
11 const QString Settings::goalWeightEnabledKey = "GoalWeightEnabled";
12 const QString Settings::weightIntervalModeKey = "WeightIntervalMode";
13 const QString Settings::weightIntervalMinKey = "WeightIntervalMin";
14 const QString Settings::weightIntervalMaxKey = "WeightIntervalMax";
15 const QString Settings::defaultTimeIntervalKey = "DefaultTimeInterval";
16
17 QString Settings::weightUnit()
18 {
19   if(!s.contains(weightUnitKey))
20     setWeightUnit("kg");
21   return s.value(weightUnitKey).toString();
22 }
23 void Settings::setWeightUnit(QString wu)
24 {
25   s.setValue(weightUnitKey, wu);
26 }
27 void Settings::setWeightUnitAndSync(QString wu)
28 {
29   setWeightUnit(wu);
30   sync();
31 }
32
33 double Settings::goalWeightMin()
34 {
35   if(!s.contains(goalWeightMinKey))
36     setGoalWeightMin(0.0);
37   return s.value(goalWeightMinKey).toDouble();
38 }
39 void Settings::setGoalWeightMin(double min)
40 {
41   s.setValue(goalWeightMinKey, min);
42 }
43 void Settings::setGoalWeightMinAndSync(double min)
44 {
45   setGoalWeightMin(min);
46   sync();
47 }
48
49 double Settings::goalWeightMax()
50 {
51   if(!s.contains(goalWeightMaxKey))
52     setGoalWeightMax(0.0);
53   return s.value(goalWeightMaxKey).toDouble();
54 }
55 void Settings::setGoalWeightMax(double max)
56 {
57   s.setValue(goalWeightMaxKey, max);
58 }
59 void Settings::setGoalWeightMaxAndSync(double max)
60 {
61   setGoalWeightMax(max);
62   sync();
63 }
64
65 bool Settings::grabZoomKeys()
66 {
67   if(!s.contains(grabZoomKeysKey))
68     setGrabZoomKeys(true);
69   return s.value(grabZoomKeysKey).toBool();
70 }
71 void Settings::setGrabZoomKeys(bool grab)
72 {
73   s.setValue(grabZoomKeysKey, grab);
74 }
75 void Settings::setGrabZoomKeysAndSync(bool grab)
76 {
77   setGrabZoomKeys(grab);
78   sync();
79 }
80
81 GraphSettings Settings::graphSettings(const QString &graphId)
82 {
83   GraphSettings ret;
84   bool changed = false;
85   s.beginGroup(graphId+graphSettingsGroupSuffix);
86
87   if (!s.contains(goalWeightEnabledKey)) {
88     s.setValue(goalWeightEnabledKey, false);
89     changed = true;
90   }
91   ret.goalWeightEnabled = s.value(goalWeightEnabledKey).toBool();
92
93   if (!s.contains(weightIntervalModeKey)) {
94     s.setValue(weightIntervalModeKey, (int)GraphSettings::AutomaticWithoutGoalWeight);
95     changed = true;
96   }
97   ret.weightIntervalMode =
98       (GraphSettings::WeightIntervalMode)s.value(weightIntervalModeKey).toInt();
99
100   if (!s.contains(weightIntervalMinKey)) {
101     s.setValue(weightIntervalMinKey, 0.0);
102     changed = true;
103   }
104   ret.weightIntervalMin = s.value(weightIntervalMinKey).toDouble();
105
106   if (!s.contains(weightIntervalMaxKey)) {
107     s.setValue(weightIntervalMaxKey, 0.0);
108     changed = true;
109   }
110   ret.weightIntervalMax = s.value(weightIntervalMaxKey).toDouble();
111
112   if (!s.contains(defaultTimeIntervalKey)) {
113     s.setValue(defaultTimeIntervalKey, 0);
114     changed = true;
115   }
116   ret.defaultTimeInterval = s.value(defaultTimeIntervalKey).toInt();
117
118   s.endGroup();
119   if (changed)
120     sync();
121   return ret;
122 }
123 void Settings::setGraphSettings(const QString &graphId, GraphSettings &gs)
124 {
125   s.beginGroup(graphId+graphSettingsGroupSuffix);
126   s.setValue(goalWeightEnabledKey, gs.goalWeightEnabled);
127   s.setValue(weightIntervalModeKey, (int)gs.weightIntervalMode);
128   s.setValue(weightIntervalMinKey, gs.weightIntervalMin);
129   s.setValue(weightIntervalMaxKey, gs.weightIntervalMax);
130   s.setValue(defaultTimeIntervalKey, gs.defaultTimeInterval);
131   s.endGroup();
132 }
133 void Settings::setGraphSettingsAndSync(const QString &graphId,
134                                        GraphSettings &gs)
135 {
136   setGraphSettings(graphId, gs);
137   sync();
138 }
139
140 QStringList Settings::graphIds()
141 {
142   QStringList tmp = s.childGroups();
143   tmp = tmp.filter(QRegExp("^\\w+Graph$"));
144   return tmp.replaceInStrings(QRegExp("^(\\w+)Graph$"), "\\1");
145 }
146 void Settings::sync()
147 {
148   s.sync();
149   emit singleton->settingChanged();
150 }
151
152 QString Settings::datadir()
153 {
154 #ifdef Q_WS_MAEMO_5
155   return QDir::home().path()+"/MyDocs/WeightGraph";
156 #else
157   return QDir::home().path()+"/.weightgraph";
158 #endif
159 }
160 QString Settings::datafile()
161 {
162   return datadir()+"/weightdata.txt";
163 }
164 QString Settings::settingsfile()
165 {
166   return datadir()+"/config.ini";
167 }