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