Index: trunk/prototype/services/iCal.php
===================================================================
--- trunk/prototype/services/iCal.php	(revision 6017)
+++ trunk/prototype/services/iCal.php	(revision 6026)
@@ -186,5 +186,5 @@
             switch (strtoupper($component->objName)) {
                 case 'VEVENT':
-                    
+	    
                     switch ($method) 
                     {
@@ -430,8 +430,10 @@
                          case 'REPLY':
                              if(  $schedulable = self::_getSchedulable($uid) )
-                             {
+                             {                                        
                                    while($property = $component->getProperty('attendee',FALSE , TRUE))                                 
                                        if( $pID  = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants']))
                                             $interation['participant://'.$pID] = array( 'id' => $pID , 'status' => constant('STATUS_'.strtoupper($property['params']['PARTSTAT'])) );                            
+                             
+                                    $interation['schedulable://'.$schedulable['id']]['sequence']  = $schedulable['sequence'] +1;   
                              }
                                break;
@@ -460,11 +462,25 @@
                            
                          default:
+			     
                              $schedulable = self::_getSchedulable($uid);
                              
                              if($schedulable && ( self::_getTime($component , 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty( 'sequence' , false , false ) > $schedulable['sequence'])) //Caso o evento exista
-                                $interation = self::_makeVEVENT($schedulable , $component , $params);
+			     {
+				 $interation = self::_makeVEVENT($schedulable , $component , $params);
+				 
+				 if(!self::_existInMyCalendars($schedulable['id']))
+				 {
+                                      $calendarToSchedulable = array();
+                                      $calendarToSchedulable['calendar'] = $params['calendar'];
+                                      $calendarToSchedulable['schedulable'] = $schedulable['id'];
+                                      $interation['calendarToSchedulable://'.mt_rand().'(Formatter)'] = $calendarToSchedulable;
+                                 }
+				 
+			     }
                              else // Importar evento
                                 $interation = self::_makeVEVENT( array() , $component , $params);
-
+			     
+			     
+			     
                              break;
                     }
@@ -582,5 +598,5 @@
 
             }
-
+          
              return $return;   
         }
@@ -797,5 +813,5 @@
                 if( $class && defined(constant(strtoupper('CLASS_'.$class))) )
                     $schedulable['class'] = constant(strtoupper('CLASS_'.$class));
-                else if( !isset($exists['class']) )
+                else if( !isset($schedulable['class']) )
                     $schedulable['class'] = CLASS_PRIVATE ; // padrão classe private
 
@@ -833,5 +849,5 @@
                 $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
 
-                $participantID = mt_rand().'2(Formatter)';
+                $participantID = ($tpID = self::_getParticipantByMail($mailUser, $schedulable['participants'])) ?  $tpID : mt_rand().'2(Formatter)';
                 $participant['schedulable'] = $eventID;                                      
                 
@@ -882,5 +898,5 @@
                 $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_'.$property['params']['PARTSTAT']) !== null ) ? constant('STATUS_'.$property['params']['PARTSTAT']) : STATUS_UNANSWERED;
                 $participant['isOrganizer'] = '1';
-				$participant['acl'] = 'rowi';
+		$participant['acl'] = 'rowi';
 
                 /* Verifica se este usuario é um usuario interno do ldap */     
@@ -915,5 +931,5 @@
                $user = Controller::read( array( 'concept' => 'user', 'id' => $params['owner'] ) , array('mail')); 
 
-               if(!self::_getParticipantByMail($user['mail'] , $exists['participants']))
+               if(!self::_getParticipantByMail($user['mail'] , $schedulable['participants']))
                {
                     $participantID = mt_rand().'2(Formatter)';
@@ -1006,5 +1022,5 @@
 		
 		
-		 static private function _getContentType( $fileName )
+	static private function _getContentType( $fileName )
         {
             $strFileType = strtolower(substr ( $fileName , strrpos($fileName, '.') ));
