Files removed from mygnokii distribution
[gnokii.git] / common / gsm-calendar.c
1 /*
2
3   G N O K I I
4
5   A Linux/Unix toolset and driver for Nokia mobile phones.
6
7   Released under the terms of the GNU GPL, see file COPYING for more details.
8         
9   Functions for manipulating calendar
10
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <sys/stat.h>
18
19 #ifdef WIN32
20   #include "misc_win32.h"
21 #else
22   #include <unistd.h>
23 #endif
24
25 #include "gsm-api.h"
26 #include "gsm-coding.h"
27
28 char *GSM_GetVCALENDARStart(int version) {
29
30   static char Buffer[1000]="";
31   int len=0;
32
33   len+=sprintf(Buffer, "BEGIN:VCALENDAR%c%c",13,10);
34
35   if (version==10) sprintf(Buffer+len, "VERSION:1.0%c%c",13,10);
36   if (version==30) sprintf(Buffer+len, "VERSION:3.0%c%c",13,10);
37
38   return Buffer;
39 }
40
41 char *GSM_GetVCALENDARNote(GSM_CalendarNote *note, int version) {
42
43   static char Buffer[1000]="";
44
45   int len=0;
46
47   u8 buffer[50*3];
48
49   len+=sprintf(Buffer+len, "BEGIN:VEVENT%c%c",13,10);
50   len+=sprintf(Buffer+len, "CATEGORIES:");
51   switch (note->Type) {
52     case GCN_REMINDER:
53       len+=sprintf(Buffer+len, "MISCELLANEOUS%c%c",13,10);
54       break;
55     case GCN_CALL:
56       len+=sprintf(Buffer+len, "PHONE CALL%c%c",13,10);
57       break;
58     case GCN_MEETING:
59       len+=sprintf(Buffer+len, "MEETING%c%c",13,10);
60       break;
61     case GCN_BIRTHDAY:
62       len+=sprintf(Buffer+len, "SPECIAL OCCASION%c%c",13,10);
63       break;
64     default:
65       len+=sprintf(Buffer+len, "UNKNOWN%c%c",13,10);
66       break;
67   }
68
69   if( note->Type == GCN_CALL && strcmp( note->Phone, "" ) ) {
70
71     if (note->Text[0]!=0 && version==30)
72       len+=sprintf(Buffer+len, "DESCRIPTION:%s%c%c",note->Text,13,10);
73
74     EncodeUTF8(buffer,note->Phone,strlen(note->Phone));
75     if (strlen(note->Phone)==strlen(buffer)) {
76       len+=sprintf(Buffer+len, "SUMMARY:%s%c%c",note->Phone,13,10);
77     } else {
78       len+=sprintf(Buffer+len, "SUMMARY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:%s%c%c",buffer,13,10);
79     }
80             
81   } else {
82
83     EncodeUTF8(buffer,note->Text,strlen(note->Text));
84     if (strlen(note->Text)==strlen(buffer)) {
85       len+=sprintf(Buffer+len, "SUMMARY:%s%c%c",note->Text,13,10);
86     } else {
87       len+=sprintf(Buffer+len, "SUMMARY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:%s%c%c",buffer,13,10);
88     }
89
90   }
91
92   /* For 3310: date is set to 2090! */
93   if (GetModelFeature (FN_CALENDAR)==F_CAL33) {
94     len+=sprintf(Buffer+len, "DTSTART:1999%02d%02dT%02d%02d%02d%c%c",
95             note->Time.Month, note->Time.Day, note->Time.Hour,
96             note->Time.Minute, note->Time.Second,13,10);
97   } else {
98     len+=sprintf(Buffer+len, "DTSTART:%04d%02d%02dT%02d%02d%02d%c%c", note->Time.Year,
99             note->Time.Month, note->Time.Day, note->Time.Hour,
100             note->Time.Minute, note->Time.Second,13,10);
101   }
102
103   if (note->Alarm.Year!=0) {
104     len+=sprintf(Buffer+len, "DALARM:%04d%02d%02dT%02d%02d%02d%c%c", note->Alarm.Year,
105             note->Alarm.Month, note->Alarm.Day, note->Alarm.Hour,
106             note->Alarm.Minute, note->Alarm.Second,13,10);
107   }
108
109   if ( GetModelFeature (FN_CALENDAR)==F_CAL71 && version==30 && note->Type == GCN_BIRTHDAY ) {
110         len+=sprintf(Buffer+len, "ALTYPE:%s%c%c", (note->AlarmType==0x00) ?
111                                               "TONE" : "SILENT",13,10);
112   }
113
114   if ( GetModelFeature (FN_CALENDAR)==F_CAL71 && note->Recurrance!= 0 ) {
115     switch(note->Recurrance/24) {
116       case 1  :len+=sprintf(Buffer+len, "RRULE:D1 #0%c%c",13,10);break;
117       case 7  :len+=sprintf(Buffer+len, "RRULE:W1 #0%c%c",13,10);break;
118       case 14 :len+=sprintf(Buffer+len, "RRULE:W2 #0%c%c",13,10);break;
119       case 365:len+=sprintf(Buffer+len, "RRULE:YD1 #0%c%c",13,10);break;
120     }
121   }
122
123   len+=sprintf(Buffer+len, "END:VEVENT%c%c",13,10);
124
125   return Buffer;
126 }
127
128 char *GSM_GetVCALENDAREnd(int version) {
129
130   static char Buffer[1000]="";
131
132   sprintf(Buffer, "END:VCALENDAR%c%c",13,10);
133
134   return Buffer;
135 }
136
137 int GSM_SaveCalendarNoteToSMS(GSM_MultiSMSMessage *SMS,
138                               GSM_CalendarNote *note)
139 {
140   char Buffer[1000]="";
141   int length;
142   GSM_UDH UDH=GSM_NoUDH;
143
144   sprintf(Buffer,GSM_GetVCALENDARStart(10));
145   sprintf(Buffer+strlen(Buffer),GSM_GetVCALENDARNote(note,10));  
146   sprintf(Buffer+strlen(Buffer),GSM_GetVCALENDAREnd(10));
147
148   /* 1 SMS: no UDH */
149   if (strlen(Buffer)<=160-8) { //8==length of ..SCKE4.
150     sprintf(Buffer,"//SCKE4 ");  
151     sprintf(Buffer+strlen(Buffer),GSM_GetVCALENDARStart(10));
152     sprintf(Buffer+strlen(Buffer),GSM_GetVCALENDARNote(note,10));  
153     sprintf(Buffer+strlen(Buffer),GSM_GetVCALENDAREnd(10));
154   } else {
155     UDH=GSM_CalendarNoteUDH;
156   }
157
158   length=strlen(Buffer);
159   GSM_MakeMultiPartSMS2(SMS,Buffer,length, UDH, GSM_Coding_Default);
160
161   return 0;
162 }