This email list is read-only.  Emails sent to this list will be discarded
----------------------------------
 ChangeLog                                  |   12 +
 moblin-system-daemon/datetime/datetime.c   |  468 ++++++++++++++++++++--------
 moblin-system-daemon/datetime/datetime.h   |    4 +
 moblin-system-daemon/datetime/datetime.xml |    4 +
 4 files changed, 364 insertions(+), 124 deletions(-)

New commits:
commit 363db2635cfd9266c37bb5d9414b4bc105a7e2c6
Author: Lilli Szafranski <[EMAIL PROTECTED]>
Date:   Tue Jul 29 13:43:25 2008 -0500

    Lilli adding the first version of the new datetime module.


Diff in this email is a maximum of 400 lines.
diff --git a/ChangeLog b/ChangeLog
index cb954b4..938bbf7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+moblin-settings (2.09)
+
+  * Rewrote get_time, set_time, get_timezone, and set_timezone functions.  
Added 
+    get_all_timezones function.  get/set_time now uses the system_get_time and 
+    system_set_time functions through the include sys/time.h headers.  
+    get/set_timezones now uses symbolic links between the file /etc/localtime 
+    and the timezone configured in /usr/share/zoneinfo directory.  
get_all_timezones
+    parses the file /usr/share/zoneinfo/zone.tab to return the list of all 
+    available timezones, their lat/lon, alpha-code, and comment if desired.
+
+ -- Lilli Szafranski <[EMAIL PROTECTED]>  Tue Jul 29 12:49:46 CDT 2008
+
 moblin-settings (2.08)
 
   * Added PolicyKit and ConsoleKit support to enforce access control of
diff --git a/moblin-system-daemon/datetime/datetime.c 
b/moblin-system-daemon/datetime/datetime.c
index ddafb1e..2b5dace 100644
--- a/moblin-system-daemon/datetime/datetime.c
+++ b/moblin-system-daemon/datetime/datetime.c
@@ -4,17 +4,25 @@
 #include "moblin-system-dbus.h"
 #include <oobs/oobs.h>
 
-static OobsSession *session = NULL;
-static OobsObject *time_config = NULL;
+// lilli
+#include <sys/time.h>
+#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#define TZ_ALL         16      // 0b00010000
+#define TZ_CODE                8       // 0b00001000
+#define TZ_LATLON      4       // 0b00000100
+#define TZ_NAME                2       // 0b00000010
+#define TZ_COMMENT     1       // 0b00000001
+
+#define AVG_LINE_LEN 50        // This will be the average length per timezone 
line in the /usr/share/zoneinfo/zone.tab file
+                                               // on an Ubuntu Hardy dist as 
of 7/28/08.  Used to approx the length of the array of zones
+
+// end lilli
 
 extern GQuark error_quark;
-const char *oobsresult[] = {
-  "OOBS_RESULT_OK",
-  "OOBS_RESULT_ACCESS_DENIED",
-  "OOBS_RESULT_NO_PLATFORM",
-  "OOBS_RESULT_MALFORMED_DATA",
-  "OOBS_RESULT_ERROR"
-};
 
 gboolean
 system_daemon_set_time(MoblinSystemServer * server,
@@ -22,20 +30,45 @@ system_daemon_set_time(MoblinSystemServer * server,
        const gint hour, const gint minute, const gint second,
        GError **error)
 {
-OobsResult res;
-
-    PRINTF("Set Time: Y:%d, M:%d, D:%d, h:%d, m:%d, s:%d\n",
-       year, month, day, hour, minute, second);
-    oobs_time_config_set_time(OOBS_TIME_CONFIG (time_config),
-       year, month, day, hour, minute, second);
-    res = oobs_object_commit(time_config);
-    if(res != OOBS_RESULT_OK)
-    {
-       PRINTF("oobs_time_config_set_time FAILED: %s\n", oobsresult[res]);
-       *error = g_error_new(error_quark, res, oobsresult[res]);
-       return FALSE;
-    }
-    return TRUE;
+       int retVal = 0;
+       
+       struct timeval* timev;
+    struct timezone* timez = NULL;
+    
+       struct tm* time;
+       
+       time->tm_sec    = second;
+       time->tm_min    = minute;
+       time->tm_hour   = hour;
+       time->tm_mday   = day;
+       time->tm_mon    = month;
+       time->tm_year   = year;
+               
+       // Getting the seconds since the Epoch from the time structure
+       timev->tv_sec = mktime(time);
+       if(timev->tv_sec == -1) 
+       {
+        // TODO: set GError error here
+               printf("mktime() failed.\n");
+               return FALSE;
+       }
+    printf("mktime() successful.\n");
+       
+       // This function does not support milliseconds
+       timev->tv_usec = 0;
+
+    retVal = settimeofday(timev, timez);
+       if(retVal) 
+       {
+        // TODO: set GError error here
+               printf("settimeofday() failed: errno= %s\n", strerror(errno));
+        return FALSE;
+       }
+       printf("settimeofday() was successful.\n");
+       
+       printf ( "The time and date was set to: %s", asctime (time) );
+       
+       return TRUE;
 }
 
 gboolean
@@ -44,133 +77,320 @@ system_daemon_get_time(MoblinSystemServer * server,
        gint *hour, gint *minute, gint *second,
        GError **error)
 {
-    oobs_time_config_get_time (OOBS_TIME_CONFIG (time_config),
-       year, month, day, hour, minute, second);
-    PRINTF("Get Time: Y:%d, M:%d, D:%d, h:%d, m:%d, s:%d\n",
-       *year, *month, *day, *hour, *minute, *second);
-    return TRUE;
+       int retVal = 0;
+
+    struct timeval timev;
+    struct timezone timez;
+       
+       struct tm* getTime;
+
+       retVal = gettimeofday(&timev, &timez);
+       if(retVal) 
+       {
+        // TODO: set GError error here
+               printf("gettimeofday() failed: errno= %s\n", strerror(errno));
+        return FALSE;
+    }
+       printf("gettimeofday() successful.\n");
+       
+       getTime = localtime(&timev.tv_sec);
+       
+       if(getTime == NULL)
+       {
+               // TODO: set GError error here
+               printf("localtime() failed.\n");
+               return FALSE;
+       }
+       
+       printf ( "The current time and date is: %s", asctime (getTime) );
+  
+       *year   = (gint)(getTime->tm_year + 1900);
+       *month  = (gint)(getTime->tm_mon + 1);
+       *day    = (gint)(getTime->tm_mday);
+       *hour   = (gint)(getTime->tm_hour);
+       *minute = (gint)(getTime->tm_min);
+       *second = (gint)(getTime->tm_sec);
+       
+       return TRUE;
+}          
+gboolean
+system_daemon_get_all_timezones(MoblinSystemServer * server,
+       gchar ***tz_list, int options, GError **error)
+{
+       int optionsBitMask = 8; // 0b00001000
+       
+       FILE* pFile;
+       long fileLen;
+       char* buffer;
+               
+       char* tok;
+       
+       char* orig = NULL;
+       char* copy = NULL;
+       char* origIter = NULL;
+       char* copyIter = NULL;
+       char* tabIter = NULL;
+       
+       int substrlen = 0;
+
+       int arrLen = 0, approxLines = 0;
+       int arrIter = 0;
+               
+       if(!options) //  if options == NULL, set options to be the default 
which is b00000010
+       {
+               options = TZ_NAME;      
+       }
+       
+       pFile = fopen("/usr/share/zoneinfo/zone.tab", "r");
+       if(pFile == NULL)
+       {
+               printf("Error opening file. errno = %s\n", strerror(errno));
+               return FALSE;
+       }
+       
+       fseek(pFile, 0, SEEK_END);
+       fileLen = ftell(pFile);
+       rewind(pFile);
+       
+       printf("length of file: %d\n", fileLen);
+
+       if(fileLen == 0)
+       {
+               printf("Error seeking length.\n");
+               return FALSE;
+       }
+       
+       printf("allocating buffer and zones\n");
+       buffer = (char*)malloc(sizeof(char) * fileLen);
+       memset(buffer, '\0', fileLen);
+               
+       arrLen = approxLines = fileLen/AVG_LINE_LEN;
+                                       
+       *tz_list = (char**)malloc(sizeof(char*) * approxLines);
+       memset(*tz_list, '\0', approxLines);
+
+       if(tz_list == NULL || buffer == NULL)
+       {
+               printf("Memory error. errno = %s\n", strerror(errno));
+               return FALSE;
+       }
+       
+       if (fread (buffer, sizeof(char), fileLen, pFile) != fileLen) 
+       {
+               printf("Reading error. errno = %s\n", strerror(errno)); 
+               return FALSE;
+       }
+       
+       tok = strtok(buffer, "\n");
+       while(tok != NULL)
+       {
+               optionsBitMask = 8; // 0b00001000
+               
+               printf("tok   = \t\"%s\"\n", tok);
+               if(tok[0] != '#')
+               {
+                       orig = origIter = tok;//(char*)malloc(sizeof(char) * 
(strlen(tok)+2));                  
+                       //strncpy(orig, tok, strlen(tok) + 1);
+                                               
+                       copyIter = copy = (char*)malloc(sizeof(char) * 
(strlen(tok)+2));                        
+                       memset(copyIter, '\0', strlen(tok) + 2);
+                                               
+                       if(!(options & TZ_ALL))
+                       {
+                               while(optionsBitMask > 0)
+                               {
+                                       
//printf("optionsBitMask=%d\tflags=%d\t(optionsBitMask&options)=%d\n", 
optionsBitMask, options, (optionsBitMask & options));
+                                       
+                                       tabIter = strpbrk(origIter, "\t");
+                                       if(!tabIter)
+                                               tabIter = strrchr(origIter, 
'\0');
+                       
+                                       substrlen = tabIter - origIter + 1;
+                                       
+                                       if( (optionsBitMask & options) && 
tabIter)
+                                       {
+                                               strncpy(copyIter, origIter, 
substrlen);
+                                               
+                                               //printf("origIter  = 
\t\"%s\"\n", origIter);
+                                               //printf("copyIter = 
\t\"%s\"\n", copyIter);
+                                               
+                                               copyIter += substrlen;
+                                               copyIter[0] = '\0';
+                                       }
+                                       origIter += substrlen;
+                                       
+                                       optionsBitMask /= 2; // 0b00001000 -> 
0b00000100 -> 0b00000010 -> 0b00000001 -> 0
+                               }
+                       }
+                       else
+                       {
+                               strncpy(copy, orig, (strlen(orig)+1));
+                       }
+                       
+                       printf("copy = \t\"%s\"\n", copy);
+                       
+                       if(arrIter > arrLen - 1)
+                       {
+                               arrLen *= 2;
+                               *tz_list = (char**)realloc(*tz_list, 
sizeof(char*) * arrLen);
+                       }
+                       *tz_list[arrIter] = copy;
+                       
+                       arrIter++;
+               }
+               tok = strtok(NULL, "\n");
+       }
+       
+       *tz_list[arrIter] = NULL;
+       
+       return TRUE;
 }
 
 gboolean
 system_daemon_set_timezone(MoblinSystemServer * server, 
        const gchar *tz, GError **error)
 {
-gint year, month, day, hour, minute, second;
-OobsResult res;
-
-    PRINTF("Set Timezone: %s\n", tz);
-    oobs_time_config_get_utc_time (OOBS_TIME_CONFIG (time_config),
-       &year, &month, &day, &hour, &minute, &second);
-    oobs_time_config_set_timezone (OOBS_TIME_CONFIG (time_config), tz);
-    oobs_object_commit (time_config);
-    oobs_time_config_set_utc_time(OOBS_TIME_CONFIG (time_config),
-       year, month, day, hour, minute, second);
-    res = oobs_object_commit (time_config);
-    if(res != OOBS_RESULT_OK)
-    {
-       PRINTF("oobs_time_config_set_timezone FAILED: %s\n", oobsresult[res]);
-       *error = g_error_new(error_quark, res, oobsresult[res]);
-       return FALSE;
-    }
-    return TRUE;
+       char* path = "/etc/localtime";
+       struct stat buffer;
+       int retVal = 0;
+       
+       printf("Checking for configured timezone (symlink from /etc... to 
/usr... exists)...\n");
+       retVal = lstat(path, &buffer);
+       if(!retVal) // lstat succeeded
+       {
+               printf("lstat() successful.\n");
+               if(S_ISLNK(buffer.st_mode)) // there is a symbolic link between 
/etc/localtime and /usr/zoneinfo/...
+               {
+                       printf("Configured timezone. Removing timezone 
(removing symlink)...\n");
+                       
+                       retVal = unlink(path);
+                       if(retVal)
+                       {
+                               // TODO: set GError error here
+                               printf("ulink() failed: errno = %s\n", 
strerror(errno));
+                       return FALSE;
+                       }
+                       printf("unlink() successful.\n");
+                       printf("Timezone (symlink) removed.\n");
+               }
+               else // /etc/localtime exists, but there is no link to anything
+               {
+                       printf("No configured timezone.\n");
+               }
+       }
+       else if(errno != ENOENT) // lstat failed()
+       {
+               // TODO: set GError error here
+               printf("lstat() failed: errno= %s\n", strerror(errno));
+        return FALSE;
+       }
+       else // lstat() failed because the file (/etc/localtime) did not exist
+       {
+               printf("No configured timezone.\n");
+       }
+       
+       printf("Configuring timezone...\n");
+       retVal = symlink(tz, path);
+       if(retVal)
+       {
+               // TODO: set GError error here
+               printf("symlink() failed: errno = %s\n", strerror(errno));
+        return FALSE;
+       }
+       printf("symlink() succesful.\n");
+       printf("Timezone configured.\n");
+           
+       return TRUE;
 }
 
 gboolean
 system_daemon_get_timezone(MoblinSystemServer * server, 
        gchar **tz, GError **error)
 {
-const gchar *timezone;
+       char* path = "/etc/localtime";
+       
+       struct stat buffer;
+       int retVal = 0;
+       int len = 0;
 
-    timezone = oobs_time_config_get_timezone (OOBS_TIME_CONFIG (time_config));
-    *tz = g_strdup(timezone);
-    PRINTF("Get Timezone: %s\n", (*tz)?(*tz):"No Timezone Set");
-    return TRUE;
+       printf("Checking for configured timezone (symlink from /etc... to 
/usr... exists)...\n");
+       retVal = lstat(path, &buffer);
+       if(retVal) // lstat failed
+       {
+               if(errno != ENOENT) // lstat failed()
+               {
+                       // TODO: set GError error here
+                       *tz = NULL;
+                       
+                       printf("lstat() failed: errno= %s\n", strerror(errno));
+               return FALSE;
+               }
+               else // lstat() failed because the file (/etc/localtime) did 
not exist
_______________________________________________
Commits mailing list
[email protected]
https://www.moblin.org/mailman/listinfo/commits

Reply via email to