PageRenderTime 51ms CodeModel.GetById 9ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/update-engine/Core/KSClientActives.m

http://macfuse.googlecode.com/
Objective C | 144 lines | 90 code | 27 blank | 27 comment | 17 complexity | e3898173adf11d578650507d63e1043f MD5 | raw file
  1// Copyright 2010 Google Inc.
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#import "KSClientActives.h"
 16
 17// Dictionary keys for the productInfo_ ivar.
 18#define kLastRollCallPingKey @"RollCallPing"  // NSDate
 19#define kLastActiveKey @"LastActive"          // NSDate
 20#define kLastActivePingKey @"LastActivePing"  // NSDate
 21#define kSentActiveKey @"SentActive"          // NSNumber-wrapped BOOL
 22#define kSentRollCallKey @"SentRollCall"      // NSNumber-wrapped BOOL
 23
 24// Approximate number of seconds in a day.
 25#define DAY_SECONDS (24 * 60 * 60)
 26
 27@implementation KSClientActives
 28
 29- (id)init {
 30  if ((self = [super init])) {
 31    productInfo_ = [[NSMutableDictionary alloc] init];
 32  }
 33  return self;
 34}
 35
 36- (void)dealloc {
 37  [productInfo_ release];
 38  [super dealloc];
 39}
 40
 41
 42// Returns a dictionary for a given product ID.  If there is no dictionary
 43// for this product, create a new one.
 44- (NSMutableDictionary *)infoForProductID:(NSString *)productID {
 45  NSMutableDictionary *info = [productInfo_ objectForKey:productID];
 46
 47  if (info == nil) {
 48    info = [NSMutableDictionary dictionary];
 49    [productInfo_ setObject:info forKey:productID];
 50  }
 51  return info;
 52}
 53
 54- (void)setLastRollCallPing:(NSDate *)lastRollCall
 55             lastActivePing:(NSDate *)lastActivePing
 56                 lastActive:(NSDate *)lastActive
 57               forProductID:(NSString *)productID {
 58  NSMutableDictionary *info = [self infoForProductID:productID];
 59  if (lastRollCall) [info setObject:lastRollCall forKey:kLastRollCallPingKey];
 60  if (lastActivePing) [info setObject:lastActivePing forKey:kLastActivePingKey];
 61  if (lastActive) [info setObject:lastActive forKey:kLastActiveKey];
 62}
 63
 64// Calculates how many active days to report betwee now and the given date.
 65- (int)activeDaysSinceDate:(NSDate *)date {
 66  if (date == nil) return kKSClientActivesFirstReport;
 67
 68  NSTimeInterval interval = [date timeIntervalSinceNow];
 69
 70  // The interval should be a negative value.  Punt if the date is in
 71  // the future.
 72  if (interval > 0) return kKSClientActivesDontReport;
 73
 74  // Flip the sign and figure the number of days.
 75  int days = fabs(interval) / DAY_SECONDS;
 76
 77  if (days == 0) return kKSClientActivesDontReport;
 78  else return days;
 79}
 80
 81- (int)rollCallDaysForProductID:(NSString *)productID {
 82  NSMutableDictionary *info = [self infoForProductID:productID];
 83  NSDate *lastRollCallPing = [info objectForKey:kLastRollCallPingKey];
 84
 85  int result = [self activeDaysSinceDate:lastRollCallPing];
 86  return result;
 87}
 88
 89- (int)activeDaysForProductID:(NSString *)productID {
 90  NSMutableDictionary *info = [self infoForProductID:productID];
 91  NSDate *lastActivePing = [info objectForKey:kLastActivePingKey];
 92  NSDate *lastActive = [info objectForKey:kLastActiveKey];
 93
 94  int result = kKSClientActivesDontReport;
 95
 96  // Only report actives if there's actually been an active.
 97  if (lastActive) {
 98    // Product *has* been active, but we've never reported it.
 99    // So this is the first time.
100    if (lastActivePing == nil) {
101      result = kKSClientActivesFirstReport;
102    } else {
103      // Otherwise, if the last active is newer than the last active
104      // ping, then the user has used the product since we last
105      // reported it.
106      if ([lastActive timeIntervalSinceDate:lastActivePing] > 0) {
107        result = [self activeDaysSinceDate:lastActivePing];
108      }
109    }
110  }
111
112  return result;
113}
114
115- (void)sentRollCallForProductID:(NSString *)productID {
116  NSMutableDictionary *info = [self infoForProductID:productID];
117  [info setObject:[NSNumber numberWithBool:YES] forKey:kSentRollCallKey];
118}
119
120- (BOOL)didSendRollCallForProductID:(NSString *)productID {
121  NSMutableDictionary *info = [self infoForProductID:productID];
122  NSNumber *didSend = [info objectForKey:kSentRollCallKey];
123  return [didSend boolValue];
124}
125
126- (void)sentActiveForProductID:(NSString *)productID {
127  NSMutableDictionary *info = [self infoForProductID:productID];
128  [info setObject:[NSNumber numberWithBool:YES] forKey:kSentActiveKey];
129}
130
131- (BOOL)didSendActiveForProductID:(NSString *)productID {
132  NSMutableDictionary *info = [self infoForProductID:productID];
133  NSNumber *didSend = [info objectForKey:kSentActiveKey];
134  return [didSend boolValue];
135}
136
137- (NSString *)description {
138  NSString *description =
139    [NSString stringWithFormat:@"<%@:%p\n"
140              "\t productInfo=%@\n>", [self class], self, productInfo_];
141  return description;
142}
143
144@end