Logo Search packages:      
Sourcecode: obex-data-server version File versions  Download package

ods-common.c

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 Tadas Dailyda <tadas@dailyda.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
#include "config.h"
 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <glib.h>

#include "ods-common.h"
#include "ods-error.h"

/* There shouldn't been a need to init this global variable, but let's do it
 * just in case */
DBusBusType ODS_DBUS_BUS=DBUS_BUS_SESSION;

gboolean
ods_check_caller (DBusGMethodInvocation *context, const gchar *owner)
{
      gchar             *caller;
      GError            *error = NULL;
      gboolean    ret = FALSE;
      
      /* check if caller matches owner of some object */
      caller = dbus_g_method_get_sender (context);
      if (strcmp (caller, owner)) {
            g_set_error (&error, ODS_ERROR, ODS_ERROR_NOT_AUTHORIZED,
                                    "Not authorized");
            dbus_g_method_return_error (context, error);
            g_clear_error (&error);
            goto out;
      }
      ret = TRUE;
out:
      g_free (caller);
      return ret;
}

glong
ods_get_uname (gunichar2 **uname, const gchar *name)
{
      glong uname_len;

      if (*name == '\0') {
            *uname = NULL;
            return 0;
      }

      *uname = g_utf8_to_utf16 (name, -1, NULL, &uname_len, NULL);

      if (*uname == NULL) {
            uname_len = -1;
      } else {
            int i;
            /* g_utf8_to_utf16 produces host-byteorder UTF-16,
             * but OBEX requires network byteorder (big endian) */
            for (i = 0; i < uname_len; i++)
                  (*uname)[i] = g_htons ((*uname)[i]);
            uname_len = (uname_len + 1) << 1;
      }

      return uname_len;
}

gchar *
ods_utf16_to_utf8 (const guchar *utf16, size_t len)
{
    return g_convert ((gchar*) utf16, len, "UTF8", "UTF16BE", NULL, NULL, NULL);
}

gint
ods_make_iso8601 (time_t time, gchar *str, gint len)
{
      struct tm tm;
#if defined(HAVE_TIMEZONE) && defined(USE_LOCALTIME)
      time_t tz_offset = 0;

      tz_offset = -timezone;
      if (daylight > 0)
            tz_offset += 3600;
      time += tz_offset;
#endif

      if (gmtime_r(&time, &tm) == NULL)
            return -1;

      tm.tm_year += 1900;
      tm.tm_mon++;

      return snprintf(str, len,
#ifdef USE_LOCALTIME
                              "%04u%02u%02uT%02u%02u%02u",
#else
                              "%04u%02u%02uT%02u%02u%02uZ",
#endif
                              tm.tm_year, tm.tm_mon, tm.tm_mday,
                              tm.tm_hour, tm.tm_min, tm.tm_sec);
}

/* From Imendio's GnomeVFS OBEX module (om-utils.c) */
time_t
ods_parse_iso8601 (const gchar *str, gint len)
{
      gchar    *tstr;
      struct tm tm;
      gint      nr;
      gchar     tz;
      time_t    time;
      time_t    tz_offset = 0;

      memset (&tm, 0, sizeof (struct tm));

      /* According to spec the time doesn't have to be null terminated */
      if (str[len - 1] != '\0') {
            tstr = g_malloc(len + 1);
            strncpy(tstr, str, len);
            tstr[len] = '\0';
      } else {
            tstr = g_strdup(str);
      }

      nr = sscanf (tstr, "%04u%02u%02uT%02u%02u%02u%c",
                              &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
                              &tm.tm_hour, &tm.tm_min, &tm.tm_sec,
                              &tz);

      g_free(tstr);

      /* Fixup the tm values */
      tm.tm_year -= 1900;       /* Year since 1900 */
      tm.tm_mon--;              /* Months since January, values 0-11 */
      tm.tm_isdst = -1;         /* Daylight savings information not avail */

      if (nr < 6) {
            /* Invalid time format */
            return -1;
      } 

      time = mktime (&tm);

#if defined(HAVE_TM_GMTOFF)
      tz_offset = tm.tm_gmtoff;
#elif defined(HAVE_TIMEZONE)
      tz_offset = -timezone;
      if (tm.tm_isdst > 0) {
            tz_offset += 3600;
      }
#endif

      if (nr == 7) { /* Date/Time was in localtime (to remote device)
                              * already. Since we don't know anything about the
                              * timezone on that one we won't try to apply UTC offset
                              */
            time += tz_offset;
      }

      return time;
}

Generated by  Doxygen 1.6.0   Back to index