vpim 0.597 → 0.602

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ # OSX wrapper methods.
2
+ #
3
+ # The OSX classes are mirrored fairly directly into ruby by ruby/cocoa. Too
4
+ # directly for ease, this is a start at convenient ruby APIs on top of the low-level cocoa
5
+ # methods.
6
+
7
+ =begin
8
+
9
+ Ideas for things to add:
10
+
11
+ + each for the addressbook
12
+
13
+ + ABRecord#[] <- valueForProperty
14
+
15
+ + [] and each for NSCFArray (which is actually an instance of OCObject)
16
+
17
+ + [] and each for NSCFDictionary (which is actually an instance of OCObject)
18
+
19
+ + Can I add methods to OCObject, and have them implement themselves based on the the 'class'?
20
+
21
+ + ABMultiValue#[index]
22
+
23
+ if index
24
+
25
+ is a :token, then its the identifier,
26
+ is a string, its a label
27
+ is a number, its an array index
28
+
29
+ return a Struct, so you can do
30
+
31
+ mvalue["work"].value
32
+
33
+ =end
34
+
35
+ require 'osx/addressbook'
36
+
37
+ # put into osx/ocobject?
38
+ module OSX
39
+ # When an NSData object is returned by an objective/c API (such as
40
+ # ABPerson.vCardRepresentation, I actually get a OCObject back, who's class
41
+ # instance points to either a NSData, or a related class.
42
+ #
43
+ # This is a convenience method to get the NSData data back as a ruby string.
44
+ # Is it the right place to put this?
45
+ class OCObject
46
+ def bytes
47
+ s = ' ' * length
48
+ getBytes(s)
49
+ s
50
+ end
51
+ end
52
+ end
53
+
54
+ # put into osx/abperson?
55
+ module OSX
56
+ class ABPerson
57
+ def vCard
58
+ card = self.vCardRepresentation.bytes
59
+
60
+ # The card representation appears to be either ASCII, or UCS-2. If its
61
+ # UCS-2, then the first byte will be 0, so check for this, and convert
62
+ # if necessary.
63
+ #
64
+ # We know it's 0, because the first character in a vCard must be the 'B'
65
+ # of "BEGIN:VCARD", and in UCS-2 all ascii are encoded as a 0 byte
66
+ # followed by the ASCII byte, UNICODE is great.
67
+ if card[0] == 0
68
+ nsstring = OSX::NSString.alloc.initWithCharacters(card, :length, card.size/2)
69
+ card = nsstring.UTF8String
70
+
71
+ # TODO: is nsstring.UTF8String == nsstring.to_s ?
72
+ end
73
+ card
74
+ end
75
+ end
76
+ end
77
+
78
+ # put into osx/group?
79
+ module OSX
80
+ class ABGroup
81
+ def name
82
+ self.valueForProperty(OSX::kABGroupNameProperty).to_s
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $-w = true
4
+
5
+ require 'ubygems' rescue "ignored"
6
+
7
+ require 'getoptlong'
8
+ require 'pp'
9
+ require 'plist'
10
+
11
+ require 'vpim/repo'
12
+
13
+ $stdout.sync = true
14
+ $stderr.sync = true
15
+
16
+ HELP =<<EOF
17
+ Usage: #{$0} [where]
18
+
19
+ Shows events and todos occuring soon.
20
+
21
+ By default, the Apple iCal v3 calendars are used, but if a location where
22
+ .ics files is specified, any calendars found there will be used.
23
+
24
+ Options
25
+ -h,--help Print this helpful message.
26
+ -n,--days N How many of the next days are considered to be "soon", default
27
+ is seven.
28
+ -v,--verbose Print more information about upcoming events.
29
+ EOF
30
+
31
+ opt_debug = nil
32
+ opt_verbose = nil
33
+ opt_days = 7
34
+
35
+ opts = GetoptLong.new(
36
+ [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
37
+ [ "--days", "-n", GetoptLong::REQUIRED_ARGUMENT ],
38
+ [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
39
+ [ "--debug", "-d", GetoptLong::NO_ARGUMENT ]
40
+ )
41
+
42
+ opts.each do |opt, arg|
43
+ case opt
44
+ when "--help" then
45
+ puts HELP
46
+ exit 0
47
+
48
+ when "--days" then
49
+ opt_days = arg.to_i
50
+
51
+ when "--verbose" then
52
+ opt_verbose = true
53
+
54
+ when "--debug" then
55
+ opt_verbose = true
56
+ opt_debug = true
57
+ end
58
+ end
59
+
60
+ calendars = []
61
+
62
+ if ARGV.length > 0
63
+ Vpim::Repo::Directory.each(ARGV.first) do |cal|
64
+ calendars << cal
65
+ end
66
+ else
67
+ Vpim::Repo::Ical3.each() do |cal|
68
+ calendars << cal
69
+ end
70
+ end
71
+
72
+ if opt_debug
73
+ pp ARGV
74
+ pp calendars
75
+ end
76
+
77
+ SECSPERDAY = (24 * 60 * 60)
78
+
79
+ t0 = Time.new.to_a
80
+ t0[0] = t0[1] = t0[2] = 0 # sec,min,hour = 0
81
+ t0 = Time.local(*t0)
82
+ t1 = t0 + opt_days * SECSPERDAY
83
+
84
+ if opt_debug
85
+ puts "to: #{t0}"
86
+ puts "t1: #{t1}"
87
+ end
88
+
89
+ if opt_verbose
90
+ puts "Events in the next #{opt_days} days:"
91
+ end
92
+
93
+ # Collect all events, then all todos.
94
+ all_events = []
95
+ all_todos = []
96
+
97
+ calendars.each do |cal|
98
+ if opt_debug; puts cal.name; end
99
+
100
+ begin
101
+ cal.events.each do |e|
102
+ begin
103
+ if opt_debug; pp e; end
104
+ if e.occurs_in?(t0, t1)
105
+ if e.summary
106
+ all_events.push(e)
107
+ end
108
+ end
109
+ rescue
110
+ $stderr.puts "error in #{cal.name} (\"#{e.summary}\"): #{$!.to_s}"
111
+ end
112
+ end
113
+
114
+ all_todos.concat(cal.todos)
115
+ end
116
+ end
117
+
118
+ puts
119
+
120
+ def start_of_first_occurence(t0, t1, e)
121
+ e.occurences.each_until(t1).each do |t|
122
+ # An event might start before t0, but end after it..., in which case
123
+ # we are still interested.
124
+ if (t + (e.duration || 0)) >= t0
125
+ return t
126
+ end
127
+ end
128
+ nil
129
+ end
130
+
131
+ all_events.sort! do |lhs, rhs|
132
+ start_of_first_occurence(t0, t1, lhs) <=> start_of_first_occurence(t0, t1, rhs)
133
+ end
134
+
135
+ all_events.each do |e|
136
+ puts "#{e.summary}:"
137
+
138
+ if opt_verbose
139
+ if e.description; puts " description=#{e.description}"; end
140
+ if e.comments; puts " comment=#{e.comments.first}"; end
141
+ if e.location; puts " location=#{e.location}"; end
142
+ if e.status; puts " status=#{e.status}"; end
143
+ if e.dtstart; puts " dtstart=#{e.dtstart}"; end
144
+ if e.duration; puts " duration=#{Vpim::Duration.new(e.duration).to_s}"; end
145
+ end
146
+
147
+ i = 1
148
+ e.occurences.each_until(t1).each do |t|
149
+ # An event might start before t0, but end after it..., in which case
150
+ # we are still interested.
151
+ dstr = ''
152
+ if e.duration
153
+ d = e.duration
154
+ dstr = " for #{Vpim::Duration.new(e.duration).to_s}"
155
+ end
156
+
157
+ if (t + (e.duration || 0)) >= t0
158
+ puts " ##{i} on #{t}#{dstr}"
159
+ i += 1
160
+ end
161
+ end
162
+ end
163
+
164
+ =begin
165
+ def fix_priority(vtodo)
166
+ p = vtodo.priority
167
+ if !p
168
+ p = 10
169
+
170
+ end
171
+ =end
172
+
173
+ all_todos.sort! do |x,y|
174
+ x = x.priority
175
+ y = y.priority
176
+
177
+ # 0 means no priority, put these last, not first
178
+ x = 10 if x == 0
179
+ y = 10 if y == 0
180
+
181
+ x <=> y
182
+ end
183
+
184
+ priorities = [
185
+ 'no importance',
186
+ 'very important',
187
+ 'very important',
188
+ 'very important',
189
+ 'important',
190
+ 'important',
191
+ 'important',
192
+ 'not important',
193
+ 'not important',
194
+ 'not important'
195
+ ]
196
+
197
+ all_todos.each do |e|
198
+ status = e.status || 'Todo'
199
+ if status != 'COMPLETED'
200
+ puts "#{status.capitalize}: #{e.summary}" # (#{priorities[e.priority]})"
201
+ end
202
+ end
203
+
data/samples/rrule.rb ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $-w = true
4
+ $:.unshift File.dirname($0) + "/../lib"
5
+
6
+ require 'vpim/rrule'
7
+ require 'getoptlong'
8
+ require 'parsedate'
9
+
10
+ HELP =<<EOF
11
+ Usage: #{$0} [options] rrule
12
+
13
+ Options
14
+ -h,--help Print this helpful message.
15
+ -t,--start Start time for recurrence rule, defaults to current time.
16
+
17
+ Examples:
18
+
19
+ FREQ=DAILY;COUNT=10
20
+ FREQ=DAILY;UNTIL=19971224T000000Z
21
+ FREQ=DAILY;INTERVAL=2
22
+ FREQ=DAILY;INTERVAL=10;COUNT=5
23
+
24
+ Demonstrate DST time change:
25
+
26
+ #{$0} --start '2004-04-03 02:00' 'FREQ=daily;count=3'
27
+ #{$0} --start '2004-10-30 02:00' 'FREQ=daily;count=3'
28
+
29
+ Note: In the US DST starts at 2AM, on the first Sunday of April, and reverts
30
+ at 2AM on the last Sunday of October.
31
+
32
+ EOF
33
+
34
+ dtstart = Time.new
35
+
36
+ opts = GetoptLong.new(
37
+ [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
38
+ [ "--start", "-t", GetoptLong::REQUIRED_ARGUMENT]
39
+ )
40
+
41
+ opts.each do |opt, arg|
42
+ case opt
43
+ when "--help" then
44
+ puts HELP
45
+ exit 0
46
+
47
+ when "--start" then
48
+ date = ParseDate.parsedate(arg)
49
+ date.pop
50
+ date.pop
51
+ dtstart = Time.local(*date)
52
+ end
53
+ end
54
+
55
+ if ARGV.length < 1
56
+ puts "no rrule specified, try -h!\n"
57
+ exit 1
58
+ end
59
+
60
+ puts "Start: #{Vpim.encode_date_time(dtstart)}"
61
+
62
+ ARGV.each do |rule|
63
+ rrule = Vpim::Rrule.new(dtstart, rule)
64
+
65
+ puts "Rule: #{rule}"
66
+
67
+ rrule.each_with_index do |t, count|
68
+ puts format("count=%3d %s", count, t.to_s)
69
+ end
70
+ end
71
+