trunk 20080912
[gitenigma.git] / src / setup_timezone.cpp
blob614660b704bb520ea92fd0ac4dd89d849ccfca7e
1 #include <setup_timezone.h>
3 #include <config.h>
4 #include <lib/gui/slider.h>
5 #include <lib/gui/ebutton.h>
6 #include <lib/gui/elabel.h>
7 #include <lib/gui/combobox.h>
8 #include <lib/gui/echeckbox.h>
9 #include <lib/gui/enumber.h>
10 #include <lib/gui/eskin.h>
11 #include <lib/gui/actions.h>
12 #include <lib/system/econfig.h>
13 #include <enigma_main.h>
15 #ifndef ZONEINFODIR
16 #define ZONEINFODIR DATADIR "/zoneinfo"
17 #endif
19 eZapTimeZoneSetup::eZapTimeZoneSetup(bool showHint)
20 :eWindow(0), showHint(showHint)
22 setText(_("Time Zone Setup"));
23 cmove(ePoint(110, 146));
24 cresize(eSize(530, 270));
26 int fd=eSkin::getActive()->queryValue("fontsize", 20);
28 ltimeZone=new eLabel(this);
29 ltimeZone->move(ePoint(20, 20));
30 ltimeZone->resize(eSize(220, fd+4));
31 ltimeZone->setText(_("Time Zone:"));
33 timeZone=new eComboBox(this, 8, ltimeZone);
34 timeZone->move(ePoint(20, 60));
35 timeZone->resize(eSize(clientrect.width()-40, 35));
36 timeZone->setHelpText(_("select your time zone (ok)"));
37 timeZone->loadDeco();
39 errLoadTimeZone = loadTimeZones();
41 ok=new eButton(this);
42 ok->setText(_("save"));
43 ok->setShortcut("green");
44 ok->setShortcutPixmap("green");
45 ok->move(ePoint(20, clientrect.height()-100));
46 ok->resize(eSize(220, 40));
47 ok->setHelpText(_("save changes and return"));
48 ok->loadDeco();
49 CONNECT(ok->selected, eZapTimeZoneSetup::okPressed);
51 statusbar=new eStatusBar(this);
52 statusbar->move( ePoint(0, clientrect.height()-50 ) );
53 statusbar->resize( eSize( clientrect.width(), 50) );
54 statusbar->loadDeco();
56 setHelpID(90);
59 struct delString
61 delString()
65 bool operator()(eListBoxEntryText &e)
67 delete (eString*)(e.getKey());
68 return false;
72 eZapTimeZoneSetup::~eZapTimeZoneSetup()
74 timeZone->forEachEntry(delString());
77 void eZapTimeZoneSetup::okPressed()
79 if (!errLoadTimeZone)
81 // save current selected time zone
82 if ( eConfig::getInstance()->setKey("/elitedvb/timezone", ((eString*) timeZone->getCurrent()->getKey())->c_str()))
84 eConfig::getInstance()->delKey("/elitedvb/timezone");
85 eDebug("Write timezone with error %i", eConfig::getInstance()->setKey("/elitedvb/timezone", ((eString*) timeZone->getCurrent()->getKey())->c_str()));
87 eConfig::getInstance()->flush();
88 setTimeZone();
89 if (showHint)
91 eMessageBox msg(_("You have to restart enigma to apply the new Timezone\nRestart now?"), _("Timezone changed"), eMessageBox::btYes|eMessageBox::btNo|eMessageBox::iconQuestion, eMessageBox::btYes );
92 msg.show();
93 if ( msg.exec() == eMessageBox::btYes )
94 eApp->quit(2);
95 msg.hide();
97 else
98 eApp->quit(2);
100 close(0);
103 void eZapTimeZoneSetup::setTimeZone()
105 char *ctimeZone=cmdTimeZones();
106 if ( system(
107 eString().sprintf(
108 "cp " ZONEINFODIR "/%s /var/etc/localtime",
109 ctimeZone).c_str() ) >> 8)
110 eDebug("couldn't set timezone");
111 free(ctimeZone);
114 int eZapTimeZoneSetup::loadTimeZones()
116 XMLTreeParser parser("ISO-8859-1");
117 int done=0;
118 const char *filename="/etc/timezone.xml";
120 FILE *in=fopen(filename, "rt");
121 if (!in)
123 eWarning("unable to open %s", filename);
124 return -1;
128 char buf[2048];
129 unsigned int len=fread(buf, 1, sizeof(buf), in);
130 done=len<sizeof(buf);
131 if (!parser.Parse(buf, len, done))
133 eDebug("parse error: %s at line %d",
134 parser.ErrorString(parser.GetErrorCode()),
135 parser.GetCurrentLineNumber());
136 fclose(in);
137 return -1;
139 } while (!done);
140 fclose(in);
142 XMLTreeNode *root=parser.RootNode();
143 if (!root)
144 return -1;
146 char *ctimeZone;
147 if ( eConfig::getInstance()->getKey("/elitedvb/timezone", ctimeZone) )
148 ctimeZone=0;
150 eListBoxEntryText *cur=0;
151 for (XMLTreeNode *node = root->GetChild(); node; node = node->GetNext())
152 if (!strcmp(node->GetType(), "zone"))
154 const char *name=node->GetAttributeValue("name");
155 if (!name)
157 eFatal("error in a file timezone.xml, no name timezone");
158 return -1;
160 eListBoxEntryText *tz=new eListBoxEntryText( *timeZone, name, (void*) new eString(name) );
161 if ( ctimeZone && !strcmp(ctimeZone, name) )
163 cur=tz;
165 } else
166 eFatal("error in a file timezone.xml, unknown timezone");
168 if ( timeZone->setCurrent(cur) == eComboBox::E_INVALID_ENTRY )
169 timeZone->setCurrent(27); // GMT+1
171 free(ctimeZone);
173 return 0;
176 char *eZapTimeZoneSetup::cmdTimeZones()
178 XMLTreeParser parser("ISO-8859-1");
179 int done=0;
180 const char *filename="/etc/timezone.xml";
182 FILE *in=fopen(filename, "rt");
183 if (!in)
185 eWarning("unable to open %s", filename);
186 return "";
190 char buf[2048];
191 unsigned int len=fread(buf, 1, sizeof(buf), in);
192 done=len<sizeof(buf);
193 if (!parser.Parse(buf, len, done))
195 eDebug("parse error: %s at line %d",
196 parser.ErrorString(parser.GetErrorCode()),
197 parser.GetCurrentLineNumber());
198 fclose(in);
199 return "";
201 } while (!done);
202 fclose(in);
204 XMLTreeNode *root=parser.RootNode();
205 if (!root)
206 return "";
208 char *ctimeZone;
209 if ( eConfig::getInstance()->getKey("/elitedvb/timezone", ctimeZone) )
210 ctimeZone=0;
212 for (XMLTreeNode *node = root->GetChild(); node; node = node->GetNext())
213 if (!strcmp(node->GetType(), "zone"))
215 const char *name=node->GetAttributeValue("name"),
216 *zone=node->GetAttributeValue("zone");
217 // *dst=node->GetAttributeValue("dst");
218 if (!zone)
220 eFatal("error in a file timezone.xml, no name timezone");
221 free(ctimeZone);
222 return "";
224 if ( ctimeZone && !strcmp(ctimeZone, name) )
226 free(ctimeZone);
227 return strdup(zone);
230 else
231 eFatal("error in file timezone.xml, unknown timezone");
232 free(ctimeZone);
234 return "";