/* 
   MultiSync Nokia (Gnokii) Plugin 
   Copyright (C) 2004 Balint Reczey <balint_reczey@yahoo.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
   SOFTWARE IS DISCLAIMED.

   based on Opie Plugin for Multisync: 
   
   Copyright (C) 2003 Tom Foottit <tom@foottit.com>
 
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include <unistd.h>
#include <gnokii.h>
#include <vobject.h>
#include <vcc.h>


int gn_vtodo2todo(char *vtodo, gn_todo * todo, int location)
{
  VObjectO *v, *t, *vtodoobj;
  VObjectIteratorO i, j;
  const char* n;
  
  /* sanity check */
  if(!vtodo || (strlen(vtodo) == 0))
  {
    return -1; 
  }
 
//  registerMimeErrorHandlerO(VObjectOErrorHander);
  vtodoobj = Parse_MIMEO(vtodo, strlen(vtodo));
  
  if(NULL == vtodoobj)
  {
   return -2;
  }
  
  initPropIteratorO(&i,vtodoobj);
  
  while(moreIterationO(&i))
  {
    v = nextVObjectO(&i);
    n = vObjectNameO(v);
    
    /* todo is a subobject of vcal */
    if(strcmp(n,VCTodoPropO) == 0)
    {
      /* get the properties of the todo */
      initPropIteratorO(&j,v);
      while(moreIterationO(&j))
      {      
        t = nextVObjectO(&j);
        n = vObjectNameO(t);
        
        if(strcmp(n,VCPriorityPropO) == 0)
        {
          switch(atoi(fakeCStringO(vObjectUStringZValueO(t))))
          {
            case 1:
            case 2:
            case 3:
              todo->priority = GN_TODO_HIGH;
              break;
            case 0: // not defined
            case 4:
            case 5:
            case 6:
              todo->priority = GN_TODO_MEDIUM;
              break;
            case 7:
            case 8:
            case 9:
              todo->priority = GN_TODO_LOW;
              break;
          }
        }    
        else if(strcmp(n,VCStatusPropO) == 0)
        {
        	;
	}
        else if(strcmp(n,VCPctCompletePropO) == 0)
        {
          ;
        }    
        else if(strcmp(n,VCSummaryPropO) == 0)
        {
          strncpy(todo->text, fakeCStringO(vObjectUStringZValueO(t)), GN_TODO_MAX_LENGTH);
        }
        else if(strcmp(n,VCDescriptionPropO) == 0)
        {
          ; 
        }
        else if(strcmp(n,VCDuePropO) == 0)
        {
        	;
	}
        
        /* categories */
        else if(strcmp(n,VCCategoriesPropO) == 0)
        {
        	;
	}
        
      } /* while */
    }
    
        
  
  } /* while */
 
  todo->location = location;
  cleanVObjectO(vtodoobj);

  return 0;

}

int gn_todo2vtodo(char** vtodo_str, gn_todo* todo)
{
  VObjectO *prop;
  VObjectO *vtodo;
  VObjectO *vcal;
  
  vcal = newVObjectO(VCCalPropO);
  vtodo = addPropO(vcal, VCTodoPropO);
  
  
  /* priority */
  if(todo->priority)
  {
    switch (todo->priority)
    {
      case 1:
      addPropValueO(vtodo, VCPriorityPropO, "2");
      break;
      case 2:
      addPropValueO(vtodo, VCPriorityPropO, "5");
      break;
      case 3:
      addPropValueO(vtodo, VCPriorityPropO, "8");
      break;
    }
  }
  
    
  /* summary */
  if(todo->text)
  {
    addPropValueO(vtodo, VCSummaryPropO, todo->text);
    
  }
  
  /* convert VObjectO to string */
  *vtodo_str = writeMemVObjectO(0,0,vcal);
  
  deleteVObjectO(vcal);
  return 0;
}
